全部产品
视频直播

主播发起连麦

更新时间:2017-09-06 11:46:48   分享:   

  1. 主播向APP Server发出请求,希望与观众A、B连麦。

    (客户端代码:StartLiveViewController.mm文件中)

    1. [SendMessageManager inviteVideoCall:self.roomId inviterUid:self.uid inviteeUids:inviteeUidArray inviterType:2 block:^(NSError *error) {
    2. }];
  2. APP Server向观众A、B反馈主播的请求。

    (APP Server端代码:controller\videocallCtrl.js文件中)

    1. inviteeUids.forEach((uid) => {
    2. if (uid) {
    3. let msg = util.createMqttMsg({
    4. inviterUid: inviterUid,
    5. inviterName: inviterInfo.name,
    6. inviterType: inviterType
    7. }, 1);
    8. mns.publish(liveRoomId, msg, 4, util.getUserTopicTag(liveRoomId, uid));
    9. }
    10. });
  3. 观众A、B向APP Server发送同意主播连麦请求的信号。

    (客户端代码:StartLiveViewController.mm文件中)

    1. - (void)onGetInviteMessage:(NSString *)inviterUid inviterName:(NSString*)inviterName inviterType:(int)inviterType
    2. {
    3. // 判断状态
    4. if (self.liveStatus == ALIVC_LIVE_ROOM_STATUS_NONE || self.liveStatus == ALIVC_LIVE_ROOM_STATUS_CHATTING) {
    5. return;
    6. }
    7. // 发送连麦同意反馈,status=1为同意连麦
    8. [SendMessageManager sendVideoCallFeedBack:inviterUid inviteeUid:self.userUid status:1 inviterType:2 inviteeType:1 block:^(NSURL *mainPlayUrl, NSArray<NSURL *> *playUrls, NSArray<NSString*>* Uids, NSString *rtmpUrl, NSError *error) {
    9. //rtmpUrl为AppServer反馈回来的推流地址,playUrls为此时正在推流的地址
    10. [self createLiveCallWithPushUrl:rtmpUrl hostPlayUrl:mainPlayUrl otherPlayUrls:playUrls otherPlayUids:Uids];
    11. }];
    12. }
  4. APP Server向观众A、B分配各自的推流地址,并发送主播流的播放地址。

    (APP Server端代码:controller\videocallCtrl.js文件中)

    1. videoCall.store(mainRoomId, uid + "|" + roomId);
    2. videoCall.setMainBy(roomId, mainRoomId);
    3. response.success(res, {
    4. mainPlayUrl: address.getShortRtmpPlayUrl(mainRoomId),//主播播放地址
    5. playUrls: otherPlayUrls, //已经推流观众播放地址
    6. rtmpUrl: address.getRtmpUrl(roomId) //被同意连麦观众的推流地址
    7. });
  5. 观众A、B开始连麦(即结束播放混流地址,开始推流、开始播放主播的播放地址)

    (客户端代码:LiveRoomViewController.m文件中,调用SDK接口onlineChats)

    1. int width = 180;
    2. int height = 320;
    3. // 设置参数
    4. self.publisherParam = [[NSMutableDictionary alloc] init];
    5. NSNumber* minBitRate = [[NSNumber alloc] initWithInt:100];
    6. [self.publisherParam setObject:minBitRate forKey:ALIVC_PUBLISHER_PARAM_MINBITRATE];
    7. ......
    8. // 显示窗口
    9. NSArray *playerViews = [self.liveRoomView addChatViewsWithArray:otherPlayUrlArray uidArrays:otherPlayUidArray];
    10. // 开始连麦,推流并打开其他播放地址
    11. int ret = [self.mediaPlayerCall onlineChats:pushUrl width:width height:height preview:self.liveRoomView.pushView publisherParam:self.publisherParam hostPlayUrl:hostPlayUrl playerUrls:otherPlayUrlArray playerViews:playerViews];
  6. APP Server收到AliLive给出的观众A、B推流成功的回调,将A、B的播放地址发送给主播和A、B、C。

    (APP Server端代码:service\alinotify.js文件中)

    1. static publish(liveInfo) {
    2. let roomId = liveInfo.roomId;
    3. delayQueue.deQueue(roomId);
    4. let msg = util.createMqttMsg({
    5. uid: liveInfo.uid,
    6. roomId: roomId,
    7. name: liveInfo.name,
    8. playUrl: address.getShortRtmpPlayUrl(roomId)
    9. }, 9);
    10. co(function*() {
    11. //设置推流状态
    12. live.setStatus(roomId, 1);
    13. let mainRoomId = yield videoCall.getMainBy(roomId);
    14. //发送推流通知
    15. mns.publish(mainRoomId ? mainRoomId : roomId, msg, 5);
    16. }).catch((err) => {
    17. console.log(err);
    18. });
    19. }
  7. 主播开始与A、B进行连麦(即播放A、B的播放地址)。

    (客户端代码:StartLiveViewController.mm文件中,调用SDK接口launchChats)

    1. - (void)createLiveCallWithInviteePlayUrls:(NSArray<NSURL*> *)inviteePlayUrlArray uids:(NSArray<NSString*> *)inviteePlayUidArray
    2. {
    3. //设置播放参数
    4. self.playerParam = [[NSMutableDictionary alloc] init];
    5. ......
    6. [self.publiserVideoCall setPlayerParam:self.playerParam];
    7. NSArray *inviteViews = [self.startLiveView addChatViewsWithArray:inviteePlayUrlArray uids:inviteePlayUidArray];
    8. int ret = [self.publiserVideoCall launchChats:inviteePlayUrlArray views:inviteViews];
    9. }
  8. 副麦A增加与B的连麦(即播放B的播放地址)。

    (客户端代码:LiveRoomViewController.m文件中,调用SDK接口addChats)

    1. - (void)onGetStartLiveMessage:(NSString *)roomId uid:(NSString *)uid name:(NSString *)name playUrl:(NSString *)playUrl
    2. {
    3. if ([self.currentInviterArray count] == 0) {
    4. [self createLiveCallWithInviteePlayUrls:@[url] uids:@[uid]];
    5. }
    6. else {
    7. [self addLiveCallWithPlayUrls:@[url] uids:@[uid]];
    8. }
    9. }
  9. 副麦B增加与A的连麦(即播放A的播放地址)。

    (客户端代码:LiveRoomViewController.m文件中,调用SDK接口addChats)

    1. - (void)onGetStartLiveMessage:(NSString *)roomId uid:(NSString *)uid name:(NSString *)name playUrl:(NSString *)playUrl
    2. {
    3. .......
    4. }
  10. APP Server调用AliLive的混流接口,开始混流且A、B加入混流画面。至此,主播与A、B连麦成功。

    (APP Server端代码:service\alinotify.js文件中,调用OpenAPI接口AddMultipleStreamMixService)

    1. if (mainRoomId) {
    2. yield videoCall.addToMix(mainRoomId, roomId, liveInfo.uid, liveInfo.name);
    3. }
本文导读目录
本文导读目录
以上内容是否对您有帮助?