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 | |
| 20 | #include <memory> |
| 21 | #include <utility> |
| 22 | #include <vector> |
| 23 | |
| 24 | #include "api/rtcerror.h" |
Niels Möller | 3a74239 | 2018-10-08 11:13:58 +0200 | [diff] [blame] | 25 | #include "api/video/encoded_image.h" |
Anton Sukhanov | f60bd4b | 2018-09-05 13:41:46 -0400 | [diff] [blame] | 26 | #include "common_types.h" // NOLINT(build/include) |
| 27 | |
| 28 | namespace rtc { |
| 29 | class PacketTransportInternal; |
| 30 | class Thread; |
| 31 | } // namespace rtc |
| 32 | |
| 33 | namespace webrtc { |
| 34 | |
| 35 | // Represents encoded audio frame in any encoding (type of encoding is opaque). |
| 36 | // 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] | 37 | class MediaTransportEncodedAudioFrame final { |
Anton Sukhanov | f60bd4b | 2018-09-05 13:41:46 -0400 | [diff] [blame] | 38 | public: |
| 39 | enum class FrameType { |
| 40 | // Normal audio frame (equivalent to webrtc::kAudioFrameSpeech). |
| 41 | kSpeech, |
| 42 | |
| 43 | // DTX frame (equivalent to webrtc::kAudioFrameCN). |
| 44 | kDiscountinuousTransmission, |
| 45 | }; |
| 46 | |
| 47 | MediaTransportEncodedAudioFrame( |
| 48 | // Audio sampling rate, for example 48000. |
| 49 | int sampling_rate_hz, |
| 50 | |
| 51 | // Starting sample index of the frame, i.e. how many audio samples were |
| 52 | // before this frame since the beginning of the call or beginning of time |
| 53 | // in one channel (the starting point should not matter for NetEq). In |
| 54 | // WebRTC it is used as a timestamp of the frame. |
| 55 | // TODO(sukhanov): Starting_sample_index is currently adjusted on the |
| 56 | // receiver side in RTP path. Non-RTP implementations should preserve it. |
| 57 | // For NetEq initial offset should not matter so we should consider fixing |
| 58 | // RTP path. |
| 59 | int starting_sample_index, |
| 60 | |
| 61 | // Number of audio samples in audio frame in 1 channel. |
| 62 | int samples_per_channel, |
| 63 | |
| 64 | // Sequence number of the frame in the order sent, it is currently |
| 65 | // required by NetEq, but we can fix NetEq, because starting_sample_index |
| 66 | // should be enough. |
| 67 | int sequence_number, |
| 68 | |
| 69 | // If audio frame is a speech or discontinued transmission. |
| 70 | FrameType frame_type, |
| 71 | |
| 72 | // Opaque payload type. In RTP codepath payload type is stored in RTP |
| 73 | // header. In other implementations it should be simply passed through the |
| 74 | // wire -- it's needed for decoder. |
| 75 | uint8_t payload_type, |
| 76 | |
| 77 | // Vector with opaque encoded data. |
Niels Möller | 3a74239 | 2018-10-08 11:13:58 +0200 | [diff] [blame] | 78 | std::vector<uint8_t> encoded_data); |
| 79 | |
| 80 | ~MediaTransportEncodedAudioFrame(); |
Piotr (Peter) Slatala | e0c2e97 | 2018-10-08 09:43:21 -0700 | [diff] [blame] | 81 | MediaTransportEncodedAudioFrame(const MediaTransportEncodedAudioFrame&); |
| 82 | MediaTransportEncodedAudioFrame& operator=( |
| 83 | const MediaTransportEncodedAudioFrame& other); |
| 84 | MediaTransportEncodedAudioFrame& operator=( |
| 85 | MediaTransportEncodedAudioFrame&& other); |
| 86 | MediaTransportEncodedAudioFrame(MediaTransportEncodedAudioFrame&&); |
Anton Sukhanov | f60bd4b | 2018-09-05 13:41:46 -0400 | [diff] [blame] | 87 | |
| 88 | // Getters. |
| 89 | int sampling_rate_hz() const { return sampling_rate_hz_; } |
| 90 | int starting_sample_index() const { return starting_sample_index_; } |
| 91 | int samples_per_channel() const { return samples_per_channel_; } |
| 92 | int sequence_number() const { return sequence_number_; } |
| 93 | |
| 94 | uint8_t payload_type() const { return payload_type_; } |
| 95 | FrameType frame_type() const { return frame_type_; } |
| 96 | |
| 97 | rtc::ArrayView<const uint8_t> encoded_data() const { return encoded_data_; } |
| 98 | |
| 99 | private: |
| 100 | int sampling_rate_hz_; |
| 101 | int starting_sample_index_; |
| 102 | int samples_per_channel_; |
| 103 | |
| 104 | // TODO(sukhanov): Refactor NetEq so we don't need sequence number. |
Piotr (Peter) Slatala | e804f92 | 2018-09-25 08:40:30 -0700 | [diff] [blame] | 105 | // Having sample_index and samples_per_channel should be enough. |
Anton Sukhanov | f60bd4b | 2018-09-05 13:41:46 -0400 | [diff] [blame] | 106 | int sequence_number_; |
| 107 | |
| 108 | FrameType frame_type_; |
| 109 | |
| 110 | // TODO(sukhanov): Consider enumerating allowed encodings and store enum |
| 111 | // instead of uint payload_type. |
| 112 | uint8_t payload_type_; |
| 113 | |
| 114 | std::vector<uint8_t> encoded_data_; |
| 115 | }; |
| 116 | |
| 117 | // Interface for receiving encoded audio frames from MediaTransportInterface |
| 118 | // implementations. |
| 119 | class MediaTransportAudioSinkInterface { |
| 120 | public: |
| 121 | virtual ~MediaTransportAudioSinkInterface() = default; |
| 122 | |
| 123 | // Called when new encoded audio frame is received. |
| 124 | virtual void OnData(uint64_t channel_id, |
| 125 | MediaTransportEncodedAudioFrame frame) = 0; |
| 126 | }; |
| 127 | |
Piotr (Peter) Slatala | e804f92 | 2018-09-25 08:40:30 -0700 | [diff] [blame] | 128 | // Represents encoded video frame, along with the codec information. |
Piotr (Peter) Slatala | e0c2e97 | 2018-10-08 09:43:21 -0700 | [diff] [blame] | 129 | class MediaTransportEncodedVideoFrame final { |
Piotr (Peter) Slatala | e804f92 | 2018-09-25 08:40:30 -0700 | [diff] [blame] | 130 | public: |
| 131 | MediaTransportEncodedVideoFrame(int64_t frame_id, |
| 132 | std::vector<int64_t> referenced_frame_ids, |
| 133 | VideoCodecType codec_type, |
Niels Möller | 3a74239 | 2018-10-08 11:13:58 +0200 | [diff] [blame] | 134 | const webrtc::EncodedImage& encoded_image); |
| 135 | ~MediaTransportEncodedVideoFrame(); |
Piotr (Peter) Slatala | e0c2e97 | 2018-10-08 09:43:21 -0700 | [diff] [blame] | 136 | MediaTransportEncodedVideoFrame(const MediaTransportEncodedVideoFrame&); |
| 137 | MediaTransportEncodedVideoFrame& operator=( |
| 138 | const MediaTransportEncodedVideoFrame& other); |
| 139 | MediaTransportEncodedVideoFrame& operator=( |
| 140 | MediaTransportEncodedVideoFrame&& other); |
| 141 | MediaTransportEncodedVideoFrame(MediaTransportEncodedVideoFrame&&); |
Piotr (Peter) Slatala | e804f92 | 2018-09-25 08:40:30 -0700 | [diff] [blame] | 142 | |
| 143 | VideoCodecType codec_type() const { return codec_type_; } |
| 144 | const webrtc::EncodedImage& encoded_image() const { return encoded_image_; } |
| 145 | |
| 146 | int64_t frame_id() const { return frame_id_; } |
| 147 | const std::vector<int64_t>& referenced_frame_ids() const { |
| 148 | return referenced_frame_ids_; |
| 149 | } |
| 150 | |
| 151 | private: |
| 152 | VideoCodecType codec_type_; |
| 153 | |
| 154 | // The buffer is not owned by the encoded image by default. On the sender it |
| 155 | // means that it will need to make a copy of it if it wants to deliver it |
| 156 | // asynchronously. |
| 157 | webrtc::EncodedImage encoded_image_; |
| 158 | |
| 159 | // Frame id uniquely identifies a frame in a stream. It needs to be unique in |
| 160 | // a given time window (i.e. technically unique identifier for the lifetime of |
| 161 | // the connection is not needed, but you need to guarantee that remote side |
| 162 | // got rid of the previous frame_id if you plan to reuse it). |
| 163 | // |
| 164 | // It is required by a remote jitter buffer, and is the same as |
| 165 | // EncodedFrame::id::picture_id. |
| 166 | // |
| 167 | // This data must be opaque to the media transport, and media transport should |
| 168 | // itself not make any assumptions about what it is and its uniqueness. |
| 169 | int64_t frame_id_; |
| 170 | |
| 171 | // A single frame might depend on other frames. This is set of identifiers on |
| 172 | // which the current frame depends. |
| 173 | std::vector<int64_t> referenced_frame_ids_; |
| 174 | }; |
| 175 | |
| 176 | // Interface for receiving encoded video frames from MediaTransportInterface |
| 177 | // implementations. |
| 178 | class MediaTransportVideoSinkInterface { |
| 179 | public: |
| 180 | virtual ~MediaTransportVideoSinkInterface() = default; |
| 181 | |
| 182 | // Called when new encoded video frame is received. |
| 183 | virtual void OnData(uint64_t channel_id, |
| 184 | MediaTransportEncodedVideoFrame frame) = 0; |
| 185 | |
| 186 | // Called when the request for keyframe is received. |
| 187 | virtual void OnKeyFrameRequested(uint64_t channel_id) = 0; |
| 188 | }; |
| 189 | |
Anton Sukhanov | f60bd4b | 2018-09-05 13:41:46 -0400 | [diff] [blame] | 190 | // Media transport interface for sending / receiving encoded audio/video frames |
| 191 | // and receiving bandwidth estimate update from congestion control. |
| 192 | class MediaTransportInterface { |
| 193 | public: |
| 194 | virtual ~MediaTransportInterface() = default; |
| 195 | |
Piotr (Peter) Slatala | e804f92 | 2018-09-25 08:40:30 -0700 | [diff] [blame] | 196 | // Start asynchronous send of audio frame. The status returned by this method |
| 197 | // only pertains to the synchronous operations (e.g. |
| 198 | // serialization/packetization), not to the asynchronous operation. |
| 199 | |
Anton Sukhanov | f60bd4b | 2018-09-05 13:41:46 -0400 | [diff] [blame] | 200 | virtual RTCError SendAudioFrame(uint64_t channel_id, |
| 201 | MediaTransportEncodedAudioFrame frame) = 0; |
| 202 | |
Piotr (Peter) Slatala | e804f92 | 2018-09-25 08:40:30 -0700 | [diff] [blame] | 203 | // Start asynchronous send of video frame. The status returned by this method |
| 204 | // only pertains to the synchronous operations (e.g. |
| 205 | // serialization/packetization), not to the asynchronous operation. |
| 206 | virtual RTCError SendVideoFrame( |
| 207 | uint64_t channel_id, |
| 208 | const MediaTransportEncodedVideoFrame& frame) = 0; |
| 209 | |
| 210 | // Requests a keyframe for the particular channel (stream). The caller should |
| 211 | // check that the keyframe is not present in a jitter buffer already (i.e. |
| 212 | // don't request a keyframe if there is one that you will get from the jitter |
| 213 | // buffer in a moment). |
| 214 | virtual RTCError RequestKeyFrame(uint64_t channel_id) = 0; |
| 215 | |
| 216 | // Sets audio sink. Sink must be unset by calling SetReceiveAudioSink(nullptr) |
| 217 | // before the media transport is destroyed or before new sink is set. |
Anton Sukhanov | f60bd4b | 2018-09-05 13:41:46 -0400 | [diff] [blame] | 218 | virtual void SetReceiveAudioSink(MediaTransportAudioSinkInterface* sink) = 0; |
| 219 | |
Piotr (Peter) Slatala | e804f92 | 2018-09-25 08:40:30 -0700 | [diff] [blame] | 220 | // Registers a video sink. Before destruction of media transport, you must |
| 221 | // pass a nullptr. |
| 222 | virtual void SetReceiveVideoSink(MediaTransportVideoSinkInterface* sink) = 0; |
| 223 | |
Anton Sukhanov | f60bd4b | 2018-09-05 13:41:46 -0400 | [diff] [blame] | 224 | // TODO(sukhanov): RtcEventLogs. |
Anton Sukhanov | f60bd4b | 2018-09-05 13:41:46 -0400 | [diff] [blame] | 225 | // TODO(sukhanov): Bandwidth updates. |
| 226 | }; |
| 227 | |
| 228 | // If media transport factory is set in peer connection factory, it will be |
| 229 | // used to create media transport for sending/receiving encoded frames and |
| 230 | // this transport will be used instead of default RTP/SRTP transport. |
| 231 | // |
| 232 | // Currently Media Transport negotiation is not supported in SDP. |
| 233 | // If application is using media transport, it must negotiate it before |
| 234 | // setting media transport factory in peer connection. |
| 235 | class MediaTransportFactory { |
| 236 | public: |
| 237 | virtual ~MediaTransportFactory() = default; |
| 238 | |
| 239 | // Creates media transport. |
| 240 | // - Does not take ownership of packet_transport or network_thread. |
| 241 | // - Does not support group calls, in 1:1 call one side must set |
| 242 | // is_caller = true and another is_caller = false. |
| 243 | virtual RTCErrorOr<std::unique_ptr<MediaTransportInterface>> |
| 244 | CreateMediaTransport(rtc::PacketTransportInternal* packet_transport, |
| 245 | rtc::Thread* network_thread, |
| 246 | bool is_caller) = 0; |
| 247 | }; |
| 248 | |
| 249 | } // namespace webrtc |
Anton Sukhanov | f60bd4b | 2018-09-05 13:41:46 -0400 | [diff] [blame] | 250 | #endif // API_MEDIA_TRANSPORT_INTERFACE_H_ |