Added mime type support.

CQTexperiment
vspader 2007-10-14 18:12:15 +00:00
parent c536ea06a5
commit f1b9141f4b
14 changed files with 111 additions and 44 deletions

View File

@ -13,6 +13,6 @@
@interface AudioDecoder : NSObject { @interface AudioDecoder : NSObject {
} }
+ (id<CogDecoder>)audioDecoderForURL:(NSURL *)url; + (id<CogDecoder>)audioDecoderForSource:(id <CogSource>)source;
@end @end

View File

@ -12,9 +12,9 @@
@implementation AudioDecoder @implementation AudioDecoder
+ (id<CogDecoder>) audioDecoderForURL:(NSURL *)url + (id<CogDecoder>) audioDecoderForSource:(id <CogSource>)source
{ {
return [[PluginController sharedPluginController] audioDecoderForURL:url]; return [[PluginController sharedPluginController] audioDecoderForSource:source];
} }
@end @end

View File

@ -322,9 +322,9 @@
PluginController *pluginController = [PluginController sharedPluginController]; PluginController *pluginController = [PluginController sharedPluginController];
NSArray *containerTypes = [[pluginController containers] allKeys]; NSArray *containerTypes = [[pluginController containers] allKeys];
NSArray *decoderTypes = [[pluginController decoders] allKeys]; NSArray *decoderTypes = [[pluginController decodersByExtension] allKeys];
NSArray *metdataReaderTypes = [[pluginController metadataReaders] allKeys]; NSArray *metdataReaderTypes = [[pluginController metadataReaders] allKeys];
NSArray *propertiesReaderTypes = [[pluginController propertiesReaders] allKeys]; NSArray *propertiesReaderTypes = [[pluginController propertiesReadersByExtension] allKeys];
NSMutableSet *types = [NSMutableSet set]; NSMutableSet *types = [NSMutableSet set];

View File

@ -58,7 +58,7 @@
[converterNode setOutputFormat:outputFormat]; [converterNode setOutputFormat:outputFormat];
if (![inputNode openURL:url withSource:source]) if (![inputNode openWithSource:source])
return NO; return NO;
// return NO; // return NO;

View File

@ -23,7 +23,7 @@
double seekTime; double seekTime;
} }
- (BOOL)openURL:(NSURL *)url withSource:(id<CogSource>)source; - (BOOL)openWithSource:(id<CogSource>)source;
- (BOOL)openWithDecoder:(id<CogDecoder>) d; - (BOOL)openWithDecoder:(id<CogDecoder>) d;
- (void)process; - (void)process;

View File

