Anton Sukhanov | f60bd4b | 2018-09-05 13:41:46 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | // This is EXPERIMENTAL interface for media transport. |
| 12 | // |
| 13 | // The goal is to refactor WebRTC code so that audio and video frames |
| 14 | // are sent / received through the media transport interface. This will |
| 15 | // enable different media transport implementations, including QUIC-based |
| 16 | // media transport. |
| 17 | |
| 18 | #ifndef API_MEDIA_TRANSPORT_INTERFACE_H_ |
| 19 | #define API_MEDIA_TRANSPORT_INTERFACE_H_ |
| 20 | |
| 21 | #include <memory> |
| 22 | #include <utility> |
| 23 | #include <vector> |
| 24 | |
| 25 | #include "api/rtcerror.h" |
| 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. |
| 37 | class MediaTransportEncodedAudioFrame { |
| 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. |
| 78 | std::vector<uint8_t> encoded_data) |
| 79 | : sampling_rate_hz_(sampling_rate_hz), |
| 80 | starting_sample_index_(starting_sample_index), |
| 81 | samples_per_channel_(samples_per_channel), |
| 82 | sequence_number_(sequence_number), |
| 83 | frame_type_(frame_type), |
| 84 | payload_type_(payload_type), |
| 85 | encoded_data_(std::move(encoded_data)) {} |
| 86 | |
| 87 | // Getters. |
| 88 | int sampling_rate_hz() const { return sampling_rate_hz_; } |
| 89 | int starting_sample_index() const { return starting_sample_index_; } |
| 90 | int samples_per_channel() const { return samples_per_channel_; } |
| 91 | int sequence_number() const { return sequence_number_; } |
| 92 | |
| 93 | uint8_t payload_type() const { return payload_type_; } |
| 94 | FrameType frame_type() const { return frame_type_; } |
| 95 | |
| 96 | rtc::ArrayView<const uint8_t> encoded_data() const { return encoded_data_; } |
| 97 | |
| 98 | private: |
| 99 | int sampling_rate_hz_; |
| 100 | int starting_sample_index_; |
| 101 | int samples_per_channel_; |
| 102 | |
| 103 | // TODO(sukhanov): Refactor NetEq so we don't need sequence number. |
Piotr (Peter) Slatala | e804f92 | 2018-09-25 08:40:30 -0700 | [diff] [blame^] | 104 | // Having sample_index and samples_per_channel should be enough. |
Anton Sukhanov | f60bd4b | 2018-09-05 13:41:46 -0400 | [diff] [blame] | 105 | int sequence_number_; |
| 106 | |
| 107 | FrameType frame_type_; |
| 108 | |
| 109 | // TODO(sukhanov): Consider enumerating allowed encodings and store enum |
| 110 | // instead of uint payload_type. |
| 111 | uint8_t payload_type_; |
| 112 | |
| 113 | std::vector<uint8_t> encoded_data_; |
| 114 | }; |
| 115 | |
| 116 | // Interface for receiving encoded audio frames from MediaTransportInterface |
| 117 | // implementations. |
| 118 | class MediaTransportAudioSinkInterface { |
| 119 | public: |
| 120 | virtual ~MediaTransportAudioSinkInterface() = default; |
| 121 | |
| 122 | // Called when new encoded audio frame is received. |
| 123 | virtual void OnData(uint64_t channel_id, |
| 124 | MediaTransportEncodedAudioFrame frame) = 0; |
| 125 | }; |
| 126 | |
Piotr (Peter) Slatala | e804f92 | 2018-09-25 08:40:30 -0700 | [diff] [blame^] | 127 | // Represents encoded video frame, along with the codec information. |
| 128 | class MediaTransportEncodedVideoFrame { |
| 129 | public: |
| 130 | MediaTransportEncodedVideoFrame(int64_t frame_id, |
| 131 | std::vector<int64_t> referenced_frame_ids, |
| 132 | VideoCodecType codec_type, |
| 133 | const webrtc::EncodedImage& encoded_image) |
| 134 | : codec_type_(codec_type), |
| 135 | encoded_image_(encoded_image), |
| 136 | frame_id_(frame_id), |
| 137 | referenced_frame_ids_(std::move(referenced_frame_ids)) {} |
| 138 | |
| 139 | VideoCodecType codec_type() const { return codec_type_; } |
| 140 | const webrtc::EncodedImage& encoded_image() const { return encoded_image_; } |
| 141 | |
| 142 | int64_t frame_id() const { return frame_id_; } |
| 143 | const std::vector<int64_t>& referenced_frame_ids() const { |
| 144 | return referenced_frame_ids_; |
| 145 | } |
| 146 | |
| 147 | private: |
| 148 | VideoCodecType codec_type_; |
| 149 | |
| 150 | // The buffer is not owned by the encoded image by default. On the sender it |
| 151 | // means that it will need to make a copy of it if it wants to deliver it |
| 152 | // asynchronously. |
| 153 | webrtc::EncodedImage encoded_image_; |
| 154 | |
| 155 | // Frame id uniquely identifies a frame in a stream. It needs to be unique in |
| 156 | // a given time window (i.e. technically unique identifier for the lifetime of |
| 157 | // the connection is not needed, but you need to guarantee that remote side |
| 158 | // got rid of the previous frame_id if you plan to reuse it). |
| 159 | // |
| 160 | // It is required by a remote jitter buffer, and is the same as |
| 161 | // EncodedFrame::id::picture_id. |
| 162 | // |
| 163 | // This data must be opaque to the media transport, and media transport should |
| 164 | // itself not make any assumptions about what it is and its uniqueness. |
| 165 | int64_t frame_id_; |
| 166 | |
| 167 | // A single frame might depend on other frames. This is set of identifiers on |
| 168 | // which the current frame depends. |
| 169 | std::vector<int64_t> referenced_frame_ids_; |
| 170 | }; |
| 171 | |
| 172 | // Interface for receiving encoded video frames from MediaTransportInterface |
| 173 | // implementations. |
| 174 | class MediaTransportVideoSinkInterface { |
| 175 | public: |
| 176 | virtual ~MediaTransportVideoSinkInterface() = default; |
| 177 | |
| 178 | // Called when new encoded video frame is received. |
| 179 | virtual void OnData(uint64_t channel_id, |
| 180 | MediaTransportEncodedVideoFrame frame) = 0; |
| 181 | |
| 182 | // Called when the request for keyframe is received. |
| 183 | virtual void OnKeyFrameRequested(uint64_t channel_id) = 0; |
| 184 | }; |
| 185 | |
Anton Sukhanov | f60bd4b | 2018-09-05 13:41:46 -0400 | [diff] [blame] | 186 | // Media transport interface for sending / receiving encoded audio/video frames |
| 187 | // and receiving bandwidth estimate update from congestion control. |
| 188 | class MediaTransportInterface { |
| 189 | public: |
| 190 | virtual ~MediaTransportInterface() = default; |
| 191 | |
Piotr (Peter) Slatala | e804f92 | 2018-09-25 08:40:30 -0700 | [diff] [blame^] | 192 | // Start asynchronous send of audio frame. The status returned by this method |
| 193 | // only pertains to the synchronous operations (e.g. |
| 194 | // serialization/packetization), not to the asynchronous operation. |
| 195 | |
Anton Sukhanov | f60bd4b | 2018-09-05 13:41:46 -0400 | [diff] [blame] | 196 | virtual RTCError SendAudioFrame(uint64_t channel_id, |
| 197 | MediaTransportEncodedAudioFrame frame) = 0; |
| 198 | |
Piotr (Peter) Slatala | e804f92 | 2018-09-25 08:40:30 -0700 | [diff] [blame^] | 199 | // Start asynchronous send of video frame. The status returned by this method |
| 200 | // only pertains to the synchronous operations (e.g. |
| 201 | // serialization/packetization), not to the asynchronous operation. |
| 202 | virtual RTCError SendVideoFrame( |
| 203 | uint64_t channel_id, |
| 204 | const MediaTransportEncodedVideoFrame& frame) = 0; |
| 205 | |
| 206 | // Requests a keyframe for the particular channel (stream). The caller should |
| 207 | // check that the keyframe is not present in a jitter buffer already (i.e. |
| 208 | // don't request a keyframe if there is one that you will get from the jitter |
| 209 | // buffer in a moment). |
| 210 | virtual RTCError RequestKeyFrame(uint64_t channel_id) = 0; |
| 211 | |
| 212 | // Sets audio sink. Sink must be unset by calling SetReceiveAudioSink(nullptr) |
| 213 | // before the media transport is destroyed or before new sink is set. |
Anton Sukhanov | f60bd4b | 2018-09-05 13:41:46 -0400 | [diff] [blame] | 214 | virtual void SetReceiveAudioSink(MediaTransportAudioSinkInterface* sink) = 0; |
| 215 | |
Piotr (Peter) Slatala | e804f92 | 2018-09-25 08:40:30 -0700 | [diff] [blame^] | 216 | // Registers a video sink. Before destruction of media transport, you must |
| 217 | // pass a nullptr. |
| 218 | virtual void SetReceiveVideoSink(MediaTransportVideoSinkInterface* sink) = 0; |
| 219 | |
Anton Sukhanov | f60bd4b | 2018-09-05 13:41:46 -0400 | [diff] [blame] | 220 | // TODO(sukhanov): RtcEventLogs. |
Anton Sukhanov | f60bd4b | 2018-09-05 13:41:46 -0400 | [diff] [blame] | 221 | // TODO(sukhanov): Bandwidth updates. |
| 222 | }; |
| 223 | |
| 224 | // If media transport factory is set in peer connection factory, it will be |
| 225 | // used to create media transport for sending/receiving encoded frames and |
| 226 | // this transport will be used instead of default RTP/SRTP transport. |
| 227 | // |
| 228 | // Currently Media Transport negotiation is not supported in SDP. |
| 229 | // If application is using media transport, it must negotiate it before |
| 230 | // setting media transport factory in peer connection. |
| 231 | class MediaTransportFactory { |
| 232 | public: |
| 233 | virtual ~MediaTransportFactory() = default; |
| 234 | |
| 235 | // Creates media transport. |
| 236 | // - Does not take ownership of packet_transport or network_thread. |
| 237 | // - Does not support group calls, in 1:1 call one side must set |
| 238 | // is_caller = true and another is_caller = false. |
| 239 | virtual RTCErrorOr<std::unique_ptr<MediaTransportInterface>> |
| 240 | CreateMediaTransport(rtc::PacketTransportInternal* packet_transport, |
| 241 | rtc::Thread* network_thread, |
| 242 | bool is_caller) = 0; |
| 243 | }; |
| 244 | |
| 245 | } // namespace webrtc |
Anton Sukhanov | f60bd4b | 2018-09-05 13:41:46 -0400 | [diff] [blame] | 246 | #endif // API_MEDIA_TRANSPORT_INTERFACE_H_ |