1 /*
2  * Copyright (C) 2020 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 package android.media.cts;
17 
18 import static com.google.common.truth.Truth.assertThat;
19 
20 import android.content.res.AssetFileDescriptor;
21 import android.content.res.AssetManager;
22 import android.media.MediaExtractor;
23 import android.media.metrics.LogSessionId;
24 import android.media.metrics.MediaMetricsManager;
25 import android.media.metrics.PlaybackSession;
26 
27 import androidx.test.ext.junit.runners.AndroidJUnit4;
28 import androidx.test.platform.app.InstrumentationRegistry;
29 
30 import com.google.common.truth.Truth;
31 
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 
36 /** Test class used by host-side tests to trigger {@link MediaExtractor} media metric events. */
37 @RunWith(AndroidJUnit4.class)
38 public class MediaExtractorDeviceSideTest {
39 
40     static {
41         System.loadLibrary("CtsMediaExtractorHostTestAppJni");
42     }
43 
44     private static final String SAMPLE_PATH = "raw/small_sample.mp4";
45     private AssetManager mAssetManager;
46 
47     @Before
setUp()48     public void setUp() {
49         mAssetManager = InstrumentationRegistry.getInstrumentation().getContext().getAssets();
50     }
51 
52     @Test
testEntryPointSdk()53     public void testEntryPointSdk() throws Exception {
54         MediaExtractor mediaExtractor = new MediaExtractor();
55         AssetManager assetManager =
56                 InstrumentationRegistry.getInstrumentation().getContext().getAssets();
57         try (AssetFileDescriptor fileDescriptor = assetManager.openFd(SAMPLE_PATH)) {
58             mediaExtractor.setDataSource(fileDescriptor);
59         } finally {
60             mediaExtractor.release();
61         }
62     }
63 
64     @Test
testEntryPointNdkNoJvm()65     public void testEntryPointNdkNoJvm() {
66         extractUsingNdkMediaExtractor(mAssetManager, SAMPLE_PATH, /* withAttachedJvm= */ false);
67     }
68 
69     @Test
testEntryPointNdkWithJvm()70     public void testEntryPointNdkWithJvm() {
71         extractUsingNdkMediaExtractor(mAssetManager, SAMPLE_PATH, /* withAttachedJvm= */ true);
72     }
73 
74     @Test
testInvalidLogSessionId()75     public void testInvalidLogSessionId() throws Exception {
76         // An arbitrary 16 char Base64Url log session id.
77         // This will be set in the extractor, but will be blocked from statsd.
78         assertMediaExtractorAcceptsLogSessionId(new LogSessionId("FakeLogSessionId"));
79     }
80 
81     @Test
testValidLogSessionId()82     public void testValidLogSessionId() throws Exception {
83         MediaMetricsManager mediaMetricsManager = InstrumentationRegistry
84                 .getInstrumentation().getTargetContext()
85                 .getSystemService(MediaMetricsManager.class);
86         PlaybackSession playbackSession = mediaMetricsManager.createPlaybackSession();
87         // Send a valid log session id from MediaMetricsManager service to statsd.
88         assertMediaExtractorAcceptsLogSessionId(playbackSession.getSessionId());
89     }
90 
91     // Test whether MediaExtractor properly sets and gets the log session id.
92     // See MediaExtractorHostSideTest.java, which verifies if it is sent to statsd.
assertMediaExtractorAcceptsLogSessionId( LogSessionId logSessionId)93     private void assertMediaExtractorAcceptsLogSessionId(
94             LogSessionId logSessionId) throws Exception {
95         MediaExtractor mediaExtractor = new MediaExtractor();
96         AssetManager assetManager =
97                 InstrumentationRegistry.getInstrumentation().getContext().getAssets();
98         try (AssetFileDescriptor fileDescriptor = assetManager.openFd(SAMPLE_PATH)) {
99             mediaExtractor.setDataSource(fileDescriptor);
100             assertThat(mediaExtractor.getLogSessionId())
101                     .isEqualTo(LogSessionId.LOG_SESSION_ID_NONE);
102             mediaExtractor.setLogSessionId(logSessionId);
103             assertThat(mediaExtractor.getLogSessionId()).isEqualTo(logSessionId);
104         } finally {
105             mediaExtractor.release();
106         }
107     }
108 
extractUsingNdkMediaExtractor( AssetManager assetManager, String assetPath, boolean withAttachedJvm)109     private native void extractUsingNdkMediaExtractor(
110             AssetManager assetManager, String assetPath, boolean withAttachedJvm);
111 }
112