summaryrefslogtreecommitdiffstats
path: root/modules/vlc/vlcplayer.class.php
blob: d124b1a41890e22ee74bf56e6b0748493e3de85b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
<?php 
/* vim:set tabstop=8 softtabstop=8 shiftwidth=8 noexpandtab: */
/**
 *
 * LICENSE: GNU General Public License, version 2 (GPLv2)
 * Copyright 2001 - 2013 Ampache.org
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License v2
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 */

/**
 * VlcPlayer Class
 *
 * This player controls an instance of Vlc webinterface 
 * which in turn controls vlc. All functions 
 * return null on failure.
 *
 */
class VlcPlayer {

      public $host;
      public $port;
    public $password;

    /**
     * VlcPlayer
     * This is the constructor, it defaults to localhost
     * with port 8080
     * i would change this to another value then standard 8080, it gets used by more things
     */    
    public function VlcPlayer($h = "localhost", $pw = "", $p = 8080) {

        $this->host = $h;
        $this->port = $p;
        $this->password = $pw;

    } // VlcPlayer
      
    /**
     * add
     * append a song to the playlist
     * $name    Name to be shown in the playlist
     * $url        URL of the song
     */      
      public function add($name, $url) {

      
                   $aurl = urlencode($url); 
                   $aurl .= "&";
                   $aurl .= urlencode($name);
                   
        $args = array('command'=>'in_enqueue','&input'=>$aurl);
        $results = $this->sendCommand('status.xml?', $args);
        if (is_null($results)) { return null; }

        return true;

    } // add

    /**
     * version
     * No version returned in the standard xml file, just need to check for xml returned 
     */
    public function version() { 

        $args = array(); 
        $results = $this->sendCommand('status.xml',$args); 
        if (is_null($results)) { return null; }
       
        return true; 

    } // version

    /**
     * clear
     * clear the playlist
     * Every command returns status.xml no other way
     */      
    public function clear() {
        $args = array('command'=>'pl_empty');
        $results = $this->sendCommand('status.xml?', $args);
         if (is_null($results)) { return null; }

        return true; 
    
    } // clear
    
    /**
     * next
     * go to next song
     */      
    public function next() {

        $args = array('command'=>'pl_next');
        $results = $this->sendCommand('status.xml?', $args);
        if (is_null($results)) { return null; }

        return true; 

    } // next        

    /**
     * prev
     * go to previous song
     */      
    public function prev() {

        $args = array('command'=>'pl_previous');
        $results = $this->sendCommand("status.xml?", $args);
        if (is_null($results)) { return null; }
    
        return true;

    } // prev    

    /**
     * skip
     * This skips to POS in the playlist
     */
    public function skip($pos) { 

        $args = array('command'=>'pl_play','&id'=>$pos); 
        $results = $this->sendCommand('status.xml?',$args); 
        if (is_null($results)) { return null; }

        // Works but if user clicks next afterwards player goes to first song our last song played before 

        return true; 

    } // skip
    
    /** 
     * play
     * play the current song
     */      
    public function play() {

        $args = array('command'=>'pl_play');
        $results = $this->sendCommand("status.xml?", $args);
         if (is_null($results)) { return null; }

        return true; 

    } // play    
        
    /** 
     * pause
     * toggle pause mode on current song
     */      
    public function pause() {

        $args = array('command'=>'pl_pause');
        $results = $this->sendCommand("status.xml?", $args);
        if (is_null($results)) { return null; }

        return true; 

    } // pause
    
    /** 
     * stop
     * stops the current song amazing!
     */      
    public function stop() {

        $args = array('command'=>'pl_stop');
        $results = $this->sendCommand('status.xml?', $args);
        if (is_null($results)) { return null; } 

        return true; 

    } // stop            

    /** 
      * repeat
     * This toggles the repeat state of Vlc
     */
    public function repeat($value) { 
        
        $args = array('command'=>'pl_repeat'); 
        $results = $this->sendCommand('status.xml?',$args); 
        if (is_null($results)) { return null; }

        return true;  

    } // repeat

    /** 
     * random
     * this toggles the random state of Vlc
     */
    public function random($value) { 

        $args = array('command'=>'pl_random'); 
        $results = $this->sendCommand('status.xml?',$args); 
        if (is_null($results)) { return null; }

        return true; 

    } // random

    /**
     * delete_pos
     * This deletes a specific track
     */
    public function delete_pos($track) { 
                          
        $args = array('command'=>'pl_delete','&id'=>$track); 
        $results = $this->sendCommand('status.xml?',$args); 
        if (is_null($results)) { return null; }   

        return true; 

    } // delete_pos

