全部产品
视频直播

主播发起直播与观众观看直播

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

  1. 主播向APP Server发起直播请求。

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

    1. [SendMessageManager creatLive:self.uid description:self.startLiveView.descTextFeild.text block:^(RoomInfoModel *roomInfo, MNSInfoModel *mnsInfo, NSError *error) {
    2. //获取推流地址
    3. self.rtmpURLString = roomInfo.rtmpUrl;
    4. self.roomId = roomInfo.roomId;
    5. }];
  2. APP Server同意主播发起直播,并分配主播的推流地址、播放地址以及混流地址。

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

    1. static createLive(userInfo, desc, status = 0, type = 2) {
    2. desc = desc ? desc : `${userInfo.name}的直播`;
    3. let roomId = dqUtil.random(6);
    4. return new Promise((resolve, reject) => {
    5. co(function*() {
    6. let mnsInfo = {};
    7. if (type == 2) {
    8. yield mns.create(roomId);
    9. yield mns.subscribe(roomId);
    10. mnsInfo = {
    11. topic: roomId,
    12. subscriptionName: roomId,
    13. topicLocation: mns.getLocation(roomId),
    14. roomTag: roomId,
    15. userRoomTag: dqUtil.getUserTopicTag(roomId, userInfo.id)
    16. };
    17. }
    18. // yield Live.addUserToPlayList(roomId, userInfo.id);
    19. let data = {
    20. uid: userInfo.id,
    21. name: userInfo.name,
    22. roomId: roomId,
    23. rtmpUrl: address.getRtmpUrl(roomId),
    24. playUrl: address.getPlayUrl(roomId),
    25. m3u8PlayUrl: address.getPlayUrl(roomId, config.videocall.templateName, 'm3u8'),
    26. rtmpPlayUrl: address.getRtmpPlayUrl(roomId),
    27. status: status,
    28. type: type,
    29. isMixReady: false,
    30. isMixed: false,
    31. mns: mnsInfo,
    32. mixedStatus: 0,
    33. description: desc,
    34. createTime: dqUtil.date()
    35. };
    36. }).catch((err) => {
    37. reject(err)
    38. });
    39. });
    40. }
  3. 主播通过推流地址开始推流。

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

    1. self.publiserVideoCall = [[AlivcVideoChatHost alloc] init];
    2. int width = 360; // or 480
    3. int height = 640; // or 848
    4. // 参数配置
    5. self.publisherParam = [[NSMutableDictionary alloc] init];
    6. NSNumber* frontCamera = [[NSNumber alloc] initWithBool:cameraPositionFront];
    7. ......
    8. [self.publisherParam setObject:cameraMirror forKey:ALIVC_PUBLISHER_PARAM_FRONTCAMERAMIRROR];
    9. // 预览
    10. int ret = [self.publiserVideoCall prepareToPublish:self.startLiveView.publisherView width:width height:height publisherParam:self.publisherParam];
    11. // 注册SDK连麦通知
    12. [self addVideoChatObserver];
    13. // 开始推流
    14. int ret = [self.publiserVideoCall startToPublish:self.rtmpURLString];
  4. APP Server收到AliLive推流成功的回调,将主播放入直播列表,表示此时观众可以播放主播的直播流。

    (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. }
  5. 观众A、B、C开始观看直播(即播放混流地址)。

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

    1. self.mediaPlayerCall = [[AlivcVideoChatParter alloc] init];
    2. // 播放通知
    3. [self addVideoChatObserver];
    4. // 播放参数配置
    5. self.playerParam = [[NSMutableDictionary alloc] init];
    6. NSNumber *dropBufferDuration = [[NSNumber alloc] initWithInt:2000];
    7. ......
    8. [self.playerParam setObject:downloadTimeOut forKey:ALIVC_PLAYER_PARAM_DOWNLOADIMEOUT];
    9. [self.mediaPlayerCall setPlayerParam:self.playerParam];
    10. // 开始播放
    11. int err = [self.mediaPlayerCall startToPlay:[NSURL URLWithString:self.playUrl] view:self.liveRoomView.mediaPalyerView];
  6. 通知AppServer,用户A、B、C正在观看。

    (客户端代码:LiveRoomViewController.m文件中)

    1. [SendMessageManager watchLive:self.userUid roomId:self.roomId block:^(MNSInfoModel *mnsInfo, NSError *error) {
    2. }];
本文导读目录
本文导读目录
以上内容是否对您有帮助?