Home | History | Annotate | Download | only in android
      1 /*
      2  * Copyright (c) 2008, The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *     http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #include "android_audio_output_threadsafe_callbacks.h"
     18 #include "android_audio_output.h"
     19 
     20 AndroidAudioOutputThreadSafeCallbackAO::AndroidAudioOutputThreadSafeCallbackAO(void* aObserver,
     21                                                                                uint32 aDepth,
     22                                                                                const char* aAOname,
     23                                                                                int32 aPriority)
     24 :ThreadSafeCallbackAO(aObserver, aDepth, aAOname, aPriority)
     25 {
     26 
     27 }
     28 
     29 
     30 AndroidAudioOutputThreadSafeCallbackAO::~AndroidAudioOutputThreadSafeCallbackAO()
     31 {
     32 
     33 }
     34 
     35 
     36 OsclReturnCode AndroidAudioOutputThreadSafeCallbackAO::ProcessEvent(OsclAny* EventData)
     37 {
     38     // In this case, ProcessEvent calls the method of the primary test AO to process the Event
     39     if (iObserver != NULL)
     40     {
     41         AndroidAudioOutput* ptr = (AndroidAudioOutput*) iObserver;
     42         // Call RunIfNotReady() for the AudioMIO
     43     if(ptr->IsAdded())
     44     {
     45         ptr->RunIfNotReady();
     46     }
     47     }
     48     return OsclSuccess;
     49 }
     50 
     51 #if PROCESS_MULTIPLE_EVENTS_IN_CALLBACK
     52 void AndroidAudioOutputThreadSafeCallbackAO::Run()
     53 {
     54     OsclAny *param;
     55     OsclReturnCode status = OsclSuccess;
     56     // Process multiple events in one attempt
     57     do
     58     {
     59         param = Dequeue(status);
     60         if((status == OsclSuccess) || (status == OsclPending))
     61         {
     62             ProcessEvent(param);
     63         }
     64     }while(status == OsclSuccess);
     65 }
     66 
     67 OsclAny* AndroidAudioOutputThreadSafeCallbackAO::Dequeue(OsclReturnCode &status)
     68 {
     69     OsclAny *param;
     70     OsclProcStatus::eOsclProcError sema_status;
     71 
     72     status = OsclSuccess;
     73 
     74     Mutex.Lock();
     75     if(Q->NumElem == 0)
     76     {
     77         status = OsclFailure;
     78         Mutex.Unlock();
     79         return NULL;
     80     }
     81 
     82     param = (Q->pFirst[Q->index_out]).pData;
     83     Q->index_out++;
     84     if(Q->index_out == Q->MaxNumElements)
     85         Q->index_out = 0;
     86     Q->NumElem--;
     87     if(Q->NumElem == 0)
     88     {
     89         // Call PendForExec() only when there are no more elements in the queue
     90         // Else, continue in the do-while loop
     91         PendForExec();
     92         status = OsclPending;
     93     }
     94     Mutex.Unlock();
     95 
     96     sema_status = RemoteThreadCtrlSema.Signal();
     97     if(sema_status != OsclProcStatus::SUCCESS_ERROR)
     98     {
     99         status = OsclFailure;
    100         return NULL;
    101     }
    102     return param;
    103 }
    104 #endif
    105 
    106 
    107