Home | History | Annotate | Download | only in media
      1 /*
      2  * Copyright (C) 2014 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 package android.media;
     18 
     19 import android.os.Handler;
     20 import android.os.Looper;
     21 import android.os.Message;
     22 import android.util.Log;
     23 
     24 import java.util.ArrayList;
     25 import java.lang.ref.WeakReference;
     26 
     27 /**
     28  * The AudioPortEventHandler handles AudioManager.OnAudioPortUpdateListener callbacks
     29  * posted from JNI
     30  * @hide
     31  */
     32 
     33 class AudioPortEventHandler {
     34     private Handler mHandler;
     35     private final ArrayList<AudioManager.OnAudioPortUpdateListener> mListeners =
     36             new ArrayList<AudioManager.OnAudioPortUpdateListener>();
     37 
     38     private static final String TAG = "AudioPortEventHandler";
     39 
     40     private static final int AUDIOPORT_EVENT_PORT_LIST_UPDATED = 1;
     41     private static final int AUDIOPORT_EVENT_PATCH_LIST_UPDATED = 2;
     42     private static final int AUDIOPORT_EVENT_SERVICE_DIED = 3;
     43     private static final int AUDIOPORT_EVENT_NEW_LISTENER = 4;
     44 
     45     void init() {
     46         synchronized (this) {
     47             if (mHandler != null) {
     48                 return;
     49             }
     50             // find the looper for our new event handler
     51             Looper looper = Looper.getMainLooper();
     52 
     53             if (looper != null) {
     54                 mHandler = new Handler(looper) {
     55                     @Override
     56                     public void handleMessage(Message msg) {
     57                         ArrayList<AudioManager.OnAudioPortUpdateListener> listeners;
     58                         synchronized (this) {
     59                             if (msg.what == AUDIOPORT_EVENT_NEW_LISTENER) {
     60                                 listeners = new ArrayList<AudioManager.OnAudioPortUpdateListener>();
     61                                 if (mListeners.contains(msg.obj)) {
     62                                     listeners.add((AudioManager.OnAudioPortUpdateListener)msg.obj);
     63                                 }
     64                             } else {
     65                                 listeners = mListeners;
     66                             }
     67                         }
     68                         if (listeners.isEmpty()) {
     69                             return;
     70                         }
     71                         // reset audio port cache if the event corresponds to a change coming
     72                         // from audio policy service or if mediaserver process died.
     73                         if (msg.what == AUDIOPORT_EVENT_PORT_LIST_UPDATED ||
     74                                 msg.what == AUDIOPORT_EVENT_PATCH_LIST_UPDATED ||
     75                                 msg.what == AUDIOPORT_EVENT_SERVICE_DIED) {
     76                             AudioManager.resetAudioPortGeneration();
     77                         }
     78                         ArrayList<AudioPort> ports = new ArrayList<AudioPort>();
     79                         ArrayList<AudioPatch> patches = new ArrayList<AudioPatch>();
     80                         if (msg.what != AUDIOPORT_EVENT_SERVICE_DIED) {
     81                             int status = AudioManager.updateAudioPortCache(ports, patches);
     82                             if (status != AudioManager.SUCCESS) {
     83                                 return;
     84                             }
     85                         }
     86 
     87                         switch (msg.what) {
     88                         case AUDIOPORT_EVENT_NEW_LISTENER:
     89                         case AUDIOPORT_EVENT_PORT_LIST_UPDATED:
     90                             AudioPort[] portList = ports.toArray(new AudioPort[0]);
     91                             for (int i = 0; i < listeners.size(); i++) {
     92                                 listeners.get(i).onAudioPortListUpdate(portList);
     93                             }
     94                             if (msg.what == AUDIOPORT_EVENT_PORT_LIST_UPDATED) {
     95                                 break;
     96                             }
     97                             // FALL THROUGH
     98 
     99                         case AUDIOPORT_EVENT_PATCH_LIST_UPDATED:
    100                             AudioPatch[] patchList = patches.toArray(new AudioPatch[0]);
    101                             for (int i = 0; i < listeners.size(); i++) {
    102                                 listeners.get(i).onAudioPatchListUpdate(patchList);
    103                             }
    104                             break;
    105 
    106                         case AUDIOPORT_EVENT_SERVICE_DIED:
    107                             for (int i = 0; i < listeners.size(); i++) {
    108                                 listeners.get(i).onServiceDied();
    109                             }
    110                             break;
    111 
    112                         default:
    113                             break;
    114                         }
    115                     }
    116                 };
    117                 native_setup(new WeakReference<AudioPortEventHandler>(this));
    118             } else {
    119                 mHandler = null;
    120             }
    121         }
    122     }
    123 
    124     private native void native_setup(Object module_this);
    125 
    126     @Override
    127     protected void finalize() {
    128         native_finalize();
    129     }
    130     private native void native_finalize();
    131 
    132     void registerListener(AudioManager.OnAudioPortUpdateListener l) {
    133         synchronized (this) {
    134             mListeners.add(l);
    135         }
    136         if (mHandler != null) {
    137             Message m = mHandler.obtainMessage(AUDIOPORT_EVENT_NEW_LISTENER, 0, 0, l);
    138             mHandler.sendMessage(m);
    139         }
    140     }
    141 
    142     void unregisterListener(AudioManager.OnAudioPortUpdateListener l) {
    143         synchronized (this) {
    144             mListeners.remove(l);
    145         }
    146     }
    147 
    148     Handler handler() {
    149         return mHandler;
    150     }
    151 
    152     @SuppressWarnings("unused")
    153     private static void postEventFromNative(Object module_ref,
    154                                             int what, int arg1, int arg2, Object obj) {
    155         AudioPortEventHandler eventHandler =
    156                 (AudioPortEventHandler)((WeakReference)module_ref).get();
    157         if (eventHandler == null) {
    158             return;
    159         }
    160 
    161         if (eventHandler != null) {
    162             Handler handler = eventHandler.handler();
    163             if (handler != null) {
    164                 Message m = handler.obtainMessage(what, arg1, arg2, obj);
    165                 handler.sendMessage(m);
    166             }
    167         }
    168     }
    169 
    170 }
    171