blob: 0e6a3fc2702ae400f31cae69526c2a3776846c88 [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)
31
32namespace rtc {
33class PacketTransportInternal;
34class Thread;
35} // namespace rtc
36
37namespace webrtc {
38
Piotr (Peter) Slatalaa0677d12018-10-29 07:31:42 -070039// A collection of settings for creation of media transport.
40struct MediaTransportSettings final {
41 MediaTransportSettings();
42 ~MediaTransportSettings();
43
44 // Group calls are not currently supported, in 1:1 call one side must set
45 // is_caller = true and another is_caller = false.
46 bool is_caller;
47
48 // Must be set if a pre-shared key is used for the call.
49 absl::optional<std::string> pre_shared_key;
50};
51
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -040052// Represents encoded audio frame in any encoding (type of encoding is opaque).
53// To avoid copying of encoded data use move semantics when passing by value.
Piotr (Peter) Slatalae0c2e972018-10-08 09:43:21 -070054class MediaTransportEncodedAudioFrame final {
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -040055 public:
56 enum class FrameType {
57 // Normal audio frame (equivalent to webrtc::kAudioFrameSpeech).
58 kSpeech,
59
60 // DTX frame (equivalent to webrtc::kAudioFrameCN).
Niels Möller7d76a312018-10-26 12:57:07 +020061 // DTX frame (equivalent to webrtc::kAudioFrameCN).
62 kDiscontinuousTransmission,
63 // TODO(nisse): Mis-spelled version, update users, then delete.
64 kDiscountinuousTransmission = kDiscontinuousTransmission,
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -040065 };
66
67 MediaTransportEncodedAudioFrame(
68 // Audio sampling rate, for example 48000.
69 int sampling_rate_hz,
70
71 // Starting sample index of the frame, i.e. how many audio samples were
72 // before this frame since the beginning of the call or beginning of time
73 // in one channel (the starting point should not matter for NetEq). In
74 // WebRTC it is used as a timestamp of the frame.
75 // TODO(sukhanov): Starting_sample_index is currently adjusted on the
76 // receiver side in RTP path. Non-RTP implementations should preserve it.
77 // For NetEq initial offset should not matter so we should consider fixing
78 // RTP path.
79 int starting_sample_index,
80
81 // Number of audio samples in audio frame in 1 channel.
82 int samples_per_channel,
83
84 // Sequence number of the frame in the order sent, it is currently
85 // required by NetEq, but we can fix NetEq, because starting_sample_index
86 // should be enough.
87 int sequence_number,
88
89 // If audio frame is a speech or discontinued transmission.
90 FrameType frame_type,
91
92 // Opaque payload type. In RTP codepath payload type is stored in RTP
93 // header. In other implementations it should be simply passed through the
94 // wire -- it's needed for decoder.
95 uint8_t payload_type,
96
97 // Vector with opaque encoded data.
Niels Möller3a742392018-10-08 11:13:58 +020098 std::vector<uint8_t> encoded_data);
99
100 ~MediaTransportEncodedAudioFrame();
Piotr (Peter) Slatalae0c2e972018-10-08 09:43:21 -0700101 MediaTransportEncodedAudioFrame(const MediaTransportEncodedAudioFrame&);
102 MediaTransportEncodedAudioFrame& operator=(
103 const MediaTransportEncodedAudioFrame& other);
104 MediaTransportEncodedAudioFrame& operator=(
105 MediaTransportEncodedAudioFrame&& other);
106 MediaTransportEncodedAudioFrame(MediaTransportEncodedAudioFrame&&);
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400107
108 // Getters.
109 int sampling_rate_hz() const { return sampling_rate_hz_; }
110 int starting_sample_index() const { return starting_sample_index_; }
111 int samples_per_channel() const { return samples_per_channel_; }
112 int sequence_number() const { return sequence_number_; }
113
114 uint8_t payload_type() const { return payload_type_; }
115 FrameType frame_type() const { return frame_type_; }
116
117 rtc::ArrayView<const uint8_t> encoded_data() const { return encoded_data_; }
118
119 private:
120 int sampling_rate_hz_;
121 int starting_sample_index_;
122 int samples_per_channel_;
123
124 // TODO(sukhanov): Refactor NetEq so we don't need sequence number.
Piotr (Peter) Slatalae804f922018-09-25 08:40:30 -0700125 // Having sample_index and samples_per_channel should be enough.
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400126 int sequence_number_;
127
128 FrameType frame_type_;
129
130 // TODO(sukhanov): Consider enumerating allowed encodings and store enum
131 // instead of uint payload_type.
132 uint8_t payload_type_;
133
134 std::vector<uint8_t> encoded_data_;
135};
136
137// Interface for receiving encoded audio frames from MediaTransportInterface
138// implementations.
139class MediaTransportAudioSinkInterface {
140 public:
141 virtual ~MediaTransportAudioSinkInterface() = default;
142
143 // Called when new encoded audio frame is received.
144 virtual void OnData(uint64_t channel_id,
145 MediaTransportEncodedAudioFrame frame) = 0;
146};
147
Piotr (Peter) Slatalae804f922018-09-25 08:40:30 -0700148// Represents encoded video frame, along with the codec information.
Piotr (Peter) Slatalae0c2e972018-10-08 09:43:21 -0700149class MediaTransportEncodedVideoFrame final {
Piotr (Peter) Slatalae804f922018-09-25 08:40:30 -0700150 public:
151 MediaTransportEncodedVideoFrame(int64_t frame_id,
152 std::vector<int64_t> referenced_frame_ids,
153 VideoCodecType codec_type,
Niels Möller3a742392018-10-08 11:13:58 +0200154 const webrtc::EncodedImage& encoded_image);
155 ~MediaTransportEncodedVideoFrame();
Piotr (Peter) Slatalae0c2e972018-10-08 09:43:21 -0700156 MediaTransportEncodedVideoFrame(const MediaTransportEncodedVideoFrame&);
157 MediaTransportEncodedVideoFrame& operator=(
158 const MediaTransportEncodedVideoFrame& other);
159 MediaTransportEncodedVideoFrame& operator=(
160 MediaTransportEncodedVideoFrame&& other);
161 MediaTransportEncodedVideoFrame(MediaTransportEncodedVideoFrame&&);
Piotr (Peter) Slatalae804f922018-09-25 08:40:30 -0700162
163 VideoCodecType codec_type() const { return codec_type_; }
164 const webrtc::EncodedImage& encoded_image() const { return encoded_image_; }
165
166 int64_t frame_id() const { return frame_id_; }
167 const std::vector<int64_t>& referenced_frame_ids() const {
168 return referenced_frame_ids_;
169 }
170
171 private:
172 VideoCodecType codec_type_;
173
174 // The buffer is not owned by the encoded image by default. On the sender it
175 // means that it will need to make a copy of it if it wants to deliver it
176 // asynchronously.
177 webrtc::EncodedImage encoded_image_;
178
179 // Frame id uniquely identifies a frame in a stream. It needs to be unique in
180 // a given time window (i.e. technically unique identifier for the lifetime of
181 // the connection is not needed, but you need to guarantee that remote side
182 // got rid of the previous frame_id if you plan to reuse it).
183 //
184 // It is required by a remote jitter buffer, and is the same as
185 // EncodedFrame::id::picture_id.
186 //
187 // This data must be opaque to the media transport, and media transport should
188 // itself not make any assumptions about what it is and its uniqueness.
189 int64_t frame_id_;
190
191 // A single frame might depend on other frames. This is set of identifiers on
192 // which the current frame depends.
193 std::vector<int64_t> referenced_frame_ids_;
194};
195
196// Interface for receiving encoded video frames from MediaTransportInterface
197// implementations.
198class MediaTransportVideoSinkInterface {
199 public:
200 virtual ~MediaTransportVideoSinkInterface() = default;
201
202 // Called when new encoded video frame is received.
203 virtual void OnData(uint64_t channel_id,
204 MediaTransportEncodedVideoFrame frame) = 0;
205
206 // Called when the request for keyframe is received.
207 virtual void OnKeyFrameRequested(uint64_t channel_id) = 0;
208};
209
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400210// Media transport interface for sending / receiving encoded audio/video frames
211// and receiving bandwidth estimate update from congestion control.
212class MediaTransportInterface {
213 public:
214 virtual ~MediaTransportInterface() = default;
215
Piotr (Peter) Slatalae804f922018-09-25 08:40:30 -0700216 // Start asynchronous send of audio frame. The status returned by this method
217 // only pertains to the synchronous operations (e.g.
218 // serialization/packetization), not to the asynchronous operation.
219
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400220 virtual RTCError SendAudioFrame(uint64_t channel_id,
221 MediaTransportEncodedAudioFrame frame) = 0;
222
Piotr (Peter) Slatalae804f922018-09-25 08:40:30 -0700223 // Start asynchronous send of video frame. The status returned by this method
224 // only pertains to the synchronous operations (e.g.
225 // serialization/packetization), not to the asynchronous operation.
226 virtual RTCError SendVideoFrame(
227 uint64_t channel_id,
228 const MediaTransportEncodedVideoFrame& frame) = 0;
229
230 // Requests a keyframe for the particular channel (stream). The caller should
231 // check that the keyframe is not present in a jitter buffer already (i.e.
232 // don't request a keyframe if there is one that you will get from the jitter
233 // buffer in a moment).
234 virtual RTCError RequestKeyFrame(uint64_t channel_id) = 0;
235
236 // Sets audio sink. Sink must be unset by calling SetReceiveAudioSink(nullptr)
237 // before the media transport is destroyed or before new sink is set.
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400238 virtual void SetReceiveAudioSink(MediaTransportAudioSinkInterface* sink) = 0;
239
Piotr (Peter) Slatalae804f922018-09-25 08:40:30 -0700240 // Registers a video sink. Before destruction of media transport, you must
241 // pass a nullptr.
242 virtual void SetReceiveVideoSink(MediaTransportVideoSinkInterface* sink) = 0;
243
Piotr (Peter) Slatala6b9d8232018-10-26 07:59:46 -0700244 // Sets a target bitrate observer. Before media transport is destructed
245 // the observer must be unregistered (set to nullptr).
246 // A newly registered observer will be called back with the latest recorded
247 // target rate, if available.
248 virtual void SetTargetTransferRateObserver(
249 webrtc::TargetTransferRateObserver* observer) = 0;
250
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400251 // TODO(sukhanov): RtcEventLogs.
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400252};
253
254// If media transport factory is set in peer connection factory, it will be
255// used to create media transport for sending/receiving encoded frames and
256// this transport will be used instead of default RTP/SRTP transport.
257//
258// Currently Media Transport negotiation is not supported in SDP.
259// If application is using media transport, it must negotiate it before
260// setting media transport factory in peer connection.
261class MediaTransportFactory {
262 public:
263 virtual ~MediaTransportFactory() = default;
264
265 // Creates media transport.
266 // - Does not take ownership of packet_transport or network_thread.
267 // - Does not support group calls, in 1:1 call one side must set
268 // is_caller = true and another is_caller = false.
Piotr (Peter) Slatalaa0677d12018-10-29 07:31:42 -0700269 // TODO(bugs.webrtc.org/9938) This constructor will be removed and replaced
270 // with the one below.
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400271 virtual RTCErrorOr<std::unique_ptr<MediaTransportInterface>>
272 CreateMediaTransport(rtc::PacketTransportInternal* packet_transport,
273 rtc::Thread* network_thread,
Piotr (Peter) Slatalaa0677d12018-10-29 07:31:42 -0700274 bool is_caller);
275
276 // Creates media transport.
277 // - Does not take ownership of packet_transport or network_thread.
278 // TODO(bugs.webrtc.org/9938): remove default implementation once all children
279 // override it.
280 virtual RTCErrorOr<std::unique_ptr<MediaTransportInterface>>
281 CreateMediaTransport(rtc::PacketTransportInternal* packet_transport,
282 rtc::Thread* network_thread,
283 const MediaTransportSettings settings);
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400284};
285
286} // namespace webrtc
Anton Sukhanovf60bd4b2018-09-05 13:41:46 -0400287#endif // API_MEDIA_TRANSPORT_INTERFACE_H_