-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathProtocolHandler.pm
230 lines (180 loc) · 6.03 KB
/
ProtocolHandler.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
package Plugins::RadioParadise::ProtocolHandler;
use strict;
use base qw(Slim::Player::Protocols::HTTPS);
use Tie::Cache::LRU;
use Slim::Utils::Log;
use Slim::Utils::Prefs;
use Plugins::RadioParadise::API;
my $prefs = preferences('plugin.radioparadise');
my $log = logger('plugin.radioparadise');
tie my %blockData, 'Tie::Cache::LRU', 16;
# To support remote streaming (synced players), we need to subclass Protocols::HTTP
sub new {
my $class = shift;
my $args = shift;
my $client = $args->{client};
my $song = $args->{song};
my $streamUrl = $song->streamUrl() || return;
main::DEBUGLOG && $log->debug( 'Remote streaming Radio Paradise track: ' . $streamUrl );
return $class->SUPER::new( {
url => $streamUrl,
song => $args->{song},
client => $client,
} );
}
# TODO - investigate wheter we can support seeking
sub canSeek { 0 }
sub isRemote { 1 }
sub canDirectStream { 0 }
sub isRepeatingStream { 1 }
sub contentType { 'audio/flac' };
sub canDoAction {
my ( $class, $client, $url, $action ) = @_;
# "stop" seems to be called when a user pressed FWD...
if ( $action eq 'stop' ) {
if (my $song = $client->master->streamingSong()) {
my $channel = Plugins::RadioParadise::API->getChannelIdFromUrl($url);
my $maxEventId = Plugins::RadioParadise::API->getMaxEventId($channel);
my $blockData = $class->getBlockData($song);
# we can only skip x tracks ahead (defined in a response's max event ID)
if (!($maxEventId > 0 && ($blockData->{event_id} || 0) < $maxEventId)) {
return 0;
}
}
}
elsif ( $action eq 'pause' || $action eq 'rew' ) {
return 0;
}
return 1;
}
# Avoid scanning
sub scanUrl {
my ( $class, $url, $args ) = @_;
$args->{cb}->( $args->{song}->currentTrack() );
}
sub getNextTrack {
my ($class, $song, $successCb, $errorCb) = @_;
my $client = $song->master;
my $event = '';
my $action;
my $channel = Plugins::RadioParadise::API->getChannelIdFromUrl($song->track()->url);
# if the stream URL is the radioparadise URL, we're starting over - don't restore pevious position
if ( $song->streamUrl() =~ /^radioparadise:/ ) {
$action = 'sync_chan_' . $channel;
}
elsif ( my $blockData = $class->getBlockData($song) ) {
$event = $blockData->{event_id};
}
Plugins::RadioParadise::API->getNextTrack(sub {
my $trackInfo = shift || {};
if (my $songs = $trackInfo->{songs}) {
if (ref $songs) {
my $songdata = $songs->[0];
my $currentEventId = $trackInfo->{current_event_id} || 0;
my $desiredEventId = $songdata->{event_id} || 0;
if ($desiredEventId - $currentEventId < -3) {
$log->error("We've tried to play a song from the past - reset position: $desiredEventId < $currentEventId");
$song->streamUrl(sprintf('radioparadise://4-%s.flac', $channel));
$class->getNextTrack($song, $successCb, $errorCb);
return;
}
elsif ($desiredEventId < $currentEventId) {
$log->info("We seem to be behind current position, but not too badly - ignore: $desiredEventId < $currentEventId");
}
__PACKAGE__->setBlockData($songdata);
$song->streamUrl($songdata->{gapless_url});
# tell Jive clients to refresh the status, or they wouldn't pick up the new duration
Slim::Music::Info::setDelayedCallback( $client, sub {
Slim::Control::Request::notifyFromArray( $client, [ 'newmetadata' ] );
} );
Plugins::RadioParadise::API->updateHistory(undef, $songdata, {
channel => $channel,
client => $client->id,
}) if !$client->isSynced() || Slim::Player::Sync::isMaster($client);
$successCb->();
return;
}
}
$log->warn("Failed to get next track?!?");
main::INFOLOG && $log->is_info && $log->info(Data::Dump::dump($trackInfo));
$errorCb->();
}, {
client => $client->id,
channel => $channel,
event => $event,
action => $action,
});
}
sub getMetadataFor {
my ( $class, $client, $url, undef, $song ) = @_;
$client = $client->master;
$song ||= $client->playingSong();
return {} unless $song;
my $icon = $class->getIcon();
my $songdata = $class->getBlockData($song);
# TODO - review?
my $bitrate = int($song->bitrate ? ($song->bitrate / 1024) : 850) . 'k VBR FLAC';
if ($songdata) {
my $meta = {
artist => $songdata->{artist},
album => $songdata->{album},
title => $songdata->{title},
year => $songdata->{year},
duration => int($songdata->{duration} / 1000),
secs => int($songdata->{duration} / 1000),
cover => Plugins::RadioParadise::API->getImageUrl($songdata),
bitrate=> $bitrate,
slideshow => $songdata->{slideshow} || [],
song_id=> $songdata->{song_id},
extid => 'radioparadise:' . $songdata->{song_id},
buttons => {
rew => 0,
},
};
Slim::Music::Info::setDuration($song->track, $meta->{duration});
main::DEBUGLOG && $log->is_debug && $log->debug("Returning meta data:" . Data::Dump::dump($meta));
return $meta;
}
main::DEBUGLOG && $log->is_debug && $log->debug("Returning default metadata");
return {
icon => $icon,
cover => $icon,
bitrate => '',
title => 'Radio Paradise',
duration=> 0,
secs => 0,
song_id => 0,
slideshow => [],
buttons => {
rew => 0,
},
};
}
sub getBlockData {
my ($class, $song) = @_;
return $blockData{_cleanupBlockURL($song->streamUrl)};
}
sub setBlockData {
my ($class, $data, $setStartTime) = @_;
return unless $data && ref $data && $data->{gapless_url};
$data->{startPlaybackTime} = time();
$blockData{_cleanupBlockURL($data->{gapless_url})} = $data;
}
sub _cleanupBlockURL {
my $url = shift || '';
$url =~ s/\?.*//;
return $url;
}
sub getIcon {
return Plugins::RadioParadise::Plugin->_pluginDataFor('icon');
}
# Optionally override replaygain to use the plugin's gain value
sub trackGain {
my ( $class, $client, $url ) = @_;
main::DEBUGLOG && $log->is_debug && $log->debug("Url: $url");
my $cPrefs = preferences('server')->client($client); # access player prefs
my $rgmode = $cPrefs->get('replayGainMode'); # is player replay gain in effect?
# if so, return the sum of remoteReplayGain and the plugin's adjustment
return $rgmode ? $cPrefs->get('remoteReplayGain') + $prefs->get('replayGain') : undef;
}
1;