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 2013 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 | |
| 28 | #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 29 | #error "This file requires ARC support." |
| 30 | #endif |
| 31 | |
fischman@webrtc.org | c693a2a | 2014-03-24 18:56:37 +0000 | [diff] [blame] | 32 | #import "RTCMediaStream+Internal.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 33 | |
fischman@webrtc.org | c693a2a | 2014-03-24 18:56:37 +0000 | [diff] [blame] | 34 | #import "RTCAudioTrack+Internal.h" |
| 35 | #import "RTCMediaStreamTrack+Internal.h" |
| 36 | #import "RTCVideoTrack+Internal.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 37 | |
Henrik Kjellander | 15583c1 | 2016-02-10 10:53:12 +0100 | [diff] [blame^] | 38 | #include "webrtc/api/mediastreaminterface.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 39 | |
| 40 | @implementation RTCMediaStream { |
fischman@webrtc.org | 7fa1fcb | 2014-03-25 00:11:56 +0000 | [diff] [blame] | 41 | NSMutableArray* _audioTracks; |
| 42 | NSMutableArray* _videoTracks; |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 43 | rtc::scoped_refptr<webrtc::MediaStreamInterface> _mediaStream; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 44 | } |
| 45 | |
fischman@webrtc.org | 7fa1fcb | 2014-03-25 00:11:56 +0000 | [diff] [blame] | 46 | - (NSString*)description { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 47 | return [NSString stringWithFormat:@"[%@:A=%lu:V=%lu]", |
fischman@webrtc.org | 7fa1fcb | 2014-03-25 00:11:56 +0000 | [diff] [blame] | 48 | [self label], |
| 49 | (unsigned long)[self.audioTracks count], |
| 50 | (unsigned long)[self.videoTracks count]]; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 51 | } |
| 52 | |
fischman@webrtc.org | 7fa1fcb | 2014-03-25 00:11:56 +0000 | [diff] [blame] | 53 | - (NSArray*)audioTracks { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 54 | return [_audioTracks copy]; |
| 55 | } |
| 56 | |
fischman@webrtc.org | 7fa1fcb | 2014-03-25 00:11:56 +0000 | [diff] [blame] | 57 | - (NSArray*)videoTracks { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 58 | return [_videoTracks copy]; |
| 59 | } |
| 60 | |
fischman@webrtc.org | 7fa1fcb | 2014-03-25 00:11:56 +0000 | [diff] [blame] | 61 | - (NSString*)label { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 62 | return @(self.mediaStream->label().c_str()); |
| 63 | } |
| 64 | |
fischman@webrtc.org | 7fa1fcb | 2014-03-25 00:11:56 +0000 | [diff] [blame] | 65 | - (BOOL)addAudioTrack:(RTCAudioTrack*)track { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 66 | if (self.mediaStream->AddTrack(track.audioTrack)) { |
| 67 | [_audioTracks addObject:track]; |
| 68 | return YES; |
| 69 | } |
| 70 | return NO; |
| 71 | } |
| 72 | |
fischman@webrtc.org | 7fa1fcb | 2014-03-25 00:11:56 +0000 | [diff] [blame] | 73 | - (BOOL)addVideoTrack:(RTCVideoTrack*)track { |
tkchin@webrtc.org | 8125744 | 2014-11-04 23:06:15 +0000 | [diff] [blame] | 74 | if (self.mediaStream->AddTrack(track.nativeVideoTrack)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 75 | [_videoTracks addObject:track]; |
| 76 | return YES; |
| 77 | } |
| 78 | return NO; |
| 79 | } |
| 80 | |
fischman@webrtc.org | 7fa1fcb | 2014-03-25 00:11:56 +0000 | [diff] [blame] | 81 | - (BOOL)removeAudioTrack:(RTCAudioTrack*)track { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 82 | NSUInteger index = [_audioTracks indexOfObjectIdenticalTo:track]; |
| 83 | NSAssert(index != NSNotFound, |
| 84 | @"|removeAudioTrack| called on unexpected RTCAudioTrack"); |
| 85 | if (index != NSNotFound && self.mediaStream->RemoveTrack(track.audioTrack)) { |
| 86 | [_audioTracks removeObjectAtIndex:index]; |
| 87 | return YES; |
| 88 | } |
| 89 | return NO; |
| 90 | } |
| 91 | |
fischman@webrtc.org | 7fa1fcb | 2014-03-25 00:11:56 +0000 | [diff] [blame] | 92 | - (BOOL)removeVideoTrack:(RTCVideoTrack*)track { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 93 | NSUInteger index = [_videoTracks indexOfObjectIdenticalTo:track]; |
| 94 | NSAssert(index != NSNotFound, |
| 95 | @"|removeAudioTrack| called on unexpected RTCVideoTrack"); |
tkchin@webrtc.org | 8125744 | 2014-11-04 23:06:15 +0000 | [diff] [blame] | 96 | if (index != NSNotFound && |
| 97 | self.mediaStream->RemoveTrack(track.nativeVideoTrack)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 98 | [_videoTracks removeObjectAtIndex:index]; |
| 99 | return YES; |
| 100 | } |
| 101 | return NO; |
| 102 | } |
| 103 | |
| 104 | @end |
| 105 | |
| 106 | @implementation RTCMediaStream (Internal) |
| 107 | |
| 108 | - (id)initWithMediaStream: |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 109 | (rtc::scoped_refptr<webrtc::MediaStreamInterface>)mediaStream { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 110 | if (!mediaStream) { |
| 111 | NSAssert(NO, @"nil arguments not allowed"); |
| 112 | self = nil; |
| 113 | return nil; |
| 114 | } |
| 115 | if ((self = [super init])) { |
| 116 | webrtc::AudioTrackVector audio_tracks = mediaStream->GetAudioTracks(); |
| 117 | webrtc::VideoTrackVector video_tracks = mediaStream->GetVideoTracks(); |
| 118 | |
| 119 | _audioTracks = [NSMutableArray arrayWithCapacity:audio_tracks.size()]; |
| 120 | _videoTracks = [NSMutableArray arrayWithCapacity:video_tracks.size()]; |
| 121 | _mediaStream = mediaStream; |
| 122 | |
| 123 | for (size_t i = 0; i < audio_tracks.size(); ++i) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 124 | rtc::scoped_refptr<webrtc::AudioTrackInterface> track = |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 125 | audio_tracks[i]; |
fischman@webrtc.org | 7fa1fcb | 2014-03-25 00:11:56 +0000 | [diff] [blame] | 126 | RTCAudioTrack* audioTrack = |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 127 | [[RTCAudioTrack alloc] initWithMediaTrack:track]; |
| 128 | [_audioTracks addObject:audioTrack]; |
| 129 | } |
henrike@webrtc.org | d3d6bce | 2014-03-10 20:41:22 +0000 | [diff] [blame] | 130 | |
| 131 | for (size_t i = 0; i < video_tracks.size(); ++i) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 132 | rtc::scoped_refptr<webrtc::VideoTrackInterface> track = |
henrike@webrtc.org | d3d6bce | 2014-03-10 20:41:22 +0000 | [diff] [blame] | 133 | video_tracks[i]; |
| 134 | RTCVideoTrack* videoTrack = |
| 135 | [[RTCVideoTrack alloc] initWithMediaTrack:track]; |
| 136 | [_videoTracks addObject:videoTrack]; |
| 137 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 138 | } |
| 139 | return self; |
| 140 | } |
| 141 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 142 | - (rtc::scoped_refptr<webrtc::MediaStreamInterface>)mediaStream { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 143 | return _mediaStream; |
| 144 | } |
| 145 | |
| 146 | @end |