    /**
     * state
     * This returns the current state of the Vlc player
     */
    public function state() { 

        $args = array(); 
        
        $results = $this->sendCommand('status.xml',$args);
        $currentstat = $results['root']['state']['value'];

        if ($currentstat == 'playing') { $state = 'play'; } 
        if ($currentstat == 'stop') { $state = 'stop'; } 
        if ($currentstat == 'paused') { $state = 'pause'; } 
        
        return $state; 

    } // state

    /**
     * extract the full state from the xml file and send to status in vlccontroller for further parsing.
     *  
     */
    public function fullstate() {
        $args = array();
        
        $results = $this->sendCommand('status.xml',$args);
        if (is_null($results)) { return null; } 
        return $results;
        
    }  //fullstate
   

    /**
     * volume_up
     * This increases the volume of vlc , set to +20 can be changed to your preference
     */
    public function volume_up() { 

        $args = array('command'=>'volume','&val'=>'%2B20'); 
        $results = $this->sendCommand('status.xml?',$args); 
         if (is_null($results)) { return null; }  

        return true; 

    } // volume_up

    /**
     * volume_down
     * This decreases the volume of vlc, can be set to your preference
     */
    public function volume_down() { 

        $args = array('command'=>'volume','&val'=>'-20'); 
        $results = $this->sendCommand('status.xml?',$args); 
        if (is_null($results)) { return null; }

        return true; 

    } // volume_down

    /**
     * set_volume
     * This sets the volume as best it can, i think it's from 0 to 400, need more testing'
     */
    public function set_volume($value) { 

        // Convert it to base 400
        $value = $value*4; 
        $args = array('command'=>'volume','&val'=>$value); 
        $results = $this->sendCommand('status.xml?',$args); 
        if (is_null($results)) { return null; }

        return true; 

    } // set_volume

    /**
     * clear_playlist
     * this flushes the playlist cache (I hope this means clear)
     */
    public function clear_playlist() { 

        $args = array('command'=>'pl_empty'); 
        $results = $this->sendcommand('status.xml?',$args); 
        if (is_null($results)) { return null; }

        return true; 

    } // clear_playlist

     /**
     * get_tracks
     * This returns a delimiated string of all of the filenames
     * current in your playlist, only url's at the moment,normal files put in the playlist with vlc wil not show'
     */
    public function get_tracks() { 

        // Gets complete playlist + medialib in vlc's case, needs to be looked at
        $args = array();
        
        $results = $this->sendCommand('playlist.xml',$args);
        if (is_null($results)) { return null; }
    
        return $results; 

    } // get_tracks

    /** 
      * sendCommand
     * This is the core of this library it takes care of sending the HTTP
     * request to the vlc server and getting the response 
     */    
    private function sendCommand($cmd, $args) {

          $fp = fsockopen($this->host, $this->port, $errno, $errstr); 

          if(!$fp) {
                debug_event('vlc',"VlcPlayer: $errstr ($errno)",'1');
            return null; 
          } 

        // Define the base message  
        $msg = "GET /requests/$cmd";              

        // Foreach our arguments 
        foreach ($args AS $key => $val) {
            $msg = $msg . "$key=$val";                    
        }

              $msg = $msg . " HTTP/1.0\r\n\r\n";  
                       
            fputs($fp, $msg);
        $data = '';
        $header = "";
             // here the header is split from the xml to avoid problems
            do // loop until the end of the header
               {
                $header .= fgets ( $fp );

               } while ( strpos ( $header, "\r\n\r\n" ) === false );

        // now put the body in variable $data

             while ( ! feof ( $fp ) )
                {
                  $data .= fgets ( $fp );
                }
        
          fclose($fp);

        // send to xml parser and make an array
        
        $result = $this->xmltoarray($data);
        
        
        return $result; 

    } // sendCommand
    
//this function parses the xml page into an array thx to bin-co
//warning vlc returns it's complete media lib if asked for playlist
   private function xmltoarray($contents, $get_attributes=1, $priority = 'attribute') {
        if(!$contents) return array();

        if(!function_exists('xml_parser_create')) {
        //print "'xml_parser_create()' function not found!";
        return array();
        }

    //Get the XML parser of PHP - PHP must have this module for the parser to work
    $parser = xml_parser_create('');
    xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, "UTF-8"); # http://minutillo.com/steve/weblog/2004/6/17/php-xml-and-character-encodings-a-tale-of-sadness-rage-and-data-loss
    xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
    xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
    xml_parse_into_struct($parser, trim($contents), $xml_values);
    xml_parser_free($parser);

