blob: f0f3f07b4bab3494274927ee9adbaf99cafedb51 [file] [log] [blame]
ossu7bb87ee2017-01-23 04:56:25 -08001/*
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
Steve Anton10542f22019-01-11 09:11:00 -080011#ifndef PC_STREAM_COLLECTION_H_
12#define PC_STREAM_COLLECTION_H_
ossu7bb87ee2017-01-23 04:56:25 -080013
14#include <string>
Niels Möllere7cc8832022-01-04 15:20:03 +010015#include <utility>
ossu7bb87ee2017-01-23 04:56:25 -080016#include <vector>
17
Steve Anton10542f22019-01-11 09:11:00 -080018#include "api/peer_connection_interface.h"
ossu7bb87ee2017-01-23 04:56:25 -080019
20namespace webrtc {
21
22// Implementation of StreamCollection.
23class StreamCollection : public StreamCollectionInterface {
24 public:
25 static rtc::scoped_refptr<StreamCollection> Create() {
Tommi87f70902021-04-27 14:43:08 +020026 return rtc::make_ref_counted<StreamCollection>();
ossu7bb87ee2017-01-23 04:56:25 -080027 }
28
29 static rtc::scoped_refptr<StreamCollection> Create(
30 StreamCollection* streams) {
Tommi87f70902021-04-27 14:43:08 +020031 return rtc::make_ref_counted<StreamCollection>(streams);
ossu7bb87ee2017-01-23 04:56:25 -080032 }
33
Yves Gerey665174f2018-06-19 15:03:05 +020034 virtual size_t count() { return media_streams_.size(); }
ossu7bb87ee2017-01-23 04:56:25 -080035
36 virtual MediaStreamInterface* at(size_t index) {
Niels Möllerafb246b2022-04-20 14:26:50 +020037 return media_streams_.at(index).get();
ossu7bb87ee2017-01-23 04:56:25 -080038 }
39
Seth Hampson13b8bad2018-03-13 16:05:28 -070040 virtual MediaStreamInterface* find(const std::string& id) {
ossu7bb87ee2017-01-23 04:56:25 -080041 for (StreamVector::iterator it = media_streams_.begin();
42 it != media_streams_.end(); ++it) {
Seth Hampson13b8bad2018-03-13 16:05:28 -070043 if ((*it)->id().compare(id) == 0) {
Niels Möllerafb246b2022-04-20 14:26:50 +020044 return (*it).get();
ossu7bb87ee2017-01-23 04:56:25 -080045 }
46 }
47 return NULL;
48 }
49
Yves Gerey665174f2018-06-19 15:03:05 +020050 virtual MediaStreamTrackInterface* FindAudioTrack(const std::string& id) {
ossu7bb87ee2017-01-23 04:56:25 -080051 for (size_t i = 0; i < media_streams_.size(); ++i) {
Niels Möllerafb246b2022-04-20 14:26:50 +020052 MediaStreamTrackInterface* track =
53 media_streams_[i]->FindAudioTrack(id).get();
ossu7bb87ee2017-01-23 04:56:25 -080054 if (track) {
55 return track;
56 }
57 }
58 return NULL;
59 }
60
Yves Gerey665174f2018-06-19 15:03:05 +020061 virtual MediaStreamTrackInterface* FindVideoTrack(const std::string& id) {
ossu7bb87ee2017-01-23 04:56:25 -080062 for (size_t i = 0; i < media_streams_.size(); ++i) {
Niels Möllerafb246b2022-04-20 14:26:50 +020063 MediaStreamTrackInterface* track =
64 media_streams_[i]->FindVideoTrack(id).get();
ossu7bb87ee2017-01-23 04:56:25 -080065 if (track) {
66 return track;
67 }
68 }
69 return NULL;
70 }
71
Niels Möllere7cc8832022-01-04 15:20:03 +010072 void AddStream(rtc::scoped_refptr<MediaStreamInterface> stream) {
ossu7bb87ee2017-01-23 04:56:25 -080073 for (StreamVector::iterator it = media_streams_.begin();
74 it != media_streams_.end(); ++it) {
Seth Hampson13b8bad2018-03-13 16:05:28 -070075 if ((*it)->id().compare(stream->id()) == 0)
ossu7bb87ee2017-01-23 04:56:25 -080076 return;
77 }
Niels Möllere7cc8832022-01-04 15:20:03 +010078 media_streams_.push_back(std::move(stream));
ossu7bb87ee2017-01-23 04:56:25 -080079 }
80
81 void RemoveStream(MediaStreamInterface* remove_stream) {
82 for (StreamVector::iterator it = media_streams_.begin();
83 it != media_streams_.end(); ++it) {
Seth Hampson13b8bad2018-03-13 16:05:28 -070084 if ((*it)->id().compare(remove_stream->id()) == 0) {
ossu7bb87ee2017-01-23 04:56:25 -080085 media_streams_.erase(it);
86 break;
87 }
88 }
89 }
90
91 protected:
92 StreamCollection() {}
93 explicit StreamCollection(StreamCollection* original)
Yves Gerey665174f2018-06-19 15:03:05 +020094 : media_streams_(original->media_streams_) {}
95 typedef std::vector<rtc::scoped_refptr<MediaStreamInterface> > StreamVector;
ossu7bb87ee2017-01-23 04:56:25 -080096 StreamVector media_streams_;
97};
98
99} // namespace webrtc
100
Steve Anton10542f22019-01-11 09:11:00 -0800101#endif // PC_STREAM_COLLECTION_H_