blob: f1730c57be73944cb07ab9d254cae98cc493c0de [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;
34class VideoCapturer;
35class VideoRenderer;
36class VideoFrame;
37
38} // namespace cricket
39
40namespace webrtc {
41
42// Generic observer interface.
43class ObserverInterface {
44 public:
45 virtual void OnChanged() = 0;
46
47 protected:
48 virtual ~ObserverInterface() {}
49};
50
51class NotifierInterface {
52 public:
53 virtual void RegisterObserver(ObserverInterface* observer) = 0;
54 virtual void UnregisterObserver(ObserverInterface* observer) = 0;
55
56 virtual ~NotifierInterface() {}
57};
58
59// Base class for sources. A MediaStreamTrack have an underlying source that
60// provide media. A source can be shared with multiple tracks.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000061class MediaSourceInterface : public rtc::RefCountInterface,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000062 public NotifierInterface {
63 public:
64 enum SourceState {
65 kInitializing,
66 kLive,
67 kEnded,
68 kMuted
69 };
70
71 virtual SourceState state() const = 0;
72
tommi6eca7e32015-12-15 04:27:11 -080073 virtual bool remote() const = 0;
74
henrike@webrtc.org28e20752013-07-10 00:45:36 +000075 protected:
76 virtual ~MediaSourceInterface() {}
77};
78
79// Information about a track.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000080class MediaStreamTrackInterface : public rtc::RefCountInterface,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000081 public NotifierInterface {
82 public:
83 enum TrackState {
perkjc8f952d2016-03-23 00:33:56 -070084 kLive,
85 kEnded,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000086 };
87
deadbeeffac06552015-11-25 11:26:01 -080088 static const char kAudioKind[];
89 static const char kVideoKind[];
90
nissefcc640f2016-04-01 01:10:42 -070091 // The kind() method must return kAudioKind only if the object is a
92 // subclass of AudioTrackInterface, and kVideoKind only if the
93 // object is a subclass of VideoTrackInterface. It is typically used
94 // to protect a static_cast<> to the corresponding subclass.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000095 virtual std::string kind() const = 0;
96 virtual std::string id() const = 0;
97 virtual bool enabled() const = 0;
98 virtual TrackState state() const = 0;
99 virtual bool set_enabled(bool enable) = 0;
perkj7ca142e2016-03-24 14:19:08 +0100100 // TODO(perkj): Remove when Chrome tests doesn't rely on this method.
101 virtual bool set_state(TrackState state) { return true;}
fischman@webrtc.org32001ef2013-08-12 23:26:21 +0000102
103 protected:
104 virtual ~MediaStreamTrackInterface() {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000105};
106
perkja3ede6c2016-03-08 01:27:48 +0100107// VideoTrackSourceInterface is a reference counted source used for VideoTracks.
108// The same source can be used in multiple VideoTracks.
109class VideoTrackSourceInterface
110 : public MediaSourceInterface,
111 public rtc::VideoSourceInterface<cricket::VideoFrame> {
112 public:
nissefcc640f2016-04-01 01:10:42 -0700113 struct Stats {
114 // Original size of captured frame, before video adaptation.
115 int input_width;
116 int input_height;
117 };
perkja3ede6c2016-03-08 01:27:48 +0100118 // Get access to the source implementation of cricket::VideoCapturer.
119 // This can be used for receiving frames and state notifications.
120 // But it should not be used for starting or stopping capturing.
121 // TODO(perkj): We are currently trying to replace all internal use of
122 // cricket::VideoCapturer with rtc::VideoSourceInterface. Once that
123 // refactoring is done,
124 // remove this method.
125 virtual cricket::VideoCapturer* GetVideoCapturer() = 0;
126
127 virtual void Stop() = 0;
128 virtual void Restart() = 0;
129
perkj0d3eef22016-03-09 02:39:17 +0100130 // Indicates that parameters suitable for screencasts should be automatically
131 // applied to RtpSenders.
132 // TODO(perkj): Remove these once all known applications have moved to
133 // explicitly setting suitable parameters for screencasts and dont' need this
134 // implicit behavior.
135 virtual bool is_screencast() const = 0;
136
Perc0d31e92016-03-31 17:23:39 +0200137 // Indicates that the encoder should denoise video before encoding it.
138 // If it is not set, the default configuration is used which is different
139 // depending on video codec.
perkj0d3eef22016-03-09 02:39:17 +0100140 // TODO(perkj): Remove this once denoising is done by the source, and not by
141 // the encoder.
Perc0d31e92016-03-31 17:23:39 +0200142 virtual rtc::Optional<bool> needs_denoising() const = 0;
perkja3ede6c2016-03-08 01:27:48 +0100143
nissefcc640f2016-04-01 01:10:42 -0700144 // Returns false if no stats are available, e.g, for a remote
145 // source, or a source which has not seen its first frame yet.
146 // Should avoid blocking.
147 virtual bool GetStats(Stats* stats) = 0;
148
perkja3ede6c2016-03-08 01:27:48 +0100149 protected:
150 virtual ~VideoTrackSourceInterface() {}
151};
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000152
nissedb25d2e2016-02-26 01:24:58 -0800153class VideoTrackInterface
154 : public MediaStreamTrackInterface,
155 public rtc::VideoSourceInterface<cricket::VideoFrame> {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000156 public:
nissedb25d2e2016-02-26 01:24:58 -0800157 // Register a video sink for this track.
158 void AddOrUpdateSink(rtc::VideoSinkInterface<cricket::VideoFrame>* sink,
159 const rtc::VideoSinkWants& wants) override{};
160 void RemoveSink(
161 rtc::VideoSinkInterface<cricket::VideoFrame>* sink) override{};
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000162
perkja3ede6c2016-03-08 01:27:48 +0100163 virtual VideoTrackSourceInterface* GetSource() const = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000164
165 protected:
166 virtual ~VideoTrackInterface() {}
167};
168
tommi6eca7e32015-12-15 04:27:11 -0800169// Interface for receiving audio data from a AudioTrack.
170class AudioTrackSinkInterface {
171 public:
172 virtual void OnData(const void* audio_data,
173 int bits_per_sample,
174 int sample_rate,
Peter Kasting69558702016-01-12 16:26:35 -0800175 size_t number_of_channels,
tommi6eca7e32015-12-15 04:27:11 -0800176 size_t number_of_frames) = 0;
177
178 protected:
179 virtual ~AudioTrackSinkInterface() {}
180};
181
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000182// AudioSourceInterface is a reference counted source used for AudioTracks.
183// The same source can be used in multiple AudioTracks.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000184class AudioSourceInterface : public MediaSourceInterface {
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000185 public:
186 class AudioObserver {
187 public:
188 virtual void OnSetVolume(double volume) = 0;
189
190 protected:
191 virtual ~AudioObserver() {}
192 };
193
194 // TODO(xians): Makes all the interface pure virtual after Chrome has their
195 // implementations.
196 // Sets the volume to the source. |volume| is in the range of [0, 10].
Tommif888bb52015-12-12 01:37:01 +0100197 // TODO(tommi): This method should be on the track and ideally volume should
198 // be applied in the track in a way that does not affect clones of the track.
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000199 virtual void SetVolume(double volume) {}
200
201 // Registers/unregisters observer to the audio source.
202 virtual void RegisterAudioObserver(AudioObserver* observer) {}
203 virtual void UnregisterAudioObserver(AudioObserver* observer) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000204
tommi6eca7e32015-12-15 04:27:11 -0800205 // TODO(tommi): Make pure virtual.
206 virtual void AddSink(AudioTrackSinkInterface* sink) {}
207 virtual void RemoveSink(AudioTrackSinkInterface* sink) {}
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000208};
209
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000210// Interface of the audio processor used by the audio track to collect
211// statistics.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000212class AudioProcessorInterface : public rtc::RefCountInterface {
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000213 public:
214 struct AudioProcessorStats {
215 AudioProcessorStats() : typing_noise_detected(false),
216 echo_return_loss(0),
217 echo_return_loss_enhancement(0),
218 echo_delay_median_ms(0),
219 aec_quality_min(0.0),
220 echo_delay_std_ms(0) {}
221 ~AudioProcessorStats() {}
222
223 bool typing_noise_detected;
224 int echo_return_loss;
225 int echo_return_loss_enhancement;
226 int echo_delay_median_ms;
227 float aec_quality_min;
228 int echo_delay_std_ms;
229 };
230
231 // Get audio processor statistics.
232 virtual void GetStats(AudioProcessorStats* stats) = 0;
233
234 protected:
235 virtual ~AudioProcessorInterface() {}
236};
237
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000238class AudioTrackInterface : public MediaStreamTrackInterface {
239 public:
240 // TODO(xians): Figure out if the following interface should be const or not.
241 virtual AudioSourceInterface* GetSource() const = 0;
242
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000243 // Add/Remove a sink that will receive the audio data from the track.
244 virtual void AddSink(AudioTrackSinkInterface* sink) = 0;
245 virtual void RemoveSink(AudioTrackSinkInterface* sink) = 0;
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000246
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000247 // Get the signal level from the audio track.
248 // Return true on success, otherwise false.
249 // TODO(xians): Change the interface to int GetSignalLevel() and pure virtual
250 // after Chrome has the correct implementation of the interface.
251 virtual bool GetSignalLevel(int* level) { return false; }
252
253 // Get the audio processor used by the audio track. Return NULL if the track
254 // does not have any processor.
255 // TODO(xians): Make the interface pure virtual.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000256 virtual rtc::scoped_refptr<AudioProcessorInterface>
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000257 GetAudioProcessor() { return NULL; }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000258
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000259 protected:
260 virtual ~AudioTrackInterface() {}
261};
262
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000263typedef std::vector<rtc::scoped_refptr<AudioTrackInterface> >
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000264 AudioTrackVector;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000265typedef std::vector<rtc::scoped_refptr<VideoTrackInterface> >
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000266 VideoTrackVector;
267
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000268class MediaStreamInterface : public rtc::RefCountInterface,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000269 public NotifierInterface {
270 public:
271 virtual std::string label() const = 0;
272
273 virtual AudioTrackVector GetAudioTracks() = 0;
274 virtual VideoTrackVector GetVideoTracks() = 0;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000275 virtual rtc::scoped_refptr<AudioTrackInterface>
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000276 FindAudioTrack(const std::string& track_id) = 0;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000277 virtual rtc::scoped_refptr<VideoTrackInterface>
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000278 FindVideoTrack(const std::string& track_id) = 0;
279
280 virtual bool AddTrack(AudioTrackInterface* track) = 0;
281 virtual bool AddTrack(VideoTrackInterface* track) = 0;
282 virtual bool RemoveTrack(AudioTrackInterface* track) = 0;
283 virtual bool RemoveTrack(VideoTrackInterface* track) = 0;
284
285 protected:
286 virtual ~MediaStreamInterface() {}
287};
288
289} // namespace webrtc
290
Henrik Kjellander15583c12016-02-10 10:53:12 +0100291#endif // WEBRTC_API_MEDIASTREAMINTERFACE_H_