blob: cc660e1be239e285a8f4124b417d458e283673f5 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2012 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellanderb24317b2016-02-10 07:54:43 -08004 * 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.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00009 */
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
Henrik Kjellander15583c12016-02-10 10:53:12 +010017#ifndef WEBRTC_API_MEDIASTREAMINTERFACE_H_
18#define WEBRTC_API_MEDIASTREAMINTERFACE_H_
henrike@webrtc.org28e20752013-07-10 00:45:36 +000019
pbos9baddf22017-01-02 06:44:41 -080020#include <stddef.h>
21
henrike@webrtc.org28e20752013-07-10 00:45:36 +000022#include <string>
23#include <vector>
24
nisseaf916892017-01-10 07:44:26 -080025#include "webrtc/api/video/video_frame.h"
Edward Lemurc20978e2017-07-06 19:44:34 +020026#include "webrtc/rtc_base/optional.h"
zhihuang38ede132017-06-15 12:52:32 -070027// 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.
zhihuang38ede132017-06-15 12:52:32 -070030#include "webrtc/media/base/streamparams.h"
nissee73afba2016-01-28 04:47:08 -080031#include "webrtc/media/base/videosinkinterface.h"
nissedb25d2e2016-02-26 01:24:58 -080032#include "webrtc/media/base/videosourceinterface.h"
Edward Lemurc20978e2017-07-06 19:44:34 +020033#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"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000038
henrike@webrtc.org28e20752013-07-10 00:45:36 +000039namespace 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
deadbeefb10f32f2017-02-08 01:38:21 -080058// Base class for sources. A MediaStreamTrack has an underlying source that
59// provides media. A source can be shared by multiple tracks.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000060class MediaSourceInterface : public rtc::RefCountInterface,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000061 public NotifierInterface {
62 public:
63 enum SourceState {
64 kInitializing,
65 kLive,
66 kEnded,
67 kMuted
68 };
69
70 virtual SourceState state() const = 0;
71
tommi6eca7e32015-12-15 04:27:11 -080072 virtual bool remote() const = 0;
73
henrike@webrtc.org28e20752013-07-10 00:45:36 +000074 protected:
75 virtual ~MediaSourceInterface() {}
76};
77
deadbeefb10f32f2017-02-08 01:38:21 -080078// C++ version of MediaStreamTrack.
79// See: https://www.w3.org/TR/mediacapture-streams/#mediastreamtrack
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000080class MediaStreamTrackInterface : public rtc::RefCountInterface,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000081 public NotifierInterface {
82 public:
83 enum TrackState {
perkjc8f952d2016-03-23 00:33:56 -070084 kLive,
85 kEnded,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000086 };
87
deadbeeffac06552015-11-25 11:26:01 -080088 static const char kAudioKind[];
89 static const char kVideoKind[];
90
nissefcc640f2016-04-01 01:10:42 -070091 // 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.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000095 virtual std::string kind() const = 0;
deadbeefb10f32f2017-02-08 01:38:21 -080096
97 // Track identifier.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000098 virtual std::string id() const = 0;
deadbeefb10f32f2017-02-08 01:38:21 -080099
100 // A disabled track will produce silence (if audio) or black frames (if
101 // video). Can be disabled and re-enabled.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000102 virtual bool enabled() const = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000103 virtual bool set_enabled(bool enable) = 0;
fischman@webrtc.org32001ef2013-08-12 23:26:21 +0000104
deadbeefb10f32f2017-02-08 01:38:21 -0800105 // Live or ended. A track will never be live again after becoming ended.
106 virtual TrackState state() const = 0;
107
fischman@webrtc.org32001ef2013-08-12 23:26:21 +0000108 protected:
109 virtual ~MediaStreamTrackInterface() {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000110};
111
deadbeefb10f32f2017-02-08 01:38:21 -0800112// VideoTrackSourceInterface is a reference counted source used for
113// VideoTracks. The same source can be used by multiple VideoTracks.
perkja3ede6c2016-03-08 01:27:48 +0100114class VideoTrackSourceInterface
115 : public MediaSourceInterface,
nisseacd935b2016-11-11 03:55:13 -0800116 public rtc::VideoSourceInterface<VideoFrame> {
perkja3ede6c2016-03-08 01:27:48 +0100117 public:
nissefcc640f2016-04-01 01:10:42 -0700118 struct Stats {
119 // Original size of captured frame, before video adaptation.
120 int input_width;
121 int input_height;
122 };
perkja3ede6c2016-03-08 01:27:48 +0100123
perkj0d3eef22016-03-09 02:39:17 +0100124 // Indicates that parameters suitable for screencasts should be automatically
125 // applied to RtpSenders.
126 // TODO(perkj): Remove these once all known applications have moved to
deadbeefb10f32f2017-02-08 01:38:21 -0800127 // explicitly setting suitable parameters for screencasts and don't need this
perkj0d3eef22016-03-09 02:39:17 +0100128 // implicit behavior.
129 virtual bool is_screencast() const = 0;
130
Perc0d31e92016-03-31 17:23:39 +0200131 // Indicates that the encoder should denoise video before encoding it.
132 // If it is not set, the default configuration is used which is different
133 // depending on video codec.
perkj0d3eef22016-03-09 02:39:17 +0100134 // TODO(perkj): Remove this once denoising is done by the source, and not by
135 // the encoder.
Perc0d31e92016-03-31 17:23:39 +0200136 virtual rtc::Optional<bool> needs_denoising() const = 0;
perkja3ede6c2016-03-08 01:27:48 +0100137
deadbeefb10f32f2017-02-08 01:38:21 -0800138 // Returns false if no stats are available, e.g, for a remote source, or a
139 // source which has not seen its first frame yet.
140 //
141 // Implementation should avoid blocking.
nissefcc640f2016-04-01 01:10:42 -0700142 virtual bool GetStats(Stats* stats) = 0;
143
perkja3ede6c2016-03-08 01:27:48 +0100144 protected:
145 virtual ~VideoTrackSourceInterface() {}
146};
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000147
nissedb25d2e2016-02-26 01:24:58 -0800148class VideoTrackInterface
149 : public MediaStreamTrackInterface,
nisseacd935b2016-11-11 03:55:13 -0800150 public rtc::VideoSourceInterface<VideoFrame> {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000151 public:
pbos5214a0a2016-12-16 15:39:11 -0800152 // Video track content hint, used to override the source is_screencast
153 // property.
154 // See https://crbug.com/653531 and https://github.com/WICG/mst-content-hint.
155 enum class ContentHint { kNone, kFluid, kDetailed };
156
mbonadei539d1042017-07-10 02:40:49 -0700157 // Register a video sink for this track. Used to connect the track to the
158 // underlying video engine.
159 void AddOrUpdateSink(rtc::VideoSinkInterface<VideoFrame>* sink,
160 const rtc::VideoSinkWants& wants) override {}
161 void RemoveSink(rtc::VideoSinkInterface<VideoFrame>* sink) override {}
162
perkja3ede6c2016-03-08 01:27:48 +0100163 virtual VideoTrackSourceInterface* GetSource() const = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000164
pbos5214a0a2016-12-16 15:39:11 -0800165 virtual ContentHint content_hint() const { return ContentHint::kNone; }
166 virtual void set_content_hint(ContentHint hint) {}
167
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000168 protected:
169 virtual ~VideoTrackInterface() {}
170};
171
tommi6eca7e32015-12-15 04:27:11 -0800172// Interface for receiving audio data from a AudioTrack.
173class AudioTrackSinkInterface {
174 public:
175 virtual void OnData(const void* audio_data,
176 int bits_per_sample,
177 int sample_rate,
Peter Kasting69558702016-01-12 16:26:35 -0800178 size_t number_of_channels,
tommi6eca7e32015-12-15 04:27:11 -0800179 size_t number_of_frames) = 0;
180
181 protected:
182 virtual ~AudioTrackSinkInterface() {}
183};
184
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000185// AudioSourceInterface is a reference counted source used for AudioTracks.
deadbeefb10f32f2017-02-08 01:38:21 -0800186// The same source can be used by multiple AudioTracks.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000187class AudioSourceInterface : public MediaSourceInterface {
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000188 public:
189 class AudioObserver {
190 public:
191 virtual void OnSetVolume(double volume) = 0;
192
193 protected:
194 virtual ~AudioObserver() {}
195 };
196
deadbeefb10f32f2017-02-08 01:38:21 -0800197 // TODO(deadbeef): Makes all the interfaces pure virtual after they're
198 // implemented in chromium.
199
200 // Sets the volume of the source. |volume| is in the range of [0, 10].
Tommif888bb52015-12-12 01:37:01 +0100201 // TODO(tommi): This method should be on the track and ideally volume should
202 // be applied in the track in a way that does not affect clones of the track.
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000203 virtual void SetVolume(double volume) {}
204
deadbeefb10f32f2017-02-08 01:38:21 -0800205 // Registers/unregisters observers to the audio source.
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000206 virtual void RegisterAudioObserver(AudioObserver* observer) {}
207 virtual void UnregisterAudioObserver(AudioObserver* observer) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000208
tommi6eca7e32015-12-15 04:27:11 -0800209 // TODO(tommi): Make pure virtual.
210 virtual void AddSink(AudioTrackSinkInterface* sink) {}
211 virtual void RemoveSink(AudioTrackSinkInterface* sink) {}
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000212};
213
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000214// Interface of the audio processor used by the audio track to collect
215// statistics.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000216class AudioProcessorInterface : public rtc::RefCountInterface {
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000217 public:
218 struct AudioProcessorStats {
ivoc4e477a12017-01-15 08:29:46 -0800219 AudioProcessorStats()
220 : typing_noise_detected(false),
221 echo_return_loss(0),
222 echo_return_loss_enhancement(0),
223 echo_delay_median_ms(0),
224 echo_delay_std_ms(0),
225 aec_quality_min(0.0),
226 residual_echo_likelihood(0.0f),
227 residual_echo_likelihood_recent_max(0.0f),
228 aec_divergent_filter_fraction(0.0) {}
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000229 ~AudioProcessorStats() {}
230
231 bool typing_noise_detected;
232 int echo_return_loss;
233 int echo_return_loss_enhancement;
234 int echo_delay_median_ms;
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000235 int echo_delay_std_ms;
ivoc8c63a822016-10-21 04:10:03 -0700236 float aec_quality_min;
237 float residual_echo_likelihood;
ivoc4e477a12017-01-15 08:29:46 -0800238 float residual_echo_likelihood_recent_max;
Minyue2a8a78c2016-04-07 16:48:15 +0200239 float aec_divergent_filter_fraction;
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000240 };
241
242 // Get audio processor statistics.
243 virtual void GetStats(AudioProcessorStats* stats) = 0;
244
245 protected:
246 virtual ~AudioProcessorInterface() {}
247};
248
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000249class AudioTrackInterface : public MediaStreamTrackInterface {
250 public:
deadbeefb10f32f2017-02-08 01:38:21 -0800251 // TODO(deadbeef): Figure out if the following interface should be const or
252 // not.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000253 virtual AudioSourceInterface* GetSource() const = 0;
254
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000255 // Add/Remove a sink that will receive the audio data from the track.
256 virtual void AddSink(AudioTrackSinkInterface* sink) = 0;
257 virtual void RemoveSink(AudioTrackSinkInterface* sink) = 0;
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000258
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000259 // Get the signal level from the audio track.
260 // Return true on success, otherwise false.
deadbeefb10f32f2017-02-08 01:38:21 -0800261 // TODO(deadbeef): Change the interface to int GetSignalLevel() and pure
262 // virtual after it's implemented in chromium.
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000263 virtual bool GetSignalLevel(int* level) { return false; }
264
deadbeef8d60a942017-02-27 14:47:33 -0800265 // Get the audio processor used by the audio track. Return null if the track
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000266 // does not have any processor.
deadbeefb10f32f2017-02-08 01:38:21 -0800267 // TODO(deadbeef): Make the interface pure virtual.
268 virtual rtc::scoped_refptr<AudioProcessorInterface> GetAudioProcessor() {
269 return nullptr;
270 }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000271
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000272 protected:
273 virtual ~AudioTrackInterface() {}
274};
275
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000276typedef std::vector<rtc::scoped_refptr<AudioTrackInterface> >
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000277 AudioTrackVector;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000278typedef std::vector<rtc::scoped_refptr<VideoTrackInterface> >
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000279 VideoTrackVector;
280
deadbeefb10f32f2017-02-08 01:38:21 -0800281// C++ version of https://www.w3.org/TR/mediacapture-streams/#mediastream.
282//
283// A major difference is that remote audio/video tracks (received by a
284// PeerConnection/RtpReceiver) are not synchronized simply by adding them to
285// the same stream; a session description with the correct "a=msid" attributes
286// must be pushed down.
287//
288// Thus, this interface acts as simply a container for tracks.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000289class MediaStreamInterface : public rtc::RefCountInterface,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000290 public NotifierInterface {
291 public:
292 virtual std::string label() const = 0;
293
294 virtual AudioTrackVector GetAudioTracks() = 0;
295 virtual VideoTrackVector GetVideoTracks() = 0;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000296 virtual rtc::scoped_refptr<AudioTrackInterface>
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000297 FindAudioTrack(const std::string& track_id) = 0;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000298 virtual rtc::scoped_refptr<VideoTrackInterface>
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000299 FindVideoTrack(const std::string& track_id) = 0;
300
301 virtual bool AddTrack(AudioTrackInterface* track) = 0;
302 virtual bool AddTrack(VideoTrackInterface* track) = 0;
303 virtual bool RemoveTrack(AudioTrackInterface* track) = 0;
304 virtual bool RemoveTrack(VideoTrackInterface* track) = 0;
305
306 protected:
307 virtual ~MediaStreamInterface() {}
308};
309
310} // namespace webrtc
311
Henrik Kjellander15583c12016-02-10 10:53:12 +0100312#endif // WEBRTC_API_MEDIASTREAMINTERFACE_H_