1 /* 2 * Copyright 2017 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 17 package com.mobileer.oboetester; 18 19 import android.content.Intent; 20 import android.media.audiofx.AcousticEchoCanceler; 21 import android.media.audiofx.AutomaticGainControl; 22 import android.net.Uri; 23 import android.os.Bundle; 24 import android.os.Environment; 25 import android.os.Handler; 26 import android.os.Looper; 27 import android.support.annotation.NonNull; 28 import android.support.v4.content.FileProvider; 29 import android.view.View; 30 import android.widget.RadioButton; 31 32 import java.io.File; 33 import java.io.IOException; 34 35 /** 36 * Test Oboe Capture 37 */ 38 39 public class TestInputActivity extends TestAudioActivity { 40 41 protected AudioInputTester mAudioInputTester; 42 private static final int NUM_VOLUME_BARS = 4; 43 private VolumeBarView[] mVolumeBars = new VolumeBarView[NUM_VOLUME_BARS]; 44 private InputMarginView mInputMarginView; 45 private int mInputMarginBursts = 0; 46 private WorkloadView mWorkloadView; 47 private CommunicationDeviceView mCommunicationDeviceView; 48 setMinimumFramesBeforeRead(int frames)49 public native void setMinimumFramesBeforeRead(int frames); saveWaveFile(String absolutePath)50 public native int saveWaveFile(String absolutePath); 51 isOutput()52 @Override boolean isOutput() { return false; } 53 54 @Override inflateActivity()55 protected void inflateActivity() { 56 setContentView(R.layout.activity_test_input); 57 58 BufferSizeView bufferSizeView = findViewById(R.id.buffer_size_view); 59 bufferSizeView.setVisibility(View.GONE); 60 } 61 62 @Override onCreate(Bundle savedInstanceState)63 protected void onCreate(Bundle savedInstanceState) { 64 super.onCreate(savedInstanceState); 65 66 mVolumeBars[0] = (VolumeBarView) findViewById(R.id.volumeBar0); 67 mVolumeBars[1] = (VolumeBarView) findViewById(R.id.volumeBar1); 68 mVolumeBars[2] = (VolumeBarView) findViewById(R.id.volumeBar2); 69 mVolumeBars[3] = (VolumeBarView) findViewById(R.id.volumeBar3); 70 71 mInputMarginView = (InputMarginView) findViewById(R.id.input_margin_view); 72 73 updateEnabledWidgets(); 74 75 mAudioInputTester = addAudioInputTester(); 76 77 mWorkloadView = (WorkloadView) findViewById(R.id.workload_view); 78 if (mWorkloadView != null) { 79 mWorkloadView.setAudioStreamTester(mAudioInputTester); 80 } 81 82 mCommunicationDeviceView = (CommunicationDeviceView) findViewById(R.id.comm_device_view); 83 } 84 85 @Override onStop()86 protected void onStop() { 87 if (mCommunicationDeviceView != null) { 88 mCommunicationDeviceView.cleanup(); 89 } 90 super.onStop(); 91 } 92 93 @Override getActivityType()94 int getActivityType() { 95 return ACTIVITY_TEST_INPUT; 96 } 97 98 @Override resetConfiguration()99 protected void resetConfiguration() { 100 super.resetConfiguration(); 101 mAudioInputTester.reset(); 102 } 103 104 @Override updateStreamDisplay()105 void updateStreamDisplay() { 106 int numChannels = mAudioInputTester.getCurrentAudioStream().getChannelCount(); 107 if (numChannels > NUM_VOLUME_BARS) { 108 numChannels = NUM_VOLUME_BARS; 109 } 110 for (int i = 0; i < numChannels; i++) { 111 if (mVolumeBars[i] == null) break; 112 double level = mAudioInputTester.getPeakLevel(i); 113 mVolumeBars[i].setAmplitude((float) level); 114 } 115 } 116 resetVolumeBars()117 void resetVolumeBars() { 118 for (int i = 0; i < mVolumeBars.length; i++) { 119 if (mVolumeBars[i] == null) break; 120 mVolumeBars[i].setAmplitude((float) 0.0); 121 } 122 } 123 setMinimumBurstsBeforeRead(int numBursts)124 void setMinimumBurstsBeforeRead(int numBursts) { 125 int framesPerBurst = mAudioInputTester.getCurrentAudioStream().getFramesPerBurst(); 126 if (framesPerBurst > 0) { 127 setMinimumFramesBeforeRead(numBursts * framesPerBurst); 128 } 129 } 130 131 @Override openAudio(View view)132 public void openAudio(View view) { 133 try { 134 openAudio(); 135 } catch (Exception e) { 136 showErrorToast(e.getMessage()); 137 } 138 } 139 140 @Override openAudio()141 public void openAudio() throws IOException { 142 super.openAudio(); 143 setMinimumBurstsBeforeRead(mInputMarginBursts); 144 resetVolumeBars(); 145 } 146 147 @Override stopAudio()148 public void stopAudio() { 149 super.stopAudio(); 150 resetVolumeBars(); 151 } 152 153 @Override toastPauseError(int result)154 protected void toastPauseError(int result) { 155 showToast("Pause not implemented. Returned " + result); 156 } 157 saveWaveFile(File file)158 protected int saveWaveFile(File file) { 159 // Pass filename to native to write WAV file 160 int result = saveWaveFile(file.getAbsolutePath()); 161 if (result < 0) { 162 showErrorToast("Save returned " + result); 163 } else { 164 showToast("Saved " + result + " bytes."); 165 } 166 return result; 167 } 168 getWaveTag()169 String getWaveTag() { 170 return "input"; 171 } 172 173 @NonNull createFileName()174 private File createFileName() { 175 // Get directory and filename 176 File dir = getExternalFilesDir(Environment.DIRECTORY_MUSIC); 177 return new File(dir, "oboe_" + getWaveTag() + "_" + AutomatedTestRunner.getTimestampString() + ".wav"); 178 } 179 shareWaveFile()180 public void shareWaveFile() { 181 // Share WAVE file via GMail, Drive or other method. 182 File file = createFileName(); 183 int result = saveWaveFile(file); 184 if (result > 0) { 185 Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND); 186 sharingIntent.setType("audio/wav"); 187 String subjectText = file.getName(); 188 sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subjectText); 189 Uri uri = FileProvider.getUriForFile(this, 190 BuildConfig.APPLICATION_ID + ".provider", 191 file); 192 sharingIntent.putExtra(Intent.EXTRA_STREAM, uri); 193 sharingIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); 194 startActivity(Intent.createChooser(sharingIntent, "Share WAV using:")); 195 } 196 } 197 onShareFile(View view)198 public void onShareFile(View view) { 199 shareWaveFile(); 200 } 201 onMarginBoxClicked(View view)202 public void onMarginBoxClicked(View view) { 203 RadioButton radioButton = (RadioButton) view; 204 String text = (String) radioButton.getText(); 205 mInputMarginBursts = Integer.parseInt(text); 206 setMinimumBurstsBeforeRead(mInputMarginBursts); 207 } 208 209 @Override startTestUsingBundle()210 public void startTestUsingBundle() { 211 try { 212 StreamConfiguration requestedInConfig = mAudioInputTester.requestedConfiguration; 213 IntentBasedTestSupport.configureInputStreamFromBundle(mBundleFromIntent, requestedInConfig); 214 215 openAudio(); 216 startAudio(); 217 218 int durationSeconds = IntentBasedTestSupport.getDurationSeconds(mBundleFromIntent); 219 if (durationSeconds > 0) { 220 // Schedule the end of the test. 221 Handler handler = new Handler(Looper.getMainLooper()); // UI thread 222 handler.postDelayed(new Runnable() { 223 @Override 224 public void run() { 225 stopAutomaticTest(); 226 } 227 }, durationSeconds * 1000); 228 } 229 } catch (Exception e) { 230 showErrorToast(e.getMessage()); 231 } finally { 232 mBundleFromIntent = null; 233 } 234 } 235 stopAutomaticTest()236 void stopAutomaticTest() { 237 String report = getCommonTestReport(); 238 stopAudio(); 239 maybeWriteTestResult(report); 240 mTestRunningByIntent = false; 241 } 242 } 243