blob: 6b461cf42743ba9a72d6cd14ca8e95f8a18552cc [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"
Bjorn Mellem1f6aa9f2018-10-30 15:15:00 -070030#include "rtc_base/copyonwritebuffer.h"
Niels Möllerd8a1b7a2018-12-06 13:00:27 +010031#include "rtc_base/deprecation.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.
Niels Möllerd8a1b7a2018-12-06 13:00:27 +0100101 int payload_type,
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400102
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
Niels Möllerd8a1b7a2018-12-06 13:00:27 +0100120 int payload_type() const { return payload_type_; }
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400121 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
Niels Möllerd8a1b7a2018-12-06 13:00:27 +0100136 int payload_type_;
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400137
138 std::vector<uint8_t> encoded_data_;
139};
140
Piotr (Peter) Slatalaada077f2018-11-08 07:43:31 -0800141// Callback to notify about network route changes.
142class MediaTransportNetworkChangeCallback {
143 public:
144 virtual ~MediaTransportNetworkChangeCallback() = default;
145
146 // Called when the network route is changed, with the new network route.
147 virtual void OnNetworkRouteChanged(
148 const rtc::NetworkRoute& new_network_route) = 0;
149};
150
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400151// Interface for receiving encoded audio frames from MediaTransportInterface
152// implementations.
153class MediaTransportAudioSinkInterface {
154 public:
155 virtual ~MediaTransportAudioSinkInterface() = default;
156
157 // Called when new encoded audio frame is received.
158 virtual void OnData(uint64_t channel_id,
159 MediaTransportEncodedAudioFrame frame) = 0;
160};
161
Piotr (Peter) Slatalae804f922018-09-25 08:40:30 -0700162// Represents encoded video frame, along with the codec information.
Piotr (Peter) Slatalae0c2e972018-10-08 09:43:21 -0700163class MediaTransportEncodedVideoFrame final {
Piotr (Peter) Slatalae804f922018-09-25 08:40:30 -0700164 public:
Niels Möllerd8a1b7a2018-12-06 13:00:27 +0100165 // TODO(bugs.webrtc.org/9719): Switch to payload_type
166 RTC_DEPRECATED MediaTransportEncodedVideoFrame(
167 int64_t frame_id,
168 std::vector<int64_t> referenced_frame_ids,
169 VideoCodecType codec_type,
170 const webrtc::EncodedImage& encoded_image);
Piotr (Peter) Slatalae804f922018-09-25 08:40:30 -0700171 MediaTransportEncodedVideoFrame(int64_t frame_id,
172 std::vector<int64_t> referenced_frame_ids,
Niels Möllerd8a1b7a2018-12-06 13:00:27 +0100173 int payload_type,
Niels Möller3a742392018-10-08 11:13:58 +0200174 const webrtc::EncodedImage& encoded_image);
175 ~MediaTransportEncodedVideoFrame();
Piotr (Peter) Slatalae0c2e972018-10-08 09:43:21 -0700176 MediaTransportEncodedVideoFrame(const MediaTransportEncodedVideoFrame&);
177 MediaTransportEncodedVideoFrame& operator=(
178 const MediaTransportEncodedVideoFrame& other);
179 MediaTransportEncodedVideoFrame& operator=(
180 MediaTransportEncodedVideoFrame&& other);
181 MediaTransportEncodedVideoFrame(MediaTransportEncodedVideoFrame&&);
Piotr (Peter) Slatalae804f922018-09-25 08:40:30 -0700182
Niels Möllerd8a1b7a2018-12-06 13:00:27 +0100183 // TODO(bugs.webrtc.org/9719): Switch to payload_type
184 RTC_DEPRECATED VideoCodecType codec_type() const { return codec_type_; }
185 int payload_type() const { return payload_type_; }
Piotr (Peter) Slatalae804f922018-09-25 08:40:30 -0700186 const webrtc::EncodedImage& encoded_image() const { return encoded_image_; }
187
188 int64_t frame_id() const { return frame_id_; }
189 const std::vector<int64_t>& referenced_frame_ids() const {
190 return referenced_frame_ids_;
191 }
192
Niels Möllerd5696fb2018-11-28 15:34:37 +0100193 // Hack to workaround lack of ownership of the encoded_image_._buffer. If we
194 // don't already own the underlying data, make a copy.
195 void Retain();
196
Piotr (Peter) Slatalae804f922018-09-25 08:40:30 -0700197 private:
Niels Möllerd5696fb2018-11-28 15:34:37 +0100198 MediaTransportEncodedVideoFrame();
199
Piotr (Peter) Slatalae804f922018-09-25 08:40:30 -0700200 VideoCodecType codec_type_;
Niels Möllerd8a1b7a2018-12-06 13:00:27 +0100201 int payload_type_;
Piotr (Peter) Slatalae804f922018-09-25 08:40:30 -0700202
Niels Möllerd5696fb2018-11-28 15:34:37 +0100203 // The buffer is not owned by the encoded image. On the sender it means that
204 // it will need to make a copy using the Retain() method, if it wants to
205 // deliver it asynchronously.
Piotr (Peter) Slatalae804f922018-09-25 08:40:30 -0700206 webrtc::EncodedImage encoded_image_;
207
Niels Möllerd5696fb2018-11-28 15:34:37 +0100208 // If non-empty, this is the data for the encoded image.
209 std::vector<uint8_t> encoded_data_;
210
Piotr (Peter) Slatalae804f922018-09-25 08:40:30 -0700211 // Frame id uniquely identifies a frame in a stream. It needs to be unique in
212 // a given time window (i.e. technically unique identifier for the lifetime of
213 // the connection is not needed, but you need to guarantee that remote side
214 // got rid of the previous frame_id if you plan to reuse it).
215 //
216 // It is required by a remote jitter buffer, and is the same as
217 // EncodedFrame::id::picture_id.
218 //
219 // This data must be opaque to the media transport, and media transport should
220 // itself not make any assumptions about what it is and its uniqueness.
221 int64_t frame_id_;
222
223 // A single frame might depend on other frames. This is set of identifiers on
224 // which the current frame depends.
225 std::vector<int64_t> referenced_frame_ids_;
226};
227
228// Interface for receiving encoded video frames from MediaTransportInterface
229// implementations.
230class MediaTransportVideoSinkInterface {
231 public:
232 virtual ~MediaTransportVideoSinkInterface() = default;
233
234 // Called when new encoded video frame is received.
235 virtual void OnData(uint64_t channel_id,
236 MediaTransportEncodedVideoFrame frame) = 0;
237
Niels Möllerd8a1b7a2018-12-06 13:00:27 +0100238 // TODO(bugs.webrtc.org/9719): Belongs on send side, not receive side.
239 RTC_DEPRECATED virtual void OnKeyFrameRequested(uint64_t channel_id) {}
Piotr (Peter) Slatalae804f922018-09-25 08:40:30 -0700240};
241
Bjorn Mellemc78b0ea2018-10-29 15:21:31 -0700242// State of the media transport. Media transport begins in the pending state.
243// It transitions to writable when it is ready to send media. It may transition
244// back to pending if the connection is blocked. It may transition to closed at
245// any time. Closed is terminal: a transport will never re-open once closed.
246enum class MediaTransportState {
247 kPending,
248 kWritable,
249 kClosed,
250};
251
252// Callback invoked whenever the state of the media transport changes.
253class MediaTransportStateCallback {
254 public:
255 virtual ~MediaTransportStateCallback() = default;
256
257 // Invoked whenever the state of the media transport changes.
258 virtual void OnStateChanged(MediaTransportState state) = 0;
259};
260
Bjorn Mellem1f6aa9f2018-10-30 15:15:00 -0700261// Supported types of application data messages.
262enum class DataMessageType {
263 // Application data buffer with the binary bit unset.
264 kText,
265
266 // Application data buffer with the binary bit set.
267 kBinary,
268
269 // Transport-agnostic control messages, such as open or open-ack messages.
270 kControl,
271};
272
273// Parameters for sending data. The parameters may change from message to
274// message, even within a single channel. For example, control messages may be
275// sent reliably and in-order, even if the data channel is configured for
276// unreliable delivery.
277struct SendDataParams {
278 SendDataParams();
Niels Möllere0446cb2018-11-30 09:35:52 +0100279 SendDataParams(const SendDataParams&);
Bjorn Mellem1f6aa9f2018-10-30 15:15:00 -0700280
281 DataMessageType type = DataMessageType::kText;
282
283 // Whether to deliver the message in order with respect to other ordered
284 // messages with the same channel_id.
285 bool ordered = false;
286
287 // If set, the maximum number of times this message may be
288 // retransmitted by the transport before it is dropped.
289 // Setting this value to zero disables retransmission.
290 // Must be non-negative. |max_rtx_count| and |max_rtx_ms| may not be set
291 // simultaneously.
292 absl::optional<int> max_rtx_count;
293
294 // If set, the maximum number of milliseconds for which the transport
295 // may retransmit this message before it is dropped.
296 // Setting this value to zero disables retransmission.
297 // Must be non-negative. |max_rtx_count| and |max_rtx_ms| may not be set
298 // simultaneously.
299 absl::optional<int> max_rtx_ms;
300};
301
302// Sink for callbacks related to a data channel.
303class DataChannelSink {
304 public:
305 virtual ~DataChannelSink() = default;
306
307 // Callback issued when data is received by the transport.
308 virtual void OnDataReceived(int channel_id,
309 DataMessageType type,
310 const rtc::CopyOnWriteBuffer& buffer) = 0;
311
312 // Callback issued when a remote data channel begins the closing procedure.
313 // Messages sent after the closing procedure begins will not be transmitted.
314 virtual void OnChannelClosing(int channel_id) = 0;
315
316 // Callback issued when a (remote or local) data channel completes the closing
317 // procedure. Closing channels become closed after all pending data has been
318 // transmitted.
319 virtual void OnChannelClosed(int channel_id) = 0;
320};
321
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400322// Media transport interface for sending / receiving encoded audio/video frames
323// and receiving bandwidth estimate update from congestion control.
324class MediaTransportInterface {
325 public:
326 virtual ~MediaTransportInterface() = default;
327
Piotr (Peter) Slatalae804f922018-09-25 08:40:30 -0700328 // Start asynchronous send of audio frame. The status returned by this method
329 // only pertains to the synchronous operations (e.g.
330 // serialization/packetization), not to the asynchronous operation.
331
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400332 virtual RTCError SendAudioFrame(uint64_t channel_id,
333 MediaTransportEncodedAudioFrame frame) = 0;
334
Piotr (Peter) Slatalae804f922018-09-25 08:40:30 -0700335 // Start asynchronous send of video frame. The status returned by this method
336 // only pertains to the synchronous operations (e.g.
337 // serialization/packetization), not to the asynchronous operation.
338 virtual RTCError SendVideoFrame(
339 uint64_t channel_id,
340 const MediaTransportEncodedVideoFrame& frame) = 0;
341
342 // Requests a keyframe for the particular channel (stream). The caller should
343 // check that the keyframe is not present in a jitter buffer already (i.e.
344 // don't request a keyframe if there is one that you will get from the jitter
345 // buffer in a moment).
346 virtual RTCError RequestKeyFrame(uint64_t channel_id) = 0;
347
348 // Sets audio sink. Sink must be unset by calling SetReceiveAudioSink(nullptr)
349 // before the media transport is destroyed or before new sink is set.
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400350 virtual void SetReceiveAudioSink(MediaTransportAudioSinkInterface* sink) = 0;
351
Piotr (Peter) Slatalae804f922018-09-25 08:40:30 -0700352 // Registers a video sink. Before destruction of media transport, you must
353 // pass a nullptr.
354 virtual void SetReceiveVideoSink(MediaTransportVideoSinkInterface* sink) = 0;
355
Piotr (Peter) Slatalaada077f2018-11-08 07:43:31 -0800356 // Adds a target bitrate observer. Before media transport is destructed
357 // the observer must be unregistered (by calling
358 // RemoveTargetTransferRateObserver).
359 // A newly registered observer will be called back with the latest recorded
360 // target rate, if available.
361 virtual void AddTargetTransferRateObserver(
362 webrtc::TargetTransferRateObserver* observer);
363
364 // Removes an existing |observer| from observers. If observer was never
365 // registered, an error is logged and method does nothing.
366 virtual void RemoveTargetTransferRateObserver(
367 webrtc::TargetTransferRateObserver* observer);
368
369 // Returns the last known target transfer rate as reported to the above
370 // observers.
371 virtual absl::optional<TargetTransferRate> GetLatestTargetTransferRate();
372
373 // Gets the audio packet overhead in bytes. Returned overhead does not include
374 // transport overhead (ipv4/6, turn channeldata, tcp/udp, etc.).
375 // If the transport is capable of fusing packets together, this overhead
376 // might not be a very accurate number.
377 virtual size_t GetAudioPacketOverhead() const;
378
379 // Sets an observer for network change events. If the network route is already
380 // established when the callback is set, |callback| will be called immediately
381 // with the current network route.
382 // Before media transport is destroyed, the callback must be unregistered by
383 // setting it to nullptr.
384 virtual void SetNetworkChangeCallback(
385 MediaTransportNetworkChangeCallback* callback);
Piotr (Peter) Slatala6b9d8232018-10-26 07:59:46 -0700386
Bjorn Mellemc78b0ea2018-10-29 15:21:31 -0700387 // Sets a state observer callback. Before media transport is destroyed, the
388 // callback must be unregistered by setting it to nullptr.
389 // A newly registered callback will be called with the current state.
390 // Media transport does not invoke this callback concurrently.
Bjorn Mellemc78b0ea2018-10-29 15:21:31 -0700391 virtual void SetMediaTransportStateCallback(
Bjorn Mellemeb2c6412018-10-31 15:25:32 -0700392 MediaTransportStateCallback* callback) = 0;
Bjorn Mellemc78b0ea2018-10-29 15:21:31 -0700393
Bjorn Mellem1f6aa9f2018-10-30 15:15:00 -0700394 // Sends a data buffer to the remote endpoint using the given send parameters.
395 // |buffer| may not be larger than 256 KiB. Returns an error if the send
396 // fails.
Bjorn Mellem1f6aa9f2018-10-30 15:15:00 -0700397 virtual RTCError SendData(int channel_id,
398 const SendDataParams& params,
Bjorn Mellemeb2c6412018-10-31 15:25:32 -0700399 const rtc::CopyOnWriteBuffer& buffer) = 0;
Bjorn Mellem1f6aa9f2018-10-30 15:15:00 -0700400
401 // Closes |channel_id| gracefully. Returns an error if |channel_id| is not
402 // open. Data sent after the closing procedure begins will not be
403 // transmitted. The channel becomes closed after pending data is transmitted.
Bjorn Mellemeb2c6412018-10-31 15:25:32 -0700404 virtual RTCError CloseChannel(int channel_id) = 0;
Bjorn Mellem1f6aa9f2018-10-30 15:15:00 -0700405
406 // Sets a sink for data messages and channel state callbacks. Before media
407 // transport is destroyed, the sink must be unregistered by setting it to
408 // nullptr.
Bjorn Mellemeb2c6412018-10-31 15:25:32 -0700409 virtual void SetDataSink(DataChannelSink* sink) = 0;
Bjorn Mellem1f6aa9f2018-10-30 15:15:00 -0700410
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400411 // TODO(sukhanov): RtcEventLogs.
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400412};
413
414// If media transport factory is set in peer connection factory, it will be
415// used to create media transport for sending/receiving encoded frames and
416// this transport will be used instead of default RTP/SRTP transport.
417//
418// Currently Media Transport negotiation is not supported in SDP.
419// If application is using media transport, it must negotiate it before
420// setting media transport factory in peer connection.
421class MediaTransportFactory {
422 public:
423 virtual ~MediaTransportFactory() = default;
424
425 // Creates media transport.
426 // - Does not take ownership of packet_transport or network_thread.
427 // - Does not support group calls, in 1:1 call one side must set
428 // is_caller = true and another is_caller = false.
Piotr (Peter) Slatalaa0677d12018-10-29 07:31:42 -0700429 // TODO(bugs.webrtc.org/9938) This constructor will be removed and replaced
430 // with the one below.
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400431 virtual RTCErrorOr<std::unique_ptr<MediaTransportInterface>>
432 CreateMediaTransport(rtc::PacketTransportInternal* packet_transport,
433 rtc::Thread* network_thread,
Piotr (Peter) Slatalaa0677d12018-10-29 07:31:42 -0700434 bool is_caller);
435
436 // Creates media transport.
437 // - Does not take ownership of packet_transport or network_thread.
438 // TODO(bugs.webrtc.org/9938): remove default implementation once all children
439 // override it.
440 virtual RTCErrorOr<std::unique_ptr<MediaTransportInterface>>
441 CreateMediaTransport(rtc::PacketTransportInternal* packet_transport,
442 rtc::Thread* network_thread,
Piotr (Peter) Slatalaed7b8b12018-10-29 10:43:16 -0700443 const MediaTransportSettings& settings);
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400444};
445
446} // namespace webrtc
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400447#endif // API_MEDIA_TRANSPORT_INTERFACE_H_