mediastreaminterface.h revision f888bb58da04c5095759b5ec7ce2e1fa2cd414fd
1/*
2 * libjingle
3 * Copyright 2012 Google Inc.
4 *
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
40#include "webrtc/base/basictypes.h"
41#include "webrtc/base/refcount.h"
42#include "webrtc/base/scoped_ref_ptr.h"
43
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.
76class MediaSourceInterface : public rtc::RefCountInterface,
77                             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.
93class MediaStreamTrackInterface : public rtc::RefCountInterface,
94                                  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  static const char kAudioKind[];
104  static const char kVideoKind[];
105
106  virtual std::string kind() const = 0;
107  virtual std::string id() const = 0;
108  virtual bool enabled() const = 0;
109  virtual TrackState state() const = 0;
110  virtual bool set_enabled(bool enable) = 0;
111  // These methods should be called by implementation only.
112  virtual bool set_state(TrackState new_state) = 0;
113
114 protected:
115  virtual ~MediaStreamTrackInterface() {}
116};
117
118// Interface for rendering VideoFrames from a VideoTrack
119class VideoRendererInterface {
120 public:
121  // TODO(guoweis): Remove this function.  Obsolete. The implementation of
122  // VideoRendererInterface should be able to handle different frame size as
123  // well as pending rotation. If it can't apply the frame rotation by itself,
124  // it should call |frame|.GetCopyWithRotationApplied() to get a frame that has
125  // the rotation applied.
126  virtual void SetSize(int width, int height) {}
127
128  // |frame| may have pending rotation. For clients which can't apply rotation,
129  // |frame|->GetCopyWithRotationApplied() will return a frame that has the
130  // rotation applied.
131  virtual void RenderFrame(const cricket::VideoFrame* frame) = 0;
132
133 protected:
134  // The destructor is protected to prevent deletion via the interface.
135  // This is so that we allow reference counted classes, where the destructor
136  // should never be public, to implement the interface.
137  virtual ~VideoRendererInterface() {}
138};
139
140class VideoSourceInterface;
141
142class VideoTrackInterface : public MediaStreamTrackInterface {
143 public:
144  // Register a renderer that will render all frames received on this track.
145  virtual void AddRenderer(VideoRendererInterface* renderer) = 0;
146  // Deregister a renderer.
147  virtual void RemoveRenderer(VideoRendererInterface* renderer) = 0;
148
149  virtual VideoSourceInterface* GetSource() const = 0;
150
151 protected:
152  virtual ~VideoTrackInterface() {}
153};
154
155// AudioSourceInterface is a reference counted source used for AudioTracks.
156// The same source can be used in multiple AudioTracks.
157class AudioSourceInterface : public MediaSourceInterface {
158 public:
159  class AudioObserver {
160   public:
161    virtual void OnSetVolume(double volume) = 0;
162
163   protected:
164    virtual ~AudioObserver() {}
165  };
166
167  // TODO(xians): Makes all the interface pure virtual after Chrome has their
168  // implementations.
169  // Sets the volume to the source. |volume| is in  the range of [0, 10].
170  // TODO(tommi): This method should be on the track and ideally volume should
171  // be applied in the track in a way that does not affect clones of the track.
172  virtual void SetVolume(double volume) {}
173
174  // Registers/unregisters observer to the audio source.
175  virtual void RegisterAudioObserver(AudioObserver* observer) {}
176  virtual void UnregisterAudioObserver(AudioObserver* observer) {}
177};
178
179// Interface for receiving audio data from a AudioTrack.
180class AudioTrackSinkInterface {
181 public:
182  virtual void OnData(const void* audio_data,
183                      int bits_per_sample,
184                      int sample_rate,
185                      int number_of_channels,
186                      size_t number_of_frames) = 0;
187 protected:
188  virtual ~AudioTrackSinkInterface() {}
189};
190
191// Interface of the audio processor used by the audio track to collect
192// statistics.
193class AudioProcessorInterface : public rtc::RefCountInterface {
194 public:
195  struct AudioProcessorStats {
196    AudioProcessorStats() : typing_noise_detected(false),
197                            echo_return_loss(0),
198                            echo_return_loss_enhancement(0),
199                            echo_delay_median_ms(0),
200                            aec_quality_min(0.0),
201                            echo_delay_std_ms(0) {}
202    ~AudioProcessorStats() {}
203
204    bool typing_noise_detected;
205    int echo_return_loss;
206    int echo_return_loss_enhancement;
207    int echo_delay_median_ms;
208    float aec_quality_min;
209    int echo_delay_std_ms;
210  };
211
212  // Get audio processor statistics.
213  virtual void GetStats(AudioProcessorStats* stats) = 0;
214
215 protected:
216  virtual ~AudioProcessorInterface() {}
217};
218
219class AudioTrackInterface : public MediaStreamTrackInterface {
220 public:
221  // TODO(xians): Figure out if the following interface should be const or not.
222  virtual AudioSourceInterface* GetSource() const =  0;
223
224  // Add/Remove a sink that will receive the audio data from the track.
225  virtual void AddSink(AudioTrackSinkInterface* sink) = 0;
226  virtual void RemoveSink(AudioTrackSinkInterface* sink) = 0;
227
228  // Get the signal level from the audio track.
229  // Return true on success, otherwise false.
230  // TODO(xians): Change the interface to int GetSignalLevel() and pure virtual
231  // after Chrome has the correct implementation of the interface.
232  virtual bool GetSignalLevel(int* level) { return false; }
233
234  // Get the audio processor used by the audio track. Return NULL if the track
235  // does not have any processor.
236  // TODO(xians): Make the interface pure virtual.
237  virtual rtc::scoped_refptr<AudioProcessorInterface>
238      GetAudioProcessor() { return NULL; }
239
240  // Get a pointer to the audio renderer of this AudioTrack.
241  // The pointer is valid for the lifetime of this AudioTrack.
242  // TODO(xians): Remove the following interface after Chrome switches to
243  // AddSink() and RemoveSink() interfaces.
244  virtual cricket::AudioRenderer* GetRenderer() { return NULL; }
245
246 protected:
247  virtual ~AudioTrackInterface() {}
248};
249
250typedef std::vector<rtc::scoped_refptr<AudioTrackInterface> >
251    AudioTrackVector;
252typedef std::vector<rtc::scoped_refptr<VideoTrackInterface> >
253    VideoTrackVector;
254
255class MediaStreamInterface : public rtc::RefCountInterface,
256                             public NotifierInterface {
257 public:
258  virtual std::string label() const = 0;
259
260  virtual AudioTrackVector GetAudioTracks() = 0;
261  virtual VideoTrackVector GetVideoTracks() = 0;
262  virtual rtc::scoped_refptr<AudioTrackInterface>
263      FindAudioTrack(const std::string& track_id) = 0;
264  virtual rtc::scoped_refptr<VideoTrackInterface>
265      FindVideoTrack(const std::string& track_id) = 0;
266
267  virtual bool AddTrack(AudioTrackInterface* track) = 0;
268  virtual bool AddTrack(VideoTrackInterface* track) = 0;
269  virtual bool RemoveTrack(AudioTrackInterface* track) = 0;
270  virtual bool RemoveTrack(VideoTrackInterface* track) = 0;
271
272 protected:
273  virtual ~MediaStreamInterface() {}
274};
275
276}  // namespace webrtc
277
278#endif  // TALK_APP_WEBRTC_MEDIASTREAMINTERFACE_H_
279