• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "../includes/common.h"
18 #include "binding/IAAudioService.h"
19 #include <binder/IPCThreadState.h>
20 #include <binder/IServiceManager.h>
21 
22 using namespace android;
23 using namespace aaudio;
24 
25 typedef struct _thread_args {
26   aaudio_handle_t aaudioHandle;
27   sp<IAAudioService> aas;
28 } thread_args;
29 
closeStreamThread(void * arg)30 static void *closeStreamThread(void *arg) {
31   thread_args *threadArgs = (thread_args *)arg;
32   if (threadArgs) {
33     if (threadArgs->aas) {
34       threadArgs->aas->closeStream(threadArgs->aaudioHandle);
35     }
36   }
37   return nullptr;
38 }
39 
startStreamThread(void * arg)40 static void *startStreamThread(void *arg) {
41   thread_args *threadArgs = (thread_args *)arg;
42   if (threadArgs) {
43     if (threadArgs->aas) {
44       threadArgs->aas->startStream(threadArgs->aaudioHandle);
45     }
46   }
47   return nullptr;
48 }
49 
main()50 int main() {
51   thread_args targs;
52 
53   sp<IServiceManager> sm = defaultServiceManager();
54   sp<IBinder> binder = sm->getService(String16("media.aaudio"));
55   targs.aas = interface_cast<IAAudioService>(binder);
56   if (!(targs.aas)) {
57     return EXIT_FAILURE;
58   }
59   aaudio::AAudioStreamRequest request;
60   request.getConfiguration().setSharingMode(AAUDIO_SHARING_MODE_SHARED);
61   request.getConfiguration().setDeviceId(0);
62   request.getConfiguration().setSampleRate(AAUDIO_UNSPECIFIED);
63 
64   time_t currentTime = start_timer();
65   while (timer_active(currentTime)) {
66     pthread_t pt[2];
67 
68     aaudio::AAudioStreamConfiguration configurationOutput;
69     targs.aaudioHandle = targs.aas->openStream(request, configurationOutput);
70     pthread_create(&pt[0], nullptr, closeStreamThread,
71                    &targs); /* close stream */
72     pthread_create(&pt[1], nullptr, startStreamThread,
73                    &targs); /* start stream */
74 
75     sleep(5);
76   }
77   return EXIT_SUCCESS;
78 }
79