henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame^] | 2 | * Copyright 2012 The WebRTC project authors. All Rights Reserved. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3 | * |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame^] | 4 | * 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.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 9 | */ |
| 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 Kjellander | 15583c1 | 2016-02-10 10:53:12 +0100 | [diff] [blame] | 17 | #ifndef WEBRTC_API_MEDIASTREAMINTERFACE_H_ |
| 18 | #define WEBRTC_API_MEDIASTREAMINTERFACE_H_ |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 19 | |
| 20 | #include <string> |
| 21 | #include <vector> |
| 22 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 23 | #include "webrtc/base/basictypes.h" |
| 24 | #include "webrtc/base/refcount.h" |
| 25 | #include "webrtc/base/scoped_ref_ptr.h" |
nisse | e73afba | 2016-01-28 04:47:08 -0800 | [diff] [blame] | 26 | #include "webrtc/media/base/videosinkinterface.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 27 | |
| 28 | namespace cricket { |
| 29 | |
| 30 | class AudioRenderer; |
| 31 | class VideoCapturer; |
| 32 | class VideoRenderer; |
| 33 | class VideoFrame; |
| 34 | |
| 35 | } // namespace cricket |
| 36 | |
| 37 | namespace webrtc { |
| 38 | |
| 39 | // Generic observer interface. |
| 40 | class ObserverInterface { |
| 41 | public: |
| 42 | virtual void OnChanged() = 0; |
| 43 | |
| 44 | protected: |
| 45 | virtual ~ObserverInterface() {} |
| 46 | }; |
| 47 | |
| 48 | class 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.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 58 | class MediaSourceInterface : public rtc::RefCountInterface, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 59 | public NotifierInterface { |
| 60 | public: |
| 61 | enum SourceState { |
| 62 | kInitializing, |
| 63 | kLive, |
| 64 | kEnded, |
| 65 | kMuted |
| 66 | }; |
| 67 | |
| 68 | virtual SourceState state() const = 0; |
| 69 | |
tommi | 6eca7e3 | 2015-12-15 04:27:11 -0800 | [diff] [blame] | 70 | virtual bool remote() const = 0; |
| 71 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 72 | protected: |
| 73 | virtual ~MediaSourceInterface() {} |
| 74 | }; |
| 75 | |
| 76 | // Information about a track. |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 77 | class MediaStreamTrackInterface : public rtc::RefCountInterface, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 78 | 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 | |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 87 | static const char kAudioKind[]; |
| 88 | static const char kVideoKind[]; |
| 89 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 90 | 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.org | 32001ef | 2013-08-12 23:26:21 +0000 | [diff] [blame] | 97 | |
| 98 | protected: |
| 99 | virtual ~MediaStreamTrackInterface() {} |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 100 | }; |
| 101 | |
| 102 | // Interface for rendering VideoFrames from a VideoTrack |
nisse | e73afba | 2016-01-28 04:47:08 -0800 | [diff] [blame] | 103 | class VideoRendererInterface |
| 104 | : public rtc::VideoSinkInterface<cricket::VideoFrame> { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 105 | public: |
guoweis@webrtc.org | 00c509a | 2015-03-12 21:37:26 +0000 | [diff] [blame] | 106 | // |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.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 109 | virtual void RenderFrame(const cricket::VideoFrame* frame) = 0; |
nisse | e73afba | 2016-01-28 04:47:08 -0800 | [diff] [blame] | 110 | // Intended to replace RenderFrame. |
| 111 | void OnFrame(const cricket::VideoFrame& frame) override { |
| 112 | RenderFrame(&frame); |
| 113 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 114 | |
| 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 | |
| 122 | class VideoSourceInterface; |
| 123 | |
| 124 | class 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.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 131 | virtual VideoSourceInterface* GetSource() const = 0; |
| 132 | |
nisse | 8e8908a | 2016-02-05 01:52:15 -0800 | [diff] [blame] | 133 | // 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.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 153 | protected: |
| 154 | virtual ~VideoTrackInterface() {} |
| 155 | }; |
| 156 | |
tommi | 6eca7e3 | 2015-12-15 04:27:11 -0800 | [diff] [blame] | 157 | // Interface for receiving audio data from a AudioTrack. |
| 158 | class AudioTrackSinkInterface { |
| 159 | public: |
| 160 | virtual void OnData(const void* audio_data, |
| 161 | int bits_per_sample, |
| 162 | int sample_rate, |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 163 | size_t number_of_channels, |
tommi | 6eca7e3 | 2015-12-15 04:27:11 -0800 | [diff] [blame] | 164 | size_t number_of_frames) = 0; |
| 165 | |
| 166 | protected: |
| 167 | virtual ~AudioTrackSinkInterface() {} |
| 168 | }; |
| 169 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 170 | // AudioSourceInterface is a reference counted source used for AudioTracks. |
| 171 | // The same source can be used in multiple AudioTracks. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 172 | class AudioSourceInterface : public MediaSourceInterface { |
wu@webrtc.org | b9a088b | 2014-02-13 23:18:49 +0000 | [diff] [blame] | 173 | 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]. |
Tommi | f888bb5 | 2015-12-12 01:37:01 +0100 | [diff] [blame] | 185 | // 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.org | b9a088b | 2014-02-13 23:18:49 +0000 | [diff] [blame] | 187 | 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.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 192 | |
tommi | 6eca7e3 | 2015-12-15 04:27:11 -0800 | [diff] [blame] | 193 | // TODO(tommi): Make pure virtual. |
| 194 | virtual void AddSink(AudioTrackSinkInterface* sink) {} |
| 195 | virtual void RemoveSink(AudioTrackSinkInterface* sink) {} |
mallinath@webrtc.org | 67ee6b9 | 2014-02-03 16:57:16 +0000 | [diff] [blame] | 196 | }; |
| 197 | |
henrike@webrtc.org | 40b3b68 | 2014-03-03 18:30:11 +0000 | [diff] [blame] | 198 | // Interface of the audio processor used by the audio track to collect |
| 199 | // statistics. |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 200 | class AudioProcessorInterface : public rtc::RefCountInterface { |
henrike@webrtc.org | 40b3b68 | 2014-03-03 18:30:11 +0000 | [diff] [blame] | 201 | 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.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 226 | class 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.org | 40b3b68 | 2014-03-03 18:30:11 +0000 | [diff] [blame] | 231 | // 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.org | 67ee6b9 | 2014-02-03 16:57:16 +0000 | [diff] [blame] | 234 | |
henrike@webrtc.org | 40b3b68 | 2014-03-03 18:30:11 +0000 | [diff] [blame] | 235 | // 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.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 244 | virtual rtc::scoped_refptr<AudioProcessorInterface> |
henrike@webrtc.org | b90991d | 2014-03-04 19:54:57 +0000 | [diff] [blame] | 245 | GetAudioProcessor() { return NULL; } |
henrike@webrtc.org | 40b3b68 | 2014-03-03 18:30:11 +0000 | [diff] [blame] | 246 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 247 | protected: |
| 248 | virtual ~AudioTrackInterface() {} |
| 249 | }; |
| 250 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 251 | typedef std::vector<rtc::scoped_refptr<AudioTrackInterface> > |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 252 | AudioTrackVector; |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 253 | typedef std::vector<rtc::scoped_refptr<VideoTrackInterface> > |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 254 | VideoTrackVector; |
| 255 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 256 | class MediaStreamInterface : public rtc::RefCountInterface, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 257 | public NotifierInterface { |
| 258 | public: |
| 259 | virtual std::string label() const = 0; |
| 260 | |
| 261 | virtual AudioTrackVector GetAudioTracks() = 0; |
| 262 | virtual VideoTrackVector GetVideoTracks() = 0; |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 263 | virtual rtc::scoped_refptr<AudioTrackInterface> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 264 | FindAudioTrack(const std::string& track_id) = 0; |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 265 | virtual rtc::scoped_refptr<VideoTrackInterface> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 266 | 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 Kjellander | 15583c1 | 2016-02-10 10:53:12 +0100 | [diff] [blame] | 279 | #endif // WEBRTC_API_MEDIASTREAMINTERFACE_H_ |