blob: 2d16e5214d48b08d3eef2d7e5419fa9b64c01252 [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"
26// TODO(nisse): Transition hack, Chrome expects that including this
27// file declares I420Buffer. Delete after users of I420Buffer are
28// fixed to include the new header.
29#include "webrtc/api/video/i420_buffer.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000030#include "webrtc/base/refcount.h"
31#include "webrtc/base/scoped_ref_ptr.h"
Perc0d31e92016-03-31 17:23:39 +020032#include "webrtc/base/optional.h"
perkja3ede6c2016-03-08 01:27:48 +010033#include "webrtc/media/base/mediachannel.h"
nissee73afba2016-01-28 04:47:08 -080034#include "webrtc/media/base/videosinkinterface.h"
nissedb25d2e2016-02-26 01:24:58 -080035#include "webrtc/media/base/videosourceinterface.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000036
henrike@webrtc.org28e20752013-07-10 00:45:36 +000037namespace webrtc {
38
39// Generic observer interface.
40class ObserverInterface {
41 public:
42 virtual void OnChanged() = 0;
43
44 protected:
45 virtual ~ObserverInterface() {}
46};
47
48class NotifierInterface {
49 public:
50 virtual void RegisterObserver(ObserverInterface* observer) = 0;
51 virtual void UnregisterObserver(ObserverInterface* observer) = 0;
52
53 virtual ~NotifierInterface() {}
54};
55
deadbeefb10f32f2017-02-08 01:38:21 -080056// Base class for sources. A MediaStreamTrack has an underlying source that
57// provides media. A source can be shared by multiple tracks.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000058class MediaSourceInterface : public rtc::RefCountInterface,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000059 public NotifierInterface {
60 public:
61 enum SourceState {
62 kInitializing,
63 kLive,
64 kEnded,
65 kMuted
66 };
67
68 virtual SourceState state() const = 0;
69
tommi6eca7e32015-12-15 04:27:11 -080070 virtual bool remote() const = 0;
71
henrike@webrtc.org28e20752013-07-10 00:45:36 +000072 protected:
73 virtual ~MediaSourceInterface() {}
74};
75
deadbeefb10f32f2017-02-08 01:38:21 -080076// C++ version of MediaStreamTrack.
77// See: https://www.w3.org/TR/mediacapture-streams/#mediastreamtrack
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000078class MediaStreamTrackInterface : public rtc::RefCountInterface,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000079 public NotifierInterface {
80 public:
81 enum TrackState {
perkjc8f952d2016-03-23 00:33:56 -070082 kLive,
83 kEnded,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000084 };
85
deadbeeffac06552015-11-25 11:26:01 -080086 static const char kAudioKind[];
87 static const char kVideoKind[];
88
nissefcc640f2016-04-01 01:10:42 -070089 // The kind() method must return kAudioKind only if the object is a
90 // subclass of AudioTrackInterface, and kVideoKind only if the
91 // object is a subclass of VideoTrackInterface. It is typically used
92 // to protect a static_cast<> to the corresponding subclass.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000093 virtual std::string kind() const = 0;
deadbeefb10f32f2017-02-08 01:38:21 -080094
95 // Track identifier.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000096 virtual std::string id() const = 0;
deadbeefb10f32f2017-02-08 01:38:21 -080097
98 // A disabled track will produce silence (if audio) or black frames (if
99 // video). Can be disabled and re-enabled.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000100 virtual bool enabled() const = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000101 virtual bool set_enabled(bool enable) = 0;
fischman@webrtc.org32001ef2013-08-12 23:26:21 +0000102
deadbeefb10f32f2017-02-08 01:38:21 -0800103 // Live or ended. A track will never be live again after becoming ended.
104 virtual TrackState state() const = 0;
105
fischman@webrtc.org32001ef2013-08-12 23:26:21 +0000106 protected:
107 virtual ~MediaStreamTrackInterface() {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000108};
109
deadbeefb10f32f2017-02-08 01:38:21 -0800110// VideoTrackSourceInterface is a reference counted source used for
111// VideoTracks. The same source can be used by multiple VideoTracks.
perkja3ede6c2016-03-08 01:27:48 +0100112class VideoTrackSourceInterface
113 : public MediaSourceInterface,
nisseacd935b2016-11-11 03:55:13 -0800114 public rtc::VideoSourceInterface<VideoFrame> {
perkja3ede6c2016-03-08 01:27:48 +0100115 public:
nissefcc640f2016-04-01 01:10:42 -0700116 struct Stats {
117 // Original size of captured frame, before video adaptation.
118 int input_width;
119 int input_height;
120 };
perkja3ede6c2016-03-08 01:27:48 +0100121
perkj0d3eef22016-03-09 02:39:17 +0100122 // Indicates that parameters suitable for screencasts should be automatically
123 // applied to RtpSenders.
124 // TODO(perkj): Remove these once all known applications have moved to
deadbeefb10f32f2017-02-08 01:38:21 -0800125 // explicitly setting suitable parameters for screencasts and don't need this
perkj0d3eef22016-03-09 02:39:17 +0100126 // implicit behavior.
127 virtual bool is_screencast() const = 0;
128
Perc0d31e92016-03-31 17:23:39 +0200129 // Indicates that the encoder should denoise video before encoding it.
130 // If it is not set, the default configuration is used which is different
131 // depending on video codec.
perkj0d3eef22016-03-09 02:39:17 +0100132 // TODO(perkj): Remove this once denoising is done by the source, and not by
133 // the encoder.
Perc0d31e92016-03-31 17:23:39 +0200134 virtual rtc::Optional<bool> needs_denoising() const = 0;
perkja3ede6c2016-03-08 01:27:48 +0100135
deadbeefb10f32f2017-02-08 01:38:21 -0800136 // Returns false if no stats are available, e.g, for a remote source, or a
137 // source which has not seen its first frame yet.
138 //
139 // Implementation should avoid blocking.
nissefcc640f2016-04-01 01:10:42 -0700140 virtual bool GetStats(Stats* stats) = 0;
141
perkja3ede6c2016-03-08 01:27:48 +0100142 protected:
143 virtual ~VideoTrackSourceInterface() {}
144};
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000145
nissedb25d2e2016-02-26 01:24:58 -0800146class VideoTrackInterface
147 : public MediaStreamTrackInterface,
nisseacd935b2016-11-11 03:55:13 -0800148 public rtc::VideoSourceInterface<VideoFrame> {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000149 public:
pbos5214a0a2016-12-16 15:39:11 -0800150 // Video track content hint, used to override the source is_screencast
151 // property.
152 // See https://crbug.com/653531 and https://github.com/WICG/mst-content-hint.
153 enum class ContentHint { kNone, kFluid, kDetailed };
154
deadbeefb10f32f2017-02-08 01:38:21 -0800155 // Register a video sink for this track. Used to connect the track to the
156 // underlying video engine.
nisseacd935b2016-11-11 03:55:13 -0800157 void AddOrUpdateSink(rtc::VideoSinkInterface<VideoFrame>* sink,
pbos5214a0a2016-12-16 15:39:11 -0800158 const rtc::VideoSinkWants& wants) override {}
159 void RemoveSink(rtc::VideoSinkInterface<VideoFrame>* sink) override {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000160
perkja3ede6c2016-03-08 01:27:48 +0100161 virtual VideoTrackSourceInterface* GetSource() const = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000162
pbos5214a0a2016-12-16 15:39:11 -0800163 virtual ContentHint content_hint() const { return ContentHint::kNone; }
164 virtual void set_content_hint(ContentHint hint) {}
165
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000166 protected:
167 virtual ~VideoTrackInterface() {}
168};
169
tommi6eca7e32015-12-15 04:27:11 -0800170// Interface for receiving audio data from a AudioTrack.
171class AudioTrackSinkInterface {
172 public:
173 virtual void OnData(const void* audio_data,
174 int bits_per_sample,
175 int sample_rate,
Peter Kasting69558702016-01-12 16:26:35 -0800176 size_t number_of_channels,
tommi6eca7e32015-12-15 04:27:11 -0800177 size_t number_of_frames) = 0;
178
179 protected:
180 virtual ~AudioTrackSinkInterface() {}
181};
182
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000183// AudioSourceInterface is a reference counted source used for AudioTracks.
deadbeefb10f32f2017-02-08 01:38:21 -0800184// The same source can be used by multiple AudioTracks.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000185class AudioSourceInterface : public MediaSourceInterface {
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000186 public:
187 class AudioObserver {
188 public:
189 virtual void OnSetVolume(double volume) = 0;
190
191 protected:
192 virtual ~AudioObserver() {}
193 };
194
deadbeefb10f32f2017-02-08 01:38:21 -0800195 // TODO(deadbeef): Makes all the interfaces pure virtual after they're
196 // implemented in chromium.
197
198 // Sets the volume of the source. |volume| is in the range of [0, 10].
Tommif888bb52015-12-12 01:37:01 +0100199 // TODO(tommi): This method should be on the track and ideally volume should
200 // be applied in the track in a way that does not affect clones of the track.
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000201 virtual void SetVolume(double volume) {}
202
deadbeefb10f32f2017-02-08 01:38:21 -0800203 // Registers/unregisters observers to the audio source.
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000204 virtual void RegisterAudioObserver(AudioObserver* observer) {}
205 virtual void UnregisterAudioObserver(AudioObserver* observer) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000206
tommi6eca7e32015-12-15 04:27:11 -0800207 // TODO(tommi): Make pure virtual.
208 virtual void AddSink(AudioTrackSinkInterface* sink) {}
209 virtual void RemoveSink(AudioTrackSinkInterface* sink) {}
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000210};
211
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000212// Interface of the audio processor used by the audio track to collect
213// statistics.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000214class AudioProcessorInterface : public rtc::RefCountInterface {
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000215 public:
216 struct AudioProcessorStats {
ivoc4e477a12017-01-15 08:29:46 -0800217 AudioProcessorStats()
218 : typing_noise_detected(false),
219 echo_return_loss(0),
220 echo_return_loss_enhancement(0),
221 echo_delay_median_ms(0),
222 echo_delay_std_ms(0),
223 aec_quality_min(0.0),
224 residual_echo_likelihood(0.0f),
225 residual_echo_likelihood_recent_max(0.0f),
226 aec_divergent_filter_fraction(0.0) {}
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000227 ~AudioProcessorStats() {}
228
229 bool typing_noise_detected;
230 int echo_return_loss;
231 int echo_return_loss_enhancement;
232 int echo_delay_median_ms;
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000233 int echo_delay_std_ms;
ivoc8c63a822016-10-21 04:10:03 -0700234 float aec_quality_min;
235 float residual_echo_likelihood;
ivoc4e477a12017-01-15 08:29:46 -0800236 float residual_echo_likelihood_recent_max;
Minyue2a8a78c2016-04-07 16:48:15 +0200237 float aec_divergent_filter_fraction;
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000238 };
239
240 // Get audio processor statistics.
241 virtual void GetStats(AudioProcessorStats* stats) = 0;
242
243 protected:
244 virtual ~AudioProcessorInterface() {}
245};
246
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000247class AudioTrackInterface : public MediaStreamTrackInterface {
248 public:
deadbeefb10f32f2017-02-08 01:38:21 -0800249 // TODO(deadbeef): Figure out if the following interface should be const or
250 // not.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000251 virtual AudioSourceInterface* GetSource() const = 0;
252
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000253 // Add/Remove a sink that will receive the audio data from the track.
254 virtual void AddSink(AudioTrackSinkInterface* sink) = 0;
255 virtual void RemoveSink(AudioTrackSinkInterface* sink) = 0;
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000256
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000257 // Get the signal level from the audio track.
258 // Return true on success, otherwise false.
deadbeefb10f32f2017-02-08 01:38:21 -0800259 // TODO(deadbeef): Change the interface to int GetSignalLevel() and pure
260 // virtual after it's implemented in chromium.
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000261 virtual bool GetSignalLevel(int* level) { return false; }
262
deadbeef8d60a942017-02-27 14:47:33 -0800263 // Get the audio processor used by the audio track. Return null if the track
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000264 // does not have any processor.
deadbeefb10f32f2017-02-08 01:38:21 -0800265 // TODO(deadbeef): Make the interface pure virtual.
266 virtual rtc::scoped_refptr<AudioProcessorInterface> GetAudioProcessor() {
267 return nullptr;
268 }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000269
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000270 protected:
271 virtual ~AudioTrackInterface() {}
272};
273
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000274typedef std::vector<rtc::scoped_refptr<AudioTrackInterface> >
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000275 AudioTrackVector;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000276typedef std::vector<rtc::scoped_refptr<VideoTrackInterface> >
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000277 VideoTrackVector;
278
deadbeefb10f32f2017-02-08 01:38:21 -0800279// C++ version of https://www.w3.org/TR/mediacapture-streams/#mediastream.
280//
281// A major difference is that remote audio/video tracks (received by a
282// PeerConnection/RtpReceiver) are not synchronized simply by adding them to
283// the same stream; a session description with the correct "a=msid" attributes
284// must be pushed down.
285//
286// Thus, this interface acts as simply a container for tracks.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000287class MediaStreamInterface : public rtc::RefCountInterface,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000288 public NotifierInterface {
289 public:
290 virtual std::string label() const = 0;
291
292 virtual AudioTrackVector GetAudioTracks() = 0;
293 virtual VideoTrackVector GetVideoTracks() = 0;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000294 virtual rtc::scoped_refptr<AudioTrackInterface>
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000295 FindAudioTrack(const std::string& track_id) = 0;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000296 virtual rtc::scoped_refptr<VideoTrackInterface>
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000297 FindVideoTrack(const std::string& track_id) = 0;
298
299 virtual bool AddTrack(AudioTrackInterface* track) = 0;
300 virtual bool AddTrack(VideoTrackInterface* track) = 0;
301 virtual bool RemoveTrack(AudioTrackInterface* track) = 0;
302 virtual bool RemoveTrack(VideoTrackInterface* track) = 0;
303
304 protected:
305 virtual ~MediaStreamInterface() {}
306};
307
308} // namespace webrtc
309
Henrik Kjellander15583c12016-02-10 10:53:12 +0100310#endif // WEBRTC_API_MEDIASTREAMINTERFACE_H_