blob: 7f563250249257b0fa5375043f64766e84425439 [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
20#include <string>
21#include <vector>
22
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000023#include "webrtc/base/basictypes.h"
24#include "webrtc/base/refcount.h"
25#include "webrtc/base/scoped_ref_ptr.h"
Perc0d31e92016-03-31 17:23:39 +020026#include "webrtc/base/optional.h"
perkja3ede6c2016-03-08 01:27:48 +010027#include "webrtc/media/base/mediachannel.h"
nissee73afba2016-01-28 04:47:08 -080028#include "webrtc/media/base/videosinkinterface.h"
nissedb25d2e2016-02-26 01:24:58 -080029#include "webrtc/media/base/videosourceinterface.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000030
31namespace cricket {
32
33class AudioRenderer;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000034class VideoRenderer;
35class VideoFrame;
36
37} // namespace cricket
38
39namespace 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
58// Base class for sources. A MediaStreamTrack have an underlying source that
59// provide media. A source can be shared with 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
78// Information about a track.
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;
95 virtual std::string id() const = 0;
96 virtual bool enabled() const = 0;
97 virtual TrackState state() const = 0;
98 virtual bool set_enabled(bool enable) = 0;
fischman@webrtc.org32001ef2013-08-12 23:26:21 +000099
100 protected:
101 virtual ~MediaStreamTrackInterface() {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000102};
103
perkja3ede6c2016-03-08 01:27:48 +0100104// VideoTrackSourceInterface is a reference counted source used for VideoTracks.
105// The same source can be used in multiple VideoTracks.
106class VideoTrackSourceInterface
107 : public MediaSourceInterface,
108 public rtc::VideoSourceInterface<cricket::VideoFrame> {
109 public:
nissefcc640f2016-04-01 01:10:42 -0700110 struct Stats {
111 // Original size of captured frame, before video adaptation.
112 int input_width;
113 int input_height;
114 };
perkja3ede6c2016-03-08 01:27:48 +0100115
116 virtual void Stop() = 0;
117 virtual void Restart() = 0;
118
perkj0d3eef22016-03-09 02:39:17 +0100119 // Indicates that parameters suitable for screencasts should be automatically
120 // applied to RtpSenders.
121 // TODO(perkj): Remove these once all known applications have moved to
122 // explicitly setting suitable parameters for screencasts and dont' need this
123 // implicit behavior.
124 virtual bool is_screencast() const = 0;
125
Perc0d31e92016-03-31 17:23:39 +0200126 // Indicates that the encoder should denoise video before encoding it.
127 // If it is not set, the default configuration is used which is different
128 // depending on video codec.
perkj0d3eef22016-03-09 02:39:17 +0100129 // TODO(perkj): Remove this once denoising is done by the source, and not by
130 // the encoder.
Perc0d31e92016-03-31 17:23:39 +0200131 virtual rtc::Optional<bool> needs_denoising() const = 0;
perkja3ede6c2016-03-08 01:27:48 +0100132
nissefcc640f2016-04-01 01:10:42 -0700133 // Returns false if no stats are available, e.g, for a remote
134 // source, or a source which has not seen its first frame yet.
135 // Should avoid blocking.
136 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,
144 public rtc::VideoSourceInterface<cricket::VideoFrame> {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000145 public:
nissedb25d2e2016-02-26 01:24:58 -0800146 // Register a video sink for this track.
147 void AddOrUpdateSink(rtc::VideoSinkInterface<cricket::VideoFrame>* sink,
148 const rtc::VideoSinkWants& wants) override{};
149 void RemoveSink(
150 rtc::VideoSinkInterface<cricket::VideoFrame>* sink) override{};
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000151
perkja3ede6c2016-03-08 01:27:48 +0100152 virtual VideoTrackSourceInterface* GetSource() const = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000153
154 protected:
155 virtual ~VideoTrackInterface() {}
156};
157
tommi6eca7e32015-12-15 04:27:11 -0800158// Interface for receiving audio data from a AudioTrack.
159class AudioTrackSinkInterface {
160 public:
161 virtual void OnData(const void* audio_data,
162 int bits_per_sample,
163 int sample_rate,
Peter Kasting69558702016-01-12 16:26:35 -0800164 size_t number_of_channels,
tommi6eca7e32015-12-15 04:27:11 -0800165 size_t number_of_frames) = 0;
166
167 protected:
168 virtual ~AudioTrackSinkInterface() {}
169};
170
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000171// AudioSourceInterface is a reference counted source used for AudioTracks.
172// The same source can be used in multiple AudioTracks.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000173class AudioSourceInterface : public MediaSourceInterface {
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000174 public:
175 class AudioObserver {
176 public:
177 virtual void OnSetVolume(double volume) = 0;
178
179 protected:
180 virtual ~AudioObserver() {}
181 };
182
183 // TODO(xians): Makes all the interface pure virtual after Chrome has their
184 // implementations.
185 // Sets the volume to the source. |volume| is in the range of [0, 10].
Tommif888bb52015-12-12 01:37:01 +0100186 // TODO(tommi): This method should be on the track and ideally volume should
187 // be applied in the track in a way that does not affect clones of the track.
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000188 virtual void SetVolume(double volume) {}
189
190 // Registers/unregisters observer to the audio source.
191 virtual void RegisterAudioObserver(AudioObserver* observer) {}
192 virtual void UnregisterAudioObserver(AudioObserver* observer) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000193
tommi6eca7e32015-12-15 04:27:11 -0800194 // TODO(tommi): Make pure virtual.
195 virtual void AddSink(AudioTrackSinkInterface* sink) {}
196 virtual void RemoveSink(AudioTrackSinkInterface* sink) {}
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000197};
198
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000199// Interface of the audio processor used by the audio track to collect
200// statistics.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000201class AudioProcessorInterface : public rtc::RefCountInterface {
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000202 public:
203 struct AudioProcessorStats {
204 AudioProcessorStats() : typing_noise_detected(false),
205 echo_return_loss(0),
206 echo_return_loss_enhancement(0),
207 echo_delay_median_ms(0),
208 aec_quality_min(0.0),
Minyue2a8a78c2016-04-07 16:48:15 +0200209 echo_delay_std_ms(0),
210 aec_divergent_filter_fraction(0.0) {}
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000211 ~AudioProcessorStats() {}
212
213 bool typing_noise_detected;
214 int echo_return_loss;
215 int echo_return_loss_enhancement;
216 int echo_delay_median_ms;
217 float aec_quality_min;
218 int echo_delay_std_ms;
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_