Support VLC as a backend.
[adorno.git] / inc / vlcPlayer.php
blobd1c9d7d730c9911908dd069e4274c080d39a980e
1 <?php
3 error_log( "Using VLC backend: ". $c->daemon_type );
4 class vlcConnection {
5 var $active; // Once it actually is...
6 var $vlc;
7 var $status;
9 /**
10 * Create a minimally initialised connection object
12 function vlcConnection() {
13 $this->active = false;
14 $this->vlc = false;
15 $this->status = array();
18 /**
19 * Connect to the remote vlc
21 function Connect() {
22 global $c;
24 if ( ! $this->vlc ) {
25 $this->vlc = stream_socket_client($c->vlc['connection'], $errno, $errstr, 1);
26 if ( ! $this->vlc ) {
27 error_log( sprintf( "vlcPlayer: ERROR: Failed to connect to '%s'. Error %d: %s", $c->vlc['connection'], $errno, $errstr) );
28 $this->vlc = 'connection failed';
29 return;
31 error_log( "vlcPlayer: Connected to ".$c->vlc['connection'] );
32 $this->active = true;
36 /**
37 * Do a question/response pair with the daemon
39 function Daemon( $say, $end_pattern = 'command' ) {
40 if ( ! $this->active ) $this->Connect();
42 $result = (object) array( 'text' => "" );
43 if ( ! $this->active ) return $result;
45 if ( $end_pattern == 'command' ) {
46 $command = preg_replace( '/\s.*$/', '', $say);
47 $end_pattern = "^$command: returned (\d+) \((.+)\)";
50 $say .= "\n";
51 fwrite($this->vlc, $say );
52 stream_set_timeout($this->vlc, 0, 50000);
54 while ( ! feof($this->vlc) ) {
55 $line = fgets($this->vlc, 8192);
56 // echo "<p>$line</p>\n";
57 $result->text .= $line;
58 if ( preg_match( "/$end_pattern/", $line, $matches) ) {
59 $result->matches = $matches;
60 break;
64 error_log( "vlcPlayer: Returning result of ".$say );
66 return $result;
70 /**
71 * Query the vlc for a simple command which just returns a single data value
73 function DaemonSimple( $command ) {
74 $response = $this->Daemon( $command, '^(\S+)\s*$' );
75 return $response->matches[1];
79 /**
80 * Query the vlc about it's current status
82 function UpdateStatus() {
83 $this->status = array();
85 $response = $this->Daemon( "status" );
86 $lines = split( "\n", $response->text );
87 foreach( $lines AS $k => $v ) {
88 $v = str_replace( 'status change: ( ', '', $v);
89 $v = preg_replace( '/ \)\s*$/', '', $v );
90 list( $key, $value ) = preg_split( '/:\s*/', $v, 2);
91 $this->status[$key] = $value;
94 $this->status['time'] = $this->DaemonSimple( "get_time" );
95 $this->status['started'] = date( 'Y-m-d H:i:s', time() - $this->status['time']);
96 $this->status['length'] = $this->DaemonSimple( "get_length" );
98 $this->status['state'] = ( $this->DaemonSimple("is_playing") == "1" ? "play" : "stop" );
103 * Play a track. If the current status is 'stop' then we
104 * first clear the playlist and after tell vlc to play.
106 function Play( $track ) {
107 global $c;
109 $this->UpdateStatus();
110 $stopped = ( $this->status['state'] == 'stop' );
112 $result = $this->Daemon( sprintf('%s %s', ( $stopped ? 'add' : 'enqueue'), $track) );
114 // echo "<p>".$result->text."</p>\n";
119 * Query the vlc about it's current playlist and position
121 function GetCurrent() {
122 global $c;
124 $this->UpdateStatus();
125 $songnow = (object) array();
126 if ( $this->status['state'] == 'stop' ) return $songnow;
128 $response = $this->Daemon( "playlist" );
129 $current = split( "\n", $response->text );
130 foreach( $current AS $k => $v ) {
131 if ( preg_match( '/^\|\*.* (.*)\|([^\|]*)\|\s*$/', $v, $matches ) ) {
132 $songnow->track = $matches[1];
133 $songnow->type = $matches[2];
136 if ( $this->status['state'] == 'play' ) {
137 $songnow->started = $this->status['started'];
139 return $songnow;
143 * Query the vlc about it's current playlist and position
145 function GetQueue() {
146 global $c;
148 $this->UpdateStatus();
149 $this->queue = array();
151 $current = "";
153 $response = $this->Daemon( "playlist" );
154 $playlist = split( "\n", $response->text );
155 foreach( $playlist AS $k => $v ) {
156 if ( $current != "" ) {
157 if ( preg_match( '/^\| .* (.*)\|([^\|]*)\|\s*$/', $v, $matches ) ) {
158 $this->queue[] = $matches[1];
161 elseif ( preg_match( '/^\|\*.* (.*)\|([^\|]*)\|\s*$/', $v, $matches ) ) {
162 $current = $matches[1];
166 return $this->queue;
171 $GLOBALS["vlc"] = new vlcConnection();
177 /******************************************************************
178 * The actual API the web interface calls is then very simple...
179 ******************************************************************/
182 * Queue a file for playing
184 function daemon_play_track( $path ) {
185 global $vlc;
186 error_log("adorno: DBG: Trying to play '$path'");
187 $vlc->Play( $path );
192 * Get a list of the files currently queued for the future
194 function daemon_get_queue() {
195 global $vlc;
196 $q = $vlc->GetQueue();
197 return $q;
202 * Get the currently playing track and it's starting time
204 function daemon_current_track() {
205 global $vlc;
206 return $vlc->GetCurrent();
211 * Get the currently playing track and it's starting time
213 function daemon_other_command( $action, $track ) {
214 global $vlc;
215 switch( $action ) {
216 case 'pause':
217 $vlc->Daemon($action);
218 break;
219 case 'next':
220 $vlc->Daemon($action);
221 break;
222 default:
223 error_log("adorno: ERROR: Unsupported command '$action'" );
226 return true;