blob: 8d6feb02ef268a8115985d517e13cd0a40a018a3 [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"
nissee73afba2016-01-28 04:47:08 -080043#include "webrtc/media/base/videosinkinterface.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000044
45namespace cricket {
46
47class AudioRenderer;
48class VideoCapturer;
49class VideoRenderer;
50class VideoFrame;
51
52} // namespace cricket
53
54namespace webrtc {
55
56// Generic observer interface.
57class ObserverInterface {
58 public:
59 virtual void OnChanged() = 0;
60
61 protected:
62 virtual ~ObserverInterface() {}
63};
64
65class NotifierInterface {
66 public:
67 virtual void RegisterObserver(ObserverInterface* observer) = 0;
68 virtual void UnregisterObserver(ObserverInterface* observer) = 0;
69
70 virtual ~NotifierInterface() {}
71};
72
73// Base class for sources. A MediaStreamTrack have an underlying source that
74// provide media. A source can be shared with multiple tracks.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000075class MediaSourceInterface : public rtc::RefCountInterface,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000076 public NotifierInterface {
77 public:
78 enum SourceState {
79 kInitializing,
80 kLive,
81 kEnded,
82 kMuted
83 };
84
85 virtual SourceState state() const = 0;
86
tommi6eca7e32015-12-15 04:27:11 -080087 virtual bool remote() const = 0;
88
henrike@webrtc.org28e20752013-07-10 00:45:36 +000089 protected:
90 virtual ~MediaSourceInterface() {}
91};
92
93// Information about a track.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000094class MediaStreamTrackInterface : public rtc::RefCountInterface,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000095 public NotifierInterface {
96 public:
97 enum TrackState {
98 kInitializing, // Track is beeing negotiated.
99 kLive = 1, // Track alive
100 kEnded = 2, // Track have ended
101 kFailed = 3, // Track negotiation failed.
102 };
103
deadbeeffac06552015-11-25 11:26:01 -0800104 static const char kAudioKind[];
105 static const char kVideoKind[];
106
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000107 virtual std::string kind() const = 0;
108 virtual std::string id() const = 0;
109 virtual bool enabled() const = 0;
110 virtual TrackState state() const = 0;
111 virtual bool set_enabled(bool enable) = 0;
112 // These methods should be called by implementation only.
113 virtual bool set_state(TrackState new_state) = 0;
fischman@webrtc.org32001ef2013-08-12 23:26:21 +0000114
115 protected:
116 virtual ~MediaStreamTrackInterface() {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000117};
118
119// Interface for rendering VideoFrames from a VideoTrack
nissee73afba2016-01-28 04:47:08 -0800120class VideoRendererInterface
121 : public rtc::VideoSinkInterface<cricket::VideoFrame> {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000122 public:
guoweis@webrtc.org00c509a2015-03-12 21:37:26 +0000123 // |frame| may have pending rotation. For clients which can't apply rotation,
124 // |frame|->GetCopyWithRotationApplied() will return a frame that has the
125 // rotation applied.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000126 virtual void RenderFrame(const cricket::VideoFrame* frame) = 0;
nissee73afba2016-01-28 04:47:08 -0800127 // Intended to replace RenderFrame.
128 void OnFrame(const cricket::VideoFrame& frame) override {
129 RenderFrame(&frame);
130 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000131
132 protected:
133 // The destructor is protected to prevent deletion via the interface.
134 // This is so that we allow reference counted classes, where the destructor
135 // should never be public, to implement the interface.
136 virtual ~VideoRendererInterface() {}
137};
138
139class VideoSourceInterface;
140
141class VideoTrackInterface : public MediaStreamTrackInterface {
142 public:
143 // Register a renderer that will render all frames received on this track.
144 virtual void AddRenderer(VideoRendererInterface* renderer) = 0;
145 // Deregister a renderer.
146 virtual void RemoveRenderer(VideoRendererInterface* renderer) = 0;
147
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000148 virtual VideoSourceInterface* GetSource() const = 0;
149
150 protected:
151 virtual ~VideoTrackInterface() {}
152};
153
tommi6eca7e32015-12-15 04:27:11 -0800154// Interface for receiving audio data from a AudioTrack.
155class AudioTrackSinkInterface {
156 public:
157 virtual void OnData(const void* audio_data,
158 int bits_per_sample,
159 int sample_rate,
Peter Kasting69558702016-01-12 16:26:35 -0800160 size_t number_of_channels,
tommi6eca7e32015-12-15 04:27:11 -0800161 size_t number_of_frames) = 0;
162
163 protected:
164 virtual ~AudioTrackSinkInterface() {}
165};
166
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000167// AudioSourceInterface is a reference counted source used for AudioTracks.
168// The same source can be used in multiple AudioTracks.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000169class AudioSourceInterface : public MediaSourceInterface {
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000170 public:
171 class AudioObserver {
172 public:
173 virtual void OnSetVolume(double volume) = 0;
174
175 protected:
176 virtual ~AudioObserver() {}
177 };
178
179 // TODO(xians): Makes all the interface pure virtual after Chrome has their
180 // implementations.
181 // Sets the volume to the source. |volume| is in the range of [0, 10].
Tommif888bb52015-12-12 01:37:01 +0100182 // TODO(tommi): This method should be on the track and ideally volume should
183 // be applied in the track in a way that does not affect clones of the track.
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000184 virtual void SetVolume(double volume) {}
185
186 // Registers/unregisters observer to the audio source.
187 virtual void RegisterAudioObserver(AudioObserver* observer) {}
188 virtual void UnregisterAudioObserver(AudioObserver* observer) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000189
tommi6eca7e32015-12-15 04:27:11 -0800190 // TODO(tommi): Make pure virtual.
191 virtual void AddSink(AudioTrackSinkInterface* sink) {}
192 virtual void RemoveSink(AudioTrackSinkInterface* sink) {}
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000193};
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
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000244 protected:
245 virtual ~AudioTrackInterface() {}
246};
247
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000248typedef std::vector<rtc::scoped_refptr<AudioTrackInterface> >
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000249 AudioTrackVector;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000250typedef std::vector<rtc::scoped_refptr<VideoTrackInterface> >
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000251 VideoTrackVector;
252
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000253class MediaStreamInterface : public rtc::RefCountInterface,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000254 public NotifierInterface {
255 public:
256 virtual std::string label() const = 0;
257
258 virtual AudioTrackVector GetAudioTracks() = 0;
259 virtual VideoTrackVector GetVideoTracks() = 0;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000260 virtual rtc::scoped_refptr<AudioTrackInterface>
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000261 FindAudioTrack(const std::string& track_id) = 0;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000262 virtual rtc::scoped_refptr<VideoTrackInterface>
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000263 FindVideoTrack(const std::string& track_id) = 0;
264
265 virtual bool AddTrack(AudioTrackInterface* track) = 0;
266 virtual bool AddTrack(VideoTrackInterface* track) = 0;
267 virtual bool RemoveTrack(AudioTrackInterface* track) = 0;
268 virtual bool RemoveTrack(VideoTrackInterface* track) = 0;
269
270 protected:
271 virtual ~MediaStreamInterface() {}
272};
273
274} // namespace webrtc
275
276#endif // TALK_APP_WEBRTC_MEDIASTREAMINTERFACE_H_