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 <binder/IPCThreadState.h>
18 #include <binder/IServiceManager.h>
19 #include <media/AudioSystem.h>
20 #include "../includes/common.h"
21 
22 using namespace android;
23 
24 #define MAX_NUMBER_OF_AUDIO_SESSIONS 1024
25 #define MAX_NUMBER_OF_THREADS 5
26 #define MAX_NUMBER_OF_ACQUIRE_SESSION_THREADS 2
27 
28 struct pocAudioSessionCtxt {
29   audio_session_t audioSession[MAX_NUMBER_OF_AUDIO_SESSIONS];
30   volatile bool startThread;
31 };
32 
acquireSoundTriggerSessionThread(void * arg)33 static void *acquireSoundTriggerSessionThread(void *arg) {
34   int i = 0;
35   pocAudioSessionCtxt *ctxt = (pocAudioSessionCtxt *)arg;
36   if (!ctxt) {
37     return nullptr;
38   }
39   time_t currentTime = start_timer();
40   while (timer_active(currentTime)) {
41       if (ctxt->startThread) {
42           audio_io_handle_t ioHandle = 0;
43           audio_devices_t device = AUDIO_DEVICE_NONE;
44           AudioSystem::acquireSoundTriggerSession(&(ctxt->audioSession[++i]), &ioHandle, &device);
45           if (i >= MAX_NUMBER_OF_AUDIO_SESSIONS) {
46               i = 0;
47           }
48       }
49   }
50   return nullptr;
51 }
52 
releaseSoundTriggerSessionThread(void * arg)53 static void *releaseSoundTriggerSessionThread(void *arg) {
54   int i = 0;
55   pocAudioSessionCtxt *ctxt = (pocAudioSessionCtxt *)arg;
56   if (!ctxt) {
57     return nullptr;
58   }
59   time_t currentTime = start_timer();
60   while (timer_active(currentTime)) {
61       if (ctxt->startThread) {
62           AudioSystem::releaseSoundTriggerSession(ctxt->audioSession[++i]);
63           if (i >= MAX_NUMBER_OF_AUDIO_SESSIONS) {
64               i = 0;
65           }
66       }
67   }
68   return nullptr;
69 }
70 
main()71 int main() {
72   pocAudioSessionCtxt ctxt;
73   pthread_t thread[MAX_NUMBER_OF_THREADS];
74   ctxt.startThread = false;
75 
76   for (int i = 0; i < MAX_NUMBER_OF_ACQUIRE_SESSION_THREADS; ++i) {
77     pthread_create(&thread[i], nullptr, acquireSoundTriggerSessionThread,
78                    &ctxt);
79   }
80 
81   for (int i = MAX_NUMBER_OF_ACQUIRE_SESSION_THREADS; i < MAX_NUMBER_OF_THREADS;
82        ++i) {
83     pthread_create(&thread[i], nullptr, releaseSoundTriggerSessionThread,
84                    &ctxt);
85   }
86 
87   ctxt.startThread = true;
88   for (int i = 0; i < MAX_NUMBER_OF_THREADS; ++i) {
89     pthread_join(thread[i], nullptr);
90   }
91   return EXIT_SUCCESS;
92 }
93