love_php/app/Http/Controllers/Admin/Aliyun.php
2026-04-02 09:20:51 +08:00

244 lines
7.7 KiB
PHP

<?php
namespace App\Http\Controllers\Admin;
use App\Models\Live\Live;
use App\Models\Live\Video;
use App\Models\MediaLibraryMedia;
use App\Models\PayLog;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Str;
class Aliyun
{
const API_URL = 'https://live.aliyuncs.com';
const VERSION = '2016-11-01';
const SIGN_METHOD = 'HMAC-SHA1';
const SIGN_VERSION = '1.0';
const FORMAT = 'json';
public $appName;
public $accessKey;
public $accessSecret;
public function __construct()
{
$this->appName = config('live.aliyun.app_name');
$this->accessKey = config('live.aliyun.access_key');
$this->accessSecret = config('live.aliyun.access_secret');
}
public function pause($cid, $vid)
{
$stream = $this->getStreamName($cid, $vid);
$params = [
'Action' => 'ForbidLiveStream',
'AppName' => $this->appName,
'DomainName' => config('live.aliyun.push_url'),
'LiveStreamType' => 'publisher',
'StreamName' => $stream,
'Oneshot' => 'yes',
];
$this->request('GET', $params);
}
public function resume($cid, $vid)
{
$stream = $this->getStreamName($cid, $vid);
$params = [
'Action' => 'ResumeLiveStream',
'AppName' => $this->appName,
'DomainName' => config('live.aliyun.push_url'),
'LiveStreamType' => 'publisher',
'StreamName' => $stream,
];
$this->request('GET', $params);
}
public function getPushUrl($cid, $vid)
{
$stream = $this->getStreamName($cid, $vid);
$uri = '/' . $this->appName . '/' . $stream;
$url = 'rtmp://' . config('live.aliyun.push_url') . $uri . '?auth_key=' . $this->getPushAuthKey($uri);
return $url;
}
public function getPlayUrl($cid, $vid)
{
$stream = $this->getStreamName($cid, $vid);
$uri = '/' . $this->appName . '/' . $stream . '.flv';
$key = $this->getPlayAuthKey($uri);
// $url = (request()->isSecure() ? 'https' : 'http') . '://' . config('live.aliyun.pull_url') . $uri . '?auth_key=' . $key;
$url = 'https://' . config('live.aliyun.pull_url') . $uri . '?auth_key=' . $key;
return $url;
}
public function getHlsPlayUrl($cid, $vid)
{
$stream = $this->getStreamName($cid, $vid);
$uri = '/' . $this->appName . '/' . $stream . '.m3u8';
$key = $this->getPlayAuthKey($uri);
// $url = (request()->isSecure() ? 'https' : 'http') . '://' . config('live.aliyun.pull_url') . $uri . '?auth_key=' . $key;
$url = 'https://' . config('live.aliyun.pull_url') . $uri . '?auth_key=' . $key;
return $url;
}
protected function getStreamName($cid, $vid)
{
return sprintf('c_%d_v_%d', $cid, $vid);
}
protected function getPushAuthKey($uri)
{
$time = time() + 3600 * 5;
$rand = Str::random(6);
$uid = 0;
$authKey = config('live.aliyun.push_key');
$key = md5($uri . '-' . $time . '-' . $rand . '-' . $uid . '-' . $authKey);
return $time . '-' . $rand . '-' . $uid . '-' . $key;
}
protected function getPlayAuthKey($uri)
{
$time = time() + 3600 * 5;
$rand = Str::random(32);
$uid = 0;
$authKey = config('live.aliyun.pull_key');
$key = md5($uri . '-' . $time . '-' . $rand . '-' . $uid . '-' . $authKey);
return $time . '-' . $rand . '-' . $uid . '-' . $key;
}
protected function request($method, $params)
{
function utc_time()
{
date_default_timezone_set('UTC');
$timestamp = new \DateTime();
return $timestamp->format("Y-m-d\TH:i:s\Z");
}
$commonParams = [
'Version' => self::VERSION,
'AccessKeyId' => $this->accessKey,
'SignatureMethod' => self::SIGN_METHOD,
'Timestamp' => utc_time(),
'SignatureVersion' => self::SIGN_VERSION,
'SignatureNonce' => mt_rand(1, 10000),
'Format' => self::FORMAT,
];
$params = array_merge($commonParams, $params);
// 字典排序
ksort($params);
// 编码转换
$str = [];
foreach ($params as $key => $val) {
if ($key === 'Timestamp') {
$val = urlencode($val);
}
$str[] = $key . '=' . $val;
}
$str = implode('&', $str);
$str = urlencode($str);
$str = $method . '&%2F&' . $str;
$str = str_replace('%3A', '%253A', $str);
$params['Signature'] = base64_encode(hash_hmac('sha1', $str, $this->accessSecret . '&', true));
$response = $this->geturl($params);
}
protected function geturl($data)
{
$headerArray = [
"Content-type:application/x-www-form-urlencoded;"
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, self::API_URL . '?' . http_build_query($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headerArray);
$output = curl_exec($ch);
curl_close($ch);
$output = json_decode($output, true);
return $output;
}
//开启录制状态回调
public function statusCallback(){
$headerArray = [
"Content-type:application/x-www-form-urlencoded;"
];
$data = [
'Action' => 'AddLiveRecordNotifyConfig',
'DomainName' => config('live.aliyun.push_url'),
'NotifyUrl' => 'http://alpha.ufutx.net/api/official/live/record/status/notify',
'NeedStatusNotify' => true
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, self::API_URL . '?' . http_build_query($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headerArray);
$output = curl_exec($ch);
curl_close($ch);
$output = json_decode($output, true);
return $output;
}
//录播上传到点播系统转码完成
public function StreamTranscodeComplete($request){
$video = Video::where('videoid', $request['VideoId'])->first();
if($video){
$video->uri = $request['FileUrl'];
$video->save();
$live = Live::find($video->live_id);
if ($live->status == 4) {
$live->status = 2;
$live->save();
}
}
//媒体库调用
$this->TranscodeComplete($request);
}
public function AddLiveRecordVideoComplete($request){
$is_set = Video::where('videoid', $request['VideoId'])->count();
if(!$is_set) {
$stream = $request['StreamName'];
$arr = explode('_', $stream);
$video = New Video();
$video->live_id = $arr[3];
$video->uri = '';
$video->videoid = $request['VideoId'];
$video->duration = 0;
$video->save();
$live = Live::find($arr[3]);
if ($live->status == 3) {
$live->status = 4;
$live->save();
}
}
}
/**
* 视视转码完成回调
* @param $request
*/
public function TranscodeComplete($request){
$video_id = $request['VideoId'];
if($request['Status'] == 'success'){
if(!empty($request['FileUrl'])){
$media = MediaLibraryMedia::where('video_id',$video_id)->get()->toArray();
if($media){
MediaLibraryMedia::where('video_id',$video_id)
->update(['transcode_url' => $request['FileUrl']]);
}
}
}
}
}