<?php
namespace Live\Lib;

use Dever;

class Cron
{
    public function getStream_api()
    {

    }

    public function getUrl_api()
    {

    }

    # 定时获取 所有流的状态 获取推流地址、拉流地址、推流历史等
    public function getStream()
    {
        $data = Dever::db('live/stream')->state();

        if ($data) {
            foreach ($data as $k => $v) {
                $handle = new Handle();
                $data = $handle->get($v['id']);
            }
        }

        return 'yes';
    }

    # 定时获取 所有的直播视频的回放地址
    public function getUrl()
    {
        $video = Dever::config('base', 'live')->video;
        $data = Dever::db($video['table'])->state();

        if ($data) {
            $time = time();
            foreach ($data as $k => $v) {
                $state = false;
                if ($v[$video['url_m3u8']]) {
                    $url = explode('/', $v[$video['url_m3u8']]);
                    $index = count($url)-1;
                    $date = explode('_', str_replace('.m3u8', '', $url[$index]));
                    if (isset($date[0]) && isset($date[1])) {
                        if ($date[0] != $v[$video['sdate']]) {
                            $state = true;
                        }
                        if ($date[1] != $v[$video['edate']]) {
                            $state = true;
                        }
                    }
                    
                } else {
                    $state = true;
                }
                if ($v[$video['stream_id']] > 0 && $state && $v[$video['sdate']] > 0 && $v[$video['edate']] > 0 && $time >= $v[$video['edate']]) {
                    $info = Dever::db('live/stream')->one($v[$video['stream_id']]);
                    $config = Dever::db('live/info')->one($info['live_id']);
                    $handle = new Handle();
                    $method = $handle->method($config);
                    $handle->createUrlOne($v, $video, $method, $config, $info);
                }
            }
        }

        return 'yes';
    }
}
