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
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  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.
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  rtc::CriticalSection lock_;
91};
92
93// 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_;
112
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_;
116};
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 AudioSourceInterface::AudioObserver,
122                                public TrackHandler {
123 public:
124  RemoteAudioTrackHandler(AudioTrackInterface* track,
125                          uint32 ssrc,
126                          AudioProviderInterface* provider);
127  virtual ~RemoteAudioTrackHandler();
128  virtual void Stop() OVERRIDE;
129
130 protected:
131  virtual void OnStateChanged() OVERRIDE;
132  virtual void OnEnabledChanged() OVERRIDE;
133
134 private:
135  // AudioSourceInterface::AudioObserver implementation.
136  virtual void OnSetVolume(double volume) OVERRIDE;
137
138  AudioTrackInterface* audio_track_;
139  AudioProviderInterface* provider_;
140};
141
142// LocalVideoTrackHandler listen to events on a local VideoTrack instance
143// connected to a PeerConnection and orders the |provider| to executes the
144// requested change.
145class LocalVideoTrackHandler : public TrackHandler {
146 public:
147  LocalVideoTrackHandler(VideoTrackInterface* track,
148                         uint32 ssrc,
149                         VideoProviderInterface* provider);
150  virtual ~LocalVideoTrackHandler();
151  virtual void Stop() OVERRIDE;
152
153 protected:
154  virtual void OnStateChanged() OVERRIDE;
155  virtual void OnEnabledChanged() OVERRIDE;
156
157 private:
158  VideoTrackInterface* local_video_track_;
159  VideoProviderInterface* provider_;
160};
161
162// RemoteVideoTrackHandler listen to events on a remote VideoTrack instance
163// connected to a PeerConnection and orders the |provider| to execute
164// requested changes.
165class RemoteVideoTrackHandler : public TrackHandler {
166 public:
167  RemoteVideoTrackHandler(VideoTrackInterface* track,
168                          uint32 ssrc,
169                          VideoProviderInterface* provider);
170  virtual ~RemoteVideoTrackHandler();
171  virtual void Stop() OVERRIDE;
172
173 protected:
174  virtual void OnStateChanged() OVERRIDE;
175  virtual void OnEnabledChanged() OVERRIDE;
176
177 private:
178  VideoTrackInterface* remote_video_track_;
179  VideoProviderInterface* provider_;
180};
181
182class MediaStreamHandler : public ObserverInterface {
183 public:
184  MediaStreamHandler(MediaStreamInterface* stream,
185                     AudioProviderInterface* audio_provider,
186                     VideoProviderInterface* video_provider);
187  ~MediaStreamHandler();
188  MediaStreamInterface* stream();
189  void Stop();
190
191  virtual void AddAudioTrack(AudioTrackInterface* audio_track, uint32 ssrc) = 0;
192  virtual void AddVideoTrack(VideoTrackInterface* video_track, uint32 ssrc) = 0;
193
194  virtual void RemoveTrack(MediaStreamTrackInterface* track);
195  virtual void OnChanged() OVERRIDE;
196
197 protected:
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_;
204};
205
206class LocalMediaStreamHandler : public MediaStreamHandler {
207 public:
208  LocalMediaStreamHandler(MediaStreamInterface* stream,
209                          AudioProviderInterface* audio_provider,
210                          VideoProviderInterface* video_provider);
211  ~LocalMediaStreamHandler();
212
213  virtual void AddAudioTrack(AudioTrackInterface* audio_track,
214                             uint32 ssrc) OVERRIDE;
215  virtual void AddVideoTrack(VideoTrackInterface* video_track,
216                             uint32 ssrc) OVERRIDE;
217};
218
219class RemoteMediaStreamHandler : public MediaStreamHandler {
220 public:
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;
229};
230
231// Container for MediaStreamHandlers of currently known local and remote
232// MediaStreams.
233class MediaStreamHandlerContainer {
234 public:
235  MediaStreamHandlerContainer(AudioProviderInterface* audio_provider,
236                              VideoProviderInterface* video_provider);
237  ~MediaStreamHandlerContainer();
238
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.
242  void TearDown();
243
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
246  // removed.
247  void RemoveRemoteStream(MediaStreamInterface* stream);
248
249  // Create a RemoteAudioTrackHandler and associate |audio_track| with |ssrc|.
250  void AddRemoteAudioTrack(MediaStreamInterface* stream,
251                           AudioTrackInterface* audio_track,
252                           uint32 ssrc);
253  // Create a RemoteVideoTrackHandler and associate |video_track| with |ssrc|.
254  void AddRemoteVideoTrack(MediaStreamInterface* stream,
255                           VideoTrackInterface* video_track,
256                           uint32 ssrc);
257  // Remove the TrackHandler for |track|.
258  void RemoveRemoteTrack(MediaStreamInterface* stream,
259                         MediaStreamTrackInterface* track);
260
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
263  // removed.
264  void RemoveLocalStream(MediaStreamInterface* stream);
265
266  // Create a LocalAudioTrackHandler and associate |audio_track| with |ssrc|.
267  void AddLocalAudioTrack(MediaStreamInterface* stream,
268                          AudioTrackInterface* audio_track,
269                          uint32 ssrc);
270  // Create a LocalVideoTrackHandler and associate |video_track| with |ssrc|.
271  void AddLocalVideoTrack(MediaStreamInterface* stream,
272                          VideoTrackInterface* video_track,
273                          uint32 ssrc);
274  // Remove the TrackHandler for |track|.
275  void RemoveLocalTrack(MediaStreamInterface* stream,
276                        MediaStreamTrackInterface* track);
277
278 private:
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);
286
287  StreamHandlerList local_streams_handlers_;
288  StreamHandlerList remote_streams_handlers_;
289  AudioProviderInterface* audio_provider_;
290  VideoProviderInterface* video_provider_;
291};
292
293}  // namespace webrtc
294
295#endif  // TALK_APP_WEBRTC_MEDIASTREAMHANDLER_H_
296