kjellander | 3e6db23 | 2015-11-26 12:44:54 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2012 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 11 | #ifndef MODULES_AUDIO_CODING_INCLUDE_AUDIO_CODING_MODULE_H_ |
| 12 | #define MODULES_AUDIO_CODING_INCLUDE_AUDIO_CODING_MODULE_H_ |
kjellander | 3e6db23 | 2015-11-26 12:44:54 | [diff] [blame] | 13 | |
kwiberg | 84be511 | 2016-04-27 08:19:58 | [diff] [blame] | 14 | #include <memory> |
henrik.lundin | 4cf61dd | 2015-12-09 14:20:58 | [diff] [blame] | 15 | #include <string> |
Fredrik Solenberg | f693bfa | 2018-12-11 11:22:10 | [diff] [blame] | 16 | #include <utility> |
kjellander | 3e6db23 | 2015-11-26 12:44:54 | [diff] [blame] | 17 | #include <vector> |
| 18 | |
Danil Chapovalov | b602123 | 2018-06-19 11:26:36 | [diff] [blame] | 19 | #include "absl/types/optional.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 20 | #include "api/audio_codecs/audio_encoder.h" |
Artem Titov | 741daaf | 2019-03-21 13:37:36 | [diff] [blame] | 21 | #include "api/function_view.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 22 | #include "modules/audio_coding/include/audio_coding_module_typedefs.h" |
kjellander | 3e6db23 | 2015-11-26 12:44:54 | [diff] [blame] | 23 | |
| 24 | namespace webrtc { |
| 25 | |
| 26 | // forward declarations |
kjellander | 3e6db23 | 2015-11-26 12:44:54 | [diff] [blame] | 27 | class AudioDecoder; |
| 28 | class AudioEncoder; |
| 29 | class AudioFrame; |
Niels Möller | afb5dbb | 2019-02-15 14:21:47 | [diff] [blame] | 30 | struct RTPHeader; |
kjellander | 3e6db23 | 2015-11-26 12:44:54 | [diff] [blame] | 31 | |
kjellander | 3e6db23 | 2015-11-26 12:44:54 | [diff] [blame] | 32 | // Callback class used for sending data ready to be packetized |
| 33 | class AudioPacketizationCallback { |
| 34 | public: |
| 35 | virtual ~AudioPacketizationCallback() {} |
| 36 | |
Niels Möller | 87e2d78 | 2019-03-07 09:18:23 | [diff] [blame] | 37 | virtual int32_t SendData(AudioFrameType frame_type, |
kjellander | 3e6db23 | 2015-11-26 12:44:54 | [diff] [blame] | 38 | uint8_t payload_type, |
| 39 | uint32_t timestamp, |
| 40 | const uint8_t* payload_data, |
Minyue Li | ff0e4db | 2020-01-23 12:45:50 | [diff] [blame] | 41 | size_t payload_len_bytes, |
| 42 | int64_t absolute_capture_timestamp_ms) { |
| 43 | // TODO(bugs.webrtc.org/10739): Deprecate the old SendData and make this one |
| 44 | // pure virtual. |
| 45 | return SendData(frame_type, payload_type, timestamp, payload_data, |
| 46 | payload_len_bytes); |
| 47 | } |
| 48 | virtual int32_t SendData(AudioFrameType frame_type, |
| 49 | uint8_t payload_type, |
| 50 | uint32_t timestamp, |
| 51 | const uint8_t* payload_data, |
| 52 | size_t payload_len_bytes) { |
Artem Titov | d325196 | 2021-11-15 15:57:07 | [diff] [blame] | 53 | RTC_DCHECK_NOTREACHED() << "This method must be overridden, or not used."; |
Minyue Li | ff0e4db | 2020-01-23 12:45:50 | [diff] [blame] | 54 | return -1; |
| 55 | } |
kjellander | 3e6db23 | 2015-11-26 12:44:54 | [diff] [blame] | 56 | }; |
| 57 | |
kjellander | 3e6db23 | 2015-11-26 12:44:54 | [diff] [blame] | 58 | class AudioCodingModule { |
| 59 | protected: |
| 60 | AudioCodingModule() {} |
| 61 | |
| 62 | public: |
Henrik Lundin | 84f7569 | 2023-02-01 12:07:10 | [diff] [blame] | 63 | static std::unique_ptr<AudioCodingModule> Create(); |
kjellander | 3e6db23 | 2015-11-26 12:44:54 | [diff] [blame] | 64 | virtual ~AudioCodingModule() = default; |
| 65 | |
Artem Titov | d00ce74 | 2021-07-28 18:00:17 | [diff] [blame] | 66 | // `modifier` is called exactly once with one argument: a pointer to the |
kwiberg | 4cdbd57 | 2016-03-30 10:10:05 | [diff] [blame] | 67 | // unique_ptr that holds the current encoder (which is null if there is no |
Artem Titov | d00ce74 | 2021-07-28 18:00:17 | [diff] [blame] | 68 | // current encoder). For the duration of the call, `modifier` has exclusive |
kwiberg | 4cdbd57 | 2016-03-30 10:10:05 | [diff] [blame] | 69 | // access to the unique_ptr; it may call the encoder, steal the encoder and |
| 70 | // replace it with another encoder or with nullptr, etc. |
| 71 | virtual void ModifyEncoder( |
kwiberg | 24c7c12 | 2016-09-28 18:57:10 | [diff] [blame] | 72 | rtc::FunctionView<void(std::unique_ptr<AudioEncoder>*)> modifier) = 0; |
kwiberg | 4cdbd57 | 2016-03-30 10:10:05 | [diff] [blame] | 73 | |
| 74 | // Utility method for simply replacing the existing encoder with a new one. |
| 75 | void SetEncoder(std::unique_ptr<AudioEncoder> new_encoder) { |
| 76 | ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) { |
| 77 | *encoder = std::move(new_encoder); |
| 78 | }); |
| 79 | } |
| 80 | |
kjellander | 3e6db23 | 2015-11-26 12:44:54 | [diff] [blame] | 81 | // int32_t RegisterTransportCallback() |
| 82 | // Register a transport callback which will be called to deliver |
| 83 | // the encoded buffers whenever Process() is called and a |
| 84 | // bit-stream is ready. |
| 85 | // |
| 86 | // Input: |
| 87 | // -transport : pointer to the callback class |
| 88 | // transport->SendData() is called whenever |
| 89 | // Process() is called and bit-stream is ready |
| 90 | // to deliver. |
| 91 | // |
| 92 | // Return value: |
| 93 | // -1 if the transport callback could not be registered |
| 94 | // 0 if registration is successful. |
| 95 | // |
| 96 | virtual int32_t RegisterTransportCallback( |
| 97 | AudioPacketizationCallback* transport) = 0; |
| 98 | |
| 99 | /////////////////////////////////////////////////////////////////////////// |
| 100 | // int32_t Add10MsData() |
| 101 | // Add 10MS of raw (PCM) audio data and encode it. If the sampling |
| 102 | // frequency of the audio does not match the sampling frequency of the |
| 103 | // current encoder ACM will resample the audio. If an encoded packet was |
| 104 | // produced, it will be delivered via the callback object registered using |
| 105 | // RegisterTransportCallback, and the return value from this function will |
| 106 | // be the number of bytes encoded. |
| 107 | // |
| 108 | // Input: |
| 109 | // -audio_frame : the input audio frame, containing raw audio |
Fredrik Solenberg | bbf21a3 | 2018-04-12 20:44:09 | [diff] [blame] | 110 | // sampling frequency etc. |
kjellander | 3e6db23 | 2015-11-26 12:44:54 | [diff] [blame] | 111 | // |
| 112 | // Return value: |
| 113 | // >= 0 number of bytes encoded. |
| 114 | // -1 some error occurred. |
| 115 | // |
| 116 | virtual int32_t Add10MsData(const AudioFrame& audio_frame) = 0; |
| 117 | |
| 118 | /////////////////////////////////////////////////////////////////////////// |
kjellander | 3e6db23 | 2015-11-26 12:44:54 | [diff] [blame] | 119 | // int SetPacketLossRate() |
| 120 | // Sets expected packet loss rate for encoding. Some encoders provide packet |
| 121 | // loss gnostic encoding to make stream less sensitive to packet losses, |
| 122 | // through e.g., FEC. No effects on codecs that do not provide such encoding. |
| 123 | // |
| 124 | // Input: |
| 125 | // -packet_loss_rate : expected packet loss rate (0 -- 100 inclusive). |
| 126 | // |
| 127 | // Return value |
| 128 | // -1 if failed to set packet loss rate, |
| 129 | // 0 if succeeded. |
| 130 | // |
minyue | 7e30432 | 2016-10-12 12:00:55 | [diff] [blame] | 131 | // This is only used in test code that rely on old ACM APIs. |
| 132 | // TODO(minyue): Remove it when possible. |
kjellander | 3e6db23 | 2015-11-26 12:44:54 | [diff] [blame] | 133 | virtual int SetPacketLossRate(int packet_loss_rate) = 0; |
| 134 | |
| 135 | /////////////////////////////////////////////////////////////////////////// |
kjellander | 3e6db23 | 2015-11-26 12:44:54 | [diff] [blame] | 136 | // statistics |
| 137 | // |
| 138 | |
ivoc | e1198e0 | 2017-09-08 15:13:19 | [diff] [blame] | 139 | virtual ANAStats GetANAStats() const = 0; |
Jakob Ivarsson | bf08745 | 2021-11-11 12:43:49 | [diff] [blame] | 140 | |
| 141 | virtual int GetTargetBitrate() const = 0; |
kjellander | 3e6db23 | 2015-11-26 12:44:54 | [diff] [blame] | 142 | }; |
| 143 | |
| 144 | } // namespace webrtc |
| 145 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 146 | #endif // MODULES_AUDIO_CODING_INCLUDE_AUDIO_CODING_MODULE_H_ |