3 * Copyright 2012, Google Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
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.
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.
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.
33 #ifndef TALK_APP_WEBRTC_MEDIASTREAMHANDLER_H_
34 #define TALK_APP_WEBRTC_MEDIASTREAMHANDLER_H_
39 #include "talk/app/webrtc/mediastreaminterface.h"
40 #include "talk/app/webrtc/mediastreamprovider.h"
41 #include "talk/app/webrtc/peerconnectioninterface.h"
42 #include "talk/media/base/audiorenderer.h"
43 #include "webrtc/base/thread.h"
47 // TrackHandler listen to events on a MediaStreamTrackInterface that is
48 // connected to a certain PeerConnection.
49 class TrackHandler : public ObserverInterface {
51 TrackHandler(MediaStreamTrackInterface* track, uint32 ssrc);
52 virtual ~TrackHandler();
53 virtual void OnChanged();
54 // Stop using |track_| on this PeerConnection.
55 virtual void Stop() = 0;
57 MediaStreamTrackInterface* track() { return track_; }
58 uint32 ssrc() const { return ssrc_; }
61 virtual void OnStateChanged() = 0;
62 virtual void OnEnabledChanged() = 0;
65 rtc::scoped_refptr<MediaStreamTrackInterface> track_;
67 MediaStreamTrackInterface::TrackState state_;
71 // LocalAudioSinkAdapter receives data callback as a sink to the local
72 // AudioTrack, and passes the data to the sink of AudioRenderer.
73 class LocalAudioSinkAdapter : public AudioTrackSinkInterface,
74 public cricket::AudioRenderer {
76 LocalAudioSinkAdapter();
77 virtual ~LocalAudioSinkAdapter();
80 // AudioSinkInterface implementation.
81 virtual void OnData(const void* audio_data, int bits_per_sample,
82 int sample_rate, int number_of_channels,
83 int number_of_frames) OVERRIDE;
85 // cricket::AudioRenderer implementation.
86 virtual void SetSink(cricket::AudioRenderer::Sink* sink) OVERRIDE;
88 cricket::AudioRenderer::Sink* sink_;
89 // Critical section protecting |sink_|.
90 rtc::CriticalSection lock_;
93 // LocalAudioTrackHandler listen to events on a local AudioTrack instance
94 // connected to a PeerConnection and orders the |provider| to executes the
96 class LocalAudioTrackHandler : public TrackHandler {
98 LocalAudioTrackHandler(AudioTrackInterface* track,
100 AudioProviderInterface* provider);
101 virtual ~LocalAudioTrackHandler();
103 virtual void Stop() OVERRIDE;
106 virtual void OnStateChanged() OVERRIDE;
107 virtual void OnEnabledChanged() OVERRIDE;
110 AudioTrackInterface* audio_track_;
111 AudioProviderInterface* provider_;
113 // Used to pass the data callback from the |audio_track_| to the other
114 // end of cricket::AudioRenderer.
115 rtc::scoped_ptr<LocalAudioSinkAdapter> sink_adapter_;
118 // RemoteAudioTrackHandler listen to events on a remote AudioTrack instance
119 // connected to a PeerConnection and orders the |provider| to executes the
121 class RemoteAudioTrackHandler : public AudioSourceInterface::AudioObserver,
122 public TrackHandler {
124 RemoteAudioTrackHandler(AudioTrackInterface* track,
126 AudioProviderInterface* provider);
127 virtual ~RemoteAudioTrackHandler();
128 virtual void Stop() OVERRIDE;
131 virtual void OnStateChanged() OVERRIDE;
132 virtual void OnEnabledChanged() OVERRIDE;
135 // AudioSourceInterface::AudioObserver implementation.
136 virtual void OnSetVolume(double volume) OVERRIDE;
138 AudioTrackInterface* audio_track_;
139 AudioProviderInterface* provider_;
142 // LocalVideoTrackHandler listen to events on a local VideoTrack instance
143 // connected to a PeerConnection and orders the |provider| to executes the
145 class LocalVideoTrackHandler : public TrackHandler {
147 LocalVideoTrackHandler(VideoTrackInterface* track,
149 VideoProviderInterface* provider);
150 virtual ~LocalVideoTrackHandler();
151 virtual void Stop() OVERRIDE;
154 virtual void OnStateChanged() OVERRIDE;
155 virtual void OnEnabledChanged() OVERRIDE;
158 VideoTrackInterface* local_video_track_;
159 VideoProviderInterface* provider_;
162 // RemoteVideoTrackHandler listen to events on a remote VideoTrack instance
163 // connected to a PeerConnection and orders the |provider| to execute
164 // requested changes.
165 class RemoteVideoTrackHandler : public TrackHandler {
167 RemoteVideoTrackHandler(VideoTrackInterface* track,
169 VideoProviderInterface* provider);
170 virtual ~RemoteVideoTrackHandler();
171 virtual void Stop() OVERRIDE;
174 virtual void OnStateChanged() OVERRIDE;
175 virtual void OnEnabledChanged() OVERRIDE;
178 VideoTrackInterface* remote_video_track_;
179 VideoProviderInterface* provider_;
182 class MediaStreamHandler : public ObserverInterface {
184 MediaStreamHandler(MediaStreamInterface* stream,
185 AudioProviderInterface* audio_provider,
186 VideoProviderInterface* video_provider);
187 ~MediaStreamHandler();
188 MediaStreamInterface* stream();
191 virtual void AddAudioTrack(AudioTrackInterface* audio_track, uint32 ssrc) = 0;
192 virtual void AddVideoTrack(VideoTrackInterface* video_track, uint32 ssrc) = 0;
194 virtual void RemoveTrack(MediaStreamTrackInterface* track);
195 virtual void OnChanged() OVERRIDE;
198 TrackHandler* FindTrackHandler(MediaStreamTrackInterface* track);
199 rtc::scoped_refptr<MediaStreamInterface> stream_;
200 AudioProviderInterface* audio_provider_;
201 VideoProviderInterface* video_provider_;
202 typedef std::vector<TrackHandler*> TrackHandlers;
203 TrackHandlers track_handlers_;
206 class LocalMediaStreamHandler : public MediaStreamHandler {
208 LocalMediaStreamHandler(MediaStreamInterface* stream,
209 AudioProviderInterface* audio_provider,
210 VideoProviderInterface* video_provider);
211 ~LocalMediaStreamHandler();
213 virtual void AddAudioTrack(AudioTrackInterface* audio_track,
214 uint32 ssrc) OVERRIDE;
215 virtual void AddVideoTrack(VideoTrackInterface* video_track,
216 uint32 ssrc) OVERRIDE;
219 class RemoteMediaStreamHandler : public MediaStreamHandler {
221 RemoteMediaStreamHandler(MediaStreamInterface* stream,
222 AudioProviderInterface* audio_provider,
223 VideoProviderInterface* video_provider);
224 ~RemoteMediaStreamHandler();
225 virtual void AddAudioTrack(AudioTrackInterface* audio_track,
226 uint32 ssrc) OVERRIDE;
227 virtual void AddVideoTrack(VideoTrackInterface* video_track,
228 uint32 ssrc) OVERRIDE;
231 // Container for MediaStreamHandlers of currently known local and remote
233 class MediaStreamHandlerContainer {
235 MediaStreamHandlerContainer(AudioProviderInterface* audio_provider,
236 VideoProviderInterface* video_provider);
237 ~MediaStreamHandlerContainer();
239 // Notify all referenced objects that MediaStreamHandlerContainer will be
240 // destroyed. This method must be called prior to the dtor and prior to the
241 // |audio_provider| and |video_provider| is destroyed.
244 // Remove all TrackHandlers for tracks in |stream| and make sure
245 // the audio_provider and video_provider is notified that the tracks has been
247 void RemoveRemoteStream(MediaStreamInterface* stream);
249 // Create a RemoteAudioTrackHandler and associate |audio_track| with |ssrc|.
250 void AddRemoteAudioTrack(MediaStreamInterface* stream,
251 AudioTrackInterface* audio_track,
253 // Create a RemoteVideoTrackHandler and associate |video_track| with |ssrc|.
254 void AddRemoteVideoTrack(MediaStreamInterface* stream,
255 VideoTrackInterface* video_track,
257 // Remove the TrackHandler for |track|.
258 void RemoveRemoteTrack(MediaStreamInterface* stream,
259 MediaStreamTrackInterface* track);
261 // Remove all TrackHandlers for tracks in |stream| and make sure
262 // the audio_provider and video_provider is notified that the tracks has been
264 void RemoveLocalStream(MediaStreamInterface* stream);
266 // Create a LocalAudioTrackHandler and associate |audio_track| with |ssrc|.
267 void AddLocalAudioTrack(MediaStreamInterface* stream,
268 AudioTrackInterface* audio_track,
270 // Create a LocalVideoTrackHandler and associate |video_track| with |ssrc|.
271 void AddLocalVideoTrack(MediaStreamInterface* stream,
272 VideoTrackInterface* video_track,
274 // Remove the TrackHandler for |track|.
275 void RemoveLocalTrack(MediaStreamInterface* stream,
276 MediaStreamTrackInterface* track);
279 typedef std::list<MediaStreamHandler*> StreamHandlerList;
280 MediaStreamHandler* FindStreamHandler(const StreamHandlerList& handlers,
281 MediaStreamInterface* stream);
282 MediaStreamHandler* CreateRemoteStreamHandler(MediaStreamInterface* stream);
283 MediaStreamHandler* CreateLocalStreamHandler(MediaStreamInterface* stream);
284 void DeleteStreamHandler(StreamHandlerList* streamhandlers,
285 MediaStreamInterface* stream);
287 StreamHandlerList local_streams_handlers_;
288 StreamHandlerList remote_streams_handlers_;
289 AudioProviderInterface* audio_provider_;
290 VideoProviderInterface* video_provider_;
293 } // namespace webrtc
295 #endif // TALK_APP_WEBRTC_MEDIASTREAMHANDLER_H_