我在 mp3 中编码并使用套接字连接写入 icecast 服务器。但问题是,虽然生成了一个新缓冲区,但转换为编码字节的新缓冲区并没有被写入 server 。
// ____________________________________________________________________________________
// AudioQueue callback function, called when an input buffers has been filled.
void AQRecorder::MyInputBufferHandler( void * inUserData,
AudioQueueRef inAQ,
AudioQueueBufferRef inBuffer,
const AudioTimeStamp * inStartTime,
UInt32 inNumPackets,
const AudioStreamPacketDescription* inPacketDesc)
{
AQRecorder *aqr = (AQRecorder *)inUserData;
try {
NSLog(@"Checking NumPacket if > 0 ");
if (inNumPackets > 0) {
// write packets to file
XThrowIfError(AudioFileWritePackets(aqr->mRecordFile, FALSE, inBuffer->mAudioDataByteSize,
inPacketDesc, aqr->mRecordPacket, &inNumPackets, inBuffer->mAudioData),
"AudioFileWritePackets failed");
aqr->mRecordPacket += inNumPackets;
NSLog(@"size = %u",(unsigned int)inBuffer->mAudioDataByteSize);
// const int PCM_SIZE = 44100;
int MP3_SIZE = inBuffer->mAudioDataByteSize * 4;
// short int pcm_buffer[PCM_SIZE*2];
unsigned char mp3_buffer[MP3_SIZE];
// unsigned char * outBuf = (unsigned char *)malloc(MP3_SIZE);
// short int leftpcm[inNumPackets];
// short int rightpcm[inNumPackets];
//
//
// leftpcm[inNumPackets] = ((short int *)inBuffer->mAudioData)[2*inNumPackets];
// rightpcm[inNumPackets] = ((short int *)inBuffer->mAudioData)[2 *inNumPackets + 1 ];
lame_t lame = lame_init();
lame_set_in_samplerate(lame, 44100);
lame_set_VBR(lame, vbr_default);
lame_init_params(lame);
int encodedBytes=lame_encode_buffer_interleaved(lame, (short int *)inBuffer->mAudioData , aqr->mRecordPacket, mp3_buffer, MP3_SIZE);
[data initWithBytes:mp3_buffer length:encodedBytes];
//[data appendBytes:mp3_buffer length:encodedBytes];
server *srv = [[server alloc]init];
srv.dataBuffer=data;
[srv connecting];
mp3_buffer[MP3_SIZE]=nil;
[data release];
lame_close(lame);
}
// if we're not stopping, re-enqueue the buffe so that it gets filled again
if (aqr->IsRunning())
XThrowIfError(AudioQueueEnqueueBuffer(inAQ, inBuffer, 0, NULL), "AudioQueueEnqueueBuffer failed");
} catch (CAXException e) {
char buf[256];
fprintf(stderr, "Error: %s (%s)\n", e.mOperation, e.FormatError(buf));
}
}