xref: /aosp_15_r20/external/webrtc/pc/stream_collection.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1*d9f75844SAndroid Build Coastguard Worker /*
2*d9f75844SAndroid Build Coastguard Worker  *  Copyright 2011 The WebRTC project authors. All Rights Reserved.
3*d9f75844SAndroid Build Coastguard Worker  *
4*d9f75844SAndroid Build Coastguard Worker  *  Use of this source code is governed by a BSD-style license
5*d9f75844SAndroid Build Coastguard Worker  *  that can be found in the LICENSE file in the root of the source
6*d9f75844SAndroid Build Coastguard Worker  *  tree. An additional intellectual property rights grant can be found
7*d9f75844SAndroid Build Coastguard Worker  *  in the file PATENTS.  All contributing project authors may
8*d9f75844SAndroid Build Coastguard Worker  *  be found in the AUTHORS file in the root of the source tree.
9*d9f75844SAndroid Build Coastguard Worker  */
10*d9f75844SAndroid Build Coastguard Worker 
11*d9f75844SAndroid Build Coastguard Worker #ifndef PC_STREAM_COLLECTION_H_
12*d9f75844SAndroid Build Coastguard Worker #define PC_STREAM_COLLECTION_H_
13*d9f75844SAndroid Build Coastguard Worker 
14*d9f75844SAndroid Build Coastguard Worker #include <string>
15*d9f75844SAndroid Build Coastguard Worker #include <utility>
16*d9f75844SAndroid Build Coastguard Worker #include <vector>
17*d9f75844SAndroid Build Coastguard Worker 
18*d9f75844SAndroid Build Coastguard Worker #include "api/peer_connection_interface.h"
19*d9f75844SAndroid Build Coastguard Worker 
20*d9f75844SAndroid Build Coastguard Worker namespace webrtc {
21*d9f75844SAndroid Build Coastguard Worker 
22*d9f75844SAndroid Build Coastguard Worker // Implementation of StreamCollection.
23*d9f75844SAndroid Build Coastguard Worker class StreamCollection : public StreamCollectionInterface {
24*d9f75844SAndroid Build Coastguard Worker  public:
Create()25*d9f75844SAndroid Build Coastguard Worker   static rtc::scoped_refptr<StreamCollection> Create() {
26*d9f75844SAndroid Build Coastguard Worker     return rtc::make_ref_counted<StreamCollection>();
27*d9f75844SAndroid Build Coastguard Worker   }
28*d9f75844SAndroid Build Coastguard Worker 
Create(StreamCollection * streams)29*d9f75844SAndroid Build Coastguard Worker   static rtc::scoped_refptr<StreamCollection> Create(
30*d9f75844SAndroid Build Coastguard Worker       StreamCollection* streams) {
31*d9f75844SAndroid Build Coastguard Worker     return rtc::make_ref_counted<StreamCollection>(streams);
32*d9f75844SAndroid Build Coastguard Worker   }
33*d9f75844SAndroid Build Coastguard Worker 
count()34*d9f75844SAndroid Build Coastguard Worker   virtual size_t count() { return media_streams_.size(); }
35*d9f75844SAndroid Build Coastguard Worker 
at(size_t index)36*d9f75844SAndroid Build Coastguard Worker   virtual MediaStreamInterface* at(size_t index) {
37*d9f75844SAndroid Build Coastguard Worker     return media_streams_.at(index).get();
38*d9f75844SAndroid Build Coastguard Worker   }
39*d9f75844SAndroid Build Coastguard Worker 
find(const std::string & id)40*d9f75844SAndroid Build Coastguard Worker   virtual MediaStreamInterface* find(const std::string& id) {
41*d9f75844SAndroid Build Coastguard Worker     for (StreamVector::iterator it = media_streams_.begin();
42*d9f75844SAndroid Build Coastguard Worker          it != media_streams_.end(); ++it) {
43*d9f75844SAndroid Build Coastguard Worker       if ((*it)->id().compare(id) == 0) {
44*d9f75844SAndroid Build Coastguard Worker         return (*it).get();
45*d9f75844SAndroid Build Coastguard Worker       }
46*d9f75844SAndroid Build Coastguard Worker     }
47*d9f75844SAndroid Build Coastguard Worker     return NULL;
48*d9f75844SAndroid Build Coastguard Worker   }
49*d9f75844SAndroid Build Coastguard Worker 
FindAudioTrack(const std::string & id)50*d9f75844SAndroid Build Coastguard Worker   virtual MediaStreamTrackInterface* FindAudioTrack(const std::string& id) {
51*d9f75844SAndroid Build Coastguard Worker     for (size_t i = 0; i < media_streams_.size(); ++i) {
52*d9f75844SAndroid Build Coastguard Worker       MediaStreamTrackInterface* track =
53*d9f75844SAndroid Build Coastguard Worker           media_streams_[i]->FindAudioTrack(id).get();
54*d9f75844SAndroid Build Coastguard Worker       if (track) {
55*d9f75844SAndroid Build Coastguard Worker         return track;
56*d9f75844SAndroid Build Coastguard Worker       }
57*d9f75844SAndroid Build Coastguard Worker     }
58*d9f75844SAndroid Build Coastguard Worker     return NULL;
59*d9f75844SAndroid Build Coastguard Worker   }
60*d9f75844SAndroid Build Coastguard Worker 
FindVideoTrack(const std::string & id)61*d9f75844SAndroid Build Coastguard Worker   virtual MediaStreamTrackInterface* FindVideoTrack(const std::string& id) {
62*d9f75844SAndroid Build Coastguard Worker     for (size_t i = 0; i < media_streams_.size(); ++i) {
63*d9f75844SAndroid Build Coastguard Worker       MediaStreamTrackInterface* track =
64*d9f75844SAndroid Build Coastguard Worker           media_streams_[i]->FindVideoTrack(id).get();
65*d9f75844SAndroid Build Coastguard Worker       if (track) {
66*d9f75844SAndroid Build Coastguard Worker         return track;
67*d9f75844SAndroid Build Coastguard Worker       }
68*d9f75844SAndroid Build Coastguard Worker     }
69*d9f75844SAndroid Build Coastguard Worker     return NULL;
70*d9f75844SAndroid Build Coastguard Worker   }
71*d9f75844SAndroid Build Coastguard Worker 
AddStream(rtc::scoped_refptr<MediaStreamInterface> stream)72*d9f75844SAndroid Build Coastguard Worker   void AddStream(rtc::scoped_refptr<MediaStreamInterface> stream) {
73*d9f75844SAndroid Build Coastguard Worker     for (StreamVector::iterator it = media_streams_.begin();
74*d9f75844SAndroid Build Coastguard Worker          it != media_streams_.end(); ++it) {
75*d9f75844SAndroid Build Coastguard Worker       if ((*it)->id().compare(stream->id()) == 0)
76*d9f75844SAndroid Build Coastguard Worker         return;
77*d9f75844SAndroid Build Coastguard Worker     }
78*d9f75844SAndroid Build Coastguard Worker     media_streams_.push_back(std::move(stream));
79*d9f75844SAndroid Build Coastguard Worker   }
80*d9f75844SAndroid Build Coastguard Worker 
RemoveStream(MediaStreamInterface * remove_stream)81*d9f75844SAndroid Build Coastguard Worker   void RemoveStream(MediaStreamInterface* remove_stream) {
82*d9f75844SAndroid Build Coastguard Worker     for (StreamVector::iterator it = media_streams_.begin();
83*d9f75844SAndroid Build Coastguard Worker          it != media_streams_.end(); ++it) {
84*d9f75844SAndroid Build Coastguard Worker       if ((*it)->id().compare(remove_stream->id()) == 0) {
85*d9f75844SAndroid Build Coastguard Worker         media_streams_.erase(it);
86*d9f75844SAndroid Build Coastguard Worker         break;
87*d9f75844SAndroid Build Coastguard Worker       }
88*d9f75844SAndroid Build Coastguard Worker     }
89*d9f75844SAndroid Build Coastguard Worker   }
90*d9f75844SAndroid Build Coastguard Worker 
91*d9f75844SAndroid Build Coastguard Worker  protected:
StreamCollection()92*d9f75844SAndroid Build Coastguard Worker   StreamCollection() {}
StreamCollection(StreamCollection * original)93*d9f75844SAndroid Build Coastguard Worker   explicit StreamCollection(StreamCollection* original)
94*d9f75844SAndroid Build Coastguard Worker       : media_streams_(original->media_streams_) {}
95*d9f75844SAndroid Build Coastguard Worker   typedef std::vector<rtc::scoped_refptr<MediaStreamInterface> > StreamVector;
96*d9f75844SAndroid Build Coastguard Worker   StreamVector media_streams_;
97*d9f75844SAndroid Build Coastguard Worker };
98*d9f75844SAndroid Build Coastguard Worker 
99*d9f75844SAndroid Build Coastguard Worker }  // namespace webrtc
100*d9f75844SAndroid Build Coastguard Worker 
101*d9f75844SAndroid Build Coastguard Worker #endif  // PC_STREAM_COLLECTION_H_
102