通过阅读本文,您可以了解iOS推流SDK的使用方法。
RTMP推流
说明 阿里云视频直播不允许同一时间向同一个推流URL进行多路推流(第二路推流会被拒绝)。
- 导入推流头文件。
#import <AliLiveSdk/AliLiveSdk.h>
- 创建AliLiveEngine。
AliLiveConfig *config = [[AliLiveConfig alloc] init];
config.videoProfile = AliLiveVideoProfile_540P;
config.videoFPS = 20;
myConfig.pauseImage = [UIImage imageNamed:@"background_img.png"];
myConfig.accountID = @"";
AliLiveEngine *engine = [[AliLiveEngine alloc] initWithConfig:myConfig];
[engine setAudioSessionOperationRestriction:AliLiveAudioSessionOperationRestrictionDeactivateSession];
[engine setRtsDelegate:self];
[engine setStatusDelegate:self];
- 开始预览。
[self.engine startPreview:self.renderView];
- 开始推流。
[self.engine startPushWithURL:self.pushUrl];
- 结束推流。
[self.engine stopPush];
[self.engine stopPreview];
[self.engine destorySdk];
self.engine = nil;
RTMP拉流
- 导入头文件。
#import <AliyunPlayer/AliyunPlayer.h>
- 创建播放器。
self.player = [[AliPlayer alloc] init];
self.player.autoPlay = YES;
self.player.delegate = self;
self.player.playerView = self.renderView;
- 开始拉流。
AVPUrlSource *source = [[AVPUrlSource alloc] urlWithString:self.playurl];
[self.player setUrlSource:source];
[self.player prepare];
- 停止拉流。
[self.player stop];
[self.player destroy];
主播PK
- 主播A发送请求至appserver,获取目前在线且符合要求的主播列表。
[self.roomClient sendCmd:@"GetOnlineRoomList"];
获取到主播列表后渲染到页面。
- (void)onReceiveCommandGetOnlineRoomList:(NSDictionary *)msg {
NSArray *roomArray = [OnlineRoom roomArrayWithOnlineRoomArray:msg[@"onlineRoomList"]];
if (self.delegate && [self.delegate respondsToSelector:@selector(anchor:didReceiveOnlineRoomArray:)]) {
[self.delegate anchor:self didReceiveOnlineRoomArray:roomArray];
}
}
- 主播A在列表中找到一个主播B,请求PK。
[self.roomClient sendCmd:@"ApplyPk" param:@{@"toUserId":@(toUserId),@"toRoomId":@(toRoomId)}];
- 主播B收到主播A的申请,并弹出提示。
- (void)onReceiveCommandApplyPkNotice:(NSDictionary *)msg {
// 这里拿到申请方信息,并弹出提示
}
- 主播B同意了主播A的申请后,开始拉RTC流,并发送同意PK信令。
[self.engine subscribeStream:self.pkOnlineRoom.rtcPullUrl preferMaster:YES];
[self.roomClient sendCmd:@"ApprovePk" param:@{@"fromUserId":@(fromUserId), @"fromRoomId":@(fromRoomId), @"approve":@(approve)}];
- 主播A收到主播B的同意申请后,开始拉RTC流。
[self.engine subscribeStream:self.pkOnlineRoom.rtcPullUrl preferMaster:YES];
- 观众端播放的RTMP地址有变化,收到NotifyPublish信令后,重新构建播放器播放新地址。
self.rtmpUrl = anchorRtmpPullUrl;
[self playRtmp];
- 主播B停止PK,发送停止PK信令,停止拉RTC流,并恢复PK前画面。
[self.roomClient sendCmd:@"CancelPk"];
[self.engine unSubscribeStream:self.pkOnlineRoom.rtcPullUrl];
self.pkOnlineRoom = nil;
self.pkRenderView = nil;
- 主播A收到停止PK信令,结束拉流,并恢复PK前画面。
- (void)onReceiveCommandCancelPkNotice:(NSDictionary *)msg {
if (self.delegate && [self.delegate respondsToSelector:@selector(anchorDidReceiveCancelPk:)]) {
[self.delegate anchorDidReceiveCancelPk:self];
}
}
- 观众端播放的RTMP地址有变化,收到NotifyPublish信令后,重新构建播放器播放新地址。
self.rtmpUrl = anchorRtmpPullUrl;
[self playRtmp];