VideoApe.js 3.5 KB
// //////////////////////////////////////////////////////////////////////////////
//
//  Copyright (C) 2016-present  All Rights Reserved.
//  Licensed under the Apache License, Version 2.0 (the "License");
//  http://www.apache.org/licenses/LICENSE-2.0
//
//  Github Home: https://github.com/AlexWang1987
//  Author: AlexWang
//  Date: 2016-08-23 18:07:28
//  QQ Email: 1669499355@qq.com
//  Last Modified time: 2016-09-06 11:13:59
//  Description: LiveClass-VideoApe
//
// //////////////////////////////////////////////////////////////////////////////

import Ape from './Ape';
import ApeConsts from './ApeConsts';
import pdu from 'pdu';
import Loger from 'Loger';
import MessageTypes from 'MessageTypes';

let loger = Loger.getLoger('VideoChat');

class VideoChat extends Ape {
  constructor() {
    super(
      ApeConsts.VIDEO_SESSION_ID,
      ApeConsts.VIDEO_SESSION_NAME,
      ApeConsts.VIDEO_SESSION_TAG


    );

    //Attributes
    this.videoChannels = {};
    this.activeChannelId = 0;
    this.activeURL = '';


    // Ape Models
    this.registerKey(this._session_id, this._session_name, this._session_tag, new ArrayBuffer);
    this.registerObj(pdu.RCPDU_REG_REGISTER_TABLE, ApeConsts.VIDEO_OBJ_TABLE_ID, ApeConsts.VIDEO_OBJ_TABLE_NAME, ApeConsts.VIDEO_OBJ_TABLE_TAG, 0, new ArrayBuffer);


    // ape listeners
    this.on(pdu.RCPDU_VIDEO_SEND_DATA_REQUEST, this.videoIncomingHandler.bind(this));
  }

  // 视频消息处理
  videoIncomingHandler(videoBuffer) {
    let videoReceivePdu = pdu['VideoSendDataRequestPdu'].decode(videoBuffer);

    let video_data = { };
    video_data._initiator = videoReceivePdu.initiator;
    video_data._is_key_frame = videoReceivePdu.keyFrame;
    video_data._sequence_id = videoReceivePdu.sequenceId;
    video_data._slice_id = videoReceivePdu.sliceId;
    video_data._data = videoReceivePdu.userData;


    // this._notify(RCApeEvent.E_VIDEO_DATA, videoReceivePdu.sessionId, videoReceivePdu.channelId, video_data);
    loger.log('接受视频消息.', video_data);
    this._emit(MessageTypes.VIDEO_SHOW, video_data);

  }

  tableUpdateHandler(ownerId, channelId, channelInfo) {
   // debugger;
    let videoChannelInfo = pdu['RCVideoChannelInfoRecordPdu'].decode(channelInfo);
    videoChannelInfo.owner = ownerId;
    videoChannelInfo.channelId = channelId;
    videoChannelInfo.status = ownerId === 0 ? ApeConsts.CGS_RELEASED : videoChannelInfo.status;

    this.videoChannels[channelId] = videoChannelInfo;
    switch (videoChannelInfo.status) {
    case ApeConsts.CGS_RELEASED:
    case ApeConsts.CGS_PENDING:
    case ApeConsts.CGS_GRABBING:
      // 只能关闭自己的流
      if (this.activeChannelId === videoChannelInfo.channelId) {
        this.activeChannelId = 0;
        this.activeURL = '';
        this.emitVideoChange();
      }
      break;
    case ApeConsts.CGS_OPENNED:
      this.activeChannelId = videoChannelInfo.channelId;
      // AMS
      if (this._confInfo.msType === '1') {

            this.activeURL = `http://dazhi.3mang.com/live/${this._confInfo.confId}/${this._confInfo.confId}_${videoChannelInfo.channelId}_flash_cam_mic_aac/playlist.m3u8`;


      }else {
           this.activeURL = `http://hls.3mang.com/live/${this._confInfo.confId}_${videoChannelInfo.channelId}_flash_cam_mic_aac/playlist.m3u8`;
      }
      // 任何人都可以打开流
      this.emitVideoChange();
      break;
    }
  }

  emitVideoChange() {
    this._emit(MessageTypes.VIDEO_SHOW, {
      activeChannelId: this.activeChannelId,
      HLSURL: this.activeURL,
    });
  };

}

export default VideoChat;