blob: 5d6544716d78cd5b10fb7cbb68c7f01b066ef0e9 [file] [log] [blame]
Henrik Kjellanderbd0ae452016-02-10 09:53:121/*
kjellander95ed4e62016-02-10 15:54:432 * Copyright 2012 The WebRTC project authors. All Rights Reserved.
Henrik Kjellanderbd0ae452016-02-10 09:53:123 *
kjellander95ed4e62016-02-10 15:54:434 * 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.
Henrik Kjellanderbd0ae452016-02-10 09:53:129 */
10
11// This file contains interfaces for MediaStream, MediaTrack and MediaSource.
12// These interfaces are used for implementing MediaStream and MediaTrack as
13// defined in http://dev.w3.org/2011/webrtc/editor/webrtc.html#stream-api. These
14// interfaces must be used only with PeerConnection. PeerConnectionManager
15// interface provides the factory methods to create MediaStream and MediaTracks.
16
17#ifndef WEBRTC_API_MEDIASTREAMINTERFACE_H_
18#define WEBRTC_API_MEDIASTREAMINTERFACE_H_
19
pbos35703e82017-01-02 14:44:4120#include <stddef.h>
21
Henrik Kjellanderbd0ae452016-02-10 09:53:1222#include <string>
23#include <vector>
24
kwiberg5b189672017-09-05 15:43:1325#include "webrtc/api/optional.h"
nissef0daa062017-01-10 15:44:2626#include "webrtc/api/video/video_frame.h"
zhihuangf91805c2017-06-15 19:52:3227// TODO(zhihuang): Remove unrelated headers once downstream applications stop
28// relying on them; they were previously transitively included by
29// mediachannel.h, which is no longer a dependency of this file.
zhihuangf91805c2017-06-15 19:52:3230#include "webrtc/media/base/streamparams.h"
Henrik Kjellanderbd0ae452016-02-10 09:53:1231#include "webrtc/media/base/videosinkinterface.h"
nisse0a70b8d2016-02-26 09:24:5832#include "webrtc/media/base/videosourceinterface.h"
Edward Lemur76de83e2017-07-06 17:44:3433#include "webrtc/rtc_base/ratetracker.h"
34#include "webrtc/rtc_base/refcount.h"
35#include "webrtc/rtc_base/scoped_ref_ptr.h"
36#include "webrtc/rtc_base/thread.h"
37#include "webrtc/rtc_base/timeutils.h"
Henrik Kjellanderbd0ae452016-02-10 09:53:1238
Henrik Kjellanderbd0ae452016-02-10 09:53:1239namespace webrtc {
40
41// Generic observer interface.
42class ObserverInterface {
43 public:
44 virtual void OnChanged() = 0;
45
46 protected:
47 virtual ~ObserverInterface() {}
48};
49
50class NotifierInterface {
51 public:
52 virtual void RegisterObserver(ObserverInterface* observer) = 0;
53 virtual void UnregisterObserver(ObserverInterface* observer) = 0;
54
55 virtual ~NotifierInterface() {}
56};
57
deadbeef6e09aef2017-02-08 09:38:2158// Base class for sources. A MediaStreamTrack has an underlying source that
59// provides media. A source can be shared by multiple tracks.
Henrik Kjellanderbd0ae452016-02-10 09:53:1260class MediaSourceInterface : public rtc::RefCountInterface,
61 public NotifierInterface {
62 public:
63 enum SourceState {
64 kInitializing,
65 kLive,
66 kEnded,
67 kMuted
68 };
69
70 virtual SourceState state() const = 0;
71
72 virtual bool remote() const = 0;
73
74 protected:
75 virtual ~MediaSourceInterface() {}
76};
77
deadbeef6e09aef2017-02-08 09:38:2178// C++ version of MediaStreamTrack.
79// See: https://www.w3.org/TR/mediacapture-streams/#mediastreamtrack
Henrik Kjellanderbd0ae452016-02-10 09:53:1280class MediaStreamTrackInterface : public rtc::RefCountInterface,
81 public NotifierInterface {
82 public:
83 enum TrackState {
perkj8dde01d2016-03-23 07:33:5684 kLive,
85 kEnded,
Henrik Kjellanderbd0ae452016-02-10 09:53:1286 };
87
88 static const char kAudioKind[];
89 static const char kVideoKind[];
90
nisse66a510a2016-04-01 08:10:4291 // The kind() method must return kAudioKind only if the object is a
92 // subclass of AudioTrackInterface, and kVideoKind only if the
93 // object is a subclass of VideoTrackInterface. It is typically used
94 // to protect a static_cast<> to the corresponding subclass.
Henrik Kjellanderbd0ae452016-02-10 09:53:1295 virtual std::string kind() const = 0;
deadbeef6e09aef2017-02-08 09:38:2196
97 // Track identifier.
Henrik Kjellanderbd0ae452016-02-10 09:53:1298 virtual std::string id() const = 0;
deadbeef6e09aef2017-02-08 09:38:2199
100 // A disabled track will produce silence (if audio) or black frames (if
101 // video). Can be disabled and re-enabled.
Henrik Kjellanderbd0ae452016-02-10 09:53:12102 virtual bool enabled() const = 0;
Henrik Kjellanderbd0ae452016-02-10 09:53:12103 virtual bool set_enabled(bool enable) = 0;
Henrik Kjellanderbd0ae452016-02-10 09:53:12104
deadbeef6e09aef2017-02-08 09:38:21105 // Live or ended. A track will never be live again after becoming ended.
106 virtual TrackState state() const = 0;
107
Henrik Kjellanderbd0ae452016-02-10 09:53:12108 protected:
109 virtual ~MediaStreamTrackInterface() {}
110};
111
deadbeef6e09aef2017-02-08 09:38:21112// VideoTrackSourceInterface is a reference counted source used for
113// VideoTracks. The same source can be used by multiple VideoTracks.
perkjcf8bdaf2017-08-01 06:22:01114// VideoTrackSourceInterface is designed to be invoked on the signaling thread
115// except for rtc::VideoSourceInterface<VideoFrame> methods that will be invoked
116// on the worker thread via a VideoTrack. A custom implementation of a source
117// can inherit AdaptedVideoTrackSource instead of directly implementing this
118// interface.
perkj2a4ff982016-03-08 00:27:48119class VideoTrackSourceInterface
120 : public MediaSourceInterface,
nissee8431852016-11-11 11:55:13121 public rtc::VideoSourceInterface<VideoFrame> {
perkj2a4ff982016-03-08 00:27:48122 public:
nisse66a510a2016-04-01 08:10:42123 struct Stats {
124 // Original size of captured frame, before video adaptation.
125 int input_width;
126 int input_height;
127 };
perkj2a4ff982016-03-08 00:27:48128
perkjf4524c42016-03-09 01:39:17129 // Indicates that parameters suitable for screencasts should be automatically
130 // applied to RtpSenders.
131 // TODO(perkj): Remove these once all known applications have moved to
deadbeef6e09aef2017-02-08 09:38:21132 // explicitly setting suitable parameters for screencasts and don't need this
perkjf4524c42016-03-09 01:39:17133 // implicit behavior.
134 virtual bool is_screencast() const = 0;
135
Per3c4cb592016-03-31 15:23:39136 // Indicates that the encoder should denoise video before encoding it.
137 // If it is not set, the default configuration is used which is different
138 // depending on video codec.
perkjf4524c42016-03-09 01:39:17139 // TODO(perkj): Remove this once denoising is done by the source, and not by
140 // the encoder.
Per3c4cb592016-03-31 15:23:39141 virtual rtc::Optional<bool> needs_denoising() const = 0;
perkj2a4ff982016-03-08 00:27:48142
deadbeef6e09aef2017-02-08 09:38:21143 // Returns false if no stats are available, e.g, for a remote source, or a
144 // source which has not seen its first frame yet.
145 //
146 // Implementation should avoid blocking.
nisse66a510a2016-04-01 08:10:42147 virtual bool GetStats(Stats* stats) = 0;
148
perkj2a4ff982016-03-08 00:27:48149 protected:
150 virtual ~VideoTrackSourceInterface() {}
151};
Henrik Kjellanderbd0ae452016-02-10 09:53:12152
perkjcf8bdaf2017-08-01 06:22:01153// VideoTrackInterface is designed to be invoked on the signaling thread except
154// for rtc::VideoSourceInterface<VideoFrame> methods that must be invoked
155// on the worker thread.
156// PeerConnectionFactory::CreateVideoTrack can be used for creating a VideoTrack
157// that ensures thread safety and that all methods are called on the right
158// thread.
nisse0a70b8d2016-02-26 09:24:58159class VideoTrackInterface
160 : public MediaStreamTrackInterface,
nissee8431852016-11-11 11:55:13161 public rtc::VideoSourceInterface<VideoFrame> {
Henrik Kjellanderbd0ae452016-02-10 09:53:12162 public:
pbos37d7b3f2016-12-16 23:39:11163 // Video track content hint, used to override the source is_screencast
164 // property.
165 // See https://crbug.com/653531 and https://github.com/WICG/mst-content-hint.
166 enum class ContentHint { kNone, kFluid, kDetailed };
167
mbonadei7b2b0612017-07-10 09:40:49168 // Register a video sink for this track. Used to connect the track to the
169 // underlying video engine.
170 void AddOrUpdateSink(rtc::VideoSinkInterface<VideoFrame>* sink,
171 const rtc::VideoSinkWants& wants) override {}
172 void RemoveSink(rtc::VideoSinkInterface<VideoFrame>* sink) override {}
173
perkj2a4ff982016-03-08 00:27:48174 virtual VideoTrackSourceInterface* GetSource() const = 0;
Henrik Kjellanderbd0ae452016-02-10 09:53:12175
pbos37d7b3f2016-12-16 23:39:11176 virtual ContentHint content_hint() const { return ContentHint::kNone; }
177 virtual void set_content_hint(ContentHint hint) {}
178
Henrik Kjellanderbd0ae452016-02-10 09:53:12179 protected:
180 virtual ~VideoTrackInterface() {}
181};
182
183// Interface for receiving audio data from a AudioTrack.
184class AudioTrackSinkInterface {
185 public:
186 virtual void OnData(const void* audio_data,
187 int bits_per_sample,
188 int sample_rate,
189 size_t number_of_channels,
190 size_t number_of_frames) = 0;
191
192 protected:
193 virtual ~AudioTrackSinkInterface() {}
194};
195
196// AudioSourceInterface is a reference counted source used for AudioTracks.
deadbeef6e09aef2017-02-08 09:38:21197// The same source can be used by multiple AudioTracks.
Henrik Kjellanderbd0ae452016-02-10 09:53:12198class AudioSourceInterface : public MediaSourceInterface {
199 public:
200 class AudioObserver {
201 public:
202 virtual void OnSetVolume(double volume) = 0;
203
204 protected:
205 virtual ~AudioObserver() {}
206 };
207
deadbeef6e09aef2017-02-08 09:38:21208 // TODO(deadbeef): Makes all the interfaces pure virtual after they're
209 // implemented in chromium.
210
211 // Sets the volume of the source. |volume| is in the range of [0, 10].
Henrik Kjellanderbd0ae452016-02-10 09:53:12212 // TODO(tommi): This method should be on the track and ideally volume should
213 // be applied in the track in a way that does not affect clones of the track.
214 virtual void SetVolume(double volume) {}
215
deadbeef6e09aef2017-02-08 09:38:21216 // Registers/unregisters observers to the audio source.
Henrik Kjellanderbd0ae452016-02-10 09:53:12217 virtual void RegisterAudioObserver(AudioObserver* observer) {}
218 virtual void UnregisterAudioObserver(AudioObserver* observer) {}
219
220 // TODO(tommi): Make pure virtual.
221 virtual void AddSink(AudioTrackSinkInterface* sink) {}
222 virtual void RemoveSink(AudioTrackSinkInterface* sink) {}
223};
224
225// Interface of the audio processor used by the audio track to collect
226// statistics.
227class AudioProcessorInterface : public rtc::RefCountInterface {
228 public:
229 struct AudioProcessorStats {
ivocee5a3162017-01-15 16:29:46230 AudioProcessorStats()
231 : typing_noise_detected(false),
232 echo_return_loss(0),
233 echo_return_loss_enhancement(0),
234 echo_delay_median_ms(0),
235 echo_delay_std_ms(0),
236 aec_quality_min(0.0),
237 residual_echo_likelihood(0.0f),
238 residual_echo_likelihood_recent_max(0.0f),
239 aec_divergent_filter_fraction(0.0) {}
Henrik Kjellanderbd0ae452016-02-10 09:53:12240 ~AudioProcessorStats() {}
241
242 bool typing_noise_detected;
243 int echo_return_loss;
244 int echo_return_loss_enhancement;
245 int echo_delay_median_ms;
Henrik Kjellanderbd0ae452016-02-10 09:53:12246 int echo_delay_std_ms;
ivocdfb640f2016-10-21 11:10:03247 float aec_quality_min;
248 float residual_echo_likelihood;
ivocee5a3162017-01-15 16:29:46249 float residual_echo_likelihood_recent_max;
Minyuef20e12f2016-04-07 14:48:15250 float aec_divergent_filter_fraction;
Henrik Kjellanderbd0ae452016-02-10 09:53:12251 };
252
253 // Get audio processor statistics.
254 virtual void GetStats(AudioProcessorStats* stats) = 0;
255
256 protected:
257 virtual ~AudioProcessorInterface() {}
258};
259
260class AudioTrackInterface : public MediaStreamTrackInterface {
261 public:
deadbeef6e09aef2017-02-08 09:38:21262 // TODO(deadbeef): Figure out if the following interface should be const or
263 // not.
Henrik Kjellanderbd0ae452016-02-10 09:53:12264 virtual AudioSourceInterface* GetSource() const = 0;
265
266 // Add/Remove a sink that will receive the audio data from the track.
267 virtual void AddSink(AudioTrackSinkInterface* sink) = 0;
268 virtual void RemoveSink(AudioTrackSinkInterface* sink) = 0;
269
270 // Get the signal level from the audio track.
271 // Return true on success, otherwise false.
deadbeef6e09aef2017-02-08 09:38:21272 // TODO(deadbeef): Change the interface to int GetSignalLevel() and pure
273 // virtual after it's implemented in chromium.
Henrik Kjellanderbd0ae452016-02-10 09:53:12274 virtual bool GetSignalLevel(int* level) { return false; }
275
deadbeef995bf172017-02-27 22:47:33276 // Get the audio processor used by the audio track. Return null if the track
Henrik Kjellanderbd0ae452016-02-10 09:53:12277 // does not have any processor.
deadbeef6e09aef2017-02-08 09:38:21278 // TODO(deadbeef): Make the interface pure virtual.
279 virtual rtc::scoped_refptr<AudioProcessorInterface> GetAudioProcessor() {
280 return nullptr;
281 }
Henrik Kjellanderbd0ae452016-02-10 09:53:12282
283 protected:
284 virtual ~AudioTrackInterface() {}
285};
286
287typedef std::vector<rtc::scoped_refptr<AudioTrackInterface> >
288 AudioTrackVector;
289typedef std::vector<rtc::scoped_refptr<VideoTrackInterface> >
290 VideoTrackVector;
291
deadbeef6e09aef2017-02-08 09:38:21292// C++ version of https://www.w3.org/TR/mediacapture-streams/#mediastream.
293//
294// A major difference is that remote audio/video tracks (received by a
295// PeerConnection/RtpReceiver) are not synchronized simply by adding them to
296// the same stream; a session description with the correct "a=msid" attributes
297// must be pushed down.
298//
299// Thus, this interface acts as simply a container for tracks.
Henrik Kjellanderbd0ae452016-02-10 09:53:12300class MediaStreamInterface : public rtc::RefCountInterface,
301 public NotifierInterface {
302 public:
303 virtual std::string label() const = 0;
304
305 virtual AudioTrackVector GetAudioTracks() = 0;
306 virtual VideoTrackVector GetVideoTracks() = 0;
307 virtual rtc::scoped_refptr<AudioTrackInterface>
308 FindAudioTrack(const std::string& track_id) = 0;
309 virtual rtc::scoped_refptr<VideoTrackInterface>
310 FindVideoTrack(const std::string& track_id) = 0;
311
312 virtual bool AddTrack(AudioTrackInterface* track) = 0;
313 virtual bool AddTrack(VideoTrackInterface* track) = 0;
314 virtual bool RemoveTrack(AudioTrackInterface* track) = 0;
315 virtual bool RemoveTrack(VideoTrackInterface* track) = 0;
316
317 protected:
318 virtual ~MediaStreamInterface() {}
319};
320
321} // namespace webrtc
322
323#endif // WEBRTC_API_MEDIASTREAMINTERFACE_H_