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