+++ /dev/null
-<?php
-
-$mediaraspclient = null;
-
-function _remoteCmd($cmd, $device)
-{
- if (!remoteCmd($cmd, $device)) {
- if ($device == 'bureau') {
- echo 'run ' . $cmd . ' with harmony';
- harmonyRequest('commands/' . $cmd, 'POST');
- } else {
- echo 'ok';
- }
- }
-}
-
-function remoteCmd($cmd, $device)
-{
- $cmd = mb_strtolower($cmd);
- if (strpos($cmd, 'key_') === 0) {
- $e = explode('key_', $cmd, 2);
- $cmd = $e[1];
- }
-
- if ($cmd == 'volume-up') {
- volume(config('VOLUME_STEP'), '+');
- return true;
- } elseif ($cmd == 'volume-down') {
- volume(config('VOLUME_STEP'), '-');
- return true;
- }
-
- if ($cmd == 'light-up') {
- if ($device == 'salon') {
- execScene('salon/on');
- } else if ($device == 'bureau') {
- execScene('bureau/auto');
- }
- return;
- } else if ($cmd == 'light-down') {
- if ($device == 'salon') {
- execScene('salon/cinema');
- } else if ($device == 'bureau') {
- execScene('bureau/cinema');
- }
- return;
- }
-
- $playing = getPlaying($device);
- $e = explode(':', $playing);
- $p = $e[0];
- if ($p == 'squeezebox') {
- $player = config('SQUEEZEBOX_PLAYER');
- remoteCmdSqueezebox($cmd, $player);
- return true;
- }
-
- if ((($p === 'netflix' || $p === 'tv' || $p==='amazonprime') && config('TVPLAYER') === 'shield') || config('VIDEOPLAYER') === 'shield') {
- return shieldRemoteCommand($cmd, $device);
- } else if ($device == 'bureau') {
- switch (getCurrentHarmonyActivity()) {
- case HARMONY_MUSIQUE:
- return remoteCmdSalonMusique($cmd);
- case HARMONY_TV:
- return remoteCmdSalonTV($cmd);
- }
- return false;
- } else {
- return remoteCmdMedia($cmd, $device, $p);
- }
- return false;
-}
-
-function remoteCmdSqueezebox($cmd, $player)
-{
- $map = ['forward' => ['button', 'jump_fwd'], 'fast-forward' => ['button', 'jump_fwd'], 'fast-rewind' => ['button', 'jump_rew'], 'rewind' => ['button', 'jump_rew'], 'play' => ['pause'], 'pause' => ['pause'], 'stop' => 'stop'];
- if (isset($map[$cmd])) {
- squeezeRequest($map[$cmd], $player);
- }
-}
-
-function remoteCmdSalonTV($cmd)
-{
- $map = ['play' => 'play',
- 'pause' => 'play',
- 'channel-up' => 'prgm_inc',
- 'channel-down' => 'prgm_dec',
- 'channelup' => 'prgm_inc',
- 'channeldown' => 'prgm_dec',
- 'up' => 'up',
- 'left' => 'left',
- 'right' => 'right',
- 'down' => 'down',
- 'ok' => 'ok',
- 'prev-channel' => 'red',
- 'tv' => 'home',
- 'media' => 'red',
- //'mute'=>'',
- 'direction-left' => 'left',
- 'direction-right' => 'right',
- 'direction-up' => 'up',
- 'direction-down' => 'down',
- 'select' => 'ok',
- //'volume-down'=>'' ,
- //'volume-up'=>'',
- 'blue' => 'blue',
- 'yellow' => 'yellow',
- 'red' => 'red',
- 'green' => 'green',
- 'guide' => 'media',
- 'epg' => 'epg',
- 'info' => 'yellow',
- 'menu' => 'home',
- 'exit' => 'red',
- 'esc' => 'red',
- 'stop' => 'stop',
- 'forward' => 'fwd',
- 'fast-forward' => 'fwd',
- 'record' => 'rec',
- 'fast-rewind' => 'bwd',
- 'rewind' => 'bwd',
- 'next' => 'next',
- 'prev' => 'prev',
- 'numeric_0' => '0',
- 'numeric_1' => '1',
- 'numeric_2' => '2',
- 'numeric_3' => '3',
- 'numeric_4' => '4',
- 'numeric_5' => '5',
- 'numeric_6' => '6',
- 'numeric_7' => '7',
- 'numeric_8' => '8',
- 'numeric_9' => '9',
- ];
-
- if (isset($map[$cmd])) {
- freeboxKey([$map[$cmd]]);
- return true;
- }
- return false;
-}
-
-function remoteCmdMedia($cmd, $device = null, $playing = null)
-{
- if (null === $device) {
- $device = config('DEVICE');
- }
-
- profile('remote cmd ' . $cmd . ' on ' . $device);
-
- $map = ['play' => 'pl_pause',
- 'pause' => 'pl_pause',
- 'stop' => 'pl_stop',
- 'exit' => 'pl_stop',
- 'title' => 'm',
- 'rewind' => 'rewind',
- 'forward' => 'forward',
- 'fast-rewind' => 'fast-rewind',
- 'fast-forward' => 'fast-forward',
- 'info' => 'z',
- 'direction-left' => 'down',
- 'direction-right' => 'up',
- 'subtitles-toggle' => 'subtitles-toggle',
- 'subtitles-stream-next' => 'subtitles-stream-next',
- 'subtitles-stream-prev' => 'subtitles-stream-prev',
- 'subtitles-increase-delay' => 'subtitles-increase-delay',
- 'subtitles-increase-delay-long' => 'subtitles-increase-delay-long',
- 'subtitles-decrease-delay' => 'subtitles-decrease-delay',
- 'subtitles-decrease-delay-long' => 'subtitles-decrease-delay-long',
- 'audio-track-next' => 'audio-track-next',
- ];
-
-
- if ($device == 'bureau') {
- $map['volume-up'] = '+';
- $map['volume-down'] = '-';
- $map['mute'] = '-';
- }
-
- if (!isset($map[$cmd])) {
- return false;
- }
-
- $key = $map[$cmd];
-
-
- if (null === $playing || $playing == 'mediarasp') {
- if ($key == 'subtitles-toggle') {
- sendHotKey('shift+v');
- } else if ($key == 'subtitles-stream-next') {
- sendHotKey('v');
- } else if ($key == 'subtitles-stream-prev') {
- sendHotKey('alt+v');
- } else if ($key == 'subtitles-increase-delay') {
- sendHotKey('h', 5);
- } else if ($key == 'subtitles-decrease-delay') {
- sendHotKey('g', 5);
- } else if ($key == 'subtitles-increase-delay-long') {
- sendHotKey('h', 40);
- } else if ($key == 'subtitles-decrease-delay-long') {
- sendHotKey('g', 40);
- } else if ($key == 'audio-track-next') {
- sendHotKey('b');
- } else if ($key == 'rewind' || $key == 'fast-rewind' || $key == 'forward' || $key == 'fast-forward') {
- if ($key == 'rewind' || $key == 'forward') {
- $amount = 15;
- } else if ($key == 'fast-rewind' || $key == 'fast-forward') {
- $amount = 300;
- }
- if ($key == 'rewind' || $key == 'fast-rewind') {
- $way = '-';
- } else if ($key == 'forward' || $key == 'fast-forward') {
- $way = '+';
- }
- $res = VLCCmd(['command' => 'seek', 'val' => $way . $amount . 's'], $device);
- } else {
- $params = ['command' => $key];
- $res = VLCCmd($params, $device);
- }
- echo $res;
- }
-
- return true;
-}
-
-function sendHotKey($key, $times = 1, $device = null)
-{
- $keys = str_repeat(' key ' . $key, $times);
- return sshCommand('DISPLAY=:0 xdotool getactivewindow' . $keys, $device, true, false);
-}
-
-function moveVLC($fullscreen = true, $device = null)
-{
- if ($fullscreen) {
- $e = '0,0,800,480';
- } else {
- $e = '163,128,414,224';
- }
- return sshCommand('DISPLAY=:0 wmctrl -r "VLC" -e 0,' . $e . ';DISPLAY=:0 wmctrl -i -r "VLC" -b add,above;DISPLAY=:0 /usr/local/bin/removeallborders', $device, true, false);
-}
-
-function remoteCmdSalonMusique($cmd)
-{
- switch ($cmd) {
- case 'PLAY':
- case 'PAUSE':
-
- break;
- }
- return false;
-}
-
-function mediaRaspPlayMovie($movie, $device = null)
-{
- setPlaying('mediarasp:' . $movie, $device);
-
- profile('mediaRaspPlayMovie ' . $movie, __FILE__, __LINE__);
-
- $m = explode('.', $movie);
- $ext = array_pop($m);
- $x264 = false;
- if ($m[count($m) - 1] == 'x264') {
- array_pop($m);
- $x264 = true;
- }
- $base = implode('.', $m);
-
- addSeen($movie);
- $res = VLCPlay($movie, $device);
-
- setPlaying('mediarasp:' . $movie, $device);
- return $res;
-}
-
-function addSeen($movie)
-{
- $redis = connectRedis();
-
- try {
- $seen = $redis->igbget('mediaseen');
- } catch (Exception $e) {
- $seen = [];
- }
-
- if (!is_array($seen)) {
- $seen = [];
- }
-
- $cacheFile = ROOT . '/cache/seen.json';
- if (!count($seen)) {
- $seen = json_decode(file_get_contents($cacheFile), true);
- }
-
- $seen[str_replace('/nas/', '/volume1/Share/', $movie)] = true;
- $redis->igbset('mediaseen', $seen);
-
- file_put_contents($cacheFile, json_encode($seen));
-}
-
-function mediaRaspCommons($device)
-{
- global $squeezeboxPlayers;
- if ($device == 'salon') {
- stopSqueezebox('Salon');
- } else if ($device == 'bureau') {
- if (getState('bureauPlaying', '0') === '0') {
- execScene('bureau/media/play');
- }
- stopSqueezebox($squeezeboxPlayers['Bureau']);
- if (!isAlive('bureau')) {
- sleep(60);
- }
- } else if ($device == 'sdb') {
- execScene('sdb/ampli/on');
- }
-}
-
-function mediaRaspPlayStream($url, $device = null)
-{
- setPlaying('mediarasp:' . $url, $device);
- mediaRaspCommons($device);
- $res = VLCPlay($url, $device);
- setPlaying('mediarasp:' . $url, $device);
- mediaRaspCommons($device);
- return $res;
-}
-
-function mediaRaspPlayCast($castURL, $device = null)
-{
- if (null === $device) {
- $device = config('DEVICE');
- }
- if ($device === 'bureau') {
- $height = 720;
- } else if ($device === 'salon') {
- $height = 1080;
- } else {
- $height = 640;
- }
-
- setPlaying('mediarasp:cast:' . $castURL);
- mediaRaspCommons($device);
- $dest = downloadYoutubeDL($castURL, $height, true, 'mkv');
- getYoutubeDLMeta($castURL);
- $res = VLCPlay($dest, $device);
- setPlaying('mediarasp:cast:' . $castURL);
- return $res;
-}
-
-function getYoutubeDLCastURL($url, $height = 1080)
-{
- $h = '';
- if ($height != 0) {
- $h = '[height<=' . $height . ']';
- }
- $cmd = "/usr/local/bin/youtube-dl -f 'best$h' --get-url '$url'";
- $res = `$cmd`;
- echo $res;
- return trim($res);
-}
-
-function getYoutubeDLPath($url, $h = 0, $ext = 'mkv')
-{
- $hash = $url;
- if ($h != 0) {
- $hash .= '--' . $h . '++';
- }
- $hash = sha1($hash);
- return "/nas/Videos/YoutubeDL/" . $hash . '.' . $ext;
-}
-
-function downloadYoutubeDL($url, $height = 1080, $cast = true, $ext = 'mkv')
-{
- $dest = getYoutubeDLPath($url, $height, $ext);
- $wip = $dest . '.wip';
- if (!file_exists($dest)) {
- if (!file_exists($wip)) {
- $convert = 'https://' . $_SERVER['HTTP_HOST'] . '/scripts/youtubedl-download.php';
- try {
- httpRequest($convert, 'get', ['url' => $url, 'h' => $height, 'ext' => $ext], null, $cast ? 1 : 0);
- } catch (Exception $e) {
-
- }
- if (!$cast) {
- return $dest;
- }
- }
- if ($cast) {
- return getYoutubeDLCastURL($url, $height);
- }
- } else {
- return $dest;
- }
-}
-
-function getYoutubeDLMeta($url, $isHash = false)
-{
- if (!$isHash) {
- $hash = sha1($url);
- } else {
- $hash = $url;
- }
- $meta = connectRedis()->igbget('youtubedl.' . $hash);
-
- if ($meta === false && !$isHash) {
- $locale = 'en_US.UTF8';
- setlocale(LC_ALL, $locale);
- putenv('LC_ALL=' . $locale);
- $res = `LC_ALL=$locale;/usr/local/bin/youtube-dl --no-warnings -e --get-thumbnail --get-duration $url`;
- $lines = explode("\n", $res, 3);
-
- // Duration
- $d = explode(':', trim($lines[2]));
- $d = array_reverse($d);
- $duration = 0;
- $m = 1;
- foreach ($d as $item) {
- $duration += $item * $m;
- $m *= 60;
- }
-
- $meta = ['title' => trim($lines[0]), 'thumbnail' => trim($lines[1]), 'duration' => $duration];
- connectRedis()->igbset('youtubedl.' . $hash, $meta);
- }
- return $meta;
-}
-
-function cleanUpSubtitle($srtcontent)
-{
- // Convert to UTF-8
- $srtenc = mb_detect_encoding($srtcontent, 'UTF-8, ISO-8859-1');
- if ($srtenc !== 'UTF-8') {
- $srtcontent = mb_convert_encoding($srtcontent, 'UTF-8', $srtenc);
-
- }
- // Remove tags
- $srtcontent = preg_replace('/\{\\\\[^}]+\}/m', '', $srtcontent);
-
- // Fix oe
- $srtcontent = str_replace('\9c', 'oe', $srtcontent);
-
-
- return $srtcontent;
-}
-
-/**
- * @return \GuzzleHttp\Client
- */
-function getMediaRaspClient()
-{
- global $mediaraspclient;
-
- if ($mediaraspclient == null) {
- $mediaraspclient = getHttpClient();
- }
- return $mediaraspclient;
-}
-
-function getDevice($device = null)
-{
- global $devices;
- if (is_array($device) && isset($device['host'])) {
- return $device;
- }
- if (null === $device) {
- $device = config('DEVICE');
- }
- return $devices[$device];
-}
-
-function VLCCmd($params, $device = null, $timeout = 5)
-{
- $d = getDevice($device);
- if (isset($params['query'])) {
- $query = $params['query'];
- unset($params['query']);
- } else {
- $query = 'requests/status.xml';
- }
- $url = 'http://' . $d['host'] . ':8754/' . $query;
- $client = getMediaRaspClient();
- try {
- $response = $client->get($url, ['query' => $params, 'timeout' => $timeout, 'auth' => ['', 'atacama']]);
- return $response->getBody();
- } catch (Exception $e) {
- echo $d['host'] . print_r($params) . ':' . $e->getMessage();
- return false;
- }
-}
-
-function VLCPlay($fileorstream, $device = null, $timeout = 5)
-{
- $device = getDevice($device);
- if (config('VIDEOPLAYER') === 'shield') {
- return shieldKodi($fileorstream);
- } else {
- VLCCmd(['command' => 'pl_empty'], $device, $timeout);
- if (file_exists($fileorstream)) {
- $fileorstream = 'file://' . $fileorstream;
- }
- echo $fileorstream;
- if ($device === 'bureau') {
- sshCommand('/usr/local/bin/soundtohdmi', $device, true, true);
- }
- return VLCCmd(['command' => 'in_play', 'input' => $fileorstream], $device, $timeout);
- }
-}
-
$p = $e[0];
if (config('TVPLAYER') === 'shield') {
- $visualApps = ['arte', 'francetv', 'mycanal', 'youtube', 'dailymotion', 'vimeo', 'kodi', 'vlc'];
+ $visualApps = ['arte', 'francetv', 'mycanal', 'youtube', 'dailymotion', 'vimeo'];
if (in_array(shieldGetCurrentApp(), $visualApps)) {
if ($p === 'squeezebox') {
setPlaying('shield');
$res = squeezeboxRemoteInfos();
} else {
if (config('TVPLAYER') === 'shield') {
- if (shieldGetCurrentApp() === 'kodi') {
- $res = ['type' => 'shieldmedia', 'can_seek' => true];
- while (true) {
- try {
- $req = httpRequest('http://' . getDevice()['shield'] . ':8754/jsonrpc?Base=1', 'post', '[{"jsonrpc":"2.0","method":"Player.GetProperties","params":[1,["playlistid","speed","position","totaltime","time","percentage","shuffled","repeat","canrepeat","canshuffle","canseek","partymode"]],"id":1},{"jsonrpc":"2.0","method":"Player.GetItem","params":[1,["title","thumbnail","file","artist","genre","year","rating","album","track","duration","playcount","dateadded","episode","artistid","albumid","tvshowid","fanart"]],"id":2}]', ['kodi', 'atacama'], 10, true, ['X-Requested-With' => 'XMLHttpRequest', 'Content-type' => 'application/json']);
- break;
- } catch (Exception $e) {
-
- }
- sleep(2);
- }
- $p = json_decode($req->getBody(), true);
- $res['duration'] = '';
- $res['time'] = '';
- $res['title'] = '';
- $res['artwork'] = '';
- $res['status'] = json_encode($p);
- if (isset($p[0]['result'])) {
- $r = $p[0]['result'];
- $res['can_seek'] = $r['canseek'];
- $res['duration'] = 3600 * $r['totaltime']['hours'] + 60 * $r['totaltime']['minutes'] + $r['totaltime']['seconds'];
- $res['time'] = 3600 * $r['time']['hours'] + 60 * $r['time']['minutes'] + $r['time']['seconds'];
- }
- if (isset($p[1]['result']['item']['file'])) {
- $res = array_merge($res, getTitleAndArtwork($p[1]['result']['item']['file']));
- }
- } else {
$res = ['type' => 'shield', 'can_seek' => false];
- }
-
} else {
if (($p === 'netflix' || $p === 'tv' || $p==='amazonprime') && config('TVPLAYER') === 'shield') {
$res = ['type' => 'shield', 'can_seek' => false];
function remoteProgress($progress)
{
if (config('TVPLAYER') === 'shield') {
- if (shieldGetCurrentApp() === 'kodi') {
- $p = json_decode(httpRequest('http://' . getDevice()['shield'] . ':8754/jsonrpc', 'post', '[{"jsonrpc":"2.0","method":"Player.Seek","params":[1,' . (round($progress * 100)) . '],"id":3}]', ['kodi', 'atacama'], 10, true, ['X-Requested-With' => 'XMLHttpRequest', 'Content-type' => 'application/json'])->getBody(), true);
- }
return;
}