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