Skip to content

Commit

Permalink
remake player
Browse files Browse the repository at this point in the history
  • Loading branch information
Denis090909 committed Aug 5, 2021
1 parent bd4eb3a commit ab70375
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,10 @@
AudioQueueRef queue;
AudioQueueBufferRef mBuffers[kNumberBuffers];
AudioStreamBasicDescription playFormat;
int index;
int buffersNum;
NSLock *sysnLock;
BOOL audioQueueUsed[kNumberBuffers];
BOOL audioQueueIsStarted;
@public
Boolean isRunning;
Boolean isInitialized;
int pip_fd[2];
UInt32 numPacketsToRead;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,41 +12,37 @@ @implementation STCAudioPlayer
@synthesize queue;
@synthesize bufferByteSize;

void AQBufferCallback(void * inUserData ,
AudioQueueRef inAQ,
AudioQueueBufferRef inBuffer) {
void AQBufferCallback(void * inUserData,
AudioQueueRef inAQ,
AudioQueueBufferRef inBuffer) {
STCAudioPlayer *THIS = (__bridge STCAudioPlayer *)(inUserData);


if (THIS->isInitialized == NO) {
return;
}
ssize_t res = 0;

if(THIS->isRunning) {

if(THIS->bufferByteSize <= 0){
THIS->buffersNum -= 1;
if(THIS->buffersNum == 0) {
[THIS playEnded];
}
return;
}

if(THIS->bufferByteSize > 0) {
res = read(THIS->pip_fd[0], inBuffer->mAudioData, MIN(THIS->bufferByteSize, 4000));
}
if(res > 0 ){
THIS->bufferByteSize -= res;
inBuffer->mPacketDescriptionCount = res/2;

inBuffer->mAudioDataByteSize = res;

if(res > 0 ){
AudioQueueEnqueueBuffer(inAQ, inBuffer, 0, NULL);
AudioQueueEnqueueBuffer(inAQ, inBuffer, 0, NULL);
} else {
int bufferIndex = [THIS getIndexForBuffer: inBuffer];
if(bufferIndex >= 0) {
THIS->audioQueueUsed[bufferIndex] = NO;
if(![THIS bufferHasUsed]) {
[THIS playEnded];
}
}
}
}

-(void) playEnded {
-(void)playEnded {
if( self.delegate != nil) {
[[self delegate] playEnded:self];
}

}

-(id)init {
Expand All @@ -57,7 +53,6 @@ -(id)initWithSampleRate:(int)sampleRate {
self = [super init];
if(self) {
sysnLock = [[NSLock alloc]init];
buffersNum = 0;
memset(&playFormat, 0, sizeof(playFormat));
playFormat.mFormatID = kAudioFormatLinearPCM;
playFormat.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger | kLinearPCMFormatFlagIsPacked ;
Expand All @@ -66,82 +61,113 @@ -(id)initWithSampleRate:(int)sampleRate {
playFormat.mBytesPerPacket = playFormat.mBytesPerFrame = (playFormat.mBitsPerChannel / 8) * playFormat.mChannelsPerFrame;
playFormat.mFramesPerPacket = 1;
playFormat.mSampleRate = sampleRate;
isRunning = false;
isInitialized = false;
audioQueueIsStarted = false;
}
return self;
}

-(void)start {
[sysnLock lock];
if (isInitialized) {
[sysnLock unlock];
return;
}
[sysnLock lock];
isInitialized = true;
bufferByteSize = 0;
AudioQueueNewOutput(&playFormat, AQBufferCallback, (__bridge void *)(self), nil, nil, 0, &queue);
for (int i=0; i<kNumberBuffers; i++) {
buffersNum += 1;
AudioQueueAllocateBuffer( queue, kBufferSize, &mBuffers[i]);
audioQueueUsed[i] = NO;
}

AudioQueueSetParameter( queue, kAudioQueueParam_Volume, 1.0);

UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute, sizeof (audioRouteOverride), &audioRouteOverride);

isInitialized = true;
int ret = pipe(pip_fd);
if (ret == -1) {
NSLog(@"create pipe failed");
}
[sysnLock unlock];
}

-(void)stop {
-(void)stop {
[sysnLock lock];
if(!isInitialized) {
[sysnLock unlock];
return;
}
[sysnLock lock];
isInitialized = false;
close(pip_fd[0]);
close(pip_fd[1]);
AudioQueueStop(queue, false);
if (queue){
AudioQueueDispose(queue, true);
queue = NULL;
isRunning = false;
}
isInitialized = false;
[sysnLock unlock];
}

-(void)putAudioData:(short*)pcmData withSize:(int)dataSize{
if(!isInitialized) {
return;
}
-(void)putAudioData: (short*)pcmData withSize: (int)dataSize {
[sysnLock lock];
if (buffersNum < kNumberBuffers) {
if(!isInitialized) {
[sysnLock unlock];
return;
}
if (!isRunning) {
memcpy(mBuffers[index]->mAudioData, pcmData, dataSize);
mBuffers[index]->mAudioDataByteSize = dataSize;
mBuffers[index]->mPacketDescriptionCount = dataSize/2;
AudioQueueEnqueueBuffer(queue, mBuffers[index], 0, NULL);
NSLog(@"fill audio queue buffer[%d]",index);
if(index == kNumberBuffers - 1) {
isRunning = true;
index = 0;
int bufferIndex = [self getNotUsedBufferIndex];
if (bufferIndex >= 0) {
AudioQueueBufferRef audioQueueBuffer = mBuffers[bufferIndex];
memcpy(audioQueueBuffer->mAudioData, pcmData, dataSize);
audioQueueBuffer->mAudioDataByteSize = dataSize;
audioQueueBuffer->mPacketDescriptionCount = dataSize/2;
AudioQueueEnqueueBuffer(queue, audioQueueBuffer, 0, NULL);
audioQueueUsed[bufferIndex] = YES;
if (!audioQueueIsStarted && ![self bufferHasNotUsed]) {
audioQueueIsStarted = true;
AudioQueueStart(queue, NULL);
} else {
index++;
}
} else {
bufferByteSize += dataSize;
if(write(pip_fd[1], pcmData, dataSize) < 0){
NSLog(@"write to the pipe failed!");
NSLog(@"STCAudioPlayer: write to the pipe failed!");
}
}
[sysnLock unlock];
}

-(BOOL)bufferHasUsed {
for (int i = 0; i < kNumberBuffers; i++) {
if (YES == audioQueueUsed[i]) {
return YES;
}
}
return NO;
}

-(BOOL)bufferHasNotUsed {
for (int i = 0; i < kNumberBuffers; i++) {
if (NO == audioQueueUsed[i]) {
return YES;
}
}
return NO;
}

-(int)getNotUsedBufferIndex {
for (int i = 0; i < kNumberBuffers; i++) {
if (NO == audioQueueUsed[i]) {
return i;
}
}
return -1;
}

-(int)getIndexForBuffer: (AudioQueueBufferRef) audioQueueBuffer{
for (int i = 0; i < kNumberBuffers; i++) {
if (audioQueueBuffer == mBuffers[i]) {
return i;
}
}
return -1;
}

@end

0 comments on commit ab70375

Please sign in to comment.