I'm developing a voice chat and I used speex to开发者_Python百科 compress the data that is being transmitted. But I encountered a crash when I try to decode the received data. I compress the data using this code:
Init method (runs only one time):
/*Create a new encoder state in narrowband mode*/
state = speex_encoder_init(&speex_nb_mode);
/*Set the quality to 8 (15 kbps)*/
int tmp=8;
speex_encoder_ctl(state, SPEEX_SET_QUALITY, &tmp);
speex_bits_init(&bits);
Compress method:
AudioBuffer sourceBuffer = bufferList->mBuffers[0];
speex_bits_reset(&bits);
/*Encode the frame*/
speex_encode(state, sourceBuffer.mData, &bits);
char cbits[200];
int nbBytes = speex_bits_write(&bits, cbits, 200);
NSData *result = [[NSData alloc] initWithBytes:cbits length:200];
Decompress method:
NSLog(@"Lenght %d", [data length]);
speex_bits_reset(&bits);
/*Copy the data into the bit-stream struct*/
speex_bits_read_from(&bits, (void*)[data bytes], [data length]);
/*Decode the data*/
speex_decode(state, &bits, tempBuffer.mData);
Same initialization as before
It crashes with no error at speex_decode. I checked the length of the input NSData and the output one and they are the same. Bytes inside are the same.
Euh, so are you actually passing the encoder state to speex_decode()??
精彩评论