public function create(RESTApiRequest $request, $parent_id) { if (empty($this->params['users.id'])) { throw new RESTBadRequest("User required"); } if (empty($this->params['ch_id'])) { throw new RESTBadRequest("Channel ID required"); } $user_id = (int) $this->params['users.id']; $ch_id = (int) $this->params['ch_id']; $program_id = (int) $parent_id; $user = \Stb::getById($user_id); if (empty($user)) { throw new RESTNotFound("User not found"); } $itv = \Itv::getInstance(); $user_channels = $itv->getAllUserChannelsIdsByUid($user['id']); if (!in_array($ch_id, $user_channels)) { throw new RESTForbidden("User don't have access to this channel"); } $channel = \Itv::getById($ch_id); if (empty($channel)) { throw new RESTNotFound("Channel not found"); } if (!$channel['allow_pvr']) { throw new RESTForbidden("Channel does not support PVR"); } $program = \Epg::getById($program_id); if (empty($program)) { throw new RESTNotFound("Program not found"); } if ($program['ch_id'] != $ch_id) { throw new RESTNotAcceptable("Channel of program mismatch"); } if (strtotime($program['time']) < time()) { throw new RESTNotAcceptable("Start time in past"); } $pvr = new \RemotePvr(); try { $rec_id = $pvr->startRecDeferredById($program['real_id']); } catch (\nPVRException $e) { throw new RESTServerError($e->getMessage()); } if (!$rec_id) { return false; } $recording = $pvr->getById($rec_id); return array('id' => $recording['id'], 'name' => $recording['program'], 'start_time' => strtotime($recording['t_start']), 'end_time' => strtotime($recording['t_stop']), 'ch_id' => (int) $recording['ch_id'], 'ch_name' => $channel['name'], 'status' => $recording['started'] ? $recording['ended'] ? 2 : 1 : 0); }
public function get(RESTApiRequest $request, $parent_id) { $epg = \Epg::getById(intval($parent_id)); if (empty($epg)) { throw new RESTNotFound("Program not found"); } $channel = \Itv::getChannelById(intval($epg['ch_id'])); if (empty($channel)) { throw new RESTNotFound("Channel not found"); } if ($channel["enable_tv_archive"] != 1) { throw new RESTForbidden("This channel does not have tv archive"); } try { $archive = new \TvArchive(); $url = $archive->getUrlByProgramId(intval($epg['id'])); } catch (\StorageSessionLimitException $e) { throw new RESTTemporaryUnavailable("Session limit"); } catch (\Exception $e) { throw new RESTServerError("Failed to obtain url"); } return $url; }
public function getUrlByProgramId($program_id, $disable_overlap = false, $program = array()) { if (empty($program)) { $program = Epg::getById($program_id); } $task = $this->getLessLoadedTaskByChId($program['ch_id']); $overlap = Config::getSafe('tv_archive_playback_overlap', 0) * 60; $overlap_start = Config::getSafe('tv_archive_playback_overlap_start', 0) * 60; $tz = new DateTimeZone(!empty(Stb::$server_timezone) ? Stb::$server_timezone : date_default_timezone_get()); $date = new DateTime(date('r', strtotime($program['time']))); $date->setTimeZone($tz); // previous part overlap compensation if ($disable_overlap) { if ($overlap) { $date->add(new DateInterval('PT' . $overlap . 'S')); } } else { if ($overlap_start) { $date->sub(new DateInterval('PT' . $overlap_start . 'S')); } } $date_now = new DateTime('now', new DateTimeZone(!empty(Stb::$server_timezone) ? Stb::$server_timezone : date_default_timezone_get())); $date_to = new DateTime(date('r', strtotime($program['time_to']))); $date_to->setTimeZone($tz); $dst_diff = $date->format('Z') - $date_now->format('Z'); $storage = Master::getStorageByName($task['storage_name']); if (!$storage['flussonic_dvr'] && !$storage['wowza_dvr']) { if ($dst_diff > 0) { $date->add(new DateInterval('PT' . $dst_diff . 'S')); $date_to->add(new DateInterval('PT' . $dst_diff . 'S')); } elseif ($dst_diff < 0) { $dst_diff *= -1; $date->sub(new DateInterval('PT' . $dst_diff . 'S')); $date_to->sub(new DateInterval('PT' . $dst_diff . 'S')); } } $start_timestamp = $date->getTimestamp(); $stop_timestamp = $date_to->getTimestamp() + $overlap; $channel = Itv::getChannelById($program['ch_id']); $filename = $date->format("Ymd-H"); if ($channel['wowza_dvr']) { $filename .= '.mp4'; } else { $filename .= '.mpg'; } $position = date("i", $start_timestamp) * 60 + date("s", $start_timestamp); $channel = Itv::getChannelById($program['ch_id']); if ($storage['flussonic_dvr']) { if (preg_match("/:\\/\\/([^\\/]*)\\/([^\\/]*).*(mpegts|m3u8)\$/", $channel['mc_cmd'], $match)) { if ($match[3] == 'mpegts') { $url = 'http://' . $storage['storage_ip'] . '/' . $match[2] . '/archive/' . $start_timestamp . '/' . ($stop_timestamp - $start_timestamp) . '/mpegts'; } else { $url = preg_replace('/:\\/\\/([^\\/]*)/', '://' . $storage['storage_ip'], $channel['mc_cmd']); $url = preg_replace('/\\.m3u8/', '-' . $start_timestamp . '-' . ($stop_timestamp - $start_timestamp) . '.m3u8', $url); } $url .= '?ch_id=' . $program['ch_id'] . '&token=' . $this->createTemporaryToken($this->stb->id) . '&start=' . $position . '&duration=' . ($stop_timestamp - $start_timestamp) . '&osd_title=' . urlencode($channel['name'] . ' — ' . $program['name']) . '&real_id=' . $program['real_id']; } else { $url = false; } } elseif ($storage['wowza_dvr']) { if (preg_match("/:\\/\\/([^\\/]*)\\/.*\\.m3u8/", $channel['mc_cmd'], $match)) { $url = preg_replace('/:\\/\\/([^\\/]*)/', '://' . $storage['storage_ip'], $channel['mc_cmd']); $url = preg_replace('/\\.m3u8.*/', '.m3u8?DVR&wowzadvrplayliststart=' . gmdate("YmdHis", $start_timestamp) . '&wowzadvrplaylistduration=' . ($stop_timestamp - $start_timestamp) * 1000, $url) . '&ch_id=' . $program['ch_id'] . '&token=' . $this->createTemporaryToken("1") . '&start=' . $position . '&duration=' . ($stop_timestamp - $start_timestamp) . '&osd_title=' . urlencode($channel['name'] . ' — ' . $program['name']) . '&real_id=' . $program['real_id']; } else { $url = false; } } else { $url = 'http://' . $storage['storage_ip'] . ':' . $storage['apache_port'] . '/stalker_portal/storage/get.php?filename=' . $filename . '&ch_id=' . $program['ch_id'] . '&token=' . $this->createTemporaryToken(true) . '&start=' . $position . '&duration=' . ($stop_timestamp - $start_timestamp) . '&osd_title=' . urlencode($channel['name'] . ' — ' . $program['name']) . '&real_id=' . $program['real_id']; } if (!empty($storage['storage_name'])) { $cache = Cache::getInstance(); $cache->set($this->stb->id . '_playback', array('type' => 'tv-archive', 'id' => $program_id, 'storage' => $storage['storage_name']), 0, 10); } else { $cache = Cache::getInstance(); $cache->del($this->stb->id . '_playback'); } return $url; }