1/*
2 * Copyright (C) 2009 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
17package com.android.mediaframeworktest.stress;
18
19import com.android.mediaframeworktest.MediaFrameworkTest;
20import com.android.mediaframeworktest.MediaPlayerStressTestRunner;
21
22import android.os.Bundle;
23import android.os.Environment;
24import android.test.ActivityInstrumentationTestCase2;
25import android.test.suitebuilder.annotation.LargeTest;
26import android.util.Log;
27
28import com.android.mediaframeworktest.MediaNames;
29import com.android.mediaframeworktest.functional.CodecTest;
30
31import java.io.BufferedWriter;
32import java.io.File;
33import java.io.FileWriter;
34import java.io.Writer;
35
36/**
37 * Junit / Instrumentation test case for the media player
38 */
39public class MediaPlayerStressTest extends ActivityInstrumentationTestCase2<MediaFrameworkTest> {
40    private String TAG = "MediaPlayerStressTest";
41    private String mMediaSrc;
42
43    public MediaPlayerStressTest() {
44        super("com.android.mediaframeworktest", MediaFrameworkTest.class);
45    }
46
47    protected void setUp() throws Exception {
48        //Insert a 2 second before launching the test activity. This is
49        //the workaround for the race condition of requesting the updated surface.
50        Thread.sleep(2000);
51        getActivity();
52        MediaPlayerStressTestRunner mRunner = (MediaPlayerStressTestRunner)getInstrumentation();
53        Bundle arguments = mRunner.getArguments();
54        mMediaSrc = arguments.getString("media-source");
55        if (mMediaSrc == null) {
56            mMediaSrc = MediaNames.MEDIA_SAMPLE_POOL;
57        }
58        super.setUp();
59    }
60
61    private int mTotalPlaybackError = 0;
62    private int mTotalComplete = 0;
63    private int mTotalInfoUnknown = 0;
64    private int mTotalVideoTrackLagging = 0;
65    private int mTotalBadInterleaving = 0;
66    private int mTotalNotSeekable = 0;
67    private int mTotalMetaDataUpdate = 0;
68
69    //Test result output file
70    private static final String PLAYBACK_RESULT = "PlaybackTestResult.txt";
71
72    private void writeTestOutput(String filename, Writer output) throws Exception{
73        output.write("File Name: " + filename);
74        output.write(" Complete: " + CodecTest.onCompleteSuccess);
75        output.write(" Error: " + CodecTest.mPlaybackError);
76        output.write(" Unknown Info: " + CodecTest.mMediaInfoUnknownCount);
77        output.write(" Track Lagging: " +  CodecTest.mMediaInfoVideoTrackLaggingCount);
78        output.write(" Bad Interleaving: " + CodecTest.mMediaInfoBadInterleavingCount);
79        output.write(" Not Seekable: " + CodecTest.mMediaInfoNotSeekableCount);
80        output.write(" Info Meta data update: " + CodecTest.mMediaInfoMetdataUpdateCount);
81        output.write("\n");
82    }
83
84    private void writeTestSummary(Writer output) throws Exception{
85        output.write("Total Result:\n");
86        output.write("Total Complete: " + mTotalComplete + "\n");
87        output.write("Total Error: " + mTotalPlaybackError + "\n");
88        output.write("Total Unknown Info: " + mTotalInfoUnknown + "\n");
89        output.write("Total Track Lagging: " + mTotalVideoTrackLagging + "\n" );
90        output.write("Total Bad Interleaving: " + mTotalBadInterleaving + "\n");
91        output.write("Total Not Seekable: " + mTotalNotSeekable + "\n");
92        output.write("Total Info Meta data update: " + mTotalMetaDataUpdate + "\n");
93        output.write("\n");
94    }
95
96    private void updateTestResult(){
97        if (CodecTest.onCompleteSuccess){
98            mTotalComplete++;
99        }
100        else if (CodecTest.mPlaybackError){
101            mTotalPlaybackError++;
102        }
103        mTotalInfoUnknown += CodecTest.mMediaInfoUnknownCount;
104        mTotalVideoTrackLagging += CodecTest.mMediaInfoVideoTrackLaggingCount;
105        mTotalBadInterleaving += CodecTest.mMediaInfoBadInterleavingCount;
106        mTotalNotSeekable += CodecTest.mMediaInfoNotSeekableCount;
107        mTotalMetaDataUpdate += CodecTest.mMediaInfoMetdataUpdateCount;
108    }
109
110    //Test that will start the playback for all the videos
111    //under the samples folder
112    @LargeTest
113    public void testVideoPlayback() throws Exception {
114        String fileWithError = "Filename:\n";
115        File playbackOutput = new File(Environment.getExternalStorageDirectory(), PLAYBACK_RESULT);
116        Writer output = new BufferedWriter(new FileWriter(playbackOutput, true));
117
118        boolean testResult = true;
119        // load directory files
120        boolean onCompleteSuccess = false;
121        String[] children = MediaNames.NETWORK_VIDEO_FILES;
122        if (MediaNames.MEDIA_SAMPLE_POOL.equals(mMediaSrc)) {
123            File dir = new File(mMediaSrc);
124            children = dir.list();
125        }
126        if (children == null) {
127            Log.v("MediaPlayerApiTest:testMediaSamples", "dir is empty");
128            return;
129        } else {
130            for (int i = 0; i < children.length; i++) {
131                //Get filename
132                String filename = children[i];
133                onCompleteSuccess =
134                    CodecTest.playMediaSamples(mMediaSrc + filename);
135                if (!onCompleteSuccess){
136                    //Don't fail the test right away, print out the failure file.
137                    fileWithError += filename + '\n';
138                    Log.v(TAG, "Failure File : " + fileWithError);
139                    testResult = false;
140                }
141                Thread.sleep(3000);
142                //Write test result to an output file
143                writeTestOutput(filename,output);
144                //Get the summary
145                updateTestResult();
146            }
147            writeTestSummary(output);
148            output.close();
149            assertTrue("testMediaSamples", testResult);
150       }
151    }
152}