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 | |
| 53 | // Returns the input sample rate in Hz, the number of input channels, and the |
| 54 | // number of 10 ms frames the encoder puts in one output packet. These are |
| 55 | // constants set at instantiation time. |
| 56 | virtual int sample_rate_hz() const = 0; |
| 57 | virtual int num_channels() const = 0; |
| 58 | virtual int num_10ms_frames_per_packet() const = 0; |
| 59 | |
| 60 | protected: |
henrik.lundin@webrtc.org | def1e97 | 2014-10-21 12:48:29 +0000 | [diff] [blame] | 61 | virtual bool Encode(uint32_t timestamp, |
| 62 | const int16_t* audio, |
| 63 | size_t max_encoded_bytes, |
| 64 | uint8_t* encoded, |
| 65 | size_t* encoded_bytes, |
| 66 | uint32_t* encoded_timestamp) = 0; |
henrik.lundin@webrtc.org | 9ea6f8a | 2014-10-16 11:26:24 +0000 | [diff] [blame] | 67 | }; |
| 68 | |
| 69 | } // namespace webrtc |
| 70 | #endif // WEBRTC_MODULES_AUDIO_CODING_CODECS_AUDIO_ENCODER_H_ |