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 #ifndef PC_STREAM_COLLECTION_H_
12 #define PC_STREAM_COLLECTION_H_
13 
14 #include <string>
15 #include <vector>
16 
17 #include "api/peer_connection_interface.h"
18 
19 namespace webrtc {
20 
21 // Implementation of StreamCollection.
22 class StreamCollection : public StreamCollectionInterface {
23  public:
Create()24   static rtc::scoped_refptr<StreamCollection> Create() {
25     rtc::RefCountedObject<StreamCollection>* implementation =
26         new rtc::RefCountedObject<StreamCollection>();
27     return implementation;
28   }
29 
Create(StreamCollection * streams)30   static rtc::scoped_refptr<StreamCollection> Create(
31       StreamCollection* streams) {
32     rtc::RefCountedObject<StreamCollection>* implementation =
33         new rtc::RefCountedObject<StreamCollection>(streams);
34     return implementation;
35   }
36 
count()37   virtual size_t count() { return media_streams_.size(); }
38 
at(size_t index)39   virtual MediaStreamInterface* at(size_t index) {
40     return media_streams_.at(index);
41   }
42 
find(const std::string & id)43   virtual MediaStreamInterface* find(const std::string& id) {
44     for (StreamVector::iterator it = media_streams_.begin();
45          it != media_streams_.end(); ++it) {
46       if ((*it)->id().compare(id) == 0) {
47         return (*it);
48       }
49     }
50     return NULL;
51   }
52 
FindAudioTrack(const std::string & id)53   virtual MediaStreamTrackInterface* FindAudioTrack(const std::string& id) {
54     for (size_t i = 0; i < media_streams_.size(); ++i) {
55       MediaStreamTrackInterface* track = media_streams_[i]->FindAudioTrack(id);
56       if (track) {
57         return track;
58       }
59     }
60     return NULL;
61   }
62 
FindVideoTrack(const std::string & id)63   virtual MediaStreamTrackInterface* FindVideoTrack(const std::string& id) {
64     for (size_t i = 0; i < media_streams_.size(); ++i) {
65       MediaStreamTrackInterface* track = media_streams_[i]->FindVideoTrack(id);
66       if (track) {
67         return track;
68       }
69     }
70     return NULL;
71   }
72 
AddStream(MediaStreamInterface * stream)73   void AddStream(MediaStreamInterface* stream) {
74     for (StreamVector::iterator it = media_streams_.begin();
75          it != media_streams_.end(); ++it) {
76       if ((*it)->id().compare(stream->id()) == 0)
77         return;
78     }
79     media_streams_.push_back(stream);
80   }
81 
RemoveStream(MediaStreamInterface * remove_stream)82   void RemoveStream(MediaStreamInterface* remove_stream) {
83     for (StreamVector::iterator it = media_streams_.begin();
84          it != media_streams_.end(); ++it) {
85       if ((*it)->id().compare(remove_stream->id()) == 0) {
86         media_streams_.erase(it);
87         break;
88       }
89     }
90   }
91 
92  protected:
StreamCollection()93   StreamCollection() {}
StreamCollection(StreamCollection * original)94   explicit StreamCollection(StreamCollection* original)
95       : media_streams_(original->media_streams_) {}
96   typedef std::vector<rtc::scoped_refptr<MediaStreamInterface> > StreamVector;
97   StreamVector media_streams_;
98 };
99 
100 }  // namespace webrtc
101 
102 #endif  // PC_STREAM_COLLECTION_H_
103