-
Notifications
You must be signed in to change notification settings - Fork 13
/
HTTP.pm
863 lines (654 loc) · 21.8 KB
/
HTTP.pm
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
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
package Plugins::MixCloud::HTTP;
# $Id$
# Logitech Media Server Copyright 2001-2011 Logitech, Vidur Apparao.
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License,
# version 2.
use strict;
use base qw(Slim::Formats::RemoteStream);
use IO::Socket qw(:crlf);
use Scalar::Util qw(blessed);
use Slim::Formats::RemoteMetadata;
use Slim::Music::Info;
use Slim::Utils::Errno;
use Slim::Utils::Log;
use Slim::Utils::Misc;
use Slim::Utils::Prefs;
use Slim::Utils::Scanner::Remote;
use Slim::Utils::Unicode;
use constant MAXCHUNKSIZE => 32768;
my $log = logger('player.streaming.remote');
my $directlog = logger('player.streaming.direct');
my $sourcelog = logger('player.source');
my $prefs = preferences('server');
sub new {
my $class = shift;
my $args = shift;
if (!$args->{'song'}) {
logWarning("No song passed!");
# XXX: MusicIP abuses this as a non-async HTTP client, can't return undef
# return undef;
}
my $self = $class->open($args);
if (defined($self)) {
${*$self}{'client'} = $args->{'client'};
${*$self}{'url'} = $args->{'url'};
}
return $self;
}
sub isRemote { 1 }
sub readMetaData {
my $self = shift;
my $client = ${*$self}{'client'};
my $metadataSize = 0;
my $byteRead = 0;
while ($byteRead == 0) {
$byteRead = $self->SUPER::sysread($metadataSize, 1);
if ($!) {
if ($! ne "Unknown error" && $! != EWOULDBLOCK) {
#$log->warn("Warning: Metadata byte not read! $!");
return;
} else {
#$log->debug("Metadata byte not read, trying again: $!");
}
}
$byteRead = defined $byteRead ? $byteRead : 0;
}
$metadataSize = ord($metadataSize) * 16;
if ($metadataSize > 0) {
main::DEBUGLOG && $log->debug("Metadata size: $metadataSize");
my $metadata;
my $metadatapart;
do {
$metadatapart = '';
$byteRead = $self->SUPER::sysread($metadatapart, $metadataSize);
if ($!) {
if ($! ne "Unknown error" && $! != EWOULDBLOCK) {
#$log->info("Metadata bytes not read! $!");
return;
} else {
#$log->info("Metadata bytes not read, trying again: $!");
}
}
$byteRead = 0 if (!defined($byteRead));
$metadataSize -= $byteRead;
$metadata .= $metadatapart;
} while ($metadataSize > 0);
main::INFOLOG && $log->info("Metadata: $metadata");
${*$self}{'title'} = __PACKAGE__->parseMetadata($client, $self->url, $metadata);
}
}
sub getFormatForURL {
my $classOrSelf = shift;
my $url = shift;
return Slim::Music::Info::typeFromSuffix($url);
}
sub parseMetadata {
my ( $class, $client, undef, $metadata ) = @_;
my $url = Slim::Player::Playlist::url(
$client, Slim::Player::Source::streamingSongIndex($client)
);
# See if there is a parser for this stream
my $parser = Slim::Formats::RemoteMetadata->getParserFor( $url );
if ( $parser ) {
if ( main::DEBUGLOG && $log->is_debug ) {
$log->debug( 'Trying metadata parser ' . Slim::Utils::PerlRunTime::realNameForCodeRef($parser) );
}
my $handled = eval { $parser->( $client, $url, $metadata ) };
if ( $@ ) {
my $name = Slim::Utils::PerlRunTime::realNameForCodeRef($parser);
logger('formats.metadata')->error( "Metadata parser $name failed: $@" );
}
return if $handled;
}
# Assume Icy metadata as first guess
# BUG 15896 - treat as single line, as some stations add cr/lf to the song title field
if ($metadata =~ (/StreamTitle=\'(.*?)\'(;|$)/s)) {
main::DEBUGLOG && $log->is_debug && $log->debug("Icy metadata received: $metadata");
my $newTitle = Slim::Utils::Unicode::utf8decode_guess($1);
# Bug 15896, a stream had CRLF in the metadata
$newTitle =~ s/\s*[\r\n]+\s*//g;
# capitalize titles that are all lowercase
# XXX: Why do we do this? Shouldn't we let metadata display as-is?
if (lc($newTitle) eq $newTitle) {
$newTitle =~ s/ (
(^\w) #at the beginning of the line
| # or
(\s\w) #preceded by whitespace
| # or
(-\w) #preceded by dash
)
/\U$1/xg;
}
# Check for an image URL in the metadata.
my $artworkUrl;
if ( $metadata =~ /StreamUrl=\'([^']+)\'/i ) {
$artworkUrl = $1;
if ( $artworkUrl !~ /\.(?:jpe?g|gif|png)$/i ) {
$artworkUrl = undef;
}
}
my $cb = sub {
Slim::Music::Info::setCurrentTitle($url, $newTitle, $client);
if ($artworkUrl) {
my $cache = Slim::Utils::Cache->new();
$cache->set( "remote_image_$url", $artworkUrl, 3600 );
if ( my $song = $client->playingSong() ) {
$song->pluginData( httpCover => $artworkUrl );
}
main::DEBUGLOG && $directlog->debug("Updating stream artwork to $artworkUrl");
};
};
# Delay metadata according to buffer size if we already have metadata
if ( $client->metaTitle() ) {
Slim::Music::Info::setDelayedCallback( $client, $cb );
}
else {
$cb->();
}
}
# Check for Ogg metadata, which is formatted as a series of
# 2-byte length/string pairs.
elsif ( $metadata =~ /^Ogg(.+)/s ) {
my $comments = $1;
my $meta = {};
while ( $comments ) {
my $length = unpack 'n', substr( $comments, 0, 2, '' );
my $value = substr $comments, 0, $length, '';
main::DEBUGLOG && $directlog->is_debug && $directlog->debug("Ogg comment: $value");
# Bug 15896, a stream had CRLF in the metadata
$metadata =~ s/\s*[\r\n]+\s*/; /g;
# Look for artist/title/album
if ( $value =~ /ARTIST=(.+)/i ) {
$meta->{artist} = $1;
}
elsif ( $value =~ /ALBUM=(.+)/i ) {
$meta->{album} = $1;
}
elsif ( $value =~ /TITLE=(.+)/i ) {
$meta->{title} = $1;
}
}
# Re-use wmaMeta field
my $song = $client->controller()->songStreamController()->song();
my $cb = sub {
$song->pluginData( wmaMeta => $meta );
Slim::Music::Info::setCurrentTitle($url, $meta->{title}, $client) if $meta->{title};
};
# Delay metadata according to buffer size if we already have metadata
if ( $song->pluginData('wmaMeta') ) {
Slim::Music::Info::setDelayedCallback( $client, $cb, 'output-only' );
}
else {
$cb->();
}
return;
}
return undef;
}
sub canDirectStream {
my ($classOrSelf, $client, $url, $inType) = @_;
# When synced, we don't direct stream so that the server can proxy a single
# stream for all players
if ( $client->isSynced(1) ) {
if ( main::INFOLOG && $directlog->is_info ) {
$directlog->info(sprintf(
"[%s] Not direct streaming because player is synced", $client->id
));
}
return 0;
}
# Allow user pref to select the method for streaming
if ( my $method = $prefs->client($client)->get('mp3StreamingMethod') ) {
if ( $method == 1 ) {
main::DEBUGLOG && $directlog->debug("Not direct streaming because of mp3StreamingMethod pref");
return 0;
}
}
# Strip noscan info from URL
$url =~ s/#slim:.+$//;
return 0;
}
sub sysread {
my $self = $_[0];
my $chunkSize = $_[2];
my $metaInterval = ${*$self}{'metaInterval'};
my $metaPointer = ${*$self}{'metaPointer'};
if ($metaInterval && ($metaPointer + $chunkSize) > $metaInterval) {
$chunkSize = $metaInterval - $metaPointer;
# This is very verbose...
#$log->debug("Reduced chunksize to $chunkSize for metadata");
}
my $readLength = CORE::sysread($self, $_[1], $chunkSize, length($_[1] || ''));
if ($metaInterval && $readLength) {
$metaPointer += $readLength;
${*$self}{'metaPointer'} = $metaPointer;
# handle instream metadata for shoutcast/icecast
if ($metaPointer == $metaInterval) {
$self->readMetaData();
${*$self}{'metaPointer'} = 0;
} elsif ($metaPointer > $metaInterval) {
main::DEBUGLOG && $log->debug("The shoutcast metadata overshot the interval.");
}
}
return $readLength;
}
sub parseDirectHeaders {
my ( $self, $client, $url, @headers ) = @_;
my $isDebug = main::DEBUGLOG && $directlog->is_debug;
# May get a track object
if ( blessed($url) ) {
$url = $url->url;
}
my ($title, $bitrate, $metaint, $redir, $contentType, $length, $body);
my ($rangeLength, $startOffset);
foreach my $header (@headers) {
# Tidy up header to make no stray nulls or \n have been left by caller.
$header =~ s/[\0]*$//;
$header =~ s/\r/\n/g;
$header =~ s/\n\n/\n/g;
$isDebug && $directlog->debug("header-ds: $header");
if ($header =~ /^(?:ic[ey]-name|x-audiocast-name):\s*(.+)/i) {
$title = Slim::Utils::Unicode::utf8decode_guess($1);
}
elsif ($header =~ /^(?:icy-br|x-audiocast-bitrate):\s*(.+)/i) {
$bitrate = $1;
$bitrate *= 1000 if $bitrate < 1000;
}
elsif ($header =~ /^icy-metaint:\s*(.+)/i) {
$metaint = $1;
}
elsif ($header =~ /^Location:\s*(.*)/i) {
$redir = $1;
}
elsif ($header =~ /^Content-Type:\s*(.*)/i) {
$contentType = $1;
}
elsif ($header =~ /^Content-Length:\s*(.*)/i) {
$length = $1;
}
elsif ($header =~ m%^Content-Range:\s+bytes\s+(\d+)-(\d+)/(\d+)%i) {
$rangeLength = $3;
$startOffset = $1;
}
# mp3tunes metadata, this is a bit of hack but creating
# an mp3tunes protocol handler is overkill
elsif ( $url =~ /mp3tunes\.com/ && $header =~ /^X-Locker-Info:\s*(.+)/i ) {
Slim::Plugin::MP3tunes::Plugin->setLockerInfo( $client, $url, $1 );
}
}
# Content-Range: has predecence over Content-Length:
if ($rangeLength) {
$length = $rangeLength;
}
my $song = ${*self}{'song'} if blessed $self;
if (!$song && $client->controller()->songStreamController()) {
$song = $client->controller()->songStreamController()->song();
}
if ($song && $length) {
my $seekdata = $song->seekdata();
if ($startOffset && $seekdata && $seekdata->{restartOffset}
&& $seekdata->{sourceStreamOffset} && $startOffset > $seekdata->{sourceStreamOffset})
{
$startOffset = $seekdata->{sourceStreamOffset};
}
my $streamLength = $length;
$streamLength -= $startOffset if $startOffset;
$song->streamLength($streamLength);
# However we got here, we want to know that we did not start at the beginning, if possible
if ($startOffset) {
# Assume saved duration is more accurate that by calculating from length and bitrate
my $duration = Slim::Music::Info::getDuration($url);
$duration ||= $length * 8 / $bitrate if $bitrate;
if ($duration) {
main::INFOLOG && $directlog->info("Setting startOffest based on Content-Range to ", $duration * ($startOffset/$length));
$song->startOffset($duration * ($startOffset/$length));
}
}
}
$contentType = Slim::Music::Info::mimeToType($contentType);
if ( !$contentType ) {
# Bugs 7225, 7423
# Default contentType to mp3 as some servers don't send the type
# or send an invalid type we don't include in types.conf
$contentType = 'mp3';
}
#$contentType = "application/octet-stream";
#$bitrate = 320;
return ($title, $bitrate, $metaint, $redir, $contentType, $length, $body);
}
=head2 parseHeaders( @headers )
Parse the response headers from an HTTP request, and set instance variables
based on items in the response, eg: bitrate, content type.
Updates the client's streamingProgressBar with the correct duration.
=cut
# XXX Still a lot of duplication here with Squeezebox2::directHeaders()
sub parseHeaders {
my $self = shift;
my $url = $self->url;
my $client = $self->client;
my ($title, $bitrate, $metaint, $redir, $contentType, $length, $body) = $self->parseDirectHeaders($client, $url, @_);
if ($contentType) {
if (($contentType =~ /text/i) && !($contentType =~ /text\/xml/i)) {
# webservers often lie about playlists. This will
# make it guess from the suffix. (unless text/xml)
$contentType = '';
}
${*$self}{'contentType'} = $contentType;
Slim::Music::Info::setContentType( $url, $contentType );
}
${*$self}{'redirect'} = $redir;
${*$self}{'contentLength'} = $length if $length;
${*$self}{'song'}->isLive($length ? 0 : 1) if !$redir;
# Always prefer the title returned in the headers of a radio station
if ( $title ) {
main::INFOLOG && $log->is_info && $log->info( "Setting new title for $url, $title" );
Slim::Music::Info::setCurrentTitle( $url, $title );
# Bug 7979, Only update the database title if this item doesn't already have a title
my $curTitle = Slim::Music::Info::title($url);
if ( !$curTitle || $curTitle =~ /^(?:http|mms)/ ) {
Slim::Music::Info::setTitle( $url, $title );
}
}
if ($bitrate) {
main::INFOLOG && $log->is_info &&
$log->info(sprintf("Bitrate for %s set to %d",
$self->infoUrl,
$bitrate,
));
${*$self}{'bitrate'} = $bitrate;
Slim::Music::Info::setBitrate( $self->infoUrl, $bitrate );
} elsif ( !$self->bitrate ) {
# Bitrate may have been set in Scanner by reading the mp3 stream
$bitrate = ${*$self}{'bitrate'} = Slim::Music::Info::getBitrate( $url );
}
if ($metaint) {
${*$self}{'metaInterval'} = $metaint;
${*$self}{'metaPointer'} = 0;
}
# See if we have an existing track object with duration info for this stream.
if ( my $secs = Slim::Music::Info::getDuration( $url ) ) {
# Display progress bar
$client->streamingProgressBar( {
'url' => $url,
'duration' => $secs,
} );
}
else {
if ( $bitrate > 0 && defined $self->contentLength && $self->contentLength > 0 ) {
# if we know the bitrate and length of a stream, display a progress bar
if ( $bitrate < 1000 ) {
${*$self}{'bitrate'} *= 1000;
}
$client->streamingProgressBar( {
'url' => $url,
'bitrate' => $self->bitrate,
'length' => $self->contentLength,
} );
}
}
# Bug 6482, refresh the cached Track object in the client playlist from the database
# so it picks up any changed data such as title, bitrate, etc
Slim::Player::Playlist::refreshTrack( $client, $url );
return;
}
=head2 requestString( $client, $url, [ $post, [ $seekdata ] ] )
Generate a HTTP request string suitable for sending to a HTTP server.
=cut
sub requestString {
my $self = shift;
my $client = shift;
my $url = shift;
my $post = shift;
my $seekdata = shift;
my ($server, $port, $path, $user, $password) = Slim::Utils::Misc::crackURL($url);
# Use full path for proxy servers
my $proxy = $prefs->get('webproxy');
if ( $proxy && $server !~ /(?:localhost|127.0.0.1)/ ) {
$path = "http://$server:$port$path";
}
my $type = $post ? 'POST' : 'GET';
# Although the port can be part of the Host: header, some hosts (such
# as online.wsj.com don't like it, and will infinitely redirect.
# According to the spec, http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
# The port is optional if it's 80, so follow that rule.
my $host = $port == 80 ? $server : "$server:$port";
# Special case, for the fallback-alarm, disable Icy Metadata, or our own
# server will try and send it
my $want_icy = 1;
if ( $path =~ m{/slim-backup-alarm.mp3$} ) {
$want_icy = 0;
}
# make the request
my $request = join($CRLF, (
"$type $path HTTP/1.0",
"Accept: */*",
"Cache-Control: no-cache",
"User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:33.0) Gecko/20100101 Firefox/33.0" ,
"Icy-MetaData: $want_icy",
"Connection: close",
"Host: $host",
));
#. Slim::Utils::Misc::userAgentString(),
if (defined($user) && defined($password)) {
$request .= $CRLF . "Authorization: Basic " . MIME::Base64::encode_base64($user . ":" . $password,'');
}
$client->songBytes(0) if $client;
# If seeking, add Range header
if ($client && $seekdata) {
$request .= $CRLF . 'Range: bytes=' . int( $seekdata->{sourceStreamOffset} + $seekdata->{restartOffset}) . '-';
if (defined $seekdata->{timeOffset}) {
# Fix progress bar
$client->playingSong()->startOffset($seekdata->{timeOffset});
$client->master()->remoteStreamStartTime( Time::HiRes::time() - $seekdata->{timeOffset} );
}
$client->songBytes(int( $seekdata->{sourceStreamOffset} ));
}
# Send additional information if we're POSTing
if ($post) {
$request .= $CRLF . "Content-Type: application/x-www-form-urlencoded";
$request .= $CRLF . sprintf("Content-Length: %d", length($post));
$request .= $CRLF . $CRLF . $post . $CRLF;
} else {
$request .= $CRLF . $CRLF;
}
# Bug 5858, add cookies to the request
my $request_object = HTTP::Request->parse($request);
$request_object->uri($url);
Slim::Networking::Async::HTTP::cookie_jar->add_cookie_header( $request_object );
$request_object->uri($path);
# Bug 9709, strip long cookies from the request
$request_object->headers->scan( sub {
if ( $_[0] eq 'Cookie' ) {
if ( length($_[1]) > 512 ) {
$request_object->headers->remove_header('Cookie');
}
}
} );
$request = $request_object->as_string( $CRLF );
return $request;
}
sub scanUrl {
my ( $class, $url, $args ) = @_;
Slim::Utils::Scanner::Remote->scanURL($url, $args);
}
# Allow mp3tunes tracks to be scrobbled
sub audioScrobblerSource {
my ( $class, $client, $url ) = @_;
if ( $url =~ /mp3tunes\.com/ ) {
# Scrobble mp3tunes as 'chosen by user' content
return 'P';
}
# R (radio source)
return 'R';
}
sub getMetadataFor {
my ( $class, $client, $url, $forceCurrent ) = @_;
# Check for an alternate metadata provider for this URL
my $provider = Slim::Formats::RemoteMetadata->getProviderFor($url);
if ( $provider ) {
my $metadata = eval { $provider->( $client, $url ) };
if ( $@ ) {
my $name = Slim::Utils::PerlRunTime::realNameForCodeRef($provider);
$log->error( "Metadata provider $name failed: $@" );
}
elsif ( scalar keys %{$metadata} ) {
return $metadata;
}
}
# Check for parsed WMA metadata, this is here because WMA may
# use HTTP protocol handler
if ( my $song = $client->playingSong() ) {
if ( my $meta = $song->pluginData('wmaMeta') ) {
my $data = {};
if ( $meta->{artist} ) {
$data->{artist} = $meta->{artist};
}
if ( $meta->{album} ) {
$data->{album} = $meta->{album};
}
if ( $meta->{title} ) {
$data->{title} = $meta->{title};
}
if ( $meta->{cover} ) {
$data->{cover} = $meta->{cover};
}
if ( scalar keys %{$data} ) {
return $data;
}
}
}
my ($artist, $title);
# Radio tracks, return artist and title if the metadata looks like Artist - Title
if ( my $currentTitle = Slim::Music::Info::getCurrentTitle( $client, $url ) ) {
my @dashes = $currentTitle =~ /( - )/g;
if ( scalar @dashes == 1 ) {
($artist, $title) = split / - /, $currentTitle;
}
else {
$title = $currentTitle;
}
}
# Remember playlist URL
my $playlistURL = $url;
# Check for radio URLs with cached covers
my $cache = Slim::Utils::Cache->new();
my $cover = $cache->get( "remote_image_$url" );
# Item may be a playlist, so get the real URL playing
if ( Slim::Music::Info::isPlaylist($url) ) {
if (my $cur = $client->currentTrackForUrl($url)) {
$url = $cur->url;
}
}
# Remote streams may include ID3 tags with embedded artwork
# Example: http://downloads.bbc.co.uk/podcasts/radio4/excessbag/excessbag_20080426-1217.mp3
my $track = Slim::Schema->objectForUrl( {
url => $url,
} );
return {} unless $track;
if ( $track->cover ) {
# XXX should remote tracks use coverid?
$cover = '/music/' . $track->id . '/cover.jpg';
}
$artist ||= $track->artistName;
if ( $url =~ /archive\.org/ || $url =~ m|squeezenetwork\.com.+/lma/| ) {
if ( Slim::Utils::PluginManager->isEnabled('Slim::Plugin::LMA::Plugin') ) {
my $icon = Slim::Plugin::LMA::Plugin->_pluginDataFor('icon');
return {
title => $title,
cover => $cover || $icon,
icon => $icon,
type => 'Live Music Archive',
};
}
}
=pod XXX - no longer needed with RadioIO metadata handling in its own plugin
elsif ( $playlistURL =~ /radioio/i ) {
if ( Slim::Plugin::InternetRadio::Plugin::RadioIO->can('_pluginDataFor') ) {
# RadioIO
my $icon = Slim::Plugin::InternetRadio::Plugin::RadioIO->_pluginDataFor('icon');
return {
artist => $artist,
title => $title,
cover => $icon,
icon => $icon,
type => 'MP3 (RadioIO)',
};
}
}
=cut
else {
if ( (my $handler = Slim::Player::ProtocolHandlers->handlerForURL($url)) !~ /^(?:$class|Slim::Player::Protocols::MMS)$/ ) {
if ( $handler && $handler->can('getMetadataFor') ) {
return $handler->getMetadataFor( $client, $url );
}
}
my $type = uc( $track->content_type ) . ' ' . Slim::Utils::Strings::cstring($client, 'RADIO');
my $icon = $class->getIcon($url, 'no fallback artwork') || $class->getIcon($playlistURL);
return {
artist => $artist,
title => $title,
type => $type,
bitrate => $track->prettyBitRate,
duration => $track->secs,
icon => $icon,
cover => $cover || $icon,
};
}
return {};
}
sub getIcon {
my ( $class, $url, $noFallback ) = @_;
my $handler;
if ( ($handler = Slim::Player::ProtocolHandlers->iconHandlerForURL($url)) && ref $handler eq 'CODE' ) {
return &{$handler};
}
return $noFallback ? '' : 'html/images/radio.png';
}
sub canSeek {
my ( $class, $client, $song ) = @_;
$client = $client->master();
# Can only seek if bitrate and duration are known
my $bitrate = $song->bitrate();
my $seconds = $song->duration();
if ( !$bitrate || !$seconds ) {
#$log->debug( "bitrate: $bitrate, duration: $seconds" );
#$log->debug( "Unknown bitrate or duration, seek disabled" );
return 0;
}
return 1;
}
sub canSeekError {
my ( $class, $client, $song ) = @_;
my $url = $song->currentTrack()->url;
my $ct = Slim::Music::Info::contentType($url);
if ( $ct ne 'mp3' ) {
return ( 'SEEK_ERROR_TYPE_NOT_SUPPORTED', $ct );
}
if ( !$song->bitrate() ) {
main::INFOLOG && $log->info("bitrate unknown for: " . $url);
return 'SEEK_ERROR_MP3_UNKNOWN_BITRATE';
}
elsif ( !$song->duration() ) {
return 'SEEK_ERROR_MP3_UNKNOWN_DURATION';
}
return 'SEEK_ERROR_MP3';
}
sub getSeekData {
my ( $class, $client, $song, $newtime ) = @_;
# Determine byte offset and song length in bytes
my $bitrate = $song->bitrate() || return;
$bitrate /= 1000;
main::INFOLOG && $log->info( "Trying to seek $newtime seconds into $bitrate kbps" );
return {
sourceStreamOffset => ( ( $bitrate * 1000 ) / 8 ) * $newtime,
timeOffset => $newtime,
};
}
sub getSeekDataByPosition {
my ($class, $client, $song, $bytesReceived) = @_;
my $seekdata = $song->seekdata() || {};
return {%$seekdata, restartOffset => $bytesReceived};
}
1;
__END__