omxsymbianoutputstreamtest.c

Go to the documentation of this file.
00001 
00031 #include <stdio.h>
00032 #include <stdlib.h>
00033 #include <unistd.h>
00034 #include <pthread.h>
00035 #include <sys/mman.h>
00036 #include <sys/types.h>
00037 #include <sys/stat.h>
00038 #include <fcntl.h>
00039 #include <string.h>
00040 
00041 #include <OMX_Core.h>
00042 #include <OMX_Component.h>
00043 #include <OMX_Types.h>
00044 #include <OMX_Audio.h>
00045 
00046 #include "omxsymbianoutputstreamtest.h"
00047 #include "tsemaphore.h"
00048 
00049 /* Application private date: should go in the component field (segs...) */
00050 appPrivateType* appPriv;
00051 
00052 int main(int argc, char** argv)
00053 {
00054     OMX_ERRORTYPE err;
00055     OMX_HANDLETYPE handle;
00056     OMX_CALLBACKTYPE callbacks;
00057     
00058 
00059     OMX_PORT_PARAM_TYPE param;
00060     OMX_AUDIO_PARAM_PCMMODETYPE omxAudioParamPcmMode;
00061     OMX_BUFFERHEADERTYPE *omxbuffer1, *omxbuffer2;
00062     int fd = 0;
00063     unsigned int filesize;
00064     int data_read;
00065     int curr_buff;
00066     int i;
00067 
00068     callbacks.EventHandler = symbianoutputstreamEventHandler;
00069     callbacks.EmptyBufferDone = symbianoutputstreamEmptyBufferDone;
00070     callbacks.FillBufferDone = NULL;
00071 
00072     DEBUG(DEB_LEV_PARAMS, "Starting SymbianOutputStreamTest\n");
00073 
00074       fd = open("C:\\music_stereo_44k.raw", O_RDONLY);
00075       if(fd < 0)
00076       {
00077         DEBUG(DEB_LEV_PARAMS, "Error with file descriptor\n");
00078         exit(1);
00079       }
00080 
00081     filesize = getFileSize(fd);
00082     
00083     /* Initialize application private data */
00084     appPriv = malloc(sizeof(appPrivateType));
00085     pthread_cond_init(&appPriv->condition, NULL);
00086     pthread_mutex_init(&appPriv->mutex, NULL);
00087     appPriv->eventSem = malloc(sizeof(tsem_t));
00088     tsem_init(appPriv->eventSem, 0);
00089     
00090     err = OMX_Init();
00091     
00094     err = OMX_GetHandle(&handle, "OMX.nokia.outputstream.component", NULL /*appPriv */, &callbacks);
00095     if(err != OMX_ErrorNone){
00096         DEBUG(DEB_LEV_ERR, "Component Not Found!!\n");
00097         exit(1);
00098     }
00099     
00102     err = OMX_GetParameter(handle, OMX_IndexParamAudioInit, &param);
00103     if(err != OMX_ErrorNone){
00104         DEBUG(DEB_LEV_ERR, "Error in getting OMX_PORT_PARAM_TYPE parameter\n");
00105         exit(1);
00106     }
00107     param.nPorts = 1;
00108     err = OMX_SetParameter(handle, OMX_IndexParamAudioInit, &param);
00109     if(err != OMX_ErrorNone){
00110       DEBUG(DEB_LEV_ERR, "Error in setting OMX_PORT_PARAM_TYPE parameter\n");
00111       exit(1);
00112     }
00113 
00116     /*
00117     omxAudioPortDefinition.nPortIndex = 0;
00118     err = OMX_GetParameter(handle, OMX_IndexParamPortDefinition, &omxAudioPortDefinition);
00119     if(err != OMX_ErrorNone){
00120         DEBUG(DEB_LEV_ERR, "Error in getting OMX_PORT_PARAM_TYPE parameter\n");
00121         exit(1);
00122     }
00123     */
00124 
00127     omxAudioParamPcmMode.nPortIndex = 0;
00128     err = OMX_GetParameter(handle, OMX_IndexParamAudioPcm, &omxAudioParamPcmMode);
00129     DEBUG(DEB_LEV_PARAMS, "Default PCM rate is %i\n", (int)omxAudioParamPcmMode.nSamplingRate);
00130     omxAudioParamPcmMode.nSamplingRate = 44100;
00131     err = OMX_SetParameter(handle, OMX_IndexParamAudioPcm, &omxAudioParamPcmMode);
00132     if(err != OMX_ErrorNone){
00133       DEBUG(DEB_LEV_ERR, "Error in setting OMX_PORT_PARAM_TYPE parameter\n");
00134       exit(1);
00135     }
00136     DEBUG(DEB_LEV_PARAMS, "Now PCM rate is %i\n", (int)omxAudioParamPcmMode.nSamplingRate);
00137     
00138     
00139     err = OMX_SendCommand(handle, OMX_CommandStateSet, OMX_StateIdle, NULL);
00140     omxbuffer1 = omxbuffer2 = NULL;
00141     err = OMX_AllocateBuffer(handle, &omxbuffer1, 0, NULL, BUFFER_SIZE);
00142     if (err != OMX_ErrorNone) {
00143       DEBUG(DEB_LEV_ERR, "Error on AllocateBuffer out 1 %i\n", err);
00144       exit(1);
00145     }
00146     
00147     err = OMX_AllocateBuffer(handle, &omxbuffer2, 0, NULL, BUFFER_SIZE);
00148     if (err != OMX_ErrorNone) {
00149       DEBUG(DEB_LEV_ERR, "Error on AllocateBuffer out 2 %i\n", err);
00150       exit(1);
00151     }
00152 
00153     tsem_down(appPriv->eventSem);
00154 
00155     err = OMX_SendCommand(handle, OMX_CommandStateSet, OMX_StateExecuting, NULL);
00156 
00157     omxbuffer1->nInputPortIndex = 0;
00158     omxbuffer2->nInputPortIndex = 0;
00159     
00160     /* Wait for commands to complete */
00161     tsem_down(appPriv->eventSem);
00162     
00163     /* Now, toss down one buffer */
00164     data_read = read(fd, omxbuffer1->pBuffer, BUFFER_SIZE);
00165     omxbuffer1->nFilledLen = data_read;
00166     err = OMX_EmptyThisBuffer(handle, omxbuffer1);
00167     curr_buff = 1;
00168     
00169     DEBUG(DEB_LEV_PARAMS, "SymbianOutputStreamTest starting data processing loop\n");
00170 
00171     /* Data processing loop */
00172     i = filesize/BUFFER_SIZE;
00173     while(--i){
00174         /* It's very important to mutual exclude this section.
00175          * We dont wanna wakeup something which nobody is waiting on...
00176          */
00177         pthread_mutex_lock(&appPriv->mutex);
00178 
00179         if(curr_buff == 2){
00180             data_read = read(fd, omxbuffer1->pBuffer, BUFFER_SIZE);
00181             if (data_read == 0)
00182                 break;
00183             // omxbuffer1->nFilledLen = data_read;
00184             omxbuffer1->nFilledLen = BUFFER_SIZE;
00185             err = OMX_EmptyThisBuffer(handle, omxbuffer1);
00186             curr_buff = 1;
00187         }
00188         else{
00189             data_read = read(fd, omxbuffer2->pBuffer, BUFFER_SIZE);
00190             if (data_read == 0)
00191                 break;
00192             //omxbuffer2->nFilledLen = data_read;
00193             omxbuffer2->nFilledLen = BUFFER_SIZE;
00194             err = OMX_EmptyThisBuffer(handle, omxbuffer2);
00195             curr_buff = 2;
00196         }
00197         
00198         DEBUG(DEB_LEV_FULL_SEQ, "Waiting for empty buffer to complete...\n");
00199         pthread_cond_wait(&appPriv->condition, &appPriv->mutex);
00200         pthread_mutex_unlock(&appPriv->mutex);
00201         DEBUG(DEB_LEV_FULL_SEQ, "Input buffer processed...\n");
00202     }
00203     err = OMX_SendCommand(handle, OMX_CommandStateSet, OMX_StateIdle, NULL);
00204     tsem_down(appPriv->eventSem);
00205     
00206     err = OMX_SendCommand(handle, OMX_CommandStateSet, OMX_StateLoaded, NULL);
00207     
00208     err = OMX_FreeBuffer(handle, 0, omxbuffer1);
00209     err = OMX_FreeBuffer(handle, 0, omxbuffer2);
00210     
00211     tsem_down(appPriv->eventSem);
00212     
00213     OMX_FreeHandle(handle);
00214 
00215     free(appPriv->eventSem);
00216     free(appPriv);
00217     
00218     return 0;
00219 }
00220 
00221 /* Callbacks implementation */
00222 OMX_ERRORTYPE symbianoutputstreamEventHandler(
00223     OMX_OUT OMX_HANDLETYPE hComponent,
00224     OMX_OUT OMX_PTR pAppData,
00225     OMX_OUT OMX_EVENTTYPE eEvent,
00226     OMX_OUT OMX_U32 Data1,
00227     OMX_OUT OMX_U32 Data2,
00228     OMX_OUT OMX_PTR pEventData)
00229 {
00230     DEBUG(DEB_LEV_SIMPLE_SEQ, "Hi there, I am in the %s callback\n", __func__);
00231     DEBUG(DEB_LEV_SIMPLE_SEQ, "Param1 is %i\n", (int)Data1);
00232     DEBUG(DEB_LEV_SIMPLE_SEQ, "Param2 is %i\n", (int)Data2);
00233     tsem_up(appPriv->eventSem);
00234     return OMX_ErrorNone;
00235 }
00236 
00237 OMX_ERRORTYPE symbianoutputstreamEmptyBufferDone(
00238     OMX_OUT OMX_HANDLETYPE hComponent,
00239     OMX_OUT OMX_PTR pAppData,
00240     OMX_OUT OMX_BUFFERHEADERTYPE* pBuffer)
00241 {
00242     pthread_mutex_lock(&appPriv->mutex);
00243     DEBUG(DEB_LEV_FULL_SEQ, "Notification for amoty buffer done %08X\n", (int)pBuffer);
00244     DEBUG(DEB_LEV_FULL_SEQ, "Waking up condition in %s", __func__);
00245     pthread_cond_signal(&appPriv->condition);
00246     pthread_mutex_unlock(&appPriv->mutex);
00247     return OMX_ErrorNone;
00248 }
00249 
00250 OMX_ERRORTYPE symbianoutputstreamFillBufferDone(
00251     OMX_OUT OMX_HANDLETYPE hComponent,
00252     OMX_OUT OMX_PTR pAppData,
00253     OMX_OUT OMX_BUFFERHEADERTYPE* pBuffer)
00254 {
00255     DEBUG(DEB_LEV_FULL_SEQ, "%s: Hmm... shouldn't be here...\n", __func__);
00256     return OMX_ErrorNotImplemented;
00257 }
00258 
00259 static int getFileSize(int fd)
00260 {
00261     struct stat input_file_stat;
00262     int err;
00263     
00264     /* Obtain input file length */
00265     err = fstat(fd, &input_file_stat);
00266     if(err){
00267         DEBUG(DEB_LEV_ERR, "fstat failed");
00268         exit(-1);
00269     }
00270     return input_file_stat.st_size;
00271 }

Generated for OpenMAX Bellagio rel. 0.3.5-svn by  doxygen 1.5.1
SourceForge.net Logo