First cut at a controller for a VLC backend.
[adorno.git] / inc / vlcPlayer.php
blob4aae07926ffc9d3df93e88136b79367216966ae5
1 <?php
3 class vlcConnection {
4 var $active; // Once it actually is...
5 var $vlc;
6 var $status;
8 /**
9 * Create a minimally initialised connection object
11 function vlcConnection() {
12 $this->active = false;
13 $this->vlc = false;
14 $this->status = array();
17 /**
18 * Connect to the remote vlc
20 function Connect() {
21 global $c;
23 $this->vlc = stream_socket_client($c->vlc['connection'], $errno, $errstr, 2);
24 $this->active = true;
27 /**
28 * Do a question/response pair with the daemon
30 function Daemon( $say, $end_pattern = 'command' ) {
31 if ( ! $this->active ) $this->Connect();
33 if ( $end_pattern == 'command' ) {
34 $command = preg_replace( '/\s.*$/', '', $say);
35 $end_pattern = "^$command: returned (\d+) \((.+)\)";
38 $say .= "\n";
39 fwrite($this->vlc, $say );
40 stream_set_timeout($this->vlc, 0, 50000);
42 $result = (object) array( 'text' => "" );
43 while ( ! feof($this->vlc) ) {
44 $line = fgets($this->vlc, 8192);
45 // echo "<p>$line</p>\n";
46 $result->text .= $line;
47 if ( preg_match( "/$end_pattern/", $line, $matches) ) {
48 $result->matches = $matches;
49 break;
53 return $result;
57 /**
58 * Query the vlc for a simple command which just returns a single data value
60 function DaemonSimple( $command ) {
61 $response = $this->Daemon( $command, '^(\S+)\s*$' );
62 return $response->matches[1];
66 /**
67 * Query the vlc about it's current status
69 function UpdateStatus() {
70 $this->status = array();
72 $response = $this->Daemon( "status" );
73 $lines = split( "\n", $response->text );
74 foreach( $lines AS $k => $v ) {
75 $v = str_replace( 'status change: ( ', '', $v);
76 $v = preg_replace( '/ \)\s*$/', '', $v );
77 list( $key, $value ) = preg_split( '/:\s*/', $v, 2);
78 $this->status[$key] = $value;
81 $this->status['time'] = $this->DaemonSimple( "get_time" );
82 $this->status['started'] = date( 'Y-m-d H:i:s', time() - $this->status['time']);
83 $this->status['length'] = $this->DaemonSimple( "get_length" );
85 $this->status['state'] = ( $this->DaemonSimple("is_playing") == "1" ? "play" : "stop" );
89 /**
90 * Play a track. If the current status is 'stop' then we
91 * first clear the playlist and after tell vlc to play.
93 function Play( $track ) {
94 global $c;
96 $this->UpdateStatus();
97 $stopped = ( $this->status['state'] == 'stop' );
99 $result = $this->Daemon( sprintf('%s %s', ( $stopped ? 'add' : 'enqueue'), $track) );
101 // echo "<p>".$result->text."</p>\n";
106 * Query the vlc about it's current playlist and position
108 function GetCurrent() {
109 global $c;
111 $this->UpdateStatus();
112 $songnow = (object) array();
113 if ( $this->status['state'] == 'stop' ) return $songnow;
115 $response = $this->Daemon( "playlist" );
116 $current = split( "\n", $response->text );
117 foreach( $current AS $k => $v ) {
118 if ( preg_match( '/^\|\*.* (.*)\|([^\|]*)\|\s*$/', $v, $matches ) ) {
119 $songnow->track = $matches[1];
120 $songnow->type = $matches[2];
123 if ( $this->status['state'] == 'play' ) {
124 $songnow->started = $this->status['started'];
126 return $songnow;
130 * Query the vlc about it's current playlist and position
132 function GetQueue() {
133 global $c;
135 $this->UpdateStatus();
136 $this->queue = array();
138 $current = "";
140 $response = $this->Daemon( "playlist" );
141 $playlist = split( "\n", $response->text );
142 foreach( $playlist AS $k => $v ) {
143 if ( $current != "" ) {
144 if ( preg_match( '/^\| .* (.*)\|([^\|]*)\|\s*$/', $v, $matches ) ) {
145 $this->queue[] = $matches[1];
148 elseif ( preg_match( '/^\|\*.* (.*)\|([^\|]*)\|\s*$/', $v, $matches ) ) {
149 $current = $matches[1];
153 return $this->queue;
158 $GLOBALS["vlc"] = new vlcConnection();
164 /******************************************************************
165 * The actual API the web interface calls is then very simple...
166 ******************************************************************/
169 * Queue a file for playing
171 function daemon_play_track( $path ) {
172 global $vlc;
173 error_log("adorno: DBG: Trying to play '$path'");
174 $vlc->Play( $path );
179 * Get a list of the files currently queued for the future
181 function daemon_get_queue() {
182 global $vlc;
183 $q = $vlc->GetQueue();
184 return $q;
189 * Get the currently playing track and it's starting time
191 function daemon_current_track() {
192 global $vlc;
193 return $vlc->GetCurrent();
198 * Get the currently playing track and it's starting time
200 function daemon_other_command( $action, $track ) {
201 global $vlc;
202 switch( $action ) {
203 case 'pause':
204 $vlc->Daemon($action);
205 break;
206 case 'next':
207 $vlc->Daemon($action);
208 break;
209 default:
210 error_log("adorno: ERROR: Unsupported command '$action'" );
213 return true;