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 classes for listening on changes on MediaStreams and |
| 29 | // MediaTracks that are connected to a certain PeerConnection. |
| 30 | // Example: If a user sets a rendererer on a remote video track the renderer is |
| 31 | // connected to the appropriate remote video stream. |
| 32 | |
| 33 | #ifndef TALK_APP_WEBRTC_MEDIASTREAMHANDLER_H_ |
| 34 | #define TALK_APP_WEBRTC_MEDIASTREAMHANDLER_H_ |
| 35 | |
| 36 | #include <list> |
| 37 | #include <vector> |
| 38 | |
| 39 | #include "talk/app/webrtc/mediastreaminterface.h" |
| 40 | #include "talk/app/webrtc/mediastreamprovider.h" |
| 41 | #include "talk/app/webrtc/peerconnectioninterface.h" |
mallinath@webrtc.org | 67ee6b9 | 2014-02-03 16:57:16 +0000 | [diff] [blame] | 42 | #include "talk/media/base/audiorenderer.h" |
buildbot@webrtc.org | a09a999 | 2014-08-13 17:26:08 +0000 | [diff] [blame] | 43 | #include "webrtc/base/thread.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 44 | |
| 45 | namespace webrtc { |
| 46 | |
| 47 | // TrackHandler listen to events on a MediaStreamTrackInterface that is |
| 48 | // connected to a certain PeerConnection. |
| 49 | class TrackHandler : public ObserverInterface { |
| 50 | public: |
| 51 | TrackHandler(MediaStreamTrackInterface* track, uint32 ssrc); |
| 52 | virtual ~TrackHandler(); |
| 53 | virtual void OnChanged(); |
deadbeef | be37888 | 2015-07-17 10:30:44 -0700 | [diff] [blame] | 54 | // Associate |track_| with |ssrc_|. Can be called multiple times. |
| 55 | virtual void Start() = 0; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 56 | // Stop using |track_| on this PeerConnection. |
| 57 | virtual void Stop() = 0; |
| 58 | |
| 59 | MediaStreamTrackInterface* track() { return track_; } |
| 60 | uint32 ssrc() const { return ssrc_; } |
| 61 | |
| 62 | protected: |
| 63 | virtual void OnStateChanged() = 0; |
| 64 | virtual void OnEnabledChanged() = 0; |
| 65 | |
| 66 | private: |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 67 | rtc::scoped_refptr<MediaStreamTrackInterface> track_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 68 | uint32 ssrc_; |
| 69 | MediaStreamTrackInterface::TrackState state_; |
| 70 | bool enabled_; |
| 71 | }; |
| 72 | |
mallinath@webrtc.org | 67ee6b9 | 2014-02-03 16:57:16 +0000 | [diff] [blame] | 73 | // LocalAudioSinkAdapter receives data callback as a sink to the local |
| 74 | // AudioTrack, and passes the data to the sink of AudioRenderer. |
| 75 | class LocalAudioSinkAdapter : public AudioTrackSinkInterface, |
| 76 | public cricket::AudioRenderer { |
| 77 | public: |
| 78 | LocalAudioSinkAdapter(); |
| 79 | virtual ~LocalAudioSinkAdapter(); |
| 80 | |
| 81 | private: |
| 82 | // AudioSinkInterface implementation. |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 83 | void OnData(const void* audio_data, |
| 84 | int bits_per_sample, |
| 85 | int sample_rate, |
| 86 | int number_of_channels, |
| 87 | int number_of_frames) override; |
mallinath@webrtc.org | 67ee6b9 | 2014-02-03 16:57:16 +0000 | [diff] [blame] | 88 | |
| 89 | // cricket::AudioRenderer implementation. |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 90 | void SetSink(cricket::AudioRenderer::Sink* sink) override; |
mallinath@webrtc.org | 67ee6b9 | 2014-02-03 16:57:16 +0000 | [diff] [blame] | 91 | |
| 92 | cricket::AudioRenderer::Sink* sink_; |
| 93 | // Critical section protecting |sink_|. |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 94 | rtc::CriticalSection lock_; |
mallinath@webrtc.org | 67ee6b9 | 2014-02-03 16:57:16 +0000 | [diff] [blame] | 95 | }; |
| 96 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 97 | // LocalAudioTrackHandler listen to events on a local AudioTrack instance |
| 98 | // connected to a PeerConnection and orders the |provider| to executes the |
| 99 | // requested change. |
| 100 | class LocalAudioTrackHandler : public TrackHandler { |
| 101 | public: |
| 102 | LocalAudioTrackHandler(AudioTrackInterface* track, |
| 103 | uint32 ssrc, |
| 104 | AudioProviderInterface* provider); |
| 105 | virtual ~LocalAudioTrackHandler(); |
deadbeef | be37888 | 2015-07-17 10:30:44 -0700 | [diff] [blame] | 106 | void Start() override; |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 107 | void Stop() override; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 108 | |
| 109 | protected: |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 110 | void OnStateChanged() override; |
| 111 | void OnEnabledChanged() override; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 112 | |
| 113 | private: |
| 114 | AudioTrackInterface* audio_track_; |
| 115 | AudioProviderInterface* provider_; |
mallinath@webrtc.org | 67ee6b9 | 2014-02-03 16:57:16 +0000 | [diff] [blame] | 116 | |
| 117 | // Used to pass the data callback from the |audio_track_| to the other |
| 118 | // end of cricket::AudioRenderer. |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 119 | rtc::scoped_ptr<LocalAudioSinkAdapter> sink_adapter_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 120 | }; |
| 121 | |
| 122 | // RemoteAudioTrackHandler listen to events on a remote AudioTrack instance |
| 123 | // connected to a PeerConnection and orders the |provider| to executes the |
| 124 | // requested change. |
wu@webrtc.org | b9a088b | 2014-02-13 23:18:49 +0000 | [diff] [blame] | 125 | class RemoteAudioTrackHandler : public AudioSourceInterface::AudioObserver, |
| 126 | public TrackHandler { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 127 | public: |
| 128 | RemoteAudioTrackHandler(AudioTrackInterface* track, |
| 129 | uint32 ssrc, |
| 130 | AudioProviderInterface* provider); |
| 131 | virtual ~RemoteAudioTrackHandler(); |
deadbeef | be37888 | 2015-07-17 10:30:44 -0700 | [diff] [blame] | 132 | void Start() override; |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 133 | void Stop() override; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 134 | |
| 135 | protected: |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 136 | void OnStateChanged() override; |
| 137 | void OnEnabledChanged() override; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 138 | |
| 139 | private: |
wu@webrtc.org | b9a088b | 2014-02-13 23:18:49 +0000 | [diff] [blame] | 140 | // AudioSourceInterface::AudioObserver implementation. |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 141 | void OnSetVolume(double volume) override; |
wu@webrtc.org | b9a088b | 2014-02-13 23:18:49 +0000 | [diff] [blame] | 142 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 143 | AudioTrackInterface* audio_track_; |
| 144 | AudioProviderInterface* provider_; |
| 145 | }; |
| 146 | |
| 147 | // LocalVideoTrackHandler listen to events on a local VideoTrack instance |
| 148 | // connected to a PeerConnection and orders the |provider| to executes the |
| 149 | // requested change. |
| 150 | class LocalVideoTrackHandler : public TrackHandler { |
| 151 | public: |
| 152 | LocalVideoTrackHandler(VideoTrackInterface* track, |
| 153 | uint32 ssrc, |
| 154 | VideoProviderInterface* provider); |
| 155 | virtual ~LocalVideoTrackHandler(); |
deadbeef | be37888 | 2015-07-17 10:30:44 -0700 | [diff] [blame] | 156 | void Start() override; |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 157 | void Stop() override; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 158 | |
| 159 | protected: |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 160 | void OnStateChanged() override; |
| 161 | void OnEnabledChanged() override; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 162 | |
| 163 | private: |
| 164 | VideoTrackInterface* local_video_track_; |
| 165 | VideoProviderInterface* provider_; |
| 166 | }; |
| 167 | |
| 168 | // RemoteVideoTrackHandler listen to events on a remote VideoTrack instance |
| 169 | // connected to a PeerConnection and orders the |provider| to execute |
| 170 | // requested changes. |
| 171 | class RemoteVideoTrackHandler : public TrackHandler { |
| 172 | public: |
| 173 | RemoteVideoTrackHandler(VideoTrackInterface* track, |
| 174 | uint32 ssrc, |
| 175 | VideoProviderInterface* provider); |
| 176 | virtual ~RemoteVideoTrackHandler(); |
deadbeef | be37888 | 2015-07-17 10:30:44 -0700 | [diff] [blame] | 177 | void Start() override; |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 178 | void Stop() override; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 179 | |
| 180 | protected: |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 181 | void OnStateChanged() override; |
| 182 | void OnEnabledChanged() override; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 183 | |
| 184 | private: |
| 185 | VideoTrackInterface* remote_video_track_; |
| 186 | VideoProviderInterface* provider_; |
| 187 | }; |
| 188 | |
| 189 | class MediaStreamHandler : public ObserverInterface { |
| 190 | public: |
| 191 | MediaStreamHandler(MediaStreamInterface* stream, |
| 192 | AudioProviderInterface* audio_provider, |
| 193 | VideoProviderInterface* video_provider); |
| 194 | ~MediaStreamHandler(); |
| 195 | MediaStreamInterface* stream(); |
| 196 | void Stop(); |
| 197 | |
| 198 | virtual void AddAudioTrack(AudioTrackInterface* audio_track, uint32 ssrc) = 0; |
| 199 | virtual void AddVideoTrack(VideoTrackInterface* video_track, uint32 ssrc) = 0; |
deadbeef | be37888 | 2015-07-17 10:30:44 -0700 | [diff] [blame] | 200 | virtual void RestartAllTracks() = 0; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 201 | |
| 202 | virtual void RemoveTrack(MediaStreamTrackInterface* track); |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 203 | void OnChanged() override; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 204 | |
| 205 | protected: |
| 206 | TrackHandler* FindTrackHandler(MediaStreamTrackInterface* track); |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 207 | rtc::scoped_refptr<MediaStreamInterface> stream_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 208 | AudioProviderInterface* audio_provider_; |
| 209 | VideoProviderInterface* video_provider_; |
| 210 | typedef std::vector<TrackHandler*> TrackHandlers; |
| 211 | TrackHandlers track_handlers_; |
| 212 | }; |
| 213 | |
| 214 | class LocalMediaStreamHandler : public MediaStreamHandler { |
| 215 | public: |
| 216 | LocalMediaStreamHandler(MediaStreamInterface* stream, |
| 217 | AudioProviderInterface* audio_provider, |
| 218 | VideoProviderInterface* video_provider); |
| 219 | ~LocalMediaStreamHandler(); |
| 220 | |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 221 | void AddAudioTrack(AudioTrackInterface* audio_track, uint32 ssrc) override; |
| 222 | void AddVideoTrack(VideoTrackInterface* video_track, uint32 ssrc) override; |
deadbeef | be37888 | 2015-07-17 10:30:44 -0700 | [diff] [blame] | 223 | void RestartAllTracks() override; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 224 | }; |
| 225 | |
| 226 | class RemoteMediaStreamHandler : public MediaStreamHandler { |
| 227 | public: |
| 228 | RemoteMediaStreamHandler(MediaStreamInterface* stream, |
| 229 | AudioProviderInterface* audio_provider, |
| 230 | VideoProviderInterface* video_provider); |
| 231 | ~RemoteMediaStreamHandler(); |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 232 | void AddAudioTrack(AudioTrackInterface* audio_track, uint32 ssrc) override; |
| 233 | void AddVideoTrack(VideoTrackInterface* video_track, uint32 ssrc) override; |
deadbeef | be37888 | 2015-07-17 10:30:44 -0700 | [diff] [blame] | 234 | void RestartAllTracks() override; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 235 | }; |
| 236 | |
| 237 | // Container for MediaStreamHandlers of currently known local and remote |
| 238 | // MediaStreams. |
| 239 | class MediaStreamHandlerContainer { |
| 240 | public: |
| 241 | MediaStreamHandlerContainer(AudioProviderInterface* audio_provider, |
| 242 | VideoProviderInterface* video_provider); |
| 243 | ~MediaStreamHandlerContainer(); |
| 244 | |
| 245 | // Notify all referenced objects that MediaStreamHandlerContainer will be |
| 246 | // destroyed. This method must be called prior to the dtor and prior to the |
| 247 | // |audio_provider| and |video_provider| is destroyed. |
| 248 | void TearDown(); |
| 249 | |
| 250 | // Remove all TrackHandlers for tracks in |stream| and make sure |
| 251 | // the audio_provider and video_provider is notified that the tracks has been |
| 252 | // removed. |
| 253 | void RemoveRemoteStream(MediaStreamInterface* stream); |
| 254 | |
| 255 | // Create a RemoteAudioTrackHandler and associate |audio_track| with |ssrc|. |
| 256 | void AddRemoteAudioTrack(MediaStreamInterface* stream, |
| 257 | AudioTrackInterface* audio_track, |
| 258 | uint32 ssrc); |
| 259 | // Create a RemoteVideoTrackHandler and associate |video_track| with |ssrc|. |
| 260 | void AddRemoteVideoTrack(MediaStreamInterface* stream, |
| 261 | VideoTrackInterface* video_track, |
| 262 | uint32 ssrc); |
| 263 | // Remove the TrackHandler for |track|. |
| 264 | void RemoveRemoteTrack(MediaStreamInterface* stream, |
| 265 | MediaStreamTrackInterface* track); |
| 266 | |
deadbeef | be37888 | 2015-07-17 10:30:44 -0700 | [diff] [blame] | 267 | // Make all remote track handlers reassociate their corresponding tracks |
| 268 | // with their corresponding SSRCs. |
| 269 | void RestartAllRemoteTracks(); |
| 270 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 271 | // Remove all TrackHandlers for tracks in |stream| and make sure |
| 272 | // the audio_provider and video_provider is notified that the tracks has been |
| 273 | // removed. |
| 274 | void RemoveLocalStream(MediaStreamInterface* stream); |
| 275 | |
| 276 | // Create a LocalAudioTrackHandler and associate |audio_track| with |ssrc|. |
| 277 | void AddLocalAudioTrack(MediaStreamInterface* stream, |
| 278 | AudioTrackInterface* audio_track, |
| 279 | uint32 ssrc); |
| 280 | // Create a LocalVideoTrackHandler and associate |video_track| with |ssrc|. |
| 281 | void AddLocalVideoTrack(MediaStreamInterface* stream, |
| 282 | VideoTrackInterface* video_track, |
| 283 | uint32 ssrc); |
| 284 | // Remove the TrackHandler for |track|. |
| 285 | void RemoveLocalTrack(MediaStreamInterface* stream, |
| 286 | MediaStreamTrackInterface* track); |
| 287 | |
deadbeef | be37888 | 2015-07-17 10:30:44 -0700 | [diff] [blame] | 288 | // Make all local track handlers reassociate their corresponding tracks |
| 289 | // with their corresponding SSRCs. |
| 290 | void RestartAllLocalTracks(); |
| 291 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 292 | private: |
| 293 | typedef std::list<MediaStreamHandler*> StreamHandlerList; |
| 294 | MediaStreamHandler* FindStreamHandler(const StreamHandlerList& handlers, |
| 295 | MediaStreamInterface* stream); |
| 296 | MediaStreamHandler* CreateRemoteStreamHandler(MediaStreamInterface* stream); |
| 297 | MediaStreamHandler* CreateLocalStreamHandler(MediaStreamInterface* stream); |
| 298 | void DeleteStreamHandler(StreamHandlerList* streamhandlers, |
| 299 | MediaStreamInterface* stream); |
| 300 | |
| 301 | StreamHandlerList local_streams_handlers_; |
| 302 | StreamHandlerList remote_streams_handlers_; |
| 303 | AudioProviderInterface* audio_provider_; |
| 304 | VideoProviderInterface* video_provider_; |
| 305 | }; |
| 306 | |
| 307 | } // namespace webrtc |
| 308 | |
| 309 | #endif // TALK_APP_WEBRTC_MEDIASTREAMHANDLER_H_ |