2222
2323import static org .junit .Assert .assertEquals ;
2424import static org .junit .Assert .assertFalse ;
25- import static org .junit .Assert .assertTrue ;
2625import static org .junit .Assert .fail ;
26+ import static org .junit .Assume .assumeTrue ;
2727
2828import android .media .MediaCodec ;
2929import android .media .MediaCodecInfo ;
5454import java .util .List ;
5555import java .util .Map ;
5656import java .util .Random ;
57- import java .util .concurrent .ExecutorService ;
58- import java .util .concurrent .Executors ;
59- import java .util .concurrent .TimeUnit ;
6057
6158import org .junit .Test ;
6259import org .junit .runner .RunWith ;
@@ -73,6 +70,7 @@ public class EncoderTest {
7370 static final String mInpPrefix = WorkDir .getMediaDirString ();
7471 private static final int kNumInputBytes = 512 * 1024 ;
7572 private static final long kTimeoutUs = 100 ;
73+ public static final int PER_TEST_TIMEOUT_SMALL_TEST_MS = 60000 ;
7674
7775 // not all combinations are valid
7876 private static final int MODE_SILENT = 0 ;
@@ -95,7 +93,7 @@ public class EncoderTest {
9593 private final int mBitrate ;
9694 private final int mSampleRate ;
9795 private final int mChannelCount ;
98- private ArrayList < MediaFormat > mFormats ;
96+ private final MediaFormat mFormat = new MediaFormat () ;
9997
10098 static boolean isDefaultCodec (String codecName , String mime )
10199 throws IOException {
@@ -187,78 +185,26 @@ public EncoderTest(String encodername, String mime, int profile, int bitrate,
187185 mChannelCount = channelcount ;
188186 }
189187
190- private void setUpFormats () {
191- mFormats = new ArrayList <MediaFormat >();
192- MediaFormat format = new MediaFormat ();
193- format .setString (MediaFormat .KEY_MIME , mMime );
194- format .setInteger (MediaFormat .KEY_SAMPLE_RATE , mSampleRate );
195- format .setInteger (MediaFormat .KEY_CHANNEL_COUNT , mChannelCount );
188+ private void setUpFormat () {
189+ mFormat .setString (MediaFormat .KEY_MIME , mMime );
190+ mFormat .setInteger (MediaFormat .KEY_SAMPLE_RATE , mSampleRate );
191+ mFormat .setInteger (MediaFormat .KEY_CHANNEL_COUNT , mChannelCount );
196192 if (mProfile >= 0 ) {
197- format .setInteger (MediaFormat .KEY_PROFILE , mProfile );
193+ mFormat .setInteger (MediaFormat .KEY_PROFILE , mProfile );
198194 if (mMime .equals (MediaFormat .MIMETYPE_AUDIO_AAC )) {
199- format .setInteger (MediaFormat .KEY_AAC_PROFILE , mProfile );
195+ mFormat .setInteger (MediaFormat .KEY_AAC_PROFILE , mProfile );
200196 }
201197 }
202- format .setInteger (MediaFormat .KEY_BIT_RATE , mBitrate );
203- mFormats .add (format );
198+ mFormat .setInteger (MediaFormat .KEY_BIT_RATE , mBitrate );
204199 }
205200
206- @ Test
207- public void testEncoders () {
208- setUpFormats ();
209- testEncoderWithFormats ();
210- }
211-
212- private void testEncoderWithFormatsParallel (String mime , ArrayList <MediaFormat > formats ,
213- String componentName , int ThreadCount ) {
214- int testsStarted = 0 ;
215- int totalDurationSeconds = 0 ;
216- ExecutorService pool = Executors .newFixedThreadPool (ThreadCount );
217-
218- for (MediaFormat format : formats ) {
219- assertEquals (mime , format .getString (MediaFormat .KEY_MIME ));
220- pool .execute (new EncoderRun (componentName , format ));
221- int sampleRate = format .getInteger (MediaFormat .KEY_SAMPLE_RATE );
222- int channelCount = format .getInteger (MediaFormat .KEY_CHANNEL_COUNT );
223- int bytesQueuedPerSecond = 2 * channelCount * sampleRate ;
224- int durationSeconds =
225- (kNumInputBytes + bytesQueuedPerSecond - 1 ) / bytesQueuedPerSecond ;
226- totalDurationSeconds += durationSeconds * kNumEncoderTestsPerRun ;
227- testsStarted ++;
228- }
229- try {
230- pool .shutdown ();
231- Log .i (TAG , "waiting up to " + totalDurationSeconds + " seconds for "
232- + testsStarted + " sub-tests to finish" );
233- assertTrue ("timed out waiting for encoder threads" ,
234- pool .awaitTermination (totalDurationSeconds , TimeUnit .SECONDS ));
235- } catch (InterruptedException e ) {
236- fail ("interrupted while waiting for encoder threads" );
237- }
238- }
239-
240- private void testEncoderWithFormats () {
241- for (MediaFormat fmt : mFormats ) {
242- if (!MediaUtils .supports (mEncoderName , fmt )) {
243- MediaUtils .skipTest ("no encoders found for " + fmt .toString ());
244- return ;
245- }
246- }
247- final int ThreadPoolCount = 1 ;
248- int instances = ThreadPoolCount ;
249- MediaCodec codec = null ;
250- try {
251- codec = MediaCodec .createByCodecName (mEncoderName );
252- MediaCodecInfo info = codec .getCodecInfo ();
253- MediaCodecInfo .CodecCapabilities cap = info .getCapabilitiesForType (mMime );
254- instances = Math .min (cap .getMaxSupportedInstances (), instances );
255- assertTrue (instances >= 1 );
256- } catch (Exception e ) {
257- fail ("codec '" + mEncoderName + "' failed construction." );
258- } finally {
259- codec .release ();
260- }
261- testEncoderWithFormatsParallel (mMime , mFormats , mEncoderName , instances );
201+ @ Test (timeout = PER_TEST_TIMEOUT_SMALL_TEST_MS )
202+ public void testEncoders () throws FileNotFoundException {
203+ setUpFormat ();
204+ assumeTrue ("no encoders found for " + mFormat .toString (),
205+ MediaUtils .supports (mEncoderName , mFormat ));
206+ assertEquals (mMime , mFormat .getString (MediaFormat .KEY_MIME ));
207+ testEncoder (mEncoderName , mFormat );
262208 }
263209
264210 // See bug 25843966
@@ -329,25 +275,6 @@ private void dequeueOutputBuffer(
329275 codec .releaseOutputBuffer (index , false /* render */ );
330276 }
331277
332- class EncoderRun implements Runnable {
333- String mComponentName ;
334- MediaFormat mFormat ;
335-
336- EncoderRun (String componentName , MediaFormat format ) {
337- mComponentName = componentName ;
338- mFormat = format ;
339- }
340- @ Override
341- public void run () {
342- try {
343- testEncoder (mComponentName , mFormat );
344- } catch (FileNotFoundException e ) {
345- e .printStackTrace ();
346- fail ("Received exception " + e );
347- }
348- }
349- }
350-
351278 // Number of tests called in testEncoder(String componentName, MediaFormat format)
352279 private static int kNumEncoderTestsPerRun = 5 + mBadSeeds .length * 2 ;
353280 private void testEncoder (String componentName , MediaFormat format )
0 commit comments