-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProtocolHandler.pm
More file actions
613 lines (477 loc) · 19.1 KB
/
ProtocolHandler.pm
File metadata and controls
613 lines (477 loc) · 19.1 KB
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
package Plugins::SiriusXM::ProtocolHandler;
use strict;
use warnings;
use base qw(Slim::Player::Protocols::HTTP);
use Slim::Utils::Log;
use Slim::Utils::Prefs;
use Slim::Utils::Misc;
use Slim::Utils::Cache;
use Slim::Utils::Timers;
use Slim::Networking::SimpleAsyncHTTP;
use JSON::XS;
use Data::Dumper;
use Date::Parse;
use Plugins::SiriusXM::API;
use Plugins::SiriusXM::APImetadata;
use Plugins::SiriusXM::TrackDurationDB;
my $log = logger('plugin.siriusxm');
my $prefs = preferences('plugin.siriusxm');
# Metadata update interval (25 seconds)
use constant METADATA_UPDATE_INTERVAL => 25;
# Global hash to track player metadata timers and states
my %playerStates = ();
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( 'PH:new(): ' . $song->track()->url() );
return $class->SUPER::new({
song => $song,
url => $streamUrl,
client => $client,
});
}
sub canSeek { 0 }
sub canSkip { 0 }
sub isRemote { 1 }
sub canDirectStream { 0 }
sub isRepeatingStream { 0 }
sub canDoAction {
my ( $class, $client, $url, $action ) = @_;
# "stop" seems to be called when a user pressed FWD...
if ( $action eq 'stop' ) {
return 0;
}
elsif ( $action eq 'rew' ) {
return 0;
}
return 1;
}
# Initialize player event callbacks for metadata tracking
sub initPlayerEvents {
my $class = shift;
$log->debug("Registering player event callbacks for metadata tracking");
# Register callbacks for player state changes
Slim::Control::Request::subscribe(
\&onPlayerEvent,
[['play', 'pause', 'stop', 'playlist']]
);
}
# Clean up player event subscriptions and timers
sub cleanupPlayerEvents {
my $class = shift;
$log->debug("Cleaning up player event callbacks and timers");
# Unsubscribe from player events
Slim::Control::Request::unsubscribe(\&onPlayerEvent);
# Stop all active metadata timers
for my $clientId (keys %playerStates) {
if ($playerStates{$clientId}->{timer}) {
# Find the client object to cancel timers
my $client = Slim::Player::Client::getClient($clientId);
if ($client) {
Slim::Utils::Timers::killTimers($client, \&_onMetadataTimer);
}
}
}
# Clear all player states - Pretty sure we don't want to do this, there may be multiple players.
#%playerStates = ();
}
# Player event callback handler
sub onPlayerEvent {
my $request = shift;
my $client = $request->client() || return;
my $command = $request->getRequest(0) || return;
return unless $client;
my $clientId = $client->id();
my $song = $client->playingSong();
my $url = $song ? $song->currentTrack()->url() : '';
# Only handle SiriusXM streams (both sxm: and converted HTTP URLs)
return unless $url =~ /^sxm:/ || $url =~ m{^http://localhost:\d+/[\w-]+\.m3u8$};
$log->debug("Player event '$command' for client $clientId, URL: $url");
if ($command eq 'play') {
_startMetadataTimer($client, $url);
} elsif ($command eq 'pause' || $command eq 'stop') {
_stopMetadataTimer($client);
} elsif ($command eq 'playlist') {
# Handle playlist changes - may need to start/stop timers
my $clientId = $client->id();
my $isPlaying = $client->isPlaying();
my $timersRunning = exists $playerStates{$clientId} && $playerStates{$clientId}->{timer};
if ($isPlaying && !$timersRunning) {
# Start timers if playing and no timers running
_startMetadataTimer($client, $url);
} elsif ($isPlaying && $timersRunning) {
# Do nothing - timers already running
} elsif (!$isPlaying && $timersRunning) {
# Stop timers if not playing but timers are running
_stopMetadataTimer($client);
}
}
# Initialize Player Metatadata
my $state = $playerStates{$clientId};
if (!$state) {
$log->debug("No current player state, configuring");
my $channel_info = __PACKAGE__->getChannelInfoFromUrl($url);
# Initialize player state
$playerStates{$clientId} = {
url => $url,
channel_info => $channel_info,
last_next => undef,
timer => undef,
};
_fetchMetadataFromAPI($client);
}
}
# Start metadata update timer for a client
sub _startMetadataTimer {
my ($client, $url) = @_;
return unless $client && $url;
# Check if metadata updates are enabled
unless ($prefs->get('enable_metadata')) {
$log->debug("Metadata updates disabled by user preference, skipping timer setup");
return;
}
my $clientId = $client->id();
# Stop any existing timer
_stopMetadataTimer($client);
# Get channel info for xmplaylist integration
my $channel_info = __PACKAGE__->getChannelInfoFromUrl($url);
return unless $channel_info && $channel_info->{xmplaylist_name};
$log->info("Starting metadata timer for client $clientId, channel: " . $channel_info->{name});
# Initialize player state
$playerStates{$clientId} = {
url => $url,
channel_info => $channel_info,
last_next => undef,
timer => undef,
};
# Start immediate metadata fetch
_fetchMetadataFromAPI($client);
# Schedule periodic updates
$playerStates{$clientId}->{timer} = Slim::Utils::Timers::setTimer(
$client,
time() + METADATA_UPDATE_INTERVAL,
\&_onMetadataTimer
);
}
# Stop metadata update timer for a client
sub _stopMetadataTimer {
my $client = shift;
return unless $client;
my $clientId = $client->id();
if (exists $playerStates{$clientId}) {
$log->debug("Stopping metadata timer for client $clientId");
# Cancel timer if exists
if ($playerStates{$clientId}->{timer}) {
Slim::Utils::Timers::killTimers($client, \&_onMetadataTimer);
}
# Clean up state
delete $playerStates{$clientId};
}
}
# Timer callback for metadata updates
sub _onMetadataTimer {
my $client = shift;
return unless $client;
my $clientId = $client->id();
# Verify client is still playing
my $isPlaying = $client->isPlaying();
if (!$isPlaying) {
$log->debug("Client $clientId no longer playing, stopping metadata timer");
_stopMetadataTimer($client);
return;
}
# Fetch metadata update
_fetchMetadataFromAPI($client);
# Let the meta data refresh one more time, to return player screens to channel artwork.
unless ($prefs->get('enable_metadata')) {
$log->debug("Metadata updates disabled by user preference, stopping timer");
_stopMetadataTimer($client);
return;
}
# Schedule next update if still playing
if (exists $playerStates{$clientId}) {
$playerStates{$clientId}->{timer} = Slim::Utils::Timers::setTimer(
$client,
time() + METADATA_UPDATE_INTERVAL,
\&_onMetadataTimer
);
}
}
# Fetch metadata from xmplaylist.com API using APImetadata module
sub _fetchMetadataFromAPI {
my $client = shift;
return unless $client;
my $clientId = $client->id();
my $state = $playerStates{$clientId};
return unless $state && $state->{channel_info};
my $channel_info = $state->{channel_info};
Plugins::SiriusXM::APImetadata->fetchMetadata($client, $channel_info, sub {
my $result = shift;
return unless $result;
_updateClientMetadata($client, $result);
});
}
# Update client with new metadata
sub _updateClientMetadata {
my ($client, $result) = @_;
return unless $client && $result;
my $clientId = $client->id();
my $state = $playerStates{$clientId};
return unless $state;
my $new_meta = $result->{metadata};
my $next = $result->{next};
my $metadata_is_fresh = $result->{is_fresh};
# Check if metadata has changed using "next" field
if (defined $state->{last_next} && defined $next && $state->{last_next} eq $next) {
# Only skip update if metadata is fresh - if stale, we need to update display
if ($metadata_is_fresh) {
$log->debug("No new metadata available and current metadata is fresh - skipping update");
return;
} else {
$log->debug("Metadata unchanged but stale - updating display to show channel info");
}
}
# Update the last_next value
$state->{last_next} = $next;
# Update the current song's metadata if we have new information
if ($new_meta && keys %$new_meta) {
$log->info("Updating metadata for client $clientId: " .
($new_meta->{title} || 'Unknown') . " by " .
($new_meta->{artist} || 'Unknown Artist'));
my $song = $client->playingSong();
if ($song) {
# Update song metadata
$song->pluginData('xmplaylist_meta', $new_meta);
# Notify clients of metadata update
$client->currentPlaylistUpdateTime(Time::HiRes::time());
Slim::Control::Request::notifyFromArray($client, ['playlist', 'newsong']);
}
}
}
# Handle sxm: protocol URLs by converting them to HTTP proxy URLs
sub getFormatForURL {
my ($class, $url) = @_;
# For sxm: URLs, we'll stream as HTTP since we convert to HTTP proxy URLs
return 'mp3'; # Default format, actual format determined by proxy
}
sub scanUrl {
my ($class, $url, $args) = @_;
$args->{'cb'}->($args->{'song'}->currentTrack());
}
sub getNextTrack {
my ($class, $song, $successCb, $errorCb) = @_;
my $client = $song->master();
my $url = $song->currentTrack()->url;
my $clientId = $client->id();
if ($clientId) {
$log->debug("getNextTrack called for: $clientId -> $url");
# Clear player state for different channels to ensure fresh state
$class->_clearPlayerStatesForDifferentChannel($client, $url);
}
# Convert sxm: URL to HTTP proxy URL
my $httpUrl = $class->sxmToHttpUrl($url);
if ($httpUrl) {
# Store channel info for metadata access only if metadata is enabled
if ($prefs->get('enable_metadata')) {
my $channel_info = $class->getChannelInfoFromUrl($url);
$song->pluginData('channel_info', $channel_info) if $channel_info;
}
# Update the track URL to the HTTP proxy URL
$song->currentTrack()->url($httpUrl);
$log->debug("Converted sxm URL to HTTP URL: $httpUrl");
$successCb->();
} else {
$errorCb->('Failed to convert sxm URL to HTTP URL');
}
}
# Convert sxm: protocol URL to HTTP proxy URL
sub sxmToHttpUrl {
my ($class, $url) = @_;
return unless $url =~ /^sxm:/;
# Extract channel ID from sxm:channelId format
my ($channel_id) = $url =~ /^sxm:(.+)$/;
return unless $channel_id;
my $port = $prefs->get('port') || '9999';
my $http_url = "http://localhost:$port/$channel_id.m3u8";
$log->debug("Converted sxm:$channel_id to $http_url");
return $http_url;
}
# Extract channel information from the URL for metadata access
sub getChannelInfoFromUrl {
my ($class, $url) = @_;
# Use the consolidated channel ID extraction function
my $channel_id = $class->_extractChannelIdFromUrl($url);
return unless $channel_id;
# Use the API's cached channel info (processed data, not menu data)
my $cache = Slim::Utils::Cache->new();
my $cached_channel_info = $cache->get('siriusxm_channel_info');
if ($cached_channel_info) {
# Search through cached channel info data (categories hash from processChannelData)
for my $category_name (keys %$cached_channel_info) {
my $channels_in_category = $cached_channel_info->{$category_name};
for my $channel (@$channels_in_category) {
# Check if this channel matches our channel ID
if ($channel->{id} && $channel->{id} eq $channel_id) {
# Return the processed channel info with correct normalized name
return {
id => $channel->{id},
name => $channel->{name},
xmplaylist_name => $channel->{xmplaylist_name},
description => $channel->{description},
channel_number => $channel->{number},
icon => $channel->{icon},
category => $channel->{category},
};
}
}
}
} else {
# No cache available - trigger async API call to populate cache
# But don't wait for it, just return fallback for now
Plugins::SiriusXM::API->getChannels(undef, sub {
# Cache will be populated for next time
});
}
# Fallback channel info if not found in cache ---- May only get here if restarting from playlist. BUt should not need this.
return {
id => $channel_id,
name => "SiriusXM Channel",
xmplaylist_name => undef,
description => "SiriusXM Channel $channel_id",
};
}
# Provide metadata for the stream
sub getMetadataFor {
my ($class, $client, $url, undef, $song) = @_;
my $song ||= $client->playingSong();
return {} unless $song;
my $channel_info;
if ($song) {
$channel_info = $song->pluginData('channel_info');
}
my $xmplaylist_meta;
# Only use external metadata sources if metadata is enabled
if ($prefs->get('enable_metadata') && $song) {
$xmplaylist_meta = $song->pluginData('xmplaylist_meta');
}
# If no channel info in song data, try to extract from URL
if (!$channel_info) {
$channel_info = $class->getChannelInfoFromUrl($url);
}
# my $meta = $class->SUPER::getMetadataFor($client, $url, $forceCurrent) || {};
my $meta;
# Use xmplaylist metadata if available and metadata is enabled, otherwise fall back to basic info
if ($prefs->get('enable_metadata') && $xmplaylist_meta && keys %$xmplaylist_meta) {
# Use enhanced metadata from xmplaylist.com
$meta->{title} = $xmplaylist_meta->{title} if $xmplaylist_meta->{title};
$meta->{artist} = $xmplaylist_meta->{artist} if $xmplaylist_meta->{artist};
$meta->{cover} = $xmplaylist_meta->{cover} if $xmplaylist_meta->{cover};
$meta->{icon} = $xmplaylist_meta->{icon} if $xmplaylist_meta->{icon};
$meta->{album} = $xmplaylist_meta->{album} if $xmplaylist_meta->{album};
$meta->{bitrate} = '';
# Add duration if available
if (defined $xmplaylist_meta->{duration}) {
$meta->{duration} = $xmplaylist_meta->{duration};
$meta->{secs} = $xmplaylist_meta->{duration};
# Set track duration for LMS
Slim::Music::Info::setDuration($song->track, $meta->{duration}) if $song;
}
# Handle track timing if we have timestamp information
if ($xmplaylist_meta->{track_timestamp}) {
my $elapsed = $class->_calculateTrackElapsed($xmplaylist_meta->{track_timestamp});
if (defined $elapsed && $elapsed >= 0 && defined $meta->{duration}) {
# Store elapsed time in metadata for potential use by clients
# Don't force current position as this might interfere with LMS streaming
$meta->{track_elapsed} = $elapsed;
$log->debug("Track elapsed time: ${elapsed}s of " . $meta->{duration} . "s");
}
}
# Really noisy log message when using a LMS web.
# $log->debug("Using xmplaylist metadata: " . ($meta->{title} || 'Unknown') .
# " by " . ($meta->{artist} || 'Unknown Artist'));
} elsif ($channel_info) {
# Fall back to basic channel info when metadata is enabled
$meta->{artist} = $channel_info->{name};
$meta->{title} = $channel_info->{description} || '';
$meta->{icon} = $channel_info->{icon};
$meta->{cover} = $channel_info->{icon};
$meta->{album} = 'SiriusXM';
$meta->{bitrate} = '';
}
# $meta->{channel_info} = $channel_info if $channel_info;
# $log->debug(Dumper($meta));
return $meta;
}
# Clear player states for channels different from the specified URL
sub _clearPlayerStatesForDifferentChannel {
my ($class, $client, $newUrl) = @_;
my $clientId = $client->id();
return unless $client, $newUrl, $clientId;
# Extract channel ID from the new URL
my $newChannelId = $class->_extractChannelIdFromUrl($newUrl);
return unless $newChannelId;
$log->debug("Checking player state for $clientId different channels than: $newChannelId");
# Check existing player state.
my $state = $playerStates{$clientId};
return unless $state && $state->{url};
# Extract channel ID from existing state URL
my $existingChannelId = $class->_extractChannelIdFromUrl($state->{url});
return unless $existingChannelId;
# If the channel ID is different, clear this player state
if ($existingChannelId ne $newChannelId) {
$log->debug("Clearing player state for client $clientId (old channel: $existingChannelId, new channel: $newChannelId)");
# Find the client object to cancel timers properly
my $client = Slim::Player::Client::getClient($clientId);
if ($client && $state->{timer}) {
Slim::Utils::Timers::killTimers($client, \&_onMetadataTimer);
}
# Remove the player state
delete $playerStates{$clientId};
}
}
# Extract channel ID from URL (supports both sxm: and HTTP URLs)
sub _extractChannelIdFromUrl {
my ($class, $url) = @_;
return unless $url;
# Handle sxm: URLs
if ($url =~ /^sxm:(.+)$/) {
return $1;
}
# Handle converted HTTP URLs
if ($url =~ m{^http://localhost:\d+/([\w-]+)\.m3u8$}) {
return $1;
}
return;
}
# Handle HTTPS support
sub requestString {
my ($class, $client, $url, $maxRedirects) = @_;
# Convert sxm: to HTTP URL first
my $httpUrl = $class->sxmToHttpUrl($url);
if ($httpUrl) {
return $class->SUPER::requestString($client, $httpUrl, $maxRedirects);
}
return $class->SUPER::requestString($client, $url, $maxRedirects);
}
# Calculate elapsed time for a track based on its start timestamp
sub _calculateTrackElapsed {
my ($class, $timestamp) = @_;
return unless $timestamp;
eval {
# Parse the UTC timestamp format: 2025-08-09T15:57:41.586Z
my $track_start_time = str2time($timestamp);
return unless defined $track_start_time;
my $current_time = time();
my $elapsed = $current_time - $track_start_time;
# Only return positive elapsed times (track has started)
return $elapsed >= 0 ? $elapsed : 0;
};
if ($@) {
$log->warn("Failed to calculate track elapsed time from timestamp '$timestamp': $@");
}
return;
}
1;