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
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.BufferedReader;
32import java.io.BufferedWriter;
33import java.io.File;
34import java.io.FileReader;
35import java.io.FileWriter;
36import java.io.Writer;
37
38import java.util.ArrayList;
39import java.util.List;
40
41/**
42 * Junit / Instrumentation test case for the media player
43 */
44public class MediaPlayerStreamingStressTest extends ActivityInstrumentationTestCase2<MediaFrameworkTest> {
45    private String TAG = "MediaPlayerStreamingStressTest";
46    private String mStreamingSrc;
47
48    public MediaPlayerStreamingStressTest() {
49        super("com.android.mediaframeworktest", MediaFrameworkTest.class);
50    }
51
52    protected void setUp() throws Exception {
53        //Insert a 2 second before launching the test activity. This is
54        //the workaround for the race condition of requesting the updated surface.
55        Thread.sleep(2000);
56        getActivity();
57        MediaPlayerStressTestRunner mRunner = (MediaPlayerStressTestRunner)getInstrumentation();
58        Bundle arguments = mRunner.getArguments();
59        mStreamingSrc = arguments.getString("streaming-source");
60        if (mStreamingSrc == null) {
61            mStreamingSrc = MediaNames.MEDIA_STREAMING_SRC;
62        }
63        super.setUp();
64    }
65
66    private int mTotalPlaybackError = 0;
67    private int mTotalComplete = 0;
68    private int mTotalInfoUnknown = 0;
69    private int mTotalVideoTrackLagging = 0;
70    private int mTotalBadInterleaving = 0;
71    private int mTotalNotSeekable = 0;
72    private int mTotalMetaDataUpdate = 0;
73
74    //Test result output file
75    private static final String PLAYBACK_RESULT = "StreamingTestResult.txt";
76
77    private void writeTestOutput(String filename, Writer output) throws Exception{
78        output.write("URL: " + filename);
79        output.write(" Complete: " + CodecTest.onCompleteSuccess);
80        output.write(" Error: " + CodecTest.mPlaybackError);
81        output.write(" Unknown Info: " + CodecTest.mMediaInfoUnknownCount);
82        output.write(" Track Lagging: " +  CodecTest.mMediaInfoVideoTrackLaggingCount);
83        output.write(" Bad Interleaving: " + CodecTest.mMediaInfoBadInterleavingCount);
84        output.write(" Not Seekable: " + CodecTest.mMediaInfoNotSeekableCount);
85        output.write(" Info Meta data update: " + CodecTest.mMediaInfoMetdataUpdateCount);
86        output.write("\n");
87    }
88
89    private void writeTestSummary(Writer output) throws Exception{
90        output.write("Total Result:\n");
91        output.write("Total Complete: " + mTotalComplete + "\n");
92        output.write("Total Error: " + mTotalPlaybackError + "\n");
93        output.write("Total Unknown Info: " + mTotalInfoUnknown + "\n");
94        output.write("Total Track Lagging: " + mTotalVideoTrackLagging + "\n" );
95        output.write("Total Bad Interleaving: " + mTotalBadInterleaving + "\n");
96        output.write("Total Not Seekable: " + mTotalNotSeekable + "\n");
97        output.write("Total Info Meta data update: " + mTotalMetaDataUpdate + "\n");
98        output.write("\n");
99    }
100
101    private void updateTestResult(){
102        if (CodecTest.onCompleteSuccess){
103            mTotalComplete++;
104        }
105        else if (CodecTest.mPlaybackError){
106            mTotalPlaybackError++;
107        }
108        mTotalInfoUnknown += CodecTest.mMediaInfoUnknownCount;
109        mTotalVideoTrackLagging += CodecTest.mMediaInfoVideoTrackLaggingCount;
110        mTotalBadInterleaving += CodecTest.mMediaInfoBadInterleavingCount;
111        mTotalNotSeekable += CodecTest.mMediaInfoNotSeekableCount;
112        mTotalMetaDataUpdate += CodecTest.mMediaInfoMetdataUpdateCount;
113    }
114
115    //Test that will start the playback for all the videos
116    //under the samples folder
117    @LargeTest
118    public void testVideoPlayback() throws Exception {
119        String fileWithError = "Filename:\n";
120        File playbackOutput = new File(Environment.getExternalStorageDirectory(), PLAYBACK_RESULT);
121        Writer output = new BufferedWriter(new FileWriter(playbackOutput, true));
122
123        boolean testResult = true;
124        // load directory files
125        boolean onCompleteSuccess = false;
126
127
128        Log.i(TAG, "Streaming src file: " + mStreamingSrc);
129        //TODO: add try catch
130
131        File f = new File(mStreamingSrc);
132        BufferedReader br = new BufferedReader(new FileReader(f));
133        List<String> urls = new ArrayList<String>();
134        String line;
135        while ((line = br.readLine()) != null) {
136           urls.add(line.trim());
137        }
138        br.close();
139        if (urls == null) {
140            Log.v("MediaPlayerStreamingTest:testVideoPlayback", "no url found");
141            return;
142        } else {
143            for (int i = 0; i < urls.size(); i++) {
144                //Get url
145                String filename = urls.get(i);
146                onCompleteSuccess =
147                    CodecTest.playMediaSamples(filename, 60000);
148                if (!onCompleteSuccess){
149                    //Don't fail the test right away, print out the failure file.
150                    fileWithError += filename + '\n';
151                    Log.v(TAG, "Failure File : " + fileWithError);
152                    testResult = false;
153                }
154                Thread.sleep(3000);
155                //Write test result to an output file
156                writeTestOutput(filename,output);
157                //Get the summary
158                updateTestResult();
159            }
160            writeTestSummary(output);
161            output.close();
162            assertTrue("testMediaSamples", testResult);
163       }
164    }
165}
166