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 
17 package com.android.media.audiotestharness.server.examples;
18 
19 import com.android.media.audiotestharness.server.core.AudioCapturer;
20 import com.android.media.audiotestharness.server.core.AudioSystemService;
21 import com.android.media.audiotestharness.server.javasound.JavaAudioCapturerFactory;
22 import com.android.media.audiotestharness.server.javasound.JavaAudioSystem;
23 import com.android.media.audiotestharness.server.javasound.JavaAudioSystemService;
24 
25 import java.io.File;
26 import java.time.Duration;
27 import java.time.temporal.ChronoUnit;
28 import java.util.concurrent.ExecutorService;
29 import java.util.concurrent.Executors;
30 import java.util.logging.Logger;
31 
32 /**
33  * Simple Audio Capture Binary that makes use of the Audio Test Harness libraries to capture raw
34  * audio samples to a file named {@link #FILENAME}.
35  */
36 public class AudioTestHarnessCliCapturer {
37     private static final Logger LOGGER =
38             Logger.getLogger(AudioTestHarnessCliCapturer.class.getName());
39 
40     /** Output file for the recording. */
41     private static final String FILENAME = "output.raw";
42 
43     /** Duration that the recording will go until stopping. */
44     private static final Duration RECORD_TIME = Duration.of(10, ChronoUnit.SECONDS);
45 
main(String[] args)46     public static void main(String[] args) throws Exception {
47         ExecutorService executorService = Executors.newFixedThreadPool(4);
48         AudioSystemService audioSystemService =
49                 new JavaAudioSystemService(
50                         JavaAudioSystem.getInstance(),
51                         new JavaAudioCapturerFactory(executorService));
52 
53         try (AudioCapturer capturer = audioSystemService.createDefaultCapturer()) {
54             LOGGER.info("Opening AudioCapturer...");
55             capturer.attachOutput(new File(FILENAME));
56             capturer.open();
57 
58             // Put main thread to sleep since the capturer operates on a separate thread provided by
59             // the
60             // above executor service.
61             LOGGER.info(String.format("Sleeping for %s...", RECORD_TIME));
62             Thread.sleep(RECORD_TIME.toMillis());
63 
64             LOGGER.info("Completed Capture. Cleaning up...");
65         }
66 
67         executorService.shutdown();
68     }
69 }
70