2007-02-25 02:43:56 +00:00
|
|
|
/*
|
|
|
|
* $Id: AudioScrobbler.m 238 2007-01-26 22:55:20Z stephen_booth $
|
|
|
|
*
|
|
|
|
* Copyright (C) 2006 - 2007 Stephen F. Booth <me@sbooth.org>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
|
|
|
#import "AudioScrobbler.h"
|
|
|
|
|
|
|
|
#import "AudioScrobblerClient.h"
|
|
|
|
#import "PlaylistEntry.h"
|
|
|
|
|
2013-10-11 12:03:55 +00:00
|
|
|
#import "Logging.h"
|
|
|
|
|
2007-07-05 23:24:25 +00:00
|
|
|
// ========================================
|
|
|
|
// Symbolic Constants
|
|
|
|
// ========================================
|
2022-02-07 05:49:27 +00:00
|
|
|
NSString *const AudioScrobblerRunLoopMode = @"org.cogx.Cog.AudioScrobbler.RunLoopMode";
|
2007-07-05 23:24:25 +00:00
|
|
|
|
|
|
|
// ========================================
|
|
|
|
// Helpers
|
|
|
|
// ========================================
|
2022-02-07 05:49:27 +00:00
|
|
|
static NSString *
|
|
|
|
escapeForLastFM(NSString *string) {
|
2007-02-25 02:43:56 +00:00
|
|
|
NSMutableString *result = [string mutableCopy];
|
2022-02-07 05:49:27 +00:00
|
|
|
|
|
|
|
[result replaceOccurrencesOfString:@"&"
|
|
|
|
withString:@"&&"
|
|
|
|
options:NSLiteralSearch
|
|
|
|
range:NSMakeRange(0, [result length])];
|
|
|
|
|
2016-05-05 20:05:39 +00:00
|
|
|
return (nil == result ? @"" : result);
|
2007-02-25 02:43:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@interface AudioScrobbler (Private)
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (NSMutableArray *)queue;
|
|
|
|
- (NSString *)pluginID;
|
2007-02-25 02:43:56 +00:00
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (void)sendCommand:(NSString *)command;
|
2007-02-25 02:43:56 +00:00
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (BOOL)keepProcessingAudioScrobblerCommands;
|
|
|
|
- (void)setKeepProcessingAudioScrobblerCommands:(BOOL)keepProcessingAudioScrobblerCommands;
|
2007-02-25 02:43:56 +00:00
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (BOOL)audioScrobblerThreadCompleted;
|
|
|
|
- (void)setAudioScrobblerThreadCompleted:(BOOL)audioScrobblerThreadCompleted;
|
2007-02-25 02:43:56 +00:00
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (semaphore_t)semaphore;
|
2007-02-25 02:43:56 +00:00
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (void)processAudioScrobblerCommands:(id)unused;
|
2007-02-25 02:43:56 +00:00
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation AudioScrobbler
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
+ (BOOL)isRunning {
|
|
|
|
NSArray *launchedApps = [[NSWorkspace sharedWorkspace] runningApplications];
|
|
|
|
BOOL running = NO;
|
|
|
|
for(NSRunningApplication *app in launchedApps) {
|
|
|
|
if([[app bundleIdentifier] isEqualToString:@"fm.last.Last.fm"] ||
|
|
|
|
[[app bundleIdentifier] isEqualToString:@"fm.last.Scrobbler"]) {
|
|
|
|
running = YES;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return running;
|
2013-10-13 22:13:27 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (id)init {
|
2007-02-25 02:43:56 +00:00
|
|
|
if((self = [super init])) {
|
2007-07-05 23:25:10 +00:00
|
|
|
_pluginID = @"cog";
|
2022-02-07 05:49:27 +00:00
|
|
|
|
2013-10-11 12:37:11 +00:00
|
|
|
if([[NSUserDefaults standardUserDefaults] boolForKey:@"automaticallyLaunchLastFM"]) {
|
2022-02-07 05:49:27 +00:00
|
|
|
if(![AudioScrobbler isRunning]) {
|
|
|
|
[[NSWorkspace sharedWorkspace] launchApplication:@"Last.fm.app"];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-02-25 02:43:56 +00:00
|
|
|
_keepProcessingAudioScrobblerCommands = YES;
|
|
|
|
|
|
|
|
kern_return_t result = semaphore_create(mach_task_self(), &_semaphore, SYNC_POLICY_FIFO, 0);
|
2022-02-07 05:49:27 +00:00
|
|
|
|
2007-02-25 02:43:56 +00:00
|
|
|
if(KERN_SUCCESS != result) {
|
2013-10-11 12:03:55 +00:00
|
|
|
ALog(@"Couldn't create semaphore (%s).", mach_error_type(result));
|
2007-02-25 02:43:56 +00:00
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
self = nil;
|
2007-02-25 02:43:56 +00:00
|
|
|
return nil;
|
|
|
|
}
|
2022-02-07 05:49:27 +00:00
|
|
|
|
2009-03-28 03:00:45 +00:00
|
|
|
[NSThread detachNewThreadSelector:@selector(processAudioScrobblerCommands:) toTarget:self withObject:nil];
|
2007-02-25 02:43:56 +00:00
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (void)dealloc {
|
2007-07-05 23:24:25 +00:00
|
|
|
if([self keepProcessingAudioScrobblerCommands] || NO == [self audioScrobblerThreadCompleted])
|
2007-02-25 02:43:56 +00:00
|
|
|
[self shutdown];
|
2022-02-07 05:49:27 +00:00
|
|
|
|
2016-05-05 20:05:39 +00:00
|
|
|
_queue = nil;
|
2022-02-07 05:49:27 +00:00
|
|
|
|
|
|
|
semaphore_destroy(mach_task_self(), _semaphore);
|
|
|
|
_semaphore = 0;
|
2007-02-25 02:43:56 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (void)start:(PlaylistEntry *)pe {
|
|
|
|
[self sendCommand:[NSString stringWithFormat:@"START c=%@&a=%@&t=%@&b=%@&m=%@&l=%i&p=%@\n",
|
|
|
|
[self pluginID],
|
|
|
|
escapeForLastFM([pe artist]),
|
|
|
|
escapeForLastFM([pe title]),
|
|
|
|
escapeForLastFM([pe album]),
|
|
|
|
@"", // TODO: MusicBrainz support
|
|
|
|
[[pe length] intValue],
|
|
|
|
escapeForLastFM([[pe URL] path])]];
|
2007-02-25 02:43:56 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (void)stop {
|
2007-02-25 02:43:56 +00:00
|
|
|
[self sendCommand:[NSString stringWithFormat:@"STOP c=%@\n", [self pluginID]]];
|
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (void)pause {
|
2007-02-25 02:43:56 +00:00
|
|
|
[self sendCommand:[NSString stringWithFormat:@"PAUSE c=%@\n", [self pluginID]]];
|
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (void)resume {
|
2007-02-25 02:43:56 +00:00
|
|
|
[self sendCommand:[NSString stringWithFormat:@"RESUME c=%@\n", [self pluginID]]];
|
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (void)shutdown {
|
2007-02-25 02:43:56 +00:00
|
|
|
[self setKeepProcessingAudioScrobblerCommands:NO];
|
|
|
|
semaphore_signal([self semaphore]);
|
|
|
|
|
|
|
|
// Wait for the thread to terminate
|
2007-07-05 23:24:25 +00:00
|
|
|
while(NO == [self audioScrobblerThreadCompleted])
|
|
|
|
[[NSRunLoop currentRunLoop] runMode:AudioScrobblerRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.2]];
|
2007-02-25 02:43:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation AudioScrobbler (Private)
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (NSMutableArray *)queue {
|
2007-07-05 23:24:25 +00:00
|
|
|
if(nil == _queue)
|
2007-02-25 02:43:56 +00:00
|
|
|
_queue = [[NSMutableArray alloc] init];
|
2022-02-07 05:49:27 +00:00
|
|
|
|
2007-02-25 02:43:56 +00:00
|
|
|
return _queue;
|
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (NSString *)pluginID {
|
2007-02-25 02:43:56 +00:00
|
|
|
return _pluginID;
|
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (void)sendCommand:(NSString *)command {
|
2007-02-25 02:43:56 +00:00
|
|
|
@synchronized([self queue]) {
|
|
|
|
[[self queue] addObject:command];
|
|
|
|
}
|
|
|
|
semaphore_signal([self semaphore]);
|
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (BOOL)keepProcessingAudioScrobblerCommands {
|
2007-02-25 02:43:56 +00:00
|
|
|
return _keepProcessingAudioScrobblerCommands;
|
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (void)setKeepProcessingAudioScrobblerCommands:(BOOL)keepProcessingAudioScrobblerCommands {
|
2007-02-25 02:43:56 +00:00
|
|
|
_keepProcessingAudioScrobblerCommands = keepProcessingAudioScrobblerCommands;
|
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (BOOL)audioScrobblerThreadCompleted {
|
2007-02-25 02:43:56 +00:00
|
|
|
return _audioScrobblerThreadCompleted;
|
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (void)setAudioScrobblerThreadCompleted:(BOOL)audioScrobblerThreadCompleted {
|
2007-02-25 02:43:56 +00:00
|
|
|
_audioScrobblerThreadCompleted = audioScrobblerThreadCompleted;
|
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (semaphore_t)semaphore {
|
2007-02-25 02:43:56 +00:00
|
|
|
return _semaphore;
|
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (void)processAudioScrobblerCommands:(id)unused {
|
|
|
|
@autoreleasepool {
|
|
|
|
AudioScrobblerClient *client = [[AudioScrobblerClient alloc] init];
|
|
|
|
mach_timespec_t timeout = { 5, 0 };
|
|
|
|
NSString *command = nil;
|
|
|
|
NSString *response = nil;
|
|
|
|
in_port_t port = 33367;
|
|
|
|
|
|
|
|
while([self keepProcessingAudioScrobblerCommands]) {
|
|
|
|
@autoreleasepool {
|
|
|
|
// Get the first command to be sent
|
|
|
|
@synchronized([self queue]) {
|
|
|
|
if([[self queue] count]) {
|
|
|
|
command = [[self queue] objectAtIndex:0];
|
|
|
|
[[self queue] removeObjectAtIndex:0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(nil != command) {
|
|
|
|
@try {
|
|
|
|
if([client connectToHost:@"localhost" port:port]) {
|
|
|
|
port = [client connectedPort];
|
|
|
|
[client send:command];
|
|
|
|
command = nil;
|
|
|
|
|
|
|
|
response = [client receive];
|
|
|
|
if(2 > [response length] || NSOrderedSame != [response compare:@"OK" options:NSLiteralSearch range:NSMakeRange(0, 2)])
|
|
|
|
ALog(@"AudioScrobbler error: %@", response);
|
|
|
|
|
|
|
|
[client shutdown];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@catch(NSException *exception) {
|
|
|
|
command = nil;
|
|
|
|
|
|
|
|
[client shutdown];
|
|
|
|
// ALog(@"Exception: %@",exception);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
semaphore_timedwait([self semaphore], timeout);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send a final stop command to cleanup
|
|
|
|
@try {
|
|
|
|
if([client connectToHost:@"localhost" port:port]) {
|
|
|
|
[client send:[NSString stringWithFormat:@"STOP c=%@\n", [self pluginID]]];
|
|
|
|
|
|
|
|
response = [client receive];
|
|
|
|
if(2 > [response length] || NSOrderedSame != [response compare:@"OK" options:NSLiteralSearch range:NSMakeRange(0, 2)])
|
|
|
|
ALog(@"AudioScrobbler error: %@", response);
|
|
|
|
|
|
|
|
[client shutdown];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@catch(NSException *exception) {
|
|
|
|
[client shutdown];
|
|
|
|
}
|
|
|
|
|
|
|
|
[self setAudioScrobblerThreadCompleted:YES];
|
|
|
|
}
|
2007-02-25 02:43:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|