blob: c0790a263552fc35243dc829af7ac7a2fbc1a520 [file] [log] [blame]
henrik.lundin@webrtc.org9ea6f8a2014-10-16 11:26:24 +00001/*
2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11#ifndef WEBRTC_MODULES_AUDIO_CODING_CODECS_AUDIO_ENCODER_H_
12#define WEBRTC_MODULES_AUDIO_CODING_CODECS_AUDIO_ENCODER_H_
13
14#include <algorithm>
henrik.lundin@webrtc.org9ea6f8a2014-10-16 11:26:24 +000015
16#include "webrtc/base/checks.h"
17#include "webrtc/typedefs.h"
18
19namespace webrtc {
20
21// This is the interface class for encoders in AudioCoding module. Each codec
22// codec type must have an implementation of this class.
23class AudioEncoder {
24 public:
henrik.lundin@webrtc.org1db20a42014-12-01 14:44:50 +000025 struct EncodedInfo {
26 uint32_t encoded_timestamp;
henrik.lundin@webrtc.org7f1dfa52014-12-02 12:08:39 +000027 int payload_type;
henrik.lundin@webrtc.org1db20a42014-12-01 14:44:50 +000028 };
29
henrik.lundin@webrtc.org9ea6f8a2014-10-16 11:26:24 +000030 virtual ~AudioEncoder() {}
31
32 // Accepts one 10 ms block of input audio (i.e., sample_rate_hz() / 100 *
33 // num_channels() samples). Multi-channel audio must be sample-interleaved.
34 // If successful, the encoder produces zero or more bytes of output in
henrik.lundin@webrtc.orgdef1e972014-10-21 12:48:29 +000035 // |encoded|, and provides the number of encoded bytes in |encoded_bytes|.
36 // In case of error, false is returned, otherwise true. It is an error for the
37 // encoder to attempt to produce more than |max_encoded_bytes| bytes of
38 // output.
39 bool Encode(uint32_t timestamp,
40 const int16_t* audio,
kwiberg@webrtc.org663fdd02014-10-29 07:28:36 +000041 size_t num_samples_per_channel,
henrik.lundin@webrtc.orgdef1e972014-10-21 12:48:29 +000042 size_t max_encoded_bytes,
43 uint8_t* encoded,
44 size_t* encoded_bytes,
henrik.lundin@webrtc.org1db20a42014-12-01 14:44:50 +000045 EncodedInfo* info) {
kwiberg@webrtc.org663fdd02014-10-29 07:28:36 +000046 CHECK_EQ(num_samples_per_channel,
47 static_cast<size_t>(sample_rate_hz() / 100));
henrik.lundin@webrtc.orgdef1e972014-10-21 12:48:29 +000048 bool ret = Encode(timestamp,
49 audio,
50 max_encoded_bytes,
51 encoded,
52 encoded_bytes,
henrik.lundin@webrtc.org1db20a42014-12-01 14:44:50 +000053 info);
henrik.lundin@webrtc.orgdef1e972014-10-21 12:48:29 +000054 CHECK_LE(*encoded_bytes, max_encoded_bytes);
55 return ret;
henrik.lundin@webrtc.org9ea6f8a2014-10-16 11:26:24 +000056 }
57
kwiberg@webrtc.orgdecd9302014-10-29 08:38:50 +000058 // Return the input sample rate in Hz and the number of input channels.
59 // These are constants set at instantiation time.
henrik.lundin@webrtc.org9ea6f8a2014-10-16 11:26:24 +000060 virtual int sample_rate_hz() const = 0;
61 virtual int num_channels() const = 0;
kwiberg@webrtc.orgdecd9302014-10-29 08:38:50 +000062
63 // Returns the number of 10 ms frames the encoder will put in the next
64 // packet. This value may only change when Encode() outputs a packet; i.e.,
65 // the encoder may vary the number of 10 ms frames from packet to packet, but
66 // it must decide the length of the next packet no later than when outputting
67 // the preceding packet.
68 virtual int Num10MsFramesInNextPacket() const = 0;
henrik.lundin@webrtc.org9ea6f8a2014-10-16 11:26:24 +000069
70 protected:
henrik.lundin@webrtc.orgdef1e972014-10-21 12:48:29 +000071 virtual bool Encode(uint32_t timestamp,
72 const int16_t* audio,
73 size_t max_encoded_bytes,
74 uint8_t* encoded,
75 size_t* encoded_bytes,
henrik.lundin@webrtc.org1db20a42014-12-01 14:44:50 +000076 EncodedInfo* info) = 0;
henrik.lundin@webrtc.org9ea6f8a2014-10-16 11:26:24 +000077};
78
79} // namespace webrtc
80#endif // WEBRTC_MODULES_AUDIO_CODING_CODECS_AUDIO_ENCODER_H_