blob: 100db08d04aa4656330dc69fca8f7f1ea6f58991 [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"
nissee73afba2016-01-28 04:47:08 -080026#include "webrtc/media/base/videosinkinterface.h"
nissedb25d2e2016-02-26 01:24:58 -080027#include "webrtc/media/base/videosourceinterface.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000028
29namespace cricket {
30
31class AudioRenderer;
32class VideoCapturer;
33class VideoRenderer;
34class VideoFrame;
35
36} // namespace cricket
37
38namespace webrtc {
39
40// Generic observer interface.
41class ObserverInterface {
42 public:
43 virtual void OnChanged() = 0;
44
45 protected:
46 virtual ~ObserverInterface() {}
47};
48
49class NotifierInterface {
50 public:
51 virtual void RegisterObserver(ObserverInterface* observer) = 0;
52 virtual void UnregisterObserver(ObserverInterface* observer) = 0;
53
54 virtual ~NotifierInterface() {}
55};
56
57// Base class for sources. A MediaStreamTrack have an underlying source that
58// provide media. A source can be shared with multiple tracks.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000059class MediaSourceInterface : public rtc::RefCountInterface,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000060 public NotifierInterface {
61 public:
62 enum SourceState {
63 kInitializing,
64 kLive,
65 kEnded,
66 kMuted
67 };
68
69 virtual SourceState state() const = 0;
70
tommi6eca7e32015-12-15 04:27:11 -080071 virtual bool remote() const = 0;
72
henrike@webrtc.org28e20752013-07-10 00:45:36 +000073 protected:
74 virtual ~MediaSourceInterface() {}
75};
76
77// Information about a track.
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 {
82 kInitializing, // Track is beeing negotiated.
83 kLive = 1, // Track alive
84 kEnded = 2, // Track have ended
85 kFailed = 3, // Track negotiation failed.
86 };
87
deadbeeffac06552015-11-25 11:26:01 -080088 static const char kAudioKind[];
89 static const char kVideoKind[];
90
henrike@webrtc.org28e20752013-07-10 00:45:36 +000091 virtual std::string kind() const = 0;
92 virtual std::string id() const = 0;
93 virtual bool enabled() const = 0;
94 virtual TrackState state() const = 0;
95 virtual bool set_enabled(bool enable) = 0;
96 // These methods should be called by implementation only.
97 virtual bool set_state(TrackState new_state) = 0;
fischman@webrtc.org32001ef2013-08-12 23:26:21 +000098
99 protected:
100 virtual ~MediaStreamTrackInterface() {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000101};
102
103// Interface for rendering VideoFrames from a VideoTrack
nissee73afba2016-01-28 04:47:08 -0800104class VideoRendererInterface
105 : public rtc::VideoSinkInterface<cricket::VideoFrame> {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000106 public:
guoweis@webrtc.org00c509a2015-03-12 21:37:26 +0000107 // |frame| may have pending rotation. For clients which can't apply rotation,
108 // |frame|->GetCopyWithRotationApplied() will return a frame that has the
109 // rotation applied.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000110 virtual void RenderFrame(const cricket::VideoFrame* frame) = 0;
nissee73afba2016-01-28 04:47:08 -0800111 // Intended to replace RenderFrame.
112 void OnFrame(const cricket::VideoFrame& frame) override {
113 RenderFrame(&frame);
114 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000115
116 protected:
117 // The destructor is protected to prevent deletion via the interface.
118 // This is so that we allow reference counted classes, where the destructor
119 // should never be public, to implement the interface.
120 virtual ~VideoRendererInterface() {}
121};
122
123class VideoSourceInterface;
124
nissedb25d2e2016-02-26 01:24:58 -0800125class VideoTrackInterface
126 : public MediaStreamTrackInterface,
127 public rtc::VideoSourceInterface<cricket::VideoFrame> {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000128 public:
nissedb25d2e2016-02-26 01:24:58 -0800129 // Make an unqualified VideoSourceInterface resolve to
130 // webrtc::VideoSourceInterface, not our base class
131 // rtc::VideoSourceInterface<cricket::VideoFrame>.
132 using VideoSourceInterface = webrtc::VideoSourceInterface;
133
134 // AddRenderer and RemoveRenderer are for backwards compatibility
135 // only. They are obsoleted by the methods of
136 // rtc::VideoSourceInterface.
137 virtual void AddRenderer(VideoRendererInterface* renderer) {
138 AddOrUpdateSink(renderer, rtc::VideoSinkWants());
139 }
140 virtual void RemoveRenderer(VideoRendererInterface* renderer) {
141 RemoveSink(renderer);
142 }
143
144 // Register a video sink for this track.
145 void AddOrUpdateSink(rtc::VideoSinkInterface<cricket::VideoFrame>* sink,
146 const rtc::VideoSinkWants& wants) override{};
147 void RemoveSink(
148 rtc::VideoSinkInterface<cricket::VideoFrame>* sink) override{};
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000149
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000150 virtual VideoSourceInterface* GetSource() const = 0;
151
nisse8e8908a2016-02-05 01:52:15 -0800152 // Return the track input sink. I.e., frames sent to this sink are
153 // propagated to all renderers registered with the track. The
154 // returned sink must not change between calls. Currently, this
155 // method is used for remote tracks (VideoRtpReceiver); further
156 // refactoring is planned for this path, it's unclear if this method
157 // belongs here long term.
158
159 // We do this instead of simply implementing the
160 // VideoSourceInterface directly, because if we did the latter, we'd
161 // need an OnFrame method in VideoTrackProxy, with a thread jump on
162 // each call.
163
164 // TODO(nisse): It has a default implementation so that mock
165 // objects, in particular, chrome's MockWebRtcVideoTrack, doesn't
166 // need to know about it. Consider removing the implementation (or
167 // this comment) after refactoring dust settles.
168 virtual rtc::VideoSinkInterface<cricket::VideoFrame>* GetSink() {
169 return nullptr;
170 };
171
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000172 protected:
173 virtual ~VideoTrackInterface() {}
174};
175
tommi6eca7e32015-12-15 04:27:11 -0800176// Interface for receiving audio data from a AudioTrack.
177class AudioTrackSinkInterface {
178 public:
179 virtual void OnData(const void* audio_data,
180 int bits_per_sample,
181 int sample_rate,
Peter Kasting69558702016-01-12 16:26:35 -0800182 size_t number_of_channels,
tommi6eca7e32015-12-15 04:27:11 -0800183 size_t number_of_frames) = 0;
184
185 protected:
186 virtual ~AudioTrackSinkInterface() {}
187};
188
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000189// AudioSourceInterface is a reference counted source used for AudioTracks.
190// The same source can be used in multiple AudioTracks.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000191class AudioSourceInterface : public MediaSourceInterface {
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000192 public:
193 class AudioObserver {
194 public:
195 virtual void OnSetVolume(double volume) = 0;
196
197 protected:
198 virtual ~AudioObserver() {}
199 };
200
201 // TODO(xians): Makes all the interface pure virtual after Chrome has their
202 // implementations.
203 // Sets the volume to the source. |volume| is in the range of [0, 10].
Tommif888bb52015-12-12 01:37:01 +0100204 // TODO(tommi): This method should be on the track and ideally volume should
205 // be applied in the track in a way that does not affect clones of the track.
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000206 virtual void SetVolume(double volume) {}
207
208 // Registers/unregisters observer to the audio source.
209 virtual void RegisterAudioObserver(AudioObserver* observer) {}
210 virtual void UnregisterAudioObserver(AudioObserver* observer) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000211
tommi6eca7e32015-12-15 04:27:11 -0800212 // TODO(tommi): Make pure virtual.
213 virtual void AddSink(AudioTrackSinkInterface* sink) {}
214 virtual void RemoveSink(AudioTrackSinkInterface* sink) {}
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000215};
216
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000217// Interface of the audio processor used by the audio track to collect
218// statistics.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000219class AudioProcessorInterface : public rtc::RefCountInterface {
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000220 public:
221 struct AudioProcessorStats {
222 AudioProcessorStats() : typing_noise_detected(false),
223 echo_return_loss(0),
224 echo_return_loss_enhancement(0),
225 echo_delay_median_ms(0),
226 aec_quality_min(0.0),
227 echo_delay_std_ms(0) {}
228 ~AudioProcessorStats() {}
229
230 bool typing_noise_detected;
231 int echo_return_loss;
232 int echo_return_loss_enhancement;
233 int echo_delay_median_ms;
234 float aec_quality_min;
235 int echo_delay_std_ms;
236 };
237
238 // Get audio processor statistics.
239 virtual void GetStats(AudioProcessorStats* stats) = 0;
240
241 protected:
242 virtual ~AudioProcessorInterface() {}
243};
244
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000245class AudioTrackInterface : public MediaStreamTrackInterface {
246 public:
247 // TODO(xians): Figure out if the following interface should be const or not.
248 virtual AudioSourceInterface* GetSource() const = 0;
249
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000250 // Add/Remove a sink that will receive the audio data from the track.
251 virtual void AddSink(AudioTrackSinkInterface* sink) = 0;
252 virtual void RemoveSink(AudioTrackSinkInterface* sink) = 0;
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000253
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000254 // Get the signal level from the audio track.
255 // Return true on success, otherwise false.
256 // TODO(xians): Change the interface to int GetSignalLevel() and pure virtual
257 // after Chrome has the correct implementation of the interface.
258 virtual bool GetSignalLevel(int* level) { return false; }
259
260 // Get the audio processor used by the audio track. Return NULL if the track
261 // does not have any processor.
262 // TODO(xians): Make the interface pure virtual.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000263 virtual rtc::scoped_refptr<AudioProcessorInterface>
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000264 GetAudioProcessor() { return NULL; }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000265
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000266 protected:
267 virtual ~AudioTrackInterface() {}
268};
269
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000270typedef std::vector<rtc::scoped_refptr<AudioTrackInterface> >
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000271 AudioTrackVector;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000272typedef std::vector<rtc::scoped_refptr<VideoTrackInterface> >
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000273 VideoTrackVector;
274
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000275class MediaStreamInterface : public rtc::RefCountInterface,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000276 public NotifierInterface {
277 public:
278 virtual std::string label() const = 0;
279
280 virtual AudioTrackVector GetAudioTracks() = 0;
281 virtual VideoTrackVector GetVideoTracks() = 0;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000282 virtual rtc::scoped_refptr<AudioTrackInterface>
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000283 FindAudioTrack(const std::string& track_id) = 0;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000284 virtual rtc::scoped_refptr<VideoTrackInterface>
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000285 FindVideoTrack(const std::string& track_id) = 0;
286
287 virtual bool AddTrack(AudioTrackInterface* track) = 0;
288 virtual bool AddTrack(VideoTrackInterface* track) = 0;
289 virtual bool RemoveTrack(AudioTrackInterface* track) = 0;
290 virtual bool RemoveTrack(VideoTrackInterface* track) = 0;
291
292 protected:
293 virtual ~MediaStreamInterface() {}
294};
295
296} // namespace webrtc
297
Henrik Kjellander15583c12016-02-10 10:53:12 +0100298#endif // WEBRTC_API_MEDIASTREAMINTERFACE_H_