      if(!$xml_values) return;//Hmm...

    //Initializations
    $bigxml_array = array();
    $parents = array();
    $opened_tags = array();
    $arr = array();

    $current = &$bigxml_array; //Refference

    //Go through the tags.
    $repeated_tag_index = array();//Multiple tags with same name will be turned into an array
    foreach($xml_values as $data) {
           unset($attributes,$value);//Remove existing values, or there will be trouble

           //This command will extract these variables into the foreach scope
           // tag(string), type(string), level(int), attributes(array).
           extract($data);//We could use the array by itself, but this cooler.

           $result = array();
           $attributes_data = array();
        
           if(isset($value)) {
            if($priority == 'tag') $result = $value;
            else $result['value'] = $value; //Put the value in a assoc array if we are in the 'Attribute' mode
    }

        //Set the attributes too.
            if(isset($attributes) and $get_attributes) {
               foreach($attributes as $attr => $val) {
                   if($priority == 'tag') $attributes_data[$attr] = $val;
                   else $result['attr'][$attr] = $val; //Set all the attributes in a array called 'attr'
               }
            }

        //See tag status and do the needed.
        if($type == "open") {//The starting of the tag '<tag>'
            $parent[$level-1] = &$current;
            if(!is_array($current) or (!in_array($tag, array_keys($current)))) { //Insert New tag
                $current[$tag] = $result;
                if($attributes_data) $current[$tag. '_attr'] = $attributes_data;
                $repeated_tag_index[$tag.'_'.$level] = 1;

                $current = &$current[$tag];

            } else { //There was another element with the same tag name

                if(isset($current[$tag][0])) {//If there is a 0th element it is already an array
                    $current[$tag][$repeated_tag_index[$tag.'_'.$level]] = $result;
                    $repeated_tag_index[$tag.'_'.$level]++;
                } else {//This section will make the value an array if multiple tags with the same name appear together
                    $current[$tag] = array($current[$tag],$result);//This will combine the existing item and the new item together to make an array
                    $repeated_tag_index[$tag.'_'.$level] = 2;
                    
                    if(isset($current[$tag.'_attr'])) { //The attribute of the last(0th) tag must be moved as well
                        $current[$tag]['0_attr'] = $current[$tag.'_attr'];
                        unset($current[$tag.'_attr']);
                    }

                }
                $last_item_index = $repeated_tag_index[$tag.'_'.$level]-1;
                $current = &$current[$tag][$last_item_index];
            }

        } elseif($type == "complete") { //Tags that ends in 1 line '<tag />'
            //See if the key is already taken.
            if(!isset($current[$tag])) { //New Key
                $current[$tag] = $result;
                $repeated_tag_index[$tag.'_'.$level] = 1;
                if($priority == 'tag' and $attributes_data) $current[$tag. '_attr'] = $attributes_data;

            } else { //If taken, put all things inside a list(array)
                if(isset($current[$tag][0]) and is_array($current[$tag])) {//If it is already an array...

                    // ...push the new element into that array.
                    $current[$tag][$repeated_tag_index[$tag.'_'.$level]] = $result;
                    
                    if($priority == 'tag' and $get_attributes and $attributes_data) {
                        $current[$tag][$repeated_tag_index[$tag.'_'.$level] . '_attr'] = $attributes_data;
                    }
                    $repeated_tag_index[$tag.'_'.$level]++;

                } else { //If it is not an array...
                    $current[$tag] = array($current[$tag],$result); //...Make it an array using using the existing value and the new value
                    $repeated_tag_index[$tag.'_'.$level] = 1;
                    if($priority == 'tag' and $get_attributes) {
                        if(isset($current[$tag.'_attr'])) { //The attribute of the last(0th) tag must be moved as well
                            
                            $current[$tag]['0_attr'] = $current[$tag.'_attr'];
                            unset($current[$tag.'_attr']);
                        }
                        
                        if($attributes_data) {
                            $current[$tag][$repeated_tag_index[$tag.'_'.$level] . '_attr'] = $attributes_data;
                        }
                    }
                    $repeated_tag_index[$tag.'_'.$level]++; //0 and 1 index is already taken
                }
            }

        } elseif($type == 'close') { //End of tag '</tag>'
            $current = &$parent[$level-1];
        }
    }
    
    return($bigxml_array);
}   //end xml parser

} // End VlcPlayer Class
?>