blob: 4de06488a79e6ffd172d7d3d2d984f4565b46094 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
2 * libjingle
jlmiller@webrtc.org5f93d0a2015-01-20 21:36:13 +00003 * Copyright 2012 Google Inc.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00004 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28// This file contains interfaces for MediaStream, MediaTrack and MediaSource.
29// These interfaces are used for implementing MediaStream and MediaTrack as
30// defined in http://dev.w3.org/2011/webrtc/editor/webrtc.html#stream-api. These
31// interfaces must be used only with PeerConnection. PeerConnectionManager
32// interface provides the factory methods to create MediaStream and MediaTracks.
33
34#ifndef TALK_APP_WEBRTC_MEDIASTREAMINTERFACE_H_
35#define TALK_APP_WEBRTC_MEDIASTREAMINTERFACE_H_
36
37#include <string>
38#include <vector>
39
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000040#include "webrtc/base/basictypes.h"
41#include "webrtc/base/refcount.h"
42#include "webrtc/base/scoped_ref_ptr.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000043
44namespace cricket {
45
46class AudioRenderer;
47class VideoCapturer;
48class VideoRenderer;
49class VideoFrame;
50
51} // namespace cricket
52
53namespace webrtc {
54
55// Generic observer interface.
56class ObserverInterface {
57 public:
58 virtual void OnChanged() = 0;
59
60 protected:
61 virtual ~ObserverInterface() {}
62};
63
64class NotifierInterface {
65 public:
66 virtual void RegisterObserver(ObserverInterface* observer) = 0;
67 virtual void UnregisterObserver(ObserverInterface* observer) = 0;
68
69 virtual ~NotifierInterface() {}
70};
71
72// Base class for sources. A MediaStreamTrack have an underlying source that
73// provide media. A source can be shared with multiple tracks.
74// TODO(perkj): Implement sources for local and remote audio tracks and
75// remote video tracks.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000076class MediaSourceInterface : public rtc::RefCountInterface,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000077 public NotifierInterface {
78 public:
79 enum SourceState {
80 kInitializing,
81 kLive,
82 kEnded,
83 kMuted
84 };
85
86 virtual SourceState state() const = 0;
87
88 protected:
89 virtual ~MediaSourceInterface() {}
90};
91
92// Information about a track.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000093class MediaStreamTrackInterface : public rtc::RefCountInterface,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000094 public NotifierInterface {
95 public:
96 enum TrackState {
97 kInitializing, // Track is beeing negotiated.
98 kLive = 1, // Track alive
99 kEnded = 2, // Track have ended
100 kFailed = 3, // Track negotiation failed.
101 };
102
103 virtual std::string kind() const = 0;
104 virtual std::string id() const = 0;
105 virtual bool enabled() const = 0;
106 virtual TrackState state() const = 0;
107 virtual bool set_enabled(bool enable) = 0;
108 // These methods should be called by implementation only.
109 virtual bool set_state(TrackState new_state) = 0;
fischman@webrtc.org32001ef2013-08-12 23:26:21 +0000110
111 protected:
112 virtual ~MediaStreamTrackInterface() {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000113};
114
115// Interface for rendering VideoFrames from a VideoTrack
116class VideoRendererInterface {
117 public:
guoweis@webrtc.org00c509a2015-03-12 21:37:26 +0000118 // TODO(guoweis): Remove this function. Obsolete. The implementation of
119 // VideoRendererInterface should be able to handle different frame size as
120 // well as pending rotation. If it can't apply the frame rotation by itself,
121 // it should call |frame|.GetCopyWithRotationApplied() to get a frame that has
122 // the rotation applied.
123 virtual void SetSize(int width, int height) {}
124
125 // |frame| may have pending rotation. For clients which can't apply rotation,
126 // |frame|->GetCopyWithRotationApplied() will return a frame that has the
127 // rotation applied.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000128 virtual void RenderFrame(const cricket::VideoFrame* frame) = 0;
129
guoweis@webrtc.org00c509a2015-03-12 21:37:26 +0000130 // TODO(guoweis): Remove this function. This is added as a temporary solution
131 // until chrome renderers can apply rotation.
132 // Whether the VideoRenderer has the ability to rotate the frame before being
133 // displayed. The rotation of a frame is carried by
134 // VideoFrame.GetVideoRotation() and is the clockwise angle the frames must be
135 // rotated in order to display the frames correctly. If returning false, the
136 // frame's rotation must be applied before being delivered by RenderFrame.
137 virtual bool CanApplyRotation() { return false; }
138
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000139 protected:
140 // The destructor is protected to prevent deletion via the interface.
141 // This is so that we allow reference counted classes, where the destructor
142 // should never be public, to implement the interface.
143 virtual ~VideoRendererInterface() {}
144};
145
146class VideoSourceInterface;
147
148class VideoTrackInterface : public MediaStreamTrackInterface {
149 public:
150 // Register a renderer that will render all frames received on this track.
151 virtual void AddRenderer(VideoRendererInterface* renderer) = 0;
152 // Deregister a renderer.
153 virtual void RemoveRenderer(VideoRendererInterface* renderer) = 0;
154
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000155 virtual VideoSourceInterface* GetSource() const = 0;
156
157 protected:
158 virtual ~VideoTrackInterface() {}
159};
160
161// AudioSourceInterface is a reference counted source used for AudioTracks.
162// The same source can be used in multiple AudioTracks.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000163class AudioSourceInterface : public MediaSourceInterface {
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000164 public:
165 class AudioObserver {
166 public:
167 virtual void OnSetVolume(double volume) = 0;
168
169 protected:
170 virtual ~AudioObserver() {}
171 };
172
173 // TODO(xians): Makes all the interface pure virtual after Chrome has their
174 // implementations.
175 // Sets the volume to the source. |volume| is in the range of [0, 10].
176 virtual void SetVolume(double volume) {}
177
178 // Registers/unregisters observer to the audio source.
179 virtual void RegisterAudioObserver(AudioObserver* observer) {}
180 virtual void UnregisterAudioObserver(AudioObserver* observer) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000181};
182
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000183// Interface for receiving audio data from a AudioTrack.
184class AudioTrackSinkInterface {
185 public:
186 virtual void OnData(const void* audio_data,
187 int bits_per_sample,
188 int sample_rate,
189 int number_of_channels,
190 int number_of_frames) = 0;
191 protected:
192 virtual ~AudioTrackSinkInterface() {}
193};
194
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000195// Interface of the audio processor used by the audio track to collect
196// statistics.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000197class AudioProcessorInterface : public rtc::RefCountInterface {
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000198 public:
199 struct AudioProcessorStats {
200 AudioProcessorStats() : typing_noise_detected(false),
201 echo_return_loss(0),
202 echo_return_loss_enhancement(0),
203 echo_delay_median_ms(0),
204 aec_quality_min(0.0),
205 echo_delay_std_ms(0) {}
206 ~AudioProcessorStats() {}
207
208 bool typing_noise_detected;
209 int echo_return_loss;
210 int echo_return_loss_enhancement;
211 int echo_delay_median_ms;
212 float aec_quality_min;
213 int echo_delay_std_ms;
214 };
215
216 // Get audio processor statistics.
217 virtual void GetStats(AudioProcessorStats* stats) = 0;
218
219 protected:
220 virtual ~AudioProcessorInterface() {}
221};
222
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000223class AudioTrackInterface : public MediaStreamTrackInterface {
224 public:
225 // TODO(xians): Figure out if the following interface should be const or not.
226 virtual AudioSourceInterface* GetSource() const = 0;
227
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000228 // Add/Remove a sink that will receive the audio data from the track.
229 virtual void AddSink(AudioTrackSinkInterface* sink) = 0;
230 virtual void RemoveSink(AudioTrackSinkInterface* sink) = 0;
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000231
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000232 // Get the signal level from the audio track.
233 // Return true on success, otherwise false.
234 // TODO(xians): Change the interface to int GetSignalLevel() and pure virtual
235 // after Chrome has the correct implementation of the interface.
236 virtual bool GetSignalLevel(int* level) { return false; }
237
238 // Get the audio processor used by the audio track. Return NULL if the track
239 // does not have any processor.
240 // TODO(xians): Make the interface pure virtual.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000241 virtual rtc::scoped_refptr<AudioProcessorInterface>
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000242 GetAudioProcessor() { return NULL; }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000243
244 // Get a pointer to the audio renderer of this AudioTrack.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000245 // The pointer is valid for the lifetime of this AudioTrack.
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000246 // TODO(xians): Remove the following interface after Chrome switches to
247 // AddSink() and RemoveSink() interfaces.
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000248 virtual cricket::AudioRenderer* GetRenderer() { return NULL; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000249
250 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
282#endif // TALK_APP_WEBRTC_MEDIASTREAMINTERFACE_H_