blob: 7570160cc2965afca79e7412e528036971a1bea8 [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
Piotr (Peter) Slatala6b9d8232018-10-26 07:59:46 -070020#include <api/transport/network_control.h>
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -040021#include <memory>
Piotr (Peter) Slatalaa0677d12018-10-29 07:31:42 -070022#include <string>
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -040023#include <utility>
24#include <vector>
25
Piotr (Peter) Slatalaa0677d12018-10-29 07:31:42 -070026#include "absl/types/optional.h"
Yves Gerey988cc082018-10-23 12:03:01 +020027#include "api/array_view.h"
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -040028#include "api/rtcerror.h"
Niels Möller3a742392018-10-08 11:13:58 +020029#include "api/video/encoded_image.h"
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -040030#include "common_types.h" // NOLINT(build/include)
Bjorn Mellem1f6aa9f2018-10-30 15:15:00 -070031#include "rtc_base/copyonwritebuffer.h"
Piotr (Peter) Slatalaada077f2018-11-08 07:43:31 -080032#include "rtc_base/networkroute.h"
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -040033
34namespace rtc {
35class PacketTransportInternal;
36class Thread;
37} // namespace rtc
38
39namespace webrtc {
40
Piotr (Peter) Slatalaa0677d12018-10-29 07:31:42 -070041// A collection of settings for creation of media transport.
42struct MediaTransportSettings final {
43 MediaTransportSettings();
Piotr (Peter) Slatalaed7b8b12018-10-29 10:43:16 -070044 MediaTransportSettings(const MediaTransportSettings&);
45 MediaTransportSettings& operator=(const MediaTransportSettings&);
Piotr (Peter) Slatalaa0677d12018-10-29 07:31:42 -070046 ~MediaTransportSettings();
47
48 // Group calls are not currently supported, in 1:1 call one side must set
49 // is_caller = true and another is_caller = false.
50 bool is_caller;
51
52 // Must be set if a pre-shared key is used for the call.
Piotr (Peter) Slatala9f956252018-10-31 08:25:26 -070053 // TODO(bugs.webrtc.org/9944): This should become zero buffer in the distant
54 // future.
Piotr (Peter) Slatalaa0677d12018-10-29 07:31:42 -070055 absl::optional<std::string> pre_shared_key;
56};
57
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -040058// Represents encoded audio frame in any encoding (type of encoding is opaque).
59// To avoid copying of encoded data use move semantics when passing by value.
Piotr (Peter) Slatalae0c2e972018-10-08 09:43:21 -070060class MediaTransportEncodedAudioFrame final {
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -040061 public:
62 enum class FrameType {
63 // Normal audio frame (equivalent to webrtc::kAudioFrameSpeech).
64 kSpeech,
65
66 // DTX frame (equivalent to webrtc::kAudioFrameCN).
Niels Möller7d76a312018-10-26 12:57:07 +020067 // DTX frame (equivalent to webrtc::kAudioFrameCN).
68 kDiscontinuousTransmission,
69 // TODO(nisse): Mis-spelled version, update users, then delete.
70 kDiscountinuousTransmission = kDiscontinuousTransmission,
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -040071 };
72
73 MediaTransportEncodedAudioFrame(
74 // Audio sampling rate, for example 48000.
75 int sampling_rate_hz,
76
77 // Starting sample index of the frame, i.e. how many audio samples were
78 // before this frame since the beginning of the call or beginning of time
79 // in one channel (the starting point should not matter for NetEq). In
80 // WebRTC it is used as a timestamp of the frame.
81 // TODO(sukhanov): Starting_sample_index is currently adjusted on the
82 // receiver side in RTP path. Non-RTP implementations should preserve it.
83 // For NetEq initial offset should not matter so we should consider fixing
84 // RTP path.
85 int starting_sample_index,
86
87 // Number of audio samples in audio frame in 1 channel.
88 int samples_per_channel,
89
90 // Sequence number of the frame in the order sent, it is currently
91 // required by NetEq, but we can fix NetEq, because starting_sample_index
92 // should be enough.
93 int sequence_number,
94
95 // If audio frame is a speech or discontinued transmission.
96 FrameType frame_type,
97
98 // Opaque payload type. In RTP codepath payload type is stored in RTP
99 // header. In other implementations it should be simply passed through the
100 // wire -- it's needed for decoder.
101 uint8_t payload_type,
102
103 // Vector with opaque encoded data.
Niels Möller3a742392018-10-08 11:13:58 +0200104 std::vector<uint8_t> encoded_data);
105
106 ~MediaTransportEncodedAudioFrame();
Piotr (Peter) Slatalae0c2e972018-10-08 09:43:21 -0700107 MediaTransportEncodedAudioFrame(const MediaTransportEncodedAudioFrame&);
108 MediaTransportEncodedAudioFrame& operator=(
109 const MediaTransportEncodedAudioFrame& other);
110 MediaTransportEncodedAudioFrame& operator=(
111 MediaTransportEncodedAudioFrame&& other);
112 MediaTransportEncodedAudioFrame(MediaTransportEncodedAudioFrame&&);
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400113
114 // Getters.
115 int sampling_rate_hz() const { return sampling_rate_hz_; }
116 int starting_sample_index() const { return starting_sample_index_; }
117 int samples_per_channel() const { return samples_per_channel_; }
118 int sequence_number() const { return sequence_number_; }
119
120 uint8_t payload_type() const { return payload_type_; }
121 FrameType frame_type() const { return frame_type_; }
122
123 rtc::ArrayView<const uint8_t> encoded_data() const { return encoded_data_; }
124
125 private:
126 int sampling_rate_hz_;
127 int starting_sample_index_;
128 int samples_per_channel_;
129
130 // TODO(sukhanov): Refactor NetEq so we don't need sequence number.
Piotr (Peter) Slatalae804f922018-09-25 08:40:30 -0700131 // Having sample_index and samples_per_channel should be enough.
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400132 int sequence_number_;
133
134 FrameType frame_type_;
135
136 // TODO(sukhanov): Consider enumerating allowed encodings and store enum
137 // instead of uint payload_type.
138 uint8_t payload_type_;
139
140 std::vector<uint8_t> encoded_data_;
141};
142
Piotr (Peter) Slatalaada077f2018-11-08 07:43:31 -0800143// Callback to notify about network route changes.
144class MediaTransportNetworkChangeCallback {
145 public:
146 virtual ~MediaTransportNetworkChangeCallback() = default;
147
148 // Called when the network route is changed, with the new network route.
149 virtual void OnNetworkRouteChanged(
150 const rtc::NetworkRoute& new_network_route) = 0;
151};
152
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400153// Interface for receiving encoded audio frames from MediaTransportInterface
154// implementations.
155class MediaTransportAudioSinkInterface {
156 public:
157 virtual ~MediaTransportAudioSinkInterface() = default;
158
159 // Called when new encoded audio frame is received.
160 virtual void OnData(uint64_t channel_id,
161 MediaTransportEncodedAudioFrame frame) = 0;
162};
163
Piotr (Peter) Slatalae804f922018-09-25 08:40:30 -0700164// Represents encoded video frame, along with the codec information.
Piotr (Peter) Slatalae0c2e972018-10-08 09:43:21 -0700165class MediaTransportEncodedVideoFrame final {
Piotr (Peter) Slatalae804f922018-09-25 08:40:30 -0700166 public:
167 MediaTransportEncodedVideoFrame(int64_t frame_id,
168 std::vector<int64_t> referenced_frame_ids,
169 VideoCodecType codec_type,
Niels Möller3a742392018-10-08 11:13:58 +0200170 const webrtc::EncodedImage& encoded_image);
171 ~MediaTransportEncodedVideoFrame();
Piotr (Peter) Slatalae0c2e972018-10-08 09:43:21 -0700172 MediaTransportEncodedVideoFrame(const MediaTransportEncodedVideoFrame&);
173 MediaTransportEncodedVideoFrame& operator=(
174 const MediaTransportEncodedVideoFrame& other);
175 MediaTransportEncodedVideoFrame& operator=(
176 MediaTransportEncodedVideoFrame&& other);
177 MediaTransportEncodedVideoFrame(MediaTransportEncodedVideoFrame&&);
Piotr (Peter) Slatalae804f922018-09-25 08:40:30 -0700178
179 VideoCodecType codec_type() const { return codec_type_; }
180 const webrtc::EncodedImage& encoded_image() const { return encoded_image_; }
181
182 int64_t frame_id() const { return frame_id_; }
183 const std::vector<int64_t>& referenced_frame_ids() const {
184 return referenced_frame_ids_;
185 }
186
187 private:
188 VideoCodecType codec_type_;
189
190 // The buffer is not owned by the encoded image by default. On the sender it
191 // means that it will need to make a copy of it if it wants to deliver it
192 // asynchronously.
193 webrtc::EncodedImage encoded_image_;
194
195 // Frame id uniquely identifies a frame in a stream. It needs to be unique in
196 // a given time window (i.e. technically unique identifier for the lifetime of
197 // the connection is not needed, but you need to guarantee that remote side
198 // got rid of the previous frame_id if you plan to reuse it).
199 //
200 // It is required by a remote jitter buffer, and is the same as
201 // EncodedFrame::id::picture_id.
202 //
203 // This data must be opaque to the media transport, and media transport should
204 // itself not make any assumptions about what it is and its uniqueness.
205 int64_t frame_id_;
206
207 // A single frame might depend on other frames. This is set of identifiers on
208 // which the current frame depends.
209 std::vector<int64_t> referenced_frame_ids_;
210};
211
212// Interface for receiving encoded video frames from MediaTransportInterface
213// implementations.
214class MediaTransportVideoSinkInterface {
215 public:
216 virtual ~MediaTransportVideoSinkInterface() = default;
217
218 // Called when new encoded video frame is received.
219 virtual void OnData(uint64_t channel_id,
220 MediaTransportEncodedVideoFrame frame) = 0;
221
222 // Called when the request for keyframe is received.
223 virtual void OnKeyFrameRequested(uint64_t channel_id) = 0;
224};
225
Bjorn Mellemc78b0ea2018-10-29 15:21:31 -0700226// State of the media transport. Media transport begins in the pending state.
227// It transitions to writable when it is ready to send media. It may transition
228// back to pending if the connection is blocked. It may transition to closed at
229// any time. Closed is terminal: a transport will never re-open once closed.
230enum class MediaTransportState {
231 kPending,
232 kWritable,
233 kClosed,
234};
235
236// Callback invoked whenever the state of the media transport changes.
237class MediaTransportStateCallback {
238 public:
239 virtual ~MediaTransportStateCallback() = default;
240
241 // Invoked whenever the state of the media transport changes.
242 virtual void OnStateChanged(MediaTransportState state) = 0;
243};
244
Bjorn Mellem1f6aa9f2018-10-30 15:15:00 -0700245// Supported types of application data messages.
246enum class DataMessageType {
247 // Application data buffer with the binary bit unset.
248 kText,
249
250 // Application data buffer with the binary bit set.
251 kBinary,
252
253 // Transport-agnostic control messages, such as open or open-ack messages.
254 kControl,
255};
256
257// Parameters for sending data. The parameters may change from message to
258// message, even within a single channel. For example, control messages may be
259// sent reliably and in-order, even if the data channel is configured for
260// unreliable delivery.
261struct SendDataParams {
262 SendDataParams();
263
264 DataMessageType type = DataMessageType::kText;
265
266 // Whether to deliver the message in order with respect to other ordered
267 // messages with the same channel_id.
268 bool ordered = false;
269
270 // If set, the maximum number of times this message may be
271 // retransmitted by the transport before it is dropped.
272 // Setting this value to zero disables retransmission.
273 // Must be non-negative. |max_rtx_count| and |max_rtx_ms| may not be set
274 // simultaneously.
275 absl::optional<int> max_rtx_count;
276
277 // If set, the maximum number of milliseconds for which the transport
278 // may retransmit this message before it is dropped.
279 // Setting this value to zero disables retransmission.
280 // Must be non-negative. |max_rtx_count| and |max_rtx_ms| may not be set
281 // simultaneously.
282 absl::optional<int> max_rtx_ms;
283};
284
285// Sink for callbacks related to a data channel.
286class DataChannelSink {
287 public:
288 virtual ~DataChannelSink() = default;
289
290 // Callback issued when data is received by the transport.
291 virtual void OnDataReceived(int channel_id,
292 DataMessageType type,
293 const rtc::CopyOnWriteBuffer& buffer) = 0;
294
295 // Callback issued when a remote data channel begins the closing procedure.
296 // Messages sent after the closing procedure begins will not be transmitted.
297 virtual void OnChannelClosing(int channel_id) = 0;
298
299 // Callback issued when a (remote or local) data channel completes the closing
300 // procedure. Closing channels become closed after all pending data has been
301 // transmitted.
302 virtual void OnChannelClosed(int channel_id) = 0;
303};
304
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400305// Media transport interface for sending / receiving encoded audio/video frames
306// and receiving bandwidth estimate update from congestion control.
307class MediaTransportInterface {
308 public:
309 virtual ~MediaTransportInterface() = default;
310
Piotr (Peter) Slatalae804f922018-09-25 08:40:30 -0700311 // Start asynchronous send of audio frame. The status returned by this method
312 // only pertains to the synchronous operations (e.g.
313 // serialization/packetization), not to the asynchronous operation.
314
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400315 virtual RTCError SendAudioFrame(uint64_t channel_id,
316 MediaTransportEncodedAudioFrame frame) = 0;
317
Piotr (Peter) Slatalae804f922018-09-25 08:40:30 -0700318 // Start asynchronous send of video frame. The status returned by this method
319 // only pertains to the synchronous operations (e.g.
320 // serialization/packetization), not to the asynchronous operation.
321 virtual RTCError SendVideoFrame(
322 uint64_t channel_id,
323 const MediaTransportEncodedVideoFrame& frame) = 0;
324
325 // Requests a keyframe for the particular channel (stream). The caller should
326 // check that the keyframe is not present in a jitter buffer already (i.e.
327 // don't request a keyframe if there is one that you will get from the jitter
328 // buffer in a moment).
329 virtual RTCError RequestKeyFrame(uint64_t channel_id) = 0;
330
331 // Sets audio sink. Sink must be unset by calling SetReceiveAudioSink(nullptr)
332 // before the media transport is destroyed or before new sink is set.
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400333 virtual void SetReceiveAudioSink(MediaTransportAudioSinkInterface* sink) = 0;
334
Piotr (Peter) Slatalae804f922018-09-25 08:40:30 -0700335 // Registers a video sink. Before destruction of media transport, you must
336 // pass a nullptr.
337 virtual void SetReceiveVideoSink(MediaTransportVideoSinkInterface* sink) = 0;
338
Piotr (Peter) Slatala6b9d8232018-10-26 07:59:46 -0700339 // Sets a target bitrate observer. Before media transport is destructed
340 // the observer must be unregistered (set to nullptr).
341 // A newly registered observer will be called back with the latest recorded
342 // target rate, if available.
Piotr (Peter) Slatalaada077f2018-11-08 07:43:31 -0800343 // TODO(psla): This method will be removed, in favor of
344 // AddTargetTransferRateObserver.
Piotr (Peter) Slatala6b9d8232018-10-26 07:59:46 -0700345 virtual void SetTargetTransferRateObserver(
Piotr (Peter) Slatalaada077f2018-11-08 07:43:31 -0800346 TargetTransferRateObserver* observer);
347
348 // Adds a target bitrate observer. Before media transport is destructed
349 // the observer must be unregistered (by calling
350 // RemoveTargetTransferRateObserver).
351 // A newly registered observer will be called back with the latest recorded
352 // target rate, if available.
353 virtual void AddTargetTransferRateObserver(
354 webrtc::TargetTransferRateObserver* observer);
355
356 // Removes an existing |observer| from observers. If observer was never
357 // registered, an error is logged and method does nothing.
358 virtual void RemoveTargetTransferRateObserver(
359 webrtc::TargetTransferRateObserver* observer);
360
361 // Returns the last known target transfer rate as reported to the above
362 // observers.
363 virtual absl::optional<TargetTransferRate> GetLatestTargetTransferRate();
364
365 // Gets the audio packet overhead in bytes. Returned overhead does not include
366 // transport overhead (ipv4/6, turn channeldata, tcp/udp, etc.).
367 // If the transport is capable of fusing packets together, this overhead
368 // might not be a very accurate number.
369 virtual size_t GetAudioPacketOverhead() const;
370
371 // Sets an observer for network change events. If the network route is already
372 // established when the callback is set, |callback| will be called immediately
373 // with the current network route.
374 // Before media transport is destroyed, the callback must be unregistered by
375 // setting it to nullptr.
376 virtual void SetNetworkChangeCallback(
377 MediaTransportNetworkChangeCallback* callback);
Piotr (Peter) Slatala6b9d8232018-10-26 07:59:46 -0700378
Bjorn Mellemc78b0ea2018-10-29 15:21:31 -0700379 // Sets a state observer callback. Before media transport is destroyed, the
380 // callback must be unregistered by setting it to nullptr.
381 // A newly registered callback will be called with the current state.
382 // Media transport does not invoke this callback concurrently.
Bjorn Mellemc78b0ea2018-10-29 15:21:31 -0700383 virtual void SetMediaTransportStateCallback(
Bjorn Mellemeb2c6412018-10-31 15:25:32 -0700384 MediaTransportStateCallback* callback) = 0;
Bjorn Mellemc78b0ea2018-10-29 15:21:31 -0700385
Bjorn Mellem1f6aa9f2018-10-30 15:15:00 -0700386 // Sends a data buffer to the remote endpoint using the given send parameters.
387 // |buffer| may not be larger than 256 KiB. Returns an error if the send
388 // fails.
Bjorn Mellem1f6aa9f2018-10-30 15:15:00 -0700389 virtual RTCError SendData(int channel_id,
390 const SendDataParams& params,
Bjorn Mellemeb2c6412018-10-31 15:25:32 -0700391 const rtc::CopyOnWriteBuffer& buffer) = 0;
Bjorn Mellem1f6aa9f2018-10-30 15:15:00 -0700392
393 // Closes |channel_id| gracefully. Returns an error if |channel_id| is not
394 // open. Data sent after the closing procedure begins will not be
395 // transmitted. The channel becomes closed after pending data is transmitted.
Bjorn Mellemeb2c6412018-10-31 15:25:32 -0700396 virtual RTCError CloseChannel(int channel_id) = 0;
Bjorn Mellem1f6aa9f2018-10-30 15:15:00 -0700397
398 // Sets a sink for data messages and channel state callbacks. Before media
399 // transport is destroyed, the sink must be unregistered by setting it to
400 // nullptr.
Bjorn Mellemeb2c6412018-10-31 15:25:32 -0700401 virtual void SetDataSink(DataChannelSink* sink) = 0;
Bjorn Mellem1f6aa9f2018-10-30 15:15:00 -0700402
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400403 // TODO(sukhanov): RtcEventLogs.
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400404};
405
406// If media transport factory is set in peer connection factory, it will be
407// used to create media transport for sending/receiving encoded frames and
408// this transport will be used instead of default RTP/SRTP transport.
409//
410// Currently Media Transport negotiation is not supported in SDP.
411// If application is using media transport, it must negotiate it before
412// setting media transport factory in peer connection.
413class MediaTransportFactory {
414 public:
415 virtual ~MediaTransportFactory() = default;
416
417 // Creates media transport.
418 // - Does not take ownership of packet_transport or network_thread.
419 // - Does not support group calls, in 1:1 call one side must set
420 // is_caller = true and another is_caller = false.
Piotr (Peter) Slatalaa0677d12018-10-29 07:31:42 -0700421 // TODO(bugs.webrtc.org/9938) This constructor will be removed and replaced
422 // with the one below.
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400423 virtual RTCErrorOr<std::unique_ptr<MediaTransportInterface>>
424 CreateMediaTransport(rtc::PacketTransportInternal* packet_transport,
425 rtc::Thread* network_thread,
Piotr (Peter) Slatalaa0677d12018-10-29 07:31:42 -0700426 bool is_caller);
427
428 // Creates media transport.
429 // - Does not take ownership of packet_transport or network_thread.
430 // TODO(bugs.webrtc.org/9938): remove default implementation once all children
431 // override it.
432 virtual RTCErrorOr<std::unique_ptr<MediaTransportInterface>>
433 CreateMediaTransport(rtc::PacketTransportInternal* packet_transport,
434 rtc::Thread* network_thread,
Piotr (Peter) Slatalaed7b8b12018-10-29 10:43:16 -0700435 const MediaTransportSettings& settings);
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400436};
437
438} // namespace webrtc
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400439#endif // API_MEDIA_TRANSPORT_INTERFACE_H_