RTCPeerConnectionFactory.peerConnectionWithConfiguration导致IOS模拟器崩溃

时间:2017-06-04 02:16:59

标签: ios objective-c webrtc

我正在尝试使用WebTRC IOS库。 我试过了https://cocoapods.org/pods/webrtc-frameworkhttps://cocoapods.org/pods/WebRTC

每当我尝试初始化RTCPeerConnection并将其分配给局部变量时,应用程序崩溃。使用EXC_BAD_ACCESS错误代码。

这是我的代码:

@interface WebRTCDelegate ()

@property (nonatomic, strong) SRWebSocket* webSocket;
@property(nonatomic) RTCPeerConnection *peerConnection;

@end

@implementation WebRTCDelegate
...
- (void)initRTCPeerConnection
{
    NSArray<RTCIceServer *> *iceServers = [NSArray arrayWithObjects:[[RTCIceServer alloc] initWithURLStrings:[NSArray arrayWithObjects:@"stun:stun.services.mozilla.com", nil]], [[RTCIceServer alloc] initWithURLStrings:[NSArray arrayWithObjects:@"stun:stun.l.google.com:19302", nil]] , nil];

    RTCConfiguration *config = [[RTCConfiguration alloc] init];
    [config setIceServers:iceServers];

    NSDictionary *mandatoryConstraints = @{
                                           @"OfferToReceiveAudio" : @"true",
                                           @"OfferToReceiveVideo" : @"true",
                                           };
    RTCMediaConstraints* constraints = [[RTCMediaConstraints alloc] initWithMandatoryConstraints:mandatoryConstraints optionalConstraints:nil];

    RTCPeerConnectionFactory *pcFactory = [[RTCPeerConnectionFactory alloc] init];
    _peerConnection = [pcFactory peerConnectionWithConfiguration:config constraints:constraints delegate:self];
}

...

@end

这是错误:

0x108895cbd <+115>: movq   0x38(%rax), %r13
0x108895cc1 <+119>: leaq   0x4f1891(%rip), %rsi      ; "OnMessage"
0x108895cc8 <+126>: leaq   0x4f1894(%rip), %rdx      ; "../../webrtc/base/rtccertificategenerator.cc:69"

我的代码有什么问题吗?

谢谢!

1 个答案:

答案 0 :(得分:3)

尽量保持工厂对象的活着。

@interface WebRTCDelegate ()

@property (nonatomic) SRWebSocket *webSocket;
@property (nonatomic) RTCPeerConnectionFactory *factory;
@property (nonatomic) RTCPeerConnection *peerConnection;

@end

@implementation WebRTCDelegate

...

- (id)init
{
    self = [super init];
    if (self != nil)
    {
        factory = [[RTCPeerConnectionFactory alloc] init];
    }
    return self;
}

- (void)initRTCPeerConnection
{
    NSArray<RTCIceServer *> *iceServers = [NSArray arrayWithObjects:[[RTCIceServer alloc] initWithURLStrings:[NSArray arrayWithObjects:@"stun:stun.services.mozilla.com", nil]], [[RTCIceServer alloc] initWithURLStrings:[NSArray arrayWithObjects:@"stun:stun.l.google.com:19302", nil]] , nil];

    RTCConfiguration *config = [[RTCConfiguration alloc] init];
    [config setIceServers:iceServers];

    NSDictionary *mandatoryConstraints = @{
                                           @"OfferToReceiveAudio" : @"true",
                                           @"OfferToReceiveVideo" : @"true",
                                           };
    RTCMediaConstraints* constraints = [[RTCMediaConstraints alloc] initWithMandatoryConstraints:mandatoryConstraints optionalConstraints:nil];
    _peerConnection = [_factory peerConnectionWithConfiguration:config constraints:constraints delegate:self];
}

...

@end