OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright 2011 The WebRTC project authors. All Rights Reserved. | |
3 * | |
4 * Use of this source code is governed by a BSD-style license | |
5 * that can be found in the LICENSE file in the root of the source | |
6 * tree. An additional intellectual property rights grant can be found | |
7 * in the file PATENTS. All contributing project authors may | |
8 * be found in the AUTHORS file in the root of the source tree. | |
9 */ | |
10 | |
11 #include "webrtc/api/mediastream.h" | |
12 #include "webrtc/base/logging.h" | |
13 | |
14 namespace webrtc { | |
15 | |
16 template <class V> | |
17 static typename V::iterator FindTrack(V* vector, | |
18 const std::string& track_id) { | |
19 typename V::iterator it = vector->begin(); | |
20 for (; it != vector->end(); ++it) { | |
21 if ((*it)->id() == track_id) { | |
22 break; | |
23 } | |
24 } | |
25 return it; | |
26 }; | |
27 | |
28 rtc::scoped_refptr<MediaStream> MediaStream::Create( | |
29 const std::string& label) { | |
30 rtc::RefCountedObject<MediaStream>* stream = | |
31 new rtc::RefCountedObject<MediaStream>(label); | |
32 return stream; | |
33 } | |
34 | |
35 MediaStream::MediaStream(const std::string& label) | |
36 : label_(label) { | |
37 } | |
38 | |
39 bool MediaStream::AddTrack(AudioTrackInterface* track) { | |
40 return AddTrack<AudioTrackVector, AudioTrackInterface>(&audio_tracks_, track); | |
41 } | |
42 | |
43 bool MediaStream::AddTrack(VideoTrackInterface* track) { | |
44 return AddTrack<VideoTrackVector, VideoTrackInterface>(&video_tracks_, track); | |
45 } | |
46 | |
47 bool MediaStream::RemoveTrack(AudioTrackInterface* track) { | |
48 return RemoveTrack<AudioTrackVector>(&audio_tracks_, track); | |
49 } | |
50 | |
51 bool MediaStream::RemoveTrack(VideoTrackInterface* track) { | |
52 return RemoveTrack<VideoTrackVector>(&video_tracks_, track); | |
53 } | |
54 | |
55 rtc::scoped_refptr<AudioTrackInterface> | |
56 MediaStream::FindAudioTrack(const std::string& track_id) { | |
57 AudioTrackVector::iterator it = FindTrack(&audio_tracks_, track_id); | |
58 if (it == audio_tracks_.end()) | |
59 return NULL; | |
60 return *it; | |
61 } | |
62 | |
63 rtc::scoped_refptr<VideoTrackInterface> | |
64 MediaStream::FindVideoTrack(const std::string& track_id) { | |
65 VideoTrackVector::iterator it = FindTrack(&video_tracks_, track_id); | |
66 if (it == video_tracks_.end()) | |
67 return NULL; | |
68 return *it; | |
69 } | |
70 | |
71 template <typename TrackVector, typename Track> | |
72 bool MediaStream::AddTrack(TrackVector* tracks, Track* track) { | |
73 typename TrackVector::iterator it = FindTrack(tracks, track->id()); | |
74 if (it != tracks->end()) | |
75 return false; | |
76 tracks->push_back(track); | |
77 FireOnChanged(); | |
78 return true; | |
79 } | |
80 | |
81 template <typename TrackVector> | |
82 bool MediaStream::RemoveTrack(TrackVector* tracks, | |
83 MediaStreamTrackInterface* track) { | |
84 ASSERT(tracks != NULL); | |
85 if (!track) | |
86 return false; | |
87 typename TrackVector::iterator it = FindTrack(tracks, track->id()); | |
88 if (it == tracks->end()) | |
89 return false; | |
90 tracks->erase(it); | |
91 FireOnChanged(); | |
92 return true; | |
93 } | |
94 | |
95 } // namespace webrtc | |
OLD | NEW |