问题说明:知识付费开源版本直播地址没有自动适配
修改文件:extend/Api/AliyunLive.php
修改方法:getPullSteam()
public function getPullSteam($site_url, $streamName, $vhost = '', $time = 1800, $type = 'm3u8')
{
$time = time() + $time;
$vhost = $vhost ? $vhost : $this->playLike;
$appName = $this->appName;
$privateKey = $this->payKey;
if ($site_url) {
$arr = parse_url($site_url);
if ($arr['scheme']) {
$scheme = $arr['scheme'];
} else {
$scheme = 'http';
}
} else {
$scheme = 'http';
}
$url = '';
switch ($type) {
case 'rtmp':
$host = 'rtmp://' . $vhost;
$url = "/{$appName}/{$streamName}";
break;
case 'flv':
$host = $scheme.'://' . $vhost;
$url = "/{$appName}/{$streamName}.flv";
break;
case 'm3u8':
$host = $scheme.'://' . $vhost;
$url = "/{$appName}/{$streamName}.m3u8";
break;
}
if ($privateKey) {
$auth_key = md5($url . '-' . $time . '-0-0-' . $privateKey);
$url = $host . $url . "?auth_key={$time}-0-0-{$auth_key}";
} else {
$url = $host . $url;
}
return $url;
}修改文件:application/wap/controller/Live.php
修改方法:index()
public function index($stream_name = '', $special_id = 0, $live_id = 0, $record_id = 0)
{
if ($special_id && $special_id != 0 && $live_id && $live_id != 0) {
$special_task = SpecialTask::where(['special_id' => $special_id, 'live_id' => $live_id])->value('live_id');
if (!$special_task) {
$this->failed('直播间不存在', Url::build('index/index'));
}
$stream_name = LiveStudio::where('id', $live_id)->value('stream_name');
}
if (!$stream_name) return $this->failed('缺少在直播间号!', Url::build('index/index'));
$liveInfo = LiveStudio::where('stream_name', $stream_name)->find();
if (!$liveInfo) return $this->failed('直播间不存在', Url::build('index/index'));
if ($liveInfo->is_del) return $this->failed('直播间已被删除', Url::build('index/index'));
$userInfo = LiveUser::setLiveUser($this->uid, $liveInfo->id);
if ($userInfo === false) return $this->failed(LiveUser::getErrorInfo('用户写入不成功'), Url::build('index/index'));
$specialLive = Special::where(['is_show' => 1, 'is_del' => 0, 'id' => $liveInfo->special_id])->find();
if (!$specialLive) return $this->failed('专题不存在或者已被删除', Url::build('index/index'));
$user_level = !$this->uid ? 0 : User::where('uid', $this->uid)->find();
if ($specialLive->pay_type == 1 && !SpecialBuy::PaySpecial($specialLive->id, $this->uid)) {
if ($specialLive->member_pay_type == 1 || ($user_level['level'] <= 0 && $specialLive->member_pay_type == 0)) {
return $this->failed('您还没有支付请支付后再进行观看', Url::build('special/details', ['id' => $liveInfo->special_id]));
}
}
if ($specialLive->pay_type == 2) {
$cookie_value = Cookie::get($stream_name . "studio_pwd");
if (!$cookie_value || $cookie_value != $liveInfo['studio_pwd']) {
return $this->failed('您需要先获得密码后再进行观看', Url::build('special/details', ['id' => $liveInfo->special_id]));
}
}
$AliyunLive = $this->aliyunLive;
if ($liveInfo->is_play){
$site_url = SystemConfigService::get('site_url');
$PullUrl = $AliyunLive->getPullSteam($site_url, $liveInfo->stream_name);
} else {
$record_id = $record_id ? $record_id : $liveInfo->playback_record_id;
if ($liveInfo->is_playback && $record_id) {
$livePlayBack = LivePlayback::where(['RecordId' => $record_id, 'stream_name' => $liveInfo->stream_name])->find();
$PullUrl = $livePlayBack ? $livePlayBack->playback_url : false;
} else
$PullUrl = false;
}
$live_status = 0;
$datatime = strtotime($liveInfo->start_play_time);
$endTime = strtotime($liveInfo->stop_play_time);
if ($datatime < time() && $endTime > time())
$live_status = 1;//正在直播
else if ($datatime < time() && $endTime < time())
$live_status = 2;//直播结束
else if ($datatime > time())
$live_status = 0;//尚未直播
$user_type = LiveHonouredGuest::where(['uid' => $this->uid, 'live_id' => $liveInfo->id])->value('type');
if (is_null($user_type)) $user_type = 2;
$uids = LiveHonouredGuest::where(['live_id' => $liveInfo->id])->column('uid');
$liveInfo['abstract'] = $specialLive->abstract;
$this->assign([
'goldInfo' => json_encode(SystemConfigService::more("gold_name,gold_rate,gold_image")),
'liveInfo' => json_encode($liveInfo),
'UserSum' => bcadd(LiveUser::where(['live_id' => $liveInfo->id, 'is_open_ben' => 0, 'is_online' => 1])->sum('visit_num'), $liveInfo->online_num, 0),
'live_title' => $liveInfo->live_title,
'PullUrl' => $PullUrl,
//'requirejs' => true,
'is_ban' => $userInfo->is_ban,
'room' => $liveInfo->id,
'datatime' => $datatime,
'workerman' => json_encode(Config::get('workerman.chat', [])),
'phone_type' => UtilService::getDeviceType(),
'live_status' => $live_status,
'user_type' => $user_type,
'OpenCommentCount' => LiveBarrage::where(['live_id' => $liveInfo->id, 'is_show' => 1])->count(),
'OpenCommentTime' => LiveBarrage::where(['live_id' => $liveInfo->id, 'is_show' => 1])->order('add_time asc')->value('add_time'),
'CommentCount' => LiveBarrage::where(['live_id' => $liveInfo->id, 'is_show' => 1])->where('uid', 'in', $uids)->count(),
'CommentTime' => LiveBarrage::where(['live_id' => $liveInfo->id, 'is_show' => 1])->where('uid', 'in', $uids)->order('add_time asc')->value('add_time'),
]);
return $this->fetch();
}修改文件:application/admin/model/live/LiveStudio.php
修改方法:setLivePalyStatus()
public static function setLivePalyStatus($StreamName, $is_play, $action)
{
$liveInfo = self::where('stream_name', $StreamName)->find();
if (!$liveInfo) return self::setErrorInfo('暂未查到直播间');
live_log('log', ['title'=>'开始','liveInfo'=>$liveInfo->toArray(),'streamName'=>$StreamName,'is_play'=>$is_play]);
if ($liveInfo->is_play == 1 && $action == 'publish') {
return true;
} else if ($liveInfo->is_play == 0 && $action == 'publish_done') {
return true;
}
$liveInfo->is_play = $is_play;
try {
$aliyunLive = \Api\AliyunLive::instance([
'AccessKey' => SystemConfigService::get('accessKeyId'),
'AccessKeySecret' => SystemConfigService::get('accessKeySecret'),
'OssEndpoint' => SystemConfigService::get('aliyun_live_end_point'),
'OssBucket' => SystemConfigService::get('aliyun_live_oss_bucket'),
'appName' => SystemConfigService::get('aliyun_live_appName'),
'payKey' => SystemConfigService::get('aliyun_live_play_key'),
'key' => SystemConfigService::get('aliyun_live_push_key'),
'playLike' => SystemConfigService::get('aliyun_live_playLike'),
'rtmpLink' => SystemConfigService::get('aliyun_live_rtmpLink'),
]);
if ($liveInfo->is_play) {
//开启自动录制,进行手开启动录制直播间
if ($liveInfo->is_recording) {
$res = $aliyunLive->liveRecording($liveInfo->stream_name)->executeResponse();
live_log('log', ['title'=>'直播录制开启','res'=>$res,'error'=>$aliyunLive->getErrorInfo()]);
if ($res) $liveInfo->recording = 1;
}
//向所有在线用户广播直播地址
$workerman = \think\Config::get('workerman.channel', []);
Gateway::$registerAddress = $workerman['ip'] . ':' . $workerman['port'];
if (Gateway::getClientIdCountByGroup($liveInfo->id)) {
$site_url = SystemConfigService::get('site_url');
Gateway::sendToGroup($liveInfo->id, json_encode([
'type' => 'live_ing',
'pull_url' => $aliyunLive->getPullSteam($site_url, $liveInfo->stream_name),
]));
}
} else {
//开启自动录制,进行手动停止录制直播间
if ($liveInfo->recording) {
$res = $aliyunLive->liveRecording($liveInfo->stream_name, false)->executeResponse();
live_log('log', ['title'=>'直播录制关闭','res'=>$res,'error'=>$aliyunLive->getErrorInfo()]);
$liveInfo->recording = 0;
}
//向所有在线用户广播直播地址
$workerman = \think\Config::get('workerman.channel', []);
Gateway::$registerAddress = $workerman['ip'] . ':' . $workerman['port'];
if (Gateway::getClientIdCountByGroup($liveInfo->id)) {
Gateway::sendToGroup($liveInfo->id, json_encode([
'type' => 'live_end',
'pull_url' => false,
]));
}
}
} catch (\Exception $e) {
live_log('error', ['code' => $e->getCode(), 'line' => $e->getLine(), 'msg' => $e->getMessage()]);
if ($liveInfo->recording) $liveInfo->recording = 0;
self::setErrorInfo($e->getMessage());
}
return $liveInfo->save();
}
