Navigation Menu

Skip to content

Commit

Permalink
Clean up after ourselves when done streaming
Browse files Browse the repository at this point in the history
In addition to closing the read stream, clear out our "cache" of packets and
don't flag ourselves as waiting any more so a spurious message won't wake things
up.

Also when finishing the stream, only set ourselves to AS_DONE when all packets
have been fully consumed by the audio stream, not when the network stream
finishes.
  • Loading branch information
alexcrichton committed Jun 14, 2012
1 parent 85374c1 commit a1e8198
Showing 1 changed file with 35 additions and 19 deletions.
54 changes: 35 additions & 19 deletions AudioStreamer/AudioStreamer.m
Expand Up @@ -536,12 +536,7 @@ - (BOOL)seekToTime:(double)newSeekTime {
}
}

/* Close the old stream */
if (stream) {
CFReadStreamClose(stream);
CFRelease(stream);
stream = nil;
}
[self closeReadStream];

/* Stop audio for now */
err = AudioQueueStop(audioQueue, true);
Expand All @@ -568,12 +563,8 @@ - (double)progress {
AudioTimeStamp queueTime;
Boolean discontinuity;
err = AudioQueueGetCurrentTime(audioQueue, NULL, &queueTime, &discontinuity);

const OSStatus AudioQueueStopped = 0x73746F70; // 0x73746F70 is 'stop'
if (err == AudioQueueStopped) {
if (err) {
return lastProgress;
} else if (err) {
[self failWithErrorCode:AS_GET_AUDIO_TIME_FAILED];
}

double progress = seekTime + queueTime.mSampleTime / sampleRate;
Expand Down Expand Up @@ -662,12 +653,12 @@ - (void) play {
// back to false
//
- (void)stop {
if (stream) {
CFReadStreamClose(stream);
CFRelease(stream);
stream = nil;
if (![self isDone]) {
[self setState:AS_STOPPED];
}

[self closeReadStream];

//
// Close the audio file strea,
//
Expand All @@ -692,9 +683,25 @@ - (void)stop {
packetsFilled = 0;
seekByteOffset = 0;
packetBufferSize = 0;
}

if (![self isDone]) {
[self setState:AS_STOPPED];
/**
* @brief Closes the read stream and frees all queued data
*/
- (void) closeReadStream {
if (waitingOnBuffer) waitingOnBuffer = FALSE;
queued_packet_t *cur = queued_head;
while (cur != NULL) {
queued_packet_t *tmp = cur->next;
free(cur);
cur = tmp;
}
queued_head = queued_tail = NULL;

if (stream) {
CFReadStreamClose(stream);
CFRelease(stream);
stream = nil;
}
}

Expand Down Expand Up @@ -742,7 +749,6 @@ - (void)handleReadFromStream:(CFReadStreamRef)aStream
err = AudioQueueStop(audioQueue, false);
CHECK_ERR(err, AS_AUDIO_QUEUE_STOP_FAILED);
}
[self setState:AS_DONE];
}
return;

Expand Down Expand Up @@ -1218,7 +1224,13 @@ - (void)handleBufferCompleteForQueue:(AudioQueueRef)inAQ
@synchronized(self) {
inuse[idx] = false;
buffersUsed--;
if (waitingOnBuffer) {
if (buffersUsed == 0 && queued_head == NULL && stream != nil &&
CFReadStreamGetStatus(stream) == kCFStreamStatusAtEnd) {
assert(!waitingOnBuffer);
[self performSelectorOnMainThread:@selector(setStateObj:)
withObject:[NSNumber numberWithInt:AS_DONE]
waitUntilDone:NO];
} else if (waitingOnBuffer) {
waitingOnBuffer = false;
[self performSelectorOnMainThread:@selector(enqueueCachedData)
withObject:nil
Expand All @@ -1227,6 +1239,10 @@ - (void)handleBufferCompleteForQueue:(AudioQueueRef)inAQ
}
}

- (void) setStateObj:(NSNumber*) num {
[self setState:[num intValue]];
}

//
// handlePropertyChangeForQueue:propertyID:
//
Expand Down

0 comments on commit a1e8198

Please sign in to comment.