3 error_log( "Using VLC backend: ". $c->daemon_type
);
5 var $active; // Once it actually is...
10 * Create a minimally initialised connection object
12 function vlcConnection() {
13 $this->active
= false;
15 $this->status
= array();
19 * Connect to the remote vlc
25 $this->vlc
= stream_socket_client($c->vlc
['connection'], $errno, $errstr, 1);
27 error_log( sprintf( "vlcPlayer: ERROR: Failed to connect to '%s'. Error %d: %s", $c->vlc
['connection'], $errno, $errstr) );
28 $this->vlc
= 'connection failed';
31 error_log( "vlcPlayer: Connected to ".$c->vlc
['connection'] );
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+) \((.+)\)";
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;
64 error_log( "vlcPlayer: Returning result of ".$say );
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];
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 ) {
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() {
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'];
143 * Query the vlc about it's current playlist and position
145 function GetQueue() {
148 $this->UpdateStatus();
149 $this->queue
= array();
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];
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 ) {
186 error_log("adorno: DBG: Trying to play '$path'");
192 * Get a list of the files currently queued for the future
194 function daemon_get_queue() {
196 $q = $vlc->GetQueue();
202 * Get the currently playing track and it's starting time
204 function daemon_current_track() {
206 return $vlc->GetCurrent();
211 * Get the currently playing track and it's starting time
213 function daemon_other_command( $action, $track ) {
217 $vlc->Daemon($action);
220 $vlc->Daemon($action);
223 error_log("adorno: ERROR: Unsupported command '$action'" );