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 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <unistd.h>
21 #include <sstream>
22 #include <string>
23 
24 #include <android_audio_controller.h>
25 
help(const char * argv0)26 static void help(const char* argv0) {
27     if (argv0 == nullptr || argv0[0] == 0) argv0 = "android_audio_controller_test";
28     printf("Usage instructions:\n");
29     printf("%s [-d] -f <file path> -s <server address> [-t]\n", argv0);
30     printf("-d to switch between non-ducking (default) and ducking\n");
31     printf("-e to switch between exclusive (default) and non-exclusive\n");
32     printf("-f <file path>: path of the WAV file containing the sound sample to play\n");
33     printf("-s <server address>: the address of the Android Audio Control HAL server\n");
34     printf("-t to switch between non-transient (default) and transient\n");
35     printf("%s -h to repeat this message\n", argv0);
36 }
37 
error(const char * msg,int err)38 static void error(const char* msg, int err) {
39     fprintf(stderr, "error: %s", msg);
40     if (err != 0) fprintf(stderr, " (%d %s)", err, strerror(err));
41     fprintf(stderr, "\n");
42     exit(1);
43 }
44 
45 class AudioSession {
46   public:
~AudioSession()47     ~AudioSession() {
48         if (mSession != AAFC_SESSION_ID_INVALID) aafc_release_audio_focus(mSession);
49     }
50 
session() const51     aafc_session_id_t session() const { return mSession; }
52 
operator bool() const53     explicit operator bool() const { return mSession != AAFC_SESSION_ID_INVALID; }
54 
AudioSession(const aafc_audio_focus_request_t & request)55     explicit AudioSession(const aafc_audio_focus_request_t& request) {
56         mSession = aafc_acquire_audio_focus(request);
57     }
58 
59   private:
60     explicit AudioSession(aafc_session_id_t id);
61 
62     aafc_session_id_t mSession;
63 };
64 
main(int argc,char ** argv)65 int main(int argc, char** argv) {
66     int c;
67     std::string file_path;
68     std::string server_addr;
69 
70     // TODO(egranata): allow custom usage & zone
71     aafc_audio_focus_request_t request = {.audio_usage = AAFC_AUDIO_USAGE_EMERGENCY,
72                                           .zone_id = 0,
73                                           .allow_duck = false,
74                                           .is_transient = false,
75                                           .is_exclusive = true};
76 
77     while ((c = getopt(argc, argv, "def:hs:t")) != -1) {
78         switch (c) {
79             case 'd':
80                 request.allow_duck = !request.allow_duck;
81                 break;
82             case 'e':
83                 request.is_exclusive = !request.is_exclusive;
84                 break;
85             case 'f':
86                 file_path = optarg;
87                 break;
88             case 'h':
89                 help(argv[0]);
90                 exit(0);
91                 break;
92             case 's':
93                 server_addr = optarg;
94                 break;
95             case 't':
96                 request.is_transient = !request.is_transient;
97                 break;
98         }
99     }
100 
101     if (file_path.empty() || server_addr.empty()) {
102         help(argv[0]);
103         exit(0);
104     }
105 
106     int ok = aafc_init_audio_focus_controller(server_addr.c_str());
107     if (ok != 0) {
108         error("server connection failed", ok);
109     }
110 
111     AudioSession session(request);
112     if (!session) {
113         error("audio focus could not be acquired", 0);
114     }
115 
116     // TODO(egranata): find a cleaner way to do this (e.g. tinyalsa APIs)
117     std::stringstream ss;
118     ss << "/usr/bin/aplay \"" << file_path << "\"";
119     std::string cmd = ss.str();
120     system(cmd.c_str());
121 
122     return 0;
123 }
124