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/base/thread.h"
46 // TrackHandler listen to events on a MediaStreamTrackInterface that is
47 // connected to a certain PeerConnection.
48 class TrackHandler : public ObserverInterface {
50 TrackHandler(MediaStreamTrackInterface* track, uint32 ssrc);
51 virtual ~TrackHandler();
52 virtual void OnChanged();
53 // Stop using |track_| on this PeerConnection.
54 virtual void Stop() = 0;
56 MediaStreamTrackInterface* track() { return track_; }
57 uint32 ssrc() const { return ssrc_; }
60 virtual void OnStateChanged() = 0;
61 virtual void OnEnabledChanged() = 0;
64 talk_base::scoped_refptr<MediaStreamTrackInterface> track_;
66 MediaStreamTrackInterface::TrackState state_;
70 // LocalAudioTrackHandler listen to events on a local AudioTrack instance
71 // connected to a PeerConnection and orders the |provider| to executes the
73 class LocalAudioTrackHandler : public TrackHandler {
75 LocalAudioTrackHandler(AudioTrackInterface* track,
77 AudioProviderInterface* provider);
78 virtual ~LocalAudioTrackHandler();
80 virtual void Stop() OVERRIDE;
83 virtual void OnStateChanged() OVERRIDE;
84 virtual void OnEnabledChanged() OVERRIDE;
87 AudioTrackInterface* audio_track_;
88 AudioProviderInterface* provider_;
91 // RemoteAudioTrackHandler listen to events on a remote AudioTrack instance
92 // connected to a PeerConnection and orders the |provider| to executes the
94 class RemoteAudioTrackHandler : public TrackHandler {
96 RemoteAudioTrackHandler(AudioTrackInterface* track,
98 AudioProviderInterface* provider);
99 virtual ~RemoteAudioTrackHandler();
100 virtual void Stop() OVERRIDE;
103 virtual void OnStateChanged() OVERRIDE;
104 virtual void OnEnabledChanged() OVERRIDE;
107 AudioTrackInterface* audio_track_;
108 AudioProviderInterface* provider_;
111 // LocalVideoTrackHandler listen to events on a local VideoTrack instance
112 // connected to a PeerConnection and orders the |provider| to executes the
114 class LocalVideoTrackHandler : public TrackHandler {
116 LocalVideoTrackHandler(VideoTrackInterface* track,
118 VideoProviderInterface* provider);
119 virtual ~LocalVideoTrackHandler();
120 virtual void Stop() OVERRIDE;
123 virtual void OnStateChanged() OVERRIDE;
124 virtual void OnEnabledChanged() OVERRIDE;
127 VideoTrackInterface* local_video_track_;
128 VideoProviderInterface* provider_;
131 // RemoteVideoTrackHandler listen to events on a remote VideoTrack instance
132 // connected to a PeerConnection and orders the |provider| to execute
133 // requested changes.
134 class RemoteVideoTrackHandler : public TrackHandler {
136 RemoteVideoTrackHandler(VideoTrackInterface* track,
138 VideoProviderInterface* provider);
139 virtual ~RemoteVideoTrackHandler();
140 virtual void Stop() OVERRIDE;
143 virtual void OnStateChanged() OVERRIDE;
144 virtual void OnEnabledChanged() OVERRIDE;
147 VideoTrackInterface* remote_video_track_;
148 VideoProviderInterface* provider_;
151 class MediaStreamHandler : public ObserverInterface {
153 MediaStreamHandler(MediaStreamInterface* stream,
154 AudioProviderInterface* audio_provider,
155 VideoProviderInterface* video_provider);
156 ~MediaStreamHandler();
157 MediaStreamInterface* stream();
160 virtual void AddAudioTrack(AudioTrackInterface* audio_track, uint32 ssrc) = 0;
161 virtual void AddVideoTrack(VideoTrackInterface* video_track, uint32 ssrc) = 0;
163 virtual void RemoveTrack(MediaStreamTrackInterface* track);
164 virtual void OnChanged() OVERRIDE;
167 TrackHandler* FindTrackHandler(MediaStreamTrackInterface* track);
168 talk_base::scoped_refptr<MediaStreamInterface> stream_;
169 AudioProviderInterface* audio_provider_;
170 VideoProviderInterface* video_provider_;
171 typedef std::vector<TrackHandler*> TrackHandlers;
172 TrackHandlers track_handlers_;
175 class LocalMediaStreamHandler : public MediaStreamHandler {
177 LocalMediaStreamHandler(MediaStreamInterface* stream,
178 AudioProviderInterface* audio_provider,
179 VideoProviderInterface* video_provider);
180 ~LocalMediaStreamHandler();
182 virtual void AddAudioTrack(AudioTrackInterface* audio_track,
183 uint32 ssrc) OVERRIDE;
184 virtual void AddVideoTrack(VideoTrackInterface* video_track,
185 uint32 ssrc) OVERRIDE;
188 class RemoteMediaStreamHandler : public MediaStreamHandler {
190 RemoteMediaStreamHandler(MediaStreamInterface* stream,
191 AudioProviderInterface* audio_provider,
192 VideoProviderInterface* video_provider);
193 ~RemoteMediaStreamHandler();
194 virtual void AddAudioTrack(AudioTrackInterface* audio_track,
195 uint32 ssrc) OVERRIDE;
196 virtual void AddVideoTrack(VideoTrackInterface* video_track,
197 uint32 ssrc) OVERRIDE;
200 // Container for MediaStreamHandlers of currently known local and remote
202 class MediaStreamHandlerContainer {
204 MediaStreamHandlerContainer(AudioProviderInterface* audio_provider,
205 VideoProviderInterface* video_provider);
206 ~MediaStreamHandlerContainer();
208 // Notify all referenced objects that MediaStreamHandlerContainer will be
209 // destroyed. This method must be called prior to the dtor and prior to the
210 // |audio_provider| and |video_provider| is destroyed.
213 // Remove all TrackHandlers for tracks in |stream| and make sure
214 // the audio_provider and video_provider is notified that the tracks has been
216 void RemoveRemoteStream(MediaStreamInterface* stream);
218 // Create a RemoteAudioTrackHandler and associate |audio_track| with |ssrc|.
219 void AddRemoteAudioTrack(MediaStreamInterface* stream,
220 AudioTrackInterface* audio_track,
222 // Create a RemoteVideoTrackHandler and associate |video_track| with |ssrc|.
223 void AddRemoteVideoTrack(MediaStreamInterface* stream,
224 VideoTrackInterface* video_track,
226 // Remove the TrackHandler for |track|.
227 void RemoveRemoteTrack(MediaStreamInterface* stream,
228 MediaStreamTrackInterface* track);
230 // Remove all TrackHandlers for tracks in |stream| and make sure
231 // the audio_provider and video_provider is notified that the tracks has been
233 void RemoveLocalStream(MediaStreamInterface* stream);
235 // Create a LocalAudioTrackHandler and associate |audio_track| with |ssrc|.
236 void AddLocalAudioTrack(MediaStreamInterface* stream,
237 AudioTrackInterface* audio_track,
239 // Create a LocalVideoTrackHandler and associate |video_track| with |ssrc|.
240 void AddLocalVideoTrack(MediaStreamInterface* stream,
241 VideoTrackInterface* video_track,
243 // Remove the TrackHandler for |track|.
244 void RemoveLocalTrack(MediaStreamInterface* stream,
245 MediaStreamTrackInterface* track);
248 typedef std::list<MediaStreamHandler*> StreamHandlerList;
249 MediaStreamHandler* FindStreamHandler(const StreamHandlerList& handlers,
250 MediaStreamInterface* stream);
251 MediaStreamHandler* CreateRemoteStreamHandler(MediaStreamInterface* stream);
252 MediaStreamHandler* CreateLocalStreamHandler(MediaStreamInterface* stream);
253 void DeleteStreamHandler(StreamHandlerList* streamhandlers,
254 MediaStreamInterface* stream);
256 StreamHandlerList local_streams_handlers_;
257 StreamHandlerList remote_streams_handlers_;
258 AudioProviderInterface* audio_provider_;
259 VideoProviderInterface* video_provider_;
262 } // namespace webrtc
264 #endif // TALK_APP_WEBRTC_MEDIASTREAMHANDLER_H_