1/*--------------------------------------------------------------------------
2Copyright (c) 2010, 2014 The Linux Foundation. All rights reserved.
3
4Redistribution and use in source and binary forms, with or without
5modification, are permitted provided that the following conditions are met:
6    * Redistributions of source code must retain the above copyright
7      notice, this list of conditions and the following disclaimer.
8    * Redistributions in binary form must reproduce the above copyright
9      notice, this list of conditions and the following disclaimer in the
10      documentation and/or other materials provided with the distribution.
11    * Neither the name of The Linux Foundation nor
12      the names of its contributors may be used to endorse or promote
13      products derived from this software without specific prior written
14      permission.
15
16THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19NON-INFRINGEMENT ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
20CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
23OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
25OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
26ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27--------------------------------------------------------------------------*/
28/*============================================================================
29@file omx_aenc_qcelp13.c
30  This module contains the implementation of the OpenMAX core & component.
31
32*//*========================================================================*/
33//////////////////////////////////////////////////////////////////////////////
34//                             Include Files
35//////////////////////////////////////////////////////////////////////////////
36
37
38#include<string.h>
39#include <fcntl.h>
40#include <sys/ioctl.h>
41#include "omx_qcelp13_aenc.h"
42#include <errno.h>
43
44using namespace std;
45#define SLEEP_MS 100
46
47// omx_cmd_queue destructor
48omx_qcelp13_aenc::omx_cmd_queue::~omx_cmd_queue()
49{
50    // Nothing to do
51}
52
53// omx cmd queue constructor
54omx_qcelp13_aenc::omx_cmd_queue::omx_cmd_queue(): m_read(0),m_write(0),m_size(0)
55{
56    memset(m_q,      0,sizeof(omx_event)*OMX_CORE_CONTROL_CMDQ_SIZE);
57}
58
59// omx cmd queue insert
60bool omx_qcelp13_aenc::omx_cmd_queue::insert_entry(unsigned long p1,
61                                                unsigned long p2,
62                                                unsigned char id)
63{
64    bool ret = true;
65    if (m_size < OMX_CORE_CONTROL_CMDQ_SIZE)
66    {
67        m_q[m_write].id       = id;
68        m_q[m_write].param1   = p1;
69        m_q[m_write].param2   = p2;
70        m_write++;
71        m_size ++;
72        if (m_write >= OMX_CORE_CONTROL_CMDQ_SIZE)
73        {
74            m_write = 0;
75        }
76    } else
77    {
78        ret = false;
79        DEBUG_PRINT_ERROR("ERROR!!! Command Queue Full");
80    }
81    return ret;
82}
83
84bool omx_qcelp13_aenc::omx_cmd_queue::pop_entry(unsigned long *p1,
85                                        unsigned long *p2, unsigned char *id)
86{
87    bool ret = true;
88    if (m_size > 0)
89    {
90        *id = m_q[m_read].id;
91        *p1 = m_q[m_read].param1;
92        *p2 = m_q[m_read].param2;
93        // Move the read pointer ahead
94        ++m_read;
95        --m_size;
96        if (m_read >= OMX_CORE_CONTROL_CMDQ_SIZE)
97        {
98            m_read = 0;
99
100        }
101    } else
102    {
103        ret = false;
104        DEBUG_PRINT_ERROR("ERROR Delete!!! Command Queue Empty");
105    }
106    return ret;
107}
108
109// factory function executed by the core to create instances
110void *get_omx_component_factory_fn(void)
111{
112    return(new omx_qcelp13_aenc);
113}
114bool omx_qcelp13_aenc::omx_cmd_queue::get_msg_id(unsigned char *id)
115{
116   if(m_size > 0)
117   {
118       *id = m_q[m_read].id;
119       DEBUG_PRINT("get_msg_id=%d\n",*id);
120   }
121   else{
122       return false;
123   }
124   return true;
125}
126/*=============================================================================
127FUNCTION:
128  wait_for_event
129
130DESCRIPTION:
131  waits for a particular event
132
133INPUT/OUTPUT PARAMETERS:
134  None
135
136RETURN VALUE:
137  None
138
139Dependency:
140  None
141
142SIDE EFFECTS:
143   None
144=============================================================================*/
145void omx_qcelp13_aenc::wait_for_event()
146{
147    int               rc;
148    struct timespec   ts;
149    pthread_mutex_lock(&m_event_lock);
150    while (0 == m_is_event_done)
151    {
152       clock_gettime(CLOCK_REALTIME, &ts);
153       ts.tv_sec += (SLEEP_MS/1000);
154       ts.tv_nsec += ((SLEEP_MS%1000) * 1000000);
155       rc = pthread_cond_timedwait(&cond, &m_event_lock, &ts);
156       if (rc == ETIMEDOUT && !m_is_event_done) {
157            DEBUG_PRINT("Timed out waiting for flush");
158            if (ioctl( m_drv_fd, AUDIO_FLUSH, 0) == -1)
159                DEBUG_PRINT_ERROR("Flush:Input port, ioctl flush failed %d\n",
160                    errno);
161       }
162    }
163    m_is_event_done = 0;
164    pthread_mutex_unlock(&m_event_lock);
165}
166
167/*=============================================================================
168FUNCTION:
169  event_complete
170
171DESCRIPTION:
172  informs about the occurance of an event
173
174INPUT/OUTPUT PARAMETERS:
175  None
176
177RETURN VALUE:
178  None
179
180Dependency:
181  None
182
183SIDE EFFECTS:
184   None
185=============================================================================*/
186void omx_qcelp13_aenc::event_complete()
187{
188    pthread_mutex_lock(&m_event_lock);
189    if (0 == m_is_event_done)
190    {
191        m_is_event_done = 1;
192        pthread_cond_signal(&cond);
193    }
194    pthread_mutex_unlock(&m_event_lock);
195}
196
197// All this non-sense because of a single qcelp13 object
198void omx_qcelp13_aenc::in_th_goto_sleep()
199{
200    pthread_mutex_lock(&m_in_th_lock);
201    while (0 == m_is_in_th_sleep)
202    {
203        pthread_cond_wait(&in_cond, &m_in_th_lock);
204    }
205    m_is_in_th_sleep = 0;
206    pthread_mutex_unlock(&m_in_th_lock);
207}
208
209void omx_qcelp13_aenc::in_th_wakeup()
210{
211    pthread_mutex_lock(&m_in_th_lock);
212    if (0 == m_is_in_th_sleep)
213    {
214        m_is_in_th_sleep = 1;
215        pthread_cond_signal(&in_cond);
216    }
217    pthread_mutex_unlock(&m_in_th_lock);
218}
219
220void omx_qcelp13_aenc::out_th_goto_sleep()
221{
222
223    pthread_mutex_lock(&m_out_th_lock);
224    while (0 == m_is_out_th_sleep)
225    {
226        pthread_cond_wait(&out_cond, &m_out_th_lock);
227    }
228    m_is_out_th_sleep = 0;
229    pthread_mutex_unlock(&m_out_th_lock);
230}
231
232void omx_qcelp13_aenc::out_th_wakeup()
233{
234    pthread_mutex_lock(&m_out_th_lock);
235    if (0 == m_is_out_th_sleep)
236    {
237        m_is_out_th_sleep = 1;
238        pthread_cond_signal(&out_cond);
239    }
240    pthread_mutex_unlock(&m_out_th_lock);
241}
242/* ======================================================================
243FUNCTION
244  omx_qcelp13_aenc::omx_qcelp13_aenc
245
246DESCRIPTION
247  Constructor
248
249PARAMETERS
250  None
251
252RETURN VALUE
253  None.
254========================================================================== */
255omx_qcelp13_aenc::omx_qcelp13_aenc(): m_tmp_meta_buf(NULL),
256        m_tmp_out_meta_buf(NULL),
257        m_flush_cnt(255),
258        m_comp_deinit(0),
259        m_volume(25),
260        m_app_data(NULL),
261        nNumInputBuf(0),
262        nNumOutputBuf(0),
263        m_drv_fd(-1),
264        bFlushinprogress(0),
265        is_in_th_sleep(false),
266        is_out_th_sleep(false),
267        m_flags(0),
268        nTimestamp(0),
269        pcm_input(0),
270        m_inp_act_buf_count (OMX_CORE_NUM_INPUT_BUFFERS),
271        m_out_act_buf_count (OMX_CORE_NUM_OUTPUT_BUFFERS),
272        m_inp_current_buf_count(0),
273        m_out_current_buf_count(0),
274        output_buffer_size((OMX_U32)OMX_QCELP13_OUTPUT_BUFFER_SIZE),
275        input_buffer_size(OMX_CORE_INPUT_BUFFER_SIZE),
276        m_session_id(0),
277        m_inp_bEnabled(OMX_TRUE),
278        m_out_bEnabled(OMX_TRUE),
279        m_inp_bPopulated(OMX_FALSE),
280        m_out_bPopulated(OMX_FALSE),
281        m_is_event_done(0),
282        m_state(OMX_StateInvalid),
283        m_ipc_to_in_th(NULL),
284        m_ipc_to_out_th(NULL),
285        m_ipc_to_cmd_th(NULL),
286        m_ipc_to_event_th(NULL)
287{
288    int cond_ret = 0;
289    component_Role.nSize = 0;
290    memset(&m_cmp, 0, sizeof(m_cmp));
291    memset(&m_cb, 0, sizeof(m_cb));
292    memset(&m_qcelp13_pb_stats, 0, sizeof(m_qcelp13_pb_stats));
293    memset(&m_qcelp13_param, 0, sizeof(m_qcelp13_param));
294    memset(&m_pcm_param, 0, sizeof(m_pcm_param));
295    memset(&m_buffer_supplier, 0, sizeof(m_buffer_supplier));
296    memset(&m_priority_mgm, 0, sizeof(m_priority_mgm));
297
298    pthread_mutexattr_init(&m_lock_attr);
299    pthread_mutex_init(&m_lock, &m_lock_attr);
300    pthread_mutexattr_init(&m_commandlock_attr);
301    pthread_mutex_init(&m_commandlock, &m_commandlock_attr);
302
303    pthread_mutexattr_init(&m_outputlock_attr);
304    pthread_mutex_init(&m_outputlock, &m_outputlock_attr);
305
306    pthread_mutexattr_init(&m_state_attr);
307    pthread_mutex_init(&m_state_lock, &m_state_attr);
308
309    pthread_mutexattr_init(&m_event_attr);
310    pthread_mutex_init(&m_event_lock, &m_event_attr);
311
312    pthread_mutexattr_init(&m_flush_attr);
313    pthread_mutex_init(&m_flush_lock, &m_flush_attr);
314
315    pthread_mutexattr_init(&m_event_attr);
316    pthread_mutex_init(&m_event_lock, &m_event_attr);
317
318    pthread_mutexattr_init(&m_in_th_attr);
319    pthread_mutex_init(&m_in_th_lock, &m_in_th_attr);
320
321    pthread_mutexattr_init(&m_out_th_attr);
322    pthread_mutex_init(&m_out_th_lock, &m_out_th_attr);
323
324    pthread_mutexattr_init(&m_in_th_attr_1);
325    pthread_mutex_init(&m_in_th_lock_1, &m_in_th_attr_1);
326
327    pthread_mutexattr_init(&m_out_th_attr_1);
328    pthread_mutex_init(&m_out_th_lock_1, &m_out_th_attr_1);
329
330    pthread_mutexattr_init(&out_buf_count_lock_attr);
331    pthread_mutex_init(&out_buf_count_lock, &out_buf_count_lock_attr);
332
333    pthread_mutexattr_init(&in_buf_count_lock_attr);
334    pthread_mutex_init(&in_buf_count_lock, &in_buf_count_lock_attr);
335    if ((cond_ret = pthread_cond_init (&cond, NULL)) != 0)
336    {
337       DEBUG_PRINT_ERROR("pthread_cond_init returns non zero for cond\n");
338       if (cond_ret == EAGAIN)
339         DEBUG_PRINT_ERROR("The system lacked necessary \
340				resources(other than mem)\n");
341       else if (cond_ret == ENOMEM)
342          DEBUG_PRINT_ERROR("Insufficient memory to initialise \
343				condition variable\n");
344    }
345    if ((cond_ret = pthread_cond_init (&in_cond, NULL)) != 0)
346    {
347       DEBUG_PRINT_ERROR("pthread_cond_init returns non zero for in_cond\n");
348       if (cond_ret == EAGAIN)
349         DEBUG_PRINT_ERROR("The system lacked necessary \
350				resources(other than mem)\n");
351       else if (cond_ret == ENOMEM)
352          DEBUG_PRINT_ERROR("Insufficient memory to initialise \
353				condition variable\n");
354    }
355    if ((cond_ret = pthread_cond_init (&out_cond, NULL)) != 0)
356    {
357       DEBUG_PRINT_ERROR("pthread_cond_init returns non zero for out_cond\n");
358       if (cond_ret == EAGAIN)
359         DEBUG_PRINT_ERROR("The system lacked necessary \
360				resources(other than mem)\n");
361       else if (cond_ret == ENOMEM)
362          DEBUG_PRINT_ERROR("Insufficient memory to initialise \
363				condition variable\n");
364    }
365
366    sem_init(&sem_read_msg,0, 0);
367    sem_init(&sem_write_msg,0, 0);
368    sem_init(&sem_States,0, 0);
369    return;
370}
371
372
373/* ======================================================================
374FUNCTION
375  omx_qcelp13_aenc::~omx_qcelp13_aenc
376
377DESCRIPTION
378  Destructor
379
380PARAMETERS
381  None
382
383RETURN VALUE
384  None.
385========================================================================== */
386omx_qcelp13_aenc::~omx_qcelp13_aenc()
387{
388    DEBUG_PRINT_ERROR("QCELP13 Object getting destroyed comp-deinit=%d\n",
389			m_comp_deinit);
390    if ( !m_comp_deinit )
391    {
392        deinit_encoder();
393    }
394    pthread_mutexattr_destroy(&m_lock_attr);
395    pthread_mutex_destroy(&m_lock);
396
397    pthread_mutexattr_destroy(&m_commandlock_attr);
398    pthread_mutex_destroy(&m_commandlock);
399
400    pthread_mutexattr_destroy(&m_outputlock_attr);
401    pthread_mutex_destroy(&m_outputlock);
402
403    pthread_mutexattr_destroy(&m_state_attr);
404    pthread_mutex_destroy(&m_state_lock);
405
406    pthread_mutexattr_destroy(&m_event_attr);
407    pthread_mutex_destroy(&m_event_lock);
408
409    pthread_mutexattr_destroy(&m_flush_attr);
410    pthread_mutex_destroy(&m_flush_lock);
411
412    pthread_mutexattr_destroy(&m_in_th_attr);
413    pthread_mutex_destroy(&m_in_th_lock);
414
415    pthread_mutexattr_destroy(&m_out_th_attr);
416    pthread_mutex_destroy(&m_out_th_lock);
417
418    pthread_mutexattr_destroy(&out_buf_count_lock_attr);
419    pthread_mutex_destroy(&out_buf_count_lock);
420
421    pthread_mutexattr_destroy(&in_buf_count_lock_attr);
422    pthread_mutex_destroy(&in_buf_count_lock);
423
424    pthread_mutexattr_destroy(&m_in_th_attr_1);
425    pthread_mutex_destroy(&m_in_th_lock_1);
426
427    pthread_mutexattr_destroy(&m_out_th_attr_1);
428    pthread_mutex_destroy(&m_out_th_lock_1);
429    pthread_cond_destroy(&cond);
430    pthread_cond_destroy(&in_cond);
431    pthread_cond_destroy(&out_cond);
432    sem_destroy (&sem_read_msg);
433    sem_destroy (&sem_write_msg);
434    sem_destroy (&sem_States);
435    DEBUG_PRINT_ERROR("OMX QCELP13 component destroyed\n");
436    return;
437}
438
439/**
440  @brief memory function for sending EmptyBufferDone event
441   back to IL client
442
443  @param bufHdr OMX buffer header to be passed back to IL client
444  @return none
445 */
446void omx_qcelp13_aenc::buffer_done_cb(OMX_BUFFERHEADERTYPE *bufHdr)
447{
448    if (m_cb.EmptyBufferDone)
449    {
450        PrintFrameHdr(OMX_COMPONENT_GENERATE_BUFFER_DONE,bufHdr);
451        bufHdr->nFilledLen = 0;
452
453        m_cb.EmptyBufferDone(&m_cmp, m_app_data, bufHdr);
454        pthread_mutex_lock(&in_buf_count_lock);
455        m_qcelp13_pb_stats.ebd_cnt++;
456        nNumInputBuf--;
457        DEBUG_DETAIL("EBD CB:: in_buf_len=%d nNumInputBuf=%d\n ebd_cnt=%d",\
458                     m_qcelp13_pb_stats.tot_in_buf_len,
459                     nNumInputBuf, m_qcelp13_pb_stats.ebd_cnt);
460        pthread_mutex_unlock(&in_buf_count_lock);
461    }
462
463    return;
464}
465
466/*=============================================================================
467FUNCTION:
468  flush_ack
469
470DESCRIPTION:
471
472
473INPUT/OUTPUT PARAMETERS:
474  None
475
476RETURN VALUE:
477  None
478
479Dependency:
480  None
481
482SIDE EFFECTS:
483  None
484=============================================================================*/
485void omx_qcelp13_aenc::flush_ack()
486{
487    // Decrement the FLUSH ACK count and notify the waiting recepients
488    pthread_mutex_lock(&m_flush_lock);
489    --m_flush_cnt;
490    if (0 == m_flush_cnt)
491    {
492        event_complete();
493    }
494    DEBUG_PRINT("Rxed FLUSH ACK cnt=%d\n",m_flush_cnt);
495    pthread_mutex_unlock(&m_flush_lock);
496}
497void omx_qcelp13_aenc::frame_done_cb(OMX_BUFFERHEADERTYPE *bufHdr)
498{
499    if (m_cb.FillBufferDone)
500    {
501        PrintFrameHdr(OMX_COMPONENT_GENERATE_FRAME_DONE,bufHdr);
502        m_qcelp13_pb_stats.fbd_cnt++;
503        pthread_mutex_lock(&out_buf_count_lock);
504        nNumOutputBuf--;
505        DEBUG_PRINT("FBD CB:: nNumOutputBuf=%d out_buf_len=%u fbd_cnt=%u\n",\
506                    nNumOutputBuf,
507                    m_qcelp13_pb_stats.tot_out_buf_len,
508                    m_qcelp13_pb_stats.fbd_cnt);
509        m_qcelp13_pb_stats.tot_out_buf_len += bufHdr->nFilledLen;
510        m_qcelp13_pb_stats.tot_pb_time  = bufHdr->nTimeStamp;
511        DEBUG_PRINT("FBD:in_buf_len=%u out_buf_len=%u\n",
512                    m_qcelp13_pb_stats.tot_in_buf_len,
513                    m_qcelp13_pb_stats.tot_out_buf_len);
514
515        pthread_mutex_unlock(&out_buf_count_lock);
516        m_cb.FillBufferDone(&m_cmp, m_app_data, bufHdr);
517    }
518    return;
519}
520
521/*=============================================================================
522FUNCTION:
523  process_out_port_msg
524
525DESCRIPTION:
526  Function for handling all commands from IL client
527IL client commands are processed and callbacks are generated through
528this routine  Audio Command Server provides the thread context for this routine
529
530INPUT/OUTPUT PARAMETERS:
531  [INOUT] client_data
532  [IN] id
533
534RETURN VALUE:
535  None
536
537Dependency:
538  None
539
540SIDE EFFECTS:
541  None
542=============================================================================*/
543void omx_qcelp13_aenc::process_out_port_msg(void *client_data, unsigned char id)
544{
545    unsigned long p1 = 0;                            // Parameter - 1
546    unsigned long p2 = 0;                            // Parameter - 2
547    unsigned char ident = 0;
548    unsigned      qsize     = 0;                 // qsize
549    unsigned      tot_qsize = 0;
550    omx_qcelp13_aenc  *pThis    = (omx_qcelp13_aenc *) client_data;
551    OMX_STATETYPE state;
552
553loopback_out:
554    pthread_mutex_lock(&pThis->m_state_lock);
555    pThis->get_state(&pThis->m_cmp, &state);
556    pthread_mutex_unlock(&pThis->m_state_lock);
557    if ( state == OMX_StateLoaded )
558    {
559        DEBUG_PRINT(" OUT: IN LOADED STATE RETURN\n");
560        return;
561    }
562    pthread_mutex_lock(&pThis->m_outputlock);
563
564    qsize = pThis->m_output_ctrl_cmd_q.m_size;
565    tot_qsize = pThis->m_output_ctrl_cmd_q.m_size;
566    tot_qsize += pThis->m_output_ctrl_fbd_q.m_size;
567    tot_qsize += pThis->m_output_q.m_size;
568
569    if ( 0 == tot_qsize )
570    {
571        pthread_mutex_unlock(&pThis->m_outputlock);
572        DEBUG_DETAIL("OUT-->BREAK FROM LOOP...%d\n",tot_qsize);
573        return;
574    }
575    if ( (state != OMX_StateExecuting) && !qsize )
576    {
577        pthread_mutex_unlock(&pThis->m_outputlock);
578        pthread_mutex_lock(&pThis->m_state_lock);
579        pThis->get_state(&pThis->m_cmp, &state);
580        pthread_mutex_unlock(&pThis->m_state_lock);
581        if ( state == OMX_StateLoaded )
582            return;
583
584        DEBUG_DETAIL("OUT:1.SLEEPING OUT THREAD\n");
585        pthread_mutex_lock(&pThis->m_out_th_lock_1);
586        pThis->is_out_th_sleep = true;
587        pthread_mutex_unlock(&pThis->m_out_th_lock_1);
588        pThis->out_th_goto_sleep();
589
590        /* Get the updated state */
591        pthread_mutex_lock(&pThis->m_state_lock);
592        pThis->get_state(&pThis->m_cmp, &state);
593        pthread_mutex_unlock(&pThis->m_state_lock);
594    }
595
596    if ( ((!pThis->m_output_ctrl_cmd_q.m_size) && !pThis->m_out_bEnabled) )
597    {
598        // case where no port reconfig and nothing in the flush q
599        DEBUG_DETAIL("No flush/port reconfig qsize=%d tot_qsize=%d",\
600            qsize,tot_qsize);
601        pthread_mutex_unlock(&pThis->m_outputlock);
602        pthread_mutex_lock(&pThis->m_state_lock);
603        pThis->get_state(&pThis->m_cmp, &state);
604        pthread_mutex_unlock(&pThis->m_state_lock);
605        if ( state == OMX_StateLoaded )
606            return;
607
608        if(pThis->m_output_ctrl_cmd_q.m_size || !(pThis->bFlushinprogress))
609        {
610            DEBUG_PRINT("OUT:2. SLEEPING OUT THREAD \n");
611            pthread_mutex_lock(&pThis->m_out_th_lock_1);
612            pThis->is_out_th_sleep = true;
613            pthread_mutex_unlock(&pThis->m_out_th_lock_1);
614            pThis->out_th_goto_sleep();
615        }
616        /* Get the updated state */
617        pthread_mutex_lock(&pThis->m_state_lock);
618        pThis->get_state(&pThis->m_cmp, &state);
619        pthread_mutex_unlock(&pThis->m_state_lock);
620    }
621    qsize = pThis->m_output_ctrl_cmd_q.m_size;
622    tot_qsize = pThis->m_output_ctrl_cmd_q.m_size;
623    tot_qsize += pThis->m_output_ctrl_fbd_q.m_size;
624    tot_qsize += pThis->m_output_q.m_size;
625    pthread_mutex_lock(&pThis->m_state_lock);
626    pThis->get_state(&pThis->m_cmp, &state);
627    pthread_mutex_unlock(&pThis->m_state_lock);
628    DEBUG_DETAIL("OUT-->QSIZE-flush=%d,fbd=%d QSIZE=%d state=%d\n",\
629        pThis->m_output_ctrl_cmd_q.m_size,
630        pThis->m_output_ctrl_fbd_q.m_size,
631        pThis->m_output_q.m_size,state);
632
633
634    if (qsize)
635    {
636        // process FLUSH message
637        pThis->m_output_ctrl_cmd_q.pop_entry(&p1,&p2,&ident);
638    } else if ( (qsize = pThis->m_output_ctrl_fbd_q.m_size) &&
639        (pThis->m_out_bEnabled) && (state == OMX_StateExecuting) )
640    {
641        // then process EBD's
642        pThis->m_output_ctrl_fbd_q.pop_entry(&p1,&p2,&ident);
643    } else if ( (qsize = pThis->m_output_q.m_size) &&
644        (pThis->m_out_bEnabled) && (state == OMX_StateExecuting) )
645    {
646        // if no FLUSH and FBD's then process FTB's
647        pThis->m_output_q.pop_entry(&p1,&p2,&ident);
648    } else if ( state == OMX_StateLoaded )
649    {
650        pthread_mutex_unlock(&pThis->m_outputlock);
651        DEBUG_PRINT("IN: ***in OMX_StateLoaded so exiting\n");
652        return ;
653    } else
654    {
655        qsize = 0;
656        DEBUG_PRINT("OUT--> Empty Queue state=%d %d %d %d\n",state,
657                     pThis->m_output_ctrl_cmd_q.m_size,
658		     pThis->m_output_ctrl_fbd_q.m_size,
659                     pThis->m_output_q.m_size);
660
661        if(state == OMX_StatePause)
662        {
663            DEBUG_DETAIL("OUT: SLEEPING AGAIN OUT THREAD\n");
664            pthread_mutex_lock(&pThis->m_out_th_lock_1);
665            pThis->is_out_th_sleep = true;
666            pthread_mutex_unlock(&pThis->m_out_th_lock_1);
667            pthread_mutex_unlock(&pThis->m_outputlock);
668            pThis->out_th_goto_sleep();
669            goto loopback_out;
670        }
671    }
672    pthread_mutex_unlock(&pThis->m_outputlock);
673
674    if ( qsize > 0 )
675    {
676        id = ident;
677        ident = 0;
678        DEBUG_DETAIL("OUT->state[%d]ident[%d]flushq[%d]fbd[%d]dataq[%d]\n",\
679            pThis->m_state,
680            ident,
681            pThis->m_output_ctrl_cmd_q.m_size,
682            pThis->m_output_ctrl_fbd_q.m_size,
683            pThis->m_output_q.m_size);
684
685        if ( OMX_COMPONENT_GENERATE_FRAME_DONE == id )
686        {
687            pThis->frame_done_cb((OMX_BUFFERHEADERTYPE *)p2);
688        } else if ( OMX_COMPONENT_GENERATE_FTB == id )
689        {
690            pThis->fill_this_buffer_proxy((OMX_HANDLETYPE)p1,
691                (OMX_BUFFERHEADERTYPE *)p2);
692        } else if ( OMX_COMPONENT_GENERATE_EOS == id )
693        {
694            pThis->m_cb.EventHandler(&pThis->m_cmp,
695                pThis->m_app_data,
696                OMX_EventBufferFlag,
697                1, 1, NULL );
698
699        }
700        else if(id == OMX_COMPONENT_RESUME)
701        {
702             DEBUG_PRINT("RESUMED...\n");
703        }
704        else if(id == OMX_COMPONENT_GENERATE_COMMAND)
705        {
706            // Execute FLUSH command
707            if ( OMX_CommandFlush == p1 )
708            {
709                DEBUG_DETAIL("Executing FLUSH command on Output port\n");
710                pThis->execute_output_omx_flush();
711            } else
712            {
713                DEBUG_DETAIL("Invalid command[%lu]\n",p1);
714            }
715        } else
716        {
717            DEBUG_PRINT_ERROR("ERROR:OUT-->Invalid Id[%d]\n",id);
718        }
719    } else
720    {
721        DEBUG_DETAIL("ERROR: OUT--> Empty OUTPUTQ\n");
722    }
723
724    return;
725}
726
727/*=============================================================================
728FUNCTION:
729  process_command_msg
730
731DESCRIPTION:
732
733
734INPUT/OUTPUT PARAMETERS:
735  [INOUT] client_data
736  [IN] id
737
738RETURN VALUE:
739  None
740
741Dependency:
742  None
743
744SIDE EFFECTS:
745  None
746=============================================================================*/
747void omx_qcelp13_aenc::process_command_msg(void *client_data, unsigned char id)
748{
749    unsigned long p1 = 0;                             // Parameter - 1
750    unsigned long p2 = 0;                             // Parameter - 2
751    unsigned char ident = 0;
752    unsigned     qsize  = 0;
753    omx_qcelp13_aenc *pThis = (omx_qcelp13_aenc*)client_data;
754    pthread_mutex_lock(&pThis->m_commandlock);
755
756    qsize = pThis->m_command_q.m_size;
757    DEBUG_DETAIL("CMD-->QSIZE=%d state=%d\n",pThis->m_command_q.m_size,
758                 pThis->m_state);
759
760    if (!qsize)
761    {
762        DEBUG_DETAIL("CMD-->BREAKING FROM LOOP\n");
763        pthread_mutex_unlock(&pThis->m_commandlock);
764        return;
765    } else
766    {
767        pThis->m_command_q.pop_entry(&p1,&p2,&ident);
768    }
769    pthread_mutex_unlock(&pThis->m_commandlock);
770
771    id = ident;
772    DEBUG_DETAIL("CMD->state[%d]id[%d]cmdq[%d]n",\
773                 pThis->m_state,ident, \
774                 pThis->m_command_q.m_size);
775
776    if (OMX_COMPONENT_GENERATE_EVENT == id)
777    {
778        if (pThis->m_cb.EventHandler)
779        {
780            if (OMX_CommandStateSet == p1)
781            {
782                pthread_mutex_lock(&pThis->m_state_lock);
783                pThis->m_state = (OMX_STATETYPE) p2;
784                pthread_mutex_unlock(&pThis->m_state_lock);
785                DEBUG_PRINT("CMD:Process->state set to %d \n", \
786                            pThis->m_state);
787
788                if (pThis->m_state == OMX_StateExecuting ||
789                    pThis->m_state == OMX_StateLoaded)
790                {
791
792                    pthread_mutex_lock(&pThis->m_in_th_lock_1);
793                    if (pThis->is_in_th_sleep)
794                    {
795                        pThis->is_in_th_sleep = false;
796                        DEBUG_DETAIL("CMD:WAKING UP IN THREADS\n");
797                        pThis->in_th_wakeup();
798                    }
799                    pthread_mutex_unlock(&pThis->m_in_th_lock_1);
800
801                    pthread_mutex_lock(&pThis->m_out_th_lock_1);
802                    if (pThis->is_out_th_sleep)
803                    {
804                        DEBUG_DETAIL("CMD:WAKING UP OUT THREADS\n");
805                        pThis->is_out_th_sleep = false;
806                        pThis->out_th_wakeup();
807                    }
808                    pthread_mutex_unlock(&pThis->m_out_th_lock_1);
809                }
810            }
811            if (OMX_StateInvalid == pThis->m_state)
812            {
813                pThis->m_cb.EventHandler(&pThis->m_cmp,
814                                         pThis->m_app_data,
815                                         OMX_EventError,
816                                         OMX_ErrorInvalidState,
817                                         0, NULL );
818            } else if ((signed)p2 == OMX_ErrorPortUnpopulated)
819            {
820                pThis->m_cb.EventHandler(&pThis->m_cmp,
821                                         pThis->m_app_data,
822                                         OMX_EventError,
823                                         (OMX_U32)p2,
824                                         0,
825                                         0 );
826            } else
827            {
828                pThis->m_cb.EventHandler(&pThis->m_cmp,
829                                         pThis->m_app_data,
830                                         OMX_EventCmdComplete,
831                                         (OMX_U32)p1, (OMX_U32)p2, NULL );
832            }
833        } else
834        {
835            DEBUG_PRINT_ERROR("ERROR:CMD-->EventHandler NULL \n");
836        }
837    } else if (OMX_COMPONENT_GENERATE_COMMAND == id)
838    {
839        pThis->send_command_proxy(&pThis->m_cmp,
840                                  (OMX_COMMANDTYPE)p1,
841                                  (OMX_U32)p2,(OMX_PTR)NULL);
842    } else if (OMX_COMPONENT_PORTSETTINGS_CHANGED == id)
843    {
844        DEBUG_DETAIL("CMD-->RXED PORTSETTINGS_CHANGED");
845        pThis->m_cb.EventHandler(&pThis->m_cmp,
846                                 pThis->m_app_data,
847                                 OMX_EventPortSettingsChanged,
848                                 1, 1, NULL );
849    }
850    else
851    {
852       DEBUG_PRINT_ERROR("CMD->state[%d]id[%d]\n",pThis->m_state,ident);
853    }
854    return;
855}
856
857/*=============================================================================
858FUNCTION:
859  process_in_port_msg
860
861DESCRIPTION:
862
863
864INPUT/OUTPUT PARAMETERS:
865  [INOUT] client_data
866  [IN] id
867
868RETURN VALUE:
869  None
870
871Dependency:
872  None
873
874SIDE EFFECTS:
875  None
876=============================================================================*/
877void omx_qcelp13_aenc::process_in_port_msg(void *client_data, unsigned char id)
878{
879    unsigned long p1 = 0;                            // Parameter - 1
880    unsigned long p2 = 0;                            // Parameter - 2
881    unsigned char ident = 0;
882    unsigned      qsize     = 0;
883    unsigned      tot_qsize = 0;
884    omx_qcelp13_aenc  *pThis    = (omx_qcelp13_aenc *) client_data;
885    OMX_STATETYPE state;
886
887    if (!pThis)
888    {
889        DEBUG_PRINT_ERROR("ERROR:IN--> Invalid Obj \n");
890        return;
891    }
892loopback_in:
893    pthread_mutex_lock(&pThis->m_state_lock);
894    pThis->get_state(&pThis->m_cmp, &state);
895    pthread_mutex_unlock(&pThis->m_state_lock);
896    if ( state == OMX_StateLoaded )
897    {
898        DEBUG_PRINT(" IN: IN LOADED STATE RETURN\n");
899        return;
900    }
901    // Protect the shared queue data structure
902    pthread_mutex_lock(&pThis->m_lock);
903
904    qsize = pThis->m_input_ctrl_cmd_q.m_size;
905    tot_qsize = qsize;
906    tot_qsize += pThis->m_input_ctrl_ebd_q.m_size;
907    tot_qsize += pThis->m_input_q.m_size;
908
909    if ( 0 == tot_qsize )
910    {
911        DEBUG_DETAIL("IN-->BREAKING FROM IN LOOP");
912        pthread_mutex_unlock(&pThis->m_lock);
913        return;
914    }
915
916    if ( (state != OMX_StateExecuting) && ! (pThis->m_input_ctrl_cmd_q.m_size))
917    {
918        pthread_mutex_unlock(&pThis->m_lock);
919        DEBUG_DETAIL("SLEEPING IN THREAD\n");
920        pthread_mutex_lock(&pThis->m_in_th_lock_1);
921        pThis->is_in_th_sleep = true;
922        pthread_mutex_unlock(&pThis->m_in_th_lock_1);
923        pThis->in_th_goto_sleep();
924
925        /* Get the updated state */
926        pthread_mutex_lock(&pThis->m_state_lock);
927        pThis->get_state(&pThis->m_cmp, &state);
928        pthread_mutex_unlock(&pThis->m_state_lock);
929    }
930    else if ((state == OMX_StatePause))
931    {
932        if(!(pThis->m_input_ctrl_cmd_q.m_size))
933        {
934           pthread_mutex_unlock(&pThis->m_lock);
935
936           DEBUG_DETAIL("IN: SLEEPING IN THREAD\n");
937           pthread_mutex_lock(&pThis->m_in_th_lock_1);
938           pThis->is_in_th_sleep = true;
939           pthread_mutex_unlock(&pThis->m_in_th_lock_1);
940           pThis->in_th_goto_sleep();
941
942           pthread_mutex_lock(&pThis->m_state_lock);
943           pThis->get_state(&pThis->m_cmp, &state);
944           pthread_mutex_unlock(&pThis->m_state_lock);
945        }
946    }
947
948    qsize = pThis->m_input_ctrl_cmd_q.m_size;
949    tot_qsize = qsize;
950    tot_qsize += pThis->m_input_ctrl_ebd_q.m_size;
951    tot_qsize += pThis->m_input_q.m_size;
952
953    DEBUG_DETAIL("Input-->QSIZE-flush=%d,ebd=%d QSIZE=%d state=%d\n",\
954        pThis->m_input_ctrl_cmd_q.m_size,
955        pThis->m_input_ctrl_ebd_q.m_size,
956        pThis->m_input_q.m_size, state);
957
958
959    if ( qsize )
960    {
961        // process FLUSH message
962        pThis->m_input_ctrl_cmd_q.pop_entry(&p1,&p2,&ident);
963    } else if ( (qsize = pThis->m_input_ctrl_ebd_q.m_size) &&
964        (state == OMX_StateExecuting) )
965    {
966        // then process EBD's
967        pThis->m_input_ctrl_ebd_q.pop_entry(&p1,&p2,&ident);
968    } else if ((qsize = pThis->m_input_q.m_size) &&
969               (state == OMX_StateExecuting))
970    {
971        // if no FLUSH and EBD's then process ETB's
972        pThis->m_input_q.pop_entry(&p1, &p2, &ident);
973    } else if ( state == OMX_StateLoaded )
974    {
975        pthread_mutex_unlock(&pThis->m_lock);
976        DEBUG_PRINT("IN: ***in OMX_StateLoaded so exiting\n");
977        return ;
978    } else
979    {
980        qsize = 0;
981        DEBUG_PRINT("IN-->state[%d]cmdq[%d]ebdq[%d]in[%d]\n",\
982                             state,pThis->m_input_ctrl_cmd_q.m_size,
983                             pThis->m_input_ctrl_ebd_q.m_size,
984				pThis->m_input_q.m_size);
985
986        if(state == OMX_StatePause)
987        {
988            DEBUG_DETAIL("IN: SLEEPING AGAIN IN THREAD\n");
989            pthread_mutex_lock(&pThis->m_in_th_lock_1);
990            pThis->is_in_th_sleep = true;
991            pthread_mutex_unlock(&pThis->m_in_th_lock_1);
992            pthread_mutex_unlock(&pThis->m_lock);
993            pThis->in_th_goto_sleep();
994            goto loopback_in;
995        }
996    }
997    pthread_mutex_unlock(&pThis->m_lock);
998
999    if ( qsize > 0 )
1000    {
1001        id = ident;
1002        DEBUG_DETAIL("Input->state[%d]id[%d]flushq[%d]ebdq[%d]dataq[%d]\n",\
1003            pThis->m_state,
1004            ident,
1005            pThis->m_input_ctrl_cmd_q.m_size,
1006            pThis->m_input_ctrl_ebd_q.m_size,
1007            pThis->m_input_q.m_size);
1008        if ( OMX_COMPONENT_GENERATE_BUFFER_DONE == id )
1009        {
1010            pThis->buffer_done_cb((OMX_BUFFERHEADERTYPE *)p2);
1011        }
1012        else if(id == OMX_COMPONENT_GENERATE_EOS)
1013        {
1014            pThis->m_cb.EventHandler(&pThis->m_cmp, pThis->m_app_data,
1015                OMX_EventBufferFlag, 0, 1, NULL );
1016        } else if ( OMX_COMPONENT_GENERATE_ETB == id )
1017        {
1018            pThis->empty_this_buffer_proxy((OMX_HANDLETYPE)p1,
1019                (OMX_BUFFERHEADERTYPE *)p2);
1020        } else if ( OMX_COMPONENT_GENERATE_COMMAND == id )
1021        {
1022            // Execute FLUSH command
1023            if ( OMX_CommandFlush == p1 )
1024            {
1025                DEBUG_DETAIL(" Executing FLUSH command on Input port\n");
1026                pThis->execute_input_omx_flush();
1027            } else
1028            {
1029                DEBUG_DETAIL("Invalid command[%lu]\n",p1);
1030            }
1031        }
1032        else
1033        {
1034            DEBUG_PRINT_ERROR("ERROR:IN-->Invalid Id[%d]\n",id);
1035        }
1036    } else
1037    {
1038        DEBUG_DETAIL("ERROR:IN-->Empty INPUT Q\n");
1039    }
1040    return;
1041}
1042
1043/**
1044 @brief member function for performing component initialization
1045
1046 @param role C string mandating role of this component
1047 @return Error status
1048 */
1049OMX_ERRORTYPE omx_qcelp13_aenc::component_init(OMX_STRING role)
1050{
1051
1052    OMX_ERRORTYPE eRet = OMX_ErrorNone;
1053    m_state                   = OMX_StateLoaded;
1054
1055    /* DSP does not give information about the bitstream
1056    randomly assign the value right now. Query will result in
1057    incorrect param */
1058    memset(&m_qcelp13_param, 0, sizeof(m_qcelp13_param));
1059    m_qcelp13_param.nSize = (OMX_U32)sizeof(m_qcelp13_param);
1060    m_qcelp13_param.nChannels = OMX_QCELP13_DEFAULT_CH_CFG;
1061    //Current DSP does not have config
1062    m_qcelp13_param.eCDMARate = OMX_AUDIO_CDMARateFull;
1063    m_qcelp13_param.nMinBitRate = OMX_QCELP13_DEFAULT_MINRATE;
1064    m_qcelp13_param.nMaxBitRate = OMX_QCELP13_DEFAULT_MAXRATE;
1065    m_volume = OMX_QCELP13_DEFAULT_VOL;             /* Close to unity gain */
1066    memset(&m_qcelp13_pb_stats,0,sizeof(QCELP13_PB_STATS));
1067    memset(&m_pcm_param, 0, sizeof(m_pcm_param));
1068    m_pcm_param.nSize = (OMX_U32)sizeof(m_pcm_param);
1069    m_pcm_param.nChannels = OMX_QCELP13_DEFAULT_CH_CFG;
1070    m_pcm_param.nSamplingRate = OMX_QCELP13_DEFAULT_SF;
1071    nTimestamp = 0;
1072
1073
1074    nNumInputBuf = 0;
1075    nNumOutputBuf = 0;
1076    m_ipc_to_in_th = NULL;  // Command server instance
1077    m_ipc_to_out_th = NULL;  // Client server instance
1078    m_ipc_to_cmd_th = NULL;  // command instance
1079    m_is_out_th_sleep = 0;
1080    m_is_in_th_sleep = 0;
1081    is_out_th_sleep= false;
1082
1083    is_in_th_sleep=false;
1084
1085    memset(&m_priority_mgm, 0, sizeof(m_priority_mgm));
1086    m_priority_mgm.nGroupID =0;
1087    m_priority_mgm.nGroupPriority=0;
1088
1089    memset(&m_buffer_supplier, 0, sizeof(m_buffer_supplier));
1090    m_buffer_supplier.nPortIndex=OMX_BufferSupplyUnspecified;
1091
1092    DEBUG_PRINT_ERROR(" component init: role = %s\n",role);
1093
1094    DEBUG_PRINT(" component init: role = %s\n",role);
1095    component_Role.nVersion.nVersion = OMX_SPEC_VERSION;
1096    if (!strcmp(role,"OMX.qcom.audio.encoder.qcelp13"))
1097    {
1098        pcm_input = 1;
1099        component_Role.nSize = (OMX_U32)sizeof(role);
1100        strlcpy((char *)component_Role.cRole, (const char*)role,
1101		sizeof(component_Role.cRole));
1102        DEBUG_PRINT("\ncomponent_init: Component %s LOADED \n", role);
1103    } else if (!strcmp(role,"OMX.qcom.audio.encoder.tunneled.qcelp13"))
1104    {
1105        pcm_input = 0;
1106        component_Role.nSize = (OMX_U32)sizeof(role);
1107        strlcpy((char *)component_Role.cRole, (const char*)role,
1108		sizeof(component_Role.cRole));
1109        DEBUG_PRINT("\ncomponent_init: Component %s LOADED \n", role);
1110    } else
1111    {
1112        component_Role.nSize = (OMX_U32)sizeof("\0");
1113        strlcpy((char *)component_Role.cRole, (const char*)"\0",
1114		sizeof(component_Role.cRole));
1115        DEBUG_PRINT("\ncomponent_init: Component %s LOADED is invalid\n", role);
1116    }
1117    if(pcm_input)
1118    {
1119
1120
1121        m_tmp_meta_buf = (OMX_U8*) malloc(sizeof(OMX_U8) *
1122                         (OMX_CORE_INPUT_BUFFER_SIZE + sizeof(META_IN)));
1123
1124        if (m_tmp_meta_buf == NULL){
1125            DEBUG_PRINT_ERROR("Mem alloc failed for in meta buf\n");
1126                return OMX_ErrorInsufficientResources;
1127            }
1128    }
1129    m_tmp_out_meta_buf =
1130		(OMX_U8*)malloc(sizeof(OMX_U8)*OMX_QCELP13_OUTPUT_BUFFER_SIZE);
1131        if ( m_tmp_out_meta_buf == NULL ) {
1132            DEBUG_PRINT_ERROR("Mem alloc failed for out meta buf\n");
1133                return OMX_ErrorInsufficientResources;
1134            }
1135
1136    if(0 == pcm_input)
1137    {
1138        m_drv_fd = open("/dev/msm_qcelp_in",O_RDONLY);
1139    DEBUG_PRINT("Driver in Tunnel mode open\n");
1140    }
1141    else
1142    {
1143        m_drv_fd = open("/dev/msm_qcelp_in",O_RDWR);
1144    DEBUG_PRINT("Driver in Non Tunnel mode open\n");
1145    }
1146    if (m_drv_fd < 0)
1147    {
1148        DEBUG_PRINT_ERROR("Component_init Open Failed[%d] errno[%d]",\
1149                                      m_drv_fd,errno);
1150
1151        return OMX_ErrorInsufficientResources;
1152    }
1153    if(ioctl(m_drv_fd, AUDIO_GET_SESSION_ID,&m_session_id) == -1)
1154    {
1155        DEBUG_PRINT_ERROR("AUDIO_GET_SESSION_ID FAILED\n");
1156    }
1157    if(pcm_input)
1158    {
1159        if (!m_ipc_to_in_th)
1160        {
1161            m_ipc_to_in_th = omx_qcelp13_thread_create(process_in_port_msg,
1162                this, (char *)"INPUT_THREAD");
1163            if (!m_ipc_to_in_th)
1164            {
1165                DEBUG_PRINT_ERROR("ERROR!!! Failed to start \
1166					Input port thread\n");
1167                return OMX_ErrorInsufficientResources;
1168            }
1169        }
1170    }
1171
1172    if (!m_ipc_to_cmd_th)
1173    {
1174        m_ipc_to_cmd_th = omx_qcelp13_thread_create(process_command_msg,
1175            this, (char *)"CMD_THREAD");
1176        if (!m_ipc_to_cmd_th)
1177        {
1178            DEBUG_PRINT_ERROR("ERROR!!!Failed to start "
1179                              "command message thread\n");
1180            return OMX_ErrorInsufficientResources;
1181        }
1182    }
1183
1184        if (!m_ipc_to_out_th)
1185        {
1186            m_ipc_to_out_th = omx_qcelp13_thread_create(process_out_port_msg,
1187                this, (char *)"OUTPUT_THREAD");
1188            if (!m_ipc_to_out_th)
1189            {
1190                DEBUG_PRINT_ERROR("ERROR!!! Failed to start output "
1191                                  "port thread\n");
1192                return OMX_ErrorInsufficientResources;
1193            }
1194        }
1195    return eRet;
1196}
1197
1198/**
1199
1200 @brief member function to retrieve version of component
1201
1202
1203
1204 @param hComp handle to this component instance
1205 @param componentName name of component
1206 @param componentVersion  pointer to memory space which stores the
1207       version number
1208 @param specVersion pointer to memory sapce which stores version of
1209        openMax specification
1210 @param componentUUID
1211 @return Error status
1212 */
1213OMX_ERRORTYPE  omx_qcelp13_aenc::get_component_version
1214(
1215    OMX_IN OMX_HANDLETYPE               hComp,
1216    OMX_OUT OMX_STRING          componentName,
1217    OMX_OUT OMX_VERSIONTYPE* componentVersion,
1218    OMX_OUT OMX_VERSIONTYPE*      specVersion,
1219    OMX_OUT OMX_UUIDTYPE*       componentUUID)
1220{
1221    if((hComp == NULL) || (componentName == NULL) ||
1222        (specVersion == NULL) || (componentUUID == NULL))
1223    {
1224        componentVersion = NULL;
1225        DEBUG_PRINT_ERROR("Returning OMX_ErrorBadParameter\n");
1226        return OMX_ErrorBadParameter;
1227    }
1228    if (m_state == OMX_StateInvalid)
1229    {
1230        DEBUG_PRINT_ERROR("Get Comp Version in Invalid State\n");
1231        return OMX_ErrorInvalidState;
1232    }
1233    componentVersion->nVersion = OMX_SPEC_VERSION;
1234    specVersion->nVersion = OMX_SPEC_VERSION;
1235    return OMX_ErrorNone;
1236}
1237/**
1238  @brief member function handles command from IL client
1239
1240  This function simply queue up commands from IL client.
1241  Commands will be processed in command server thread context later
1242
1243  @param hComp handle to component instance
1244  @param cmd type of command
1245  @param param1 parameters associated with the command type
1246  @param cmdData
1247  @return Error status
1248*/
1249OMX_ERRORTYPE  omx_qcelp13_aenc::send_command(OMX_IN OMX_HANDLETYPE hComp,
1250                                           OMX_IN OMX_COMMANDTYPE  cmd,
1251                                           OMX_IN OMX_U32       param1,
1252                                           OMX_IN OMX_PTR      cmdData)
1253{
1254    int portIndex = (int)param1;
1255
1256    if(hComp == NULL)
1257    {
1258        cmdData = cmdData;
1259        DEBUG_PRINT_ERROR("Returning OMX_ErrorBadParameter\n");
1260        return OMX_ErrorBadParameter;
1261    }
1262    if (OMX_StateInvalid == m_state)
1263    {
1264        return OMX_ErrorInvalidState;
1265    }
1266    if ( (cmd == OMX_CommandFlush) && (portIndex > 1) )
1267    {
1268        return OMX_ErrorBadPortIndex;
1269    }
1270    post_command((unsigned)cmd,(unsigned)param1,OMX_COMPONENT_GENERATE_COMMAND);
1271    DEBUG_PRINT("Send Command : returns with OMX_ErrorNone \n");
1272    DEBUG_PRINT("send_command : recieved state before semwait= %u\n",param1);
1273    sem_wait (&sem_States);
1274    DEBUG_PRINT("send_command : recieved state after semwait\n");
1275    return OMX_ErrorNone;
1276}
1277
1278/**
1279 @brief member function performs actual processing of commands excluding
1280  empty buffer call
1281
1282 @param hComp handle to component
1283 @param cmd command type
1284 @param param1 parameter associated with the command
1285 @param cmdData
1286
1287 @return error status
1288*/
1289OMX_ERRORTYPE  omx_qcelp13_aenc::send_command_proxy(OMX_IN OMX_HANDLETYPE hComp,
1290                                                 OMX_IN OMX_COMMANDTYPE  cmd,
1291                                                 OMX_IN OMX_U32       param1,
1292                                                 OMX_IN OMX_PTR      cmdData)
1293{
1294    OMX_ERRORTYPE eRet = OMX_ErrorNone;
1295    //   Handle only IDLE and executing
1296    OMX_STATETYPE eState = (OMX_STATETYPE) param1;
1297    int bFlag = 1;
1298    nState = eState;
1299
1300    if(hComp == NULL)
1301    {
1302        cmdData = cmdData;
1303        DEBUG_PRINT_ERROR("Returning OMX_ErrorBadParameter\n");
1304        return OMX_ErrorBadParameter;
1305    }
1306    if (OMX_CommandStateSet == cmd)
1307    {
1308        /***************************/
1309        /* Current State is Loaded */
1310        /***************************/
1311        if (OMX_StateLoaded == m_state)
1312        {
1313            if (OMX_StateIdle == eState)
1314            {
1315
1316                 if (allocate_done() ||
1317                        (m_inp_bEnabled == OMX_FALSE
1318                         && m_out_bEnabled == OMX_FALSE))
1319                 {
1320                       DEBUG_PRINT("SCP-->Allocate Done Complete\n");
1321                 }
1322                 else
1323                 {
1324                        DEBUG_PRINT("SCP-->Loaded to Idle-Pending\n");
1325                        BITMASK_SET(&m_flags, OMX_COMPONENT_IDLE_PENDING);
1326                        bFlag = 0;
1327                 }
1328
1329            } else if (eState == OMX_StateLoaded)
1330            {
1331                DEBUG_PRINT("OMXCORE-SM: Loaded-->Loaded\n");
1332                m_cb.EventHandler(&this->m_cmp,
1333                                  this->m_app_data,
1334                                  OMX_EventError,
1335                                  OMX_ErrorSameState,
1336                                  0, NULL );
1337                eRet = OMX_ErrorSameState;
1338            }
1339
1340            else if (eState == OMX_StateWaitForResources)
1341            {
1342                DEBUG_PRINT("OMXCORE-SM: Loaded-->WaitForResources\n");
1343                eRet = OMX_ErrorNone;
1344            }
1345
1346            else if (eState == OMX_StateExecuting)
1347            {
1348                DEBUG_PRINT("OMXCORE-SM: Loaded-->Executing\n");
1349                m_cb.EventHandler(&this->m_cmp,
1350                                  this->m_app_data,
1351                                  OMX_EventError,
1352                                  OMX_ErrorIncorrectStateTransition,
1353                                  0, NULL );
1354                eRet = OMX_ErrorIncorrectStateTransition;
1355            }
1356
1357            else if (eState == OMX_StatePause)
1358            {
1359                DEBUG_PRINT("OMXCORE-SM: Loaded-->Pause\n");
1360                m_cb.EventHandler(&this->m_cmp,
1361                                  this->m_app_data,
1362                                  OMX_EventError,
1363                                  OMX_ErrorIncorrectStateTransition,
1364                                  0, NULL );
1365                eRet = OMX_ErrorIncorrectStateTransition;
1366            }
1367
1368            else if (eState == OMX_StateInvalid)
1369            {
1370                DEBUG_PRINT("OMXCORE-SM: Loaded-->Invalid\n");
1371                m_cb.EventHandler(&this->m_cmp,
1372                                  this->m_app_data,
1373                                  OMX_EventError,
1374                                  OMX_ErrorInvalidState,
1375                                  0, NULL );
1376                m_state = OMX_StateInvalid;
1377                eRet = OMX_ErrorInvalidState;
1378            } else
1379            {
1380                DEBUG_PRINT_ERROR("SCP-->Loaded to Invalid(%d))\n",eState);
1381                eRet = OMX_ErrorBadParameter;
1382            }
1383        }
1384
1385        /***************************/
1386        /* Current State is IDLE */
1387        /***************************/
1388        else if (OMX_StateIdle == m_state)
1389        {
1390            if (OMX_StateLoaded == eState)
1391            {
1392                if (release_done(-1))
1393                {
1394                    if (ioctl(m_drv_fd, AUDIO_STOP, 0) == -1)
1395                    {
1396                        DEBUG_PRINT_ERROR("SCP:Idle->Loaded,\
1397				ioctl stop failed %d\n", errno);
1398                    }
1399
1400                    nTimestamp=0;
1401
1402                    DEBUG_PRINT("SCP-->Idle to Loaded\n");
1403                } else
1404                {
1405                    DEBUG_PRINT("SCP--> Idle to Loaded-Pending\n");
1406                    BITMASK_SET(&m_flags, OMX_COMPONENT_LOADING_PENDING);
1407                    // Skip the event notification
1408                    bFlag = 0;
1409                }
1410            }
1411            else if (OMX_StateExecuting == eState)
1412            {
1413
1414                struct msm_audio_qcelp_enc_config drv_qcelp13_enc_config;
1415                struct msm_audio_stream_config drv_stream_config;
1416                struct msm_audio_buf_cfg buf_cfg;
1417                struct msm_audio_config pcm_cfg;
1418
1419                if(ioctl(m_drv_fd, AUDIO_GET_STREAM_CONFIG, &drv_stream_config)
1420			== -1)
1421                {
1422                    DEBUG_PRINT_ERROR("ioctl AUDIO_GET_STREAM_CONFIG failed, \
1423					errno[%d]\n", errno);
1424                }
1425                if(ioctl(m_drv_fd, AUDIO_SET_STREAM_CONFIG, &drv_stream_config)
1426			== -1)
1427                {
1428                    DEBUG_PRINT_ERROR("ioctl AUDIO_SET_STREAM_CONFIG failed, \
1429					errno[%d]\n", errno);
1430                }
1431
1432                if(ioctl(m_drv_fd, AUDIO_GET_QCELP_ENC_CONFIG,
1433			&drv_qcelp13_enc_config) == -1)
1434                {
1435                    DEBUG_PRINT_ERROR("ioctl AUDIO_GET_QCELP_ENC_CONFIG failed,\
1436					errno[%d]\n", errno);
1437                }
1438        drv_qcelp13_enc_config.min_bit_rate = m_qcelp13_param.nMinBitRate;
1439        drv_qcelp13_enc_config.max_bit_rate = m_qcelp13_param.nMaxBitRate;
1440        if(ioctl(m_drv_fd, AUDIO_SET_QCELP_ENC_CONFIG, &drv_qcelp13_enc_config)
1441		== -1)
1442                {
1443                    DEBUG_PRINT_ERROR("ioctl AUDIO_SET_QCELP_ENC_CONFIG \
1444					failed, errno[%d]\n", errno);
1445                }
1446                if (ioctl(m_drv_fd, AUDIO_GET_BUF_CFG, &buf_cfg) == -1)
1447                {
1448                    DEBUG_PRINT_ERROR("ioctl AUDIO_GET_BUF_CFG, errno[%d]\n",
1449					errno);
1450                }
1451                buf_cfg.meta_info_enable = 1;
1452                buf_cfg.frames_per_buf = NUMOFFRAMES;
1453                if (ioctl(m_drv_fd, AUDIO_SET_BUF_CFG, &buf_cfg) == -1)
1454                {
1455                    DEBUG_PRINT_ERROR("ioctl AUDIO_SET_BUF_CFG, errno[%d]\n",
1456					errno);
1457                }
1458                if(pcm_input)
1459                {
1460                    if (ioctl(m_drv_fd, AUDIO_GET_CONFIG, &pcm_cfg) == -1)
1461                    {
1462                        DEBUG_PRINT_ERROR("ioctl AUDIO_GET_CONFIG, errno[%d]\n",
1463						errno);
1464                    }
1465                    pcm_cfg.channel_count = m_pcm_param.nChannels;
1466                    pcm_cfg.sample_rate  =  m_pcm_param.nSamplingRate;
1467                    DEBUG_PRINT("pcm config %u %u\n",m_pcm_param.nChannels,
1468				m_pcm_param.nSamplingRate);
1469
1470                    if (ioctl(m_drv_fd, AUDIO_SET_CONFIG, &pcm_cfg) == -1)
1471                    {
1472                        DEBUG_PRINT_ERROR("ioctl AUDIO_SET_CONFIG, errno[%d]\n",
1473						errno);
1474                    }
1475                }
1476                if(ioctl(m_drv_fd, AUDIO_START, 0) == -1)
1477                {
1478                    DEBUG_PRINT_ERROR("ioctl AUDIO_START failed, errno[%d]\n",
1479					errno);
1480                }
1481                DEBUG_PRINT("SCP-->Idle to Executing\n");
1482                nState = eState;
1483            } else if (eState == OMX_StateIdle)
1484            {
1485                DEBUG_PRINT("OMXCORE-SM: Idle-->Idle\n");
1486                m_cb.EventHandler(&this->m_cmp,
1487                                  this->m_app_data,
1488                                  OMX_EventError,
1489                                  OMX_ErrorSameState,
1490                                  0, NULL );
1491                eRet = OMX_ErrorSameState;
1492            } else if (eState == OMX_StateWaitForResources)
1493            {
1494                DEBUG_PRINT("OMXCORE-SM: Idle-->WaitForResources\n");
1495                this->m_cb.EventHandler(&this->m_cmp, this->m_app_data,
1496                                        OMX_EventError,
1497                                        OMX_ErrorIncorrectStateTransition,
1498                                        0, NULL );
1499                eRet = OMX_ErrorIncorrectStateTransition;
1500            }
1501
1502            else if (eState == OMX_StatePause)
1503            {
1504                DEBUG_PRINT("OMXCORE-SM: Idle-->Pause\n");
1505            }
1506
1507            else if (eState == OMX_StateInvalid)
1508            {
1509                DEBUG_PRINT("OMXCORE-SM: Idle-->Invalid\n");
1510                m_state = OMX_StateInvalid;
1511                this->m_cb.EventHandler(&this->m_cmp,
1512                                        this->m_app_data,
1513                                        OMX_EventError,
1514                                        OMX_ErrorInvalidState,
1515                                        0, NULL );
1516                eRet = OMX_ErrorInvalidState;
1517            } else
1518            {
1519                DEBUG_PRINT_ERROR("SCP--> Idle to %d Not Handled\n",eState);
1520                eRet = OMX_ErrorBadParameter;
1521            }
1522        }
1523
1524        /******************************/
1525        /* Current State is Executing */
1526        /******************************/
1527        else if (OMX_StateExecuting == m_state)
1528        {
1529            if (OMX_StateIdle == eState)
1530            {
1531                DEBUG_PRINT("SCP-->Executing to Idle \n");
1532                if(pcm_input)
1533                    execute_omx_flush(-1,false);
1534                else
1535                    execute_omx_flush(1,false);
1536
1537
1538            } else if (OMX_StatePause == eState)
1539            {
1540                DEBUG_DETAIL("*************************\n");
1541                DEBUG_PRINT("SCP-->RXED PAUSE STATE\n");
1542                DEBUG_DETAIL("*************************\n");
1543                //ioctl(m_drv_fd, AUDIO_PAUSE, 0);
1544            } else if (eState == OMX_StateLoaded)
1545            {
1546                DEBUG_PRINT("\n OMXCORE-SM: Executing --> Loaded \n");
1547                this->m_cb.EventHandler(&this->m_cmp, this->m_app_data,
1548                                        OMX_EventError,
1549                                        OMX_ErrorIncorrectStateTransition,
1550                                        0, NULL );
1551                eRet = OMX_ErrorIncorrectStateTransition;
1552            } else if (eState == OMX_StateWaitForResources)
1553            {
1554                DEBUG_PRINT("\n OMXCORE-SM: Executing --> WaitForResources \n");
1555                this->m_cb.EventHandler(&this->m_cmp, this->m_app_data,
1556                                        OMX_EventError,
1557                                        OMX_ErrorIncorrectStateTransition,
1558                                        0, NULL );
1559                eRet = OMX_ErrorIncorrectStateTransition;
1560            } else if (eState == OMX_StateExecuting)
1561            {
1562                DEBUG_PRINT("\n OMXCORE-SM: Executing --> Executing \n");
1563                this->m_cb.EventHandler(&this->m_cmp, this->m_app_data,
1564                                        OMX_EventError, OMX_ErrorSameState,
1565                                        0, NULL );
1566                eRet = OMX_ErrorSameState;
1567            } else if (eState == OMX_StateInvalid)
1568            {
1569                DEBUG_PRINT("\n OMXCORE-SM: Executing --> Invalid \n");
1570                m_state = OMX_StateInvalid;
1571                this->m_cb.EventHandler(&this->m_cmp, this->m_app_data,
1572                                        OMX_EventError, OMX_ErrorInvalidState,
1573                                        0, NULL );
1574                eRet = OMX_ErrorInvalidState;
1575            } else
1576            {
1577                DEBUG_PRINT_ERROR("SCP--> Executing to %d Not Handled\n",
1578				eState);
1579                eRet = OMX_ErrorBadParameter;
1580            }
1581        }
1582        /***************************/
1583        /* Current State is Pause  */
1584        /***************************/
1585        else if (OMX_StatePause == m_state)
1586        {
1587            if( (eState == OMX_StateExecuting || eState == OMX_StateIdle) )
1588            {
1589                pthread_mutex_lock(&m_out_th_lock_1);
1590                if(is_out_th_sleep)
1591                {
1592                    DEBUG_DETAIL("PE: WAKING UP OUT THREAD\n");
1593                    is_out_th_sleep = false;
1594                    out_th_wakeup();
1595                }
1596                pthread_mutex_unlock(&m_out_th_lock_1);
1597            }
1598            if ( OMX_StateExecuting == eState )
1599            {
1600                nState = eState;
1601            } else if ( OMX_StateIdle == eState )
1602            {
1603                DEBUG_PRINT("SCP-->Paused to Idle \n");
1604                DEBUG_PRINT ("\n Internal flush issued");
1605                pthread_mutex_lock(&m_flush_lock);
1606                m_flush_cnt = 2;
1607                pthread_mutex_unlock(&m_flush_lock);
1608                if(pcm_input)
1609                    execute_omx_flush(-1,false);
1610                else
1611                    execute_omx_flush(1,false);
1612
1613            } else if ( eState == OMX_StateLoaded )
1614            {
1615                DEBUG_PRINT("\n Pause --> loaded \n");
1616                this->m_cb.EventHandler(&this->m_cmp, this->m_app_data,
1617                                        OMX_EventError,
1618					OMX_ErrorIncorrectStateTransition,
1619                                        0, NULL );
1620                eRet = OMX_ErrorIncorrectStateTransition;
1621            } else if (eState == OMX_StateWaitForResources)
1622            {
1623                DEBUG_PRINT("\n Pause --> WaitForResources \n");
1624                this->m_cb.EventHandler(&this->m_cmp, this->m_app_data,
1625                                        OMX_EventError,
1626					OMX_ErrorIncorrectStateTransition,
1627                                        0, NULL );
1628                eRet = OMX_ErrorIncorrectStateTransition;
1629            } else if (eState == OMX_StatePause)
1630            {
1631                DEBUG_PRINT("\n Pause --> Pause \n");
1632                this->m_cb.EventHandler(&this->m_cmp, this->m_app_data,
1633                                        OMX_EventError, OMX_ErrorSameState,
1634                                        0, NULL );
1635                eRet = OMX_ErrorSameState;
1636            } else if (eState == OMX_StateInvalid)
1637            {
1638                DEBUG_PRINT("\n Pause --> Invalid \n");
1639                m_state = OMX_StateInvalid;
1640                this->m_cb.EventHandler(&this->m_cmp, this->m_app_data,
1641                                        OMX_EventError, OMX_ErrorInvalidState,
1642                                        0, NULL );
1643                eRet = OMX_ErrorInvalidState;
1644            } else
1645            {
1646                DEBUG_PRINT("SCP-->Paused to %d Not Handled\n",eState);
1647                eRet = OMX_ErrorBadParameter;
1648            }
1649        }
1650        /**************************************/
1651        /* Current State is WaitForResources  */
1652        /**************************************/
1653        else if (m_state == OMX_StateWaitForResources)
1654        {
1655            if (eState == OMX_StateLoaded)
1656            {
1657                DEBUG_PRINT("OMXCORE-SM: WaitForResources-->Loaded\n");
1658            } else if (eState == OMX_StateWaitForResources)
1659            {
1660                DEBUG_PRINT("OMXCORE-SM: \
1661				WaitForResources-->WaitForResources\n");
1662                this->m_cb.EventHandler(&this->m_cmp, this->m_app_data,
1663                                        OMX_EventError, OMX_ErrorSameState,
1664                                        0, NULL );
1665                eRet = OMX_ErrorSameState;
1666            } else if (eState == OMX_StateExecuting)
1667            {
1668                DEBUG_PRINT("OMXCORE-SM: WaitForResources-->Executing\n");
1669                this->m_cb.EventHandler(&this->m_cmp, this->m_app_data,
1670                                        OMX_EventError,
1671                                        OMX_ErrorIncorrectStateTransition,
1672                                        0, NULL );
1673                eRet = OMX_ErrorIncorrectStateTransition;
1674            } else if (eState == OMX_StatePause)
1675            {
1676                DEBUG_PRINT("OMXCORE-SM: WaitForResources-->Pause\n");
1677                this->m_cb.EventHandler(&this->m_cmp, this->m_app_data,
1678                                        OMX_EventError,
1679                                        OMX_ErrorIncorrectStateTransition,
1680                                        0, NULL );
1681                eRet = OMX_ErrorIncorrectStateTransition;
1682            } else if (eState == OMX_StateInvalid)
1683            {
1684                DEBUG_PRINT("OMXCORE-SM: WaitForResources-->Invalid\n");
1685                m_state = OMX_StateInvalid;
1686                this->m_cb.EventHandler(&this->m_cmp, this->m_app_data,
1687                                        OMX_EventError,
1688                                        OMX_ErrorInvalidState,
1689                                        0, NULL );
1690                eRet = OMX_ErrorInvalidState;
1691            } else
1692            {
1693                DEBUG_PRINT_ERROR("SCP--> %d to %d(Not Handled)\n",
1694					m_state,eState);
1695                eRet = OMX_ErrorBadParameter;
1696            }
1697        }
1698        /****************************/
1699        /* Current State is Invalid */
1700        /****************************/
1701        else if (m_state == OMX_StateInvalid)
1702        {
1703            if (OMX_StateLoaded == eState || OMX_StateWaitForResources == eState
1704                || OMX_StateIdle == eState || OMX_StateExecuting == eState
1705                || OMX_StatePause == eState || OMX_StateInvalid == eState)
1706            {
1707                DEBUG_PRINT("OMXCORE-SM: Invalid-->Loaded/Idle/Executing"
1708                            "/Pause/Invalid/WaitForResources\n");
1709                m_state = OMX_StateInvalid;
1710                this->m_cb.EventHandler(&this->m_cmp, this->m_app_data,
1711                                        OMX_EventError, OMX_ErrorInvalidState,
1712                                        0, NULL );
1713                eRet = OMX_ErrorInvalidState;
1714            }
1715        } else
1716        {
1717            DEBUG_PRINT_ERROR("OMXCORE-SM: %d --> %d(Not Handled)\n",\
1718                              m_state,eState);
1719            eRet = OMX_ErrorBadParameter;
1720        }
1721    } else if (OMX_CommandFlush == cmd)
1722    {
1723        DEBUG_DETAIL("*************************\n");
1724        DEBUG_PRINT("SCP-->RXED FLUSH COMMAND port=%u\n",param1);
1725        DEBUG_DETAIL("*************************\n");
1726        bFlag = 0;
1727        if ( param1 == OMX_CORE_INPUT_PORT_INDEX ||
1728             param1 == OMX_CORE_OUTPUT_PORT_INDEX ||
1729            (signed)param1 == -1 )
1730        {
1731            execute_omx_flush(param1);
1732        } else
1733        {
1734            eRet = OMX_ErrorBadPortIndex;
1735            m_cb.EventHandler(&m_cmp, m_app_data, OMX_EventError,
1736                OMX_CommandFlush, OMX_ErrorBadPortIndex, NULL );
1737        }
1738    } else if ( cmd == OMX_CommandPortDisable )
1739    {
1740    bFlag = 0;
1741        if ( param1 == OMX_CORE_INPUT_PORT_INDEX || param1 == OMX_ALL )
1742        {
1743            DEBUG_PRINT("SCP: Disabling Input port Indx\n");
1744            m_inp_bEnabled = OMX_FALSE;
1745            if ( (m_state == OMX_StateLoaded || m_state == OMX_StateIdle)
1746                && release_done(0) )
1747            {
1748                DEBUG_PRINT("send_command_proxy:OMX_CommandPortDisable:\
1749                            OMX_CORE_INPUT_PORT_INDEX:release_done \n");
1750                DEBUG_PRINT("************* OMX_CommandPortDisable:\
1751                            m_inp_bEnabled=%d********\n",m_inp_bEnabled);
1752
1753                post_command(OMX_CommandPortDisable,
1754                             OMX_CORE_INPUT_PORT_INDEX,
1755                             OMX_COMPONENT_GENERATE_EVENT);
1756            }
1757
1758            else
1759            {
1760                if (m_state == OMX_StatePause ||m_state == OMX_StateExecuting)
1761                {
1762                    DEBUG_PRINT("SCP: execute_omx_flush in Disable in "\
1763                                " param1=%u m_state=%d \n",param1, m_state);
1764                    execute_omx_flush(param1);
1765                }
1766                DEBUG_PRINT("send_command_proxy:OMX_CommandPortDisable:\
1767                            OMX_CORE_INPUT_PORT_INDEX \n");
1768                BITMASK_SET(&m_flags, OMX_COMPONENT_INPUT_DISABLE_PENDING);
1769                // Skip the event notification
1770
1771            }
1772
1773        }
1774        if (param1 == OMX_CORE_OUTPUT_PORT_INDEX || param1 == OMX_ALL)
1775        {
1776
1777            DEBUG_PRINT("SCP: Disabling Output port Indx\n");
1778            m_out_bEnabled = OMX_FALSE;
1779            if ((m_state == OMX_StateLoaded || m_state == OMX_StateIdle)
1780                && release_done(1))
1781            {
1782                DEBUG_PRINT("send_command_proxy:OMX_CommandPortDisable:\
1783                            OMX_CORE_OUTPUT_PORT_INDEX:release_done \n");
1784                DEBUG_PRINT("************* OMX_CommandPortDisable:\
1785                            m_out_bEnabled=%d********\n",m_inp_bEnabled);
1786
1787                post_command(OMX_CommandPortDisable,
1788                             OMX_CORE_OUTPUT_PORT_INDEX,
1789                             OMX_COMPONENT_GENERATE_EVENT);
1790            } else
1791            {
1792                if (m_state == OMX_StatePause ||m_state == OMX_StateExecuting)
1793                {
1794                    DEBUG_PRINT("SCP: execute_omx_flush in Disable out "\
1795                                "param1=%u m_state=%d \n",param1, m_state);
1796                    execute_omx_flush(param1);
1797                }
1798                BITMASK_SET(&m_flags, OMX_COMPONENT_OUTPUT_DISABLE_PENDING);
1799                // Skip the event notification
1800
1801            }
1802        } else
1803        {
1804            DEBUG_PRINT_ERROR("OMX_CommandPortDisable: disable wrong port ID");
1805        }
1806
1807    } else if (cmd == OMX_CommandPortEnable)
1808    {
1809    bFlag = 0;
1810        if (param1 == OMX_CORE_INPUT_PORT_INDEX  || param1 == OMX_ALL)
1811        {
1812            m_inp_bEnabled = OMX_TRUE;
1813            DEBUG_PRINT("SCP: Enabling Input port Indx\n");
1814            if ((m_state == OMX_StateLoaded
1815                 && !BITMASK_PRESENT(&m_flags,OMX_COMPONENT_IDLE_PENDING))
1816                || (m_state == OMX_StateWaitForResources)
1817                || (m_inp_bPopulated == OMX_TRUE))
1818            {
1819                post_command(OMX_CommandPortEnable,
1820                             OMX_CORE_INPUT_PORT_INDEX,
1821                             OMX_COMPONENT_GENERATE_EVENT);
1822
1823
1824            } else
1825            {
1826                BITMASK_SET(&m_flags, OMX_COMPONENT_INPUT_ENABLE_PENDING);
1827                // Skip the event notification
1828
1829            }
1830        }
1831
1832        if (param1 == OMX_CORE_OUTPUT_PORT_INDEX || param1 == OMX_ALL)
1833        {
1834            DEBUG_PRINT("SCP: Enabling Output port Indx\n");
1835            m_out_bEnabled = OMX_TRUE;
1836            if ((m_state == OMX_StateLoaded
1837                 && !BITMASK_PRESENT(&m_flags,OMX_COMPONENT_IDLE_PENDING))
1838                || (m_state == OMX_StateWaitForResources)
1839                || (m_out_bPopulated == OMX_TRUE))
1840            {
1841                post_command(OMX_CommandPortEnable,
1842                             OMX_CORE_OUTPUT_PORT_INDEX,
1843                             OMX_COMPONENT_GENERATE_EVENT);
1844            } else
1845            {
1846                DEBUG_PRINT("send_command_proxy:OMX_CommandPortEnable:\
1847                            OMX_CORE_OUTPUT_PORT_INDEX:release_done \n");
1848                BITMASK_SET(&m_flags, OMX_COMPONENT_OUTPUT_ENABLE_PENDING);
1849                // Skip the event notification
1850
1851            }
1852            pthread_mutex_lock(&m_in_th_lock_1);
1853            if(is_in_th_sleep)
1854            {
1855                    is_in_th_sleep = false;
1856                    DEBUG_DETAIL("SCP:WAKING UP IN THREADS\n");
1857                    in_th_wakeup();
1858            }
1859            pthread_mutex_unlock(&m_in_th_lock_1);
1860            pthread_mutex_lock(&m_out_th_lock_1);
1861            if (is_out_th_sleep)
1862            {
1863                is_out_th_sleep = false;
1864                DEBUG_PRINT("SCP:WAKING OUT THR, OMX_CommandPortEnable\n");
1865                out_th_wakeup();
1866            }
1867            pthread_mutex_unlock(&m_out_th_lock_1);
1868        } else
1869        {
1870            DEBUG_PRINT_ERROR("OMX_CommandPortEnable: disable wrong port ID");
1871        }
1872
1873    } else
1874    {
1875        DEBUG_PRINT_ERROR("SCP-->ERROR: Invali Command [%d]\n",cmd);
1876        eRet = OMX_ErrorNotImplemented;
1877    }
1878    DEBUG_PRINT("posting sem_States\n");
1879    sem_post (&sem_States);
1880    if (eRet == OMX_ErrorNone && bFlag)
1881    {
1882        post_command(cmd,eState,OMX_COMPONENT_GENERATE_EVENT);
1883    }
1884    return eRet;
1885}
1886
1887/*=============================================================================
1888FUNCTION:
1889  execute_omx_flush
1890
1891DESCRIPTION:
1892  Function that flushes buffers that are pending to be written to driver
1893
1894INPUT/OUTPUT PARAMETERS:
1895  [IN] param1
1896  [IN] cmd_cmpl
1897
1898RETURN VALUE:
1899  true
1900  false
1901
1902Dependency:
1903  None
1904
1905SIDE EFFECTS:
1906  None
1907=============================================================================*/
1908bool omx_qcelp13_aenc::execute_omx_flush(OMX_IN OMX_U32 param1, bool cmd_cmpl)
1909{
1910    bool bRet = true;
1911
1912    DEBUG_PRINT("Execute_omx_flush Port[%u]", param1);
1913    struct timespec abs_timeout;
1914    abs_timeout.tv_sec = 1;
1915    abs_timeout.tv_nsec = 0;
1916
1917    if ((signed)param1 == -1)
1918    {
1919        bFlushinprogress = true;
1920        DEBUG_PRINT("Execute flush for both I/p O/p port\n");
1921        pthread_mutex_lock(&m_flush_lock);
1922        m_flush_cnt = 2;
1923        pthread_mutex_unlock(&m_flush_lock);
1924
1925        // Send Flush commands to input and output threads
1926        post_input(OMX_CommandFlush,
1927                   OMX_CORE_INPUT_PORT_INDEX,OMX_COMPONENT_GENERATE_COMMAND);
1928        post_output(OMX_CommandFlush,
1929                    OMX_CORE_OUTPUT_PORT_INDEX,OMX_COMPONENT_GENERATE_COMMAND);
1930        // Send Flush to the kernel so that the in and out buffers are released
1931        if (ioctl( m_drv_fd, AUDIO_FLUSH, 0) == -1)
1932            DEBUG_PRINT_ERROR("FLush:ioctl flush failed errno=%d\n",errno);
1933        DEBUG_DETAIL("****************************************");
1934        DEBUG_DETAIL("is_in_th_sleep=%d is_out_th_sleep=%d\n",\
1935                     is_in_th_sleep,is_out_th_sleep);
1936        DEBUG_DETAIL("****************************************");
1937
1938        pthread_mutex_lock(&m_in_th_lock_1);
1939        if (is_in_th_sleep)
1940        {
1941            is_in_th_sleep = false;
1942            DEBUG_DETAIL("For FLUSH-->WAKING UP IN THREADS\n");
1943            in_th_wakeup();
1944        }
1945        pthread_mutex_unlock(&m_in_th_lock_1);
1946
1947        pthread_mutex_lock(&m_out_th_lock_1);
1948        if (is_out_th_sleep)
1949        {
1950            is_out_th_sleep = false;
1951            DEBUG_DETAIL("For FLUSH-->WAKING UP OUT THREADS\n");
1952            out_th_wakeup();
1953        }
1954        pthread_mutex_unlock(&m_out_th_lock_1);
1955
1956
1957        // sleep till the FLUSH ACK are done by both the input and
1958        // output threads
1959        DEBUG_DETAIL("WAITING FOR FLUSH ACK's param1=%d",param1);
1960        wait_for_event();
1961
1962        DEBUG_PRINT("RECIEVED BOTH FLUSH ACK's param1=%u cmd_cmpl=%d",\
1963                    param1,cmd_cmpl);
1964
1965        // If not going to idle state, Send FLUSH complete message
1966	// to the Client, now that FLUSH ACK's have been recieved.
1967        if (cmd_cmpl)
1968        {
1969            m_cb.EventHandler(&m_cmp, m_app_data, OMX_EventCmdComplete,
1970                              OMX_CommandFlush, OMX_CORE_INPUT_PORT_INDEX,
1971				NULL );
1972            m_cb.EventHandler(&m_cmp, m_app_data, OMX_EventCmdComplete,
1973                              OMX_CommandFlush, OMX_CORE_OUTPUT_PORT_INDEX,
1974				NULL );
1975            DEBUG_PRINT("Inside FLUSH.. sending FLUSH CMPL\n");
1976        }
1977        bFlushinprogress = false;
1978    }
1979    else if (param1 == OMX_CORE_INPUT_PORT_INDEX)
1980    {
1981        DEBUG_PRINT("Execute FLUSH for I/p port\n");
1982        pthread_mutex_lock(&m_flush_lock);
1983        m_flush_cnt = 1;
1984        pthread_mutex_unlock(&m_flush_lock);
1985        post_input(OMX_CommandFlush,
1986                   OMX_CORE_INPUT_PORT_INDEX,OMX_COMPONENT_GENERATE_COMMAND);
1987        if (ioctl( m_drv_fd, AUDIO_FLUSH, 0) == -1)
1988            DEBUG_PRINT_ERROR("Flush:Input port, ioctl flush failed %d\n",
1989				errno);
1990        DEBUG_DETAIL("****************************************");
1991        DEBUG_DETAIL("is_in_th_sleep=%d is_out_th_sleep=%d\n",\
1992                     is_in_th_sleep,is_out_th_sleep);
1993        DEBUG_DETAIL("****************************************");
1994
1995        if (is_in_th_sleep)
1996        {
1997            pthread_mutex_lock(&m_in_th_lock_1);
1998            is_in_th_sleep = false;
1999            pthread_mutex_unlock(&m_in_th_lock_1);
2000            DEBUG_DETAIL("For FLUSH-->WAKING UP IN THREADS\n");
2001            in_th_wakeup();
2002        }
2003
2004        if (is_out_th_sleep)
2005        {
2006            pthread_mutex_lock(&m_out_th_lock_1);
2007            is_out_th_sleep = false;
2008            pthread_mutex_unlock(&m_out_th_lock_1);
2009            DEBUG_DETAIL("For FLUSH-->WAKING UP OUT THREADS\n");
2010            out_th_wakeup();
2011        }
2012
2013        //sleep till the FLUSH ACK are done by both the input and output threads
2014        DEBUG_DETAIL("Executing FLUSH for I/p port\n");
2015        DEBUG_DETAIL("WAITING FOR FLUSH ACK's param1=%d",param1);
2016        wait_for_event();
2017        DEBUG_DETAIL(" RECIEVED FLUSH ACK FOR I/P PORT param1=%d",param1);
2018
2019        // Send FLUSH complete message to the Client,
2020        // now that FLUSH ACK's have been recieved.
2021        if (cmd_cmpl)
2022        {
2023            m_cb.EventHandler(&m_cmp, m_app_data, OMX_EventCmdComplete,
2024                              OMX_CommandFlush, OMX_CORE_INPUT_PORT_INDEX,
2025				NULL );
2026        }
2027    } else if (OMX_CORE_OUTPUT_PORT_INDEX == param1)
2028    {
2029        DEBUG_PRINT("Executing FLUSH for O/p port\n");
2030        pthread_mutex_lock(&m_flush_lock);
2031        m_flush_cnt = 1;
2032        pthread_mutex_unlock(&m_flush_lock);
2033        DEBUG_DETAIL("Executing FLUSH for O/p port\n");
2034        DEBUG_DETAIL("WAITING FOR FLUSH ACK's param1=%d",param1);
2035        post_output(OMX_CommandFlush,
2036                    OMX_CORE_OUTPUT_PORT_INDEX,OMX_COMPONENT_GENERATE_COMMAND);
2037        if (ioctl( m_drv_fd, AUDIO_FLUSH, 0) ==-1)
2038            DEBUG_PRINT_ERROR("Flush:Output port, ioctl flush failed %d\n",
2039				errno);
2040        DEBUG_DETAIL("****************************************");
2041        DEBUG_DETAIL("is_in_th_sleep=%d is_out_th_sleep=%d\n",\
2042                     is_in_th_sleep,is_out_th_sleep);
2043        DEBUG_DETAIL("****************************************");
2044        if (is_in_th_sleep)
2045        {
2046            pthread_mutex_lock(&m_in_th_lock_1);
2047            is_in_th_sleep = false;
2048            pthread_mutex_unlock(&m_in_th_lock_1);
2049            DEBUG_DETAIL("For FLUSH-->WAKING UP IN THREADS\n");
2050            in_th_wakeup();
2051        }
2052
2053        if (is_out_th_sleep)
2054        {
2055            pthread_mutex_lock(&m_out_th_lock_1);
2056            is_out_th_sleep = false;
2057            pthread_mutex_unlock(&m_out_th_lock_1);
2058            DEBUG_DETAIL("For FLUSH-->WAKING UP OUT THREADS\n");
2059            out_th_wakeup();
2060        }
2061
2062        // sleep till the FLUSH ACK are done by both the input and
2063	// output threads
2064        wait_for_event();
2065        // Send FLUSH complete message to the Client,
2066        // now that FLUSH ACK's have been recieved.
2067        if (cmd_cmpl)
2068        {
2069            m_cb.EventHandler(&m_cmp, m_app_data, OMX_EventCmdComplete,
2070                              OMX_CommandFlush, OMX_CORE_OUTPUT_PORT_INDEX,
2071				NULL );
2072        }
2073        DEBUG_DETAIL("RECIEVED FLUSH ACK FOR O/P PORT param1=%d",param1);
2074    } else
2075    {
2076        DEBUG_PRINT("Invalid Port ID[%u]",param1);
2077    }
2078    return bRet;
2079}
2080
2081/*=============================================================================
2082FUNCTION:
2083  execute_input_omx_flush
2084
2085DESCRIPTION:
2086  Function that flushes buffers that are pending to be written to driver
2087
2088INPUT/OUTPUT PARAMETERS:
2089  None
2090
2091RETURN VALUE:
2092  true
2093  false
2094
2095Dependency:
2096  None
2097
2098SIDE EFFECTS:
2099  None
2100=============================================================================*/
2101bool omx_qcelp13_aenc::execute_input_omx_flush()
2102{
2103    OMX_BUFFERHEADERTYPE *omx_buf;
2104    unsigned long p1 = 0;                            // Parameter - 1
2105    unsigned long p2 = 0;                            // Parameter - 2
2106    unsigned char ident = 0;
2107    unsigned      qsize=0;                       // qsize
2108    unsigned      tot_qsize=0;                   // qsize
2109
2110    DEBUG_PRINT("Execute_omx_flush on input port");
2111
2112    pthread_mutex_lock(&m_lock);
2113    do
2114    {
2115        qsize = m_input_q.m_size;
2116        tot_qsize = qsize;
2117        tot_qsize += m_input_ctrl_ebd_q.m_size;
2118
2119        DEBUG_DETAIL("Input FLUSH-->flushq[%d] ebd[%d]dataq[%d]",\
2120                     m_input_ctrl_cmd_q.m_size,
2121                     m_input_ctrl_ebd_q.m_size,qsize);
2122        if (!tot_qsize)
2123        {
2124            DEBUG_DETAIL("Input-->BREAKING FROM execute_input_flush LOOP");
2125            pthread_mutex_unlock(&m_lock);
2126            break;
2127        }
2128        if (qsize)
2129        {
2130            m_input_q.pop_entry(&p1, &p2, &ident);
2131            if ((ident == OMX_COMPONENT_GENERATE_ETB) ||
2132                (ident == OMX_COMPONENT_GENERATE_BUFFER_DONE))
2133            {
2134                omx_buf = (OMX_BUFFERHEADERTYPE *) p2;
2135                DEBUG_DETAIL("Flush:Input dataq=%p \n", omx_buf);
2136                omx_buf->nFilledLen = 0;
2137                buffer_done_cb((OMX_BUFFERHEADERTYPE *)omx_buf);
2138            }
2139        } else if (m_input_ctrl_ebd_q.m_size)
2140        {
2141            m_input_ctrl_ebd_q.pop_entry(&p1, &p2, &ident);
2142            if (ident == OMX_COMPONENT_GENERATE_BUFFER_DONE)
2143            {
2144                omx_buf = (OMX_BUFFERHEADERTYPE *) p2;
2145                omx_buf->nFilledLen = 0;
2146                DEBUG_DETAIL("Flush:ctrl dataq=%p \n", omx_buf);
2147                buffer_done_cb((OMX_BUFFERHEADERTYPE *)omx_buf);
2148            }
2149        } else
2150        {
2151        }
2152    }while (tot_qsize>0);
2153    DEBUG_DETAIL("*************************\n");
2154    DEBUG_DETAIL("IN-->FLUSHING DONE\n");
2155    DEBUG_DETAIL("*************************\n");
2156    flush_ack();
2157    pthread_mutex_unlock(&m_lock);
2158    return true;
2159}
2160
2161/*=============================================================================
2162FUNCTION:
2163  execute_output_omx_flush
2164
2165DESCRIPTION:
2166  Function that flushes buffers that are pending to be written to driver
2167
2168INPUT/OUTPUT PARAMETERS:
2169  None
2170
2171RETURN VALUE:
2172  true
2173  false
2174
2175Dependency:
2176  None
2177
2178SIDE EFFECTS:
2179  None
2180=============================================================================*/
2181bool omx_qcelp13_aenc::execute_output_omx_flush()
2182{
2183    OMX_BUFFERHEADERTYPE *omx_buf;
2184    unsigned long p1 = 0;                            // Parameter - 1
2185    unsigned long p2 = 0;                            // Parameter - 2
2186    unsigned char ident = 0;
2187    unsigned      qsize=0;                       // qsize
2188    unsigned      tot_qsize=0;                   // qsize
2189
2190    DEBUG_PRINT("Execute_omx_flush on output port");
2191
2192    pthread_mutex_lock(&m_outputlock);
2193    do
2194    {
2195        qsize = m_output_q.m_size;
2196        DEBUG_DETAIL("OUT FLUSH-->flushq[%d] fbd[%d]dataq[%d]",\
2197                     m_output_ctrl_cmd_q.m_size,
2198                     m_output_ctrl_fbd_q.m_size,qsize);
2199        tot_qsize = qsize;
2200        tot_qsize += m_output_ctrl_fbd_q.m_size;
2201        if (!tot_qsize)
2202        {
2203            DEBUG_DETAIL("OUT-->BREAKING FROM execute_input_flush LOOP");
2204            pthread_mutex_unlock(&m_outputlock);
2205            break;
2206        }
2207        if (qsize)
2208        {
2209            m_output_q.pop_entry(&p1,&p2,&ident);
2210            if ( (OMX_COMPONENT_GENERATE_FTB == ident) ||
2211                 (OMX_COMPONENT_GENERATE_FRAME_DONE == ident))
2212            {
2213                omx_buf = (OMX_BUFFERHEADERTYPE *) p2;
2214                DEBUG_DETAIL("Ouput Buf_Addr=%p TS[0x%x] \n",\
2215                             omx_buf,nTimestamp);
2216                omx_buf->nTimeStamp = nTimestamp;
2217                omx_buf->nFilledLen = 0;
2218                frame_done_cb((OMX_BUFFERHEADERTYPE *)omx_buf);
2219                DEBUG_DETAIL("CALLING FBD FROM FLUSH");
2220            }
2221        } else if ((qsize = m_output_ctrl_fbd_q.m_size))
2222        {
2223            m_output_ctrl_fbd_q.pop_entry(&p1, &p2, &ident);
2224            if (OMX_COMPONENT_GENERATE_FRAME_DONE == ident)
2225            {
2226                omx_buf = (OMX_BUFFERHEADERTYPE *) p2;
2227                DEBUG_DETAIL("Ouput Buf_Addr=%p TS[0x%x] \n", \
2228                             omx_buf,nTimestamp);
2229                omx_buf->nTimeStamp = nTimestamp;
2230                omx_buf->nFilledLen = 0;
2231                frame_done_cb((OMX_BUFFERHEADERTYPE *)omx_buf);
2232                DEBUG_DETAIL("CALLING FROM CTRL-FBDQ FROM FLUSH");
2233            }
2234        }
2235    }while (qsize>0);
2236    DEBUG_DETAIL("*************************\n");
2237    DEBUG_DETAIL("OUT-->FLUSHING DONE\n");
2238    DEBUG_DETAIL("*************************\n");
2239    flush_ack();
2240    pthread_mutex_unlock(&m_outputlock);
2241    return true;
2242}
2243
2244/*=============================================================================
2245FUNCTION:
2246  post_input
2247
2248DESCRIPTION:
2249  Function that posts command in the command queue
2250
2251INPUT/OUTPUT PARAMETERS:
2252  [IN] p1
2253  [IN] p2
2254  [IN] id - command ID
2255  [IN] lock - self-locking mode
2256
2257RETURN VALUE:
2258  true
2259  false
2260
2261Dependency:
2262  None
2263
2264SIDE EFFECTS:
2265  None
2266=============================================================================*/
2267bool omx_qcelp13_aenc::post_input(unsigned long p1,
2268                               unsigned long p2,
2269                               unsigned char id)
2270{
2271    bool bRet = false;
2272    pthread_mutex_lock(&m_lock);
2273
2274    if((OMX_COMPONENT_GENERATE_COMMAND == id) || (id == OMX_COMPONENT_SUSPEND))
2275    {
2276        // insert flush message and ebd
2277        m_input_ctrl_cmd_q.insert_entry(p1,p2,id);
2278    } else if ((OMX_COMPONENT_GENERATE_BUFFER_DONE == id))
2279    {
2280        // insert ebd
2281        m_input_ctrl_ebd_q.insert_entry(p1,p2,id);
2282    } else
2283    {
2284        // ETBS in this queue
2285        m_input_q.insert_entry(p1,p2,id);
2286    }
2287
2288    if (m_ipc_to_in_th)
2289    {
2290        bRet = true;
2291        omx_qcelp13_post_msg(m_ipc_to_in_th, id);
2292    }
2293
2294    DEBUG_DETAIL("PostInput-->state[%d]id[%d]flushq[%d]ebdq[%d]dataq[%d] \n",\
2295                 m_state,
2296                 id,
2297                 m_input_ctrl_cmd_q.m_size,
2298                 m_input_ctrl_ebd_q.m_size,
2299                 m_input_q.m_size);
2300
2301    pthread_mutex_unlock(&m_lock);
2302    return bRet;
2303}
2304
2305/*=============================================================================
2306FUNCTION:
2307  post_command
2308
2309DESCRIPTION:
2310  Function that posts command in the command queue
2311
2312INPUT/OUTPUT PARAMETERS:
2313  [IN] p1
2314  [IN] p2
2315  [IN] id - command ID
2316  [IN] lock - self-locking mode
2317
2318RETURN VALUE:
2319  true
2320  false
2321
2322Dependency:
2323  None
2324
2325SIDE EFFECTS:
2326  None
2327=============================================================================*/
2328bool omx_qcelp13_aenc::post_command(unsigned int p1,
2329                                 unsigned int p2,
2330                                 unsigned char id)
2331{
2332    bool bRet  = false;
2333
2334    pthread_mutex_lock(&m_commandlock);
2335
2336    m_command_q.insert_entry(p1,p2,id);
2337
2338    if (m_ipc_to_cmd_th)
2339    {
2340        bRet = true;
2341        omx_qcelp13_post_msg(m_ipc_to_cmd_th, id);
2342    }
2343
2344    DEBUG_DETAIL("PostCmd-->state[%d]id[%d]cmdq[%d]flags[%x]\n",\
2345                 m_state,
2346                 id,
2347                 m_command_q.m_size,
2348                 m_flags >> 3);
2349
2350    pthread_mutex_unlock(&m_commandlock);
2351    return bRet;
2352}
2353
2354/*=============================================================================
2355FUNCTION:
2356  post_output
2357
2358DESCRIPTION:
2359  Function that posts command in the command queue
2360
2361INPUT/OUTPUT PARAMETERS:
2362  [IN] p1
2363  [IN] p2
2364  [IN] id - command ID
2365  [IN] lock - self-locking mode
2366
2367RETURN VALUE:
2368  true
2369  false
2370
2371Dependency:
2372  None
2373
2374SIDE EFFECTS:
2375  None
2376=============================================================================*/
2377bool omx_qcelp13_aenc::post_output(unsigned long p1,
2378                                unsigned long p2,
2379                                unsigned char id)
2380{
2381    bool bRet = false;
2382
2383    pthread_mutex_lock(&m_outputlock);
2384    if((OMX_COMPONENT_GENERATE_COMMAND == id) || (id == OMX_COMPONENT_SUSPEND)
2385        || (id == OMX_COMPONENT_RESUME))
2386    {
2387        // insert flush message and fbd
2388        m_output_ctrl_cmd_q.insert_entry(p1,p2,id);
2389    } else if ( (OMX_COMPONENT_GENERATE_FRAME_DONE == id) )
2390    {
2391        // insert flush message and fbd
2392        m_output_ctrl_fbd_q.insert_entry(p1,p2,id);
2393    } else
2394    {
2395        m_output_q.insert_entry(p1,p2,id);
2396    }
2397    if ( m_ipc_to_out_th )
2398    {
2399        bRet = true;
2400        omx_qcelp13_post_msg(m_ipc_to_out_th, id);
2401    }
2402    DEBUG_DETAIL("PostOutput-->state[%d]id[%d]flushq[%d]ebdq[%d]dataq[%d]\n",\
2403                 m_state,
2404                 id,
2405                 m_output_ctrl_cmd_q.m_size,
2406                 m_output_ctrl_fbd_q.m_size,
2407                 m_output_q.m_size);
2408
2409    pthread_mutex_unlock(&m_outputlock);
2410    return bRet;
2411}
2412/**
2413  @brief member function that return parameters to IL client
2414
2415  @param hComp handle to component instance
2416  @param paramIndex Parameter type
2417  @param paramData pointer to memory space which would hold the
2418        paramter
2419  @return error status
2420*/
2421OMX_ERRORTYPE  omx_qcelp13_aenc::get_parameter(OMX_IN OMX_HANDLETYPE     hComp,
2422                                            OMX_IN OMX_INDEXTYPE paramIndex,
2423                                            OMX_INOUT OMX_PTR     paramData)
2424{
2425    OMX_ERRORTYPE eRet = OMX_ErrorNone;
2426
2427    if(hComp == NULL)
2428    {
2429        DEBUG_PRINT_ERROR("Returning OMX_ErrorBadParameter\n");
2430        return OMX_ErrorBadParameter;
2431    }
2432    if (m_state == OMX_StateInvalid)
2433    {
2434        DEBUG_PRINT_ERROR("Get Param in Invalid State\n");
2435        return OMX_ErrorInvalidState;
2436    }
2437    if (paramData == NULL)
2438    {
2439        DEBUG_PRINT("get_parameter: paramData is NULL\n");
2440        return OMX_ErrorBadParameter;
2441    }
2442
2443    switch ((int)paramIndex)
2444    {
2445        case OMX_IndexParamPortDefinition:
2446            {
2447                OMX_PARAM_PORTDEFINITIONTYPE *portDefn;
2448                portDefn = (OMX_PARAM_PORTDEFINITIONTYPE *) paramData;
2449
2450                DEBUG_PRINT("OMX_IndexParamPortDefinition " \
2451                            "portDefn->nPortIndex = %u\n",
2452				portDefn->nPortIndex);
2453
2454                portDefn->nVersion.nVersion = OMX_SPEC_VERSION;
2455                portDefn->nSize = (OMX_U32)sizeof(portDefn);
2456                portDefn->eDomain    = OMX_PortDomainAudio;
2457
2458                if (0 == portDefn->nPortIndex)
2459                {
2460                    portDefn->eDir       = OMX_DirInput;
2461                    portDefn->bEnabled   = m_inp_bEnabled;
2462                    portDefn->bPopulated = m_inp_bPopulated;
2463                    portDefn->nBufferCountActual = m_inp_act_buf_count;
2464                    portDefn->nBufferCountMin    = OMX_CORE_NUM_INPUT_BUFFERS;
2465                    portDefn->nBufferSize        = input_buffer_size;
2466                    portDefn->format.audio.bFlagErrorConcealment = OMX_TRUE;
2467                    portDefn->format.audio.eEncoding = OMX_AUDIO_CodingPCM;
2468                    portDefn->format.audio.pNativeRender = 0;
2469                } else if (1 == portDefn->nPortIndex)
2470                {
2471                    portDefn->eDir =  OMX_DirOutput;
2472                    portDefn->bEnabled   = m_out_bEnabled;
2473                    portDefn->bPopulated = m_out_bPopulated;
2474                    portDefn->nBufferCountActual = m_out_act_buf_count;
2475                    portDefn->nBufferCountMin    = OMX_CORE_NUM_OUTPUT_BUFFERS;
2476                    portDefn->nBufferSize        = output_buffer_size;
2477                    portDefn->format.audio.bFlagErrorConcealment = OMX_TRUE;
2478                    portDefn->format.audio.eEncoding = OMX_AUDIO_CodingQCELP13;
2479                    portDefn->format.audio.pNativeRender = 0;
2480                } else
2481                {
2482                    portDefn->eDir =  OMX_DirMax;
2483                    DEBUG_PRINT_ERROR("Bad Port idx %d\n",\
2484                                       (int)portDefn->nPortIndex);
2485                    eRet = OMX_ErrorBadPortIndex;
2486                }
2487                break;
2488            }
2489
2490        case OMX_IndexParamAudioInit:
2491            {
2492                OMX_PORT_PARAM_TYPE *portParamType =
2493                (OMX_PORT_PARAM_TYPE *) paramData;
2494                DEBUG_PRINT("OMX_IndexParamAudioInit\n");
2495
2496                portParamType->nVersion.nVersion = OMX_SPEC_VERSION;
2497                portParamType->nSize = (OMX_U32)sizeof(portParamType);
2498                portParamType->nPorts           = 2;
2499                portParamType->nStartPortNumber = 0;
2500                break;
2501            }
2502
2503        case OMX_IndexParamAudioPortFormat:
2504            {
2505                OMX_AUDIO_PARAM_PORTFORMATTYPE *portFormatType =
2506                (OMX_AUDIO_PARAM_PORTFORMATTYPE *) paramData;
2507                DEBUG_PRINT("OMX_IndexParamAudioPortFormat\n");
2508                portFormatType->nVersion.nVersion = OMX_SPEC_VERSION;
2509                portFormatType->nSize = (OMX_U32)sizeof(portFormatType);
2510
2511                if (OMX_CORE_INPUT_PORT_INDEX == portFormatType->nPortIndex)
2512                {
2513
2514                    portFormatType->eEncoding = OMX_AUDIO_CodingPCM;
2515                } else if (OMX_CORE_OUTPUT_PORT_INDEX ==
2516				portFormatType->nPortIndex)
2517                {
2518                    DEBUG_PRINT("get_parameter: OMX_IndexParamAudioFormat: "\
2519                                "%u\n", portFormatType->nIndex);
2520
2521            portFormatType->eEncoding = OMX_AUDIO_CodingQCELP13;
2522                } else
2523                {
2524                    DEBUG_PRINT_ERROR("get_parameter: Bad port index %d\n",
2525                                      (int)portFormatType->nPortIndex);
2526                    eRet = OMX_ErrorBadPortIndex;
2527                }
2528                break;
2529            }
2530
2531        case OMX_IndexParamAudioQcelp13:
2532            {
2533                OMX_AUDIO_PARAM_QCELP13TYPE *qcelp13Param =
2534                (OMX_AUDIO_PARAM_QCELP13TYPE *) paramData;
2535                DEBUG_PRINT("OMX_IndexParamAudioQcelp13\n");
2536                if (OMX_CORE_OUTPUT_PORT_INDEX== qcelp13Param->nPortIndex)
2537                {
2538                    memcpy(qcelp13Param,&m_qcelp13_param,
2539                    sizeof(OMX_AUDIO_PARAM_QCELP13TYPE));
2540                } else
2541                {
2542                    DEBUG_PRINT_ERROR("get_parameter:\
2543					OMX_IndexParamAudioQcelp13 \
2544					OMX_ErrorBadPortIndex %d\n", \
2545                                      (int)qcelp13Param->nPortIndex);
2546                    eRet = OMX_ErrorBadPortIndex;
2547                }
2548                break;
2549            }
2550    case QOMX_IndexParamAudioSessionId:
2551    {
2552       QOMX_AUDIO_STREAM_INFO_DATA *streaminfoparam =
2553               (QOMX_AUDIO_STREAM_INFO_DATA *) paramData;
2554       streaminfoparam->sessionId = (OMX_U8)m_session_id;
2555       break;
2556    }
2557
2558        case OMX_IndexParamAudioPcm:
2559            {
2560                OMX_AUDIO_PARAM_PCMMODETYPE *pcmparam =
2561                (OMX_AUDIO_PARAM_PCMMODETYPE *) paramData;
2562
2563                if (OMX_CORE_INPUT_PORT_INDEX== pcmparam->nPortIndex)
2564                {
2565                    memcpy(pcmparam,&m_pcm_param,\
2566                        sizeof(OMX_AUDIO_PARAM_PCMMODETYPE));
2567                    DEBUG_PRINT("get_parameter: Sampling rate %u",\
2568                                 pcmparam->nSamplingRate);
2569                    DEBUG_PRINT("get_parameter: Number of channels %u",\
2570                                 pcmparam->nChannels);
2571                } else
2572                {
2573                    DEBUG_PRINT_ERROR("get_parameter:OMX_IndexParamAudioPcm "\
2574                                      "OMX_ErrorBadPortIndex %d\n", \
2575                                      (int)pcmparam->nPortIndex);
2576                     eRet = OMX_ErrorBadPortIndex;
2577                }
2578                break;
2579         }
2580        case OMX_IndexParamComponentSuspended:
2581        {
2582            OMX_PARAM_SUSPENSIONTYPE *suspend =
2583				(OMX_PARAM_SUSPENSIONTYPE *) paramData;
2584            DEBUG_PRINT("get_parameter: OMX_IndexParamComponentSuspended %p\n",
2585			suspend);
2586            break;
2587        }
2588        case OMX_IndexParamVideoInit:
2589            {
2590                OMX_PORT_PARAM_TYPE *portParamType =
2591                    (OMX_PORT_PARAM_TYPE *) paramData;
2592                DEBUG_PRINT("get_parameter: OMX_IndexParamVideoInit\n");
2593                portParamType->nVersion.nVersion = OMX_SPEC_VERSION;
2594                portParamType->nSize = (OMX_U32)sizeof(portParamType);
2595                portParamType->nPorts           = 0;
2596                portParamType->nStartPortNumber = 0;
2597                break;
2598            }
2599        case OMX_IndexParamPriorityMgmt:
2600            {
2601                OMX_PRIORITYMGMTTYPE *priorityMgmtType =
2602                (OMX_PRIORITYMGMTTYPE*)paramData;
2603                DEBUG_PRINT("get_parameter: OMX_IndexParamPriorityMgmt\n");
2604                priorityMgmtType->nSize = (OMX_U32)sizeof(priorityMgmtType);
2605                priorityMgmtType->nVersion.nVersion = OMX_SPEC_VERSION;
2606                priorityMgmtType->nGroupID = m_priority_mgm.nGroupID;
2607                priorityMgmtType->nGroupPriority =
2608					m_priority_mgm.nGroupPriority;
2609                break;
2610            }
2611        case OMX_IndexParamImageInit:
2612            {
2613                OMX_PORT_PARAM_TYPE *portParamType =
2614                (OMX_PORT_PARAM_TYPE *) paramData;
2615                DEBUG_PRINT("get_parameter: OMX_IndexParamImageInit\n");
2616                portParamType->nVersion.nVersion = OMX_SPEC_VERSION;
2617                portParamType->nSize = (OMX_U32)sizeof(portParamType);
2618                portParamType->nPorts           = 0;
2619                portParamType->nStartPortNumber = 0;
2620                break;
2621            }
2622
2623        case OMX_IndexParamCompBufferSupplier:
2624            {
2625                DEBUG_PRINT("get_parameter: \
2626				OMX_IndexParamCompBufferSupplier\n");
2627                OMX_PARAM_BUFFERSUPPLIERTYPE *bufferSupplierType
2628                = (OMX_PARAM_BUFFERSUPPLIERTYPE*) paramData;
2629                DEBUG_PRINT("get_parameter: \
2630				OMX_IndexParamCompBufferSupplier\n");
2631
2632                bufferSupplierType->nSize = (OMX_U32)sizeof(bufferSupplierType);
2633                bufferSupplierType->nVersion.nVersion = OMX_SPEC_VERSION;
2634                if (OMX_CORE_INPUT_PORT_INDEX   ==
2635				bufferSupplierType->nPortIndex)
2636                {
2637                    bufferSupplierType->nPortIndex =
2638				OMX_BufferSupplyUnspecified;
2639                } else if (OMX_CORE_OUTPUT_PORT_INDEX ==
2640				bufferSupplierType->nPortIndex)
2641                {
2642                    bufferSupplierType->nPortIndex =
2643				OMX_BufferSupplyUnspecified;
2644                } else
2645                {
2646                    DEBUG_PRINT_ERROR("get_parameter:"\
2647                                      "OMX_IndexParamCompBufferSupplier eRet"\
2648                                      "%08x\n", eRet);
2649                    eRet = OMX_ErrorBadPortIndex;
2650                }
2651                 break;
2652            }
2653
2654            /*Component should support this port definition*/
2655        case OMX_IndexParamOtherInit:
2656            {
2657                OMX_PORT_PARAM_TYPE *portParamType =
2658                    (OMX_PORT_PARAM_TYPE *) paramData;
2659                DEBUG_PRINT("get_parameter: OMX_IndexParamOtherInit\n");
2660                portParamType->nVersion.nVersion = OMX_SPEC_VERSION;
2661                portParamType->nSize = (OMX_U32)sizeof(portParamType);
2662                portParamType->nPorts           = 0;
2663                portParamType->nStartPortNumber = 0;
2664                break;
2665            }
2666	case OMX_IndexParamStandardComponentRole:
2667            {
2668                OMX_PARAM_COMPONENTROLETYPE *componentRole;
2669                componentRole = (OMX_PARAM_COMPONENTROLETYPE*)paramData;
2670                componentRole->nSize = component_Role.nSize;
2671                componentRole->nVersion = component_Role.nVersion;
2672                strlcpy((char *)componentRole->cRole,
2673			(const char*)component_Role.cRole,
2674			sizeof(componentRole->cRole));
2675                DEBUG_PRINT_ERROR("nSize = %d , nVersion = %d, cRole = %s\n",
2676				component_Role.nSize,
2677				component_Role.nVersion,
2678				component_Role.cRole);
2679                break;
2680
2681            }
2682        default:
2683            {
2684                DEBUG_PRINT_ERROR("unknown param %08x\n", paramIndex);
2685                eRet = OMX_ErrorUnsupportedIndex;
2686            }
2687    }
2688    return eRet;
2689
2690}
2691
2692/**
2693 @brief member function that set paramter from IL client
2694
2695 @param hComp handle to component instance
2696 @param paramIndex parameter type
2697 @param paramData pointer to memory space which holds the paramter
2698 @return error status
2699 */
2700OMX_ERRORTYPE  omx_qcelp13_aenc::set_parameter(OMX_IN OMX_HANDLETYPE     hComp,
2701                                            OMX_IN OMX_INDEXTYPE paramIndex,
2702                                            OMX_IN OMX_PTR        paramData)
2703{
2704    OMX_ERRORTYPE eRet = OMX_ErrorNone;
2705
2706    if(hComp == NULL)
2707    {
2708        DEBUG_PRINT_ERROR("Returning OMX_ErrorBadParameter\n");
2709        return OMX_ErrorBadParameter;
2710    }
2711    if (m_state != OMX_StateLoaded)
2712    {
2713        DEBUG_PRINT_ERROR("set_parameter is not in proper state\n");
2714        return OMX_ErrorIncorrectStateOperation;
2715    }
2716    if (paramData == NULL)
2717    {
2718        DEBUG_PRINT("param data is NULL");
2719        return OMX_ErrorBadParameter;
2720    }
2721
2722    switch (paramIndex)
2723    {
2724        case OMX_IndexParamAudioQcelp13:
2725            {
2726                DEBUG_PRINT("OMX_IndexParamAudioQcelp13");
2727                OMX_AUDIO_PARAM_QCELP13TYPE *qcelp13param
2728                = (OMX_AUDIO_PARAM_QCELP13TYPE *) paramData;
2729                memcpy(&m_qcelp13_param,qcelp13param,
2730                                      sizeof(OMX_AUDIO_PARAM_QCELP13TYPE));
2731                break;
2732            }
2733        case OMX_IndexParamPortDefinition:
2734            {
2735                OMX_PARAM_PORTDEFINITIONTYPE *portDefn;
2736                portDefn = (OMX_PARAM_PORTDEFINITIONTYPE *) paramData;
2737
2738                if (((m_state == OMX_StateLoaded)&&
2739                     !BITMASK_PRESENT(&m_flags,OMX_COMPONENT_IDLE_PENDING))
2740                    || (m_state == OMX_StateWaitForResources &&
2741                        ((OMX_DirInput == portDefn->eDir &&
2742			m_inp_bEnabled == true)||
2743                         (OMX_DirInput == portDefn->eDir &&
2744			m_out_bEnabled == true)))
2745                    ||(((OMX_DirInput == portDefn->eDir &&
2746			m_inp_bEnabled == false)||
2747                        (OMX_DirInput == portDefn->eDir &&
2748			m_out_bEnabled == false)) &&
2749                       (m_state != OMX_StateWaitForResources)))
2750                {
2751                    DEBUG_PRINT("Set Parameter called in valid state\n");
2752                } else
2753                {
2754                    DEBUG_PRINT_ERROR("Set Parameter called in \
2755						Invalid State\n");
2756                    return OMX_ErrorIncorrectStateOperation;
2757                }
2758                DEBUG_PRINT("OMX_IndexParamPortDefinition portDefn->nPortIndex "
2759                            "= %u\n",portDefn->nPortIndex);
2760                if (OMX_CORE_INPUT_PORT_INDEX == portDefn->nPortIndex)
2761                {
2762                    if ( portDefn->nBufferCountActual >
2763					OMX_CORE_NUM_INPUT_BUFFERS )
2764                    {
2765                        m_inp_act_buf_count = portDefn->nBufferCountActual;
2766                    } else
2767                    {
2768                        m_inp_act_buf_count =OMX_CORE_NUM_INPUT_BUFFERS;
2769                    }
2770                    input_buffer_size = portDefn->nBufferSize;
2771
2772                } else if (OMX_CORE_OUTPUT_PORT_INDEX == portDefn->nPortIndex)
2773                {
2774                    if ( portDefn->nBufferCountActual >
2775					OMX_CORE_NUM_OUTPUT_BUFFERS )
2776                    {
2777                        m_out_act_buf_count = portDefn->nBufferCountActual;
2778                    } else
2779                    {
2780                        m_out_act_buf_count =OMX_CORE_NUM_OUTPUT_BUFFERS;
2781                    }
2782                    output_buffer_size = portDefn->nBufferSize;
2783                } else
2784                {
2785                    DEBUG_PRINT(" set_parameter: Bad Port idx %d",\
2786                                  (int)portDefn->nPortIndex);
2787                    eRet = OMX_ErrorBadPortIndex;
2788                }
2789                break;
2790            }
2791        case OMX_IndexParamPriorityMgmt:
2792            {
2793                DEBUG_PRINT("set_parameter: OMX_IndexParamPriorityMgmt\n");
2794
2795                if (m_state != OMX_StateLoaded)
2796                {
2797                    DEBUG_PRINT_ERROR("Set Parameter called in \
2798						Invalid State\n");
2799                    return OMX_ErrorIncorrectStateOperation;
2800                }
2801                OMX_PRIORITYMGMTTYPE *priorityMgmtype
2802                = (OMX_PRIORITYMGMTTYPE*) paramData;
2803                DEBUG_PRINT("set_parameter: OMX_IndexParamPriorityMgmt %u\n",
2804                            priorityMgmtype->nGroupID);
2805
2806                DEBUG_PRINT("set_parameter: priorityMgmtype %u\n",
2807                            priorityMgmtype->nGroupPriority);
2808
2809                m_priority_mgm.nGroupID = priorityMgmtype->nGroupID;
2810                m_priority_mgm.nGroupPriority = priorityMgmtype->nGroupPriority;
2811
2812                break;
2813            }
2814        case  OMX_IndexParamAudioPortFormat:
2815            {
2816
2817                OMX_AUDIO_PARAM_PORTFORMATTYPE *portFormatType =
2818                (OMX_AUDIO_PARAM_PORTFORMATTYPE *) paramData;
2819                DEBUG_PRINT("set_parameter: OMX_IndexParamAudioPortFormat\n");
2820
2821                if (OMX_CORE_INPUT_PORT_INDEX== portFormatType->nPortIndex)
2822                {
2823                    portFormatType->eEncoding = OMX_AUDIO_CodingPCM;
2824                } else if (OMX_CORE_OUTPUT_PORT_INDEX ==
2825					 portFormatType->nPortIndex)
2826                {
2827                    DEBUG_PRINT("set_parameter: OMX_IndexParamAudioFormat:"\
2828                                " %u\n", portFormatType->nIndex);
2829                    portFormatType->eEncoding = OMX_AUDIO_CodingQCELP13;
2830                } else
2831                {
2832                    DEBUG_PRINT_ERROR("set_parameter: Bad port index %d\n", \
2833                                      (int)portFormatType->nPortIndex);
2834                    eRet = OMX_ErrorBadPortIndex;
2835                }
2836                break;
2837            }
2838
2839
2840        case OMX_IndexParamCompBufferSupplier:
2841            {
2842                DEBUG_PRINT("set_parameter: \
2843				OMX_IndexParamCompBufferSupplier\n");
2844                OMX_PARAM_BUFFERSUPPLIERTYPE *bufferSupplierType
2845                = (OMX_PARAM_BUFFERSUPPLIERTYPE*) paramData;
2846                DEBUG_PRINT("set_param: OMX_IndexParamCompBufferSupplier %d",\
2847                            bufferSupplierType->eBufferSupplier);
2848
2849                if (bufferSupplierType->nPortIndex == OMX_CORE_INPUT_PORT_INDEX
2850                    || bufferSupplierType->nPortIndex ==
2851					OMX_CORE_OUTPUT_PORT_INDEX)
2852                {
2853                    DEBUG_PRINT("set_parameter:\
2854					OMX_IndexParamCompBufferSupplier\n");
2855                    m_buffer_supplier.eBufferSupplier =
2856				bufferSupplierType->eBufferSupplier;
2857                } else
2858                {
2859                    DEBUG_PRINT_ERROR("set_param:\
2860				IndexParamCompBufferSup %08x\n", eRet);
2861                    eRet = OMX_ErrorBadPortIndex;
2862                }
2863
2864                break; }
2865
2866        case OMX_IndexParamAudioPcm:
2867            {
2868                DEBUG_PRINT("set_parameter: OMX_IndexParamAudioPcm\n");
2869                OMX_AUDIO_PARAM_PCMMODETYPE *pcmparam
2870                = (OMX_AUDIO_PARAM_PCMMODETYPE *) paramData;
2871
2872                if (OMX_CORE_INPUT_PORT_INDEX== pcmparam->nPortIndex)
2873                {
2874                    memcpy(&m_pcm_param,pcmparam,\
2875                        sizeof(OMX_AUDIO_PARAM_PCMMODETYPE));
2876                    DEBUG_PRINT("set_pcm_parameter: %u %u",\
2877                                 m_pcm_param.nChannels,
2878				m_pcm_param.nSamplingRate);
2879                } else
2880                {
2881                    DEBUG_PRINT_ERROR("Set_parameter:OMX_IndexParamAudioPcm "
2882                                      "OMX_ErrorBadPortIndex %d\n",
2883                                      (int)pcmparam->nPortIndex);
2884                    eRet = OMX_ErrorBadPortIndex;
2885                }
2886                break;
2887            }
2888        case OMX_IndexParamSuspensionPolicy:
2889            {
2890                eRet = OMX_ErrorNotImplemented;
2891                break;
2892            }
2893        case OMX_IndexParamStandardComponentRole:
2894            {
2895                OMX_PARAM_COMPONENTROLETYPE *componentRole;
2896                componentRole = (OMX_PARAM_COMPONENTROLETYPE*)paramData;
2897                component_Role.nSize = componentRole->nSize;
2898                component_Role.nVersion = componentRole->nVersion;
2899                strlcpy((char *)component_Role.cRole,
2900                       (const char*)componentRole->cRole,
2901			sizeof(component_Role.cRole));
2902                break;
2903            }
2904
2905        default:
2906            {
2907                DEBUG_PRINT_ERROR("unknown param %d\n", paramIndex);
2908                eRet = OMX_ErrorUnsupportedIndex;
2909            }
2910    }
2911    return eRet;
2912}
2913
2914/* ======================================================================
2915FUNCTION
2916  omx_qcelp13_aenc::GetConfig
2917
2918DESCRIPTION
2919  OMX Get Config Method implementation.
2920
2921PARAMETERS
2922  <TBD>.
2923
2924RETURN VALUE
2925  OMX Error None if successful.
2926
2927========================================================================== */
2928OMX_ERRORTYPE  omx_qcelp13_aenc::get_config(OMX_IN OMX_HANDLETYPE      hComp,
2929                                         OMX_IN OMX_INDEXTYPE configIndex,
2930                                         OMX_INOUT OMX_PTR     configData)
2931{
2932    OMX_ERRORTYPE eRet = OMX_ErrorNone;
2933
2934    if(hComp == NULL)
2935    {
2936        DEBUG_PRINT_ERROR("Returning OMX_ErrorBadParameter\n");
2937        return OMX_ErrorBadParameter;
2938    }
2939    if (m_state == OMX_StateInvalid)
2940    {
2941        DEBUG_PRINT_ERROR("Get Config in Invalid State\n");
2942        return OMX_ErrorInvalidState;
2943    }
2944
2945    switch (configIndex)
2946    {
2947        case OMX_IndexConfigAudioVolume:
2948            {
2949                OMX_AUDIO_CONFIG_VOLUMETYPE *volume =
2950                (OMX_AUDIO_CONFIG_VOLUMETYPE*) configData;
2951
2952                if (OMX_CORE_INPUT_PORT_INDEX == volume->nPortIndex)
2953                {
2954                    volume->nSize = (OMX_U32)sizeof(volume);
2955                    volume->nVersion.nVersion = OMX_SPEC_VERSION;
2956                    volume->bLinear = OMX_TRUE;
2957                    volume->sVolume.nValue = m_volume;
2958                    volume->sVolume.nMax   = OMX_AENC_MAX;
2959                    volume->sVolume.nMin   = OMX_AENC_MIN;
2960                } else
2961                {
2962                    eRet = OMX_ErrorBadPortIndex;
2963                }
2964            }
2965            break;
2966
2967        case OMX_IndexConfigAudioMute:
2968            {
2969                OMX_AUDIO_CONFIG_MUTETYPE *mute =
2970                (OMX_AUDIO_CONFIG_MUTETYPE*) configData;
2971
2972                if (OMX_CORE_INPUT_PORT_INDEX == mute->nPortIndex)
2973                {
2974                    mute->nSize = (OMX_U32)sizeof(mute);
2975                    mute->nVersion.nVersion = OMX_SPEC_VERSION;
2976                    mute->bMute = (BITMASK_PRESENT(&m_flags,
2977                                      OMX_COMPONENT_MUTED)?OMX_TRUE:OMX_FALSE);
2978                } else
2979                {
2980                    eRet = OMX_ErrorBadPortIndex;
2981                }
2982            }
2983            break;
2984
2985        default:
2986            eRet = OMX_ErrorUnsupportedIndex;
2987            break;
2988    }
2989    return eRet;
2990}
2991
2992/* ======================================================================
2993FUNCTION
2994  omx_qcelp13_aenc::SetConfig
2995
2996DESCRIPTION
2997  OMX Set Config method implementation
2998
2999PARAMETERS
3000  <TBD>.
3001
3002RETURN VALUE
3003  OMX Error None if successful.
3004========================================================================== */
3005OMX_ERRORTYPE  omx_qcelp13_aenc::set_config(OMX_IN OMX_HANDLETYPE      hComp,
3006                                         OMX_IN OMX_INDEXTYPE configIndex,
3007                                         OMX_IN OMX_PTR        configData)
3008{
3009    OMX_ERRORTYPE eRet = OMX_ErrorNone;
3010
3011    if(hComp == NULL)
3012    {
3013        DEBUG_PRINT_ERROR("Returning OMX_ErrorBadParameter\n");
3014        return OMX_ErrorBadParameter;
3015    }
3016    if (m_state == OMX_StateInvalid)
3017    {
3018        DEBUG_PRINT_ERROR("Set Config in Invalid State\n");
3019        return OMX_ErrorInvalidState;
3020    }
3021    if ( m_state == OMX_StateExecuting)
3022    {
3023        DEBUG_PRINT_ERROR("set_config:Ignore in Exe state\n");
3024        return OMX_ErrorInvalidState;
3025    }
3026
3027    switch (configIndex)
3028    {
3029        case OMX_IndexConfigAudioVolume:
3030            {
3031                OMX_AUDIO_CONFIG_VOLUMETYPE *vol =
3032			(OMX_AUDIO_CONFIG_VOLUMETYPE*)configData;
3033                if (vol->nPortIndex == OMX_CORE_INPUT_PORT_INDEX)
3034                {
3035                    if ((vol->sVolume.nValue <= OMX_AENC_MAX) &&
3036                        (vol->sVolume.nValue >= OMX_AENC_MIN))
3037                    {
3038                        m_volume = vol->sVolume.nValue;
3039                        if (BITMASK_ABSENT(&m_flags, OMX_COMPONENT_MUTED))
3040                        {
3041                            /* ioctl(m_drv_fd, AUDIO_VOLUME,
3042                            m_volume * OMX_AENC_VOLUME_STEP); */
3043                        }
3044
3045                    } else
3046                    {
3047                        eRet = OMX_ErrorBadParameter;
3048                    }
3049                } else
3050                {
3051                    eRet = OMX_ErrorBadPortIndex;
3052                }
3053            }
3054            break;
3055
3056        case OMX_IndexConfigAudioMute:
3057            {
3058                OMX_AUDIO_CONFIG_MUTETYPE *mute = (OMX_AUDIO_CONFIG_MUTETYPE*)
3059                                                  configData;
3060                if (mute->nPortIndex == OMX_CORE_INPUT_PORT_INDEX)
3061                {
3062                    if (mute->bMute == OMX_TRUE)
3063                    {
3064                        BITMASK_SET(&m_flags, OMX_COMPONENT_MUTED);
3065                        /* ioctl(m_drv_fd, AUDIO_VOLUME, 0); */
3066                    } else
3067                    {
3068                        BITMASK_CLEAR(&m_flags, OMX_COMPONENT_MUTED);
3069                        /* ioctl(m_drv_fd, AUDIO_VOLUME,
3070                        m_volume * OMX_AENC_VOLUME_STEP); */
3071                    }
3072                } else
3073                {
3074                    eRet = OMX_ErrorBadPortIndex;
3075                }
3076            }
3077            break;
3078
3079        default:
3080            eRet = OMX_ErrorUnsupportedIndex;
3081            break;
3082    }
3083    return eRet;
3084}
3085
3086/* ======================================================================
3087FUNCTION
3088  omx_qcelp13_aenc::GetExtensionIndex
3089
3090DESCRIPTION
3091  OMX GetExtensionIndex method implementaion.  <TBD>
3092
3093PARAMETERS
3094  <TBD>.
3095
3096RETURN VALUE
3097  OMX Error None if everything successful.
3098
3099========================================================================== */
3100OMX_ERRORTYPE  omx_qcelp13_aenc::get_extension_index(
3101				OMX_IN OMX_HANDLETYPE      hComp,
3102				OMX_IN OMX_STRING      paramName,
3103				OMX_OUT OMX_INDEXTYPE* indexType)
3104{
3105    if((hComp == NULL) || (paramName == NULL) || (indexType == NULL))
3106    {
3107        DEBUG_PRINT_ERROR("Returning OMX_ErrorBadParameter\n");
3108        return OMX_ErrorBadParameter;
3109    }
3110    if (m_state == OMX_StateInvalid)
3111    {
3112        DEBUG_PRINT_ERROR("Get Extension Index in Invalid State\n");
3113        return OMX_ErrorInvalidState;
3114    }
3115  if(strncmp(paramName,"OMX.Qualcomm.index.audio.sessionId",
3116		strlen("OMX.Qualcomm.index.audio.sessionId")) == 0)
3117  {
3118      *indexType =(OMX_INDEXTYPE)QOMX_IndexParamAudioSessionId;
3119      DEBUG_PRINT("Extension index type - %d\n", *indexType);
3120
3121  }
3122  else
3123  {
3124      return OMX_ErrorBadParameter;
3125
3126  }
3127  return OMX_ErrorNone;
3128}
3129
3130/* ======================================================================
3131FUNCTION
3132  omx_qcelp13_aenc::GetState
3133
3134DESCRIPTION
3135  Returns the state information back to the caller.<TBD>
3136
3137PARAMETERS
3138  <TBD>.
3139
3140RETURN VALUE
3141  Error None if everything is successful.
3142========================================================================== */
3143OMX_ERRORTYPE  omx_qcelp13_aenc::get_state(OMX_IN OMX_HANDLETYPE  hComp,
3144                                        OMX_OUT OMX_STATETYPE* state)
3145{
3146    if(hComp == NULL)
3147    {
3148        DEBUG_PRINT_ERROR("Returning OMX_ErrorBadParameter\n");
3149        return OMX_ErrorBadParameter;
3150    }
3151    *state = m_state;
3152    DEBUG_PRINT("Returning the state %d\n",*state);
3153    return OMX_ErrorNone;
3154}
3155
3156/* ======================================================================
3157FUNCTION
3158  omx_qcelp13_aenc::ComponentTunnelRequest
3159
3160DESCRIPTION
3161  OMX Component Tunnel Request method implementation. <TBD>
3162
3163PARAMETERS
3164  None.
3165
3166RETURN VALUE
3167  OMX Error None if everything successful.
3168
3169========================================================================== */
3170OMX_ERRORTYPE  omx_qcelp13_aenc::component_tunnel_request
3171(
3172    OMX_IN OMX_HANDLETYPE                hComp,
3173    OMX_IN OMX_U32                        port,
3174    OMX_IN OMX_HANDLETYPE        peerComponent,
3175    OMX_IN OMX_U32                    peerPort,
3176    OMX_INOUT OMX_TUNNELSETUPTYPE* tunnelSetup)
3177{
3178    DEBUG_PRINT_ERROR("Error: component_tunnel_request Not Implemented\n");
3179
3180    if((hComp == NULL) || (peerComponent == NULL) || (tunnelSetup == NULL))
3181    {
3182        port = port;
3183        peerPort = peerPort;
3184        DEBUG_PRINT_ERROR("Returning OMX_ErrorBadParameter\n");
3185        return OMX_ErrorBadParameter;
3186    }
3187    return OMX_ErrorNotImplemented;
3188}
3189
3190/* ======================================================================
3191FUNCTION
3192  omx_qcelp13_aenc::AllocateInputBuffer
3193
3194DESCRIPTION
3195  Helper function for allocate buffer in the input pin
3196
3197PARAMETERS
3198  None.
3199
3200RETURN VALUE
3201  true/false
3202
3203========================================================================== */
3204OMX_ERRORTYPE  omx_qcelp13_aenc::allocate_input_buffer
3205(
3206    OMX_IN OMX_HANDLETYPE                hComp,
3207    OMX_INOUT OMX_BUFFERHEADERTYPE** bufferHdr,
3208    OMX_IN OMX_U32                        port,
3209    OMX_IN OMX_PTR                     appData,
3210    OMX_IN OMX_U32                       bytes)
3211{
3212    OMX_ERRORTYPE         eRet = OMX_ErrorNone;
3213    OMX_BUFFERHEADERTYPE  *bufHdr;
3214    unsigned              nBufSize = MAX(bytes, input_buffer_size);
3215    char                  *buf_ptr;
3216  if(m_inp_current_buf_count < m_inp_act_buf_count)
3217  {
3218    buf_ptr = (char *) calloc((nBufSize + \
3219		sizeof(OMX_BUFFERHEADERTYPE)+sizeof(META_IN)) , 1);
3220
3221    if(hComp == NULL)
3222    {
3223        port = port;
3224        DEBUG_PRINT_ERROR("Returning OMX_ErrorBadParameter\n");
3225        free(buf_ptr);
3226        return OMX_ErrorBadParameter;
3227    }
3228    if (buf_ptr != NULL)
3229    {
3230        bufHdr = (OMX_BUFFERHEADERTYPE *) buf_ptr;
3231        *bufferHdr = bufHdr;
3232        memset(bufHdr,0,sizeof(OMX_BUFFERHEADERTYPE));
3233
3234        bufHdr->pBuffer           = (OMX_U8 *)((buf_ptr) + sizeof(META_IN)+
3235                                               sizeof(OMX_BUFFERHEADERTYPE));
3236        bufHdr->nSize             = (OMX_U32)sizeof(OMX_BUFFERHEADERTYPE);
3237        bufHdr->nVersion.nVersion = OMX_SPEC_VERSION;
3238        bufHdr->nAllocLen         = nBufSize;
3239        bufHdr->pAppPrivate       = appData;
3240        bufHdr->nInputPortIndex   = OMX_CORE_INPUT_PORT_INDEX;
3241        m_input_buf_hdrs.insert(bufHdr, NULL);
3242
3243        m_inp_current_buf_count++;
3244        DEBUG_PRINT("AIB:bufHdr %p bufHdr->pBuffer %p m_inp_buf_cnt=%d \
3245		bytes=%u", bufHdr, bufHdr->pBuffer,m_inp_current_buf_count,
3246                    bytes);
3247
3248    } else
3249    {
3250        DEBUG_PRINT("Input buffer memory allocation failed 1 \n");
3251        eRet =  OMX_ErrorInsufficientResources;
3252    }
3253  }
3254  else
3255  {
3256     DEBUG_PRINT("Input buffer memory allocation failed 2\n");
3257    eRet =  OMX_ErrorInsufficientResources;
3258  }
3259    return eRet;
3260}
3261
3262OMX_ERRORTYPE  omx_qcelp13_aenc::allocate_output_buffer
3263(
3264    OMX_IN OMX_HANDLETYPE                hComp,
3265    OMX_INOUT OMX_BUFFERHEADERTYPE** bufferHdr,
3266    OMX_IN OMX_U32                        port,
3267    OMX_IN OMX_PTR                     appData,
3268    OMX_IN OMX_U32                       bytes)
3269{
3270    OMX_ERRORTYPE         eRet = OMX_ErrorNone;
3271    OMX_BUFFERHEADERTYPE  *bufHdr;
3272    unsigned                   nBufSize = MAX(bytes,output_buffer_size);
3273    char                  *buf_ptr;
3274
3275    if(hComp == NULL)
3276    {
3277        port = port;
3278        DEBUG_PRINT_ERROR("Returning OMX_ErrorBadParameter\n");
3279        return OMX_ErrorBadParameter;
3280    }
3281    if (m_out_current_buf_count < m_out_act_buf_count)
3282    {
3283        buf_ptr = (char *) calloc( (nBufSize + sizeof(OMX_BUFFERHEADERTYPE)),1);
3284
3285        if (buf_ptr != NULL)
3286        {
3287            bufHdr = (OMX_BUFFERHEADERTYPE *) buf_ptr;
3288            *bufferHdr = bufHdr;
3289            memset(bufHdr,0,sizeof(OMX_BUFFERHEADERTYPE));
3290
3291            bufHdr->pBuffer           = (OMX_U8 *)((buf_ptr) +
3292					sizeof(OMX_BUFFERHEADERTYPE));
3293            bufHdr->nSize             = (OMX_U32)sizeof(OMX_BUFFERHEADERTYPE);
3294            bufHdr->nVersion.nVersion = OMX_SPEC_VERSION;
3295            bufHdr->nAllocLen         = nBufSize;
3296            bufHdr->pAppPrivate       = appData;
3297            bufHdr->nOutputPortIndex   = OMX_CORE_OUTPUT_PORT_INDEX;
3298            m_output_buf_hdrs.insert(bufHdr, NULL);
3299            m_out_current_buf_count++;
3300            DEBUG_PRINT("AOB::bufHdr %p bufHdr->pBuffer %p m_out_buf_cnt=%d "\
3301                        "bytes=%u",bufHdr, bufHdr->pBuffer,\
3302                        m_out_current_buf_count, bytes);
3303        } else
3304        {
3305            DEBUG_PRINT("Output buffer memory allocation failed 1 \n");
3306            eRet =  OMX_ErrorInsufficientResources;
3307        }
3308    } else
3309    {
3310        DEBUG_PRINT("Output buffer memory allocation failed\n");
3311        eRet =  OMX_ErrorInsufficientResources;
3312    }
3313    return eRet;
3314}
3315
3316
3317// AllocateBuffer  -- API Call
3318/* ======================================================================
3319FUNCTION
3320  omx_qcelp13_aenc::AllocateBuffer
3321
3322DESCRIPTION
3323  Returns zero if all the buffers released..
3324
3325PARAMETERS
3326  None.
3327
3328RETURN VALUE
3329  true/false
3330
3331========================================================================== */
3332OMX_ERRORTYPE  omx_qcelp13_aenc::allocate_buffer
3333(
3334    OMX_IN OMX_HANDLETYPE                hComp,
3335    OMX_INOUT OMX_BUFFERHEADERTYPE** bufferHdr,
3336    OMX_IN OMX_U32                        port,
3337    OMX_IN OMX_PTR                     appData,
3338    OMX_IN OMX_U32                       bytes)
3339{
3340
3341    OMX_ERRORTYPE eRet = OMX_ErrorNone;          // OMX return type
3342
3343    if (m_state == OMX_StateInvalid)
3344    {
3345        DEBUG_PRINT_ERROR("Allocate Buf in Invalid State\n");
3346        return OMX_ErrorInvalidState;
3347    }
3348    // What if the client calls again.
3349    if (OMX_CORE_INPUT_PORT_INDEX == port)
3350    {
3351        eRet = allocate_input_buffer(hComp,bufferHdr,port,appData,bytes);
3352    } else if (OMX_CORE_OUTPUT_PORT_INDEX == port)
3353    {
3354        eRet = allocate_output_buffer(hComp,bufferHdr,port,appData,bytes);
3355    } else
3356    {
3357        DEBUG_PRINT_ERROR("Error: Invalid Port Index received %d\n",
3358                          (int)port);
3359        eRet = OMX_ErrorBadPortIndex;
3360    }
3361
3362    if (eRet == OMX_ErrorNone)
3363    {
3364        DEBUG_PRINT("allocate_buffer:  before allocate_done \n");
3365        if (allocate_done())
3366        {
3367            DEBUG_PRINT("allocate_buffer:  after allocate_done \n");
3368            if (BITMASK_PRESENT(&m_flags,OMX_COMPONENT_IDLE_PENDING))
3369            {
3370                BITMASK_CLEAR(&m_flags, OMX_COMPONENT_IDLE_PENDING);
3371                post_command(OMX_CommandStateSet,OMX_StateIdle,
3372                             OMX_COMPONENT_GENERATE_EVENT);
3373                DEBUG_PRINT("allocate_buffer:  post idle transition event \n");
3374            }
3375            DEBUG_PRINT("allocate_buffer:  complete \n");
3376        }
3377        if (port == OMX_CORE_INPUT_PORT_INDEX && m_inp_bPopulated)
3378        {
3379            if (BITMASK_PRESENT(&m_flags,OMX_COMPONENT_INPUT_ENABLE_PENDING))
3380            {
3381                BITMASK_CLEAR((&m_flags),OMX_COMPONENT_INPUT_ENABLE_PENDING);
3382                post_command(OMX_CommandPortEnable, OMX_CORE_INPUT_PORT_INDEX,
3383                             OMX_COMPONENT_GENERATE_EVENT);
3384            }
3385        }
3386        if (port == OMX_CORE_OUTPUT_PORT_INDEX && m_out_bPopulated)
3387        {
3388            if (BITMASK_PRESENT(&m_flags,OMX_COMPONENT_OUTPUT_ENABLE_PENDING))
3389            {
3390                BITMASK_CLEAR((&m_flags),OMX_COMPONENT_OUTPUT_ENABLE_PENDING);
3391                m_out_bEnabled = OMX_TRUE;
3392
3393                DEBUG_PRINT("AllocBuf-->is_out_th_sleep=%d\n",is_out_th_sleep);
3394                pthread_mutex_lock(&m_out_th_lock_1);
3395                if (is_out_th_sleep)
3396                {
3397                    is_out_th_sleep = false;
3398                    DEBUG_DETAIL("AllocBuf:WAKING UP OUT THREADS\n");
3399                    out_th_wakeup();
3400                }
3401                pthread_mutex_unlock(&m_out_th_lock_1);
3402                pthread_mutex_lock(&m_in_th_lock_1);
3403                if(is_in_th_sleep)
3404                {
3405                   is_in_th_sleep = false;
3406                   DEBUG_DETAIL("AB:WAKING UP IN THREADS\n");
3407                   in_th_wakeup();
3408                }
3409                pthread_mutex_unlock(&m_in_th_lock_1);
3410                post_command(OMX_CommandPortEnable, OMX_CORE_OUTPUT_PORT_INDEX,
3411                             OMX_COMPONENT_GENERATE_EVENT);
3412            }
3413        }
3414    }
3415    DEBUG_PRINT("Allocate Buffer exit with ret Code %d\n", eRet);
3416    return eRet;
3417}
3418
3419/*=============================================================================
3420FUNCTION:
3421  use_buffer
3422
3423DESCRIPTION:
3424  OMX Use Buffer method implementation.
3425
3426INPUT/OUTPUT PARAMETERS:
3427  [INOUT] bufferHdr
3428  [IN] hComp
3429  [IN] port
3430  [IN] appData
3431  [IN] bytes
3432  [IN] buffer
3433
3434RETURN VALUE:
3435  OMX_ERRORTYPE
3436
3437Dependency:
3438  None
3439
3440SIDE EFFECTS:
3441  None
3442=============================================================================*/
3443OMX_ERRORTYPE  omx_qcelp13_aenc::use_buffer
3444(
3445    OMX_IN OMX_HANDLETYPE            hComp,
3446    OMX_INOUT OMX_BUFFERHEADERTYPE** bufferHdr,
3447    OMX_IN OMX_U32                   port,
3448    OMX_IN OMX_PTR                   appData,
3449    OMX_IN OMX_U32                   bytes,
3450    OMX_IN OMX_U8*                   buffer)
3451{
3452    OMX_ERRORTYPE eRet = OMX_ErrorNone;
3453
3454    if (OMX_CORE_INPUT_PORT_INDEX == port)
3455    {
3456        eRet = use_input_buffer(hComp,bufferHdr,port,appData,bytes,buffer);
3457
3458    } else if (OMX_CORE_OUTPUT_PORT_INDEX == port)
3459    {
3460        eRet = use_output_buffer(hComp,bufferHdr,port,appData,bytes,buffer);
3461    } else
3462    {
3463        DEBUG_PRINT_ERROR("Error: Invalid Port Index received %d\n",(int)port);
3464        eRet = OMX_ErrorBadPortIndex;
3465    }
3466
3467    if (eRet == OMX_ErrorNone)
3468    {
3469        DEBUG_PRINT("Checking for Output Allocate buffer Done");
3470        if (allocate_done())
3471        {
3472            if (BITMASK_PRESENT(&m_flags,OMX_COMPONENT_IDLE_PENDING))
3473            {
3474                BITMASK_CLEAR(&m_flags, OMX_COMPONENT_IDLE_PENDING);
3475                post_command(OMX_CommandStateSet,OMX_StateIdle,
3476                             OMX_COMPONENT_GENERATE_EVENT);
3477            }
3478        }
3479        if (port == OMX_CORE_INPUT_PORT_INDEX && m_inp_bPopulated)
3480        {
3481            if (BITMASK_PRESENT(&m_flags,OMX_COMPONENT_INPUT_ENABLE_PENDING))
3482            {
3483                BITMASK_CLEAR((&m_flags),OMX_COMPONENT_INPUT_ENABLE_PENDING);
3484                post_command(OMX_CommandPortEnable, OMX_CORE_INPUT_PORT_INDEX,
3485                             OMX_COMPONENT_GENERATE_EVENT);
3486
3487            }
3488        }
3489        if (port == OMX_CORE_OUTPUT_PORT_INDEX && m_out_bPopulated)
3490        {
3491            if (BITMASK_PRESENT(&m_flags,OMX_COMPONENT_OUTPUT_ENABLE_PENDING))
3492            {
3493                BITMASK_CLEAR((&m_flags),OMX_COMPONENT_OUTPUT_ENABLE_PENDING);
3494                post_command(OMX_CommandPortEnable, OMX_CORE_OUTPUT_PORT_INDEX,
3495                             OMX_COMPONENT_GENERATE_EVENT);
3496                pthread_mutex_lock(&m_out_th_lock_1);
3497                if (is_out_th_sleep)
3498                {
3499                    is_out_th_sleep = false;
3500                    DEBUG_DETAIL("UseBuf:WAKING UP OUT THREADS\n");
3501                    out_th_wakeup();
3502                }
3503                pthread_mutex_unlock(&m_out_th_lock_1);
3504                pthread_mutex_lock(&m_in_th_lock_1);
3505                if(is_in_th_sleep)
3506                {
3507                   is_in_th_sleep = false;
3508                   DEBUG_DETAIL("UB:WAKING UP IN THREADS\n");
3509                   in_th_wakeup();
3510                }
3511                pthread_mutex_unlock(&m_in_th_lock_1);
3512        }
3513    }
3514  }
3515    DEBUG_PRINT("Use Buffer for port[%u] eRet[%d]\n", port,eRet);
3516    return eRet;
3517}
3518/*=============================================================================
3519FUNCTION:
3520  use_input_buffer
3521
3522DESCRIPTION:
3523  Helper function for Use buffer in the input pin
3524
3525INPUT/OUTPUT PARAMETERS:
3526  [INOUT] bufferHdr
3527  [IN] hComp
3528  [IN] port
3529  [IN] appData
3530  [IN] bytes
3531  [IN] buffer
3532
3533RETURN VALUE:
3534  OMX_ERRORTYPE
3535
3536Dependency:
3537  None
3538
3539SIDE EFFECTS:
3540  None
3541=============================================================================*/
3542OMX_ERRORTYPE  omx_qcelp13_aenc::use_input_buffer
3543(
3544    OMX_IN OMX_HANDLETYPE            hComp,
3545    OMX_INOUT OMX_BUFFERHEADERTYPE** bufferHdr,
3546    OMX_IN OMX_U32                   port,
3547    OMX_IN OMX_PTR                   appData,
3548    OMX_IN OMX_U32                   bytes,
3549    OMX_IN OMX_U8*                   buffer)
3550{
3551    OMX_ERRORTYPE         eRet = OMX_ErrorNone;
3552    OMX_BUFFERHEADERTYPE  *bufHdr;
3553    unsigned              nBufSize = MAX(bytes, input_buffer_size);
3554    char                  *buf_ptr;
3555
3556    if(hComp == NULL)
3557    {
3558        port = port;
3559        DEBUG_PRINT_ERROR("Returning OMX_ErrorBadParameter\n");
3560        return OMX_ErrorBadParameter;
3561    }
3562    if(bytes < input_buffer_size)
3563    {
3564      /* return if i\p buffer size provided by client
3565       is less than min i\p buffer size supported by omx component*/
3566      return OMX_ErrorInsufficientResources;
3567    }
3568    if (m_inp_current_buf_count < m_inp_act_buf_count)
3569    {
3570        buf_ptr = (char *) calloc(sizeof(OMX_BUFFERHEADERTYPE), 1);
3571
3572        if (buf_ptr != NULL)
3573        {
3574            bufHdr = (OMX_BUFFERHEADERTYPE *) buf_ptr;
3575            *bufferHdr = bufHdr;
3576            memset(bufHdr,0,sizeof(OMX_BUFFERHEADERTYPE));
3577
3578            bufHdr->pBuffer           = (OMX_U8 *)(buffer);
3579            DEBUG_PRINT("use_input_buffer:bufHdr %p bufHdr->pBuffer %p \
3580			bytes=%u", bufHdr, bufHdr->pBuffer,bytes);
3581            bufHdr->nSize             = (OMX_U32)sizeof(OMX_BUFFERHEADERTYPE);
3582            bufHdr->nVersion.nVersion = OMX_SPEC_VERSION;
3583            bufHdr->nAllocLen         = nBufSize;
3584            input_buffer_size         = nBufSize;
3585            bufHdr->pAppPrivate       = appData;
3586            bufHdr->nInputPortIndex   = OMX_CORE_INPUT_PORT_INDEX;
3587            bufHdr->nOffset           = 0;
3588            m_input_buf_hdrs.insert(bufHdr, NULL);
3589            m_inp_current_buf_count++;
3590        } else
3591        {
3592            DEBUG_PRINT("Input buffer memory allocation failed 1 \n");
3593            eRet =  OMX_ErrorInsufficientResources;
3594        }
3595    } else
3596    {
3597        DEBUG_PRINT("Input buffer memory allocation failed\n");
3598        eRet =  OMX_ErrorInsufficientResources;
3599    }
3600    return eRet;
3601}
3602
3603/*=============================================================================
3604FUNCTION:
3605  use_output_buffer
3606
3607DESCRIPTION:
3608  Helper function for Use buffer in the output pin
3609
3610INPUT/OUTPUT PARAMETERS:
3611  [INOUT] bufferHdr
3612  [IN] hComp
3613  [IN] port
3614  [IN] appData
3615  [IN] bytes
3616  [IN] buffer
3617
3618RETURN VALUE:
3619  OMX_ERRORTYPE
3620
3621Dependency:
3622  None
3623
3624SIDE EFFECTS:
3625  None
3626=============================================================================*/
3627OMX_ERRORTYPE  omx_qcelp13_aenc::use_output_buffer
3628(
3629    OMX_IN OMX_HANDLETYPE            hComp,
3630    OMX_INOUT OMX_BUFFERHEADERTYPE** bufferHdr,
3631    OMX_IN OMX_U32                   port,
3632    OMX_IN OMX_PTR                   appData,
3633    OMX_IN OMX_U32                   bytes,
3634    OMX_IN OMX_U8*                   buffer)
3635{
3636    OMX_ERRORTYPE         eRet = OMX_ErrorNone;
3637    OMX_BUFFERHEADERTYPE  *bufHdr;
3638    unsigned              nBufSize = MAX(bytes,output_buffer_size);
3639    char                  *buf_ptr;
3640
3641    if(hComp == NULL)
3642    {
3643        port = port;
3644        DEBUG_PRINT_ERROR("Returning OMX_ErrorBadParameter\n");
3645        return OMX_ErrorBadParameter;
3646    }
3647    if (bytes < output_buffer_size)
3648    {
3649        /* return if o\p buffer size provided by client
3650        is less than min o\p buffer size supported by omx component*/
3651        return OMX_ErrorInsufficientResources;
3652    }
3653
3654    DEBUG_PRINT("Inside omx_qcelp13_aenc::use_output_buffer");
3655    if (m_out_current_buf_count < m_out_act_buf_count)
3656    {
3657
3658        buf_ptr = (char *) calloc(sizeof(OMX_BUFFERHEADERTYPE), 1);
3659
3660        if (buf_ptr != NULL)
3661        {
3662            bufHdr = (OMX_BUFFERHEADERTYPE *) buf_ptr;
3663            DEBUG_PRINT("BufHdr=%p buffer=%p\n",bufHdr,buffer);
3664            *bufferHdr = bufHdr;
3665            memset(bufHdr,0,sizeof(OMX_BUFFERHEADERTYPE));
3666
3667            bufHdr->pBuffer           = (OMX_U8 *)(buffer);
3668            DEBUG_PRINT("use_output_buffer:bufHdr %p bufHdr->pBuffer %p \
3669			len=%u\n", bufHdr, bufHdr->pBuffer,bytes);
3670            bufHdr->nSize             = (OMX_U32)sizeof(OMX_BUFFERHEADERTYPE);
3671            bufHdr->nVersion.nVersion = OMX_SPEC_VERSION;
3672            bufHdr->nAllocLen         = nBufSize;
3673            output_buffer_size        = nBufSize;
3674            bufHdr->pAppPrivate       = appData;
3675            bufHdr->nOutputPortIndex   = OMX_CORE_OUTPUT_PORT_INDEX;
3676            bufHdr->nOffset           = 0;
3677            m_output_buf_hdrs.insert(bufHdr, NULL);
3678            m_out_current_buf_count++;
3679
3680        } else
3681        {
3682            DEBUG_PRINT("Output buffer memory allocation failed\n");
3683            eRet =  OMX_ErrorInsufficientResources;
3684        }
3685    } else
3686    {
3687        DEBUG_PRINT("Output buffer memory allocation failed 2\n");
3688        eRet =  OMX_ErrorInsufficientResources;
3689    }
3690    return eRet;
3691}
3692/**
3693 @brief member function that searches for caller buffer
3694
3695 @param buffer pointer to buffer header
3696 @return bool value indicating whether buffer is found
3697 */
3698bool omx_qcelp13_aenc::search_input_bufhdr(OMX_BUFFERHEADERTYPE *buffer)
3699{
3700
3701    bool eRet = false;
3702    OMX_BUFFERHEADERTYPE *temp = NULL;
3703
3704    //access only in IL client context
3705    temp = m_input_buf_hdrs.find_ele(buffer);
3706    if (buffer && temp)
3707    {
3708        DEBUG_DETAIL("search_input_bufhdr %p \n", buffer);
3709        eRet = true;
3710    }
3711    return eRet;
3712}
3713
3714/**
3715 @brief member function that searches for caller buffer
3716
3717 @param buffer pointer to buffer header
3718 @return bool value indicating whether buffer is found
3719 */
3720bool omx_qcelp13_aenc::search_output_bufhdr(OMX_BUFFERHEADERTYPE *buffer)
3721{
3722
3723    bool eRet = false;
3724    OMX_BUFFERHEADERTYPE *temp = NULL;
3725
3726    //access only in IL client context
3727    temp = m_output_buf_hdrs.find_ele(buffer);
3728    if (buffer && temp)
3729    {
3730        DEBUG_DETAIL("search_output_bufhdr %p \n", buffer);
3731        eRet = true;
3732    }
3733    return eRet;
3734}
3735
3736// Free Buffer - API call
3737/**
3738  @brief member function that handles free buffer command from IL client
3739
3740  This function is a block-call function that handles IL client request to
3741  freeing the buffer
3742
3743  @param hComp handle to component instance
3744  @param port id of port which holds the buffer
3745  @param buffer buffer header
3746  @return Error status
3747*/
3748OMX_ERRORTYPE  omx_qcelp13_aenc::free_buffer(
3749				OMX_IN OMX_HANDLETYPE         hComp,
3750				OMX_IN OMX_U32                 port,
3751				OMX_IN OMX_BUFFERHEADERTYPE* buffer)
3752{
3753    OMX_ERRORTYPE eRet = OMX_ErrorNone;
3754
3755    DEBUG_PRINT("Free_Buffer buf %p\n", buffer);
3756    if(hComp == NULL)
3757    {
3758        DEBUG_PRINT_ERROR("Returning OMX_ErrorBadParameter\n");
3759        return OMX_ErrorBadParameter;
3760    }
3761    if (m_state == OMX_StateIdle &&
3762        (BITMASK_PRESENT(&m_flags ,OMX_COMPONENT_LOADING_PENDING)))
3763    {
3764        DEBUG_PRINT(" free buffer while Component in Loading pending\n");
3765    } else if ((m_inp_bEnabled == OMX_FALSE &&
3766		port == OMX_CORE_INPUT_PORT_INDEX)||
3767               (m_out_bEnabled == OMX_FALSE &&
3768		port == OMX_CORE_OUTPUT_PORT_INDEX))
3769    {
3770        DEBUG_PRINT("Free Buffer while port %u disabled\n", port);
3771    } else if (m_state == OMX_StateExecuting || m_state == OMX_StatePause)
3772    {
3773        DEBUG_PRINT("Invalid state to free buffer,ports need to be disabled:\
3774                    OMX_ErrorPortUnpopulated\n");
3775        post_command(OMX_EventError,
3776                     OMX_ErrorPortUnpopulated,
3777                     OMX_COMPONENT_GENERATE_EVENT);
3778
3779        return eRet;
3780    } else
3781    {
3782        DEBUG_PRINT("free_buffer: Invalid state to free buffer,ports need to be\
3783                    disabled:OMX_ErrorPortUnpopulated\n");
3784        post_command(OMX_EventError,
3785                     OMX_ErrorPortUnpopulated,
3786                     OMX_COMPONENT_GENERATE_EVENT);
3787    }
3788    if (OMX_CORE_INPUT_PORT_INDEX == port)
3789    {
3790        if (m_inp_current_buf_count != 0)
3791        {
3792            m_inp_bPopulated = OMX_FALSE;
3793            if (true == search_input_bufhdr(buffer))
3794            {
3795                /* Buffer exist */
3796                //access only in IL client context
3797                DEBUG_PRINT("Free_Buf:in_buffer[%p]\n",buffer);
3798                m_input_buf_hdrs.erase(buffer);
3799                free(buffer);
3800                m_inp_current_buf_count--;
3801            } else
3802            {
3803                DEBUG_PRINT_ERROR("Free_Buf:Error-->free_buffer, \
3804                                  Invalid Input buffer header\n");
3805                eRet = OMX_ErrorBadParameter;
3806            }
3807        } else
3808        {
3809            DEBUG_PRINT_ERROR("Error: free_buffer,Port Index calculation \
3810                              came out Invalid\n");
3811            eRet = OMX_ErrorBadPortIndex;
3812        }
3813        if (BITMASK_PRESENT((&m_flags),OMX_COMPONENT_INPUT_DISABLE_PENDING)
3814            && release_done(0))
3815        {
3816            DEBUG_PRINT("INPUT PORT MOVING TO DISABLED STATE \n");
3817            BITMASK_CLEAR((&m_flags),OMX_COMPONENT_INPUT_DISABLE_PENDING);
3818            post_command(OMX_CommandPortDisable,
3819                         OMX_CORE_INPUT_PORT_INDEX,
3820                         OMX_COMPONENT_GENERATE_EVENT);
3821        }
3822    } else if (OMX_CORE_OUTPUT_PORT_INDEX == port)
3823    {
3824        if (m_out_current_buf_count != 0)
3825        {
3826            m_out_bPopulated = OMX_FALSE;
3827            if (true == search_output_bufhdr(buffer))
3828            {
3829                /* Buffer exist */
3830                //access only in IL client context
3831                DEBUG_PRINT("Free_Buf:out_buffer[%p]\n",buffer);
3832                m_output_buf_hdrs.erase(buffer);
3833                free(buffer);
3834                m_out_current_buf_count--;
3835            } else
3836            {
3837                DEBUG_PRINT("Free_Buf:Error-->free_buffer , \
3838                            Invalid Output buffer header\n");
3839                eRet = OMX_ErrorBadParameter;
3840            }
3841        } else
3842        {
3843            eRet = OMX_ErrorBadPortIndex;
3844        }
3845
3846        if (BITMASK_PRESENT((&m_flags),OMX_COMPONENT_OUTPUT_DISABLE_PENDING)
3847            && release_done(1))
3848        {
3849            DEBUG_PRINT("OUTPUT PORT MOVING TO DISABLED STATE \n");
3850            BITMASK_CLEAR((&m_flags),OMX_COMPONENT_OUTPUT_DISABLE_PENDING);
3851            post_command(OMX_CommandPortDisable,
3852                         OMX_CORE_OUTPUT_PORT_INDEX,
3853                         OMX_COMPONENT_GENERATE_EVENT);
3854
3855        }
3856    } else
3857    {
3858        eRet = OMX_ErrorBadPortIndex;
3859    }
3860    if ((OMX_ErrorNone == eRet) &&
3861        (BITMASK_PRESENT(&m_flags ,OMX_COMPONENT_LOADING_PENDING)))
3862    {
3863        if (release_done(-1))
3864        {
3865            if(ioctl(m_drv_fd, AUDIO_STOP, 0) < 0)
3866               DEBUG_PRINT_ERROR("AUDIO STOP in free buffer failed\n");
3867            else
3868               DEBUG_PRINT("AUDIO STOP in free buffer passed\n");
3869
3870
3871            DEBUG_PRINT("Free_Buf: Free buffer\n");
3872
3873
3874            // Send the callback now
3875            BITMASK_CLEAR((&m_flags),OMX_COMPONENT_LOADING_PENDING);
3876            DEBUG_PRINT("Before OMX_StateLoaded \
3877				OMX_COMPONENT_GENERATE_EVENT\n");
3878            post_command(OMX_CommandStateSet,
3879                         OMX_StateLoaded,OMX_COMPONENT_GENERATE_EVENT);
3880            DEBUG_PRINT("After OMX_StateLoaded OMX_COMPONENT_GENERATE_EVENT\n");
3881
3882        }
3883    }
3884    return eRet;
3885}
3886
3887
3888/**
3889 @brief member function that that handles empty this buffer command
3890
3891 This function meremly queue up the command and data would be consumed
3892 in command server thread context
3893
3894 @param hComp handle to component instance
3895 @param buffer pointer to buffer header
3896 @return error status
3897 */
3898OMX_ERRORTYPE  omx_qcelp13_aenc::empty_this_buffer(
3899				OMX_IN OMX_HANDLETYPE         hComp,
3900				OMX_IN OMX_BUFFERHEADERTYPE* buffer)
3901{
3902    OMX_ERRORTYPE eRet = OMX_ErrorNone;
3903
3904    DEBUG_PRINT("ETB:Buf:%p Len %u TS %lld numInBuf=%d\n", \
3905                buffer, buffer->nFilledLen, buffer->nTimeStamp, (nNumInputBuf));
3906    if (m_state == OMX_StateInvalid)
3907    {
3908        DEBUG_PRINT("Empty this buffer in Invalid State\n");
3909        return OMX_ErrorInvalidState;
3910    }
3911    if (!m_inp_bEnabled)
3912    {
3913        DEBUG_PRINT("empty_this_buffer OMX_ErrorIncorrectStateOperation "\
3914                    "Port Status %d \n", m_inp_bEnabled);
3915        return OMX_ErrorIncorrectStateOperation;
3916    }
3917    if (buffer->nSize != sizeof(OMX_BUFFERHEADERTYPE))
3918    {
3919        DEBUG_PRINT("omx_qcelp13_aenc::etb--> Buffer Size Invalid\n");
3920        return OMX_ErrorBadParameter;
3921    }
3922    if (buffer->nVersion.nVersion != OMX_SPEC_VERSION)
3923    {
3924        DEBUG_PRINT("omx_qcelp13_aenc::etb--> OMX Version Invalid\n");
3925        return OMX_ErrorVersionMismatch;
3926    }
3927
3928    if (buffer->nInputPortIndex != OMX_CORE_INPUT_PORT_INDEX)
3929    {
3930        return OMX_ErrorBadPortIndex;
3931    }
3932    if ((m_state != OMX_StateExecuting) &&
3933        (m_state != OMX_StatePause))
3934    {
3935        DEBUG_PRINT_ERROR("Invalid state\n");
3936        eRet = OMX_ErrorInvalidState;
3937    }
3938    if (OMX_ErrorNone == eRet)
3939    {
3940        if (search_input_bufhdr(buffer) == true)
3941        {
3942            post_input((unsigned long)hComp,
3943                       (unsigned long) buffer,OMX_COMPONENT_GENERATE_ETB);
3944        } else
3945        {
3946            DEBUG_PRINT_ERROR("Bad header %p \n", buffer);
3947            eRet = OMX_ErrorBadParameter;
3948        }
3949    }
3950    pthread_mutex_lock(&in_buf_count_lock);
3951    nNumInputBuf++;
3952    m_qcelp13_pb_stats.etb_cnt++;
3953    pthread_mutex_unlock(&in_buf_count_lock);
3954    return eRet;
3955}
3956/**
3957  @brief member function that writes data to kernel driver
3958
3959  @param hComp handle to component instance
3960  @param buffer pointer to buffer header
3961  @return error status
3962 */
3963OMX_ERRORTYPE  omx_qcelp13_aenc::empty_this_buffer_proxy
3964(
3965    OMX_IN OMX_HANDLETYPE         hComp,
3966    OMX_BUFFERHEADERTYPE* buffer)
3967{
3968    OMX_STATETYPE state;
3969    META_IN meta_in;
3970    //Pointer to the starting location of the data to be transcoded
3971    OMX_U8 *srcStart;
3972    //The total length of the data to be transcoded
3973    srcStart = buffer->pBuffer;
3974    OMX_U8 *data = NULL;
3975    PrintFrameHdr(OMX_COMPONENT_GENERATE_ETB,buffer);
3976    memset(&meta_in,0,sizeof(meta_in));
3977    if ( search_input_bufhdr(buffer) == false )
3978    {
3979        DEBUG_PRINT("ETBP: INVALID BUF HDR\n");
3980        buffer_done_cb((OMX_BUFFERHEADERTYPE *)buffer);
3981        return OMX_ErrorBadParameter;
3982    }
3983    if (m_tmp_meta_buf)
3984    {
3985        data = m_tmp_meta_buf;
3986
3987        // copy the metadata info from the BufHdr and insert to payload
3988        meta_in.offsetVal  = (OMX_U16)sizeof(META_IN);
3989        meta_in.nTimeStamp.LowPart =
3990           (unsigned int)((((OMX_BUFFERHEADERTYPE*)buffer)->nTimeStamp) & 0xFFFFFFFF);
3991        meta_in.nTimeStamp.HighPart =
3992           (unsigned int)(((((OMX_BUFFERHEADERTYPE*)buffer)->nTimeStamp) >> 32) & 0xFFFFFFFF);
3993        meta_in.nFlags &= ~OMX_BUFFERFLAG_EOS;
3994        if(buffer->nFlags & OMX_BUFFERFLAG_EOS)
3995        {
3996            DEBUG_PRINT("EOS OCCURED \n");
3997            meta_in.nFlags  |= OMX_BUFFERFLAG_EOS;
3998        }
3999        memcpy(data,&meta_in, meta_in.offsetVal);
4000        DEBUG_PRINT("meta_in.nFlags = 0x%8x\n",meta_in.nFlags);
4001    }
4002
4003    memcpy(&data[sizeof(META_IN)],buffer->pBuffer,buffer->nFilledLen);
4004    write(m_drv_fd, data, buffer->nFilledLen+sizeof(META_IN));
4005
4006    pthread_mutex_lock(&m_state_lock);
4007    get_state(&m_cmp, &state);
4008    pthread_mutex_unlock(&m_state_lock);
4009
4010    if (OMX_StateExecuting == state)
4011    {
4012        DEBUG_DETAIL("In Exe state, EBD CB");
4013        buffer_done_cb((OMX_BUFFERHEADERTYPE *)buffer);
4014    } else
4015    {
4016        /* Assume empty this buffer function has already checked
4017        validity of buffer */
4018        DEBUG_PRINT("Empty buffer %p to kernel driver\n", buffer);
4019        post_input((unsigned long) & hComp,(unsigned long) buffer,
4020                   OMX_COMPONENT_GENERATE_BUFFER_DONE);
4021    }
4022    return OMX_ErrorNone;
4023}
4024
4025OMX_ERRORTYPE  omx_qcelp13_aenc::fill_this_buffer_proxy
4026(
4027    OMX_IN OMX_HANDLETYPE         hComp,
4028    OMX_BUFFERHEADERTYPE* buffer)
4029{
4030    OMX_STATETYPE state;
4031    ENC_META_OUT *meta_out = NULL;
4032    ssize_t nReadbytes = 0;
4033
4034    pthread_mutex_lock(&m_state_lock);
4035    get_state(&m_cmp, &state);
4036    pthread_mutex_unlock(&m_state_lock);
4037
4038    if (true == search_output_bufhdr(buffer))
4039    {
4040          DEBUG_PRINT("\nBefore Read..m_drv_fd = %d,\n",m_drv_fd);
4041          nReadbytes = read(m_drv_fd,buffer->pBuffer,output_buffer_size );
4042          DEBUG_DETAIL("FTBP->Al_len[%lu]buf[%p]size[%d]numOutBuf[%d]\n",\
4043                         buffer->nAllocLen,buffer->pBuffer,
4044                         nReadbytes,nNumOutputBuf);
4045      if (nReadbytes <= 0) {
4046                  buffer->nFilledLen = 0;
4047            buffer->nOffset = 0;
4048                buffer->nTimeStamp = nTimestamp;
4049             frame_done_cb((OMX_BUFFERHEADERTYPE *)buffer);
4050                  return OMX_ErrorNone;
4051      } else
4052              DEBUG_PRINT("Read bytes %d\n",nReadbytes);
4053
4054      // Buffer from Driver will have
4055      // 1 byte => Nr of frame field
4056      // (sizeof(ENC_META_OUT) * Nr of frame) bytes => meta_out->offset_to_frame
4057      // Frame Size * Nr of frame =>
4058
4059      meta_out = (ENC_META_OUT *)(buffer->pBuffer + sizeof(unsigned char));
4060          buffer->nTimeStamp = (((OMX_TICKS)meta_out->msw_ts << 32)+
4061				meta_out->lsw_ts);
4062          buffer->nFlags |= meta_out->nflags;
4063          buffer->nOffset = (OMX_U32) (meta_out->offset_to_frame +
4064                              sizeof(unsigned char));
4065          buffer->nFilledLen = (OMX_U32)(nReadbytes - buffer->nOffset);
4066          nTimestamp = buffer->nTimeStamp;
4067          DEBUG_PRINT("nflags %d frame_size %d offset_to_frame %d \
4068			timestamp %lld\n", meta_out->nflags,
4069			meta_out->frame_size, meta_out->offset_to_frame,
4070			buffer->nTimeStamp);
4071
4072          if ((buffer->nFlags & OMX_BUFFERFLAG_EOS) == OMX_BUFFERFLAG_EOS )
4073          {
4074              buffer->nFilledLen = 0;
4075              buffer->nOffset = 0;
4076              buffer->nTimeStamp = nTimestamp;
4077              frame_done_cb((OMX_BUFFERHEADERTYPE *)buffer);
4078              if ((buffer->nFlags & OMX_BUFFERFLAG_EOS) == OMX_BUFFERFLAG_EOS )
4079              {
4080                  DEBUG_PRINT("FTBP: Now, Send EOS flag to Client \n");
4081                  m_cb.EventHandler(&m_cmp,
4082                                  m_app_data,
4083                                  OMX_EventBufferFlag,
4084                                  1, 1, NULL );
4085              }
4086
4087              return OMX_ErrorNone;
4088          }
4089          DEBUG_PRINT("nState %d \n",nState );
4090
4091          pthread_mutex_lock(&m_state_lock);
4092          get_state(&m_cmp, &state);
4093          pthread_mutex_unlock(&m_state_lock);
4094
4095          if (state == OMX_StatePause)
4096          {
4097              DEBUG_PRINT("FTBP:Post the FBD to event thread currstate=%d\n",\
4098                            state);
4099              post_output((unsigned long) & hComp,(unsigned long) buffer,
4100                            OMX_COMPONENT_GENERATE_FRAME_DONE);
4101          }
4102          else
4103          {
4104              frame_done_cb((OMX_BUFFERHEADERTYPE *)buffer);
4105
4106          }
4107
4108    }
4109    else
4110        DEBUG_PRINT("\n FTBP-->Invalid buffer in FTB \n");
4111
4112
4113    return OMX_ErrorNone;
4114}
4115
4116/* ======================================================================
4117FUNCTION
4118  omx_qcelp13_aenc::FillThisBuffer
4119
4120DESCRIPTION
4121  IL client uses this method to release the frame buffer
4122  after displaying them.
4123
4124
4125
4126PARAMETERS
4127
4128  None.
4129
4130RETURN VALUE
4131  true/false
4132
4133========================================================================== */
4134OMX_ERRORTYPE  omx_qcelp13_aenc::fill_this_buffer
4135(
4136    OMX_IN OMX_HANDLETYPE         hComp,
4137    OMX_IN OMX_BUFFERHEADERTYPE* buffer)
4138{
4139    OMX_ERRORTYPE eRet = OMX_ErrorNone;
4140    if (buffer->nSize != sizeof(OMX_BUFFERHEADERTYPE))
4141    {
4142        DEBUG_PRINT("omx_qcelp13_aenc::ftb--> Buffer Size Invalid\n");
4143        return OMX_ErrorBadParameter;
4144    }
4145    if (m_out_bEnabled == OMX_FALSE)
4146    {
4147        return OMX_ErrorIncorrectStateOperation;
4148    }
4149
4150    if (buffer->nVersion.nVersion != OMX_SPEC_VERSION)
4151    {
4152        DEBUG_PRINT("omx_qcelp13_aenc::ftb--> OMX Version Invalid\n");
4153        return OMX_ErrorVersionMismatch;
4154    }
4155    if (buffer->nOutputPortIndex != OMX_CORE_OUTPUT_PORT_INDEX)
4156    {
4157        return OMX_ErrorBadPortIndex;
4158    }
4159    pthread_mutex_lock(&out_buf_count_lock);
4160    nNumOutputBuf++;
4161    m_qcelp13_pb_stats.ftb_cnt++;
4162    DEBUG_DETAIL("FTB:nNumOutputBuf is %d", nNumOutputBuf);
4163    pthread_mutex_unlock(&out_buf_count_lock);
4164    post_output((unsigned long)hComp,
4165                (unsigned long) buffer,OMX_COMPONENT_GENERATE_FTB);
4166    return eRet;
4167}
4168
4169/* ======================================================================
4170FUNCTION
4171  omx_qcelp13_aenc::SetCallbacks
4172
4173DESCRIPTION
4174  Set the callbacks.
4175
4176PARAMETERS
4177  None.
4178
4179RETURN VALUE
4180  OMX Error None if everything successful.
4181
4182========================================================================== */
4183OMX_ERRORTYPE  omx_qcelp13_aenc::set_callbacks(
4184				OMX_IN OMX_HANDLETYPE        hComp,
4185				OMX_IN OMX_CALLBACKTYPE* callbacks,
4186				OMX_IN OMX_PTR             appData)
4187{
4188    if(hComp == NULL)
4189    {
4190        DEBUG_PRINT_ERROR("Returning OMX_ErrorBadParameter\n");
4191        return OMX_ErrorBadParameter;
4192    }
4193    m_cb       = *callbacks;
4194    m_app_data =    appData;
4195
4196    return OMX_ErrorNone;
4197}
4198
4199/* ======================================================================
4200FUNCTION
4201  omx_qcelp13_aenc::ComponentDeInit
4202
4203DESCRIPTION
4204  Destroys the component and release memory allocated to the heap.
4205
4206PARAMETERS
4207  <TBD>.
4208
4209RETURN VALUE
4210  OMX Error None if everything successful.
4211
4212========================================================================== */
4213OMX_ERRORTYPE  omx_qcelp13_aenc::component_deinit(OMX_IN OMX_HANDLETYPE hComp)
4214{
4215    if(hComp == NULL)
4216    {
4217        DEBUG_PRINT_ERROR("Returning OMX_ErrorBadParameter\n");
4218        return OMX_ErrorBadParameter;
4219    }
4220    if (OMX_StateLoaded != m_state && OMX_StateInvalid != m_state)
4221    {
4222        DEBUG_PRINT_ERROR("Warning: Rxed DeInit when not in LOADED state %d\n",
4223            m_state);
4224    }
4225  deinit_encoder();
4226
4227DEBUG_PRINT_ERROR("%s:COMPONENT DEINIT...\n", __FUNCTION__);
4228  return OMX_ErrorNone;
4229}
4230
4231/* ======================================================================
4232FUNCTION
4233  omx_qcelp13_aenc::deinit_encoder
4234
4235DESCRIPTION
4236  Closes all the threads and release memory allocated to the heap.
4237
4238PARAMETERS
4239  None.
4240
4241RETURN VALUE
4242  None.
4243
4244========================================================================== */
4245void  omx_qcelp13_aenc::deinit_encoder()
4246{
4247    DEBUG_PRINT("Component-deinit being processed\n");
4248    DEBUG_PRINT("********************************\n");
4249    DEBUG_PRINT("STATS: in-buf-len[%u]out-buf-len[%u] tot-pb-time[%lld]",\
4250                m_qcelp13_pb_stats.tot_in_buf_len,
4251                m_qcelp13_pb_stats.tot_out_buf_len,
4252                m_qcelp13_pb_stats.tot_pb_time);
4253    DEBUG_PRINT("STATS: fbd-cnt[%u]ftb-cnt[%u]etb-cnt[%u]ebd-cnt[%u]",\
4254                m_qcelp13_pb_stats.fbd_cnt,m_qcelp13_pb_stats.ftb_cnt,
4255                m_qcelp13_pb_stats.etb_cnt,
4256                m_qcelp13_pb_stats.ebd_cnt);
4257   memset(&m_qcelp13_pb_stats,0,sizeof(QCELP13_PB_STATS));
4258
4259    if((OMX_StateLoaded != m_state) && (OMX_StateInvalid != m_state))
4260    {
4261        DEBUG_PRINT_ERROR("%s,Deinit called in state[%d]\n",__FUNCTION__,\
4262                                                                m_state);
4263        // Get back any buffers from driver
4264        if(pcm_input)
4265            execute_omx_flush(-1,false);
4266        else
4267            execute_omx_flush(1,false);
4268        // force state change to loaded so that all threads can be exited
4269        pthread_mutex_lock(&m_state_lock);
4270        m_state = OMX_StateLoaded;
4271        pthread_mutex_unlock(&m_state_lock);
4272        DEBUG_PRINT_ERROR("Freeing Buf:inp_current_buf_count[%d][%d]\n",\
4273        m_inp_current_buf_count,
4274        m_input_buf_hdrs.size());
4275        m_input_buf_hdrs.eraseall();
4276        DEBUG_PRINT_ERROR("Freeing Buf:out_current_buf_count[%d][%d]\n",\
4277        m_out_current_buf_count,
4278        m_output_buf_hdrs.size());
4279        m_output_buf_hdrs.eraseall();
4280
4281    }
4282    if(pcm_input)
4283    {
4284        pthread_mutex_lock(&m_in_th_lock_1);
4285        if (is_in_th_sleep)
4286        {
4287            is_in_th_sleep = false;
4288            DEBUG_DETAIL("Deinit:WAKING UP IN THREADS\n");
4289            in_th_wakeup();
4290        }
4291        pthread_mutex_unlock(&m_in_th_lock_1);
4292    }
4293    pthread_mutex_lock(&m_out_th_lock_1);
4294    if (is_out_th_sleep)
4295    {
4296        is_out_th_sleep = false;
4297        DEBUG_DETAIL("SCP:WAKING UP OUT THREADS\n");
4298        out_th_wakeup();
4299    }
4300    pthread_mutex_unlock(&m_out_th_lock_1);
4301    if(pcm_input)
4302    {
4303        if (m_ipc_to_in_th != NULL)
4304        {
4305            omx_qcelp13_thread_stop(m_ipc_to_in_th);
4306            m_ipc_to_in_th = NULL;
4307        }
4308    }
4309
4310    if (m_ipc_to_cmd_th != NULL)
4311    {
4312        omx_qcelp13_thread_stop(m_ipc_to_cmd_th);
4313        m_ipc_to_cmd_th = NULL;
4314    }
4315    if (m_ipc_to_out_th != NULL)
4316    {
4317         DEBUG_DETAIL("Inside omx_qcelp13_thread_stop\n");
4318        omx_qcelp13_thread_stop(m_ipc_to_out_th);
4319        m_ipc_to_out_th = NULL;
4320     }
4321
4322
4323    if(ioctl(m_drv_fd, AUDIO_STOP, 0) <0)
4324          DEBUG_PRINT_ERROR("De-init: AUDIO_STOP FAILED\n");
4325
4326    if(pcm_input && m_tmp_meta_buf )
4327    {
4328        free(m_tmp_meta_buf);
4329    }
4330
4331    if(m_tmp_out_meta_buf)
4332    {
4333        free(m_tmp_out_meta_buf);
4334    }
4335    nNumInputBuf = 0;
4336    nNumOutputBuf = 0;
4337    bFlushinprogress = 0;
4338
4339    m_inp_current_buf_count=0;
4340    m_out_current_buf_count=0;
4341    m_out_act_buf_count = 0;
4342    m_inp_act_buf_count = 0;
4343    m_inp_bEnabled = OMX_FALSE;
4344    m_out_bEnabled = OMX_FALSE;
4345    m_inp_bPopulated = OMX_FALSE;
4346    m_out_bPopulated = OMX_FALSE;
4347
4348    if ( m_drv_fd >= 0 )
4349    {
4350        if(close(m_drv_fd) < 0)
4351        DEBUG_PRINT("De-init: Driver Close Failed \n");
4352        m_drv_fd = -1;
4353    }
4354    else
4355    {
4356        DEBUG_PRINT_ERROR(" QCELP13 device already closed\n");
4357    }
4358    m_comp_deinit=1;
4359    m_is_out_th_sleep = 1;
4360    m_is_in_th_sleep = 1;
4361    DEBUG_PRINT("************************************\n");
4362    DEBUG_PRINT(" DEINIT COMPLETED");
4363    DEBUG_PRINT("************************************\n");
4364
4365}
4366
4367/* ======================================================================
4368FUNCTION
4369  omx_qcelp13_aenc::UseEGLImage
4370
4371DESCRIPTION
4372  OMX Use EGL Image method implementation <TBD>.
4373
4374PARAMETERS
4375  <TBD>.
4376
4377RETURN VALUE
4378  Not Implemented error.
4379
4380========================================================================== */
4381OMX_ERRORTYPE  omx_qcelp13_aenc::use_EGL_image
4382(
4383    OMX_IN OMX_HANDLETYPE                hComp,
4384    OMX_INOUT OMX_BUFFERHEADERTYPE** bufferHdr,
4385    OMX_IN OMX_U32                        port,
4386    OMX_IN OMX_PTR                     appData,
4387    OMX_IN void*                      eglImage)
4388{
4389    DEBUG_PRINT_ERROR("Error : use_EGL_image:  Not Implemented \n");
4390
4391    if((hComp == NULL) || (appData == NULL) || (eglImage == NULL))
4392    {
4393        bufferHdr = bufferHdr;
4394        port = port;
4395        DEBUG_PRINT_ERROR("Returning OMX_ErrorBadParameter\n");
4396        return OMX_ErrorBadParameter;
4397    }
4398    return OMX_ErrorNotImplemented;
4399}
4400
4401/* ======================================================================
4402FUNCTION
4403  omx_qcelp13_aenc::ComponentRoleEnum
4404
4405DESCRIPTION
4406  OMX Component Role Enum method implementation.
4407
4408PARAMETERS
4409  <TBD>.
4410
4411RETURN VALUE
4412  OMX Error None if everything is successful.
4413========================================================================== */
4414OMX_ERRORTYPE  omx_qcelp13_aenc::component_role_enum(
4415				OMX_IN OMX_HANDLETYPE hComp,
4416				OMX_OUT OMX_U8*        role,
4417				OMX_IN OMX_U32        index)
4418{
4419    OMX_ERRORTYPE eRet = OMX_ErrorNone;
4420    const char *cmp_role = "audio_encoder.qcelp13";
4421
4422    if(hComp == NULL)
4423    {
4424        DEBUG_PRINT_ERROR("Returning OMX_ErrorBadParameter\n");
4425        return OMX_ErrorBadParameter;
4426    }
4427    if (index == 0 && role)
4428    {
4429        memcpy(role, cmp_role, strlen(cmp_role));
4430        *(((char *) role) + strlen(cmp_role) + 1) = '\0';
4431    } else
4432    {
4433        eRet = OMX_ErrorNoMore;
4434    }
4435    return eRet;
4436}
4437
4438
4439
4440
4441/* ======================================================================
4442FUNCTION
4443  omx_qcelp13_aenc::AllocateDone
4444
4445DESCRIPTION
4446  Checks if entire buffer pool is allocated by IL Client or not.
4447  Need this to move to IDLE state.
4448
4449PARAMETERS
4450  None.
4451
4452RETURN VALUE
4453  true/false.
4454
4455========================================================================== */
4456bool omx_qcelp13_aenc::allocate_done(void)
4457{
4458    OMX_BOOL bRet = OMX_FALSE;
4459    if (pcm_input==1)
4460    {
4461        if ((m_inp_act_buf_count == m_inp_current_buf_count)
4462            &&(m_out_act_buf_count == m_out_current_buf_count))
4463        {
4464            bRet=OMX_TRUE;
4465
4466        }
4467        if ((m_inp_act_buf_count == m_inp_current_buf_count) && m_inp_bEnabled )
4468        {
4469            m_inp_bPopulated = OMX_TRUE;
4470        }
4471
4472        if ((m_out_act_buf_count == m_out_current_buf_count) && m_out_bEnabled )
4473        {
4474            m_out_bPopulated = OMX_TRUE;
4475        }
4476    } else if (pcm_input==0)
4477    {
4478        if (m_out_act_buf_count == m_out_current_buf_count)
4479        {
4480            bRet=OMX_TRUE;
4481
4482        }
4483        if ((m_out_act_buf_count == m_out_current_buf_count) && m_out_bEnabled )
4484        {
4485            m_out_bPopulated = OMX_TRUE;
4486        }
4487
4488    }
4489    return bRet;
4490}
4491
4492
4493/* ======================================================================
4494FUNCTION
4495  omx_qcelp13_aenc::ReleaseDone
4496
4497DESCRIPTION
4498  Checks if IL client has released all the buffers.
4499
4500PARAMETERS
4501  None.
4502
4503RETURN VALUE
4504  true/false
4505
4506========================================================================== */
4507bool omx_qcelp13_aenc::release_done(OMX_U32 param1)
4508{
4509    DEBUG_PRINT("Inside omx_qcelp13_aenc::release_done");
4510    OMX_BOOL bRet = OMX_FALSE;
4511
4512    if (param1 == OMX_ALL)
4513    {
4514        if ((0 == m_inp_current_buf_count)&&(0 == m_out_current_buf_count))
4515        {
4516            bRet=OMX_TRUE;
4517        }
4518    } else if (param1 == OMX_CORE_INPUT_PORT_INDEX )
4519    {
4520        if ((0 == m_inp_current_buf_count))
4521        {
4522            bRet=OMX_TRUE;
4523        }
4524    } else if (param1 == OMX_CORE_OUTPUT_PORT_INDEX)
4525    {
4526        if ((0 == m_out_current_buf_count))
4527        {
4528            bRet=OMX_TRUE;
4529        }
4530    }
4531    return bRet;
4532}
4533