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