4 var $active; // Once it actually is...
9 * Create a minimally initialised connection object
11 function vlcConnection() {
12 $this->active
= false;
14 $this->status
= array();
18 * Connect to the remote vlc
23 $this->vlc
= stream_socket_client($c->vlc
['connection'], $errno, $errstr, 2);
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+) \((.+)\)";
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;
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];
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" );
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 ) {
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() {
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'];
130 * Query the vlc about it's current playlist and position
132 function GetQueue() {
135 $this->UpdateStatus();
136 $this->queue
= array();
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];
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 ) {
173 error_log("adorno: DBG: Trying to play '$path'");
179 * Get a list of the files currently queued for the future
181 function daemon_get_queue() {
183 $q = $vlc->GetQueue();
189 * Get the currently playing track and it's starting time
191 function daemon_current_track() {
193 return $vlc->GetCurrent();
198 * Get the currently playing track and it's starting time
200 function daemon_other_command( $action, $track ) {
204 $vlc->Daemon($action);
207 $vlc->Daemon($action);
210 error_log("adorno: ERROR: Unsupported command '$action'" );