public static function getGoodStreamersIdsForLink($link_id)
 {
     $link = Itv::getLinkById($link_id);
     if (empty($link)) {
         return false;
     }
     $ch_link_on_streamer = Mysql::getInstance()->from('ch_link_on_streamer')->where(array('link_id' => $link_id))->get()->all();
     if ($link['enable_monitoring'] && $link['enable_balancer_monitoring']) {
         $ch_link_on_streamer = array_values(array_filter($ch_link_on_streamer, function ($link_on_streamer) {
             return $link_on_streamer['monitoring_status'] == 1;
         }));
     }
     $ch_link_on_streamer = array_map(function ($link_on_streamer) {
         return $link_on_streamer['streamer_id'];
     }, $ch_link_on_streamer);
     return $ch_link_on_streamer;
 }
Пример #2
0
 public function getRealChannelByChannelId($ch_id, $link_id = null)
 {
     $ch_id = intval($ch_id);
     $channel = Itv::getById($ch_id);
     $channel['link_id'] = $link_id;
     if ($link_id != null) {
         $link = Itv::getLinkById($link_id);
         if (!empty($link)) {
             $channel['cmd'] = $link['url'];
             $channel['use_http_tmp_link'] = $link['use_http_tmp_link'];
             $channel['wowza_tmp_link'] = $link['wowza_tmp_link'];
             $channel['nginx_secure_link'] = $link['nginx_secure_link'];
             $channel['use_load_balancing'] = $link['use_load_balancing'];
         }
     }
     if (empty($link_id) || empty($link)) {
         throw new ItvChannelTemporaryUnavailable();
     }
     if (empty($channel)) {
         throw new ItvLinkException('nothing_to_play');
     }
     if ($channel['enable_wowza_load_balancing']) {
         $balancer_addr = $this->getWowzaBalancer($channel['cmd']);
         $edge = $this->getWowzaEdge('http://' . $balancer_addr . '/loadbalancer');
         if (!$edge) {
             throw new ItvLinkException('nothing_to_play');
         } else {
             $cmd = preg_replace("/" . preg_replace('/:.*/', '', $balancer_addr) . "/", $edge, $channel['cmd']);
             if ($cmd) {
                 $channel['cmd'] = $cmd;
             }
         }
     }
     if ($channel['use_load_balancing']) {
         try {
             $streamers = StreamServer::getForLink($link_id);
         } catch (Exception $e) {
             throw new ItvLinkException($e->getCode());
         }
         if ($streamers) {
             $new_addr = $streamers[0]['address'];
             $channel['load'] = $streamers[0]['load'];
             if ($channel['load'] >= 1) {
                 throw new ItvLinkException('limit');
             }
             $channel['streamer_id'] = $streamers[0]['id'];
             $channel['cmd'] = preg_replace('/:\\/\\/([^\\/]*)/', '://' . $new_addr, $channel['cmd']);
         } else {
             throw new ItvLinkException('nothing_to_play');
         }
     }
     if ($channel['use_http_tmp_link']) {
         if ($channel['wowza_tmp_link']) {
             $key = $this->createTemporaryLink("1");
             if (!$key) {
                 throw new ItvLinkException('link_fault');
             } else {
                 if (Config::getSafe('use_named_wowza_token', false)) {
                     $channel['cmd'] = $channel['cmd'] . (strpos($channel['cmd'], '?') ? '&' : '?') . 'token=' . $key;
                 } else {
                     $channel['cmd'] = $channel['cmd'] . '?' . $key;
                 }
             }
         } else {
             if (!empty($link) && $link['flussonic_tmp_link']) {
                 $key = $this->createTemporaryLink($this->stb->id);
                 if (!$key) {
                     throw new ItvLinkException('link_fault');
                 } else {
                     $channel['cmd'] = $channel['cmd'] . (strpos($channel['cmd'], '?') ? '&' : '?') . 'token=' . $key;
                 }
             } else {
                 if ($channel['nginx_secure_link']) {
                     // http://wiki.nginx.org/HttpSecureLinkModule
                     $channel['cmd'] = self::getNginxSecureLink($channel['cmd']);
                 } else {
                     if (strpos($channel['cmd'], 'rtp://') !== false || strpos($channel['cmd'], 'udp://') !== false) {
                         return $channel;
                     }
                     if (Config::getSafe('stream_proxy', '') != '') {
                         preg_match("/http:\\/\\/([^\\/]*)[\\/]?([^\\s]*)?(\\s*)?(.*)?\$/", $channel['cmd'], $tmp_url_arr);
                     } else {
                         preg_match("/http:\\/\\/([^\\/]*)\\/([^\\/]*)[\\/]?([^\\s]*)?(\\s*)?(.*)?\$/", $channel['cmd'], $tmp_url_arr);
                     }
                     if (empty($tmp_url_arr)) {
                         throw new ItvLinkException('nothing_to_play');
                     } else {
                         if (count($tmp_url_arr) == 6) {
                             $streamer = $tmp_url_arr[1];
                             $redirect_host = $tmp_url_arr[2];
                             $redirect_uri = $tmp_url_arr[3];
                         } else {
                             $streamer = Config::get('stream_proxy');
                             $redirect_host = $tmp_url_arr[1];
                             $redirect_uri = $tmp_url_arr[2];
                         }
                         $redirect_url = '/get/' . $redirect_host . '/' . $redirect_uri;
                         $link_result = $this->createTemporaryLink($redirect_url);
                         if (!$link_result) {
                             throw new ItvLinkException('link_fault');
                         } else {
                             if (preg_match("/(\\w+)\\s+http:/", $channel['cmd'], $match)) {
                                 $solution = $match[1];
                             } else {
                                 $solution = 'ffrt';
                             }
                             $channel['cmd'] = $solution . ' http://' . $streamer . '/ch/' . $link_result . (empty($tmp_url_arr[4]) ? '' : $tmp_url_arr[4]) . (empty($tmp_url_arr[5]) ? '' : $tmp_url_arr[5]);
                         }
                     }
                 }
             }
         }
     }
     return $channel;
 }