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