Home | History | Annotate | Download | only in cts
      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.cts;
     18 
     19 import java.util.concurrent.Semaphore;
     20 import java.util.concurrent.TimeUnit;
     21 
     22 import android.content.Context;
     23 import android.graphics.SurfaceTexture;
     24 import android.util.AttributeSet;
     25 import android.util.Log;
     26 import android.view.Surface;
     27 import android.view.TextureView;
     28 
     29 public class CompositionTextureView extends TextureView
     30     implements TextureView.SurfaceTextureListener {
     31     private static final String TAG = "CompositionTextureView";
     32     private static final boolean DBG = true;
     33     private static final boolean DBG_VERBOSE = false;
     34 
     35     private final Semaphore mInitWaitSemaphore = new Semaphore(0);
     36     private Surface mSurface;
     37 
     38     public CompositionTextureView(Context context) {
     39         super(context);
     40         if (DBG) {
     41             Log.i(TAG, "CompositionTextureView");
     42         }
     43     }
     44 
     45     public CompositionTextureView(Context context, AttributeSet attrs) {
     46         super(context, attrs);
     47         if (DBG) {
     48             Log.i(TAG, "CompositionTextureView");
     49         }
     50     }
     51 
     52     public CompositionTextureView(Context context, AttributeSet attrs, int defStyle) {
     53         super(context, attrs, defStyle);
     54         if (DBG) {
     55             Log.i(TAG, "CompositionTextureView");
     56         }
     57     }
     58 
     59     public void startListening() {
     60         if (DBG) {
     61             Log.i(TAG, "startListening");
     62         }
     63         setSurfaceTextureListener(this);
     64     }
     65 
     66     @Override
     67     public void onSurfaceTextureAvailable(SurfaceTexture arg0, int arg1, int arg2) {
     68         if (DBG) {
     69             Log.i(TAG, "onSurfaceTextureAvailable");
     70         }
     71         recreateSurface(arg0);
     72         mInitWaitSemaphore.release();
     73     }
     74 
     75     @Override
     76     public boolean onSurfaceTextureDestroyed(SurfaceTexture arg0) {
     77         if (DBG) {
     78             Log.i(TAG, "onSurfaceTextureDestroyed");
     79         }
     80         return true;
     81     }
     82 
     83     @Override
     84     public void onSurfaceTextureSizeChanged(SurfaceTexture arg0, int arg1, int arg2) {
     85         if (DBG) {
     86             Log.i(TAG, "onSurfaceTextureSizeChanged");
     87         }
     88         recreateSurface(arg0);
     89         mInitWaitSemaphore.release();
     90     }
     91 
     92     @Override
     93     public void onSurfaceTextureUpdated(SurfaceTexture arg0) {
     94         if (DBG_VERBOSE) {
     95             Log.i(TAG, "onSurfaceTextureUpdated");
     96         }
     97         //ignore
     98     }
     99 
    100     public boolean waitForSurfaceReady(long timeoutMs) throws Exception {
    101         if (isSurfaceTextureAvailable()) {
    102             return true;
    103         }
    104         if (mInitWaitSemaphore.tryAcquire(timeoutMs, TimeUnit.MILLISECONDS)) {
    105             return true;
    106         }
    107         return isSurfaceTextureAvailable();
    108     }
    109 
    110     private boolean isSurfaceTextureAvailable() {
    111         SurfaceTexture surfaceTexture = getSurfaceTexture();
    112         if (mSurface == null && surfaceTexture != null) {
    113             recreateSurface(surfaceTexture);
    114         }
    115         return (mSurface != null);
    116     }
    117 
    118     private synchronized void recreateSurface(SurfaceTexture surfaceTexture) {
    119         if (mSurface != null) {
    120             mSurface.release();
    121         }
    122         mSurface = new Surface(surfaceTexture);
    123     }
    124 
    125     public Surface getSurface() {
    126         return mSurface;
    127     }
    128 
    129 }
    130