blob: 5c5bd167c032af10ceb894c23532d2004e73e60c [file] [log] [blame]
kjellander3e6db232015-11-26 12:44:541/*
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 04:47:3111#ifndef MODULES_AUDIO_CODING_INCLUDE_AUDIO_CODING_MODULE_H_
12#define MODULES_AUDIO_CODING_INCLUDE_AUDIO_CODING_MODULE_H_
kjellander3e6db232015-11-26 12:44:5413
kwiberg84be5112016-04-27 08:19:5814#include <memory>
henrik.lundin4cf61dd2015-12-09 14:20:5815#include <string>
Fredrik Solenbergf693bfa2018-12-11 11:22:1016#include <utility>
kjellander3e6db232015-11-26 12:44:5417#include <vector>
18
Danil Chapovalovb6021232018-06-19 11:26:3619#include "absl/types/optional.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3120#include "api/audio_codecs/audio_encoder.h"
Artem Titov741daaf2019-03-21 13:37:3621#include "api/function_view.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3122#include "modules/audio_coding/include/audio_coding_module_typedefs.h"
kjellander3e6db232015-11-26 12:44:5423
24namespace webrtc {
25
26// forward declarations
kjellander3e6db232015-11-26 12:44:5427class AudioDecoder;
28class AudioEncoder;
29class AudioFrame;
Niels Möllerafb5dbb2019-02-15 14:21:4730struct RTPHeader;
kjellander3e6db232015-11-26 12:44:5431
kjellander3e6db232015-11-26 12:44:5432// Callback class used for sending data ready to be packetized
33class AudioPacketizationCallback {
34 public:
35 virtual ~AudioPacketizationCallback() {}
36
Niels Möller87e2d782019-03-07 09:18:2337 virtual int32_t SendData(AudioFrameType frame_type,
kjellander3e6db232015-11-26 12:44:5438 uint8_t payload_type,
39 uint32_t timestamp,
40 const uint8_t* payload_data,
Minyue Liff0e4db2020-01-23 12:45:5041 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 Titovd3251962021-11-15 15:57:0753 RTC_DCHECK_NOTREACHED() << "This method must be overridden, or not used.";
Minyue Liff0e4db2020-01-23 12:45:5054 return -1;
55 }
kjellander3e6db232015-11-26 12:44:5456};
57
kjellander3e6db232015-11-26 12:44:5458class AudioCodingModule {
59 protected:
60 AudioCodingModule() {}
61
62 public:
Henrik Lundin84f75692023-02-01 12:07:1063 static std::unique_ptr<AudioCodingModule> Create();
kjellander3e6db232015-11-26 12:44:5464 virtual ~AudioCodingModule() = default;
65
Artem Titovd00ce742021-07-28 18:00:1766 // `modifier` is called exactly once with one argument: a pointer to the
kwiberg4cdbd572016-03-30 10:10:0567 // unique_ptr that holds the current encoder (which is null if there is no
Artem Titovd00ce742021-07-28 18:00:1768 // current encoder). For the duration of the call, `modifier` has exclusive
kwiberg4cdbd572016-03-30 10:10:0569 // 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(
kwiberg24c7c122016-09-28 18:57:1072 rtc::FunctionView<void(std::unique_ptr<AudioEncoder>*)> modifier) = 0;
kwiberg4cdbd572016-03-30 10:10:0573
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
kjellander3e6db232015-11-26 12:44:5481 // 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 Solenbergbbf21a32018-04-12 20:44:09110 // sampling frequency etc.
kjellander3e6db232015-11-26 12:44:54111 //
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 ///////////////////////////////////////////////////////////////////////////
kjellander3e6db232015-11-26 12:44:54119 // 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 //
minyue7e304322016-10-12 12:00:55131 // This is only used in test code that rely on old ACM APIs.
132 // TODO(minyue): Remove it when possible.
kjellander3e6db232015-11-26 12:44:54133 virtual int SetPacketLossRate(int packet_loss_rate) = 0;
134
135 ///////////////////////////////////////////////////////////////////////////
kjellander3e6db232015-11-26 12:44:54136 // statistics
137 //
138
ivoce1198e02017-09-08 15:13:19139 virtual ANAStats GetANAStats() const = 0;
Jakob Ivarssonbf087452021-11-11 12:43:49140
141 virtual int GetTargetBitrate() const = 0;
kjellander3e6db232015-11-26 12:44:54142};
143
144} // namespace webrtc
145
Mirko Bonadei92ea95e2017-09-15 04:47:31146#endif // MODULES_AUDIO_CODING_INCLUDE_AUDIO_CODING_MODULE_H_