blob: c9ce4a5a7b49d7bf26a9e5e07677228a3651cc98 [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"
perkja3ede6c2016-03-08 01:27:48 +010026#include "webrtc/media/base/mediachannel.h"
nissee73afba2016-01-28 04:47:08 -080027#include "webrtc/media/base/videosinkinterface.h"
nissedb25d2e2016-02-26 01:24:58 -080028#include "webrtc/media/base/videosourceinterface.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000029
30namespace cricket {
31
32class AudioRenderer;
33class VideoCapturer;
34class 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 {
83 kInitializing, // Track is beeing negotiated.
84 kLive = 1, // Track alive
85 kEnded = 2, // Track have ended
86 kFailed = 3, // Track negotiation failed.
87 };
88
deadbeeffac06552015-11-25 11:26:01 -080089 static const char kAudioKind[];
90 static const char kVideoKind[];
91
henrike@webrtc.org28e20752013-07-10 00:45:36 +000092 virtual std::string kind() const = 0;
93 virtual std::string id() const = 0;
94 virtual bool enabled() const = 0;
95 virtual TrackState state() const = 0;
96 virtual bool set_enabled(bool enable) = 0;
97 // These methods should be called by implementation only.
98 virtual bool set_state(TrackState new_state) = 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
104// Interface for rendering VideoFrames from a VideoTrack
nissee73afba2016-01-28 04:47:08 -0800105class VideoRendererInterface
106 : public rtc::VideoSinkInterface<cricket::VideoFrame> {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000107 public:
guoweis@webrtc.org00c509a2015-03-12 21:37:26 +0000108 // |frame| may have pending rotation. For clients which can't apply rotation,
109 // |frame|->GetCopyWithRotationApplied() will return a frame that has the
110 // rotation applied.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000111 virtual void RenderFrame(const cricket::VideoFrame* frame) = 0;
nissee73afba2016-01-28 04:47:08 -0800112 // Intended to replace RenderFrame.
113 void OnFrame(const cricket::VideoFrame& frame) override {
114 RenderFrame(&frame);
115 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000116
117 protected:
118 // The destructor is protected to prevent deletion via the interface.
119 // This is so that we allow reference counted classes, where the destructor
120 // should never be public, to implement the interface.
121 virtual ~VideoRendererInterface() {}
122};
123
perkja3ede6c2016-03-08 01:27:48 +0100124// VideoTrackSourceInterface is a reference counted source used for VideoTracks.
125// The same source can be used in multiple VideoTracks.
126class VideoTrackSourceInterface
127 : public MediaSourceInterface,
128 public rtc::VideoSourceInterface<cricket::VideoFrame> {
129 public:
130 // Get access to the source implementation of cricket::VideoCapturer.
131 // This can be used for receiving frames and state notifications.
132 // But it should not be used for starting or stopping capturing.
133 // TODO(perkj): We are currently trying to replace all internal use of
134 // cricket::VideoCapturer with rtc::VideoSourceInterface. Once that
135 // refactoring is done,
136 // remove this method.
137 virtual cricket::VideoCapturer* GetVideoCapturer() = 0;
138
139 virtual void Stop() = 0;
140 virtual void Restart() = 0;
141
142 virtual const cricket::VideoOptions* options() const = 0;
143
144 protected:
145 virtual ~VideoTrackSourceInterface() {}
146};
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000147
nissedb25d2e2016-02-26 01:24:58 -0800148class VideoTrackInterface
149 : public MediaStreamTrackInterface,
150 public rtc::VideoSourceInterface<cricket::VideoFrame> {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000151 public:
nissedb25d2e2016-02-26 01:24:58 -0800152 // AddRenderer and RemoveRenderer are for backwards compatibility
153 // only. They are obsoleted by the methods of
154 // rtc::VideoSourceInterface.
155 virtual void AddRenderer(VideoRendererInterface* renderer) {
156 AddOrUpdateSink(renderer, rtc::VideoSinkWants());
157 }
158 virtual void RemoveRenderer(VideoRendererInterface* renderer) {
159 RemoveSink(renderer);
160 }
161
162 // Register a video sink for this track.
163 void AddOrUpdateSink(rtc::VideoSinkInterface<cricket::VideoFrame>* sink,
164 const rtc::VideoSinkWants& wants) override{};
165 void RemoveSink(
166 rtc::VideoSinkInterface<cricket::VideoFrame>* sink) override{};
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000167
perkja3ede6c2016-03-08 01:27:48 +0100168 virtual VideoTrackSourceInterface* GetSource() const = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000169
nisse8e8908a2016-02-05 01:52:15 -0800170 // Return the track input sink. I.e., frames sent to this sink are
171 // propagated to all renderers registered with the track. The
172 // returned sink must not change between calls. Currently, this
173 // method is used for remote tracks (VideoRtpReceiver); further
174 // refactoring is planned for this path, it's unclear if this method
175 // belongs here long term.
176
177 // We do this instead of simply implementing the
178 // VideoSourceInterface directly, because if we did the latter, we'd
179 // need an OnFrame method in VideoTrackProxy, with a thread jump on
180 // each call.
181
182 // TODO(nisse): It has a default implementation so that mock
183 // objects, in particular, chrome's MockWebRtcVideoTrack, doesn't
184 // need to know about it. Consider removing the implementation (or
185 // this comment) after refactoring dust settles.
186 virtual rtc::VideoSinkInterface<cricket::VideoFrame>* GetSink() {
187 return nullptr;
188 };
189
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000190 protected:
191 virtual ~VideoTrackInterface() {}
192};
193
tommi6eca7e32015-12-15 04:27:11 -0800194// Interface for receiving audio data from a AudioTrack.
195class AudioTrackSinkInterface {
196 public:
197 virtual void OnData(const void* audio_data,
198 int bits_per_sample,
199 int sample_rate,
Peter Kasting69558702016-01-12 16:26:35 -0800200 size_t number_of_channels,
tommi6eca7e32015-12-15 04:27:11 -0800201 size_t number_of_frames) = 0;
202
203 protected:
204 virtual ~AudioTrackSinkInterface() {}
205};
206
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000207// AudioSourceInterface is a reference counted source used for AudioTracks.
208// The same source can be used in multiple AudioTracks.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000209class AudioSourceInterface : public MediaSourceInterface {
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000210 public:
211 class AudioObserver {
212 public:
213 virtual void OnSetVolume(double volume) = 0;
214
215 protected:
216 virtual ~AudioObserver() {}
217 };
218
219 // TODO(xians): Makes all the interface pure virtual after Chrome has their
220 // implementations.
221 // Sets the volume to the source. |volume| is in the range of [0, 10].
Tommif888bb52015-12-12 01:37:01 +0100222 // TODO(tommi): This method should be on the track and ideally volume should
223 // be applied in the track in a way that does not affect clones of the track.
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000224 virtual void SetVolume(double volume) {}
225
226 // Registers/unregisters observer to the audio source.
227 virtual void RegisterAudioObserver(AudioObserver* observer) {}
228 virtual void UnregisterAudioObserver(AudioObserver* observer) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000229
tommi6eca7e32015-12-15 04:27:11 -0800230 // TODO(tommi): Make pure virtual.
231 virtual void AddSink(AudioTrackSinkInterface* sink) {}
232 virtual void RemoveSink(AudioTrackSinkInterface* sink) {}
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000233};
234
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000235// Interface of the audio processor used by the audio track to collect
236// statistics.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000237class AudioProcessorInterface : public rtc::RefCountInterface {
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000238 public:
239 struct AudioProcessorStats {
240 AudioProcessorStats() : typing_noise_detected(false),
241 echo_return_loss(0),
242 echo_return_loss_enhancement(0),
243 echo_delay_median_ms(0),
244 aec_quality_min(0.0),
245 echo_delay_std_ms(0) {}
246 ~AudioProcessorStats() {}
247
248 bool typing_noise_detected;
249 int echo_return_loss;
250 int echo_return_loss_enhancement;
251 int echo_delay_median_ms;
252 float aec_quality_min;
253 int echo_delay_std_ms;
254 };
255
256 // Get audio processor statistics.
257 virtual void GetStats(AudioProcessorStats* stats) = 0;
258
259 protected:
260 virtual ~AudioProcessorInterface() {}
261};
262
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000263class AudioTrackInterface : public MediaStreamTrackInterface {
264 public:
265 // TODO(xians): Figure out if the following interface should be const or not.
266 virtual AudioSourceInterface* GetSource() const = 0;
267
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000268 // Add/Remove a sink that will receive the audio data from the track.
269 virtual void AddSink(AudioTrackSinkInterface* sink) = 0;
270 virtual void RemoveSink(AudioTrackSinkInterface* sink) = 0;
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000271
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000272 // Get the signal level from the audio track.
273 // Return true on success, otherwise false.
274 // TODO(xians): Change the interface to int GetSignalLevel() and pure virtual
275 // after Chrome has the correct implementation of the interface.
276 virtual bool GetSignalLevel(int* level) { return false; }
277
278 // Get the audio processor used by the audio track. Return NULL if the track
279 // does not have any processor.
280 // TODO(xians): Make the interface pure virtual.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000281 virtual rtc::scoped_refptr<AudioProcessorInterface>
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000282 GetAudioProcessor() { return NULL; }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000283
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000284 protected:
285 virtual ~AudioTrackInterface() {}
286};
287
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000288typedef std::vector<rtc::scoped_refptr<AudioTrackInterface> >
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000289 AudioTrackVector;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000290typedef std::vector<rtc::scoped_refptr<VideoTrackInterface> >
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000291 VideoTrackVector;
292
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000293class MediaStreamInterface : public rtc::RefCountInterface,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000294 public NotifierInterface {
295 public:
296 virtual std::string label() const = 0;
297
298 virtual AudioTrackVector GetAudioTracks() = 0;
299 virtual VideoTrackVector GetVideoTracks() = 0;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000300 virtual rtc::scoped_refptr<AudioTrackInterface>
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000301 FindAudioTrack(const std::string& track_id) = 0;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000302 virtual rtc::scoped_refptr<VideoTrackInterface>
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000303 FindVideoTrack(const std::string& track_id) = 0;
304
305 virtual bool AddTrack(AudioTrackInterface* track) = 0;
306 virtual bool AddTrack(VideoTrackInterface* track) = 0;
307 virtual bool RemoveTrack(AudioTrackInterface* track) = 0;
308 virtual bool RemoveTrack(VideoTrackInterface* track) = 0;
309
310 protected:
311 virtual ~MediaStreamInterface() {}
312};
313
314} // namespace webrtc
315
Henrik Kjellander15583c12016-02-10 10:53:12 +0100316#endif // WEBRTC_API_MEDIASTREAMINTERFACE_H_