2006-01-20 12:34:02 -03:00
|
|
|
//
|
2006-04-02 11:44:08 -04:00
|
|
|
// InputNode.m
|
2006-01-20 12:34:02 -03:00
|
|
|
// Cog
|
|
|
|
//
|
2006-09-04 14:46:18 -04:00
|
|
|
// Created by Vincent Spader on 8/2/05.
|
|
|
|
// Copyright 2005 Vincent Spader. All rights reserved.
|
2006-01-20 12:34:02 -03:00
|
|
|
//
|
|
|
|
|
|
|
|
#import "InputNode.h"
|
2022-02-07 02:49:27 -03:00
|
|
|
#import "AudioPlayer.h"
|
2007-02-24 17:36:27 -03:00
|
|
|
#import "BufferChain.h"
|
2007-03-03 14:19:37 -03:00
|
|
|
#import "CoreAudioUtils.h"
|
2013-10-21 02:04:09 -03:00
|
|
|
#import "OutputNode.h"
|
2022-02-07 02:49:27 -03:00
|
|
|
#import "Plugin.h"
|
2013-10-02 03:00:16 -03:00
|
|
|
|
2013-10-11 09:03:55 -03:00
|
|
|
#import "Logging.h"
|
|
|
|
|
2006-01-20 12:34:02 -03:00
|
|
|
@implementation InputNode
|
2013-10-12 17:59:34 -03:00
|
|
|
@synthesize exitAtTheEndOfTheStream;
|
|
|
|
|
2013-10-12 17:52:58 -03:00
|
|
|
- (id)initWithController:(id)c previous:(id)p {
|
2022-02-07 02:49:27 -03:00
|
|
|
self = [super initWithController:c previous:p];
|
|
|
|
if(self) {
|
|
|
|
exitAtTheEndOfTheStream = [[Semaphore alloc] init];
|
|
|
|
}
|
2013-10-12 17:52:58 -03:00
|
|
|
|
2022-02-07 02:49:27 -03:00
|
|
|
return self;
|
2013-10-12 17:52:58 -03:00
|
|
|
}
|
|
|
|
|
2022-02-07 02:49:27 -03:00
|
|
|
- (BOOL)openWithSource:(id<CogSource>)source {
|
2007-10-14 15:12:15 -03:00
|
|
|
decoder = [AudioDecoder audioDecoderForSource:source];
|
2007-03-03 14:19:37 -03:00
|
|
|
|
2022-02-07 02:49:27 -03:00
|
|
|
if(decoder == nil)
|
2006-04-12 22:51:22 -04:00
|
|
|
return NO;
|
2007-02-24 17:36:27 -03:00
|
|
|
|
2007-10-10 22:08:29 -04:00
|
|
|
[self registerObservers];
|
|
|
|
|
2022-02-07 02:49:27 -03:00
|
|
|
if(![decoder open:source]) {
|
2013-10-11 09:03:55 -03:00
|
|
|
ALog(@"Couldn't open decoder...");
|
2007-02-24 17:36:27 -03:00
|
|
|
return NO;
|
2007-03-01 22:36:52 -03:00
|
|
|
}
|
2022-02-07 02:49:27 -03:00
|
|
|
|
2007-11-24 17:16:27 -03:00
|
|
|
NSDictionary *properties = [decoder properties];
|
|
|
|
int bitsPerSample = [[properties objectForKey:@"bitsPerSample"] intValue];
|
|
|
|
int channels = [[properties objectForKey:@"channels"] intValue];
|
2022-02-07 02:49:27 -03:00
|
|
|
|
2021-12-11 05:22:19 -03:00
|
|
|
bytesPerFrame = ((bitsPerSample + 7) / 8) * channels;
|
2022-02-07 02:49:27 -03:00
|
|
|
|
|
|
|
nodeFormat = propertiesToASBD(properties);
|
|
|
|
nodeLossless = [[properties valueForKey:@"encoding"] isEqualToString:@"lossless"];
|
|
|
|
|
2006-04-02 11:44:08 -04:00
|
|
|
shouldContinue = YES;
|
2006-04-02 16:03:12 -04:00
|
|
|
shouldSeek = NO;
|
2007-11-24 17:16:27 -03:00
|
|
|
|
2006-04-12 22:51:22 -04:00
|
|
|
return YES;
|
2006-01-20 12:34:02 -03:00
|
|
|
}
|
|
|
|
|
2022-02-07 02:49:27 -03:00
|
|
|
- (BOOL)openWithDecoder:(id<CogDecoder>)d {
|
2013-10-11 09:03:55 -03:00
|
|
|
DLog(@"Opening with old decoder: %@", d);
|
2007-10-10 22:08:29 -04:00
|
|
|
decoder = d;
|
2008-05-01 19:34:23 -04:00
|
|
|
|
|
|
|
NSDictionary *properties = [decoder properties];
|
|
|
|
int bitsPerSample = [[properties objectForKey:@"bitsPerSample"] intValue];
|
|
|
|
int channels = [[properties objectForKey:@"channels"] intValue];
|
2022-02-07 02:49:27 -03:00
|
|
|
|
2021-12-11 05:22:19 -03:00
|
|
|
bytesPerFrame = ((bitsPerSample + 7) / 8) * channels;
|
2022-02-07 02:49:27 -03:00
|
|
|
|
|
|
|
nodeFormat = propertiesToASBD(properties);
|
|
|
|
nodeLossless = [[properties valueForKey:@"encoding"] isEqualToString:@"lossless"];
|
2022-02-06 08:08:34 -03:00
|
|
|
|
2008-05-01 19:34:23 -04:00
|
|
|
[self registerObservers];
|
|
|
|
|
2007-10-10 22:08:29 -04:00
|
|
|
shouldContinue = YES;
|
|
|
|
shouldSeek = NO;
|
2022-02-07 02:49:27 -03:00
|
|
|
|
2013-10-11 09:03:55 -03:00
|
|
|
DLog(@"DONES: %@", decoder);
|
2007-10-10 22:08:29 -04:00
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
2022-02-07 02:49:27 -03:00
|
|
|
- (void)registerObservers {
|
2013-10-11 09:03:55 -03:00
|
|
|
DLog(@"REGISTERING OBSERVERS");
|
2007-03-03 14:19:37 -03:00
|
|
|
[decoder addObserver:self
|
2022-02-07 02:49:27 -03:00
|
|
|
forKeyPath:@"properties"
|
|
|
|
options:(NSKeyValueObservingOptionNew)
|
|
|
|
context:NULL];
|
2007-03-03 14:19:37 -03:00
|
|
|
|
|
|
|
[decoder addObserver:self
|
2022-02-07 02:49:27 -03:00
|
|
|
forKeyPath:@"metadata"
|
|
|
|
options:(NSKeyValueObservingOptionNew)
|
|
|
|
context:NULL];
|
|
|
|
|
|
|
|
observersAdded = YES;
|
2007-03-03 14:19:37 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)observeValueForKeyPath:(NSString *)keyPath
|
2022-02-07 02:49:27 -03:00
|
|
|
ofObject:(id)object
|
2007-03-03 14:19:37 -03:00
|
|
|
change:(NSDictionary *)change
|
2022-02-07 02:49:27 -03:00
|
|
|
context:(void *)context {
|
2013-10-11 09:03:55 -03:00
|
|
|
DLog(@"SOMETHING CHANGED!");
|
2022-02-07 02:49:27 -03:00
|
|
|
if([keyPath isEqual:@"properties"]) {
|
|
|
|
DLog(@"Input format changed");
|
|
|
|
// Converter may need resetting, it'll do that when it reaches the new chunks
|
|
|
|
NSDictionary *properties = [decoder properties];
|
|
|
|
nodeFormat = propertiesToASBD(properties);
|
|
|
|
nodeLossless = [[properties valueForKey:@"encoding"] isEqualToString:@"lossless"];
|
|
|
|
} else if([keyPath isEqual:@"metadata"]) {
|
|
|
|
// Inform something of metadata change
|
2007-03-03 14:19:37 -03:00
|
|
|
}
|
2013-10-05 18:15:09 -03:00
|
|
|
}
|
|
|
|
|
2022-02-07 02:49:27 -03:00
|
|
|
- (void)process {
|
2007-11-24 17:16:27 -03:00
|
|
|
int amountInBuffer = 0;
|
2007-03-03 18:13:25 -03:00
|
|
|
void *inputBuffer = malloc(CHUNK_SIZE);
|
2022-02-07 02:49:27 -03:00
|
|
|
|
2007-10-10 22:08:29 -04:00
|
|
|
BOOL shouldClose = YES;
|
2022-02-07 02:49:27 -03:00
|
|
|
BOOL seekError = NO;
|
|
|
|
|
|
|
|
while([self shouldContinue] == YES && [self endOfStream] == NO) {
|
|
|
|
if(shouldSeek == YES) {
|
|
|
|
ConverterNode *converter = [[[controller controller] bufferChain] converter];
|
2021-12-26 04:41:45 -03:00
|
|
|
DLog(@"SEEKING! Resetting Buffer");
|
2022-02-07 02:49:27 -03:00
|
|
|
|
|
|
|
amountInBuffer = 0;
|
|
|
|
// This resets the converter's buffer
|
|
|
|
[self resetBuffer];
|
|
|
|
[converter resetBuffer];
|
|
|
|
[converter inputFormatDidChange:[[[controller controller] bufferChain] inputFormat]];
|
|
|
|
|
|
|
|
DLog(@"Reset buffer!");
|
|
|
|
|
|
|
|
DLog(@"SEEKING!");
|
|
|
|
seekError = [decoder seek:seekFrame] < 0;
|
|
|
|
|
2006-04-02 16:03:12 -04:00
|
|
|
shouldSeek = NO;
|
2013-10-11 09:03:55 -03:00
|
|
|
DLog(@"Seeked! Resetting Buffer");
|
2007-05-26 18:13:11 -04:00
|
|
|
initialBufferFilled = NO;
|
2006-04-02 16:03:12 -04:00
|
|
|
}
|
2007-05-15 21:06:23 -04:00
|
|
|
|
2022-02-07 02:49:27 -03:00
|
|
|
if(amountInBuffer < CHUNK_SIZE) {
|
|
|
|
int framesToRead = (CHUNK_SIZE - amountInBuffer) / bytesPerFrame;
|
2007-11-24 17:16:27 -03:00
|
|
|
int framesRead = [decoder readAudio:((char *)inputBuffer) + amountInBuffer frames:framesToRead];
|
2009-03-07 02:58:50 -03:00
|
|
|
|
2022-02-07 02:49:27 -03:00
|
|
|
if(framesRead > 0 && !seekError) {
|
|
|
|
amountInBuffer += (framesRead * bytesPerFrame);
|
|
|
|
[self writeData:inputBuffer amount:amountInBuffer];
|
|
|
|
amountInBuffer = 0;
|
|
|
|
} else {
|
|
|
|
if(initialBufferFilled == NO) {
|
2007-10-11 22:55:59 -04:00
|
|
|
[controller initialBufferFilled:self];
|
2007-10-03 16:23:14 -04:00
|
|
|
}
|
2022-02-07 02:49:27 -03:00
|
|
|
|
2013-10-11 09:03:55 -03:00
|
|
|
DLog(@"End of stream? %@", [self properties]);
|
2013-10-12 17:52:58 -03:00
|
|
|
|
2007-10-03 16:23:14 -04:00
|
|
|
endOfStream = YES;
|
2022-02-07 02:49:27 -03:00
|
|
|
shouldClose = [controller endOfInputReached]; // Lets us know if we should keep going or not (occassionally, for track changes within a file)
|
2013-10-11 09:03:55 -03:00
|
|
|
DLog(@"closing? is %i", shouldClose);
|
2013-10-12 17:52:58 -03:00
|
|
|
|
2022-02-07 02:49:27 -03:00
|
|
|
// wait before exiting, as we might still get seeking request
|
|
|
|
DLog("InputNode: Before wait")
|
|
|
|
[exitAtTheEndOfTheStream waitIndefinitely];
|
|
|
|
DLog("InputNode: After wait, should seek = %d", shouldSeek) if(shouldSeek) {
|
|
|
|
endOfStream = NO;
|
|
|
|
shouldClose = NO;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
break;
|
|
|
|
}
|
2007-05-16 19:07:00 -04:00
|
|
|
}
|
2022-02-07 02:49:27 -03:00
|
|
|
}
|
2006-01-20 12:34:02 -03:00
|
|
|
}
|
2013-10-12 17:52:58 -03:00
|
|
|
|
2022-02-07 02:49:27 -03:00
|
|
|
if(shouldClose)
|
2007-10-10 22:08:29 -04:00
|
|
|
[decoder close];
|
2013-10-12 17:52:58 -03:00
|
|
|
|
2022-02-07 02:49:27 -03:00
|
|
|
free(inputBuffer);
|
2013-10-12 17:52:58 -03:00
|
|
|
|
2022-02-07 02:49:27 -03:00
|
|
|
[exitAtTheEndOfTheStream signal];
|
2013-10-12 17:59:34 -03:00
|
|
|
|
2022-02-07 02:49:27 -03:00
|
|
|
DLog("Input node thread stopping");
|
2006-01-20 12:34:02 -03:00
|
|
|
}
|
|
|
|
|
2022-02-07 02:49:27 -03:00
|
|
|
- (void)seek:(long)frame {
|
2007-11-24 17:16:27 -03:00
|
|
|
seekFrame = frame;
|
2006-04-02 16:03:12 -04:00
|
|
|
shouldSeek = YES;
|
2013-10-11 09:03:55 -03:00
|
|
|
DLog(@"Should seek!");
|
2007-05-26 18:13:11 -04:00
|
|
|
[semaphore signal];
|
2013-10-12 17:52:58 -03:00
|
|
|
|
2022-02-07 02:49:27 -03:00
|
|
|
if(endOfStream) {
|
|
|
|
[exitAtTheEndOfTheStream signal];
|
|
|
|
}
|
2006-04-02 16:03:12 -04:00
|
|
|
}
|
|
|
|
|
2022-02-07 02:49:27 -03:00
|
|
|
- (BOOL)setTrack:(NSURL *)track {
|
|
|
|
if([decoder respondsToSelector:@selector(setTrack:)] && [decoder setTrack:track]) {
|
2013-10-11 09:03:55 -03:00
|
|
|
DLog(@"SET TRACK!");
|
2022-02-07 02:49:27 -03:00
|
|
|
|
2007-10-10 22:08:29 -04:00
|
|
|
return YES;
|
|
|
|
}
|
2022-02-07 02:49:27 -03:00
|
|
|
|
2007-10-10 22:08:29 -04:00
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
2022-02-07 02:49:27 -03:00
|
|
|
- (void)removeObservers {
|
|
|
|
if(observersAdded) {
|
|
|
|
[decoder removeObserver:self forKeyPath:@"properties"];
|
|
|
|
[decoder removeObserver:self forKeyPath:@"metadata"];
|
|
|
|
observersAdded = NO;
|
|
|
|
}
|
2022-01-30 02:10:59 -03:00
|
|
|
}
|
|
|
|
|
2022-02-07 02:49:27 -03:00
|
|
|
- (void)setShouldContinue:(BOOL)s {
|
|
|
|
[super setShouldContinue:s];
|
|
|
|
if(!s)
|
|
|
|
[self removeObservers];
|
2021-12-26 07:01:02 -03:00
|
|
|
}
|
|
|
|
|
2022-02-07 02:49:27 -03:00
|
|
|
- (void)dealloc {
|
2013-10-11 09:03:55 -03:00
|
|
|
DLog(@"Input Node dealloc");
|
2022-02-07 02:49:27 -03:00
|
|
|
[self removeObservers];
|
2007-02-24 17:36:27 -03:00
|
|
|
}
|
|
|
|
|
2022-02-07 02:49:27 -03:00
|
|
|
- (NSDictionary *)properties {
|
2007-02-24 17:36:27 -03:00
|
|
|
return [decoder properties];
|
2006-01-20 12:34:02 -03:00
|
|
|
}
|
|
|
|
|
2022-02-07 02:49:27 -03:00
|
|
|
- (id<CogDecoder>)decoder {
|
2007-10-10 22:08:29 -04:00
|
|
|
return decoder;
|
|
|
|
}
|
|
|
|
|
2022-02-07 02:49:27 -03:00
|
|
|
- (double)secondsBuffered {
|
|
|
|
return [buffer listDuration];
|
2022-01-14 04:05:32 -03:00
|
|
|
}
|
|
|
|
|
2006-01-20 12:34:02 -03:00
|
|
|
@end
|