• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 *  Copyright 2015 The WebRTC project authors. All Rights Reserved.
3 *
4 *  Use of this source code is governed by a BSD-style license
5 *  that can be found in the LICENSE file in the root of the source
6 *  tree. An additional intellectual property rights grant can be found
7 *  in the file PATENTS.  All contributing project authors may
8 *  be found in the AUTHORS file in the root of the source tree.
9 */
10
11#import "RTCAudioTrack+Private.h"
12#import "RTCMediaStreamTrack+Private.h"
13#import "RTCVideoTrack+Private.h"
14
15#import "helpers/NSString+StdString.h"
16
17NSString * const kRTCMediaStreamTrackKindAudio =
18    @(webrtc::MediaStreamTrackInterface::kAudioKind);
19NSString * const kRTCMediaStreamTrackKindVideo =
20    @(webrtc::MediaStreamTrackInterface::kVideoKind);
21
22@implementation RTC_OBJC_TYPE (RTCMediaStreamTrack) {
23  RTC_OBJC_TYPE(RTCPeerConnectionFactory) * _factory;
24  rtc::scoped_refptr<webrtc::MediaStreamTrackInterface> _nativeTrack;
25  RTCMediaStreamTrackType _type;
26}
27
28- (NSString *)kind {
29  return [NSString stringForStdString:_nativeTrack->kind()];
30}
31
32- (NSString *)trackId {
33  return [NSString stringForStdString:_nativeTrack->id()];
34}
35
36- (BOOL)isEnabled {
37  return _nativeTrack->enabled();
38}
39
40- (void)setIsEnabled:(BOOL)isEnabled {
41  _nativeTrack->set_enabled(isEnabled);
42}
43
44- (RTCMediaStreamTrackState)readyState {
45  return [[self class] trackStateForNativeState:_nativeTrack->state()];
46}
47
48- (NSString *)description {
49  NSString *readyState = [[self class] stringForState:self.readyState];
50  return [NSString stringWithFormat:@"RTC_OBJC_TYPE(RTCMediaStreamTrack):\n%@\n%@\n%@\n%@",
51                                    self.kind,
52                                    self.trackId,
53                                    self.isEnabled ? @"enabled" : @"disabled",
54                                    readyState];
55}
56
57- (BOOL)isEqual:(id)object {
58  if (self == object) {
59    return YES;
60  }
61  if (![object isMemberOfClass:[self class]]) {
62    return NO;
63  }
64  return [self isEqualToTrack:(RTC_OBJC_TYPE(RTCMediaStreamTrack) *)object];
65}
66
67- (NSUInteger)hash {
68  return (NSUInteger)_nativeTrack.get();
69}
70
71#pragma mark - Private
72
73- (rtc::scoped_refptr<webrtc::MediaStreamTrackInterface>)nativeTrack {
74  return _nativeTrack;
75}
76
77@synthesize factory = _factory;
78
79- (instancetype)initWithFactory:(RTC_OBJC_TYPE(RTCPeerConnectionFactory) *)factory
80                    nativeTrack:(rtc::scoped_refptr<webrtc::MediaStreamTrackInterface>)nativeTrack
81                           type:(RTCMediaStreamTrackType)type {
82  NSParameterAssert(nativeTrack);
83  NSParameterAssert(factory);
84  if (self = [super init]) {
85    _factory = factory;
86    _nativeTrack = nativeTrack;
87    _type = type;
88  }
89  return self;
90}
91
92- (instancetype)initWithFactory:(RTC_OBJC_TYPE(RTCPeerConnectionFactory) *)factory
93                    nativeTrack:(rtc::scoped_refptr<webrtc::MediaStreamTrackInterface>)nativeTrack {
94  NSParameterAssert(nativeTrack);
95  if (nativeTrack->kind() ==
96      std::string(webrtc::MediaStreamTrackInterface::kAudioKind)) {
97    return [self initWithFactory:factory nativeTrack:nativeTrack type:RTCMediaStreamTrackTypeAudio];
98  }
99  if (nativeTrack->kind() ==
100      std::string(webrtc::MediaStreamTrackInterface::kVideoKind)) {
101    return [self initWithFactory:factory nativeTrack:nativeTrack type:RTCMediaStreamTrackTypeVideo];
102  }
103  return nil;
104}
105
106- (BOOL)isEqualToTrack:(RTC_OBJC_TYPE(RTCMediaStreamTrack) *)track {
107  if (!track) {
108    return NO;
109  }
110  return _nativeTrack == track.nativeTrack;
111}
112
113+ (webrtc::MediaStreamTrackInterface::TrackState)nativeTrackStateForState:
114    (RTCMediaStreamTrackState)state {
115  switch (state) {
116    case RTCMediaStreamTrackStateLive:
117      return webrtc::MediaStreamTrackInterface::kLive;
118    case RTCMediaStreamTrackStateEnded:
119      return webrtc::MediaStreamTrackInterface::kEnded;
120  }
121}
122
123+ (RTCMediaStreamTrackState)trackStateForNativeState:
124    (webrtc::MediaStreamTrackInterface::TrackState)nativeState {
125  switch (nativeState) {
126    case webrtc::MediaStreamTrackInterface::kLive:
127      return RTCMediaStreamTrackStateLive;
128    case webrtc::MediaStreamTrackInterface::kEnded:
129      return RTCMediaStreamTrackStateEnded;
130  }
131}
132
133+ (NSString *)stringForState:(RTCMediaStreamTrackState)state {
134  switch (state) {
135    case RTCMediaStreamTrackStateLive:
136      return @"Live";
137    case RTCMediaStreamTrackStateEnded:
138      return @"Ended";
139  }
140}
141
142+ (RTC_OBJC_TYPE(RTCMediaStreamTrack) *)
143    mediaTrackForNativeTrack:(rtc::scoped_refptr<webrtc::MediaStreamTrackInterface>)nativeTrack
144                     factory:(RTC_OBJC_TYPE(RTCPeerConnectionFactory) *)factory {
145  NSParameterAssert(nativeTrack);
146  NSParameterAssert(factory);
147  if (nativeTrack->kind() == webrtc::MediaStreamTrackInterface::kAudioKind) {
148    return [[RTC_OBJC_TYPE(RTCAudioTrack) alloc] initWithFactory:factory
149                                                     nativeTrack:nativeTrack
150                                                            type:RTCMediaStreamTrackTypeAudio];
151  } else if (nativeTrack->kind() == webrtc::MediaStreamTrackInterface::kVideoKind) {
152    return [[RTC_OBJC_TYPE(RTCVideoTrack) alloc] initWithFactory:factory
153                                                     nativeTrack:nativeTrack
154                                                            type:RTCMediaStreamTrackTypeVideo];
155  } else {
156    return [[RTC_OBJC_TYPE(RTCMediaStreamTrack) alloc] initWithFactory:factory
157                                                           nativeTrack:nativeTrack];
158  }
159}
160
161@end
162