blob: 625de85019d7c68d47c0cdd69f2dc3791ce84308 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
2 * libjingle
3 * Copyright 2012, Google Inc.
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"
42#include "talk/base/thread.h"
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +000043#include "talk/media/base/audiorenderer.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000044
45namespace webrtc {
46
47// TrackHandler listen to events on a MediaStreamTrackInterface that is
48// connected to a certain PeerConnection.
49class TrackHandler : public ObserverInterface {
50 public:
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;
56
57 MediaStreamTrackInterface* track() { return track_; }
58 uint32 ssrc() const { return ssrc_; }
59
60 protected:
61 virtual void OnStateChanged() = 0;
62 virtual void OnEnabledChanged() = 0;
63
64 private:
65 talk_base::scoped_refptr<MediaStreamTrackInterface> track_;
66 uint32 ssrc_;
67 MediaStreamTrackInterface::TrackState state_;
68 bool enabled_;
69};
70
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +000071// LocalAudioSinkAdapter receives data callback as a sink to the local
72// AudioTrack, and passes the data to the sink of AudioRenderer.
73class LocalAudioSinkAdapter : public AudioTrackSinkInterface,
74 public cricket::AudioRenderer {
75 public:
76 LocalAudioSinkAdapter();
77 virtual ~LocalAudioSinkAdapter();
78
79 private:
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;
84
85 // cricket::AudioRenderer implementation.
86 virtual void SetSink(cricket::AudioRenderer::Sink* sink) OVERRIDE;
87
88 cricket::AudioRenderer::Sink* sink_;
89 // Critical section protecting |sink_|.
90 talk_base::CriticalSection lock_;
91};
92
henrike@webrtc.org28e20752013-07-10 00:45:36 +000093// LocalAudioTrackHandler listen to events on a local AudioTrack instance
94// connected to a PeerConnection and orders the |provider| to executes the
95// requested change.
96class LocalAudioTrackHandler : public TrackHandler {
97 public:
98 LocalAudioTrackHandler(AudioTrackInterface* track,
99 uint32 ssrc,
100 AudioProviderInterface* provider);
101 virtual ~LocalAudioTrackHandler();
102
103 virtual void Stop() OVERRIDE;
104
105 protected:
106 virtual void OnStateChanged() OVERRIDE;
107 virtual void OnEnabledChanged() OVERRIDE;
108
109 private:
110 AudioTrackInterface* audio_track_;
111 AudioProviderInterface* provider_;
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000112
113 // Used to pass the data callback from the |audio_track_| to the other
114 // end of cricket::AudioRenderer.
115 talk_base::scoped_ptr<LocalAudioSinkAdapter> sink_adapter_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000116};
117
118// RemoteAudioTrackHandler listen to events on a remote AudioTrack instance
119// connected to a PeerConnection and orders the |provider| to executes the
120// requested change.
121class RemoteAudioTrackHandler : public TrackHandler {
122 public:
123 RemoteAudioTrackHandler(AudioTrackInterface* track,
124 uint32 ssrc,
125 AudioProviderInterface* provider);
126 virtual ~RemoteAudioTrackHandler();
127 virtual void Stop() OVERRIDE;
128
129 protected:
130 virtual void OnStateChanged() OVERRIDE;
131 virtual void OnEnabledChanged() OVERRIDE;
132
133 private:
134 AudioTrackInterface* audio_track_;
135 AudioProviderInterface* provider_;
136};
137
138// LocalVideoTrackHandler listen to events on a local VideoTrack instance
139// connected to a PeerConnection and orders the |provider| to executes the
140// requested change.
141class LocalVideoTrackHandler : public TrackHandler {
142 public:
143 LocalVideoTrackHandler(VideoTrackInterface* track,
144 uint32 ssrc,
145 VideoProviderInterface* provider);
146 virtual ~LocalVideoTrackHandler();
147 virtual void Stop() OVERRIDE;
148
149 protected:
150 virtual void OnStateChanged() OVERRIDE;
151 virtual void OnEnabledChanged() OVERRIDE;
152
153 private:
154 VideoTrackInterface* local_video_track_;
155 VideoProviderInterface* provider_;
156};
157
158// RemoteVideoTrackHandler listen to events on a remote VideoTrack instance
159// connected to a PeerConnection and orders the |provider| to execute
160// requested changes.
161class RemoteVideoTrackHandler : public TrackHandler {
162 public:
163 RemoteVideoTrackHandler(VideoTrackInterface* track,
164 uint32 ssrc,
165 VideoProviderInterface* provider);
166 virtual ~RemoteVideoTrackHandler();
167 virtual void Stop() OVERRIDE;
168
169 protected:
170 virtual void OnStateChanged() OVERRIDE;
171 virtual void OnEnabledChanged() OVERRIDE;
172
173 private:
174 VideoTrackInterface* remote_video_track_;
175 VideoProviderInterface* provider_;
176};
177
178class MediaStreamHandler : public ObserverInterface {
179 public:
180 MediaStreamHandler(MediaStreamInterface* stream,
181 AudioProviderInterface* audio_provider,
182 VideoProviderInterface* video_provider);
183 ~MediaStreamHandler();
184 MediaStreamInterface* stream();
185 void Stop();
186
187 virtual void AddAudioTrack(AudioTrackInterface* audio_track, uint32 ssrc) = 0;
188 virtual void AddVideoTrack(VideoTrackInterface* video_track, uint32 ssrc) = 0;
189
190 virtual void RemoveTrack(MediaStreamTrackInterface* track);
191 virtual void OnChanged() OVERRIDE;
192
193 protected:
194 TrackHandler* FindTrackHandler(MediaStreamTrackInterface* track);
195 talk_base::scoped_refptr<MediaStreamInterface> stream_;
196 AudioProviderInterface* audio_provider_;
197 VideoProviderInterface* video_provider_;
198 typedef std::vector<TrackHandler*> TrackHandlers;
199 TrackHandlers track_handlers_;
200};
201
202class LocalMediaStreamHandler : public MediaStreamHandler {
203 public:
204 LocalMediaStreamHandler(MediaStreamInterface* stream,
205 AudioProviderInterface* audio_provider,
206 VideoProviderInterface* video_provider);
207 ~LocalMediaStreamHandler();
208
209 virtual void AddAudioTrack(AudioTrackInterface* audio_track,
210 uint32 ssrc) OVERRIDE;
211 virtual void AddVideoTrack(VideoTrackInterface* video_track,
212 uint32 ssrc) OVERRIDE;
213};
214
215class RemoteMediaStreamHandler : public MediaStreamHandler {
216 public:
217 RemoteMediaStreamHandler(MediaStreamInterface* stream,
218 AudioProviderInterface* audio_provider,
219 VideoProviderInterface* video_provider);
220 ~RemoteMediaStreamHandler();
221 virtual void AddAudioTrack(AudioTrackInterface* audio_track,
222 uint32 ssrc) OVERRIDE;
223 virtual void AddVideoTrack(VideoTrackInterface* video_track,
224 uint32 ssrc) OVERRIDE;
225};
226
227// Container for MediaStreamHandlers of currently known local and remote
228// MediaStreams.
229class MediaStreamHandlerContainer {
230 public:
231 MediaStreamHandlerContainer(AudioProviderInterface* audio_provider,
232 VideoProviderInterface* video_provider);
233 ~MediaStreamHandlerContainer();
234
235 // Notify all referenced objects that MediaStreamHandlerContainer will be
236 // destroyed. This method must be called prior to the dtor and prior to the
237 // |audio_provider| and |video_provider| is destroyed.
238 void TearDown();
239
240 // Remove all TrackHandlers for tracks in |stream| and make sure
241 // the audio_provider and video_provider is notified that the tracks has been
242 // removed.
243 void RemoveRemoteStream(MediaStreamInterface* stream);
244
245 // Create a RemoteAudioTrackHandler and associate |audio_track| with |ssrc|.
246 void AddRemoteAudioTrack(MediaStreamInterface* stream,
247 AudioTrackInterface* audio_track,
248 uint32 ssrc);
249 // Create a RemoteVideoTrackHandler and associate |video_track| with |ssrc|.
250 void AddRemoteVideoTrack(MediaStreamInterface* stream,
251 VideoTrackInterface* video_track,
252 uint32 ssrc);
253 // Remove the TrackHandler for |track|.
254 void RemoveRemoteTrack(MediaStreamInterface* stream,
255 MediaStreamTrackInterface* track);
256
257 // Remove all TrackHandlers for tracks in |stream| and make sure
258 // the audio_provider and video_provider is notified that the tracks has been
259 // removed.
260 void RemoveLocalStream(MediaStreamInterface* stream);
261
262 // Create a LocalAudioTrackHandler and associate |audio_track| with |ssrc|.
263 void AddLocalAudioTrack(MediaStreamInterface* stream,
264 AudioTrackInterface* audio_track,
265 uint32 ssrc);
266 // Create a LocalVideoTrackHandler and associate |video_track| with |ssrc|.
267 void AddLocalVideoTrack(MediaStreamInterface* stream,
268 VideoTrackInterface* video_track,
269 uint32 ssrc);
270 // Remove the TrackHandler for |track|.
271 void RemoveLocalTrack(MediaStreamInterface* stream,
272 MediaStreamTrackInterface* track);
273
274 private:
275 typedef std::list<MediaStreamHandler*> StreamHandlerList;
276 MediaStreamHandler* FindStreamHandler(const StreamHandlerList& handlers,
277 MediaStreamInterface* stream);
278 MediaStreamHandler* CreateRemoteStreamHandler(MediaStreamInterface* stream);
279 MediaStreamHandler* CreateLocalStreamHandler(MediaStreamInterface* stream);
280 void DeleteStreamHandler(StreamHandlerList* streamhandlers,
281 MediaStreamInterface* stream);
282
283 StreamHandlerList local_streams_handlers_;
284 StreamHandlerList remote_streams_handlers_;
285 AudioProviderInterface* audio_provider_;
286 VideoProviderInterface* video_provider_;
287};
288
289} // namespace webrtc
290
291#endif // TALK_APP_WEBRTC_MEDIASTREAMHANDLER_H_