From 972ae0e15b49cb9fe706a7488cbc23360787bd8c Mon Sep 17 00:00:00 2001 From: Thong Nguyen Date: Tue, 18 Feb 2014 19:02:47 +0000 Subject: [PATCH] Some more buffering data source work --- .../STKAutoRecoveringHTTPDataSource.m | 5 ++ .../StreamingKit/STKBufferingDataSource.h | 3 +- .../StreamingKit/STKBufferingDataSource.m | 72 +++++++++++++++---- StreamingKit/StreamingKit/STKDataSource.m | 2 +- 4 files changed, 68 insertions(+), 14 deletions(-) diff --git a/StreamingKit/StreamingKit/STKAutoRecoveringHTTPDataSource.m b/StreamingKit/StreamingKit/STKAutoRecoveringHTTPDataSource.m index 9248554..37fa5e5 100644 --- a/StreamingKit/StreamingKit/STKAutoRecoveringHTTPDataSource.m +++ b/StreamingKit/StreamingKit/STKAutoRecoveringHTTPDataSource.m @@ -292,6 +292,11 @@ static void PopulateOptionsWithDefault(STKAutoRecoveringHTTPDataSourceOptions* o -(void) dataSourceDataAvailable:(STKDataSource*)dataSource { + if (![self.innerDataSource hasBytesAvailable]) + { + return; + } + serial++; waitSeconds = 1; ticksWhenLastDataReceived = GetTickCount(); diff --git a/StreamingKit/StreamingKit/STKBufferingDataSource.h b/StreamingKit/StreamingKit/STKBufferingDataSource.h index c5cdefd..c41aafb 100644 --- a/StreamingKit/StreamingKit/STKBufferingDataSource.h +++ b/StreamingKit/StreamingKit/STKBufferingDataSource.h @@ -39,5 +39,6 @@ @property (readonly) SInt64 position; @property (readonly) SInt64 length; --(id) initWithDataSource:(STKDataSource*)dataSourceIn withBufferSize:(int)bufferSize; +-(id) initWithDataSource:(STKDataSource*)dataSourceIn withMaxSize:(int)maxSizeIn; + @end diff --git a/StreamingKit/StreamingKit/STKBufferingDataSource.m b/StreamingKit/StreamingKit/STKBufferingDataSource.m index ec4b9c2..77f88c0 100644 --- a/StreamingKit/StreamingKit/STKBufferingDataSource.m +++ b/StreamingKit/StreamingKit/STKBufferingDataSource.m @@ -37,7 +37,11 @@ @interface STKBufferingDataSource() { @private - int bufferSize; + int bufferStartIndex; + int bufferStartFileOffset; + int bufferBytesUsed; + int bufferBytesTotal; + SInt64 position; uint8_t* buffer; STKDataSource* dataSource; } @@ -45,12 +49,12 @@ @implementation STKBufferingDataSource --(id) initWithDataSource:(STKDataSource*)dataSourceIn withBufferSize:(int)bufferSizeIn; +-(id) initWithDataSource:(STKDataSource*)dataSourceIn withMaxSize:(int)maxSizeIn { if (self = [super init]) { - self->bufferSize = bufferSizeIn; self->dataSource = dataSourceIn; + self->bufferBytesTotal = maxSizeIn; self->dataSource.delegate = self.delegate; } @@ -69,9 +73,10 @@ { if (self->buffer == nil) { - if (self->bufferSize == 0) - { - } + self->bufferBytesTotal = MIN((int)self.length, self->bufferBytesTotal); + self->bufferBytesTotal = MAX(self->bufferBytesTotal, 1024); + + self->buffer = malloc(self->bufferBytesTotal); } } @@ -84,20 +89,63 @@ { } --(void) dataSourceDataAvailable:(STKDataSource*)dataSource +-(void) dataSourceDataAvailable:(STKDataSource*)dataSourceIn { - [self.delegate dataSourceDataAvailable:self]; + if (self->buffer == nil) + { + [self createBuffer]; + } + + UInt32 start = (bufferStartIndex + bufferBytesUsed) % bufferBytesTotal; + UInt32 end = (position - bufferStartFileOffset + bufferStartIndex) % bufferBytesTotal; + + if (start >= end) + { + int bytesRead; + int bytesToRead = bufferBytesTotal - start; + + if (bytesToRead > 0) + { + bytesRead = [dataSource readIntoBuffer:self->buffer + bufferStartIndex withSize:bytesToRead]; + } + else + { + bytesToRead = start; + + bytesRead = [dataSource readIntoBuffer:self->buffer withSize:bytesToRead]; + } + + if (bytesRead < 0) + { + return; + } + + bufferStartIndex += bytesRead; + bufferBytesUsed += bytesRead; + } + else + { + int bytesToRead = end - start; + + int bytesRead = [dataSource readIntoBuffer:self->buffer + start withSize:bytesToRead]; + + if (bytesToRead < 0) + { + return; + } + + bufferStartIndex += bytesRead; + bufferBytesUsed += bytesRead; + } } --(void) dataSourceErrorOccured:(STKDataSource*)dataSource +-(void) dataSourceErrorOccured:(STKDataSource*)dataSourceIn { [self.delegate dataSourceErrorOccured:self]; } --(void) dataSourceEof:(STKDataSource*)dataSource +-(void) dataSourceEof:(STKDataSource*)dataSourceIn { - [self.delegate dataSourceEof:self]; } - @end diff --git a/StreamingKit/StreamingKit/STKDataSource.m b/StreamingKit/StreamingKit/STKDataSource.m index a6f4911..9abd821 100644 --- a/StreamingKit/StreamingKit/STKDataSource.m +++ b/StreamingKit/StreamingKit/STKDataSource.m @@ -66,7 +66,7 @@ } -(void) close -{ +{ } -(BOOL) hasBytesAvailable