@ -13,9 +13,9 @@
@implementation InputNode @implementation InputNode
- (BOOL)openURL:(NSURL *)url withSource:(id<CogSource>)source - (BOOL)openWithSource:(id<CogSource>)source
{ {
decoder = [AudioDecoder audioDecoderForURL:url]; decoder = [AudioDecoder audioDecoderForSource:source];
[decoder retain]; [decoder retain];
if (decoder == nil) if (decoder == nil)

View File

@ -63,9 +63,10 @@
@protocol CogPluginController <NSObject> @protocol CogPluginController <NSObject>
- (id<CogSource>) audioSourceForURL:(NSURL *)url; - (id<CogSource>) audioSourceForURL:(NSURL *)url;
- (NSArray *) urlsForContainerURL:(NSURL *)url; - (NSArray *) urlsForContainerURL:(NSURL *)url;
- (id<CogDecoder>) audioDecoderForURL:(NSURL *)url;
- (NSDictionary *) metadataForURL:(NSURL *)url; - (NSDictionary *) metadataForURL:(NSURL *)url;
- (NSDictionary *) propertiesForURL:(NSURL *)url; - (NSDictionary *) propertiesForURL:(NSURL *)url;
- (id<CogDecoder>) audioDecoderForSource:(id<CogSource>)source;
@end @end

View File

@ -9,9 +9,13 @@
{ {
NSMutableDictionary *sources; NSMutableDictionary *sources;
NSMutableDictionary *containers; NSMutableDictionary *containers;
NSMutableDictionary *decoders;
NSMutableDictionary *metadataReaders; NSMutableDictionary *metadataReaders;
NSMutableDictionary *propertiesReaders;
NSMutableDictionary *propertiesReadersByExtension;
NSMutableDictionary *propertiesReadersByMimeType;
NSMutableDictionary *decodersByExtension;
NSMutableDictionary *decodersByMimeType;
} }
+ (PluginController *)sharedPluginController; //Use this to get the instance. + (PluginController *)sharedPluginController; //Use this to get the instance.
@ -30,8 +34,12 @@
- (NSDictionary *)sources; - (NSDictionary *)sources;
- (NSDictionary *)containers; - (NSDictionary *)containers;
- (NSDictionary *)decoders;
- (NSDictionary *)metadataReaders; - (NSDictionary *)metadataReaders;
- (NSDictionary *)propertiesReaders;
- (NSDictionary *)propertiesReadersByExtension;
- (NSDictionary *)propertiesReadersByMimeType;
- (NSDictionary *)decodersByExtension;
- (NSDictionary *)decodersByMimeType;
@end @end

View File

@ -60,9 +60,14 @@ static PluginController *sharedPluginController = nil;
if (self) { if (self) {
sources = [[NSMutableDictionary alloc] init]; sources = [[NSMutableDictionary alloc] init];
containers = [[NSMutableDictionary alloc] init]; containers = [[NSMutableDictionary alloc] init];
decoders = [[NSMutableDictionary alloc] init];
metadataReaders = [[NSMutableDictionary alloc] init]; metadataReaders = [[NSMutableDictionary alloc] init];
propertiesReaders = [[NSMutableDictionary alloc] init];
propertiesReadersByExtension = [[NSMutableDictionary alloc] init];
propertiesReadersByMimeType = [[NSMutableDictionary alloc] init];
decodersByExtension = [[NSMutableDictionary alloc] init];
decodersByMimeType = [[NSMutableDictionary alloc] init];
} }
return self; return self;
@ -154,11 +159,20 @@ static PluginController *sharedPluginController = nil;
{ {
Class decoder = NSClassFromString(className); Class decoder = NSClassFromString(className);
if (decoder && [decoder respondsToSelector:@selector(fileTypes)]) { if (decoder && [decoder respondsToSelector:@selector(fileTypes)]) {
NSEnumerator *fileTypesEnum = [[decoder fileTypes] objectEnumerator];
id fileType; id fileType;
NSEnumerator *fileTypesEnum = [[decoder fileTypes] objectEnumerator];
while (fileType = [fileTypesEnum nextObject]) while (fileType = [fileTypesEnum nextObject])
{ {
[decoders setObject:className forKey:[fileType lowercaseString]]; [decodersByExtension setObject:className forKey:[fileType lowercaseString]];
}
}
if (decoder && [decoder respondsToSelector:@selector(mimeTypes)]) {
id mimeType;
NSEnumerator *mimeTypesEnum = [[decoder mimeTypes] objectEnumerator];
while (mimeType = [mimeTypesEnum nextObject])
{
[decodersByMimeType setObject:className forKey:[mimeType lowercaseString]];
} }
} }
} }
@ -184,7 +198,16 @@ static PluginController *sharedPluginController = nil;
id fileType; id fileType;
while (fileType = [fileTypesEnum nextObject]) while (fileType = [fileTypesEnum nextObject])
{ {
[propertiesReaders setObject:className forKey:[fileType lowercaseString]]; [propertiesReadersByExtension setObject:className forKey:[fileType lowercaseString]];
}
}
if (propertiesReader && [propertiesReader respondsToSelector:@selector(mimeTypes)]) {
id mimeType;
NSEnumerator *mimeTypesEnum = [[propertiesReader mimeTypes] objectEnumerator];
while (mimeType = [mimeTypesEnum nextObject])
{
[propertiesReadersByMimeType setObject:className forKey:[mimeType lowercaseString]];
} }
} }
} }
@ -206,9 +229,13 @@ static PluginController *sharedPluginController = nil;
{ {
NSLog(@"Sources: %@", sources); NSLog(@"Sources: %@", sources);
NSLog(@"Containers: %@", containers); NSLog(@"Containers: %@", containers);
NSLog(@"Decoders: %@", decoders);
NSLog(@"Metadata Readers: %@", metadataReaders); NSLog(@"Metadata Readers: %@", metadataReaders);
NSLog(@"Properties Readers: %@", propertiesReaders);
NSLog(@"Properties Readers By Extension: %@", propertiesReadersByExtension);
NSLog(@"Properties Readers By Mime Type: %@", propertiesReadersByMimeType);
NSLog(@"Decoders by Extension: %@", decodersByExtension);
NSLog(@"Decoders by Mime Type: %@", decodersByMimeType);
} }
- (NSDictionary *)sources - (NSDictionary *)sources
@ -221,14 +248,24 @@ static PluginController *sharedPluginController = nil;
return containers; return containers;
} }
- (NSDictionary *)decoders - (NSDictionary *)decodersByExtension
{ {
return decoders; return decodersByExtension;
} }
- (NSDictionary *)propertiesReaders - (NSDictionary *)decodersByMimeType
{ {
return propertiesReaders; return decodersByMimeType;
}
- (NSDictionary *)propertiesReadersByExtension
{
return propertiesReadersByExtension;
}
- (NSDictionary *)propertiesReadersByMimeType
{
return propertiesReadersByMimeType;
} }
- (NSDictionary *)metadataReaders - (NSDictionary *)metadataReaders
@ -255,11 +292,16 @@ static PluginController *sharedPluginController = nil;
return [container urlsForContainerURL:url]; return [container urlsForContainerURL:url];
} }
- (id<CogDecoder>) audioDecoderForURL:(NSURL *)url //Note: Source is assumed to already be opened.
- (id<CogDecoder>) audioDecoderForSource:(id <CogSource>)source
{ {
NSString *ext = [[url path] pathExtension]; NSString *ext = [[[source url] path] pathExtension];
NSString *classString = [decodersByExtension objectForKey:[ext lowercaseString]];
if (!classString) {
classString = [decodersByMimeType objectForKey:[[source mimeType] lowercaseString]];
}
Class decoder = NSClassFromString([decoders objectForKey:[ext lowercaseString]]); Class decoder = NSClassFromString(classString);
return [[[decoder alloc] init] autorelease]; return [[[decoder alloc] init] autorelease];
} }
@ -284,7 +326,11 @@ static PluginController *sharedPluginController = nil;
if (![source open:url]) if (![source open:url])
return nil; return nil;
NSString *classString = [propertiesReaders objectForKey:[ext lowercaseString]]; NSString *classString = [propertiesReadersByExtension objectForKey:[ext lowercaseString]];
if (!classString) {
classString = [propertiesReadersByMimeType objectForKey:[[source mimeType] lowercaseString]];
}
if (classString) if (classString)
{ {
Class propertiesReader = NSClassFromString(classString); Class propertiesReader = NSClassFromString(classString);
@ -293,7 +339,8 @@ static PluginController *sharedPluginController = nil;
} }
else else
{ {
id<CogDecoder> decoder = [self audioDecoderForURL:url];
id<CogDecoder> decoder = [self audioDecoderForSource:source];
if (![decoder open:source]) if (![decoder open:source])
{ {
return nil; return nil;

View File

@ -213,7 +213,7 @@
continue; continue;
//Need a better way to determine acceptable file types than basing it on extensions. //Need a better way to determine acceptable file types than basing it on extensions.
if (![[AudioPlayer fileTypes] containsObject:[[[url path] pathExtension] lowercaseString]]) if ([url isFileURL] && ![[AudioPlayer fileTypes] containsObject:[[[url path] pathExtension] lowercaseString]])
continue; continue;
if (![uniqueURLs containsObject:url]) if (![uniqueURLs containsObject:url])

View File

@ -17,6 +17,4 @@
NSURL *_url; NSURL *_url;
} }
- (void)setURL:(NSURL *)url;
@end @end

View File

@ -13,18 +13,14 @@
- (BOOL)open:(NSURL *)url - (BOOL)open:(NSURL *)url
{ {
[self setURL:url]; _url = url;
[_url retain];
_fd = fopen([[url path] UTF8String], "r"); _fd = fopen([[url path] UTF8String], "r");
return (_fd != NULL); return (_fd != NULL);
} }
- (NSDictionary *)properties
{
return nil;
}
- (BOOL)seekable - (BOOL)seekable
{ {
return YES; return YES;
@ -47,9 +43,11 @@
- (void)close - (void)close
{ {
[self setURL:nil]; [_url release];
_url = nil;
fclose(_fd); fclose(_fd);
_fd = NULL;
} }
- (NSURL *)url - (NSURL *)url
@ -57,6 +55,11 @@
return _url; return _url;
} }
- (NSString *)mimeType
{
return nil;
}
- (void)setURL:(NSURL *)url - (void)setURL:(NSURL *)url
{ {
[url retain]; [url retain];

View File

@ -15,7 +15,7 @@
{ {
_url = [url copy]; _url = [url copy];
_responseReceived = YES; _responseReceived = NO;
_connectionFinished = NO; _connectionFinished = NO;
_byteCount = 0; _byteCount = 0;
_data = [[NSMutableData alloc] init]; _data = [[NSMutableData alloc] init];
@ -55,6 +55,7 @@
- (NSString *)mimeType - (NSString *)mimeType
{ {
NSLog(@"Returning mimetype! %@", _mimeType);
return _mimeType; return _mimeType;
} }
@ -122,6 +123,9 @@
[_url release]; [_url release];
_url = nil; _url = nil;
[_mimeType release];
_mimeType = nil;
[_sem release]; [_sem release];
_sem = nil; _sem = nil;
} }
@ -146,10 +150,11 @@
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response -(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{ {
NSLog(@"Received response: %@", [response MIMEType]); _mimeType = [[response MIMEType] copy];
_mimeType = [response MIMEType];
_responseReceived = YES; _responseReceived = YES;
NSLog(@"Received response: %@", _mimeType);
[_sem signal]; [_sem signal];
} }

View File

@ -618,5 +618,10 @@ static inline signed int scale (mad_fixed_t sample)
return [NSArray arrayWithObjects:@"mp3",nil]; return [NSArray arrayWithObjects:@"mp3",nil];
} }
+ (NSArray *)mimeTypes
{
return [NSArray arrayWithObjects:@"audio/mpeg",nil];
}
@end @end