love_php/app/Models/TeamLive.php
2026-04-02 09:20:51 +08:00

218 lines
6.7 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use App\Http\Response\ResponseJson;
use App\Services\IMService;
class TeamLive extends Model
{
use ResponseJson;
protected $fillable = [];
protected $guarded = [];
public function clickNumKey()
{
return 'team_live_'.$this->team_id.'_click_num';
}
public function liveLogs()
{
return $this->hasOne(TeamLiveLog::class, 'live_id', 'id');
}
/**
* 主播
*/
public function anchor()
{
return $this->belongsTo(User::class, 'user_id');
}
/**
* 创建聊天室
* @param [type] $creator [description]
* @param [type] $name [description]
* @param [type] $announcement [description]
* @return [type] [description]
*/
public function createChatRoom($creator, $name, $announcement='', $broadcasturl='', $ext=[], $queuelevel=0)
{
try {
// $im = new IMService(env('IM_APP_KEY'), env('IM_APP_SECRET'));
// $result = $im->chatRoomCreate($creator, $name, $announcement, $broadcasturl, $ext, $queuelevel);
// if (empty($result) || $result['code'] != 200) {//失败
// throw new \Exception("创建IM聊天室请求失败", 1);
// }
// return $result['chatroom'];
return '';
} catch (\Exception $e) {
$this->getError($e);
return false;
}
}
public function channel()
{
return $this->hasOne(TeamLiveChannel::class, 'live_id', 'id');
}
/**
* 创建直播流地址
* @param [type] $name [description]
* @param [type] $type [description]
* @return [type] [description]
*/
public function createLiveChannel($name)
{
try {
//判断数据是否有对应流
$channel = $this->channel;
if (empty($channel)) {
$im = new IMService();
$result = $im->channelCreate($name,0);
if (empty($result) || $result['code'] != 200) {//失败
throw new \Exception("创建直播流失败", 1);
}
$ret = $result['ret'];
//创建直播流
$channel = $this->channel()->create([
'name'=>$name,
'push_url'=>$ret['pushUrl'],
'http_pull_url'=>$ret['httpPullUrl'],
'hls_pull_url'=>$ret['hlsPullUrl'],
'rtmp_pull_url'=>$ret['rtmpPullUrl'],
'cid'=>$ret['cid']
]);
}
return $channel;
} catch (\Exception $e) {
$this->getError($e);
return false;
}
}
/**
* [createChannel 创建音视频房间]
* @param [type] $type [description]
* @param [type] $channelName [description]
* @param [type] $accid [description]
* @param integer $webrtc [description]
* @param array $selfconfig [description]
* @param array $roomconfig [description]
* @return [type] [description]
*/
public function createChannelRoom($type, $channelName, $accid=null, $webrtc=0, $selfconfig=[], $roomconfig='', $version=1)
{
try {
$im = new IMService(env('IM_APP_KEY'), env('IM_APP_SECRET'));
switch ($version){
case 1:
$result = $im->createChannel($type, $channelName, $accid, $webrtc, $selfconfig, $roomconfig);
break;
case 2:
$result = $im->createChannelV2($channelName, $accid);
break;
default:
$result = '';
break;
}
if (empty($result) || $result['code'] != 200) {//失败
throw new \Exception("创建音视频房间请求失败", 1);
}
return $result['cid'];
} catch (\Exception $e) {
$this->getError($e);
return false;
}
}
public function channelRoom()
{
try {
if (empty($this->channel_id)) return false;
$im = new IMService();
if($this->version == 1){
$result = $im->liveRoom($this->channel_id);
}else{
$result = $im->liveRoomV2($this->channel_id);
}
if (is_array($result) && isset($result['cid'])) {
return $result;
}
return false;
} catch (\Exception $e) {
$this->getError($e);
return false;
}
}
/**
* [addRtmpTask 增加一个房间推流任务]
* @param [type] $channelName [description]
*/
public function addRtmpTask($mian_user_id)
{
try {
$live_name = $this->name;
$channel = TeamLiveChannel::where('live_id', $this->id)->first();
if (empty($channel)) throw new \Exception("直播频道不存在", 1);
//加入直播间 todo
$task = [
"taskId"=>$channel->cid,
"streamUrl"=>$channel->push_url,
"layoutMode"=>"M-0",
"record"=>true,
"accid"=>$this->user_id,
];
//增加一个房间推流任务
$im = new IMService();
if($this->version == 2){
$result = $im->addRtmpTaskV2($live_name, $this->channel_id, $task);
}else{
$task = json_encode($task, JSON_UNESCAPED_UNICODE);
$result = $im->addRtmpTask($live_name, 0, $task);
}
if (empty($result) || $result['code'] != 200) {//失败
throw new \Exception("增加一个房间推流任务失败", 1);
}
return true;
} catch (\Exception $e) {
$this->getError($e);
return false;
}
}
public function getRtmpTask()
{
$live_name = $this->name;
$channel = $this->channel;
if (empty($channel)) return false;
try {
//查看房间推流
$im = new IMService();
if($this->version == 1){
$result = $im->getRtmpTask($live_name, $type=0, $channel->cid);
}else{
$result = $im->getRtmpTaskV2($live_name, $this->channel_id, $channel->cid);
}
if (empty($result) || $result['code'] == 404) {//失败
return false;
}elseif (empty($result) || ($result['code'] != 200 && $result['code'] != 404)) {
throw new \Exception("查看单个房间推流任务失败", 1);
}
return $result['rtmpTask'];
} catch (\Exception $e) {
$this->getError($e);
return false;
}
}
}