blob: 45c0a855b702a15688fc6b67a2cb53797f94524e [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:
25 virtual ~AudioEncoder() {}
26
27 // Accepts one 10 ms block of input audio (i.e., sample_rate_hz() / 100 *
28 // num_channels() samples). Multi-channel audio must be sample-interleaved.
29 // If successful, the encoder produces zero or more bytes of output in
henrik.lundin@webrtc.orgdef1e972014-10-21 12:48:29 +000030 // |encoded|, and provides the number of encoded bytes in |encoded_bytes|.
31 // In case of error, false is returned, otherwise true. It is an error for the
32 // encoder to attempt to produce more than |max_encoded_bytes| bytes of
33 // output.
34 bool Encode(uint32_t timestamp,
35 const int16_t* audio,
kwiberg@webrtc.org663fdd02014-10-29 07:28:36 +000036 size_t num_samples_per_channel,
henrik.lundin@webrtc.orgdef1e972014-10-21 12:48:29 +000037 size_t max_encoded_bytes,
38 uint8_t* encoded,
39 size_t* encoded_bytes,
40 uint32_t* encoded_timestamp) {
kwiberg@webrtc.org663fdd02014-10-29 07:28:36 +000041 CHECK_EQ(num_samples_per_channel,
42 static_cast<size_t>(sample_rate_hz() / 100));
henrik.lundin@webrtc.orgdef1e972014-10-21 12:48:29 +000043 bool ret = Encode(timestamp,
44 audio,
45 max_encoded_bytes,
46 encoded,
47 encoded_bytes,
48 encoded_timestamp);
49 CHECK_LE(*encoded_bytes, max_encoded_bytes);
50 return ret;
henrik.lundin@webrtc.org9ea6f8a2014-10-16 11:26:24 +000051 }
52
kwiberg@webrtc.orgdecd9302014-10-29 08:38:50 +000053 // Return the input sample rate in Hz and the number of input channels.
54 // These are constants set at instantiation time.
henrik.lundin@webrtc.org9ea6f8a2014-10-16 11:26:24 +000055 virtual int sample_rate_hz() const = 0;
56 virtual int num_channels() const = 0;
kwiberg@webrtc.orgdecd9302014-10-29 08:38:50 +000057
58 // Returns the number of 10 ms frames the encoder will put in the next
59 // packet. This value may only change when Encode() outputs a packet; i.e.,
60 // the encoder may vary the number of 10 ms frames from packet to packet, but
61 // it must decide the length of the next packet no later than when outputting
62 // the preceding packet.
63 virtual int Num10MsFramesInNextPacket() const = 0;
henrik.lundin@webrtc.org9ea6f8a2014-10-16 11:26:24 +000064
65 protected:
henrik.lundin@webrtc.orgdef1e972014-10-21 12:48:29 +000066 virtual bool Encode(uint32_t timestamp,
67 const int16_t* audio,
68 size_t max_encoded_bytes,
69 uint8_t* encoded,
70 size_t* encoded_bytes,
71 uint32_t* encoded_timestamp) = 0;
henrik.lundin@webrtc.org9ea6f8a2014-10-16 11:26:24 +000072};
73
74} // namespace webrtc
75#endif // WEBRTC_MODULES_AUDIO_CODING_CODECS_AUDIO_ENCODER_H_