mediastreamer2 library - modular sound and video processing and streaming
Copyright (C) 2013 Belledonne Communications SARL
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <mediastreamer2/msfilter.h>
#include <AudioToolbox/AudioToolbox.h>
...
...
@@ -12,282 +31,270 @@ struct EncState {
intnbytes;/* amount of data in a processedTime window usually sampling rate*time*number of byte per sample*number of channels */
intnchannels;/* number of channels, default 1(mono) */
MSBufferizer*bufferizer;/* buffer to store data from input queue before processing them */
uint8_t*inputBuffer;/* buffer to store nbytes of input data and send them to the encoder */
/** AAC ELD related properties */
AudioStreamBasicDescriptionsourceFormat;/* description of input audio format */
AudioStreamBasicDescriptiondestinationFormat;/* description of output audio format */
AudioConverterRefaudioConverter;
UInt32maxOutputPacketSize;/* maximum size of the output packet */
UInt32bytesToEncode;
uint8_t*inputBuffer;/* buffer to store nbytes of input data and send them to the encoder */
/** AAC ELD related properties */
AudioStreamBasicDescriptionsourceFormat;/* description of input audio format */
AudioStreamBasicDescriptiondestinationFormat;/* description of output audio format */
AudioConverterRefaudioConverter;
UInt32maxOutputPacketSize;/* maximum size of the output packet */
UInt32bytesToEncode;
};
/* Encoder */
/* called at init and any modification of ptime: compute the input data length */
staticvoidenc_update(structEncState*s){
s->nbytes=(sizeof(AudioSampleType)*s->nchannels*s->samplingRate*s->ptime)/1000;/* input is 16 bits LPCM: 2 bytes per sample, ptime is in ms so /1000 */
/* nbytes % SIGNAL_FRAME_SIZE must be 0, min SIGNAL_FRAME_SIZE */
if(s->nbytes>SIGNAL_FRAME_SIZE){
s->nbytes-=(s->nbytes%SIGNAL_FRAME_SIZE);
}else{
s->nbytes=SIGNAL_FRAME_SIZE;
}
ms_message("nbytes is %d",s->nbytes);
staticvoidenc_update(structEncState*s){
s->nbytes=(sizeof(AudioSampleType)*s->nchannels*s->samplingRate*s->ptime)/1000;/* input is 16 bits LPCM: 2 bytes per sample, ptime is in ms so /1000 */
/* nbytes % SIGNAL_FRAME_SIZE must be 0, min SIGNAL_FRAME_SIZE */
if(s->nbytes>SIGNAL_FRAME_SIZE){
s->nbytes-=(s->nbytes%SIGNAL_FRAME_SIZE);
}else{
s->nbytes=SIGNAL_FRAME_SIZE;
}
ms_message("nbytes is %d",s->nbytes);
}
/* init the encoder: create an encoder State structure, initialise it and attach it to the MSFilter structure data property */
ms_message("AAC-ELD encoder received incorrect sampling/bitrate settings (sr%d br%d). Switch back to narrow band settings: 22050Hz 32000b/s",s->samplingRate,s->bitRate);
ms_message("AAC-ELD encoder received incorrect sampling/bitrate settings (sr%d br%d). Switch back to narrow band settings: 22050Hz 32000b/s",s->samplingRate,s->bitRate);
mblk_t*outputMessage=allocb(s->maxOutputPacketSize,0);/* create an output message of requested size(may be actually to big but allocate the maximum output buffer for the encoder */
/*** encoding ***/
/* create an audio stream packet description to feed the encoder in order to get the description of encoded data */
AudioStreamPacketDescriptionoutPacketDesc[1];
/* encode a 512 sample frame (nbytes is 512 or a greater multiple */
mblk_t*outputMessage=allocb(s->maxOutputPacketSize,0);/* create an output message of requested size(may be actually to big but allocate the maximum output buffer for the encoder */
/*** encoding ***/
/* create an audio stream packet description to feed the encoder in order to get the description of encoded data */
AudioStreamPacketDescriptionoutPacketDesc[1];
/* encode a 512 sample frame (nbytes is 512 or a greater multiple */
s->timeStamp+=s->nbytes/(sizeof(AudioSampleType)*s->nchannels);/* increment timeStamp by the number of sample processed (divided by the number of channels) */
s->timeStamp+=s->nbytes/(sizeof(AudioSampleType)*s->nchannels);/* increment timeStamp by the number of sample processed (divided by the number of channels) */
/* insert the output message into the output queue of MSFilter */
ms_queue_put(f->outputs[0],outputMessage);
ms_queue_put(f->outputs[0],outputMessage);
}
/* release the lock */
ms_filter_unlock(f);
ms_filter_unlock(f);
}
staticvoidenc_postprocess(MSFilter*f)
{
staticvoidenc_postprocess(MSFilter*f){
}
staticvoidenc_uninit(MSFilter*f)
{
structEncState*s=(structEncState*)f->data;
ms_bufferizer_destroy(s->bufferizer);
free(s->inputBuffer);
ms_free(s);
staticvoidenc_uninit(MSFilter*f){
structEncState*s=(structEncState*)f->data;
ms_bufferizer_destroy(s->bufferizer);
free(s->inputBuffer);
ms_free(s);
f->data=0;
}
staticvoidset_ptime(structEncState*s,intvalue){
if(value>0&&value<=100){
staticvoidset_ptime(structEncState*s,intvalue){
if(value>0&&value<=100){
s->ptime=value;
ms_message("AAC-ELD encoder using ptime=%i",value);
enc_update(s);
ms_message("AAC-ELD encoder using ptime=%i",value);