blob: 2acaa000e4898ca1f2c067d1b605b5561d42d406 [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"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000026#include "webrtc/base/refcount.h"
27#include "webrtc/base/scoped_ref_ptr.h"
Perc0d31e92016-03-31 17:23:39 +020028#include "webrtc/base/optional.h"
perkja3ede6c2016-03-08 01:27:48 +010029#include "webrtc/media/base/mediachannel.h"
nissee73afba2016-01-28 04:47:08 -080030#include "webrtc/media/base/videosinkinterface.h"
nissedb25d2e2016-02-26 01:24:58 -080031#include "webrtc/media/base/videosourceinterface.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000032
henrike@webrtc.org28e20752013-07-10 00:45:36 +000033namespace webrtc {
34
35// Generic observer interface.
36class ObserverInterface {
37 public:
38 virtual void OnChanged() = 0;
39
40 protected:
41 virtual ~ObserverInterface() {}
42};
43
44class NotifierInterface {
45 public:
46 virtual void RegisterObserver(ObserverInterface* observer) = 0;
47 virtual void UnregisterObserver(ObserverInterface* observer) = 0;
48
49 virtual ~NotifierInterface() {}
50};
51
deadbeefb10f32f2017-02-08 01:38:21 -080052// Base class for sources. A MediaStreamTrack has an underlying source that
53// provides media. A source can be shared by multiple tracks.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000054class MediaSourceInterface : public rtc::RefCountInterface,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000055 public NotifierInterface {
56 public:
57 enum SourceState {
58 kInitializing,
59 kLive,
60 kEnded,
61 kMuted
62 };
63
64 virtual SourceState state() const = 0;
65
tommi6eca7e32015-12-15 04:27:11 -080066 virtual bool remote() const = 0;
67
henrike@webrtc.org28e20752013-07-10 00:45:36 +000068 protected:
69 virtual ~MediaSourceInterface() {}
70};
71
deadbeefb10f32f2017-02-08 01:38:21 -080072// C++ version of MediaStreamTrack.
73// See: https://www.w3.org/TR/mediacapture-streams/#mediastreamtrack
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000074class MediaStreamTrackInterface : public rtc::RefCountInterface,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000075 public NotifierInterface {
76 public:
77 enum TrackState {
perkjc8f952d2016-03-23 00:33:56 -070078 kLive,
79 kEnded,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000080 };
81
deadbeeffac06552015-11-25 11:26:01 -080082 static const char kAudioKind[];
83 static const char kVideoKind[];
84
nissefcc640f2016-04-01 01:10:42 -070085 // The kind() method must return kAudioKind only if the object is a
86 // subclass of AudioTrackInterface, and kVideoKind only if the
87 // object is a subclass of VideoTrackInterface. It is typically used
88 // to protect a static_cast<> to the corresponding subclass.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000089 virtual std::string kind() const = 0;
deadbeefb10f32f2017-02-08 01:38:21 -080090
91 // Track identifier.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000092 virtual std::string id() const = 0;
deadbeefb10f32f2017-02-08 01:38:21 -080093
94 // A disabled track will produce silence (if audio) or black frames (if
95 // video). Can be disabled and re-enabled.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000096 virtual bool enabled() const = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000097 virtual bool set_enabled(bool enable) = 0;
fischman@webrtc.org32001ef2013-08-12 23:26:21 +000098
deadbeefb10f32f2017-02-08 01:38:21 -080099 // Live or ended. A track will never be live again after becoming ended.
100 virtual TrackState state() const = 0;
101
fischman@webrtc.org32001ef2013-08-12 23:26:21 +0000102 protected:
103 virtual ~MediaStreamTrackInterface() {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000104};
105
deadbeefb10f32f2017-02-08 01:38:21 -0800106// VideoTrackSourceInterface is a reference counted source used for
107// VideoTracks. The same source can be used by multiple VideoTracks.
perkja3ede6c2016-03-08 01:27:48 +0100108class VideoTrackSourceInterface
109 : public MediaSourceInterface,
nisseacd935b2016-11-11 03:55:13 -0800110 public rtc::VideoSourceInterface<VideoFrame> {
perkja3ede6c2016-03-08 01:27:48 +0100111 public:
nissefcc640f2016-04-01 01:10:42 -0700112 struct Stats {
113 // Original size of captured frame, before video adaptation.
114 int input_width;
115 int input_height;
116 };
perkja3ede6c2016-03-08 01:27:48 +0100117
perkj0d3eef22016-03-09 02:39:17 +0100118 // Indicates that parameters suitable for screencasts should be automatically
119 // applied to RtpSenders.
120 // TODO(perkj): Remove these once all known applications have moved to
deadbeefb10f32f2017-02-08 01:38:21 -0800121 // explicitly setting suitable parameters for screencasts and don't need this
perkj0d3eef22016-03-09 02:39:17 +0100122 // implicit behavior.
123 virtual bool is_screencast() const = 0;
124
Perc0d31e92016-03-31 17:23:39 +0200125 // Indicates that the encoder should denoise video before encoding it.
126 // If it is not set, the default configuration is used which is different
127 // depending on video codec.
perkj0d3eef22016-03-09 02:39:17 +0100128 // TODO(perkj): Remove this once denoising is done by the source, and not by
129 // the encoder.
Perc0d31e92016-03-31 17:23:39 +0200130 virtual rtc::Optional<bool> needs_denoising() const = 0;
perkja3ede6c2016-03-08 01:27:48 +0100131
deadbeefb10f32f2017-02-08 01:38:21 -0800132 // Returns false if no stats are available, e.g, for a remote source, or a
133 // source which has not seen its first frame yet.
134 //
135 // Implementation should avoid blocking.
nissefcc640f2016-04-01 01:10:42 -0700136 virtual bool GetStats(Stats* stats) = 0;
137
perkja3ede6c2016-03-08 01:27:48 +0100138 protected:
139 virtual ~VideoTrackSourceInterface() {}
140};
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000141
nissedb25d2e2016-02-26 01:24:58 -0800142class VideoTrackInterface
143 : public MediaStreamTrackInterface,
nisseacd935b2016-11-11 03:55:13 -0800144 public rtc::VideoSourceInterface<VideoFrame> {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000145 public:
pbos5214a0a2016-12-16 15:39:11 -0800146 // Video track content hint, used to override the source is_screencast
147 // property.
148 // See https://crbug.com/653531 and https://github.com/WICG/mst-content-hint.
149 enum class ContentHint { kNone, kFluid, kDetailed };
150
deadbeefb10f32f2017-02-08 01:38:21 -0800151 // Register a video sink for this track. Used to connect the track to the
152 // underlying video engine.
nisseacd935b2016-11-11 03:55:13 -0800153 void AddOrUpdateSink(rtc::VideoSinkInterface<VideoFrame>* sink,
pbos5214a0a2016-12-16 15:39:11 -0800154 const rtc::VideoSinkWants& wants) override {}
155 void RemoveSink(rtc::VideoSinkInterface<VideoFrame>* sink) override {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000156
perkja3ede6c2016-03-08 01:27:48 +0100157 virtual VideoTrackSourceInterface* GetSource() const = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000158
pbos5214a0a2016-12-16 15:39:11 -0800159 virtual ContentHint content_hint() const { return ContentHint::kNone; }
160 virtual void set_content_hint(ContentHint hint) {}
161
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000162 protected:
163 virtual ~VideoTrackInterface() {}
164};
165
tommi6eca7e32015-12-15 04:27:11 -0800166// Interface for receiving audio data from a AudioTrack.
167class AudioTrackSinkInterface {
168 public:
169 virtual void OnData(const void* audio_data,
170 int bits_per_sample,
171 int sample_rate,
Peter Kasting69558702016-01-12 16:26:35 -0800172 size_t number_of_channels,
tommi6eca7e32015-12-15 04:27:11 -0800173 size_t number_of_frames) = 0;
174
175 protected:
176 virtual ~AudioTrackSinkInterface() {}
177};
178
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000179// AudioSourceInterface is a reference counted source used for AudioTracks.
deadbeefb10f32f2017-02-08 01:38:21 -0800180// The same source can be used by multiple AudioTracks.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000181class AudioSourceInterface : public MediaSourceInterface {
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000182 public:
183 class AudioObserver {
184 public:
185 virtual void OnSetVolume(double volume) = 0;
186
187 protected:
188 virtual ~AudioObserver() {}
189 };
190
deadbeefb10f32f2017-02-08 01:38:21 -0800191 // TODO(deadbeef): Makes all the interfaces pure virtual after they're
192 // implemented in chromium.
193
194 // Sets the volume of the source. |volume| is in the range of [0, 10].
Tommif888bb52015-12-12 01:37:01 +0100195 // TODO(tommi): This method should be on the track and ideally volume should
196 // be applied in the track in a way that does not affect clones of the track.
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000197 virtual void SetVolume(double volume) {}
198
deadbeefb10f32f2017-02-08 01:38:21 -0800199 // Registers/unregisters observers to the audio source.
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000200 virtual void RegisterAudioObserver(AudioObserver* observer) {}
201 virtual void UnregisterAudioObserver(AudioObserver* observer) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000202
tommi6eca7e32015-12-15 04:27:11 -0800203 // TODO(tommi): Make pure virtual.
204 virtual void AddSink(AudioTrackSinkInterface* sink) {}
205 virtual void RemoveSink(AudioTrackSinkInterface* sink) {}
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000206};
207
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000208// Interface of the audio processor used by the audio track to collect
209// statistics.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000210class AudioProcessorInterface : public rtc::RefCountInterface {
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000211 public:
212 struct AudioProcessorStats {
ivoc4e477a12017-01-15 08:29:46 -0800213 AudioProcessorStats()
214 : typing_noise_detected(false),
215 echo_return_loss(0),
216 echo_return_loss_enhancement(0),
217 echo_delay_median_ms(0),
218 echo_delay_std_ms(0),
219 aec_quality_min(0.0),
220 residual_echo_likelihood(0.0f),
221 residual_echo_likelihood_recent_max(0.0f),
222 aec_divergent_filter_fraction(0.0) {}
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000223 ~AudioProcessorStats() {}
224
225 bool typing_noise_detected;
226 int echo_return_loss;
227 int echo_return_loss_enhancement;
228 int echo_delay_median_ms;
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000229 int echo_delay_std_ms;
ivoc8c63a822016-10-21 04:10:03 -0700230 float aec_quality_min;
231 float residual_echo_likelihood;
ivoc4e477a12017-01-15 08:29:46 -0800232 float residual_echo_likelihood_recent_max;
Minyue2a8a78c2016-04-07 16:48:15 +0200233 float aec_divergent_filter_fraction;
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000234 };
235
236 // Get audio processor statistics.
237 virtual void GetStats(AudioProcessorStats* stats) = 0;
238
239 protected:
240 virtual ~AudioProcessorInterface() {}
241};
242
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000243class AudioTrackInterface : public MediaStreamTrackInterface {
244 public:
deadbeefb10f32f2017-02-08 01:38:21 -0800245 // TODO(deadbeef): Figure out if the following interface should be const or
246 // not.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000247 virtual AudioSourceInterface* GetSource() const = 0;
248
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000249 // Add/Remove a sink that will receive the audio data from the track.
250 virtual void AddSink(AudioTrackSinkInterface* sink) = 0;
251 virtual void RemoveSink(AudioTrackSinkInterface* sink) = 0;
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000252
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000253 // Get the signal level from the audio track.
254 // Return true on success, otherwise false.
deadbeefb10f32f2017-02-08 01:38:21 -0800255 // TODO(deadbeef): Change the interface to int GetSignalLevel() and pure
256 // virtual after it's implemented in chromium.
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000257 virtual bool GetSignalLevel(int* level) { return false; }
258
deadbeef8d60a942017-02-27 14:47:33 -0800259 // Get the audio processor used by the audio track. Return null if the track
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000260 // does not have any processor.
deadbeefb10f32f2017-02-08 01:38:21 -0800261 // TODO(deadbeef): Make the interface pure virtual.
262 virtual rtc::scoped_refptr<AudioProcessorInterface> GetAudioProcessor() {
263 return nullptr;
264 }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000265
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000266 protected:
267 virtual ~AudioTrackInterface() {}
268};
269
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000270typedef std::vector<rtc::scoped_refptr<AudioTrackInterface> >
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000271 AudioTrackVector;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000272typedef std::vector<rtc::scoped_refptr<VideoTrackInterface> >
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000273 VideoTrackVector;
274
deadbeefb10f32f2017-02-08 01:38:21 -0800275// C++ version of https://www.w3.org/TR/mediacapture-streams/#mediastream.
276//
277// A major difference is that remote audio/video tracks (received by a
278// PeerConnection/RtpReceiver) are not synchronized simply by adding them to
279// the same stream; a session description with the correct "a=msid" attributes
280// must be pushed down.
281//
282// Thus, this interface acts as simply a container for tracks.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000283class MediaStreamInterface : public rtc::RefCountInterface,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000284 public NotifierInterface {
285 public:
286 virtual std::string label() const = 0;
287
288 virtual AudioTrackVector GetAudioTracks() = 0;
289 virtual VideoTrackVector GetVideoTracks() = 0;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000290 virtual rtc::scoped_refptr<AudioTrackInterface>
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000291 FindAudioTrack(const std::string& track_id) = 0;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000292 virtual rtc::scoped_refptr<VideoTrackInterface>
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000293 FindVideoTrack(const std::string& track_id) = 0;
294
295 virtual bool AddTrack(AudioTrackInterface* track) = 0;
296 virtual bool AddTrack(VideoTrackInterface* track) = 0;
297 virtual bool RemoveTrack(AudioTrackInterface* track) = 0;
298 virtual bool RemoveTrack(VideoTrackInterface* track) = 0;
299
300 protected:
301 virtual ~MediaStreamInterface() {}
302};
303
304} // namespace webrtc
305
Henrik Kjellander15583c12016-02-10 10:53:12 +0100306#endif // WEBRTC_API_MEDIASTREAMINTERFACE_H_