blob: 609ae2c5c1a94df959f668a2b3347c82f506c669 [file] [log] [blame]
Piotr (Peter) Slatalae0c2e972018-10-08 09:43:21 -07001/* Copyright 2018 The WebRTC project authors. All Rights Reserved.
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -04002 *
3 * Use of this source code is governed by a BSD-style license
4 * that can be found in the LICENSE file in the root of the source
5 * tree. An additional intellectual property rights grant can be found
6 * in the file PATENTS. All contributing project authors may
7 * be found in the AUTHORS file in the root of the source tree.
8 */
9
10// This is EXPERIMENTAL interface for media transport.
11//
12// The goal is to refactor WebRTC code so that audio and video frames
13// are sent / received through the media transport interface. This will
14// enable different media transport implementations, including QUIC-based
15// media transport.
16
17#ifndef API_MEDIA_TRANSPORT_INTERFACE_H_
18#define API_MEDIA_TRANSPORT_INTERFACE_H_
19
20#include <memory>
Piotr (Peter) Slatalaa0677d12018-10-29 07:31:42 -070021#include <string>
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -040022#include <utility>
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -040023
Piotr (Peter) Slatalaa0677d12018-10-29 07:31:42 -070024#include "absl/types/optional.h"
Yves Gerey988cc082018-10-23 12:03:01 +020025#include "api/array_view.h"
Bjorn A Mellem05497f22019-08-01 10:48:20 -070026#include "api/data_channel_transport_interface.h"
Steve Anton10542f22019-01-11 09:11:00 -080027#include "api/rtc_error.h"
Niels Möllerec3b9ff2019-02-08 00:28:39 +010028#include "api/transport/media/audio_transport.h"
Niels Möller7e0e44f2019-02-12 14:04:11 +010029#include "api/transport/media/video_transport.h"
Oleh Prypin4d695162019-06-26 10:19:20 +020030#include "api/transport/network_control.h"
Piotr (Peter) Slatala48c54932019-01-28 06:50:38 -080031#include "api/units/data_rate.h"
Niels Möllerd5af4022019-03-05 08:56:48 +010032#include "common_types.h" // NOLINT(build/include)
Steve Anton10542f22019-01-11 09:11:00 -080033#include "rtc_base/copy_on_write_buffer.h"
Steve Anton10542f22019-01-11 09:11:00 -080034#include "rtc_base/network_route.h"
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -040035
36namespace rtc {
37class PacketTransportInternal;
38class Thread;
39} // namespace rtc
40
41namespace webrtc {
42
Anton Sukhanov6b319e62019-05-17 14:48:23 -070043class DatagramTransportInterface;
Piotr (Peter) Slatala0c022502018-12-28 10:39:39 -080044class RtcEventLog;
45
Piotr (Peter) Slatala309aafe2019-01-15 14:24:34 -080046class AudioPacketReceivedObserver {
47 public:
48 virtual ~AudioPacketReceivedObserver() = default;
49
50 // Invoked for the first received audio packet on a given channel id.
51 // It will be invoked once for each channel id.
52 virtual void OnFirstAudioPacketReceived(int64_t channel_id) = 0;
53};
54
Piotr (Peter) Slatala946b9682019-03-18 10:25:02 -070055// Used to configure stream allocations.
Piotr (Peter) Slatala48c54932019-01-28 06:50:38 -080056struct MediaTransportAllocatedBitrateLimits {
57 DataRate min_pacing_rate = DataRate::Zero();
58 DataRate max_padding_bitrate = DataRate::Zero();
59 DataRate max_total_allocated_bitrate = DataRate::Zero();
60};
61
Piotr (Peter) Slatala946b9682019-03-18 10:25:02 -070062// Used to configure target bitrate constraints.
63// If the value is provided, the constraint is updated.
64// If the value is omitted, the value is left unchanged.
65struct MediaTransportTargetRateConstraints {
66 absl::optional<DataRate> min_bitrate;
67 absl::optional<DataRate> max_bitrate;
68 absl::optional<DataRate> starting_bitrate;
69};
70
Piotr (Peter) Slatalaa0677d12018-10-29 07:31:42 -070071// A collection of settings for creation of media transport.
72struct MediaTransportSettings final {
73 MediaTransportSettings();
Piotr (Peter) Slatalaed7b8b12018-10-29 10:43:16 -070074 MediaTransportSettings(const MediaTransportSettings&);
75 MediaTransportSettings& operator=(const MediaTransportSettings&);
Piotr (Peter) Slatalaa0677d12018-10-29 07:31:42 -070076 ~MediaTransportSettings();
77
78 // Group calls are not currently supported, in 1:1 call one side must set
79 // is_caller = true and another is_caller = false.
80 bool is_caller;
81
82 // Must be set if a pre-shared key is used for the call.
Piotr (Peter) Slatala9f956252018-10-31 08:25:26 -070083 // TODO(bugs.webrtc.org/9944): This should become zero buffer in the distant
84 // future.
Piotr (Peter) Slatalaa0677d12018-10-29 07:31:42 -070085 absl::optional<std::string> pre_shared_key;
Piotr (Peter) Slatala0c022502018-12-28 10:39:39 -080086
Piotr (Peter) Slatalad6f61dd2019-02-26 12:08:27 -080087 // If present, this is a config passed from the caller to the answerer in the
88 // offer. Each media transport knows how to understand its own parameters.
89 absl::optional<std::string> remote_transport_parameters;
90
Piotr (Peter) Slatala0c022502018-12-28 10:39:39 -080091 // If present, provides the event log that media transport should use.
92 // Media transport does not own it. The lifetime of |event_log| will exceed
93 // the lifetime of the instance of MediaTransportInterface instance.
94 RtcEventLog* event_log = nullptr;
Piotr (Peter) Slatalaa0677d12018-10-29 07:31:42 -070095};
96
Piotr (Peter) Slatalaada077f2018-11-08 07:43:31 -080097// Callback to notify about network route changes.
98class MediaTransportNetworkChangeCallback {
99 public:
100 virtual ~MediaTransportNetworkChangeCallback() = default;
101
102 // Called when the network route is changed, with the new network route.
103 virtual void OnNetworkRouteChanged(
104 const rtc::NetworkRoute& new_network_route) = 0;
105};
106
Bjorn Mellemc78b0ea2018-10-29 15:21:31 -0700107// State of the media transport. Media transport begins in the pending state.
108// It transitions to writable when it is ready to send media. It may transition
109// back to pending if the connection is blocked. It may transition to closed at
110// any time. Closed is terminal: a transport will never re-open once closed.
111enum class MediaTransportState {
112 kPending,
113 kWritable,
114 kClosed,
115};
116
117// Callback invoked whenever the state of the media transport changes.
118class MediaTransportStateCallback {
119 public:
120 virtual ~MediaTransportStateCallback() = default;
121
122 // Invoked whenever the state of the media transport changes.
123 virtual void OnStateChanged(MediaTransportState state) = 0;
124};
125
Niels Möller46879152019-01-07 15:54:47 +0100126// Callback for RTT measurements on the receive side.
127// TODO(nisse): Related interfaces: CallStatsObserver and RtcpRttStats. It's
128// somewhat unclear what type of measurement is needed. It's used to configure
129// NACK generation and playout buffer. Either raw measurement values or recent
130// maximum would make sense for this use. Need consolidation of RTT signalling.
131class MediaTransportRttObserver {
132 public:
133 virtual ~MediaTransportRttObserver() = default;
134
135 // Invoked when a new RTT measurement is available, typically once per ACK.
136 virtual void OnRttUpdated(int64_t rtt_ms) = 0;
137};
138
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400139// Media transport interface for sending / receiving encoded audio/video frames
140// and receiving bandwidth estimate update from congestion control.
Bjorn A Mellem05497f22019-08-01 10:48:20 -0700141class MediaTransportInterface : public DataChannelTransportInterface {
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400142 public:
Piotr (Peter) Slatala309aafe2019-01-15 14:24:34 -0800143 MediaTransportInterface();
144 virtual ~MediaTransportInterface();
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400145
Piotr (Peter) Slatalad6f61dd2019-02-26 12:08:27 -0800146 // Retrieves callers config (i.e. media transport offer) that should be passed
147 // to the callee, before the call is connected. Such config is opaque to SDP
148 // (sdp just passes it through). The config is a binary blob, so SDP may
149 // choose to use base64 to serialize it (or any other approach that guarantees
150 // that the binary blob goes through). This should only be called for the
151 // caller's perspective.
152 //
153 // This may return an unset optional, which means that the given media
154 // transport is not supported / disabled and shouldn't be reported in SDP.
155 //
156 // It may also return an empty string, in which case the media transport is
157 // supported, but without any extra settings.
158 // TODO(psla): Make abstract.
159 virtual absl::optional<std::string> GetTransportParametersOffer() const;
160
161 // Connect the media transport to the ICE transport.
162 // The implementation must be able to ignore incoming packets that don't
163 // belong to it.
164 // TODO(psla): Make abstract.
165 virtual void Connect(rtc::PacketTransportInternal* packet_transport);
166
Piotr (Peter) Slatalae804f922018-09-25 08:40:30 -0700167 // Start asynchronous send of audio frame. The status returned by this method
168 // only pertains to the synchronous operations (e.g.
169 // serialization/packetization), not to the asynchronous operation.
Sergey Silkine049eba2019-02-18 09:52:26 +0000170
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400171 virtual RTCError SendAudioFrame(uint64_t channel_id,
172 MediaTransportEncodedAudioFrame frame) = 0;
173
Piotr (Peter) Slatalae804f922018-09-25 08:40:30 -0700174 // Start asynchronous send of video frame. The status returned by this method
175 // only pertains to the synchronous operations (e.g.
176 // serialization/packetization), not to the asynchronous operation.
177 virtual RTCError SendVideoFrame(
178 uint64_t channel_id,
179 const MediaTransportEncodedVideoFrame& frame) = 0;
180
Niels Möller1c7f5f62018-12-10 11:06:02 +0100181 // Used by video sender to be notified on key frame requests.
182 virtual void SetKeyFrameRequestCallback(
183 MediaTransportKeyFrameRequestCallback* callback);
184
Piotr (Peter) Slatalae804f922018-09-25 08:40:30 -0700185 // Requests a keyframe for the particular channel (stream). The caller should
186 // check that the keyframe is not present in a jitter buffer already (i.e.
187 // don't request a keyframe if there is one that you will get from the jitter
188 // buffer in a moment).
189 virtual RTCError RequestKeyFrame(uint64_t channel_id) = 0;
190
191 // Sets audio sink. Sink must be unset by calling SetReceiveAudioSink(nullptr)
192 // before the media transport is destroyed or before new sink is set.
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400193 virtual void SetReceiveAudioSink(MediaTransportAudioSinkInterface* sink) = 0;
194
Piotr (Peter) Slatalae804f922018-09-25 08:40:30 -0700195 // Registers a video sink. Before destruction of media transport, you must
196 // pass a nullptr.
197 virtual void SetReceiveVideoSink(MediaTransportVideoSinkInterface* sink) = 0;
198
Piotr (Peter) Slatalaada077f2018-11-08 07:43:31 -0800199 // Adds a target bitrate observer. Before media transport is destructed
200 // the observer must be unregistered (by calling
201 // RemoveTargetTransferRateObserver).
202 // A newly registered observer will be called back with the latest recorded
203 // target rate, if available.
204 virtual void AddTargetTransferRateObserver(
Niels Möller46879152019-01-07 15:54:47 +0100205 TargetTransferRateObserver* observer);
Piotr (Peter) Slatalaada077f2018-11-08 07:43:31 -0800206
207 // Removes an existing |observer| from observers. If observer was never
208 // registered, an error is logged and method does nothing.
209 virtual void RemoveTargetTransferRateObserver(
Niels Möller46879152019-01-07 15:54:47 +0100210 TargetTransferRateObserver* observer);
211
Piotr (Peter) Slatala309aafe2019-01-15 14:24:34 -0800212 // Sets audio packets observer, which gets informed about incoming audio
213 // packets. Before destruction, the observer must be unregistered by setting
214 // nullptr.
215 //
216 // This method may be temporary, when the multiplexer is implemented (or
217 // multiplexer may use it to demultiplex channel ids).
218 virtual void SetFirstAudioPacketReceivedObserver(
219 AudioPacketReceivedObserver* observer);
220
Niels Möller46879152019-01-07 15:54:47 +0100221 // Intended for receive side. AddRttObserver registers an observer to be
222 // called for each RTT measurement, typically once per ACK. Before media
223 // transport is destructed the observer must be unregistered.
224 virtual void AddRttObserver(MediaTransportRttObserver* observer);
225 virtual void RemoveRttObserver(MediaTransportRttObserver* observer);
Piotr (Peter) Slatalaada077f2018-11-08 07:43:31 -0800226
227 // Returns the last known target transfer rate as reported to the above
228 // observers.
229 virtual absl::optional<TargetTransferRate> GetLatestTargetTransferRate();
230
231 // Gets the audio packet overhead in bytes. Returned overhead does not include
232 // transport overhead (ipv4/6, turn channeldata, tcp/udp, etc.).
233 // If the transport is capable of fusing packets together, this overhead
234 // might not be a very accurate number.
Niels Möllerd5af4022019-03-05 08:56:48 +0100235 // TODO(nisse): Deprecated.
Piotr (Peter) Slatalaada077f2018-11-08 07:43:31 -0800236 virtual size_t GetAudioPacketOverhead() const;
237
Niels Möllerd5af4022019-03-05 08:56:48 +0100238 // Corresponding observers for audio and video overhead. Before destruction,
239 // the observers must be unregistered by setting nullptr.
240
241 // TODO(nisse): Should move to per-stream objects, since packetization
242 // overhead can vary per stream, e.g., depending on negotiated extensions. In
243 // addition, we should move towards reporting total overhead including all
244 // layers. Currently, overhead of the lower layers is reported elsewhere,
245 // e.g., on route change between IPv4 and IPv6.
246 virtual void SetAudioOverheadObserver(OverheadObserver* observer) {}
247
Niels Möllerd70a1142019-02-06 17:36:29 +0100248 // Registers an observer for network change events. If the network route is
249 // already established when the callback is added, |callback| will be called
250 // immediately with the current network route. Before media transport is
251 // destroyed, the callback must be removed.
Niels Möller30b182a2019-02-05 00:59:35 +0100252 virtual void AddNetworkChangeCallback(
253 MediaTransportNetworkChangeCallback* callback);
254 virtual void RemoveNetworkChangeCallback(
255 MediaTransportNetworkChangeCallback* callback);
Piotr (Peter) Slatala6b9d8232018-10-26 07:59:46 -0700256
Bjorn Mellemc78b0ea2018-10-29 15:21:31 -0700257 // Sets a state observer callback. Before media transport is destroyed, the
258 // callback must be unregistered by setting it to nullptr.
259 // A newly registered callback will be called with the current state.
260 // Media transport does not invoke this callback concurrently.
Bjorn Mellemc78b0ea2018-10-29 15:21:31 -0700261 virtual void SetMediaTransportStateCallback(
Bjorn Mellemeb2c6412018-10-31 15:25:32 -0700262 MediaTransportStateCallback* callback) = 0;
Bjorn Mellemc78b0ea2018-10-29 15:21:31 -0700263
Piotr (Peter) Slatala48c54932019-01-28 06:50:38 -0800264 // Updates allocation limits.
265 // TODO(psla): Make abstract when downstream implementation implement it.
266 virtual void SetAllocatedBitrateLimits(
267 const MediaTransportAllocatedBitrateLimits& limits);
268
Piotr (Peter) Slatala946b9682019-03-18 10:25:02 -0700269 // Sets starting rate.
270 // TODO(psla): Make abstract when downstream implementation implement it.
271 virtual void SetTargetBitrateLimits(
272 const MediaTransportTargetRateConstraints& target_rate_constraints) {}
273
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400274 // TODO(sukhanov): RtcEventLogs.
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400275};
276
277// If media transport factory is set in peer connection factory, it will be
278// used to create media transport for sending/receiving encoded frames and
279// this transport will be used instead of default RTP/SRTP transport.
280//
281// Currently Media Transport negotiation is not supported in SDP.
282// If application is using media transport, it must negotiate it before
283// setting media transport factory in peer connection.
284class MediaTransportFactory {
285 public:
286 virtual ~MediaTransportFactory() = default;
287
288 // Creates media transport.
289 // - Does not take ownership of packet_transport or network_thread.
290 // - Does not support group calls, in 1:1 call one side must set
291 // is_caller = true and another is_caller = false.
Piotr (Peter) Slatalaa0677d12018-10-29 07:31:42 -0700292 virtual RTCErrorOr<std::unique_ptr<MediaTransportInterface>>
293 CreateMediaTransport(rtc::PacketTransportInternal* packet_transport,
294 rtc::Thread* network_thread,
Piotr (Peter) Slatalaed7b8b12018-10-29 10:43:16 -0700295 const MediaTransportSettings& settings);
Piotr (Peter) Slatalad6f61dd2019-02-26 12:08:27 -0800296
297 // Creates a new Media Transport in a disconnected state. If the media
298 // transport for the caller is created, one can then call
299 // MediaTransportInterface::GetTransportParametersOffer on that new instance.
300 // TODO(psla): Make abstract.
301 virtual RTCErrorOr<std::unique_ptr<webrtc::MediaTransportInterface>>
302 CreateMediaTransport(rtc::Thread* network_thread,
303 const MediaTransportSettings& settings);
304
Anton Sukhanov6b319e62019-05-17 14:48:23 -0700305 // Creates a new Datagram Transport in a disconnected state. If the datagram
306 // transport for the caller is created, one can then call
307 // DatagramTransportInterface::GetTransportParametersOffer on that new
308 // instance.
309 //
310 // TODO(sukhanov): Consider separating media and datagram transport factories.
311 // TODO(sukhanov): Move factory to a separate .h file.
312 virtual RTCErrorOr<std::unique_ptr<DatagramTransportInterface>>
313 CreateDatagramTransport(rtc::Thread* network_thread,
314 const MediaTransportSettings& settings);
315
Piotr (Peter) Slatalad6f61dd2019-02-26 12:08:27 -0800316 // Gets a transport name which is supported by the implementation.
317 // Different factories should return different transport names, and at runtime
318 // it will be checked that different names were used.
319 // For example, "rtp" or "generic" may be returned by two different
320 // implementations.
321 // The value returned by this method must never change in the lifetime of the
322 // factory.
323 // TODO(psla): Make abstract.
324 virtual std::string GetTransportName() const;
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400325};
326
327} // namespace webrtc
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400328#endif // API_MEDIA_TRANSPORT_INTERFACE_H_