1 /*
2  * Copyright (C) 2021 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.video.cts;
18 
19 import android.app.Activity;
20 import android.os.Bundle;
21 import android.os.SystemClock;
22 import android.util.Log;
23 import android.view.Surface;
24 import android.view.SurfaceHolder;
25 import android.view.SurfaceView;
26 
27 import java.util.concurrent.TimeUnit;
28 import java.util.concurrent.locks.Condition;
29 import java.util.concurrent.locks.Lock;
30 import java.util.concurrent.locks.ReentrantLock;
31 
32 public class CodecTestActivity extends Activity implements SurfaceHolder.Callback {
33     private static final String LOG_TAG = CodecTestActivity.class.getSimpleName();
34     private SurfaceView mSurfaceView;
35     private SurfaceHolder mHolder;
36     private Surface mSurface;
37     private final Lock mLock = new ReentrantLock();
38     private final Condition mCondition = mLock.newCondition();
39 
40     @Override
onCreate(Bundle savedInstanceState)41     protected void onCreate(Bundle savedInstanceState) {
42         super.onCreate(savedInstanceState);
43         setContentView(R.layout.media_decoder_surface_layout);
44         mSurfaceView = findViewById(R.id.surface);
45         mHolder = mSurfaceView.getHolder();
46         mHolder.addCallback(this);
47     }
48 
49     @Override
surfaceCreated(SurfaceHolder holder)50     public void surfaceCreated(SurfaceHolder holder) {
51         Log.v(LOG_TAG, "surface created");
52         mLock.lock();
53         mSurface = mHolder.getSurface();
54         mLock.unlock();
55     }
56 
57     @Override
surfaceChanged(SurfaceHolder holder, int format, int width, int height)58     public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
59         Log.v(LOG_TAG, "surface changed " + format + " " + width + " " + height);
60     }
61 
62     @Override
surfaceDestroyed(SurfaceHolder holder)63     public void surfaceDestroyed(SurfaceHolder holder) {
64         Log.v(LOG_TAG, "surface deleted");
65         mLock.lock();
66         mSurface = null;
67         mLock.unlock();
68     }
69 
waitTillSurfaceIsCreated()70     public void waitTillSurfaceIsCreated() throws InterruptedException {
71         final long mWaitTimeMs = 1000;
72         final int retries = 3;
73         mLock.lock();
74         final long start = SystemClock.elapsedRealtime();
75         while ((SystemClock.elapsedRealtime() - start) < (retries * mWaitTimeMs) &&
76                 mSurface == null) {
77             mCondition.await(mWaitTimeMs, TimeUnit.MILLISECONDS);
78         }
79         mLock.unlock();
80         if (mSurface == null) {
81             throw new InterruptedException("Taking too long to attach a SurfaceView to a window.");
82         }
83     }
84 
getSurface()85     public Surface getSurface() { return mSurface; }
86 }
87