Пример #1
0
 function show()
 {
     //最多提前10秒取出切播计划
     $sql = "SELECT ccp.id, ccp.channel_id, ccp.stream_uri, ccp.type, ccp.channel2_id, (ccp.change_time + ccp.toff) AS resume_time, c.chg_id FROM " . DB_PREFIX . "channel_chg_plan ccp \r\n\t\t\t\tLEFT JOIN " . DB_PREFIX . "channel c\r\n\t\t\t\t\tON ccp.channel_id = c.id\r\n\t\t\t\tWHERE c.id > 0 AND ccp.is_exec=0 AND ccp.change_time <= " . (TIMENOW - 10);
     $q = $this->db->query($sql);
     include CUR_CONF_PATH . 'lib/channel_change.class.php';
     $channel_changes = new ChannelChange();
     $chgids = array();
     while ($row = $this->db->fetch_array($q)) {
         $ret_name = $channel_changes->channel_emergency_change($row['chg_id'], 'file', $row['stream_uri']);
         $chgids[] = $row['id'];
         if ($row['type'] == 3 || $row['type'] == 1) {
             $chg_type = array(3 => 'stream', 1 => 'file');
             $sql = "UPDATE " . DB_PREFIX . "channel SET chg2_stream_id={$row['channel2_id']}, chg_type='{$chg_type[$row['type']]}' WHERE id=" . $row['channel_id'];
             $this->db->query($sql);
         }
         $sql = 'REPLACE INTO ' . DB_PREFIX . "channel_change (channel_id, resume_time) VALUES ({$row['channel_id']}, {$row['resume_time']})";
         $this->db->query($sql);
         $this->addItem($row);
     }
     if ($chgids) {
         $sql = 'UPDATE ' . DB_PREFIX . 'channel_chg_plan SET is_exec=1 WHERE id IN (' . implode(',', $chgids) . ')';
         $this->db->query($sql);
     }
     $sql = "SELECT cc.channel_id, c.chg_id FROM " . DB_PREFIX . "channel_change cc \r\n\t\t\t\tLEFT JOIN " . DB_PREFIX . "channel c\r\n\t\t\t\t\tON cc.channel_id = c.id\r\n\t\t\t\tWHERE c.id > 0 AND cc.resume_time <= " . (TIMENOW - 5);
     $q = $this->db->query($sql);
     $chgids = array();
     while ($row = $this->db->fetch_array($q)) {
         $ret_name = $channel_changes->channel_resume($row['chg_id']);
         $chgids[] = $row['channel_id'];
         if ($row['type'] == 3 || $row['type'] == 1) {
             $sql = "UPDATE " . DB_PREFIX . "channel SET chg2_stream_id=0 WHERE id=" . $row['channel_id'];
             $this->db->query($sql);
         }
         $this->addItem($row);
     }
     if ($chgids) {
         $sql = 'UPDATE ' . DB_PREFIX . 'channel_change SET is_exec=1 WHERE channel_id IN (' . implode(',', $chgids) . ')';
         $this->db->query($sql);
     }
     $this->output();
 }
