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> |
| 15 | #include <limits> |
| 16 | |
| 17 | #include "webrtc/base/checks.h" |
| 18 | #include "webrtc/typedefs.h" |
| 19 | |
| 20 | namespace webrtc { |
| 21 | |
| 22 | // This is the interface class for encoders in AudioCoding module. Each codec |
| 23 | // codec type must have an implementation of this class. |
| 24 | class AudioEncoder { |
| 25 | public: |
| 26 | virtual ~AudioEncoder() {} |
| 27 | |
| 28 | // Accepts one 10 ms block of input audio (i.e., sample_rate_hz() / 100 * |
| 29 | // num_channels() samples). Multi-channel audio must be sample-interleaved. |
| 30 | // If successful, the encoder produces zero or more bytes of output in |
| 31 | // |encoded|, and returns the number of bytes. In case of error, -1 is |
| 32 | // returned. It is an error for the encoder to attempt to produce more than |
| 33 | // |max_encoded_bytes| bytes of output. |
| 34 | ssize_t Encode(uint32_t timestamp, |
| 35 | const int16_t* audio, |
| 36 | size_t num_samples, |
| 37 | size_t max_encoded_bytes, |
| 38 | uint8_t* encoded) { |
| 39 | CHECK_EQ(num_samples, |
| 40 | static_cast<size_t>(sample_rate_hz() / 100 * num_channels())); |
| 41 | ssize_t num_bytes = Encode(timestamp, audio, max_encoded_bytes, encoded); |
| 42 | CHECK_LE(num_bytes, |
| 43 | static_cast<ssize_t>(std::min( |
| 44 | max_encoded_bytes, |
| 45 | static_cast<size_t>(std::numeric_limits<ssize_t>::max())))); |
| 46 | return num_bytes; |
| 47 | } |
| 48 | |
| 49 | // Returns the input sample rate in Hz, the number of input channels, and the |
| 50 | // number of 10 ms frames the encoder puts in one output packet. These are |
| 51 | // constants set at instantiation time. |
| 52 | virtual int sample_rate_hz() const = 0; |
| 53 | virtual int num_channels() const = 0; |
| 54 | virtual int num_10ms_frames_per_packet() const = 0; |
| 55 | |
| 56 | protected: |
| 57 | virtual ssize_t Encode(uint32_t timestamp, |
| 58 | const int16_t* audio, |
| 59 | size_t max_encoded_bytes, |
| 60 | uint8_t* encoded) = 0; |
| 61 | }; |
| 62 | |
| 63 | } // namespace webrtc |
| 64 | #endif // WEBRTC_MODULES_AUDIO_CODING_CODECS_AUDIO_ENCODER_H_ |