1 /*
2 * libjingle
3 * Copyright 2011 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 #include "talk/app/webrtc/mediastream.h"
29 #include "webrtc/base/logging.h"
30
31 namespace webrtc {
32
33 template <class V>
FindTrack(V * vector,const std::string & track_id)34 static typename V::iterator FindTrack(V* vector,
35 const std::string& track_id) {
36 typename V::iterator it = vector->begin();
37 for (; it != vector->end(); ++it) {
38 if ((*it)->id() == track_id) {
39 break;
40 }
41 }
42 return it;
43 };
44
Create(const std::string & label)45 rtc::scoped_refptr<MediaStream> MediaStream::Create(
46 const std::string& label) {
47 rtc::RefCountedObject<MediaStream>* stream =
48 new rtc::RefCountedObject<MediaStream>(label);
49 return stream;
50 }
51
MediaStream(const std::string & label)52 MediaStream::MediaStream(const std::string& label)
53 : label_(label) {
54 }
55
AddTrack(AudioTrackInterface * track)56 bool MediaStream::AddTrack(AudioTrackInterface* track) {
57 return AddTrack<AudioTrackVector, AudioTrackInterface>(&audio_tracks_, track);
58 }
59
AddTrack(VideoTrackInterface * track)60 bool MediaStream::AddTrack(VideoTrackInterface* track) {
61 return AddTrack<VideoTrackVector, VideoTrackInterface>(&video_tracks_, track);
62 }
63
RemoveTrack(AudioTrackInterface * track)64 bool MediaStream::RemoveTrack(AudioTrackInterface* track) {
65 return RemoveTrack<AudioTrackVector>(&audio_tracks_, track);
66 }
67
RemoveTrack(VideoTrackInterface * track)68 bool MediaStream::RemoveTrack(VideoTrackInterface* track) {
69 return RemoveTrack<VideoTrackVector>(&video_tracks_, track);
70 }
71
72 rtc::scoped_refptr<AudioTrackInterface>
FindAudioTrack(const std::string & track_id)73 MediaStream::FindAudioTrack(const std::string& track_id) {
74 AudioTrackVector::iterator it = FindTrack(&audio_tracks_, track_id);
75 if (it == audio_tracks_.end())
76 return NULL;
77 return *it;
78 }
79
80 rtc::scoped_refptr<VideoTrackInterface>
FindVideoTrack(const std::string & track_id)81 MediaStream::FindVideoTrack(const std::string& track_id) {
82 VideoTrackVector::iterator it = FindTrack(&video_tracks_, track_id);
83 if (it == video_tracks_.end())
84 return NULL;
85 return *it;
86 }
87
88 template <typename TrackVector, typename Track>
AddTrack(TrackVector * tracks,Track * track)89 bool MediaStream::AddTrack(TrackVector* tracks, Track* track) {
90 typename TrackVector::iterator it = FindTrack(tracks, track->id());
91 if (it != tracks->end())
92 return false;
93 tracks->push_back(track);
94 FireOnChanged();
95 return true;
96 }
97
98 template <typename TrackVector>
RemoveTrack(TrackVector * tracks,MediaStreamTrackInterface * track)99 bool MediaStream::RemoveTrack(TrackVector* tracks,
100 MediaStreamTrackInterface* track) {
101 ASSERT(tracks != NULL);
102 if (!track)
103 return false;
104 typename TrackVector::iterator it = FindTrack(tracks, track->id());
105 if (it == tracks->end())
106 return false;
107 tracks->erase(it);
108 FireOnChanged();
109 return true;
110 }
111
112 } // namespace webrtc
113