summaryrefslogtreecommitdiff
path: root/apps/CtsVerifier/src/com/android/cts/verifier/tunnelmode/MediaCodecFlushActivity.java
blob: 055f26f7af6be9fb6892861e6b31b6afa2454ab8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package com.android.cts.verifier.tunnelmode;

import android.media.AudioManager;
import android.media.cts.MediaCodecTunneledPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import com.android.cts.verifier.PassFailButtons;
import com.android.cts.verifier.R;

import java.io.File;

/**
 * Test for verifying tunnel mode implementations properly handle content flushing. Plays a stream
 * in tunnel mode, pause it, flush it, resume, and user can mark Pass/Fail depending on quality of
 * the AV Sync. More details in go/atv-tunnel-mode-s.
 * TODO: Implement the actual test. This is a placeholder implementation until the test design is
 * stable and approved.
 */
public class MediaCodecFlushActivity extends PassFailButtons.Activity {
    private static final String TAG = MediaCodecFlushActivity.class.getSimpleName();

    private SurfaceHolder mHolder;
    private int mAudioSessionId = 0;
    private MediaCodecTunneledPlayer mPlayer;
    private Handler mHandler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sv_play);
        setPassFailButtonClickListeners();
        disablePassButton();

        SurfaceView surfaceView = (SurfaceView) findViewById(R.id.surface);
        mHolder = surfaceView.getHolder();
        mHolder.addCallback(new SurfaceHolder.Callback(){
                public void surfaceCreated(SurfaceHolder holder) {
                    // TODO: Implement a start button, rather than playing the video as soon as the
                    // surface is ready
                    playVideo();
                }

                public void surfaceChanged(
                        SurfaceHolder holder, int format, int width, int height) {}
                public void surfaceDestroyed(SurfaceHolder holder) {}
            });

        mHandler = new Handler(Looper.getMainLooper());

        AudioManager am = (AudioManager) getApplicationContext().getSystemService(AUDIO_SERVICE);
        mAudioSessionId = am.generateAudioSessionId();

        mPlayer = new MediaCodecTunneledPlayer(this, mHolder, true, mAudioSessionId);

        // TODO: Do not rely on the video being pre-loaded on the device
        Uri mediaUri = Uri.fromFile(new File("/data/local/tmp/video.webm"));
        mPlayer.setVideoDataSource(mediaUri, null);
        mPlayer.setAudioDataSource(mediaUri, null);
    }

    private void playVideo() {
        try {
            mPlayer.start();
            mPlayer.prepare();
            mPlayer.startThread();
            mHandler.postDelayed(this::pauseStep, 5000);
        } catch(Exception e) {
            Log.d(TAG, "Could not play video", e);
        }
    }

    private void pauseStep() {
        try {
            mPlayer.pause();
            mHandler.postDelayed(this::flushStep, 3000);
        } catch(Exception e) {
            Log.d(TAG, "Could not pause video", e);
        }
    }

    private void flushStep() {
        try {
            mPlayer.flush();
            mHandler.postDelayed(this::resumeStep, 3000);
        } catch(Exception e) {
            Log.d(TAG, "Could not flush video", e);
        }
    }

    private void resumeStep() {
        try {
            mPlayer.start();
            mHandler.postDelayed(this::enablePassButton, 3000);
        } catch(Exception e) {
            Log.d(TAG, "Could not resume video", e);
        }
    }

    private void enablePassButton() {
        getPassButton().setEnabled(true);
    }

    private void disablePassButton() {
        getPassButton().setEnabled(false);
    }

    @Override
    public void onPause() {
        super.onPause();
        if (mPlayer != null) {
            mPlayer.pause();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mPlayer != null) {
            mPlayer.reset();
            mPlayer = null;
        }
    }
}