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/checks.h" | |
13 #include "webrtc/base/logging.h" | |
14 | |
15 namespace webrtc { | |
16 | |
17 template <class V> | |
18 static typename V::iterator FindTrack(V* vector, | |
19 const std::string& track_id) { | |
20 typename V::iterator it = vector->begin(); | |
21 for (; it != vector->end(); ++it) { | |
22 if ((*it)->id() == track_id) { | |
23 break; | |
24 } | |
25 } | |
26 return it; | |
27 }; | |
28 | |
29 rtc::scoped_refptr<MediaStream> MediaStream::Create( | |
30 const std::string& label) { | |
31 rtc::RefCountedObject<MediaStream>* stream = | |
32 new rtc::RefCountedObject<MediaStream>(label); | |
33 return stream; | |
34 } | |
35 | |
36 MediaStream::MediaStream(const std::string& label) | |
37 : label_(label) { | |
38 } | |
39 | |
40 bool MediaStream::AddTrack(AudioTrackInterface* track) { | |
41 return AddTrack<AudioTrackVector, AudioTrackInterface>(&audio_tracks_, track); | |
42 } | |
43 | |
44 bool MediaStream::AddTrack(VideoTrackInterface* track) { | |
45 return AddTrack<VideoTrackVector, VideoTrackInterface>(&video_tracks_, track); | |
46 } | |
47 | |
48 bool MediaStream::RemoveTrack(AudioTrackInterface* track) { | |
49 return RemoveTrack<AudioTrackVector>(&audio_tracks_, track); | |
50 } | |
51 | |
52 bool MediaStream::RemoveTrack(VideoTrackInterface* track) { | |
53 return RemoveTrack<VideoTrackVector>(&video_tracks_, track); | |
54 } | |
55 | |
56 rtc::scoped_refptr<AudioTrackInterface> | |
57 MediaStream::FindAudioTrack(const std::string& track_id) { | |
58 AudioTrackVector::iterator it = FindTrack(&audio_tracks_, track_id); | |
59 if (it == audio_tracks_.end()) | |
60 return NULL; | |
61 return *it; | |
62 } | |
63 | |
64 rtc::scoped_refptr<VideoTrackInterface> | |
65 MediaStream::FindVideoTrack(const std::string& track_id) { | |
66 VideoTrackVector::iterator it = FindTrack(&video_tracks_, track_id); | |
67 if (it == video_tracks_.end()) | |
68 return NULL; | |
69 return *it; | |
70 } | |
71 | |
72 template <typename TrackVector, typename Track> | |
73 bool MediaStream::AddTrack(TrackVector* tracks, Track* track) { | |
74 typename TrackVector::iterator it = FindTrack(tracks, track->id()); | |
75 if (it != tracks->end()) | |
76 return false; | |
77 tracks->push_back(track); | |
78 FireOnChanged(); | |
79 return true; | |
80 } | |
81 | |
82 template <typename TrackVector> | |
83 bool MediaStream::RemoveTrack(TrackVector* tracks, | |
84 MediaStreamTrackInterface* track) { | |
85 RTC_DCHECK(tracks != NULL); | |
86 if (!track) | |
87 return false; | |
88 typename TrackVector::iterator it = FindTrack(tracks, track->id()); | |
89 if (it == tracks->end()) | |
90 return false; | |
91 tracks->erase(it); | |
92 FireOnChanged(); | |
93 return true; | |
94 } | |
95 | |
96 } // namespace webrtc | |
OLD | NEW |