blob: d7493a7d2941953fc93e2f65eb03fe86399e30e0 [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
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000025#include "webrtc/base/refcount.h"
26#include "webrtc/base/scoped_ref_ptr.h"
Perc0d31e92016-03-31 17:23:39 +020027#include "webrtc/base/optional.h"
perkja3ede6c2016-03-08 01:27:48 +010028#include "webrtc/media/base/mediachannel.h"
nissee73afba2016-01-28 04:47:08 -080029#include "webrtc/media/base/videosinkinterface.h"
nissedb25d2e2016-02-26 01:24:58 -080030#include "webrtc/media/base/videosourceinterface.h"
nisseacd935b2016-11-11 03:55:13 -080031#include "webrtc/video_frame.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
52// Base class for sources. A MediaStreamTrack have an underlying source that
53// provide media. A source can be shared with 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
72// Information about a track.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000073class MediaStreamTrackInterface : public rtc::RefCountInterface,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000074 public NotifierInterface {
75 public:
76 enum TrackState {
perkjc8f952d2016-03-23 00:33:56 -070077 kLive,
78 kEnded,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000079 };
80
deadbeeffac06552015-11-25 11:26:01 -080081 static const char kAudioKind[];
82 static const char kVideoKind[];
83
nissefcc640f2016-04-01 01:10:42 -070084 // The kind() method must return kAudioKind only if the object is a
85 // subclass of AudioTrackInterface, and kVideoKind only if the
86 // object is a subclass of VideoTrackInterface. It is typically used
87 // to protect a static_cast<> to the corresponding subclass.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000088 virtual std::string kind() const = 0;
89 virtual std::string id() const = 0;
90 virtual bool enabled() const = 0;
91 virtual TrackState state() const = 0;
92 virtual bool set_enabled(bool enable) = 0;
fischman@webrtc.org32001ef2013-08-12 23:26:21 +000093
94 protected:
95 virtual ~MediaStreamTrackInterface() {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +000096};
97
perkja3ede6c2016-03-08 01:27:48 +010098// VideoTrackSourceInterface is a reference counted source used for VideoTracks.
99// The same source can be used in multiple VideoTracks.
100class VideoTrackSourceInterface
101 : public MediaSourceInterface,
nisseacd935b2016-11-11 03:55:13 -0800102 public rtc::VideoSourceInterface<VideoFrame> {
perkja3ede6c2016-03-08 01:27:48 +0100103 public:
nissefcc640f2016-04-01 01:10:42 -0700104 struct Stats {
105 // Original size of captured frame, before video adaptation.
106 int input_width;
107 int input_height;
108 };
perkja3ede6c2016-03-08 01:27:48 +0100109
perkj0d3eef22016-03-09 02:39:17 +0100110 // Indicates that parameters suitable for screencasts should be automatically
111 // applied to RtpSenders.
112 // TODO(perkj): Remove these once all known applications have moved to
113 // explicitly setting suitable parameters for screencasts and dont' need this
114 // implicit behavior.
115 virtual bool is_screencast() const = 0;
116
Perc0d31e92016-03-31 17:23:39 +0200117 // Indicates that the encoder should denoise video before encoding it.
118 // If it is not set, the default configuration is used which is different
119 // depending on video codec.
perkj0d3eef22016-03-09 02:39:17 +0100120 // TODO(perkj): Remove this once denoising is done by the source, and not by
121 // the encoder.
Perc0d31e92016-03-31 17:23:39 +0200122 virtual rtc::Optional<bool> needs_denoising() const = 0;
perkja3ede6c2016-03-08 01:27:48 +0100123
nissefcc640f2016-04-01 01:10:42 -0700124 // Returns false if no stats are available, e.g, for a remote
125 // source, or a source which has not seen its first frame yet.
126 // Should avoid blocking.
127 virtual bool GetStats(Stats* stats) = 0;
128
perkja3ede6c2016-03-08 01:27:48 +0100129 protected:
130 virtual ~VideoTrackSourceInterface() {}
131};
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000132
nissedb25d2e2016-02-26 01:24:58 -0800133class VideoTrackInterface
134 : public MediaStreamTrackInterface,
nisseacd935b2016-11-11 03:55:13 -0800135 public rtc::VideoSourceInterface<VideoFrame> {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000136 public:
pbos5214a0a2016-12-16 15:39:11 -0800137 // Video track content hint, used to override the source is_screencast
138 // property.
139 // See https://crbug.com/653531 and https://github.com/WICG/mst-content-hint.
140 enum class ContentHint { kNone, kFluid, kDetailed };
141
nissedb25d2e2016-02-26 01:24:58 -0800142 // Register a video sink for this track.
nisseacd935b2016-11-11 03:55:13 -0800143 void AddOrUpdateSink(rtc::VideoSinkInterface<VideoFrame>* sink,
pbos5214a0a2016-12-16 15:39:11 -0800144 const rtc::VideoSinkWants& wants) override {}
145 void RemoveSink(rtc::VideoSinkInterface<VideoFrame>* sink) override {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000146
perkja3ede6c2016-03-08 01:27:48 +0100147 virtual VideoTrackSourceInterface* GetSource() const = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000148
pbos5214a0a2016-12-16 15:39:11 -0800149 virtual ContentHint content_hint() const { return ContentHint::kNone; }
150 virtual void set_content_hint(ContentHint hint) {}
151
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000152 protected:
153 virtual ~VideoTrackInterface() {}
154};
155
tommi6eca7e32015-12-15 04:27:11 -0800156// Interface for receiving audio data from a AudioTrack.
157class AudioTrackSinkInterface {
158 public:
159 virtual void OnData(const void* audio_data,
160 int bits_per_sample,
161 int sample_rate,
Peter Kasting69558702016-01-12 16:26:35 -0800162 size_t number_of_channels,
tommi6eca7e32015-12-15 04:27:11 -0800163 size_t number_of_frames) = 0;
164
165 protected:
166 virtual ~AudioTrackSinkInterface() {}
167};
168
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000169// AudioSourceInterface is a reference counted source used for AudioTracks.
170// The same source can be used in multiple AudioTracks.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000171class AudioSourceInterface : public MediaSourceInterface {
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000172 public:
173 class AudioObserver {
174 public:
175 virtual void OnSetVolume(double volume) = 0;
176
177 protected:
178 virtual ~AudioObserver() {}
179 };
180
181 // TODO(xians): Makes all the interface pure virtual after Chrome has their
182 // implementations.
183 // Sets the volume to the source. |volume| is in the range of [0, 10].
Tommif888bb52015-12-12 01:37:01 +0100184 // TODO(tommi): This method should be on the track and ideally volume should
185 // be applied in the track in a way that does not affect clones of the track.
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000186 virtual void SetVolume(double volume) {}
187
188 // Registers/unregisters observer to the audio source.
189 virtual void RegisterAudioObserver(AudioObserver* observer) {}
190 virtual void UnregisterAudioObserver(AudioObserver* observer) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000191
tommi6eca7e32015-12-15 04:27:11 -0800192 // TODO(tommi): Make pure virtual.
193 virtual void AddSink(AudioTrackSinkInterface* sink) {}
194 virtual void RemoveSink(AudioTrackSinkInterface* sink) {}
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000195};
196
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000197// Interface of the audio processor used by the audio track to collect
198// statistics.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000199class AudioProcessorInterface : public rtc::RefCountInterface {
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000200 public:
201 struct AudioProcessorStats {
202 AudioProcessorStats() : typing_noise_detected(false),
203 echo_return_loss(0),
204 echo_return_loss_enhancement(0),
205 echo_delay_median_ms(0),
Minyue2a8a78c2016-04-07 16:48:15 +0200206 echo_delay_std_ms(0),
ivoc8c63a822016-10-21 04:10:03 -0700207 aec_quality_min(0.0),
208 residual_echo_likelihood(0.0f),
Minyue2a8a78c2016-04-07 16:48:15 +0200209 aec_divergent_filter_fraction(0.0) {}
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000210 ~AudioProcessorStats() {}
211
212 bool typing_noise_detected;
213 int echo_return_loss;
214 int echo_return_loss_enhancement;
215 int echo_delay_median_ms;
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000216 int echo_delay_std_ms;
ivoc8c63a822016-10-21 04:10:03 -0700217 float aec_quality_min;
218 float residual_echo_likelihood;
Minyue2a8a78c2016-04-07 16:48:15 +0200219 float aec_divergent_filter_fraction;
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000220 };
221
222 // Get audio processor statistics.
223 virtual void GetStats(AudioProcessorStats* stats) = 0;
224
225 protected:
226 virtual ~AudioProcessorInterface() {}
227};
228
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000229class AudioTrackInterface : public MediaStreamTrackInterface {
230 public:
231 // TODO(xians): Figure out if the following interface should be const or not.
232 virtual AudioSourceInterface* GetSource() const = 0;
233
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000234 // Add/Remove a sink that will receive the audio data from the track.
235 virtual void AddSink(AudioTrackSinkInterface* sink) = 0;
236 virtual void RemoveSink(AudioTrackSinkInterface* sink) = 0;
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000237
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000238 // Get the signal level from the audio track.
239 // Return true on success, otherwise false.
240 // TODO(xians): Change the interface to int GetSignalLevel() and pure virtual
241 // after Chrome has the correct implementation of the interface.
242 virtual bool GetSignalLevel(int* level) { return false; }
243
244 // Get the audio processor used by the audio track. Return NULL if the track
245 // does not have any processor.
246 // TODO(xians): Make the interface pure virtual.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000247 virtual rtc::scoped_refptr<AudioProcessorInterface>
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000248 GetAudioProcessor() { return NULL; }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000249
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000250 protected:
251 virtual ~AudioTrackInterface() {}
252};
253
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000254typedef std::vector<rtc::scoped_refptr<AudioTrackInterface> >
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000255 AudioTrackVector;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000256typedef std::vector<rtc::scoped_refptr<VideoTrackInterface> >
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000257 VideoTrackVector;
258
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000259class MediaStreamInterface : public rtc::RefCountInterface,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000260 public NotifierInterface {
261 public:
262 virtual std::string label() const = 0;
263
264 virtual AudioTrackVector GetAudioTracks() = 0;
265 virtual VideoTrackVector GetVideoTracks() = 0;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000266 virtual rtc::scoped_refptr<AudioTrackInterface>
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000267 FindAudioTrack(const std::string& track_id) = 0;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000268 virtual rtc::scoped_refptr<VideoTrackInterface>
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000269 FindVideoTrack(const std::string& track_id) = 0;
270
271 virtual bool AddTrack(AudioTrackInterface* track) = 0;
272 virtual bool AddTrack(VideoTrackInterface* track) = 0;
273 virtual bool RemoveTrack(AudioTrackInterface* track) = 0;
274 virtual bool RemoveTrack(VideoTrackInterface* track) = 0;
275
276 protected:
277 virtual ~MediaStreamInterface() {}
278};
279
280} // namespace webrtc
281
Henrik Kjellander15583c12016-02-10 10:53:12 +0100282#endif // WEBRTC_API_MEDIASTREAMINTERFACE_H_