blob: d7bee84778b3950721ca21dd26f239afa4c04c4b [file] [log] [blame]
kjellander3e6db232015-11-26 04:44:54 -08001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef MODULES_AUDIO_CODING_INCLUDE_AUDIO_CODING_MODULE_H_
12#define MODULES_AUDIO_CODING_INCLUDE_AUDIO_CODING_MODULE_H_
kjellander3e6db232015-11-26 04:44:54 -080013
kwiberg84be5112016-04-27 01:19:58 -070014#include <memory>
henrik.lundin4cf61dd2015-12-09 06:20:58 -080015#include <string>
Fredrik Solenbergf693bfa2018-12-11 12:22:10 +010016#include <utility>
kjellander3e6db232015-11-26 04:44:54 -080017#include <vector>
18
Danil Chapovalovb6021232018-06-19 13:26:36 +020019#include "absl/types/optional.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "api/audio_codecs/audio_decoder_factory.h"
21#include "api/audio_codecs/audio_encoder.h"
Artem Titov741daaf2019-03-21 14:37:36 +010022#include "api/function_view.h"
Ivo Creusen3ce44a32019-10-31 14:38:11 +010023#include "api/neteq/neteq.h"
Ivo Creusenc3d1f9b2019-11-01 11:47:51 +010024#include "api/neteq/neteq_factory.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020025#include "modules/audio_coding/include/audio_coding_module_typedefs.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020026#include "system_wrappers/include/clock.h"
kjellander3e6db232015-11-26 04:44:54 -080027
28namespace webrtc {
29
30// forward declarations
kjellander3e6db232015-11-26 04:44:54 -080031class AudioDecoder;
32class AudioEncoder;
33class AudioFrame;
Niels Möllerafb5dbb2019-02-15 15:21:47 +010034struct RTPHeader;
kjellander3e6db232015-11-26 04:44:54 -080035
kjellander3e6db232015-11-26 04:44:54 -080036// Callback class used for sending data ready to be packetized
37class AudioPacketizationCallback {
38 public:
39 virtual ~AudioPacketizationCallback() {}
40
Niels Möller87e2d782019-03-07 10:18:23 +010041 virtual int32_t SendData(AudioFrameType frame_type,
kjellander3e6db232015-11-26 04:44:54 -080042 uint8_t payload_type,
43 uint32_t timestamp,
44 const uint8_t* payload_data,
Minyue Liff0e4db2020-01-23 13:45:50 +010045 size_t payload_len_bytes,
46 int64_t absolute_capture_timestamp_ms) {
47 // TODO(bugs.webrtc.org/10739): Deprecate the old SendData and make this one
48 // pure virtual.
49 return SendData(frame_type, payload_type, timestamp, payload_data,
50 payload_len_bytes);
51 }
52 virtual int32_t SendData(AudioFrameType frame_type,
53 uint8_t payload_type,
54 uint32_t timestamp,
55 const uint8_t* payload_data,
56 size_t payload_len_bytes) {
Artem Titovd3251962021-11-15 16:57:07 +010057 RTC_DCHECK_NOTREACHED() << "This method must be overridden, or not used.";
Minyue Liff0e4db2020-01-23 13:45:50 +010058 return -1;
59 }
kjellander3e6db232015-11-26 04:44:54 -080060};
61
kjellander3e6db232015-11-26 04:44:54 -080062class AudioCodingModule {
63 protected:
64 AudioCodingModule() {}
65
66 public:
Henrik Lundin84f75692023-02-01 12:07:10 +000067 // Deprecated. Will be deleted when downlink clients have migrated off it.
kjellander3e6db232015-11-26 04:44:54 -080068 struct Config {
Henrik Lundin84f75692023-02-01 12:07:10 +000069 Config() = default;
70 Config(const Config&) = default;
71 ~Config() = default;
kjellander3e6db232015-11-26 04:44:54 -080072
kjellander3e6db232015-11-26 04:44:54 -080073 NetEq::Config neteq_config;
74 Clock* clock;
ossue3525782016-05-25 07:37:43 -070075 rtc::scoped_refptr<AudioDecoderFactory> decoder_factory;
Ivo Creusenc3d1f9b2019-11-01 11:47:51 +010076 NetEqFactory* neteq_factory = nullptr;
kjellander3e6db232015-11-26 04:44:54 -080077 };
78
Henrik Lundin84f75692023-02-01 12:07:10 +000079 static std::unique_ptr<AudioCodingModule> Create();
80 // Deprecated. Will be deleted when downlink clients have migrated to the
81 // above method.
kjellander3e6db232015-11-26 04:44:54 -080082 static AudioCodingModule* Create(const Config& config);
83 virtual ~AudioCodingModule() = default;
84
Artem Titovd00ce742021-07-28 20:00:17 +020085 // `modifier` is called exactly once with one argument: a pointer to the
kwiberg4cdbd572016-03-30 03:10:05 -070086 // unique_ptr that holds the current encoder (which is null if there is no
Artem Titovd00ce742021-07-28 20:00:17 +020087 // current encoder). For the duration of the call, `modifier` has exclusive
kwiberg4cdbd572016-03-30 03:10:05 -070088 // access to the unique_ptr; it may call the encoder, steal the encoder and
89 // replace it with another encoder or with nullptr, etc.
90 virtual void ModifyEncoder(
kwiberg24c7c122016-09-28 11:57:10 -070091 rtc::FunctionView<void(std::unique_ptr<AudioEncoder>*)> modifier) = 0;
kwiberg4cdbd572016-03-30 03:10:05 -070092
93 // Utility method for simply replacing the existing encoder with a new one.
94 void SetEncoder(std::unique_ptr<AudioEncoder> new_encoder) {
95 ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
96 *encoder = std::move(new_encoder);
97 });
98 }
99
kjellander3e6db232015-11-26 04:44:54 -0800100 // int32_t RegisterTransportCallback()
101 // Register a transport callback which will be called to deliver
102 // the encoded buffers whenever Process() is called and a
103 // bit-stream is ready.
104 //
105 // Input:
106 // -transport : pointer to the callback class
107 // transport->SendData() is called whenever
108 // Process() is called and bit-stream is ready
109 // to deliver.
110 //
111 // Return value:
112 // -1 if the transport callback could not be registered
113 // 0 if registration is successful.
114 //
115 virtual int32_t RegisterTransportCallback(
116 AudioPacketizationCallback* transport) = 0;
117
118 ///////////////////////////////////////////////////////////////////////////
119 // int32_t Add10MsData()
120 // Add 10MS of raw (PCM) audio data and encode it. If the sampling
121 // frequency of the audio does not match the sampling frequency of the
122 // current encoder ACM will resample the audio. If an encoded packet was
123 // produced, it will be delivered via the callback object registered using
124 // RegisterTransportCallback, and the return value from this function will
125 // be the number of bytes encoded.
126 //
127 // Input:
128 // -audio_frame : the input audio frame, containing raw audio
Fredrik Solenbergbbf21a32018-04-12 22:44:09 +0200129 // sampling frequency etc.
kjellander3e6db232015-11-26 04:44:54 -0800130 //
131 // Return value:
132 // >= 0 number of bytes encoded.
133 // -1 some error occurred.
134 //
135 virtual int32_t Add10MsData(const AudioFrame& audio_frame) = 0;
136
137 ///////////////////////////////////////////////////////////////////////////
kjellander3e6db232015-11-26 04:44:54 -0800138 // int SetPacketLossRate()
139 // Sets expected packet loss rate for encoding. Some encoders provide packet
140 // loss gnostic encoding to make stream less sensitive to packet losses,
141 // through e.g., FEC. No effects on codecs that do not provide such encoding.
142 //
143 // Input:
144 // -packet_loss_rate : expected packet loss rate (0 -- 100 inclusive).
145 //
146 // Return value
147 // -1 if failed to set packet loss rate,
148 // 0 if succeeded.
149 //
minyue7e304322016-10-12 05:00:55 -0700150 // This is only used in test code that rely on old ACM APIs.
151 // TODO(minyue): Remove it when possible.
kjellander3e6db232015-11-26 04:44:54 -0800152 virtual int SetPacketLossRate(int packet_loss_rate) = 0;
153
154 ///////////////////////////////////////////////////////////////////////////
kjellander3e6db232015-11-26 04:44:54 -0800155 // statistics
156 //
157
ivoce1198e02017-09-08 08:13:19 -0700158 virtual ANAStats GetANAStats() const = 0;
Jakob Ivarssonbf087452021-11-11 13:43:49 +0100159
160 virtual int GetTargetBitrate() const = 0;
kjellander3e6db232015-11-26 04:44:54 -0800161};
162
163} // namespace webrtc
164
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200165#endif // MODULES_AUDIO_CODING_INCLUDE_AUDIO_CODING_MODULE_H_