blob: d4fe2bf0eb228f0a3323d9f2742faf2ea2fd388b [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"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000027
28namespace cricket {
29
30class AudioRenderer;
31class VideoCapturer;
32class VideoRenderer;
33class VideoFrame;
34
35} // namespace cricket
36
37namespace webrtc {
38
39// Generic observer interface.
40class ObserverInterface {
41 public:
42 virtual void OnChanged() = 0;
43
44 protected:
45 virtual ~ObserverInterface() {}
46};
47
48class NotifierInterface {
49 public:
50 virtual void RegisterObserver(ObserverInterface* observer) = 0;
51 virtual void UnregisterObserver(ObserverInterface* observer) = 0;
52
53 virtual ~NotifierInterface() {}
54};
55
56// Base class for sources. A MediaStreamTrack have an underlying source that
57// provide media. A source can be shared with multiple tracks.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000058class MediaSourceInterface : public rtc::RefCountInterface,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000059 public NotifierInterface {
60 public:
61 enum SourceState {
62 kInitializing,
63 kLive,
64 kEnded,
65 kMuted
66 };
67
68 virtual SourceState state() const = 0;
69
tommi6eca7e32015-12-15 04:27:11 -080070 virtual bool remote() const = 0;
71
henrike@webrtc.org28e20752013-07-10 00:45:36 +000072 protected:
73 virtual ~MediaSourceInterface() {}
74};
75
76// Information about a track.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000077class MediaStreamTrackInterface : public rtc::RefCountInterface,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000078 public NotifierInterface {
79 public:
80 enum TrackState {
81 kInitializing, // Track is beeing negotiated.
82 kLive = 1, // Track alive
83 kEnded = 2, // Track have ended
84 kFailed = 3, // Track negotiation failed.
85 };
86
deadbeeffac06552015-11-25 11:26:01 -080087 static const char kAudioKind[];
88 static const char kVideoKind[];
89
henrike@webrtc.org28e20752013-07-10 00:45:36 +000090 virtual std::string kind() const = 0;
91 virtual std::string id() const = 0;
92 virtual bool enabled() const = 0;
93 virtual TrackState state() const = 0;
94 virtual bool set_enabled(bool enable) = 0;
95 // These methods should be called by implementation only.
96 virtual bool set_state(TrackState new_state) = 0;
fischman@webrtc.org32001ef2013-08-12 23:26:21 +000097
98 protected:
99 virtual ~MediaStreamTrackInterface() {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000100};
101
102// Interface for rendering VideoFrames from a VideoTrack
nissee73afba2016-01-28 04:47:08 -0800103class VideoRendererInterface
104 : public rtc::VideoSinkInterface<cricket::VideoFrame> {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000105 public:
guoweis@webrtc.org00c509a2015-03-12 21:37:26 +0000106 // |frame| may have pending rotation. For clients which can't apply rotation,
107 // |frame|->GetCopyWithRotationApplied() will return a frame that has the
108 // rotation applied.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000109 virtual void RenderFrame(const cricket::VideoFrame* frame) = 0;
nissee73afba2016-01-28 04:47:08 -0800110 // Intended to replace RenderFrame.
111 void OnFrame(const cricket::VideoFrame& frame) override {
112 RenderFrame(&frame);
113 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000114
115 protected:
116 // The destructor is protected to prevent deletion via the interface.
117 // This is so that we allow reference counted classes, where the destructor
118 // should never be public, to implement the interface.
119 virtual ~VideoRendererInterface() {}
120};
121
122class VideoSourceInterface;
123
124class VideoTrackInterface : public MediaStreamTrackInterface {
125 public:
126 // Register a renderer that will render all frames received on this track.
127 virtual void AddRenderer(VideoRendererInterface* renderer) = 0;
128 // Deregister a renderer.
129 virtual void RemoveRenderer(VideoRendererInterface* renderer) = 0;
130
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000131 virtual VideoSourceInterface* GetSource() const = 0;
132
nisse8e8908a2016-02-05 01:52:15 -0800133 // Return the track input sink. I.e., frames sent to this sink are
134 // propagated to all renderers registered with the track. The
135 // returned sink must not change between calls. Currently, this
136 // method is used for remote tracks (VideoRtpReceiver); further
137 // refactoring is planned for this path, it's unclear if this method
138 // belongs here long term.
139
140 // We do this instead of simply implementing the
141 // VideoSourceInterface directly, because if we did the latter, we'd
142 // need an OnFrame method in VideoTrackProxy, with a thread jump on
143 // each call.
144
145 // TODO(nisse): It has a default implementation so that mock
146 // objects, in particular, chrome's MockWebRtcVideoTrack, doesn't
147 // need to know about it. Consider removing the implementation (or
148 // this comment) after refactoring dust settles.
149 virtual rtc::VideoSinkInterface<cricket::VideoFrame>* GetSink() {
150 return nullptr;
151 };
152
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000153 protected:
154 virtual ~VideoTrackInterface() {}
155};
156
tommi6eca7e32015-12-15 04:27:11 -0800157// Interface for receiving audio data from a AudioTrack.
158class AudioTrackSinkInterface {
159 public:
160 virtual void OnData(const void* audio_data,
161 int bits_per_sample,
162 int sample_rate,
Peter Kasting69558702016-01-12 16:26:35 -0800163 size_t number_of_channels,
tommi6eca7e32015-12-15 04:27:11 -0800164 size_t number_of_frames) = 0;
165
166 protected:
167 virtual ~AudioTrackSinkInterface() {}
168};
169
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000170// AudioSourceInterface is a reference counted source used for AudioTracks.
171// The same source can be used in multiple AudioTracks.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000172class AudioSourceInterface : public MediaSourceInterface {
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000173 public:
174 class AudioObserver {
175 public:
176 virtual void OnSetVolume(double volume) = 0;
177
178 protected:
179 virtual ~AudioObserver() {}
180 };
181
182 // TODO(xians): Makes all the interface pure virtual after Chrome has their
183 // implementations.
184 // Sets the volume to the source. |volume| is in the range of [0, 10].
Tommif888bb52015-12-12 01:37:01 +0100185 // TODO(tommi): This method should be on the track and ideally volume should
186 // be applied in the track in a way that does not affect clones of the track.
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000187 virtual void SetVolume(double volume) {}
188
189 // Registers/unregisters observer to the audio source.
190 virtual void RegisterAudioObserver(AudioObserver* observer) {}
191 virtual void UnregisterAudioObserver(AudioObserver* observer) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000192
tommi6eca7e32015-12-15 04:27:11 -0800193 // TODO(tommi): Make pure virtual.
194 virtual void AddSink(AudioTrackSinkInterface* sink) {}
195 virtual void RemoveSink(AudioTrackSinkInterface* sink) {}
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000196};
197
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000198// Interface of the audio processor used by the audio track to collect
199// statistics.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000200class AudioProcessorInterface : public rtc::RefCountInterface {
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000201 public:
202 struct AudioProcessorStats {
203 AudioProcessorStats() : typing_noise_detected(false),
204 echo_return_loss(0),
205 echo_return_loss_enhancement(0),
206 echo_delay_median_ms(0),
207 aec_quality_min(0.0),
208 echo_delay_std_ms(0) {}
209 ~AudioProcessorStats() {}
210
211 bool typing_noise_detected;
212 int echo_return_loss;
213 int echo_return_loss_enhancement;
214 int echo_delay_median_ms;
215 float aec_quality_min;
216 int echo_delay_std_ms;
217 };
218
219 // Get audio processor statistics.
220 virtual void GetStats(AudioProcessorStats* stats) = 0;
221
222 protected:
223 virtual ~AudioProcessorInterface() {}
224};
225
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000226class AudioTrackInterface : public MediaStreamTrackInterface {
227 public:
228 // TODO(xians): Figure out if the following interface should be const or not.
229 virtual AudioSourceInterface* GetSource() const = 0;
230
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000231 // Add/Remove a sink that will receive the audio data from the track.
232 virtual void AddSink(AudioTrackSinkInterface* sink) = 0;
233 virtual void RemoveSink(AudioTrackSinkInterface* sink) = 0;
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000234
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000235 // Get the signal level from the audio track.
236 // Return true on success, otherwise false.
237 // TODO(xians): Change the interface to int GetSignalLevel() and pure virtual
238 // after Chrome has the correct implementation of the interface.
239 virtual bool GetSignalLevel(int* level) { return false; }
240
241 // Get the audio processor used by the audio track. Return NULL if the track
242 // does not have any processor.
243 // TODO(xians): Make the interface pure virtual.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000244 virtual rtc::scoped_refptr<AudioProcessorInterface>
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000245 GetAudioProcessor() { return NULL; }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000246
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000247 protected:
248 virtual ~AudioTrackInterface() {}
249};
250
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000251typedef std::vector<rtc::scoped_refptr<AudioTrackInterface> >
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000252 AudioTrackVector;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000253typedef std::vector<rtc::scoped_refptr<VideoTrackInterface> >
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000254 VideoTrackVector;
255
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000256class MediaStreamInterface : public rtc::RefCountInterface,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000257 public NotifierInterface {
258 public:
259 virtual std::string label() const = 0;
260
261 virtual AudioTrackVector GetAudioTracks() = 0;
262 virtual VideoTrackVector GetVideoTracks() = 0;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000263 virtual rtc::scoped_refptr<AudioTrackInterface>
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000264 FindAudioTrack(const std::string& track_id) = 0;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000265 virtual rtc::scoped_refptr<VideoTrackInterface>
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000266 FindVideoTrack(const std::string& track_id) = 0;
267
268 virtual bool AddTrack(AudioTrackInterface* track) = 0;
269 virtual bool AddTrack(VideoTrackInterface* track) = 0;
270 virtual bool RemoveTrack(AudioTrackInterface* track) = 0;
271 virtual bool RemoveTrack(VideoTrackInterface* track) = 0;
272
273 protected:
274 virtual ~MediaStreamInterface() {}
275};
276
277} // namespace webrtc
278
Henrik Kjellander15583c12016-02-10 10:53:12 +0100279#endif // WEBRTC_API_MEDIASTREAMINTERFACE_H_