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
13#import "RTCAudioSource+Private.h"
14#import "RTCMediaStreamTrack+Private.h"
15#import "RTCPeerConnectionFactory+Private.h"
16#import "helpers/NSString+StdString.h"
17
18#include "rtc_base/checks.h"
19
20@implementation RTC_OBJC_TYPE (RTCAudioTrack)
21
22@synthesize source = _source;
23
24- (instancetype)initWithFactory:(RTC_OBJC_TYPE(RTCPeerConnectionFactory) *)factory
25                         source:(RTC_OBJC_TYPE(RTCAudioSource) *)source
26                        trackId:(NSString *)trackId {
27  RTC_DCHECK(factory);
28  RTC_DCHECK(source);
29  RTC_DCHECK(trackId.length);
30
31  std::string nativeId = [NSString stdStringForString:trackId];
32  rtc::scoped_refptr<webrtc::AudioTrackInterface> track =
33      factory.nativeFactory->CreateAudioTrack(nativeId, source.nativeAudioSource);
34  if (self = [self initWithFactory:factory nativeTrack:track type:RTCMediaStreamTrackTypeAudio]) {
35    _source = source;
36  }
37  return self;
38}
39
40- (instancetype)initWithFactory:(RTC_OBJC_TYPE(RTCPeerConnectionFactory) *)factory
41                    nativeTrack:(rtc::scoped_refptr<webrtc::MediaStreamTrackInterface>)nativeTrack
42                           type:(RTCMediaStreamTrackType)type {
43  NSParameterAssert(factory);
44  NSParameterAssert(nativeTrack);
45  NSParameterAssert(type == RTCMediaStreamTrackTypeAudio);
46  return [super initWithFactory:factory nativeTrack:nativeTrack type:type];
47}
48
49- (RTC_OBJC_TYPE(RTCAudioSource) *)source {
50  if (!_source) {
51    rtc::scoped_refptr<webrtc::AudioSourceInterface> source =
52        self.nativeAudioTrack->GetSource();
53    if (source) {
54      _source = [[RTC_OBJC_TYPE(RTCAudioSource) alloc] initWithFactory:self.factory
55                                                     nativeAudioSource:source.get()];
56    }
57  }
58  return _source;
59}
60
61#pragma mark - Private
62
63- (rtc::scoped_refptr<webrtc::AudioTrackInterface>)nativeAudioTrack {
64  return static_cast<webrtc::AudioTrackInterface *>(self.nativeTrack.get());
65}
66
67@end
68