henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* |
| 2 | * libjingle |
jlmiller@webrtc.org | 5f93d0a | 2015-01-20 21:36:13 +0000 | [diff] [blame] | 3 | * Copyright 2011 Google Inc. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 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 | |
Henrik Kjellander | 15583c1 | 2016-02-10 10:53:12 +0100 | [diff] [blame^] | 28 | #include "webrtc/api/mediastream.h" |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 29 | #include "webrtc/base/logging.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 30 | |
| 31 | namespace webrtc { |
| 32 | |
| 33 | template <class V> |
| 34 | static typename V::iterator FindTrack(V* vector, |
| 35 | const std::string& track_id) { |
| 36 | typename V::iterator it = vector->begin(); |
| 37 | for (; it != vector->end(); ++it) { |
| 38 | if ((*it)->id() == track_id) { |
| 39 | break; |
| 40 | } |
| 41 | } |
| 42 | return it; |
| 43 | }; |
| 44 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 45 | rtc::scoped_refptr<MediaStream> MediaStream::Create( |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 46 | const std::string& label) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 47 | rtc::RefCountedObject<MediaStream>* stream = |
| 48 | new rtc::RefCountedObject<MediaStream>(label); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 49 | return stream; |
| 50 | } |
| 51 | |
| 52 | MediaStream::MediaStream(const std::string& label) |
| 53 | : label_(label) { |
| 54 | } |
| 55 | |
| 56 | bool MediaStream::AddTrack(AudioTrackInterface* track) { |
| 57 | return AddTrack<AudioTrackVector, AudioTrackInterface>(&audio_tracks_, track); |
| 58 | } |
| 59 | |
| 60 | bool MediaStream::AddTrack(VideoTrackInterface* track) { |
| 61 | return AddTrack<VideoTrackVector, VideoTrackInterface>(&video_tracks_, track); |
| 62 | } |
| 63 | |
| 64 | bool MediaStream::RemoveTrack(AudioTrackInterface* track) { |
| 65 | return RemoveTrack<AudioTrackVector>(&audio_tracks_, track); |
| 66 | } |
| 67 | |
| 68 | bool MediaStream::RemoveTrack(VideoTrackInterface* track) { |
| 69 | return RemoveTrack<VideoTrackVector>(&video_tracks_, track); |
| 70 | } |
| 71 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 72 | rtc::scoped_refptr<AudioTrackInterface> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 73 | MediaStream::FindAudioTrack(const std::string& track_id) { |
| 74 | AudioTrackVector::iterator it = FindTrack(&audio_tracks_, track_id); |
| 75 | if (it == audio_tracks_.end()) |
| 76 | return NULL; |
| 77 | return *it; |
| 78 | } |
| 79 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 80 | rtc::scoped_refptr<VideoTrackInterface> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 81 | MediaStream::FindVideoTrack(const std::string& track_id) { |
| 82 | VideoTrackVector::iterator it = FindTrack(&video_tracks_, track_id); |
| 83 | if (it == video_tracks_.end()) |
| 84 | return NULL; |
| 85 | return *it; |
| 86 | } |
| 87 | |
| 88 | template <typename TrackVector, typename Track> |
| 89 | bool MediaStream::AddTrack(TrackVector* tracks, Track* track) { |
| 90 | typename TrackVector::iterator it = FindTrack(tracks, track->id()); |
| 91 | if (it != tracks->end()) |
| 92 | return false; |
| 93 | tracks->push_back(track); |
| 94 | FireOnChanged(); |
| 95 | return true; |
| 96 | } |
| 97 | |
| 98 | template <typename TrackVector> |
| 99 | bool MediaStream::RemoveTrack(TrackVector* tracks, |
| 100 | MediaStreamTrackInterface* track) { |
| 101 | ASSERT(tracks != NULL); |
| 102 | if (!track) |
| 103 | return false; |
| 104 | typename TrackVector::iterator it = FindTrack(tracks, track->id()); |
| 105 | if (it == tracks->end()) |
| 106 | return false; |
| 107 | tracks->erase(it); |
| 108 | FireOnChanged(); |
| 109 | return true; |
| 110 | } |
| 111 | |
| 112 | } // namespace webrtc |