Piotr (Peter) Slatala | e0c2e97 | 2018-10-08 09:43:21 -0700 | [diff] [blame] | 1 | /* Copyright 2018 The WebRTC project authors. All Rights Reserved. |
Anton Sukhanov | f60bd4b | 2018-09-05 13:41:46 -0400 | [diff] [blame] | 2 | * |
| 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) Slatala | 6b9d823 | 2018-10-26 07:59:46 -0700 | [diff] [blame] | 20 | #include <api/transport/network_control.h> |
Anton Sukhanov | f60bd4b | 2018-09-05 13:41:46 -0400 | [diff] [blame] | 21 | #include <memory> |
Piotr (Peter) Slatala | a0677d1 | 2018-10-29 07:31:42 -0700 | [diff] [blame] | 22 | #include <string> |
Anton Sukhanov | f60bd4b | 2018-09-05 13:41:46 -0400 | [diff] [blame] | 23 | #include <utility> |
| 24 | #include <vector> |
| 25 | |
Piotr (Peter) Slatala | a0677d1 | 2018-10-29 07:31:42 -0700 | [diff] [blame] | 26 | #include "absl/types/optional.h" |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 27 | #include "api/array_view.h" |
Anton Sukhanov | f60bd4b | 2018-09-05 13:41:46 -0400 | [diff] [blame] | 28 | #include "api/rtcerror.h" |
Niels Möller | 3a74239 | 2018-10-08 11:13:58 +0200 | [diff] [blame] | 29 | #include "api/video/encoded_image.h" |
Anton Sukhanov | f60bd4b | 2018-09-05 13:41:46 -0400 | [diff] [blame] | 30 | #include "common_types.h" // NOLINT(build/include) |
| 31 | |
| 32 | namespace rtc { |
| 33 | class PacketTransportInternal; |
| 34 | class Thread; |
| 35 | } // namespace rtc |
| 36 | |
| 37 | namespace webrtc { |
| 38 | |
Piotr (Peter) Slatala | a0677d1 | 2018-10-29 07:31:42 -0700 | [diff] [blame] | 39 | // A collection of settings for creation of media transport. |
| 40 | struct MediaTransportSettings final { |
| 41 | MediaTransportSettings(); |
Piotr (Peter) Slatala | ed7b8b1 | 2018-10-29 10:43:16 -0700 | [diff] [blame] | 42 | MediaTransportSettings(const MediaTransportSettings&); |
| 43 | MediaTransportSettings& operator=(const MediaTransportSettings&); |
Piotr (Peter) Slatala | a0677d1 | 2018-10-29 07:31:42 -0700 | [diff] [blame] | 44 | ~MediaTransportSettings(); |
| 45 | |
| 46 | // Group calls are not currently supported, in 1:1 call one side must set |
| 47 | // is_caller = true and another is_caller = false. |
| 48 | bool is_caller; |
| 49 | |
| 50 | // Must be set if a pre-shared key is used for the call. |
Piotr (Peter) Slatala | 9f95625 | 2018-10-31 08:25:26 -0700 | [diff] [blame] | 51 | // TODO(bugs.webrtc.org/9944): This should become zero buffer in the distant |
| 52 | // future. |
Piotr (Peter) Slatala | a0677d1 | 2018-10-29 07:31:42 -0700 | [diff] [blame] | 53 | absl::optional<std::string> pre_shared_key; |
| 54 | }; |
| 55 | |
Anton Sukhanov | f60bd4b | 2018-09-05 13:41:46 -0400 | [diff] [blame] | 56 | // Represents encoded audio frame in any encoding (type of encoding is opaque). |
| 57 | // To avoid copying of encoded data use move semantics when passing by value. |
Piotr (Peter) Slatala | e0c2e97 | 2018-10-08 09:43:21 -0700 | [diff] [blame] | 58 | class MediaTransportEncodedAudioFrame final { |
Anton Sukhanov | f60bd4b | 2018-09-05 13:41:46 -0400 | [diff] [blame] | 59 | public: |
| 60 | enum class FrameType { |
| 61 | // Normal audio frame (equivalent to webrtc::kAudioFrameSpeech). |
| 62 | kSpeech, |
| 63 | |
| 64 | // DTX frame (equivalent to webrtc::kAudioFrameCN). |
Niels Möller | 7d76a31 | 2018-10-26 12:57:07 +0200 | [diff] [blame] | 65 | // DTX frame (equivalent to webrtc::kAudioFrameCN). |
| 66 | kDiscontinuousTransmission, |
| 67 | // TODO(nisse): Mis-spelled version, update users, then delete. |
| 68 | kDiscountinuousTransmission = kDiscontinuousTransmission, |
Anton Sukhanov | f60bd4b | 2018-09-05 13:41:46 -0400 | [diff] [blame] | 69 | }; |
| 70 | |
| 71 | MediaTransportEncodedAudioFrame( |
| 72 | // Audio sampling rate, for example 48000. |
| 73 | int sampling_rate_hz, |
| 74 | |
| 75 | // Starting sample index of the frame, i.e. how many audio samples were |
| 76 | // before this frame since the beginning of the call or beginning of time |
| 77 | // in one channel (the starting point should not matter for NetEq). In |
| 78 | // WebRTC it is used as a timestamp of the frame. |
| 79 | // TODO(sukhanov): Starting_sample_index is currently adjusted on the |
| 80 | // receiver side in RTP path. Non-RTP implementations should preserve it. |
| 81 | // For NetEq initial offset should not matter so we should consider fixing |
| 82 | // RTP path. |
| 83 | int starting_sample_index, |
| 84 | |
| 85 | // Number of audio samples in audio frame in 1 channel. |
| 86 | int samples_per_channel, |
| 87 | |
| 88 | // Sequence number of the frame in the order sent, it is currently |
| 89 | // required by NetEq, but we can fix NetEq, because starting_sample_index |
| 90 | // should be enough. |
| 91 | int sequence_number, |
| 92 | |
| 93 | // If audio frame is a speech or discontinued transmission. |
| 94 | FrameType frame_type, |
| 95 | |
| 96 | // Opaque payload type. In RTP codepath payload type is stored in RTP |
| 97 | // header. In other implementations it should be simply passed through the |
| 98 | // wire -- it's needed for decoder. |
| 99 | uint8_t payload_type, |
| 100 | |
| 101 | // Vector with opaque encoded data. |
Niels Möller | 3a74239 | 2018-10-08 11:13:58 +0200 | [diff] [blame] | 102 | std::vector<uint8_t> encoded_data); |
| 103 | |
| 104 | ~MediaTransportEncodedAudioFrame(); |
Piotr (Peter) Slatala | e0c2e97 | 2018-10-08 09:43:21 -0700 | [diff] [blame] | 105 | MediaTransportEncodedAudioFrame(const MediaTransportEncodedAudioFrame&); |
| 106 | MediaTransportEncodedAudioFrame& operator=( |
| 107 | const MediaTransportEncodedAudioFrame& other); |
| 108 | MediaTransportEncodedAudioFrame& operator=( |
| 109 | MediaTransportEncodedAudioFrame&& other); |
| 110 | MediaTransportEncodedAudioFrame(MediaTransportEncodedAudioFrame&&); |
Anton Sukhanov | f60bd4b | 2018-09-05 13:41:46 -0400 | [diff] [blame] | 111 | |
| 112 | // Getters. |
| 113 | int sampling_rate_hz() const { return sampling_rate_hz_; } |
| 114 | int starting_sample_index() const { return starting_sample_index_; } |
| 115 | int samples_per_channel() const { return samples_per_channel_; } |
| 116 | int sequence_number() const { return sequence_number_; } |
| 117 | |
| 118 | uint8_t payload_type() const { return payload_type_; } |
| 119 | FrameType frame_type() const { return frame_type_; } |
| 120 | |
| 121 | rtc::ArrayView<const uint8_t> encoded_data() const { return encoded_data_; } |
| 122 | |
| 123 | private: |
| 124 | int sampling_rate_hz_; |
| 125 | int starting_sample_index_; |
| 126 | int samples_per_channel_; |
| 127 | |
| 128 | // TODO(sukhanov): Refactor NetEq so we don't need sequence number. |
Piotr (Peter) Slatala | e804f92 | 2018-09-25 08:40:30 -0700 | [diff] [blame] | 129 | // Having sample_index and samples_per_channel should be enough. |
Anton Sukhanov | f60bd4b | 2018-09-05 13:41:46 -0400 | [diff] [blame] | 130 | int sequence_number_; |
| 131 | |
| 132 | FrameType frame_type_; |
| 133 | |
| 134 | // TODO(sukhanov): Consider enumerating allowed encodings and store enum |
| 135 | // instead of uint payload_type. |
| 136 | uint8_t payload_type_; |
| 137 | |
| 138 | std::vector<uint8_t> encoded_data_; |
| 139 | }; |
| 140 | |
| 141 | // Interface for receiving encoded audio frames from MediaTransportInterface |
| 142 | // implementations. |
| 143 | class MediaTransportAudioSinkInterface { |
| 144 | public: |
| 145 | virtual ~MediaTransportAudioSinkInterface() = default; |
| 146 | |
| 147 | // Called when new encoded audio frame is received. |
| 148 | virtual void OnData(uint64_t channel_id, |
| 149 | MediaTransportEncodedAudioFrame frame) = 0; |
| 150 | }; |
| 151 | |
Piotr (Peter) Slatala | e804f92 | 2018-09-25 08:40:30 -0700 | [diff] [blame] | 152 | // Represents encoded video frame, along with the codec information. |
Piotr (Peter) Slatala | e0c2e97 | 2018-10-08 09:43:21 -0700 | [diff] [blame] | 153 | class MediaTransportEncodedVideoFrame final { |
Piotr (Peter) Slatala | e804f92 | 2018-09-25 08:40:30 -0700 | [diff] [blame] | 154 | public: |
| 155 | MediaTransportEncodedVideoFrame(int64_t frame_id, |
| 156 | std::vector<int64_t> referenced_frame_ids, |
| 157 | VideoCodecType codec_type, |
Niels Möller | 3a74239 | 2018-10-08 11:13:58 +0200 | [diff] [blame] | 158 | const webrtc::EncodedImage& encoded_image); |
| 159 | ~MediaTransportEncodedVideoFrame(); |
Piotr (Peter) Slatala | e0c2e97 | 2018-10-08 09:43:21 -0700 | [diff] [blame] | 160 | MediaTransportEncodedVideoFrame(const MediaTransportEncodedVideoFrame&); |
| 161 | MediaTransportEncodedVideoFrame& operator=( |
| 162 | const MediaTransportEncodedVideoFrame& other); |
| 163 | MediaTransportEncodedVideoFrame& operator=( |
| 164 | MediaTransportEncodedVideoFrame&& other); |
| 165 | MediaTransportEncodedVideoFrame(MediaTransportEncodedVideoFrame&&); |
Piotr (Peter) Slatala | e804f92 | 2018-09-25 08:40:30 -0700 | [diff] [blame] | 166 | |
| 167 | VideoCodecType codec_type() const { return codec_type_; } |
| 168 | const webrtc::EncodedImage& encoded_image() const { return encoded_image_; } |
| 169 | |
| 170 | int64_t frame_id() const { return frame_id_; } |
| 171 | const std::vector<int64_t>& referenced_frame_ids() const { |
| 172 | return referenced_frame_ids_; |
| 173 | } |
| 174 | |
| 175 | private: |
| 176 | VideoCodecType codec_type_; |
| 177 | |
| 178 | // The buffer is not owned by the encoded image by default. On the sender it |
| 179 | // means that it will need to make a copy of it if it wants to deliver it |
| 180 | // asynchronously. |
| 181 | webrtc::EncodedImage encoded_image_; |
| 182 | |
| 183 | // Frame id uniquely identifies a frame in a stream. It needs to be unique in |
| 184 | // a given time window (i.e. technically unique identifier for the lifetime of |
| 185 | // the connection is not needed, but you need to guarantee that remote side |
| 186 | // got rid of the previous frame_id if you plan to reuse it). |
| 187 | // |
| 188 | // It is required by a remote jitter buffer, and is the same as |
| 189 | // EncodedFrame::id::picture_id. |
| 190 | // |
| 191 | // This data must be opaque to the media transport, and media transport should |
| 192 | // itself not make any assumptions about what it is and its uniqueness. |
| 193 | int64_t frame_id_; |
| 194 | |
| 195 | // A single frame might depend on other frames. This is set of identifiers on |
| 196 | // which the current frame depends. |
| 197 | std::vector<int64_t> referenced_frame_ids_; |
| 198 | }; |
| 199 | |
| 200 | // Interface for receiving encoded video frames from MediaTransportInterface |
| 201 | // implementations. |
| 202 | class MediaTransportVideoSinkInterface { |
| 203 | public: |
| 204 | virtual ~MediaTransportVideoSinkInterface() = default; |
| 205 | |
| 206 | // Called when new encoded video frame is received. |
| 207 | virtual void OnData(uint64_t channel_id, |
| 208 | MediaTransportEncodedVideoFrame frame) = 0; |
| 209 | |
| 210 | // Called when the request for keyframe is received. |
| 211 | virtual void OnKeyFrameRequested(uint64_t channel_id) = 0; |
| 212 | }; |
| 213 | |
Bjorn Mellem | c78b0ea | 2018-10-29 15:21:31 -0700 | [diff] [blame] | 214 | // State of the media transport. Media transport begins in the pending state. |
| 215 | // It transitions to writable when it is ready to send media. It may transition |
| 216 | // back to pending if the connection is blocked. It may transition to closed at |
| 217 | // any time. Closed is terminal: a transport will never re-open once closed. |
| 218 | enum class MediaTransportState { |
| 219 | kPending, |
| 220 | kWritable, |
| 221 | kClosed, |
| 222 | }; |
| 223 | |
| 224 | // Callback invoked whenever the state of the media transport changes. |
| 225 | class MediaTransportStateCallback { |
| 226 | public: |
| 227 | virtual ~MediaTransportStateCallback() = default; |
| 228 | |
| 229 | // Invoked whenever the state of the media transport changes. |
| 230 | virtual void OnStateChanged(MediaTransportState state) = 0; |
| 231 | }; |
| 232 | |
Anton Sukhanov | f60bd4b | 2018-09-05 13:41:46 -0400 | [diff] [blame] | 233 | // Media transport interface for sending / receiving encoded audio/video frames |
| 234 | // and receiving bandwidth estimate update from congestion control. |
| 235 | class MediaTransportInterface { |
| 236 | public: |
| 237 | virtual ~MediaTransportInterface() = default; |
| 238 | |
Piotr (Peter) Slatala | e804f92 | 2018-09-25 08:40:30 -0700 | [diff] [blame] | 239 | // Start asynchronous send of audio frame. The status returned by this method |
| 240 | // only pertains to the synchronous operations (e.g. |
| 241 | // serialization/packetization), not to the asynchronous operation. |
| 242 | |
Anton Sukhanov | f60bd4b | 2018-09-05 13:41:46 -0400 | [diff] [blame] | 243 | virtual RTCError SendAudioFrame(uint64_t channel_id, |
| 244 | MediaTransportEncodedAudioFrame frame) = 0; |
| 245 | |
Piotr (Peter) Slatala | e804f92 | 2018-09-25 08:40:30 -0700 | [diff] [blame] | 246 | // Start asynchronous send of video frame. The status returned by this method |
| 247 | // only pertains to the synchronous operations (e.g. |
| 248 | // serialization/packetization), not to the asynchronous operation. |
| 249 | virtual RTCError SendVideoFrame( |
| 250 | uint64_t channel_id, |
| 251 | const MediaTransportEncodedVideoFrame& frame) = 0; |
| 252 | |
| 253 | // Requests a keyframe for the particular channel (stream). The caller should |
| 254 | // check that the keyframe is not present in a jitter buffer already (i.e. |
| 255 | // don't request a keyframe if there is one that you will get from the jitter |
| 256 | // buffer in a moment). |
| 257 | virtual RTCError RequestKeyFrame(uint64_t channel_id) = 0; |
| 258 | |
| 259 | // Sets audio sink. Sink must be unset by calling SetReceiveAudioSink(nullptr) |
| 260 | // before the media transport is destroyed or before new sink is set. |
Anton Sukhanov | f60bd4b | 2018-09-05 13:41:46 -0400 | [diff] [blame] | 261 | virtual void SetReceiveAudioSink(MediaTransportAudioSinkInterface* sink) = 0; |
| 262 | |
Piotr (Peter) Slatala | e804f92 | 2018-09-25 08:40:30 -0700 | [diff] [blame] | 263 | // Registers a video sink. Before destruction of media transport, you must |
| 264 | // pass a nullptr. |
| 265 | virtual void SetReceiveVideoSink(MediaTransportVideoSinkInterface* sink) = 0; |
| 266 | |
Piotr (Peter) Slatala | 6b9d823 | 2018-10-26 07:59:46 -0700 | [diff] [blame] | 267 | // Sets a target bitrate observer. Before media transport is destructed |
| 268 | // the observer must be unregistered (set to nullptr). |
| 269 | // A newly registered observer will be called back with the latest recorded |
| 270 | // target rate, if available. |
| 271 | virtual void SetTargetTransferRateObserver( |
| 272 | webrtc::TargetTransferRateObserver* observer) = 0; |
| 273 | |
Bjorn Mellem | c78b0ea | 2018-10-29 15:21:31 -0700 | [diff] [blame] | 274 | // Sets a state observer callback. Before media transport is destroyed, the |
| 275 | // callback must be unregistered by setting it to nullptr. |
| 276 | // A newly registered callback will be called with the current state. |
| 277 | // Media transport does not invoke this callback concurrently. |
| 278 | // TODO(mellem): Make this pure virtual once all implementations support it. |
| 279 | virtual void SetMediaTransportStateCallback( |
| 280 | MediaTransportStateCallback* callback) {} |
| 281 | |
Anton Sukhanov | f60bd4b | 2018-09-05 13:41:46 -0400 | [diff] [blame] | 282 | // TODO(sukhanov): RtcEventLogs. |
Anton Sukhanov | f60bd4b | 2018-09-05 13:41:46 -0400 | [diff] [blame] | 283 | }; |
| 284 | |
| 285 | // If media transport factory is set in peer connection factory, it will be |
| 286 | // used to create media transport for sending/receiving encoded frames and |
| 287 | // this transport will be used instead of default RTP/SRTP transport. |
| 288 | // |
| 289 | // Currently Media Transport negotiation is not supported in SDP. |
| 290 | // If application is using media transport, it must negotiate it before |
| 291 | // setting media transport factory in peer connection. |
| 292 | class MediaTransportFactory { |
| 293 | public: |
| 294 | virtual ~MediaTransportFactory() = default; |
| 295 | |
| 296 | // Creates media transport. |
| 297 | // - Does not take ownership of packet_transport or network_thread. |
| 298 | // - Does not support group calls, in 1:1 call one side must set |
| 299 | // is_caller = true and another is_caller = false. |
Piotr (Peter) Slatala | a0677d1 | 2018-10-29 07:31:42 -0700 | [diff] [blame] | 300 | // TODO(bugs.webrtc.org/9938) This constructor will be removed and replaced |
| 301 | // with the one below. |
Anton Sukhanov | f60bd4b | 2018-09-05 13:41:46 -0400 | [diff] [blame] | 302 | virtual RTCErrorOr<std::unique_ptr<MediaTransportInterface>> |
| 303 | CreateMediaTransport(rtc::PacketTransportInternal* packet_transport, |
| 304 | rtc::Thread* network_thread, |
Piotr (Peter) Slatala | a0677d1 | 2018-10-29 07:31:42 -0700 | [diff] [blame] | 305 | bool is_caller); |
| 306 | |
| 307 | // Creates media transport. |
| 308 | // - Does not take ownership of packet_transport or network_thread. |
| 309 | // TODO(bugs.webrtc.org/9938): remove default implementation once all children |
| 310 | // override it. |
| 311 | virtual RTCErrorOr<std::unique_ptr<MediaTransportInterface>> |
| 312 | CreateMediaTransport(rtc::PacketTransportInternal* packet_transport, |
| 313 | rtc::Thread* network_thread, |
Piotr (Peter) Slatala | ed7b8b1 | 2018-10-29 10:43:16 -0700 | [diff] [blame] | 314 | const MediaTransportSettings& settings); |
Anton Sukhanov | f60bd4b | 2018-09-05 13:41:46 -0400 | [diff] [blame] | 315 | }; |
| 316 | |
| 317 | } // namespace webrtc |
Anton Sukhanov | f60bd4b | 2018-09-05 13:41:46 -0400 | [diff] [blame] | 318 | #endif // API_MEDIA_TRANSPORT_INTERFACE_H_ |