blob: 2cc5923d7e08d426355cc671697d2a79bd040b2f [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#ifndef API_MEDIASTREAMINTERFACE_H_
18#define 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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020025#include "api/optional.h"
26#include "api/video/video_frame.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.
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020030#include "media/base/videosinkinterface.h"
31#include "media/base/videosourceinterface.h"
32#include "rtc_base/ratetracker.h"
33#include "rtc_base/refcount.h"
34#include "rtc_base/scoped_ref_ptr.h"
35#include "rtc_base/thread.h"
36#include "rtc_base/timeutils.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000037
henrike@webrtc.org28e20752013-07-10 00:45:36 +000038namespace webrtc {
39
40// Generic observer interface.
41class ObserverInterface {
42 public:
43 virtual void OnChanged() = 0;
44
45 protected:
46 virtual ~ObserverInterface() {}
47};
48
49class NotifierInterface {
50 public:
51 virtual void RegisterObserver(ObserverInterface* observer) = 0;
52 virtual void UnregisterObserver(ObserverInterface* observer) = 0;
53
54 virtual ~NotifierInterface() {}
55};
56
deadbeefb10f32f2017-02-08 01:38:21 -080057// Base class for sources. A MediaStreamTrack has an underlying source that
58// provides media. A source can be shared by multiple tracks.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000059class MediaSourceInterface : public rtc::RefCountInterface,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000060 public NotifierInterface {
61 public:
62 enum SourceState {
63 kInitializing,
64 kLive,
65 kEnded,
66 kMuted
67 };
68
69 virtual SourceState state() const = 0;
70
tommi6eca7e32015-12-15 04:27:11 -080071 virtual bool remote() const = 0;
72
henrike@webrtc.org28e20752013-07-10 00:45:36 +000073 protected:
74 virtual ~MediaSourceInterface() {}
75};
76
deadbeefb10f32f2017-02-08 01:38:21 -080077// C++ version of MediaStreamTrack.
78// See: https://www.w3.org/TR/mediacapture-streams/#mediastreamtrack
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000079class MediaStreamTrackInterface : public rtc::RefCountInterface,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000080 public NotifierInterface {
81 public:
82 enum TrackState {
perkjc8f952d2016-03-23 00:33:56 -070083 kLive,
84 kEnded,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000085 };
86
deadbeeffac06552015-11-25 11:26:01 -080087 static const char kAudioKind[];
88 static const char kVideoKind[];
89
nissefcc640f2016-04-01 01:10:42 -070090 // The kind() method must return kAudioKind only if the object is a
91 // subclass of AudioTrackInterface, and kVideoKind only if the
92 // object is a subclass of VideoTrackInterface. It is typically used
93 // to protect a static_cast<> to the corresponding subclass.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000094 virtual std::string kind() const = 0;
deadbeefb10f32f2017-02-08 01:38:21 -080095
96 // Track identifier.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000097 virtual std::string id() const = 0;
deadbeefb10f32f2017-02-08 01:38:21 -080098
99 // A disabled track will produce silence (if audio) or black frames (if
100 // video). Can be disabled and re-enabled.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000101 virtual bool enabled() const = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000102 virtual bool set_enabled(bool enable) = 0;
fischman@webrtc.org32001ef2013-08-12 23:26:21 +0000103
deadbeefb10f32f2017-02-08 01:38:21 -0800104 // Live or ended. A track will never be live again after becoming ended.
105 virtual TrackState state() const = 0;
106
fischman@webrtc.org32001ef2013-08-12 23:26:21 +0000107 protected:
108 virtual ~MediaStreamTrackInterface() {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000109};
110
deadbeefb10f32f2017-02-08 01:38:21 -0800111// VideoTrackSourceInterface is a reference counted source used for
112// VideoTracks. The same source can be used by multiple VideoTracks.
perkj773be362017-07-31 23:22:01 -0700113// VideoTrackSourceInterface is designed to be invoked on the signaling thread
114// except for rtc::VideoSourceInterface<VideoFrame> methods that will be invoked
115// on the worker thread via a VideoTrack. A custom implementation of a source
116// can inherit AdaptedVideoTrackSource instead of directly implementing this
117// interface.
perkja3ede6c2016-03-08 01:27:48 +0100118class VideoTrackSourceInterface
119 : public MediaSourceInterface,
nisseacd935b2016-11-11 03:55:13 -0800120 public rtc::VideoSourceInterface<VideoFrame> {
perkja3ede6c2016-03-08 01:27:48 +0100121 public:
nissefcc640f2016-04-01 01:10:42 -0700122 struct Stats {
123 // Original size of captured frame, before video adaptation.
124 int input_width;
125 int input_height;
126 };
perkja3ede6c2016-03-08 01:27:48 +0100127
perkj0d3eef22016-03-09 02:39:17 +0100128 // Indicates that parameters suitable for screencasts should be automatically
129 // applied to RtpSenders.
130 // TODO(perkj): Remove these once all known applications have moved to
deadbeefb10f32f2017-02-08 01:38:21 -0800131 // explicitly setting suitable parameters for screencasts and don't need this
perkj0d3eef22016-03-09 02:39:17 +0100132 // implicit behavior.
133 virtual bool is_screencast() const = 0;
134
Perc0d31e92016-03-31 17:23:39 +0200135 // Indicates that the encoder should denoise video before encoding it.
136 // If it is not set, the default configuration is used which is different
137 // depending on video codec.
perkj0d3eef22016-03-09 02:39:17 +0100138 // TODO(perkj): Remove this once denoising is done by the source, and not by
139 // the encoder.
Perc0d31e92016-03-31 17:23:39 +0200140 virtual rtc::Optional<bool> needs_denoising() const = 0;
perkja3ede6c2016-03-08 01:27:48 +0100141
deadbeefb10f32f2017-02-08 01:38:21 -0800142 // Returns false if no stats are available, e.g, for a remote source, or a
143 // source which has not seen its first frame yet.
144 //
145 // Implementation should avoid blocking.
nissefcc640f2016-04-01 01:10:42 -0700146 virtual bool GetStats(Stats* stats) = 0;
147
perkja3ede6c2016-03-08 01:27:48 +0100148 protected:
149 virtual ~VideoTrackSourceInterface() {}
150};
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000151
perkj773be362017-07-31 23:22:01 -0700152// VideoTrackInterface is designed to be invoked on the signaling thread except
153// for rtc::VideoSourceInterface<VideoFrame> methods that must be invoked
154// on the worker thread.
155// PeerConnectionFactory::CreateVideoTrack can be used for creating a VideoTrack
156// that ensures thread safety and that all methods are called on the right
157// thread.
nissedb25d2e2016-02-26 01:24:58 -0800158class VideoTrackInterface
159 : public MediaStreamTrackInterface,
nisseacd935b2016-11-11 03:55:13 -0800160 public rtc::VideoSourceInterface<VideoFrame> {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000161 public:
pbos5214a0a2016-12-16 15:39:11 -0800162 // Video track content hint, used to override the source is_screencast
163 // property.
164 // See https://crbug.com/653531 and https://github.com/WICG/mst-content-hint.
165 enum class ContentHint { kNone, kFluid, kDetailed };
166
mbonadei539d1042017-07-10 02:40:49 -0700167 // Register a video sink for this track. Used to connect the track to the
168 // underlying video engine.
169 void AddOrUpdateSink(rtc::VideoSinkInterface<VideoFrame>* sink,
170 const rtc::VideoSinkWants& wants) override {}
171 void RemoveSink(rtc::VideoSinkInterface<VideoFrame>* sink) override {}
172
perkja3ede6c2016-03-08 01:27:48 +0100173 virtual VideoTrackSourceInterface* GetSource() const = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000174
pbos5214a0a2016-12-16 15:39:11 -0800175 virtual ContentHint content_hint() const { return ContentHint::kNone; }
176 virtual void set_content_hint(ContentHint hint) {}
177
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000178 protected:
179 virtual ~VideoTrackInterface() {}
180};
181
tommi6eca7e32015-12-15 04:27:11 -0800182// Interface for receiving audio data from a AudioTrack.
183class AudioTrackSinkInterface {
184 public:
185 virtual void OnData(const void* audio_data,
186 int bits_per_sample,
187 int sample_rate,
Peter Kasting69558702016-01-12 16:26:35 -0800188 size_t number_of_channels,
tommi6eca7e32015-12-15 04:27:11 -0800189 size_t number_of_frames) = 0;
190
191 protected:
192 virtual ~AudioTrackSinkInterface() {}
193};
194
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000195// AudioSourceInterface is a reference counted source used for AudioTracks.
deadbeefb10f32f2017-02-08 01:38:21 -0800196// The same source can be used by multiple AudioTracks.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000197class AudioSourceInterface : public MediaSourceInterface {
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000198 public:
199 class AudioObserver {
200 public:
201 virtual void OnSetVolume(double volume) = 0;
202
203 protected:
204 virtual ~AudioObserver() {}
205 };
206
deadbeefb10f32f2017-02-08 01:38:21 -0800207 // TODO(deadbeef): Makes all the interfaces pure virtual after they're
208 // implemented in chromium.
209
210 // Sets the volume of the source. |volume| is in the range of [0, 10].
Tommif888bb52015-12-12 01:37:01 +0100211 // TODO(tommi): This method should be on the track and ideally volume should
212 // be applied in the track in a way that does not affect clones of the track.
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000213 virtual void SetVolume(double volume) {}
214
deadbeefb10f32f2017-02-08 01:38:21 -0800215 // Registers/unregisters observers to the audio source.
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000216 virtual void RegisterAudioObserver(AudioObserver* observer) {}
217 virtual void UnregisterAudioObserver(AudioObserver* observer) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000218
tommi6eca7e32015-12-15 04:27:11 -0800219 // TODO(tommi): Make pure virtual.
220 virtual void AddSink(AudioTrackSinkInterface* sink) {}
221 virtual void RemoveSink(AudioTrackSinkInterface* sink) {}
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000222};
223
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000224// Interface of the audio processor used by the audio track to collect
225// statistics.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000226class AudioProcessorInterface : public rtc::RefCountInterface {
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000227 public:
Ivo Creusenae026092017-11-20 13:07:16 +0100228 // Deprecated, use AudioProcessorStatistics instead.
229 // TODO(ivoc): Remove this when all implementations have switched to the new
230 // GetStats function. See b/67926135.
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000231 struct AudioProcessorStats {
ivoc4e477a12017-01-15 08:29:46 -0800232 AudioProcessorStats()
233 : typing_noise_detected(false),
234 echo_return_loss(0),
235 echo_return_loss_enhancement(0),
236 echo_delay_median_ms(0),
237 echo_delay_std_ms(0),
238 aec_quality_min(0.0),
239 residual_echo_likelihood(0.0f),
240 residual_echo_likelihood_recent_max(0.0f),
241 aec_divergent_filter_fraction(0.0) {}
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000242 ~AudioProcessorStats() {}
243
244 bool typing_noise_detected;
245 int echo_return_loss;
246 int echo_return_loss_enhancement;
247 int echo_delay_median_ms;
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000248 int echo_delay_std_ms;
ivoc8c63a822016-10-21 04:10:03 -0700249 float aec_quality_min;
250 float residual_echo_likelihood;
ivoc4e477a12017-01-15 08:29:46 -0800251 float residual_echo_likelihood_recent_max;
Minyue2a8a78c2016-04-07 16:48:15 +0200252 float aec_divergent_filter_fraction;
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000253 };
Ivo Creusenae026092017-11-20 13:07:16 +0100254 // This struct maintains the optionality of the stats, and will replace the
255 // regular stats struct when all users have been updated.
256 struct AudioProcessorStatistics {
257 bool typing_noise_detected = false;
258 rtc::Optional<double> echo_return_loss;
259 rtc::Optional<double> echo_return_loss_enhancement;
260 rtc::Optional<int32_t> echo_delay_median_ms;
261 rtc::Optional<int32_t> echo_delay_std_ms;
262 rtc::Optional<double> aec_quality_min;
263 rtc::Optional<double> residual_echo_likelihood;
264 rtc::Optional<double> residual_echo_likelihood_recent_max;
265 rtc::Optional<double> aec_divergent_filter_fraction;
266 };
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000267
268 // Get audio processor statistics.
269 virtual void GetStats(AudioProcessorStats* stats) = 0;
270
Ivo Creusenae026092017-11-20 13:07:16 +0100271 // Get audio processor statistics. The |has_remote_tracks| argument should be
272 // set if there are active remote tracks (this would usually be true during
273 // a call). If there are no remote tracks some of the stats will not be set by
274 // the AudioProcessor, because they only make sense if there is at least one
275 // remote track.
276 // TODO(ivoc): Make pure virtual when all implementions are updated.
277 virtual AudioProcessorStatistics GetStats(bool has_remote_tracks);
278
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000279 protected:
280 virtual ~AudioProcessorInterface() {}
281};
282
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000283class AudioTrackInterface : public MediaStreamTrackInterface {
284 public:
deadbeefb10f32f2017-02-08 01:38:21 -0800285 // TODO(deadbeef): Figure out if the following interface should be const or
286 // not.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000287 virtual AudioSourceInterface* GetSource() const = 0;
288
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000289 // Add/Remove a sink that will receive the audio data from the track.
290 virtual void AddSink(AudioTrackSinkInterface* sink) = 0;
291 virtual void RemoveSink(AudioTrackSinkInterface* sink) = 0;
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000292
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000293 // Get the signal level from the audio track.
294 // Return true on success, otherwise false.
deadbeefb10f32f2017-02-08 01:38:21 -0800295 // TODO(deadbeef): Change the interface to int GetSignalLevel() and pure
296 // virtual after it's implemented in chromium.
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000297 virtual bool GetSignalLevel(int* level) { return false; }
298
deadbeef8d60a942017-02-27 14:47:33 -0800299 // Get the audio processor used by the audio track. Return null if the track
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000300 // does not have any processor.
deadbeefb10f32f2017-02-08 01:38:21 -0800301 // TODO(deadbeef): Make the interface pure virtual.
302 virtual rtc::scoped_refptr<AudioProcessorInterface> GetAudioProcessor() {
303 return nullptr;
304 }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000305
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000306 protected:
307 virtual ~AudioTrackInterface() {}
308};
309
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000310typedef std::vector<rtc::scoped_refptr<AudioTrackInterface> >
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000311 AudioTrackVector;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000312typedef std::vector<rtc::scoped_refptr<VideoTrackInterface> >
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000313 VideoTrackVector;
314
deadbeefb10f32f2017-02-08 01:38:21 -0800315// C++ version of https://www.w3.org/TR/mediacapture-streams/#mediastream.
316//
317// A major difference is that remote audio/video tracks (received by a
318// PeerConnection/RtpReceiver) are not synchronized simply by adding them to
319// the same stream; a session description with the correct "a=msid" attributes
320// must be pushed down.
321//
322// Thus, this interface acts as simply a container for tracks.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000323class MediaStreamInterface : public rtc::RefCountInterface,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000324 public NotifierInterface {
325 public:
Steve Anton8ffb9c32017-08-31 15:45:38 -0700326 // TODO(steveanton): This could be renamed to id() to match the spec.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000327 virtual std::string label() const = 0;
328
329 virtual AudioTrackVector GetAudioTracks() = 0;
330 virtual VideoTrackVector GetVideoTracks() = 0;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000331 virtual rtc::scoped_refptr<AudioTrackInterface>
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000332 FindAudioTrack(const std::string& track_id) = 0;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000333 virtual rtc::scoped_refptr<VideoTrackInterface>
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000334 FindVideoTrack(const std::string& track_id) = 0;
335
336 virtual bool AddTrack(AudioTrackInterface* track) = 0;
337 virtual bool AddTrack(VideoTrackInterface* track) = 0;
338 virtual bool RemoveTrack(AudioTrackInterface* track) = 0;
339 virtual bool RemoveTrack(VideoTrackInterface* track) = 0;
340
341 protected:
342 virtual ~MediaStreamInterface() {}
343};
344
345} // namespace webrtc
346
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200347#endif // API_MEDIASTREAMINTERFACE_H_