Better error reporting for errors when sending video to dreambox
[playVideoOnDreamboxProxy.git] / www / play.php
index 314fee7a660d0fc5c19aa7ab6153234148b57088..810995123a9881030249c296421364ec3b657b45 100644 (file)
 <?php
 $youtubedlPath = '/usr/bin/youtube-dl';
-$dreamboxHost  = 'dreambox';
+$dreamboxUrl   = 'http://dreambox';
 
+require_once __DIR__ . '/functions.php';
 $cfgFile = __DIR__ . '/../data/config.php';
 if (file_exists($cfgFile)) {
     include $cfgFile;
 }
 
-$pageUrl  = getPageUrl();
-$videoUrl = extractVideoUrl($pageUrl, $youtubedlPath);
-header('Video-URL: ' . $videoUrl);
-playVideoOnDreambox($videoUrl, $dreamboxHost);
-
-function getPageUrl()
-{
-    global $argv, $argc;
-    if (php_sapi_name() == 'cli') {
-        if ($argc < 2) {
-            errorInput('No URL given as command line parameter');
-        }
-        $pageUrl = $argv[1];
-    } else {
-        if (!isset($_SERVER['CONTENT_TYPE'])) {
-            errorInput('Content type header missing');
-        } else if ($_SERVER['CONTENT_TYPE'] != 'text/plain') {
-            errorInput('Content type is not text/plain but ' . $_SERVER['CONTENT_TYPE']);
-        }
-        $pageUrl = file_get_contents('php://input');
-    }
-    $parts = parse_url($pageUrl);
-    if ($parts === false) {
-        errorInput('Invalid URL in POST data');
-    } else if (!isset($parts['scheme'])) {
-        errorInput('Invalid URL in POST data: No scheme');
-    } else if ($parts['scheme'] !== 'http' && $parts['scheme'] !== 'https') {
-        errorInput('Invalid URL in POST data: Non-HTTP scheme');
-    }
-    return $pageUrl;
-}
+set_error_handler('errorHandlerStore');
 
-function extractVideoUrl($pageUrl, $youtubedlPath)
-{
-    $cmd = $youtubedlPath
-        . ' --quiet'
-        . ' --dump-json'
-        . ' ' . escapeshellarg($pageUrl)
-        . ' 2>&1';
-
-    $lastLine = exec($cmd, $output, $exitCode);
-    if ($exitCode !== 0) {
-        if (strpos($lastLine, 'Unsupported URL') !== false) {
-            errorOut('Unsupported URL', '400 Unsupported URL (No video found)');
-        } else {
-            errorOut('youtube-dl error: ' . $lastLine);
-        }
-    }
-
-    $json = implode("\n", $output);
-    $data = json_decode($json);
-
-    $url = null;
-    foreach ($data->formats as $format) {
-        //dreambox 7080hd does not play hls files
-        if (strpos($format->format, 'hls') !== false) {
-            continue;
-        }
-        $url = $format->url;
-    }
-
-    if ($url === null) {
-        //use URL chosen by youtube-dl
-        $url = $data->url;
-    }
-    return $url;
+$pageUrl  = getPageUrl();
+$json     = getYoutubeDlJson($pageUrl, $youtubedlPath);
+$videoUrl = extractVideoUrlFromJson($json);
+if (php_sapi_name() == 'cli') {
+    echo $videoUrl .  "\n";
+} else {
+    header('Video-URL: ' . $videoUrl);
 }
+playVideoOnDreambox($videoUrl, $dreamboxUrl);
 
-function playVideoOnDreambox($videoUrl, $dreamboxHost)
-{
-    ini_set('track_errors', 1);
-    $xml = file_get_contents('http://' . $dreamboxHost . '/web/session');
-    if ($xml === false) {
-        errorOut('Failed to fetch dreambox session token: ' . $php_errormsg);
-    }
-    $sx = simplexml_load_string($xml);
-    $token = (string) $sx;
-
-    $playUrl = 'http://' . $dreamboxHost
-        . '/web/mediaplayerplay'
-        . '?file=4097:0:1:0:0:0:0:0:0:0:'
-        . str_replace('%3A', '%253A', rawurlencode($videoUrl));
-
-    $ctx = stream_context_create(
-        array(
-            'http' => array(
-                'method'  => 'POST',
-                'header'  => 'Content-type: application/x-www-form-urlencoded',
-                'content' => 'sessionid=' . $token,
-                //'ignore_errors' => true
-            )
-        )
-    );
-    $ret = file_get_contents($playUrl, false, $ctx);
-    if ($ret !== false) {
-        header('HTTP/1.0 200 OK');
-        echo "Video play request sent to dreambox\n";
-        exit(0);
-    } else {
-        errorOut(
-            'Failed to send video play request to dreambox: ' . $php_errormsg
-        );
-    }
-}
 
 function errorInput($msg)
 {
-    header('HTTP/1.0 400 Bad Request');
-    header('Content-type: text/plain');
+    if (!headers_sent()) {
+        header('HTTP/1.0 400 Bad Request');
+        header('Content-type: text/plain');
+    }
     echo $msg . "\n";
     exit(1);
 }
 
 function errorOut($msg, $httpStatus = '500 Internal Server Error')
 {
-    header('HTTP/1.0 ' . $httpStatus);
-    header('Content-type: text/plain');
+    if (!headers_sent()) {
+        header('HTTP/1.0 ' . $httpStatus);
+        header('Content-type: text/plain');
+    }
     echo $msg . "\n";
+    syslog(LOG_ERR, 'playVideoOnDreamboxProxy: ' . $httpStatus . ': ' . $msg);
     exit(2);
 }
-?>
\ No newline at end of file
+?>