Пример #2
0
 public function emergency_change()
 {
     $channel_id = intval($this->input['channel_id']);
     if (!$channel_id) {
         $this->errorOutput('请选择要切播的频道');
     }
     $stream_id = intval($this->input['stream_id']);
     $source = trim(urldecode($this->input['source']));
     $bkfile_id = intval($this->input['bkfile_id']);
     if (!$stream_id && !$source) {
         $this->errorOutput('请选择要切播到的流或备播文件');
     }
     $sql = "select main_stream_name,stream_id,live_delay,is_live,stream_state, chg_id, chg2_stream_id from " . DB_PREFIX . "channel where id=" . $channel_id;
     $channel_info = $this->db->query_first($sql);
     if (!$channel_info['stream_state']) {
         $this->errorOutput('频道尚未对外输出流');
     }
     if (!$channel_info['is_live']) {
         $this->errorOutput('该频道不支持播出控制');
     }
     $sql = "select id, ch_name,other_info,s_status from " . DB_PREFIX . "stream where id IN (" . $channel_info['stream_id'] . ',' . $stream_id . ')';
     $q = $this->db->query($sql);
     $stream_info = array();
     while ($r = $this->db->fetch_array($q)) {
         if (!$r['s_status']) {
             continue;
         }
         $r['other_info'] = unserialize($r['other_info']);
         $stream_info[$r['id']] = $r;
     }
     $live_stream_info = $stream_info[$channel_info['stream_id']]['other_info'];
     if (!$live_stream_info) {
         $this->errorOutput('频道尚未对外输出流');
     }
     $chg_type = 'stream';
     $beibo_stream_info = $stream_info[$stream_id]['other_info'];
     if (!$beibo_stream_info) {
         if (!$source) {
             $this->errorOutput('切播信号未启动,无法切播');
         } else {
             $chg_type = 'file';
         }
     }
     //与主信号流一致,回到直播
     if ($stream_id == $channel_info['stream_id']) {
         include CUR_CONF_PATH . 'lib/channel_change.class.php';
         $channel_changes = new ChannelChange();
         $ret = $channel_changes->channel_resume($channel_info['chg_id']);
         if ($ret != 1) {
             $msg = array(-1 => '无法连接切播服务', 0 => '切播失败');
             $this->errorOutput($msg[$ret_name]);
         }
         $sql = "UPDATE " . DB_PREFIX . "channel SET chg2_stream_id=0 WHERE id=" . $channel_id;
         $this->db->query($sql);
         $ret = array('msg' => $ret_name, 'stream_id' => $stream_id, 'chg_type' => $chg_type);
         $this->addItem($ret);
         $this->output();
     }
     if ($chg_type == 'stream' && $stream_id == $channel_info['chg2_stream_id']) {
         $ret = array('msg' => $ret_name, 'stream_id' => $stream_id, 'chg_type' => $chg_type);
         $this->addItem($ret);
         $this->output();
     }
     if ($channel_info['chg2_stream_id'] && $stream_id != $channel_info['chg2_stream_id']) {
         $this->errorOutput('频道正在切播中, 请先回到直播');
     }
     //匹配码流一致
     $stream_to = array();
     foreach ($live_stream_info as $key => $value) {
         if ($value['name'] != $channel_info['main_stream_name']) {
             continue;
         }
         $stream_to[$value['name']] = $source;
         if ($beibo_stream_info) {
             foreach ($beibo_stream_info as $k => $v) {
                 if (abs($value['bitrate'] - $v['bitrate']) < 200) {
                     $stream_to[$value['name']] = hg_get_stream_url($this->settings['tvie']['up_stream_server'], array('channel' => $stream_info[$stream_id]['ch_name'], 'stream_name' => $v['name']));
                     break;
                 }
             }
         }
     }
     if (0 && $channel_info['live_delay']) {
         //有延时就记录数据
         $stream_uri = array();
         foreach ($stream_name_beibo as $key => $value) {
             $stream_uri[] = hg_get_stream_url($this->settings['tvie']['up_stream_server'], array('channel' => $beibo['ch_name'], 'stream_name' => $value), 'channels', 'tvie://');
         }
         $change_time = TIMENOW + $channel_info['live_delay'] * 60;
         include CUR_CONF_PATH . 'lib/channel_change_plan.class.php';
         $channel_plan = new ChannelChangePlan();
         $ret = $channel_plan->change_plan_create($channel_id, $stream_id, $stream_name_down, $stream_uri, $channel_info['stream_state'], $change_time, '', '');
     } else {
         if (!$stream_to[$channel_info['main_stream_name']]) {
             $this->errorOutput('未找到匹配的流');
         }
         include CUR_CONF_PATH . 'lib/channel_change.class.php';
         $channel_changes = new ChannelChange();
         $ret_name = $channel_changes->channel_emergency_change($channel_info['chg_id'], 'file', $stream_to[$channel_info['main_stream_name']]);
         if ($ret_name != 1) {
             $msg = array(-1 => '无法连接切播服务', 0 => '切播失败');
             $this->errorOutput($msg[$ret_name]);
         }
         $sql = "UPDATE " . DB_PREFIX . "channel SET chg2_stream_id=" . ($stream_id ? $stream_id : $bkfile_id) . ", chg_type='{$chg_type}' WHERE id=" . $channel_id;
         $this->db->query($sql);
     }
     $ret = array('msg' => $ret_name, 'stream_id' => $stream_id, 'chg_type' => $chg_type);
     $this->addItem($ret);
     $this->output();
 }