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  rtc::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.nativeVideoTrack)) {
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 &&
97      self.mediaStream->RemoveTrack(track.nativeVideoTrack)) {
98    [_videoTracks removeObjectAtIndex:index];
99    return YES;
100  }
101  return NO;
102}
103
104@end
105
106@implementation RTCMediaStream (Internal)
107
108- (id)initWithMediaStream:
109          (rtc::scoped_refptr<webrtc::MediaStreamInterface>)mediaStream {
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) {
124      rtc::scoped_refptr<webrtc::AudioTrackInterface> track =
125          audio_tracks[i];
126      RTCAudioTrack* audioTrack =
127          [[RTCAudioTrack alloc] initWithMediaTrack:track];
128      [_audioTracks addObject:audioTrack];
129    }
130
131    for (size_t i = 0; i < video_tracks.size(); ++i) {
132      rtc::scoped_refptr<webrtc::VideoTrackInterface> track =
133          video_tracks[i];
134      RTCVideoTrack* videoTrack =
135          [[RTCVideoTrack alloc] initWithMediaTrack:track];
136      [_videoTracks addObject:videoTrack];
137    }
138  }
139  return self;
140}
141
142- (rtc::scoped_refptr<webrtc::MediaStreamInterface>)mediaStream {
143  return _mediaStream;
144}
145
146@end
147