henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* |
| 2 | * libjingle |
jlmiller@webrtc.org | 5f93d0a | 2015-01-20 21:36:13 +0000 | [diff] [blame] | 3 | * Copyright 2012 Google Inc. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 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 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 40 | #include "webrtc/base/basictypes.h" |
| 41 | #include "webrtc/base/refcount.h" |
| 42 | #include "webrtc/base/scoped_ref_ptr.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 43 | |
| 44 | namespace cricket { |
| 45 | |
| 46 | class AudioRenderer; |
| 47 | class VideoCapturer; |
| 48 | class VideoRenderer; |
| 49 | class VideoFrame; |
| 50 | |
| 51 | } // namespace cricket |
| 52 | |
| 53 | namespace webrtc { |
| 54 | |
| 55 | // Generic observer interface. |
| 56 | class ObserverInterface { |
| 57 | public: |
| 58 | virtual void OnChanged() = 0; |
| 59 | |
| 60 | protected: |
| 61 | virtual ~ObserverInterface() {} |
| 62 | }; |
| 63 | |
| 64 | class 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. |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 74 | class MediaSourceInterface : public rtc::RefCountInterface, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 75 | public NotifierInterface { |
| 76 | public: |
| 77 | enum SourceState { |
| 78 | kInitializing, |
| 79 | kLive, |
| 80 | kEnded, |
| 81 | kMuted |
| 82 | }; |
| 83 | |
| 84 | virtual SourceState state() const = 0; |
| 85 | |
tommi | 6eca7e3 | 2015-12-15 04:27:11 -0800 | [diff] [blame] | 86 | virtual bool remote() const = 0; |
| 87 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 88 | protected: |
| 89 | virtual ~MediaSourceInterface() {} |
| 90 | }; |
| 91 | |
| 92 | // Information about a track. |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 93 | class MediaStreamTrackInterface : public rtc::RefCountInterface, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 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 | |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 103 | static const char kAudioKind[]; |
| 104 | static const char kVideoKind[]; |
| 105 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 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; |
fischman@webrtc.org | 32001ef | 2013-08-12 23:26:21 +0000 | [diff] [blame] | 113 | |
| 114 | protected: |
| 115 | virtual ~MediaStreamTrackInterface() {} |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 116 | }; |
| 117 | |
| 118 | // Interface for rendering VideoFrames from a VideoTrack |
nisse | 2098fca | 2016-01-27 06:12:49 -0800 | [diff] [blame] | 119 | class VideoRendererInterface { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 120 | public: |
guoweis@webrtc.org | 00c509a | 2015-03-12 21:37:26 +0000 | [diff] [blame] | 121 | // |frame| may have pending rotation. For clients which can't apply rotation, |
| 122 | // |frame|->GetCopyWithRotationApplied() will return a frame that has the |
| 123 | // rotation applied. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 124 | virtual void RenderFrame(const cricket::VideoFrame* frame) = 0; |
| 125 | |
| 126 | protected: |
| 127 | // The destructor is protected to prevent deletion via the interface. |
| 128 | // This is so that we allow reference counted classes, where the destructor |
| 129 | // should never be public, to implement the interface. |
| 130 | virtual ~VideoRendererInterface() {} |
| 131 | }; |
| 132 | |
| 133 | class VideoSourceInterface; |
| 134 | |
| 135 | class VideoTrackInterface : public MediaStreamTrackInterface { |
| 136 | public: |
| 137 | // Register a renderer that will render all frames received on this track. |
| 138 | virtual void AddRenderer(VideoRendererInterface* renderer) = 0; |
| 139 | // Deregister a renderer. |
| 140 | virtual void RemoveRenderer(VideoRendererInterface* renderer) = 0; |
| 141 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 142 | virtual VideoSourceInterface* GetSource() const = 0; |
| 143 | |
| 144 | protected: |
| 145 | virtual ~VideoTrackInterface() {} |
| 146 | }; |
| 147 | |
tommi | 6eca7e3 | 2015-12-15 04:27:11 -0800 | [diff] [blame] | 148 | // Interface for receiving audio data from a AudioTrack. |
| 149 | class AudioTrackSinkInterface { |
| 150 | public: |
| 151 | virtual void OnData(const void* audio_data, |
| 152 | int bits_per_sample, |
| 153 | int sample_rate, |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 154 | size_t number_of_channels, |
tommi | 6eca7e3 | 2015-12-15 04:27:11 -0800 | [diff] [blame] | 155 | size_t number_of_frames) = 0; |
| 156 | |
| 157 | protected: |
| 158 | virtual ~AudioTrackSinkInterface() {} |
| 159 | }; |
| 160 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 161 | // AudioSourceInterface is a reference counted source used for AudioTracks. |
| 162 | // The same source can be used in multiple AudioTracks. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 163 | class AudioSourceInterface : public MediaSourceInterface { |
wu@webrtc.org | b9a088b | 2014-02-13 23:18:49 +0000 | [diff] [blame] | 164 | 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]. |
Tommi | f888bb5 | 2015-12-12 01:37:01 +0100 | [diff] [blame] | 176 | // TODO(tommi): This method should be on the track and ideally volume should |
| 177 | // be applied in the track in a way that does not affect clones of the track. |
wu@webrtc.org | b9a088b | 2014-02-13 23:18:49 +0000 | [diff] [blame] | 178 | virtual void SetVolume(double volume) {} |
| 179 | |
| 180 | // Registers/unregisters observer to the audio source. |
| 181 | virtual void RegisterAudioObserver(AudioObserver* observer) {} |
| 182 | virtual void UnregisterAudioObserver(AudioObserver* observer) {} |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 183 | |
tommi | 6eca7e3 | 2015-12-15 04:27:11 -0800 | [diff] [blame] | 184 | // TODO(tommi): Make pure virtual. |
| 185 | virtual void AddSink(AudioTrackSinkInterface* sink) {} |
| 186 | virtual void RemoveSink(AudioTrackSinkInterface* sink) {} |
mallinath@webrtc.org | 67ee6b9 | 2014-02-03 16:57:16 +0000 | [diff] [blame] | 187 | }; |
| 188 | |
henrike@webrtc.org | 40b3b68 | 2014-03-03 18:30:11 +0000 | [diff] [blame] | 189 | // Interface of the audio processor used by the audio track to collect |
| 190 | // statistics. |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 191 | class AudioProcessorInterface : public rtc::RefCountInterface { |
henrike@webrtc.org | 40b3b68 | 2014-03-03 18:30:11 +0000 | [diff] [blame] | 192 | public: |
| 193 | struct AudioProcessorStats { |
| 194 | AudioProcessorStats() : typing_noise_detected(false), |
| 195 | echo_return_loss(0), |
| 196 | echo_return_loss_enhancement(0), |
| 197 | echo_delay_median_ms(0), |
| 198 | aec_quality_min(0.0), |
| 199 | echo_delay_std_ms(0) {} |
| 200 | ~AudioProcessorStats() {} |
| 201 | |
| 202 | bool typing_noise_detected; |
| 203 | int echo_return_loss; |
| 204 | int echo_return_loss_enhancement; |
| 205 | int echo_delay_median_ms; |
| 206 | float aec_quality_min; |
| 207 | int echo_delay_std_ms; |
| 208 | }; |
| 209 | |
| 210 | // Get audio processor statistics. |
| 211 | virtual void GetStats(AudioProcessorStats* stats) = 0; |
| 212 | |
| 213 | protected: |
| 214 | virtual ~AudioProcessorInterface() {} |
| 215 | }; |
| 216 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 217 | class AudioTrackInterface : public MediaStreamTrackInterface { |
| 218 | public: |
| 219 | // TODO(xians): Figure out if the following interface should be const or not. |
| 220 | virtual AudioSourceInterface* GetSource() const = 0; |
| 221 | |
henrike@webrtc.org | 40b3b68 | 2014-03-03 18:30:11 +0000 | [diff] [blame] | 222 | // Add/Remove a sink that will receive the audio data from the track. |
| 223 | virtual void AddSink(AudioTrackSinkInterface* sink) = 0; |
| 224 | virtual void RemoveSink(AudioTrackSinkInterface* sink) = 0; |
mallinath@webrtc.org | 67ee6b9 | 2014-02-03 16:57:16 +0000 | [diff] [blame] | 225 | |
henrike@webrtc.org | 40b3b68 | 2014-03-03 18:30:11 +0000 | [diff] [blame] | 226 | // Get the signal level from the audio track. |
| 227 | // Return true on success, otherwise false. |
| 228 | // TODO(xians): Change the interface to int GetSignalLevel() and pure virtual |
| 229 | // after Chrome has the correct implementation of the interface. |
| 230 | virtual bool GetSignalLevel(int* level) { return false; } |
| 231 | |
| 232 | // Get the audio processor used by the audio track. Return NULL if the track |
| 233 | // does not have any processor. |
| 234 | // TODO(xians): Make the interface pure virtual. |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 235 | virtual rtc::scoped_refptr<AudioProcessorInterface> |
henrike@webrtc.org | b90991d | 2014-03-04 19:54:57 +0000 | [diff] [blame] | 236 | GetAudioProcessor() { return NULL; } |
henrike@webrtc.org | 40b3b68 | 2014-03-03 18:30:11 +0000 | [diff] [blame] | 237 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 238 | protected: |
| 239 | virtual ~AudioTrackInterface() {} |
| 240 | }; |
| 241 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 242 | typedef std::vector<rtc::scoped_refptr<AudioTrackInterface> > |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 243 | AudioTrackVector; |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 244 | typedef std::vector<rtc::scoped_refptr<VideoTrackInterface> > |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 245 | VideoTrackVector; |
| 246 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 247 | class MediaStreamInterface : public rtc::RefCountInterface, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 248 | public NotifierInterface { |
| 249 | public: |
| 250 | virtual std::string label() const = 0; |
| 251 | |
| 252 | virtual AudioTrackVector GetAudioTracks() = 0; |
| 253 | virtual VideoTrackVector GetVideoTracks() = 0; |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 254 | virtual rtc::scoped_refptr<AudioTrackInterface> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 255 | FindAudioTrack(const std::string& track_id) = 0; |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 256 | virtual rtc::scoped_refptr<VideoTrackInterface> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 257 | FindVideoTrack(const std::string& track_id) = 0; |
| 258 | |
| 259 | virtual bool AddTrack(AudioTrackInterface* track) = 0; |
| 260 | virtual bool AddTrack(VideoTrackInterface* track) = 0; |
| 261 | virtual bool RemoveTrack(AudioTrackInterface* track) = 0; |
| 262 | virtual bool RemoveTrack(VideoTrackInterface* track) = 0; |
| 263 | |
| 264 | protected: |
| 265 | virtual ~MediaStreamInterface() {} |
| 266 | }; |
| 267 | |
| 268 | } // namespace webrtc |
| 269 | |
| 270 | #endif // TALK_APP_WEBRTC_MEDIASTREAMINTERFACE_H_ |