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