Update API for Objective-C RTCAudioTrack and RTCVideoTrack.

BUG=
R=tkchin@webrtc.org

Review URL: https://codereview.webrtc.org/1553743003 .

Patch from Jon Hjelle <hjon@andyet.net>.

Cr-Commit-Position: refs/heads/master@{#11350}
This commit is contained in:
Jon Hjelle 2016-01-21 15:36:47 -08:00 committed by Zeke Chin
parent 97888bd95a
commit ca91e38a3a
10 changed files with 284 additions and 4 deletions

View File

@ -29,6 +29,9 @@ if (is_ios) {
]
sources = [
# Add these when there's a BUILD.gn for peer connection APIs
#"objc/RTCAudioTrack+Private.h",
#"objc/RTCAudioTrack.h",
#"objc/RTCAudioTrack.mm",s
#"objc/RTCAVFoundationVideoSource+Private.h",
#"objc/RTCAVFoundationVideoSource.h",
#"objc/RTCAVFoundationVideoSource.mm",
@ -44,6 +47,9 @@ if (is_ios) {
#"objc/RTCVideoSource+Private.h",
#"objc/RTCVideoSource.h",
#"objc/RTCVideoSource.mm",
#"objc/RTCVideoTrack+Private.h",
#"objc/RTCVideoTrack.h",
#"objc/RTCVideoTrack.mm",
"objc/RTCIceServer+Private.h",
"objc/RTCIceServer.h",
"objc/RTCIceServer.mm",

View File

@ -19,6 +19,9 @@
'../../talk/libjingle.gyp:libjingle_peerconnection',
],
'sources': [
'objc/RTCAudioTrack+Private.h',
'objc/RTCAudioTrack.h',
'objc/RTCAudioTrack.mm',
'objc/RTCAVFoundationVideoSource+Private.h',
'objc/RTCAVFoundationVideoSource.h',
'objc/RTCAVFoundationVideoSource.mm',
@ -55,6 +58,9 @@
'objc/RTCVideoSource+Private.h',
'objc/RTCVideoSource.h',
'objc/RTCVideoSource.mm',
'objc/RTCVideoTrack+Private.h',
'objc/RTCVideoTrack.h',
'objc/RTCVideoTrack.mm',
'objc/avfoundationvideocapturer.h',
'objc/avfoundationvideocapturer.mm',
],

View File

@ -0,0 +1,25 @@
/*
* Copyright 2015 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#import "RTCAudioTrack.h"
#include "talk/app/webrtc/mediastreaminterface.h"
NS_ASSUME_NONNULL_BEGIN
@interface RTCAudioTrack ()
/** AudioTrackInterface created or passed in at construction. */
@property(nonatomic, readonly)
rtc::scoped_refptr<webrtc::AudioTrackInterface> nativeAudioTrack;
@end
NS_ASSUME_NONNULL_END

View File

@ -0,0 +1,27 @@
/*
* Copyright 2015 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#import "RTCMediaStreamTrack.h"
NS_ASSUME_NONNULL_BEGIN
@class RTCPeerConnectionFactory;
@interface RTCAudioTrack : RTCMediaStreamTrack
- (instancetype)init NS_UNAVAILABLE;
/** Initialize an RTCAudioTrack with an id. */
- (instancetype)initWithFactory:(RTCPeerConnectionFactory *)factory
trackId:(NSString *)trackId;
@end
NS_ASSUME_NONNULL_END

View File

@ -0,0 +1,44 @@
/*
* Copyright 2015 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#import "RTCAudioTrack.h"
#import "webrtc/api/objc/RTCAudioTrack+Private.h"
#import "webrtc/api/objc/RTCMediaStreamTrack+Private.h"
#import "webrtc/api/objc/RTCPeerConnectionFactory+Private.h"
#import "webrtc/base/objc/NSString+StdString.h"
@implementation RTCAudioTrack
- (instancetype)initWithFactory:(RTCPeerConnectionFactory *)factory
trackId:(NSString *)trackId {
NSParameterAssert(factory);
NSParameterAssert(trackId.length);
std::string nativeId = [NSString stdStringForString:trackId];
rtc::scoped_refptr<webrtc::AudioTrackInterface> track =
factory.nativeFactory->CreateAudioTrack(nativeId, nullptr);
return [self initWithNativeTrack:track type:RTCMediaStreamTrackTypeAudio];
}
- (instancetype)initWithNativeTrack:
(rtc::scoped_refptr<webrtc::MediaStreamTrackInterface>)nativeTrack
type:(RTCMediaStreamTrackType)type {
NSParameterAssert(nativeTrack);
NSParameterAssert(type == RTCMediaStreamTrackTypeAudio);
return [super initWithNativeTrack:nativeTrack type:type];
}
#pragma mark - Private
- (rtc::scoped_refptr<webrtc::AudioTrackInterface>)nativeAudioTrack {
return static_cast<webrtc::AudioTrackInterface *>(self.nativeTrack.get());
}
@end

View File

@ -13,14 +13,18 @@
#include "talk/app/webrtc/mediastreaminterface.h"
#include "webrtc/base/scoped_ptr.h"
typedef NS_ENUM(NSInteger, RTCMediaStreamTrackType) {
RTCMediaStreamTrackTypeAudio,
RTCMediaStreamTrackTypeVideo,
};
NS_ASSUME_NONNULL_BEGIN
@interface RTCMediaStreamTrack ()
/**
* The native MediaStreamTrackInterface representation of this
* RTCMediaStreamTrack object. This is needed to pass to the underlying C++
* APIs.
* The native MediaStreamTrackInterface passed in or created during
* construction.
*/
@property(nonatomic, readonly)
rtc::scoped_refptr<webrtc::MediaStreamTrackInterface> nativeTrack;
@ -30,6 +34,7 @@ NS_ASSUME_NONNULL_BEGIN
*/
- (instancetype)initWithNativeTrack:
(rtc::scoped_refptr<webrtc::MediaStreamTrackInterface>)nativeTrack
type:(RTCMediaStreamTrackType)type
NS_DESIGNATED_INITIALIZER;
+ (webrtc::MediaStreamTrackInterface::TrackState)nativeTrackStateForState:

View File

@ -15,6 +15,7 @@
@implementation RTCMediaStreamTrack {
rtc::scoped_refptr<webrtc::MediaStreamTrackInterface> _nativeTrack;
RTCMediaStreamTrackType _type;
}
- (NSString *)kind {
@ -53,10 +54,12 @@
}
- (instancetype)initWithNativeTrack:
(rtc::scoped_refptr<webrtc::MediaStreamTrackInterface>)nativeTrack {
(rtc::scoped_refptr<webrtc::MediaStreamTrackInterface>)nativeTrack
type:(RTCMediaStreamTrackType)type {
NSParameterAssert(nativeTrack);
if (self = [super init]) {
_nativeTrack = nativeTrack;
_type = type;
}
return self;
}

View File

@ -0,0 +1,25 @@
/*
* Copyright 2015 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#import "RTCVideoTrack.h"
#include "talk/app/webrtc/mediastreaminterface.h"
NS_ASSUME_NONNULL_BEGIN
@interface RTCVideoTrack ()
/** VideoTrackInterface created or passed in at construction. */
@property(nonatomic, readonly)
rtc::scoped_refptr<webrtc::VideoTrackInterface> nativeVideoTrack;
@end
NS_ASSUME_NONNULL_END

View File

@ -0,0 +1,39 @@
/*
* Copyright 2015 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#import "RTCMediaStreamTrack.h"
NS_ASSUME_NONNULL_BEGIN
@protocol RTCVideoRenderer;
@class RTCPeerConnectionFactory;
@class RTCVideoSource;
@interface RTCVideoTrack : RTCMediaStreamTrack
/** The video source for this video track. */
@property(nonatomic, readonly) RTCVideoSource *source;
- (instancetype)init NS_UNAVAILABLE;
/** Initialize an RTCVideoTrack with its source and an id. */
- (instancetype)initWithFactory:(RTCPeerConnectionFactory *)factory
source:(RTCVideoSource *)source
trackId:(NSString *)trackId;
/** Register a renderer that will render all frames received on this track. */
- (void)addRenderer:(id<RTCVideoRenderer>)renderer;
/** Deregister a renderer. */
- (void)removeRenderer:(id<RTCVideoRenderer>)renderer;
@end
NS_ASSUME_NONNULL_END

View File

@ -0,0 +1,100 @@
/*
* Copyright 2015 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#import "RTCVideoTrack.h"
#import "webrtc/api/objc/RTCMediaStreamTrack+Private.h"
#import "webrtc/api/objc/RTCPeerConnectionFactory+Private.h"
#import "webrtc/api/objc/RTCVideoRendererAdapter+Private.h"
#import "webrtc/api/objc/RTCVideoSource+Private.h"
#import "webrtc/api/objc/RTCVideoTrack+Private.h"
#import "webrtc/base/objc/NSString+StdString.h"
@implementation RTCVideoTrack {
NSMutableArray *_adapters;
}
@synthesize source = _source;
- (instancetype)initWithFactory:(RTCPeerConnectionFactory *)factory
source:(RTCVideoSource *)source
trackId:(NSString *)trackId {
NSParameterAssert(factory);
NSParameterAssert(source);
NSParameterAssert(trackId.length);
std::string nativeId = [NSString stdStringForString:trackId];
rtc::scoped_refptr<webrtc::VideoTrackInterface> track =
factory.nativeFactory->CreateVideoTrack(nativeId,
source.nativeVideoSource);
return [self initWithNativeTrack:track type:RTCMediaStreamTrackTypeVideo];
}
- (instancetype)initWithNativeMediaTrack:
(rtc::scoped_refptr<webrtc::MediaStreamTrackInterface>)nativeMediaTrack
type:(RTCMediaStreamTrackType)type {
NSParameterAssert(nativeMediaTrack);
NSParameterAssert(type == RTCMediaStreamTrackTypeVideo);
if (self = [super initWithNativeTrack:nativeMediaTrack type:type]) {
_adapters = [NSMutableArray array];
rtc::scoped_refptr<webrtc::VideoSourceInterface> source =
self.nativeVideoTrack->GetSource();
if (source) {
_source = [[RTCVideoSource alloc] initWithNativeVideoSource:source.get()];
}
}
return self;
}
- (void)dealloc {
for (RTCVideoRendererAdapter *adapter in _adapters) {
self.nativeVideoTrack->RemoveRenderer(adapter.nativeVideoRenderer);
}
}
- (void)addRenderer:(id<RTCVideoRenderer>)renderer {
// Make sure we don't have this renderer yet.
for (RTCVideoRendererAdapter *adapter in _adapters) {
// Getting around unused variable error
if (adapter.videoRenderer != renderer) {
NSAssert(NO, @"|renderer| is already attached to this track");
}
}
// Create a wrapper that provides a native pointer for us.
RTCVideoRendererAdapter* adapter =
[[RTCVideoRendererAdapter alloc] initWithNativeRenderer:renderer];
[_adapters addObject:adapter];
self.nativeVideoTrack->AddRenderer(adapter.nativeVideoRenderer);
}
- (void)removeRenderer:(id<RTCVideoRenderer>)renderer {
RTCVideoRendererAdapter *adapter;
__block NSUInteger indexToRemove = NSNotFound;
[_adapters enumerateObjectsUsingBlock:^(RTCVideoRendererAdapter *adapter,
NSUInteger idx,
BOOL *stop) {
if (adapter.videoRenderer == renderer) {
indexToRemove = idx;
*stop = YES;
}
}];
if (indexToRemove == NSNotFound) {
return;
}
self.nativeVideoTrack->RemoveRenderer(adapter.nativeVideoRenderer);
[_adapters removeObjectAtIndex:indexToRemove];
}
#pragma mark - Private
- (rtc::scoped_refptr<webrtc::VideoTrackInterface>)nativeVideoTrack {
return static_cast<webrtc::VideoTrackInterface *>(self.nativeTrack.get());
}
@end