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