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 | c1c9291 | 2014-12-16 13:41:36 +0000 | [diff] [blame] | 15 | #include <vector> |
henrik.lundin@webrtc.org | 9ea6f8a | 2014-10-16 11:26:24 +0000 | [diff] [blame] | 16 | |
henrik.lundin@webrtc.org | 9ea6f8a | 2014-10-16 11:26:24 +0000 | [diff] [blame] | 17 | #include "webrtc/typedefs.h" |
| 18 | |
| 19 | namespace webrtc { |
| 20 | |
| 21 | // This is the interface class for encoders in AudioCoding module. Each codec |
henrik.lundin@webrtc.org | c1c9291 | 2014-12-16 13:41:36 +0000 | [diff] [blame] | 22 | // type must have an implementation of this class. |
henrik.lundin@webrtc.org | 9ea6f8a | 2014-10-16 11:26:24 +0000 | [diff] [blame] | 23 | class AudioEncoder { |
| 24 | public: |
henrik.lundin@webrtc.org | c1c9291 | 2014-12-16 13:41:36 +0000 | [diff] [blame] | 25 | struct EncodedInfoLeaf { |
| 26 | EncodedInfoLeaf() |
henrik.lundin@webrtc.org | bb1219e | 2015-02-12 15:53:25 +0000 | [diff] [blame] | 27 | : encoded_bytes(0), |
| 28 | encoded_timestamp(0), |
| 29 | payload_type(0), |
| 30 | send_even_if_empty(false) {} |
henrik.lundin@webrtc.org | 3b79daf | 2014-12-12 13:31:24 +0000 | [diff] [blame] | 31 | |
| 32 | size_t encoded_bytes; |
henrik.lundin@webrtc.org | 1db20a4 | 2014-12-01 14:44:50 +0000 | [diff] [blame] | 33 | uint32_t encoded_timestamp; |
henrik.lundin@webrtc.org | 7f1dfa5 | 2014-12-02 12:08:39 +0000 | [diff] [blame] | 34 | int payload_type; |
henrik.lundin@webrtc.org | bb1219e | 2015-02-12 15:53:25 +0000 | [diff] [blame] | 35 | bool send_even_if_empty; |
henrik.lundin@webrtc.org | 1db20a4 | 2014-12-01 14:44:50 +0000 | [diff] [blame] | 36 | }; |
| 37 | |
henrik.lundin@webrtc.org | c1c9291 | 2014-12-16 13:41:36 +0000 | [diff] [blame] | 38 | // This is the main struct for auxiliary encoding information. Each encoded |
| 39 | // packet should be accompanied by one EncodedInfo struct, containing the |
| 40 | // total number of |encoded_bytes|, the |encoded_timestamp| and the |
| 41 | // |payload_type|. If the packet contains redundant encodings, the |redundant| |
| 42 | // vector will be populated with EncodedInfoLeaf structs. Each struct in the |
| 43 | // vector represents one encoding; the order of structs in the vector is the |
| 44 | // same as the order in which the actual payloads are written to the byte |
| 45 | // stream. When EncoderInfoLeaf structs are present in the vector, the main |
| 46 | // struct's |encoded_bytes| will be the sum of all the |encoded_bytes| in the |
| 47 | // vector. |
| 48 | struct EncodedInfo : public EncodedInfoLeaf { |
| 49 | EncodedInfo(); |
| 50 | ~EncodedInfo(); |
| 51 | |
| 52 | std::vector<EncodedInfoLeaf> redundant; |
| 53 | }; |
| 54 | |
henrik.lundin@webrtc.org | 9ea6f8a | 2014-10-16 11:26:24 +0000 | [diff] [blame] | 55 | virtual ~AudioEncoder() {} |
| 56 | |
| 57 | // Accepts one 10 ms block of input audio (i.e., sample_rate_hz() / 100 * |
| 58 | // num_channels() samples). Multi-channel audio must be sample-interleaved. |
| 59 | // 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] | 60 | // |encoded|, and provides the number of encoded bytes in |encoded_bytes|. |
| 61 | // In case of error, false is returned, otherwise true. It is an error for the |
| 62 | // encoder to attempt to produce more than |max_encoded_bytes| bytes of |
| 63 | // output. |
henrik.lundin@webrtc.org | 478cedc | 2015-01-27 18:24:45 +0000 | [diff] [blame] | 64 | bool Encode(uint32_t rtp_timestamp, |
henrik.lundin@webrtc.org | def1e97 | 2014-10-21 12:48:29 +0000 | [diff] [blame] | 65 | const int16_t* audio, |
kwiberg@webrtc.org | 663fdd0 | 2014-10-29 07:28:36 +0000 | [diff] [blame] | 66 | size_t num_samples_per_channel, |
henrik.lundin@webrtc.org | def1e97 | 2014-10-21 12:48:29 +0000 | [diff] [blame] | 67 | size_t max_encoded_bytes, |
| 68 | uint8_t* encoded, |
henrik.lundin@webrtc.org | f45c8ca | 2015-02-05 18:29:39 +0000 | [diff] [blame] | 69 | EncodedInfo* info); |
henrik.lundin@webrtc.org | 9ea6f8a | 2014-10-16 11:26:24 +0000 | [diff] [blame] | 70 | |
kwiberg@webrtc.org | decd930 | 2014-10-29 08:38:50 +0000 | [diff] [blame] | 71 | // Return the input sample rate in Hz and the number of input channels. |
| 72 | // These are constants set at instantiation time. |
kwiberg@webrtc.org | 0521127 | 2015-02-18 12:00:32 +0000 | [diff] [blame^] | 73 | virtual int SampleRateHz() const = 0; |
| 74 | virtual int NumChannels() const = 0; |
kwiberg@webrtc.org | decd930 | 2014-10-29 08:38:50 +0000 | [diff] [blame] | 75 | |
henrik.lundin@webrtc.org | 478cedc | 2015-01-27 18:24:45 +0000 | [diff] [blame] | 76 | // Returns the rate with which the RTP timestamps are updated. By default, |
| 77 | // this is the same as sample_rate_hz(). |
kwiberg@webrtc.org | 0521127 | 2015-02-18 12:00:32 +0000 | [diff] [blame^] | 78 | virtual int RtpTimestampRateHz() const; |
henrik.lundin@webrtc.org | 478cedc | 2015-01-27 18:24:45 +0000 | [diff] [blame] | 79 | |
kwiberg@webrtc.org | decd930 | 2014-10-29 08:38:50 +0000 | [diff] [blame] | 80 | // Returns the number of 10 ms frames the encoder will put in the next |
| 81 | // packet. This value may only change when Encode() outputs a packet; i.e., |
| 82 | // the encoder may vary the number of 10 ms frames from packet to packet, but |
| 83 | // it must decide the length of the next packet no later than when outputting |
| 84 | // the preceding packet. |
| 85 | virtual int Num10MsFramesInNextPacket() const = 0; |
henrik.lundin@webrtc.org | 9ea6f8a | 2014-10-16 11:26:24 +0000 | [diff] [blame] | 86 | |
henrik.lundin@webrtc.org | 8911bc5 | 2014-12-08 21:15:55 +0000 | [diff] [blame] | 87 | // Returns the maximum value that can be returned by |
| 88 | // Num10MsFramesInNextPacket(). |
| 89 | virtual int Max10MsFramesInAPacket() const = 0; |
| 90 | |
henrik.lundin@webrtc.org | 478cedc | 2015-01-27 18:24:45 +0000 | [diff] [blame] | 91 | // Changes the target bitrate. The implementation is free to alter this value, |
| 92 | // e.g., if the desired value is outside the valid range. |
| 93 | virtual void SetTargetBitrate(int bits_per_second) {} |
| 94 | |
| 95 | // Tells the implementation what the projected packet loss rate is. The rate |
| 96 | // is in the range [0.0, 1.0]. This rate is typically used to adjust channel |
| 97 | // coding efforts, such as FEC. |
| 98 | virtual void SetProjectedPacketLossRate(double fraction) {} |
| 99 | |
henrik.lundin@webrtc.org | 9ea6f8a | 2014-10-16 11:26:24 +0000 | [diff] [blame] | 100 | protected: |
henrik.lundin@webrtc.org | 478cedc | 2015-01-27 18:24:45 +0000 | [diff] [blame] | 101 | virtual bool EncodeInternal(uint32_t rtp_timestamp, |
henrik.lundin@webrtc.org | 8dc21dc | 2014-12-03 20:36:03 +0000 | [diff] [blame] | 102 | const int16_t* audio, |
| 103 | size_t max_encoded_bytes, |
| 104 | uint8_t* encoded, |
henrik.lundin@webrtc.org | 8dc21dc | 2014-12-03 20:36:03 +0000 | [diff] [blame] | 105 | EncodedInfo* info) = 0; |
henrik.lundin@webrtc.org | 9ea6f8a | 2014-10-16 11:26:24 +0000 | [diff] [blame] | 106 | }; |
| 107 | |
| 108 | } // namespace webrtc |
| 109 | #endif // WEBRTC_MODULES_AUDIO_CODING_CODECS_AUDIO_ENCODER_H_ |