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
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
|
<?php
/* vim:set tabstop=8 softtabstop=8 shiftwidth=8 noexpandtab: */
/*
* AmpacheVlc Class
*
* PHP version 5
*
* LICENSE: GNU General Public License, version 2 (GPLv2)
* Copyright (c) 2001 - 2011 Ampache.org All Rights Reserved
*
* 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.
*
* @category AmpacheVlc
* @package Modules
* @author Karl Vollmer <vollmer@ampache.org>
* @copyright 2001 - 2011 Ampache.org
* @license http://opensource.org/licenses/gpl-2.0 GPLv2
* @version PHP 5.2
* @link http://www.ampache.org/
* @since File available since Release 1.0
*/
/**
* AmpacheVlc Class
*
* This is the class for the vlc localplay method to remote control
* a VLC Instance
*
* @category AmpacheVlc
* @package Modules
* @author Karl Vollmer <vollmer@ampache.org>
* @copyright 2001 - 2011 Ampache.org
* @license http://opensource.org/licenses/gpl-2.0 GPLv2
* @version Release:
* @link http://www.ampache.org/
* @since Class available since Release 1.0
*/
class AmpacheVlc extends localplay_controller {
/* Variables */
private $version = 'Beta 0.2';
private $description = 'Controls a Vlc instance';
/* Constructed variables */
private $_vlc;
/**
* Constructor
* This returns the array map for the localplay object
* REQUIRED for Localplay
*/
public function __construct() {
/* Do a Require Once On the needed Libraries */
require_once Config::get('prefix') . '/modules/vlc/vlcplayer.class.php';
} // Constructor
/**
* get_description
* This returns the description of this localplay method
*/
public function get_description() {
return $this->description;
} // get_description
/**
* get_version
* This returns the current version
*/
public function get_version() {
return $this->version;
} // get_version
/**
* is_installed
* This returns true or false if vlc controller is installed
*/
public function is_installed() {
$sql = "DESCRIBE `localplay_vlc`";
$db_results = Dba::query($sql);
return Dba::num_rows($db_results);
} // is_installed
/**
* install
* This function installs the VLC localplay controller
*/
public function install() {
$sql = "CREATE TABLE `localplay_vlc` (`id` INT( 11 ) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY , ".
"`name` VARCHAR( 128 ) COLLATE utf8_unicode_ci NOT NULL , " .
"`owner` INT( 11 ) NOT NULL, " .
"`host` VARCHAR( 255 ) COLLATE utf8_unicode_ci NOT NULL , " .
"`port` INT( 11 ) UNSIGNED NOT NULL , " .
"`password` VARCHAR( 255 ) COLLATE utf8_unicode_ci NOT NULL , " .
"`access` SMALLINT( 4 ) UNSIGNED NOT NULL DEFAULT '0'" .
") ENGINE = MYISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci";
$db_results = Dba::query($sql);
// Add an internal preference for the users current active instance
Preference::insert('vlc_active','VLC Active Instance','0','25','integer','internal');
User::rebuild_all_preferences();
return true;
} // install
/**
* uninstall
* This removes the localplay controller
*/
public function uninstall() {
$sql = "DROP TABLE `localplay_vlc`";
$db_results = Dba::query($sql);
// Remove the pref we added for this
Preference::delete('vlc_active');
return true;
} // uninstall
/**
* add_instance
* This takes key'd data and inserts a new vlc instance
*/
public function add_instance($data) {
// Foreach and clean up what we need
foreach ($data as $key=>$value) {
switch ($key) {
case 'name':
case 'host':
case 'port':
case 'password':
${$key} = Dba::escape($value);
break;
default:
// Rien a faire
break;
} // end switch on key
} // end foreach
$user_id = Dba::escape($GLOBALS['user']->id);
$sql = "INSERT INTO `localplay_vlc` (`name`,`host`,`port`,`password`,`owner`) " .
"VALUES ('$name','$host','$port','$password','$user_id')";
$db_results = Dba::query($sql);
return $db_results;
} // add_instance
/**
* delete_instance
* This takes a UID and deletes the instance in question
*/
public function delete_instance($uid) {
$uid = Dba::escape($uid);
$sql = "DELETE FROM `localplay_vlc` WHERE `id`='$uid'";
$db_results = Dba::query($sql);
return true;
} // delete_instance
/**
* get_instances
* This returns a key'd array of the instance information with
* [UID]=>[NAME]
*/
public function get_instances() {
$sql = "SELECT * FROM `localplay_vlc` ORDER BY `name`";
$db_results = Dba::query($sql);
$results = array();
while ($row = Dba::fetch_assoc($db_results)) {
$results[$row['id']] = $row['name'];
}
return $results;
} // get_instances
/**
* update_instance
* This takes an ID and an array of data and updates the instance specified
*/
public function update_instance($uid,$data) {
$uid = Dba::escape($uid);
$port = Dba::escape($data['port']);
$host = Dba::escape($data['host']);
$name = Dba::escape($data['name']);
$pass = Dba::escape($data['password']);
$sql = "UPDATE `localplay_vlc` SET `host`='$host', `port`='$port', `name`='$name', `password`='$pass' WHERE `id`='$uid'";
$db_results = Dba::query($sql);
return true;
} // update_instance
/**
* instance_fields
* This returns a key'd array of [NAME]=>array([DESCRIPTION]=>VALUE,[TYPE]=>VALUE) for the
* fields so that we can on-the-fly generate a form
*/
public function instance_fields() {
$fields['name'] = array('description'=>_('Instance Name'),'type'=>'textbox');
$fields['host'] = array('description'=>_('Hostname'),'type'=>'textbox');
$fields['port'] = array('description'=>_('Port'),'type'=>'textbox');
$fields['password'] = array('description'=>_('Password'),'type'=>'textbox');
return $fields;
} // instance_fields
/**
* get_instance
* This returns a single instance and all it's variables
*/
public function get_instance($instance='') {
$instance = $instance ? $instance : Config::get('vlc_active');
$instance = Dba::escape($instance);
$sql = "SELECT * FROM `localplay_vlc` WHERE `id`='$instance'";
$db_results = Dba::query($sql);
$row = Dba::fetch_assoc($db_results);
return $row;
} // get_instance
/**
* set_active_instance
* This sets the specified instance as the 'active' one
*/
public function set_active_instance($uid,$user_id='') {
// Not an admin? bubkiss!
if (!$GLOBALS['user']->has_access('100')) {
$user_id = $GLOBALS['user']->id;
}
$user_id = $user_id ? $user_id : $GLOBALS['user']->id;
Preference::update('vlc_active',$user_id,intval($uid));
Config::set('vlc_active',intval($uid),'1');
return true;
} // set_active_instance
/**
* get_active_instance
* This returns the UID of the current active instance
* false if none are active
*/
public function get_active_instance() {
} // get_active_instance
/**
* add
* This must take an array of URL's from Ampache
* and then add them to Vlc webinterface
*/
public function add($object) {
$url = $this->get_url($object);
// Try to pass a title (if we can)
if (is_object($object)) {
$title = $object->title;
}
if (is_null($this->_vlc->add($title,$url))) {
debug_event('vlc_add',"Error: Unable to add $url to Vlc",'1');
}
return true;
} // add
/**
* delete_track
* This must take an array of ID's (as passed by get function) from Ampache
* and delete them from vlc webinterface
*/
public function delete_track($object_id) {
if (is_null($this->_vlc->delete_pos($object_id))) {
debug_event('vlc_del','ERROR Unable to delete ' . $object_id . ' from Vlc','1');
return false;
}
return true;
} // delete_track
/**
* clear_playlist
* This deletes the entire vlc playlist... nuff said
*/
public function clear_playlist() {
if (is_null($this->_vlc->clear())) { return false; }
// If the clear worked we should stop it!
$this->stop();
return true;
} // clear_playlist
/**
* play
* This just tells vlc to start playing, it does not
* take any arguments
*/
public function play() {
/* A play when it's already playing causes a track restart
* which we don't want to doublecheck its state
*/
if ($this->_vlc->state() == 'play') {
return true;
}
if (is_null($this->_vlc->play())) { return false; }
return true;
} // play
/**
* stop
* This just tells vlc to stop playing, it does not take
* any arguments
*/
public function stop() {
if (is_null($this->_vlc->stop())) { return false; }
return true;
} // stop
/**
* skip
* This tells vlc to skip to the specified song
*/
public function skip($song) {
if (is_null($this->_vlc->skip($song))) { return false; }
return true;
} // skip
/**
* This tells vlc to increase the volume by in vlcplayerclass set amount
*/
public function volume_up() {
if (is_null($this->_vlc->volume_up())) { return false; }
return true;
} // volume_up
/**
* This tells vlc to decrease the volume by vlcplayerclass set amount
*/
public function volume_down() {
if (is_null($this->_vlc->volume_down())) { return false; }
return true;
} // volume_down
/**
* next
* This just tells vlc to skip to the next song, if you play a song by direct
* clicking and hit next vlc will start with the first song , needs work.
*/
public function next() {
if (is_null($this->_vlc->next())) { return false; }
return true;
} // next
/**
* prev
* This just tells vlc to skip to the prev song
*/
public function prev() {
if (is_null($this->_vlc->prev())) { return false; }
return true;
} // prev
/**
* pause
* This tells vlc to pause the current song
*/
public function pause() {
if (is_null($this->_vlc->pause())) { return false; }
return true;
} // pause
/**
* volume
* This tells vlc to set the volume to the specified amount this
* is 0-400 procent
*/
public function volume($volume) {
if (is_null($this->_vlc->set_volume($volume))) { return false; }
return true;
} // volume
/**
* repeat
* This tells vlc to set the repeating the playlist (i.e. loop) to either on or off
*/
public function repeat($state) {
if (is_null($this->_vlc->repeat($state))) { return false; }
return true;
} // repeat
/**
* random
* This tells vlc to turn on or off the playing of songs from the playlist in random order
*/
public function random($onoff) {
if (is_null($this->_vlc->random($onoff))) { return false; }
return true;
} // random
/**
* get
* This functions returns an array containing information about
* The songs that vlc currently has in it's playlist. This must be
* done in a standardized fashion
* Warning ! if you got files in vlc medialibary those files will be sent to the php xml parser
* to, not to your browser but still this can take a lot of work for your server.
* The xml files of vlc need work, not much documentation on them....
*/
public function get() {
/* Get the Current Playlist */
$list = $this->_vlc->get_tracks();
if (!$list) { return array(); }
$counterforarray = 0;
// here we look if there are song in the playlist when media libary is used
if ($list['node']['node'][0]['leaf'][$counterforarray]['attr']['uri']) {
while ($list['node']['node'][0]['leaf'][$counterforarray]){
$songs[] = htmlspecialchars_decode($list['node']['node'][0]['leaf'][$counterforarray]['attr']['uri'], ENT_NOQUOTES);
$songid[] = $list['node']['node'][0]['leaf'][$counterforarray]['attr']['id'];
$counterforarray++;
}
// if there is only one song look here,and media libary is used
}
elseif($list['node']['node'][0]['leaf']['attr']['uri']) {
$songs[] = htmlspecialchars_decode($list['node']['node'][0]['leaf']['attr']['uri'], ENT_NOQUOTES);
$songid[] = $list['node']['node'][0]['leaf']['attr']['id'];
}
// look for songs when media libary isn't used
elseif ($list['node']['node']['leaf'][$counterforarray]['attr']['uri']) {
while ($list['node']['node']['leaf'][$counterforarray]){
$songs[] = htmlspecialchars_decode($list['node']['node']['leaf'][$counterforarray]['attr']['uri'], ENT_NOQUOTES);
$songid[] = $list['node']['node']['leaf'][$counterforarray]['attr']['id'];
$counterforarray++;
}
}
elseif ($list['node']['node']['leaf']['attr']['uri']) {
$songs[] = htmlspecialchars_decode($list['node']['node']['leaf']['attr']['uri'], ENT_NOQUOTES);
$songid[] = $list['node']['node']['leaf']['attr']['id'];
}
else { return array(); }
$counterforarray = 0;
foreach ($songs as $key=>$entry) {
$data = array();
/* Required Elements */
$data['id'] = $songid[$counterforarray]; // id number of the files in the vlc playlist, needed for other operations
$data['raw'] = $entry;
$url_data = $this->parse_url($entry);
switch ($url_data['primary_key']) {
case 'oid':
$song = new Song($url_data['oid']);
$song->format();
$data['name'] = $song->f_title . ' - ' . $song->f_album . ' - ' . $song->f_artist;
$data['link'] = $song->f_link;
break;
case 'demo_id':
$democratic = new Democratic($url_data['demo_id']);
$data['name'] = _('Democratic') . ' - ' . $democratic->name;
$data['link'] = '';
break;
case 'random':
$data['name'] = _('Random') . ' - ' . scrub_out(ucfirst($url_data['type']));
$data['link'] = '';
break;
default:
/* If we don't know it, look up by filename */
$filename = Dba::escape($entry);
$sql = "SELECT `name` FROM `live_stream` WHERE `url`='$filename' ";
$db_results = Dba::read($sql);
if ($row = Dba::fetch_assoc($db_results)) {
//if stream is known just send name
$data['name'] = htmlspecialchars(substr($row['name'], 0, 50));
}
//if it's a http stream not in ampacha's database just show the url'
elseif ( strncmp($entry, 'http', 4)== 0) {
$data['name'] = htmlspecialchars("(VLC stream) " . substr($entry, 0, 50));
}
//if it's a file get the last output after and show that, hard to take every output possible in account
else {
$getlast = explode("/",$entry);
$lastis = count($getlast) - 1;
$data['name'] = htmlspecialchars("(VLC local) " . substr($getlast[$lastis], 0, 50));
} // end if loop
break;
} // end switch on primary key type
$data['track'] = $key+1;
$counterforarray++;
$results[] = $data;
} // foreach playlist items
return $results;
} // get
/**
* status
* This returns bool/int values for features, loop, repeat and any other features
* That this localplay method supports. required function
* This works as in requesting the status.xml file from vlc.
*/
public function status() {
$arrayholder = $this->_vlc->fullstate(); //get status.xml via parser xmltoarray
/* Construct the Array */
$currentstat = $arrayholder['root']['state']['value'];
if ($currentstat == 'playing') { $state = 'play'; } //change to something ampache understands
if ($currentstat == 'stop') { $state = 'stop'; }
if ($currentstat == 'paused') { $state = 'pause'; }
$array['state'] = $state;
$array['volume'] = intval((intval($arrayholder['root']['volume']['value'])/2.6));
$array['repeat'] = $arrayholder['root']['repeat']['value'];
$array['random'] = $arrayholder['root']['random']['value'];
$array['track'] = htmlspecialchars_decode($arrayholder['root']['information']['meta-information']['title']['value'], ENT_NOQUOTES);
$url_data = $this->parse_url($array['track']);
$song = new Song($url_data['oid']);
if ($song->title || $song->get_artist_name() || $song->get_album_name()) {
$array['track_title'] = $song->title;
$array['track_artist'] = $song->get_artist_name();
$array['track_album'] = $song->get_album_name();
}
// if not a known format
else {
$array['track_title'] = htmlspecialchars(substr($arrayholder['root']['information']['meta-information']['title']['value'], 0, 25));
$array['track_artist'] = htmlspecialchars(substr($arrayholder['root']['information']['meta-information']['artist']['value'], 0, 20));
}
return $array;
} // status
/**
* connect
* This functions creates the connection to vlc and returns
* a boolean value for the status, to save time this handle
* is stored in this class
*/
public function connect() {
$options = self::get_instance();
$this->_vlc = new VlcPlayer($options['host'],$options['password'],$options['port']);
// Test our connection by retriving the version, no version in status file, just need to see if returned
//Not yet working all values returned are true for beta testing purpose
if (!is_null($this->_vlc->version())) { return true; }
return false;
} // connect
} //end of AmpacheVlc
?>
|