12次后添加和删除本地视频轨道,在WebRTC iOS中断开视频轨道

时间:2017-08-26 12:38:32

标签: ios objective-c video webrtc

我只使用RTCVideoTrack创建本地流,并在RTCEAGLVideoView中播放视频轨道。按btn 24次后(意味着播放12次),视频轨道断开,只显示卡住的图片。

代码看起来很简单,但我无法弄清楚问题是什么。 我将代码上传到github,任何人都可以下载项目来用iphone测试演示。

github中的代码:{{3}}

ViewController.m的内容:

@interface ViewController ()

@property (nonatomic, strong) RTCPeerConnectionFactory *peerFactory;
@property (nonatomic, strong) RTCMediaStream *mediaStream;
@property (nonatomic, strong) RTCEAGLVideoView *videoView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    // create local stream
    [self createLocalStream];

    // button to control the play and unplay video of local stream
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(300, 300, 100, 100);
    [btn addTarget:self action:@selector(playOrUnPlay) forControlEvents:UIControlEventTouchUpInside];
    btn.backgroundColor = [UIColor redColor];
    [self.view addSubview:btn];
}

// control the play and unplay video of local stream
- (void)playOrUnPlay {
    static int flag = 0;
    static int times = 0;
    if (flag % 2 == 0) {
        [self play];
        ++times;
        NSLog(@"play times: %d", times);
    } else {
        [self unplay];
    }
    ++flag;
}

- (void)createLocalStream {
    self.peerFactory = [[RTCPeerConnectionFactory alloc] init];
    self.mediaStream = [self.peerFactory mediaStreamWithStreamId:@"Fuck"];

    // create video track
    RTCAVFoundationVideoSource *source = [self.peerFactory avFoundationVideoSourceWithConstraints:nil];
    RTCVideoTrack *videoTrack = [self.peerFactory videoTrackWithSource:source trackId:@"FuckVideo"];

    // add video track to stream
    if (videoTrack) {
        [self.mediaStream addVideoTrack:videoTrack];
    }

}

// play video of local stream
- (void)play {
    self.videoView = [[RTCEAGLVideoView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
    [self.view addSubview:self.videoView];

    if (self.mediaStream.videoTracks.count > 0) {
        RTCVideoTrack *videoTrack = self.mediaStream.videoTracks.firstObject;
        [videoTrack addRenderer:self.videoView];
    }
}

// stop play video of local stream
- (void)unplay {
    if (self.mediaStream.videoTracks.count > 0) {
        RTCVideoTrack *videoTrack = self.mediaStream.videoTracks.firstObject;
        [videoTrack removeRenderer:self.videoView];
    }
    [self.videoView removeFromSuperview];
}

0 个答案:

没有答案