<?php
// +----------------------------------------------------------------------
// | likeadmin快速开发前后端分离管理后台(PHP版)
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
// | 开源版本可自由商用,可去除界面版权logo
// | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
// | github下载:https://github.com/likeshop-github/likeadmin
// | 访问官网:https://www.likeadmin.cn
// | likeadmin团队 版权所有 拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeadminTeam
// +----------------------------------------------------------------------

namespace app\api\controller;

use think\facade\Db;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Exception\GuzzleException;
use Guzzle\Http\Exception\RequestException;

use Darabonba\OpenApi\OpenApiClient;
use AlibabaCloud\OpenApiUtil\OpenApiUtilClient;
use Darabonba\OpenApi\Models\Config;
use Darabonba\OpenApi\Models\Params;
use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
use Darabonba\OpenApi\Models\OpenApiRequest;

/**
 * 阿里云直播
 * Class ZhiboController
 */
class ZhiboController extends BaseApiController
{
    public array $notNeedLogin = ['createPushUrl', 'getPullUrl', 'forbidLiveStream', 'resumeLiveStream', 'test'];

    private $access_key_id = 'LTAI5t7mhH3ij2cNWs1zhPmv';
    private $access_key_secret = 'gqo2wMpvi8h5bDBmCpMje6BaiXvcPu';

    // 推流host
    private $push_host = 'rtmp://push-live.lihaink.cn';
    // 拉流host
    private $pull_host = 'rtmp://live.lihaink.cn';
    // URL推流鉴权key
    private $push_auth_key = '5WRlJV0QxK731AJe';
    // URL拉流鉴权key
    private $pull_auth_key = '59YEMIXP9260MZBe';

    public function createPushUrl()
    {
        $uri = $this->push_host . '/app/ceshi';
        $key = $this->push_auth_key;
        $exp = time() + 3600;
        $authurl = $this->authKey($uri, $key, $exp);
        return $this->data(['push_url'=>$authurl]);
    }

    public function getPullUrl()
    {
        $uri = $this->pull_host . '/app/ceshi';
        $key = $this->pull_auth_key;
        $exp = time() + 3600;
        $authurl = $this->authKey($uri, $key, $exp);
        return $this->data(['push_url'=>$authurl]);
    }

    private function authKey($uri, $key, $exp) 
    {
        preg_match("/^(rtmp:\/\/)?([^\/?]+)?(\/[^?]*)?(\\?.*)?$/", $uri, $matches);
        $scheme = $matches[1] ?? '';
        $host = $matches[2] ?? '';
        $path = $matches[3] ?? '';
        if  (empty($scheme)) {
            $scheme ="rtmp://";
        }
        if  (empty($path)) {
            $path ="/";
        }
        $uid = $rand = "0";
        $string = sprintf("%s-%u-%s-%s-%s", $path, $exp, $rand, $uid, $key);
        $hashvalue = md5($string);
        $auth_key = sprintf("%u-%s-%s-%s", $exp, $rand, $uid, $hashvalue);
        return sprintf("%s%s%s?auth_key=%s", $scheme, $host, $path, $auth_key);
    }

    public function forbidLiveStream()
    {
        $config = new Config([
            "accessKeyId"     => $this->access_key_id,
            "accessKeySecret" => $this->access_key_secret
        ]);
        $config->endpoint = "live.aliyuncs.com";
        $client = new OpenApiClient($config);
        $params = new Params([
            "action" => "ForbidLiveStream",
            "version" => "2016-11-01",
            "protocol" => "HTTP",
            "method" => "POST",
            "authType" => "AK",
            "style" => "RPC",
            "pathname" => "/",
            "reqBodyType" => "json",
            "bodyType" => "json"
        ]);
        
        $queries = [];
        $queries["DomainName"] = "push-live.lihaink.cn";
        $queries["AppName"] = "app";
        $queries["StreamName"] = "ceshi";
        $queries["LiveStreamType"] = "publisher";
        
        $runtime = new RuntimeOptions([]);
        $request = new OpenApiRequest([
            "query" => OpenApiUtilClient::query($queries)
        ]);

        $res = $client->callApi($params, $request, $runtime);
        halt($res);
    }

    public function resumeLiveStream()
    {
        $config = new Config([
            "accessKeyId"     => $this->access_key_id,
            "accessKeySecret" => $this->access_key_secret
        ]);
        $config->endpoint = "live.aliyuncs.com";
        $client = new OpenApiClient($config);
        $params = new Params([
            "action" => "ResumeLiveStream",
            "version" => "2016-11-01",
            "protocol" => "HTTP",
            "method" => "POST",
            "authType" => "AK",
            "style" => "RPC",
            "pathname" => "/",
            "reqBodyType" => "json",
            "bodyType" => "json"
        ]);
    
        $queries = [];
        $queries["DomainName"] = "push-live.lihaink.cn";
        $queries["LiveStreamType"] = "publisher";
        $queries["AppName"] = "app";
        $queries["StreamName"] = "ceshi";
        
        $runtime = new RuntimeOptions([]);
        $request = new OpenApiRequest([
            "query" => OpenApiUtilClient::query($queries)
        ]);

        $res = $client->callApi($params, $request, $runtime);
        halt($res);
    }

    public function test() {
        $config = new Config([
            "accessKeyId"     => $this->access_key_id,
            "accessKeySecret" => $this->access_key_secret
        ]);
        $config->endpoint = "live.aliyuncs.com";
        $client = new OpenApiClient($config);
        $params = new Params([
            "action" => "DescribeLiveStreamsOnlineList",
            "version" => "2016-11-01",
            "protocol" => "HTTP",
            "method" => "POST",
            "authType" => "AK",
            "style" => "RPC",
            "pathname" => "/",
            "reqBodyType" => "json",
            "bodyType" => "json"
        ]);
        $queries = [];
        $queries["DomainName"] = "live.lihaink.cn";
        
        $runtime = new RuntimeOptions([]);
        $request = new OpenApiRequest([
            "query" => OpenApiUtilClient::query($queries)
        ]);

        $res = $client->callApi($params, $request, $runtime);
        halt($res);
    }

}