1/*
2 * libjingle
3 * Copyright 2011, Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 *  1. Redistributions of source code must retain the above copyright notice,
9 *     this list of conditions and the following disclaimer.
10 *  2. Redistributions in binary form must reproduce the above copyright notice,
11 *     this list of conditions and the following disclaimer in the documentation
12 *     and/or other materials provided with the distribution.
13 *  3. The name of the author may not be used to endorse or promote products
14 *     derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#ifndef TALK_APP_WEBRTC_STREAMCOLLECTION_H_
29#define TALK_APP_WEBRTC_STREAMCOLLECTION_H_
30
31#include <string>
32#include <vector>
33
34#include "talk/app/webrtc/peerconnectioninterface.h"
35
36namespace webrtc {
37
38// Implementation of StreamCollection.
39class StreamCollection : public StreamCollectionInterface {
40 public:
41  static rtc::scoped_refptr<StreamCollection> Create() {
42    rtc::RefCountedObject<StreamCollection>* implementation =
43         new rtc::RefCountedObject<StreamCollection>();
44    return implementation;
45  }
46
47  static rtc::scoped_refptr<StreamCollection> Create(
48      StreamCollection* streams) {
49    rtc::RefCountedObject<StreamCollection>* implementation =
50         new rtc::RefCountedObject<StreamCollection>(streams);
51    return implementation;
52  }
53
54  virtual size_t count() {
55    return media_streams_.size();
56  }
57
58  virtual MediaStreamInterface* at(size_t index) {
59    return media_streams_.at(index);
60  }
61
62  virtual MediaStreamInterface* find(const std::string& label) {
63    for (StreamVector::iterator it = media_streams_.begin();
64         it != media_streams_.end(); ++it) {
65      if ((*it)->label().compare(label) == 0) {
66        return (*it);
67      }
68    }
69    return NULL;
70  }
71
72  virtual MediaStreamTrackInterface* FindAudioTrack(
73      const std::string& id) {
74    for (size_t i = 0; i < media_streams_.size(); ++i) {
75      MediaStreamTrackInterface* track = media_streams_[i]->FindAudioTrack(id);
76      if (track) {
77        return track;
78      }
79    }
80    return NULL;
81  }
82
83  virtual MediaStreamTrackInterface* FindVideoTrack(
84      const std::string& id) {
85    for (size_t i = 0; i < media_streams_.size(); ++i) {
86      MediaStreamTrackInterface* track = media_streams_[i]->FindVideoTrack(id);
87      if (track) {
88        return track;
89      }
90    }
91    return NULL;
92  }
93
94  void AddStream(MediaStreamInterface* stream) {
95    for (StreamVector::iterator it = media_streams_.begin();
96         it != media_streams_.end(); ++it) {
97      if ((*it)->label().compare(stream->label()) == 0)
98        return;
99    }
100    media_streams_.push_back(stream);
101  }
102
103  void RemoveStream(MediaStreamInterface* remove_stream) {
104    for (StreamVector::iterator it = media_streams_.begin();
105         it != media_streams_.end(); ++it) {
106      if ((*it)->label().compare(remove_stream->label()) == 0) {
107        media_streams_.erase(it);
108        break;
109      }
110    }
111  }
112
113 protected:
114  StreamCollection() {}
115  explicit StreamCollection(StreamCollection* original)
116      : media_streams_(original->media_streams_) {
117  }
118  typedef std::vector<rtc::scoped_refptr<MediaStreamInterface> >
119      StreamVector;
120  StreamVector media_streams_;
121};
122
123}  // namespace webrtc
124
125#endif  // TALK_APP_WEBRTC_STREAMCOLLECTION_H_
126