/** * 创建频道 (当tvie为open时开始创建,创建顺序:延时层-->切播层-->输出层-->本地) * @name create * @access public * @author lijiaying * @category hogesoft * @copyright hogesoft * @param $name string 频道名称 * @param $code string 台号 * @param $code_2 string 台号(修改用) * @param $logo string 台标 * @param $stream_id int 信号ID * @param $delay_id int 延时层ID * @param $chg_id int 切播层ID * @param $ch_id int 输出层ID * @param $save_time int 回看时间(小时) * @param $live_delay int 延时时间 (分钟) * @param $is_live tinyint 是否播控 (1-是 0-否) * @param $stream_name string 流名称 not null * @param $main_stream_name string 主信号名称 * @param $beibo string 备播信号 (暂最多支持2个) * @param $uri_in_num tinyint 输入流数目 * @param $uri_out_num tinyint 输出流数目 * @param $level tinyint 频道层数 * @param $open_ts tinyint 是否支持手机流 (1-是 0-否) * @param $record_time int 自动收录节目时间偏差设置 (±30秒 大于30秒就等于30秒,小于-30秒就等于-30秒) * @param $audio_only tinyint 记录是否是音频 (1-是 0-否 ) * @param $create_time int 创建时间 * @param $update_time int 更新时间 * @param $ip string 创建者ip * @param $channel_id int 频道ID * @param $delay_stream_id int 延时层流ID * @param $chg_stream_id int 切播层流ID * @param $out_stream_id int 输出层流ID * @param $stream_name string 流名称 * @param $out_stream_name string 输出流名称 * @param $is_main tinyint 是否主流 (1-是 0-否) * @param $bitrate int 码流 * @param $flag_stream 标识(单流改为多流) * @param $drm tyinint 防盗链设置 (1-启用 0-关闭) * @param $logo_info array logo素材信息 * @return $ret['id'] int 频道ID * @include tvie_api.php */ public function create() { $name = urldecode($this->input['name']); if (!$name) { $this->errorOutput('频道名称不能为空!'); } $code = urldecode($this->input['code']); if (!$code) { $this->errorOutput('台号不能为空!'); } $stream_id = $this->input['stream_id'] ? intval($this->input['stream_id']) : 0; $save_time = $this->input['save_time'] ? intval($this->input['save_time']) : 0; //回看时间 $live_delay = $this->input['live_delay'] ? intval($this->input['live_delay']) : 0; //延时时间 $is_live = intval($this->input['is_live']); //是否播控 1表示有播控 0表示无播控 $drm = intval($this->input['drm']); //防盗链设置 $stream_name = $this->input['stream_name']; if (!$stream_name) { $this->errorOutput('至少为频道选择一条流'); } $main_stream_name = $this->input['main_stream_name']; if ($this->input['beibo']) { if ($is_live && count($this->input['beibo']) > 2) { $this->errorOutput('最多两个备播信号,请重新选择!'); } foreach ($this->input['beibo'] as $value) { $beibo_key_value = explode('#', urldecode($value)); $beibo[$beibo_key_value[0]] = $beibo_key_value[1]; } $beibo = serialize($beibo); } $sql = "SELECT * FROM " . DB_PREFIX . "stream WHERE id=" . $stream_id; $streams = $this->db->query_first($sql); if (!$streams) { $this->errorOutput('所选信号不存在,请重新选择'); } $other_info = unserialize($streams['other_info']); if (!$other_info) { $this->errorOutput('所选信号没有信号流'); } $stream_name_arr = array(); foreach ($other_info as $v) { $stream_name_arr[$v['name']] = $v; } $stream_info = array(); foreach ($stream_name as $n) { if ($stream_name_arr[$n]) { $stream_info[] = $stream_name_arr[$n]; } } if (!$stream_info) { $this->errorOutput('所选流不存在或已被删除'); } //输入流的数目 $uri_in_num = count($stream_name); //层数目 if ($uri_in_num > 1) { $level = 1; } else { if (!$is_live) { $level = 1; } elseif (!$live_delay) { $level = 2; } else { $level = 3; } } //选择流与选择信号流的差集 $diff_stream_name = array_diff($stream_name_arr, $stream_name); //开启tvie if ($this->settings['tvie']['open']) { include CUR_CONF_PATH . 'lib/tvie_api.php'; $delay_tvie = new TVie_api($this->settings['tvie']['up_stream_server']); $delay_type = 'normal_virtual'; $chg_tvie = $delay_tvie; $out_tvie = new TVie_api($this->settings['tvie']['stream_server']); $first_stream = $stream_info[0]; //获取主信号流 unset($stream_info[0]); $delay_stream_uri = hg_get_stream_url($this->settings['tvie']['up_stream_server'], array('channel' => $streams['ch_name'], 'stream_name' => $first_stream['name']), 'channels', 'tvie://'); if (is_array($first_stream['backstore'])) { $backstore = implode(',', $first_stream['backstore']); } else { $backstore = $first_stream['backstore']; } //32创建延时层 $delay_channel = $delay_tvie->create_channel('delay_' . $code, $name, $streams['server_id'], 1, $live_delay, $delay_type, $first_stream['name'], $first_stream['recover_cache'], $first_stream['source_name'], $delay_stream_uri, $first_stream['bitrate'], $first_stream['drm'], $first_stream['wait_relay'], $backstore); $delay_channel_id = $delay_channel['channel_id']; if (!$delay_channel_id) { $this->errorOutput('频道创建失败,原因:' . serialize($delay_channel) . $delay_channel['message'] . $delay_channel['errors']); } $ret_delay_channel_info = $delay_tvie->get_channel_by_id($delay_channel_id); $ret_delay_stream_info = $ret_delay_channel_info['channel']['streams']; $first_delay_stream_id = $ret_delay_stream_info[0]['id']; //延时层创建流 $stream_ids = array('delay_stream_id' => array($first_stream['name'] => $first_delay_stream_id)); if ($stream_info) { foreach ($stream_info as $key => $value) { $delay_uri = hg_get_stream_url($this->settings['tvie']['up_stream_server'], array('channel' => $streams['ch_name'], 'stream_name' => $value['name']), 'channels', 'tvie://'); if (is_array($value['backstore'])) { $backstore = implode(',', $value['backstore']); } else { $backstore = $value['backstore']; } $ret_delay_stream = $delay_tvie->create_channel_stream($value['name'], $value['recover_cache'], $value['source_name'], $delay_uri, $value['drm'], $backstore, $value['wait_relay'], 0, $value['bitrate'], $delay_channel_id); if ($ret_delay_stream['stream_id']) { $stream_ids['delay_stream_id'][$value['name']] = $ret_delay_stream['stream_id']; } } } //创建切播层 $chg_type = 'normal_virtual'; if (!$live_delay) { //没有延时,上游流地址直接是信号流地址 $chg_stream_uri = hg_get_stream_url($this->settings['tvie']['up_stream_server'], array('channel' => $streams['ch_name'], 'stream_name' => $first_stream['name'])); } else { //有延时,上游流地址延迟层流地址 $chg_stream_uri = hg_get_stream_url($this->settings['tvie']['up_stream_server'], array('channel' => 'delay_' . $code, 'stream_name' => $first_stream['name'])); } if (is_array($first_stream['backstore'])) { $backstore = implode(',', $first_stream['backstore']); } else { $backstore = $first_stream['backstore']; } //32创建切播层 $chg_channel = $chg_tvie->create_channel('chg_' . $code, $name, $streams['server_id'], 0, 0, $chg_type, $first_stream['name'], $first_stream['recover_cache'], $first_stream['source_name'], $chg_stream_uri, $first_stream['bitrate'], $first_stream['drm'], $first_stream['wait_relay'], $backstore); $chg_channel_id = $chg_channel['channel_id']; if (!$chg_channel_id) { $delay_tvie->delete_channel($delay_channel_id); $this->errorOutput('频道创建失败,原因:' . $chg_channel['message'] . $chg_channel['errors']); } $ret_chg_channel_info = $chg_tvie->get_channel_by_id($chg_channel_id); $ret_chg_stream_info = $ret_chg_channel_info['channel']['streams']; $first_chg_stream_id = $ret_chg_stream_info[0]['id']; $stream_ids['chg_stream_id'][$first_stream['name']] = $first_chg_stream_id; //创建切播层流 $chg_url = array(); foreach ($stream_info as $key => $value) { if (!$live_delay) { //没有延时,上游流地址直接是信号流地址 $chg_uri = hg_get_stream_url($this->settings['tvie']['up_stream_server'], array('channel' => $streams['ch_name'], 'stream_name' => $value['name']), 'channels', 'tvie://'); $chg_uri_http = hg_get_stream_url($this->settings['tvie']['up_stream_server'], array('channel' => $streams['ch_name'], 'stream_name' => $value['name'])); } else { //有延迟,上游流地址是延迟层流地址 $chg_uri = hg_get_stream_url($this->settings['tvie']['up_stream_server'], array('channel' => 'delay_' . $code, 'stream_name' => $value['name']), 'channels', 'tvie://'); $chg_uri_http = hg_get_stream_url($this->settings['tvie']['up_stream_server'], array('channel' => 'delay_' . $code, 'stream_name' => $value['name'])); } $chg_url['tvie'][] = $chg_uri; $chg_url['http'][] = $chg_uri_http; if (is_array($value['backstore'])) { $backstore = implode(',', $value['backstore']); } else { $backstore = $value['backstore']; } $ret_chg_stream = $chg_tvie->create_channel_stream($value['name'], $value['recover_cache'], $value['source_name'], $chg_uri_http, $value['drm'], $backstore, $value['wait_relay'], 0, $value['bitrate'], $chg_channel_id); if ($ret_chg_stream['stream_id']) { $stream_ids['chg_stream_id'][$value['name']] = $ret_chg_stream['stream_id']; } } //32延时层或者切播层创建成功后,再向21创建输出层 $out_type = 'normal_virtual'; if (!$is_live) { //无切播层,直接信号流地址 $out_stream_uri = hg_get_stream_url($this->settings['tvie']['up_stream_server'], array('channel' => $streams['ch_name'], 'stream_name' => $first_stream['name']), 'channels', 'tvie://'); } else { //有切播层,切播层流地址 $out_stream_uri = hg_get_stream_url($this->settings['tvie']['up_stream_server'], array('channel' => 'chg_' . $code, 'stream_name' => $first_stream['name']), 'channels', 'tvie://'); } if ($this->input['open_ts']) { $first_stream['backstore'] = array(0 => 'flv', 1 => 'ts'); } if (is_array($first_stream['backstore'])) { $backstore = implode(',', $first_stream['backstore']); } else { $backstore = $first_stream['backstore']; } $out_live_delay = $live_delay; if ($level != 1) { $out_live_delay = 0; } else { if (!$save_time) { $save_time = 1; } } //创建频道 $out_channel = $out_tvie->create_channel($code, $name, $streams['server_id'], $save_time, $out_live_delay, $out_type, $first_stream['name'], $first_stream['recover_cache'], $first_stream['source_name'], $out_stream_uri, $first_stream['bitrate'], $drm, $first_stream['wait_relay'], $backstore); $out_channel_id = $out_channel['channel_id']; //返回频道id if (!$out_channel_id) { //删除切播层、延时层 $delay_tvie->delete_channel($delay_channel_id); $chg_tvie->delete_channel($chg_channel_id); $this->errorOutput('频道创建失败,原因:' . $out_channel['message'] . $out_channel['errors']); } $ret_out_channel_info = $out_tvie->get_channel_by_id($out_channel_id); $ret_out_stream_info = $ret_out_channel_info['channel']['streams']; $first_out_stream_id = $ret_out_stream_info[0]['id']; $stream_ids['out_stream_id'][$first_stream['name']] = $first_out_stream_id; //创建输出层流 foreach ($stream_info as $key => $value) { if (!$is_live) { //无切播层,直接信号流地址 $out_uri = hg_get_stream_url($this->settings['tvie']['up_stream_server'], array('channel' => $streams['ch_name'], 'stream_name' => $value['name']), 'channels', 'tvie://'); } else { //有切播层,切播层流地址 $out_uri = hg_get_stream_url($this->settings['tvie']['up_stream_server'], array('channel' => 'chg_' . $code, 'stream_name' => $value['name']), 'channels', 'tvie://'); } if ($this->input['open_ts']) { $value['backstore'] = array(0 => 'flv', 1 => 'ts'); } if (is_array($value['backstore'])) { $backstore = implode(',', $value['backstore']); } else { $backstore = $value['backstore']; } $ret_out_stream = $out_tvie->create_channel_stream($value['name'], $value['recover_cache'], $value['source_name'], $out_uri, $drm, $backstore, $value['wait_relay'], 0, $value['bitrate'], $out_channel_id); if ($ret_out_stream['stream_id']) { $stream_ids['out_stream_id'][$value['name']] = $ret_out_stream['stream_id']; } } } //录制节目时间偏差设置 if ($this->input['record_time'] >= 0) { if ($this->input['record_time'] > 30) { $record_time = 30; } else { $record_time = $this->input['record_time']; } } else { if ($this->input['record_time'] < -30) { $record_time = -30; } else { $record_time = $this->input['record_time']; } } $info = array('user_id' => $this->user['user_id'], 'user_name' => $this->user['user_name'], 'code' => $code, 'code_2' => $code, 'name' => $name, 'delay_id' => $delay_channel_id, 'chg_id' => $chg_channel_id, 'ch_id' => $out_channel_id, 'is_live' => $is_live, 'drm' => $drm, 'open_ts' => $this->input['open_ts'], 'uri_in_num' => $uri_in_num, 'uri_out_num' => $uri_out_num, 'save_time' => $save_time ? $save_time : 0, 'live_delay' => $live_delay ? $live_delay : 0, 'stream_display_name' => $streams['s_name'], 'stream_mark' => $streams['ch_name'], 'level' => $level, 'beibo' => $beibo, 'stream_id' => $stream_id, 'main_stream_name' => $main_stream_name[0], 'stream_info_all' => serialize($stream_name), 'create_time' => TIMENOW, 'update_time' => TIMENOW, 'ip' => hg_getip(), 'record_time' => $record_time, 'audio_only' => $first_stream['audio_only']); $createsql = "INSERT INTO " . DB_PREFIX . "channel SET "; $space = ""; foreach ($info as $key => $value) { $createsql .= $space . $key . "=" . "'" . $value . "'"; $space = ","; } $ret = array(); $this->db->query($createsql); $ret['id'] = $this->db->insert_id(); //插入排序id //插入工作量统计 $statistic = new statistic(); $statistics_data = array('content_id' => $ret['id'], 'contentfather_id' => '', 'type' => 'insert', 'user_id' => $this->user['user_id'], 'user_name' => $this->user['user_name'], 'app_uniqueid' => APP_UNIQUEID, 'module_uniqueid' => MODULE_UNIQUEID, 'before_data' => '', 'last_data' => $name, 'num' => 1); $statistic->insert_record($statistics_data); if ($_FILES['files']['tmp_name']) { include_once ROOT_PATH . 'lib/class/material.class.php'; $this->mMaterial = new material(); $file['Filedata'] = $_FILES['files']; $material = $this->mMaterial->addMaterial($file, $ret['id'], intval($this->input['mmid']), 'img4'); $logo_info['id'] = $material['id']; $logo_info['type'] = $material['type']; $logo_info['server_mark'] = $material['server_mark']; $logo_info['filepath'] = $material['filepath']; $logo_info['name'] = $material['name']; $logo_info['filename'] = $material['filename']; $logo_info['url'] = $material['url']; } $sql = "UPDATE " . DB_PREFIX . "channel SET order_id = " . $ret['id'] . ", logo_info = '" . serialize($logo_info) . "' WHERE id=" . $ret['id']; $this->db->query($sql); //流信息 $stream_info[] = $first_stream; $i = 0; $stream_num = count($stream_info) - 1; foreach ($stream_info as $k => $v) { $main_stream_info = array('channel_id' => $ret['id'], 'stream_id' => $stream_id, 'delay_stream_id' => $stream_ids['delay_stream_id'][$v['name']], 'chg_stream_id' => $stream_ids['chg_stream_id'][$v['name']], 'out_stream_id' => $stream_ids['out_stream_id'][$v['name']], 'stream_name' => $v['name'], 'out_stream_name' => $v['name'], 'is_main' => $main_stream_name[0] == $v['name'] ? 1 : 0, 'bitrate' => $v['bitrate'], 'flag_stream' => '', 'create_time' => TIMENOW, 'update_time' => TIMENOW, 'ip' => hg_getip()); $cresql = "INSERT INTO " . DB_PREFIX . "channel_stream SET "; $space = ""; foreach ($main_stream_info as $key => $value) { $cresql .= $space . $key . "=" . "'" . $value . "'"; $space = ","; } $this->db->query($cresql); $i++; } $this->setXmlNode('channel', 'info'); $this->addItem($ret['id']); $this->output(); }
/** * 修改台号 (code 只修改输出层和本地) * @name channelCodeEdit * @access public * @author lijiaying * @category hogesoft * @copyright hogesoft * @param $channel_id int 频道id * @param $code_2 string 台号 * @return $code_2 string 修改后台号 * @include tvie_api.php */ public function channelCodeEdit() { $channel_id = intval($this->input['channel_id']); if (!$channel_id) { $this->errorOutput('未传入频道ID'); } $code_2 = urldecode($this->input['code_2']); //频道信息 $sql = "SELECT * FROM " . DB_PREFIX . "channel WHERE id=" . $channel_id; $channel_info = $this->db->query_first($sql); $name = $channel_info['name']; $code = $channel_info['code']; $stream_id = $channel_info['stream_id']; $delay_id = $channel_info['delay_id']; $chg_id = $channel_info['chg_id']; $out_id = $channel_info['ch_id']; $level = $channel_info['level']; $open_ts = $channel_info['open_ts']; $live_delay = $channel_info['live_delay']; $save_time = $channel_info['save_time']; $drm = $channel_info['drm']; //频道流信息 $sql = "SELECT * FROM " . DB_PREFIX . "channel_stream WHERE channel_id IN(" . $channel_id . ")"; $q = $this->db->query($sql); $channel_stream = $channel_stream_name = array(); while ($row = $this->db->fetch_array($q)) { $channel_stream[$row['stream_name']] = $row; $channel_stream_name[] = $row['stream_name']; } //信号信息 $sql = "SELECT * FROM " . DB_PREFIX . "stream WHERE id=" . $stream_id; $streams = $this->db->query_first($sql); $other_info = unserialize($streams['other_info']); //channel_stream和stream 组成的数据 $stream_name_arr = array(); foreach ($other_info as $v) { $stream_name_arr[$v['name']] = $v; } $stream_info_merge = array(); foreach ($channel_stream_name as $n) { if ($stream_name_arr[$n]) { $stream_info_merge[$n] = $stream_name_arr[$n]; } } //合并数据 $stream_info = array(); foreach ($channel_stream as $k => $v) { $stream_info[] = array_merge($stream_info_merge[$k], $channel_stream[$k]); } //开启tvie if ($this->settings['tvie']['open']) { include CUR_CONF_PATH . 'lib/tvie_api.php'; $delay_type = 'normal_virtual'; $out_tvie = new TVie_api($this->settings['tvie']['stream_server']); //删除输出层 if ($code_2 && $code_2 != $code) { $ret_out = $out_tvie->delete_channel($out_id); $out_live_delay = $live_delay; if ($level != 1) { $out_live_delay = 0; } else { if (!$save_time) { $save_time = 1; } } //创建输出层 if ($ret_out['message'] == 'Handled') { //无切播层,直接信号流地址 if ($level == 1) { $out_stream_uri = hg_get_stream_url($this->settings['tvie']['up_stream_server'], array('channel' => $streams['ch_name'], 'stream_name' => $stream_info[0]['out_stream_name']), 'channels', 'tvie://'); } else { $out_stream_uri = hg_get_stream_url($this->settings['tvie']['up_stream_server'], array('channel' => 'chg_' . $channel_info['code_2'], 'stream_name' => $stream_info[0]['out_stream_name']), 'channels', 'tvie://'); } if ($open_ts) { $stream_info[0]['backstore'] = array(0 => 'flv', 1 => 'ts'); } if (is_array($stream_info[0]['backstore'])) { $backstore = implode(',', $stream_info[0]['backstore']); } else { $backstore = $stream_info[0]['backstore']; } $out_channel = $out_tvie->create_channel($code_2, $name, $streams['server_id'], $save_time, $out_live_delay, $delay_type, $stream_info[0]['out_stream_name'], $stream_info[0]['recover_cache'], $stream_info[0]['source_name'], $out_stream_uri, $stream_info[0]['bitrate'], $drm, $stream_info[0]['wait_relay'], $backstore); $out_channel_id = $out_channel['channel_id']; //返回频道id // unset($stream_info[0]); $ret_out_channel_info = $out_tvie->get_channel_by_id($out_channel_id); $ret_out_stream_info = $ret_out_channel_info['channel']['streams']; $first_out_stream_id = $ret_out_stream_info[0]['id']; $stream_ids = array($ret_out_stream_info[0]['name'] => $first_out_stream_id); if ($out_channel_id) { foreach ($stream_info as $value) { if ($level == 1) { $out_uri = hg_get_stream_url($this->settings['tvie']['up_stream_server'], array('channel' => $streams['ch_name'], 'stream_name' => $value['out_stream_name']), 'channels', 'tvie://'); } else { $out_uri = hg_get_stream_url($this->settings['tvie']['up_stream_server'], array('channel' => 'chg_' . $channel_info['code_2'], 'stream_name' => $value['out_stream_name']), 'channels', 'tvie://'); } if ($open_ts) { $value['backstore'] = array(0 => 'flv', 1 => 'ts'); } if (is_array($value['backstore'])) { $backstore = implode(',', $value['backstore']); } else { $backstore = $value['backstore']; } $out_create_stream = $out_tvie->create_channel_stream($value['out_stream_name'], $value['recover_cache'], $value['source_name'], $out_uri, $drm, $backstore, $value['wait_relay'], 0, $value['bitrate'], $out_channel_id); if ($out_create_stream['stream_id']) { $stream_ids[$value['out_stream_name']] = $out_create_stream['stream_id']; } } foreach ($stream_info as $value) { //更新channel_stream $updateSql = "UPDATE " . DB_PREFIX . "channel_stream SET stream_id=" . $stream_id . ", out_stream_id=" . $stream_ids[$value['out_stream_name']] . ", stream_name='" . $value['name'] . "', update_time=" . TIMENOW; $updateSql .= " WHERE channel_id=" . $channel_id . " AND stream_name='" . $value['name'] . "'"; $this->db->query($updateSql); if ($channel_info['stream_state']) { $out_tvie->stop_stream($stream_ids[$value['out_stream_name']]); $out_tvie->start_stream($stream_ids[$value['out_stream_name']]); } else { $out_tvie->start_stream($stream_ids[$value['out_stream_name']]); $out_tvie->stop_stream($stream_ids[$value['out_stream_name']]); } } $up_sql = "UPDATE " . DB_PREFIX . "channel SET ch_id=" . $out_channel_id . ", code='" . $code_2 . "' WHERE id=" . $channel_id; $this->db->query($up_sql); } if ($out_channel_id) { $this->addItem($code_2); } else { return; } } } else { $this->addItem($code); } } $this->output(); }
/** * 更新信号流信息 * @name update * @access public * @author lijiaying * @category hogesoft * @copyright hogesoft * @param $id int 频道信号ID * @param $s_name string 信号名称 * @param $ch_name string 信号标识 * @param $save_time int 回看时间 (小时) * @param $live_delay int 延时时间 (分钟) * @param $uri string 信号流地址 * @param $name string 流名称 * @param $bitrate int 码流 * @param $backstore array 支持格式(flv,ts) * @param $audio_only tinyint 是否音频 (1-是 0-否) * @param $wait_relay tinyint 是否推送 (1-是 0-否) * @param $other_info string 流信息 * @param $update_time int 更新时间 * @param $audio_temp tinyint 临时变量 * @param $audio_only tinyint 同步频道音频设置参数 * @return $info['id'] int 频道信号ID * @include tvie_api.php */ function update() { $id = $stream_id = intval($this->input['id']); if (!$id) { $this->errorOutput('未传入频道信号ID'); } $s_name = urldecode($this->input['s_name']); if (!$s_name) { $this->errorOutput('信号名称不能为空'); } $save_time = intval($this->input['save_time']); $live_delay = intval($this->input['live_delay']); $sql = "SELECT * FROM " . DB_PREFIX . "stream WHERE id = " . $id; $stream = $this->db->query_first($sql); $stream_other_info = unserialize($stream['other_info']); $input_info = $stream_other_info; if (!empty($input_info)) { $input_name = $input_name_index = $input_info_index = array(); foreach ($input_info as $k => $v) { $input_name[] = $v['name']; $input_name_index[$v['name']] = $v['name']; $input_info_index[$v['name']] = $v; } } $ch_id = $stream['ch_id']; $s_status = $stream['s_status']; if (is_array($this->input['counts']) && $this->input['counts']) { $streams_info = $tpl_uri = $tpl_name = $tpl_name_index = array(); for ($i = 0; $i < count($this->input['counts']); $i++) { $streams_info[$i]['id'] = urldecode($this->input['id_' . $i]); $streams_info[$i]['name'] = urldecode($this->input['name_' . $i]); if (!$streams_info[$i]['name']) { $this->errorOutput('输出标识不能为空'); } $streams_info[$i]['ch_name'] = urldecode($this->input['ch_name_hidden']); $streams_info[$i]['uri'] = urldecode($this->input['uri_' . $i]); if (!$streams_info[$i]['uri']) { $this->errorOutput('来源地址不能为空'); } $streams_info[$i]['bitrate'] = $this->input['bitrate_' . $i]; $streams_info[$i]['backstore'][] = 'flv'; $streams_info[$i]['audio_only'] = $this->input['audio_only'] ? intval($this->input['audio_only']) : 0; $streams_info[$i]['wait_relay'] = $this->input['wait_relay'] ? intval($this->input['wait_relay']) : 0; $streams_info[$i]['recover_cache'] = 1; $streams_info[$i]['drm'] = 0; $streams_info[$i]['source_name'] = 'tvie-live-encoder'; $tpl_uri[] = urldecode($this->input['uri_' . $i]); $tpl_name[] = urldecode($this->input['name_' . $i]); $tpl_name_index[urldecode($this->input['name_' . $i])] = urldecode($this->input['name_' . $i]); } } if (empty($streams_info)) { $this->errorOutput('数据不完整,请重新填写!'); } //新增信号 $add_diff = @array_diff($tpl_name, $input_name); //删除信号 $del_diff = @array_diff($input_name, $tpl_name); $del_diff_index = @array_diff($input_name_index, $tpl_name_index); //更新信号 $update_inter = @array_intersect($tpl_name, $input_name); if ($this->settings['tvie']['open']) { include CUR_CONF_PATH . 'lib/tvie_api.php'; $up_tvie = new TVie_api($this->settings['tvie']['up_stream_server']); $ret_channel = $up_tvie->update_channel($s_name, $save_time, $live_delay, $ch_id); if (!$ret_channel['message'] == 'Updated') { $this->errorOutput('媒体服务器更新失败'); } foreach ($streams_info as $k => $v) { if ($add_diff[$k] == $v['name']) { //添加信号流 if (is_array($v['backstore'])) { $backstore = implode(',', $v['backstore']); } else { $backstore = $v['backstore']; } $ret_stream = $up_tvie->create_channel_stream($v['name'], $v['recover_cache'], $v['source_name'], $v['uri'], $v['drm'], 'flv', $v['wait_relay'], $v['audio_only'], $v['bitrate'], $ch_id); if (!$ret_stream['stream_id']) { $this->errorOutput('媒体服务器添加信号流失败'); } $other_info[$k] = array('id' => $ret_stream['stream_id'], 'name' => $v['name'], 'ch_name' => $v['ch_name'], 'uri' => $v['uri'], 'recover_cache' => $v['recover_cache'], 'source_name' => $v['source_name'], 'drm' => 0, 'backstore' => $v['backstore'], 'wait_relay' => $v['wait_relay'], 'audio_only' => $v['audio_only'], 'bitrate' => $v['bitrate']); if ($ret_stream['stream_id']) { if ($s_status) { $up_tvie->stop_stream($ret_stream['stream_id']); $up_tvie->start_stream($ret_stream['stream_id']); } else { $up_tvie->start_stream($ret_stream['stream_id']); $up_tvie->stop_stream($ret_stream['stream_id']); } } } else { //更新信号流 if (is_array($v['backstore'])) { $backstore = implode(',', $v['backstore']); } else { $backstore = $v['backstore']; } $ret_stream = $up_tvie->update_channel_stream($v['id'], 0, 'flv', 1, $v['uri'], $v['audio_only'], $v['wait_relay'], $v['source_name']); if ($ret_stream['message'] != 'Modified') { $this->errorOutput('媒体服务器更新信号流失败'); } if ($ret_stream['message'] == 'Modified') { $other_info[$k] = array('id' => $v['id'], 'name' => $v['name'], 'ch_name' => $v['ch_name'], 'uri' => $v['uri'], 'recover_cache' => $v['recover_cache'], 'source_name' => $v['source_name'], 'drm' => $v['drm'], 'backstore' => $v['backstore'], 'wait_relay' => $v['wait_relay'], 'audio_only' => $v['audio_only'], 'bitrate' => $v['bitrate']); } else { if ($update_inter[$k] == $v['name']) { $other_info[$k] = array('id' => $input_info_index[$v['name']]['id'], 'name' => $input_info_index[$v['name']]['name'], 'ch_name' => $input_info_index[$v['name']]['ch_name'], 'uri' => $input_info_index[$v['name']]['uri'], 'recover_cache' => $input_info_index[$v['name']]['recover_cache'], 'source_name' => $input_info_index[$v['name']]['source_name'], 'drm' => $input_info_index[$v['name']]['drm'], 'backstore' => $input_info_index[$v['name']]['backstore'], 'wait_relay' => $input_info_index[$v['name']]['wait_relay'], 'audio_only' => $input_info_index[$v['name']]['audio_only'], 'bitrate' => $input_info_index[$v['name']]['bitrate']); } } } } //删除输入输出流 if (!empty($input_info)) { foreach ($input_info as $k => $v) { if ($del_diff[$k] == $v['name']) { $ret_del = $up_tvie->delete_stream($v['id']); //删除流信息 } } } } $info = array('s_name' => $s_name, 'uri' => serialize($tpl_uri), 's_status' => $s_status, 'other_info' => serialize($other_info), 'update_time' => TIMENOW); $sql = "UPDATE " . DB_PREFIX . "stream SET "; $space = ""; foreach ($info as $key => $value) { $sql .= $space . $key . "=" . "'" . $value . "'"; $space = ","; } $sql .= " WHERE id=" . $id; $this->db->query($sql); //同步频道音频设置 if ($this->input['audio_temp']) { if ($this->input['audio_only']) { $channelSql = "UPDATE " . DB_PREFIX . "channel SET audio_only=1 WHERE stream_id IN(" . $id . ")"; $this->db->query($channelSql); } else { $channelSql = "UPDATE " . DB_PREFIX . "channel SET audio_only=0 WHERE stream_id IN(" . $id . ")"; $this->db->query($channelSql); } } $info['id'] = $id; //start //获取频道相关信息 $channelStreamInfo = $this->getChannelStreamInfo($stream_id); if (!empty($channelStreamInfo)) { $out_tvie = new TVie_api($this->settings['tvie']['stream_server']); foreach ($channelStreamInfo as $k => $channel) { //channel_stream if (!empty($channel['channel_stream'])) { foreach ($channel['channel_stream'] as $kk => $vv) { if ($del_diff_index[$kk] == $kk) { //延时层 $ret_delay = $up_tvie->delete_stream($vv['delay_stream_id']); //切播层 if ($ret_delay['message'] == 'Handled') { $ret_chg = $up_tvie->delete_stream($vv['chg_stream_id']); } //输出层 if ($ret_chg['message'] == 'Handled') { $ret_out = $out_tvie->delete_stream($vv['out_stream_id']); } //本地 if ($ret_out['message'] == 'Handled') { $sql = "DELETE FROM " . DB_PREFIX . "channel_stream WHERE id = " . $vv['id']; $this->db->query($sql); } } } } //channel $channel_id[$k] = ''; if ($channel['stream_info_all_index']) { foreach ($channel['stream_info_all_index'] as $kk => $vv) { if ($del_diff_index[$kk] == $kk) { $channel_id[$k] = $channel['id']; unset($channel['stream_info_all_index'][$kk]); } if ($del_diff_index[$kk] == $channel['main_stream_name']) { $main_stream_name_flag = 1; } } if (!empty($channel['stream_info_all_index'])) { $stream_info_all = array(); foreach ($channel['stream_info_all_index'] as $v) { $stream_info_all[] = $v; } } if ($main_stream_name_flag) { $main_stream_name = ''; } else { $main_stream_name = $channel['main_stream_name']; } } if ($channel_id[$k]) { $sql = "UPDATE " . DB_PREFIX . "channel SET stream_info_all = '" . serialize($stream_info_all) . "', main_stream_name = '" . $main_stream_name . "' WHERE id = " . $channel_id[$k]; $this->db->query($sql); } } } //end $this->setXmlNode('stream', 'info'); $this->addItem($info['id']); $this->output(); }
/** * 创建频道信号 * @name create * @access public * @author lijiaying * @category hogesoft * @copyright hogesoft * @param $ch_name string 信号名称 * @param $s_name string 信号标识 * @param $save_time int 回看时间 (小时) * @param $live_delay int 延时时间 (分钟) * @param $uri string 信号流地址 * @param $name string 流名称 * @param $bitrate int 码流 * @param $backstore array 支持格式(flv,ts) * @param $audio_only tinyint 是否音频 (1-是 0-否) * @param $wait_relay tinyint 是否推送 (1-是 0-否) * @param $other_info string 流信息 * @param $server_id int 服务器ID * @param $create_time int 创建时间 * @param $update_time int 更新时间 * @param $ip string 创建者IP * @return $info['id'] int 频道信号ID * @include tvie_api.php */ function create() { $ch_name = urldecode($this->input['ch_name']); //信号名称 if (!ch_name) { $this->errorOutput('信号名称的不能为空,请重新填写!'); } $s_name = urldecode($this->input['s_name']); //信号标识 if (!$s_name) { $this->errorOutput('信号标识的不能为空,请重新填写!'); } $save_time = intval($this->input['save_time']); //回看(小时) $live_delay = intval($this->input['live_delay']); //延时(分钟) $type = 'live'; $recover_cache = 1; $source_name = 'tvie-live-encoder'; $streams_info = $uri_arr = array(); if (is_array($this->input['counts']) && $this->input['counts']) { for ($i = 0; $i < count($this->input['counts']); $i++) { $streams_info[$i]['name'] = urldecode($this->input['name_' . $i]); $streams_info[$i]['uri'] = urldecode($this->input['uri_' . $i]); if (!$streams_info[$i]['name']) { $this->errorOutput('输出标识不能为空'); } if (!$streams_info[$i]['uri']) { $this->errorOutput('来源地址不能为空'); } $streams_info[$i]['bitrate'] = $this->input['bitrate_' . $i]; $streams_info[$i]['backstore'][] = $this->input['flv_' . $i]; $streams_info[$i]['backstore'][] = $this->input['ts_' . $i]; $streams_info[$i]['audio_only'] = $this->input['audio_only'] ? $this->input['audio_only'] : 0; $streams_info[$i]['wait_relay'] = $this->input['wait_relay'] ? $this->input['wait_relay'] : 0; $uri_arr[] = urldecode($this->input['uri_' . $i]); } } //开启 if ($this->settings['tvie']['open']) { include CUR_CONF_PATH . 'lib/tvie_api.php'; $up_tvie = new TVie_api($this->settings['tvie']['up_stream_server']); $servers = $up_tvie->get_all_servers(); //获取媒体服务器ID if (!$servers) { $this->errorOutput("网络延时"); } if (is_array($streams_info[0]['backstore'])) { $backstore = implode(',', $streams_info[0]['backstore']); } else { $backstore = $streams_info[0]['backstore']; } //创建直播频道 $ret_channel = $up_tvie->create_channel($ch_name, $s_name, $servers['items'][0]['id'], $save_time, $live_delay, $type, $streams_info[0]['name'], $recover_cache, $source_name, $streams_info[0]['uri'], urldecode($this->input['bitrate']), 0, $streams_info[0]['wait_relay'], 'flv'); $ch_id = $ret_channel['channel_id']; //返回虚拟频道id if (!$ch_id) { $this->errorOutput('媒体服务器数据异常'); } $ret_channel_info = $up_tvie->get_channel_by_id($ch_id); $ret_stream_info = $ret_channel_info['channel']['streams']; $first_stream_id = $ret_stream_info[0]['id']; if (!$first_stream_id) { $this->errorOutput('媒体服务器创建信号流失败'); } //创建流 foreach ($streams_info as $key => $value) { $ret = $up_tvie->create_channel_stream($value['name'], $recover_cache, $source_name, $value['uri'], 0, 'flv', $value['wait_relay'], $value['audio_only'], $value['bitrate'], $ch_id); if (!$ret['stream_id']) { $this->errorOutput('媒体服务器创建信号失败'); } $backstore = array(0 => 'flv'); $other_info[$key] = array('id' => $key == 0 ? $first_stream_id : $ret['stream_id'], 'name' => $value['name'], 'ch_name' => $ch_name, 'uri' => $value['uri'], 'recover_cache' => $recover_cache, 'source_name' => $source_name, 'drm' => 0, 'backstore' => $backstore, 'wait_relay' => $value['wait_relay'], 'audio_only' => $value['audio_only'], 'bitrate' => $value['bitrate']); } } else { $this->errorOutput('媒体服务器未启动'); } $info = array('ch_id' => $ch_id, 's_name' => $s_name, 'ch_name' => $ch_name, 'uri' => serialize($uri_arr), 'type' => $type, 'save_time' => $save_time, 'live_delay' => $live_delay, 'other_info' => serialize($other_info), 'server_id' => $this->input['server_id'] ? intval($this->input['server_id']) : $servers['items'][0]['id'], 'create_time' => TIMENOW, 'update_time' => TIMENOW, 'ip' => hg_getip()); $sql = "INSERT INTO " . DB_PREFIX . "stream SET "; $space = ""; foreach ($info as $key => $value) { $sql .= $space . $key . "=" . "'" . $value . "'"; $space = ","; } $this->db->query($sql); $info['id'] = $this->db->insert_id(); $this->setXmlNode('stream', 'info'); $this->addItem($info['id']); $this->output(); }