henrik.lundin@webrtc.org | 9ea6f8a | 2014-10-16 11:26:24 +0000 | [diff] [blame] | 1 | /* |
| 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.org | 9ea6f8a | 2014-10-16 11:26:24 +0000 | [diff] [blame] | 15 | |
| 16 | #include "webrtc/base/checks.h" |
| 17 | #include "webrtc/typedefs.h" |
| 18 | |
| 19 | namespace 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. |
| 23 | class 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.org | def1e97 | 2014-10-21 12:48:29 +0000 | [diff] [blame] | 30 | // |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.org | 663fdd0 | 2014-10-29 07:28:36 +0000 | [diff] [blame] | 36 | size_t num_samples_per_channel, |
henrik.lundin@webrtc.org | def1e97 | 2014-10-21 12:48:29 +0000 | [diff] [blame] | 37 | size_t max_encoded_bytes, |
| 38 | uint8_t* encoded, |
| 39 | size_t* encoded_bytes, |
| 40 | uint32_t* encoded_timestamp) { |
kwiberg@webrtc.org | 663fdd0 | 2014-10-29 07:28:36 +0000 | [diff] [blame] | 41 | CHECK_EQ(num_samples_per_channel, |
| 42 | static_cast<size_t>(sample_rate_hz() / 100)); |
henrik.lundin@webrtc.org | def1e97 | 2014-10-21 12:48:29 +0000 | [diff] [blame] | 43 | 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.org | 9ea6f8a | 2014-10-16 11:26:24 +0000 | [diff] [blame] | 51 | } |
| 52 | |
kwiberg@webrtc.org | decd930 | 2014-10-29 08:38:50 +0000 | [diff] [blame^] | 53 | // Return the input sample rate in Hz and the number of input channels. |
| 54 | // These are constants set at instantiation time. |
henrik.lundin@webrtc.org | 9ea6f8a | 2014-10-16 11:26:24 +0000 | [diff] [blame] | 55 | virtual int sample_rate_hz() const = 0; |
| 56 | virtual int num_channels() const = 0; |
kwiberg@webrtc.org | decd930 | 2014-10-29 08:38:50 +0000 | [diff] [blame^] | 57 | |
| 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.org | 9ea6f8a | 2014-10-16 11:26:24 +0000 | [diff] [blame] | 64 | |
| 65 | protected: |
henrik.lundin@webrtc.org | def1e97 | 2014-10-21 12:48:29 +0000 | [diff] [blame] | 66 | 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.org | 9ea6f8a | 2014-10-16 11:26:24 +0000 | [diff] [blame] | 72 | }; |
| 73 | |
| 74 | } // namespace webrtc |
| 75 | #endif // WEBRTC_MODULES_AUDIO_CODING_CODECS_AUDIO_ENCODER_H_ |