summaryrefslogtreecommitdiff
path: root/audio/effect/all-versions/default/Effect.cpp
blob: 87e1ab7a7d0022d82883ad213f42eb962d15e208 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
/*
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <memory.h>

#define LOG_TAG "EffectHAL"
#define ATRACE_TAG ATRACE_TAG_AUDIO

#include "Effect.h"
#include "common/all-versions/default/EffectMap.h"

#define ATRACE_TAG ATRACE_TAG_AUDIO
#include <HidlUtils.h>
#include <android/log.h>
#include <cutils/properties.h>
#include <media/EffectsFactoryApi.h>
#include <mediautils/ScopedStatistics.h>
#include <sys/syscall.h>
#include <system/audio_effects/effect_spatializer.h>
#include <util/EffectUtils.h>
#include <utils/Trace.h>

#include "VersionUtils.h"

namespace android {
namespace hardware {
namespace audio {
namespace effect {
namespace CPP_VERSION {
namespace implementation {

#if MAJOR_VERSION <= 6
using ::android::hardware::audio::common::COMMON_TYPES_CPP_VERSION::implementation::
        AudioChannelBitfield;
#endif
using ::android::hardware::audio::common::COMMON_TYPES_CPP_VERSION::implementation::HidlUtils;

namespace {

/**
 * Some basic scheduling tools.
 */
namespace scheduler {

int getCpu() {
    return sched_getcpu();
}

uint64_t getAffinity(pid_t tid) {
    cpu_set_t set;
    CPU_ZERO_S(sizeof(set), &set);

    if (sched_getaffinity(tid, sizeof(set), &set)) {
        ALOGW("%s: for tid:%d returning 0, failed %s", __func__, tid, strerror(errno));
        return 0;
    }
    const int count = CPU_COUNT_S(sizeof(set), &set);
    uint64_t mask = 0;
    for (int i = 0; i < CPU_SETSIZE; ++i) {
        if (CPU_ISSET_S(i, sizeof(set), &set)) {
            mask |= 1 << i;
        }
    }
    ALOGV("%s: for tid:%d returning cpu count %d mask %llu", __func__, tid, count,
          (unsigned long long)mask);
    return mask;
}

status_t setAffinity(pid_t tid, uint64_t mask) {
    cpu_set_t set;
    CPU_ZERO_S(sizeof(set), &set);

    for (uint64_t m = mask; m != 0;) {
        uint64_t tz = __builtin_ctz(m);
        CPU_SET_S(tz, sizeof(set), &set);
        m &= ~(1 << tz);
    }
    if (sched_setaffinity(tid, sizeof(set), &set)) {
        ALOGW("%s: for tid:%d setting cpu mask %llu failed %s", __func__, tid,
              (unsigned long long)mask, strerror(errno));
        return -errno;
    }
    ALOGV("%s: for tid:%d setting cpu mask %llu", __func__, tid, (unsigned long long)mask);
    return OK;
}

__unused status_t setPriority(pid_t tid, int policy, int priority) {
    struct sched_param param {
        .sched_priority = priority,
    };
    if (sched_setscheduler(tid, policy, &param) != 0) {
        ALOGW("%s: Cannot set FIFO priority for tid %d to policy %d priority %d  %s", __func__, tid,
              policy, priority, strerror(errno));
        return -errno;
    }
    ALOGV("%s: Successfully set priority for tid %d to policy %d priority %d", __func__, tid,
          policy, priority);
    return NO_ERROR;
}

status_t setUtilMin(pid_t tid, uint32_t utilMin) {
    // Currently, there is no wrapper in bionic: b/183240349.
    struct {
        uint32_t size;
        uint32_t sched_policy;
        uint64_t sched_flags;
        int32_t sched_nice;
        uint32_t sched_priority;
        uint64_t sched_runtime;
        uint64_t sched_deadline;
        uint64_t sched_period;
        uint32_t sched_util_min;
        uint32_t sched_util_max;
    } attr{
            .size = sizeof(attr),
            .sched_flags = SCHED_FLAG_KEEP_ALL | SCHED_FLAG_UTIL_CLAMP_MIN,
            .sched_util_min = utilMin,
    };

    if (syscall(__NR_sched_setattr, tid, &attr, 0 /* flags */)) {
        ALOGW("%s: Cannot set sched_util_min for pid %d to %u  %s", __func__, tid, utilMin,
              strerror(errno));
        return -errno;
    }
    ALOGV("%s: Successfully set sched_util_min for pid %d to %u", __func__, tid, utilMin);
    return NO_ERROR;
}

/*
   Attempts to raise the priority and usage of tid for spatialization.
   Returns OK if everything works.
*/
status_t updateSpatializerPriority(pid_t tid) {
    status_t status = OK;

    const int cpu = getCpu();
    ALOGV("%s: current CPU:%d", __func__, cpu);

    const auto currentAffinity = getAffinity(tid);
    ALOGV("%s: current Affinity:%llx", __func__, (unsigned long long)currentAffinity);

    // Set the desired CPU core affinity.
    // Typically this would be done to move the Spatializer effect off of the little cores.
    // The mid cores and large cores typically have more FP/NEON units
    // and will advantageously reduce power and prevent glitches due CPU limitations.
    //
    // Since this is SOC dependent, we do not set the core affinity here but
    // prefer to set the util_clamp_min below.
    //
    constexpr uint64_t kDefaultAffinity = 0;
    const int32_t desiredAffinity =
            property_get_int32("audio.spatializer.effect.affinity", kDefaultAffinity);
    if (desiredAffinity != 0 && (desiredAffinity & ~currentAffinity) == 0) {
        const status_t localStatus = setAffinity(tid, desiredAffinity);
        status = status ? status : localStatus;
    }

    // Set the util_clamp_min.
    // This is beneficial to reduce glitches when starting up, or due to scheduler
    // thread statistics reset (e.g. core migration), which cause the CPU frequency to drop
    // to minimum.
    //
    // Experimentation has found that moving to a mid core over a little core reduces
    // power if the mid core (e.g. A76/78) has more (e.g. 2x) FP/NEON units
    // than the little core (e.g. A55).
    // A possible value is 300.
    //
    constexpr uint32_t kUtilMin = 0;
    const int32_t utilMin = property_get_int32("audio.spatializer.effect.util_clamp_min", kUtilMin);
    if (utilMin > 0 && utilMin <= 1024) {
        const status_t localStatus = setUtilMin(tid, utilMin);
        status = status ? status : localStatus;
    }

#if 0
    // Provided for local vendor testing but not enabled as audioserver does this for us.
    //
    // Set priority if specified.
    constexpr int32_t kRTPriorityMin = 1;
    constexpr int32_t kRTPriorityMax = 3;
    const int32_t priorityBoost =
            property_get_int32("audio.spatializer.priority", kRTPriorityMin);
    if (priorityBoost >= kRTPriorityMin && priorityBoost <= kRTPriorityMax) {
        const status_t localStatus = scheduler::setPriority(threadId, SCHED_FIFO, priorityBoost);
        status = status ? status : localStatus;
    }
#endif

    return status;
}

}  // namespace scheduler

#define SCOPED_STATS()                                                       \
    ::android::mediautils::ScopedStatistics scopedStatistics {               \
        std::string("EffectHal::").append(__func__), mEffectHal->mStatistics \
    }

class ProcessThread : public Thread {
   public:
    // ProcessThread's lifespan never exceeds Effect's lifespan.
     ProcessThread(std::atomic<bool>* stop, effect_handle_t effect,
                   std::atomic<audio_buffer_t*>* inBuffer, std::atomic<audio_buffer_t*>* outBuffer,
                   Effect::StatusMQ* statusMQ, EventFlag* efGroup, Effect* effectHal)
         : Thread(false /*canCallJava*/),
           mStop(stop),
           mEffect(effect),
           mHasProcessReverse((*mEffect)->process_reverse != NULL),
           mInBuffer(inBuffer),
           mOutBuffer(outBuffer),
           mStatusMQ(statusMQ),
           mEfGroup(efGroup),
           mEffectHal(effectHal) {}
     virtual ~ProcessThread() {}

   private:
    std::atomic<bool>* mStop;
    effect_handle_t mEffect;
    bool mHasProcessReverse;
    std::atomic<audio_buffer_t*>* mInBuffer;
    std::atomic<audio_buffer_t*>* mOutBuffer;
    Effect::StatusMQ* mStatusMQ;
    EventFlag* mEfGroup;
    Effect* const mEffectHal;

    bool threadLoop() override;
};

bool ProcessThread::threadLoop() {
    // For a spatializer effect, we perform scheduler adjustments to reduce glitches and power.
    {
        effect_descriptor_t halDescriptor{};
        if ((*mEffect)->get_descriptor(mEffect, &halDescriptor) == NO_ERROR &&
            memcmp(&halDescriptor.type, FX_IID_SPATIALIZER, sizeof(effect_uuid_t)) == 0) {
            const status_t status = scheduler::updateSpatializerPriority(gettid());
            ALOGW_IF(status != OK, "Failed to update Spatializer priority");
        }
    }

    // This implementation doesn't return control back to the Thread until it decides to stop,
    // as the Thread uses mutexes, and this can lead to priority inversion.
    while (!std::atomic_load_explicit(mStop, std::memory_order_acquire)) {
        uint32_t efState = 0;
        mEfGroup->wait(static_cast<uint32_t>(MessageQueueFlagBits::REQUEST_PROCESS_ALL), &efState);
        if (!(efState & static_cast<uint32_t>(MessageQueueFlagBits::REQUEST_PROCESS_ALL)) ||
            (efState & static_cast<uint32_t>(MessageQueueFlagBits::REQUEST_QUIT))) {
            continue;  // Nothing to do or time to quit.
        }
        Result retval = Result::OK;
        if (efState & static_cast<uint32_t>(MessageQueueFlagBits::REQUEST_PROCESS_REVERSE) &&
            !mHasProcessReverse) {
            retval = Result::NOT_SUPPORTED;
        }

        if (retval == Result::OK) {
            // affects both buffer pointers and their contents.
            std::atomic_thread_fence(std::memory_order_acquire);
            int32_t processResult;
            audio_buffer_t* inBuffer =
                std::atomic_load_explicit(mInBuffer, std::memory_order_relaxed);
            audio_buffer_t* outBuffer =
                std::atomic_load_explicit(mOutBuffer, std::memory_order_relaxed);
            if (inBuffer != nullptr && outBuffer != nullptr) {
                // Time this effect process
                SCOPED_STATS();

                if (efState & static_cast<uint32_t>(MessageQueueFlagBits::REQUEST_PROCESS)) {
                    processResult = (*mEffect)->process(mEffect, inBuffer, outBuffer);
                } else {
                    processResult = (*mEffect)->process_reverse(mEffect, inBuffer, outBuffer);
                }
                std::atomic_thread_fence(std::memory_order_release);
            } else {
                ALOGE("processing buffers were not set before calling 'process'");
                processResult = -ENODEV;
            }
            switch (processResult) {
                case 0:
                    retval = Result::OK;
                    break;
                case -ENODATA:
                    retval = Result::INVALID_STATE;
                    break;
                case -EINVAL:
                    retval = Result::INVALID_ARGUMENTS;
                    break;
                default:
                    retval = Result::NOT_INITIALIZED;
            }
        }
        if (!mStatusMQ->write(&retval)) {
            ALOGW("status message queue write failed");
        }
        mEfGroup->wake(static_cast<uint32_t>(MessageQueueFlagBits::DONE_PROCESSING));
    }

    return false;
}

}  // namespace

// static
const char* Effect::sContextResultOfCommand = "returned status";
const char* Effect::sContextCallToCommand = "error";
const char* Effect::sContextCallFunction = sContextCallToCommand;
const char* Effect::sContextConversion = "conversion";

Effect::Effect(bool isInput, effect_handle_t handle)
    : mIsInput(isInput), mHandle(handle), mEfGroup(nullptr), mStopProcessThread(false) {
    (void)mIsInput;  // prevent 'unused field' warnings in pre-V7 versions.
}

Effect::~Effect() {
    ATRACE_CALL();
    (void)close();
    if (mProcessThread.get()) {
        ATRACE_NAME("mProcessThread->join");
        status_t status = mProcessThread->join();
        ALOGE_IF(status, "processing thread exit error: %s", strerror(-status));
    }
    if (mEfGroup) {
        status_t status = EventFlag::deleteEventFlag(&mEfGroup);
        ALOGE_IF(status, "processing MQ event flag deletion error: %s", strerror(-status));
    }
    mInBuffer.clear();
    mOutBuffer.clear();
#if MAJOR_VERSION <= 5
    int status = EffectRelease(mHandle);
    ALOGW_IF(status, "Error releasing effect %p: %s", mHandle, strerror(-status));
#endif
    EffectMap::getInstance().remove(mHandle);
    mHandle = 0;
}

// static
template <typename T>
size_t Effect::alignedSizeIn(size_t s) {
    return (s + sizeof(T) - 1) / sizeof(T);
}

// static
template <typename T>
std::unique_ptr<uint8_t[]> Effect::hidlVecToHal(const hidl_vec<T>& vec, uint32_t* halDataSize) {
    // Due to bugs in HAL, they may attempt to write into the provided
    // input buffer. The original binder buffer is r/o, thus it is needed
    // to create a r/w version.
    *halDataSize = vec.size() * sizeof(T);
    std::unique_ptr<uint8_t[]> halData(new uint8_t[*halDataSize]);
    memcpy(&halData[0], &vec[0], *halDataSize);
    return halData;
}

#if MAJOR_VERSION <= 6

void Effect::effectAuxChannelsConfigFromHal(const channel_config_t& halConfig,
                                            EffectAuxChannelsConfig* config) {
    config->mainChannels = AudioChannelBitfield(halConfig.main_channels);
    config->auxChannels = AudioChannelBitfield(halConfig.aux_channels);
}

// static
void Effect::effectAuxChannelsConfigToHal(const EffectAuxChannelsConfig& config,
                                          channel_config_t* halConfig) {
    halConfig->main_channels = static_cast<audio_channel_mask_t>(config.mainChannels);
    halConfig->aux_channels = static_cast<audio_channel_mask_t>(config.auxChannels);
}

#else  // MAJOR_VERSION <= 6

void Effect::effectAuxChannelsConfigFromHal(const channel_config_t& halConfig,
                                            EffectAuxChannelsConfig* config) {
    (void)HidlUtils::audioChannelMaskFromHal(halConfig.main_channels, mIsInput,
                                             &config->mainChannels);
    (void)HidlUtils::audioChannelMaskFromHal(halConfig.aux_channels, mIsInput,
                                             &config->auxChannels);
}

// static
void Effect::effectAuxChannelsConfigToHal(const EffectAuxChannelsConfig& config,
                                          channel_config_t* halConfig) {
    (void)HidlUtils::audioChannelMaskToHal(config.mainChannels, &halConfig->main_channels);
    (void)HidlUtils::audioChannelMaskToHal(config.auxChannels, &halConfig->aux_channels);
}

#endif  // MAJOR_VERSION <= 6

// static
void Effect::effectOffloadParamToHal(const EffectOffloadParameter& offload,
                                     effect_offload_param_t* halOffload) {
    halOffload->isOffload = offload.isOffload;
    halOffload->ioHandle = offload.ioHandle;
}

// static
bool Effect::parameterToHal(uint32_t paramSize, const void* paramData, uint32_t valueSize,
                            const void** valueData, std::vector<uint8_t>* halParamBuffer) {
    constexpr size_t kMaxSize = EFFECT_PARAM_SIZE_MAX - sizeof(effect_param_t);
    if (paramSize > kMaxSize) {
        ALOGE("%s: Parameter size is too big: %" PRIu32, __func__, paramSize);
        return false;
    }
    size_t valueOffsetFromData = alignedSizeIn<uint32_t>(paramSize) * sizeof(uint32_t);
    if (valueOffsetFromData > kMaxSize) {
        ALOGE("%s: Aligned parameter size is too big: %zu", __func__, valueOffsetFromData);
        return false;
    }
    if (valueSize > kMaxSize - valueOffsetFromData) {
        ALOGE("%s: Value size is too big: %" PRIu32 ", max size is %zu", __func__, valueSize,
              kMaxSize - valueOffsetFromData);
        android_errorWriteLog(0x534e4554, "237291425");
        return false;
    }
    size_t halParamBufferSize = sizeof(effect_param_t) + valueOffsetFromData + valueSize;
    halParamBuffer->resize(halParamBufferSize, 0);
    effect_param_t* halParam = reinterpret_cast<effect_param_t*>(halParamBuffer->data());
    halParam->psize = paramSize;
    halParam->vsize = valueSize;
    memcpy(halParam->data, paramData, paramSize);
    if (valueData) {
        if (*valueData) {
            // Value data is provided.
            memcpy(halParam->data + valueOffsetFromData, *valueData, valueSize);
        } else {
            // The caller needs the pointer to the value data location.
            *valueData = halParam->data + valueOffsetFromData;
        }
    }
    return true;
}

Result Effect::analyzeCommandStatus(const char* commandName, const char* context, status_t status) {
    return analyzeStatus("command", commandName, context, status);
}

Result Effect::analyzeStatus(const char* funcName, const char* subFuncName,
                             const char* contextDescription, status_t status) {
    if (status != OK) {
        ALOGW("Effect %p %s %s %s: %s", mHandle, funcName, subFuncName, contextDescription,
              strerror(-status));
    }
    switch (status) {
        case OK:
            return Result::OK;
        case -EINVAL:
            return Result::INVALID_ARGUMENTS;
        case -ENODATA:
            return Result::INVALID_STATE;
        case -ENODEV:
            return Result::NOT_INITIALIZED;
        case -ENOMEM:
            return Result::RESULT_TOO_BIG;
        case -ENOSYS:
            return Result::NOT_SUPPORTED;
        default:
            return Result::INVALID_STATE;
    }
}

void Effect::getConfigImpl(int commandCode, const char* commandName, GetConfigCallback cb) {
    uint32_t halResultSize = sizeof(effect_config_t);
    effect_config_t halConfig{};
    status_t status =
        (*mHandle)->command(mHandle, commandCode, 0, NULL, &halResultSize, &halConfig);
    EffectConfig config;
    if (status == OK) {
        status = EffectUtils::effectConfigFromHal(halConfig, mIsInput, &config);
    }
    cb(analyzeCommandStatus(commandName, sContextCallToCommand, status), config);
}

Result Effect::getCurrentConfigImpl(uint32_t featureId, uint32_t configSize,
                                    GetCurrentConfigSuccessCallback onSuccess) {
    if (configSize > kMaxDataSize - sizeof(uint32_t)) {
        ALOGE("%s: Config size is too big: %" PRIu32, __func__, configSize);
        android_errorWriteLog(0x534e4554, "240266798");
        return Result::INVALID_ARGUMENTS;
    }
    uint32_t halCmd = featureId;
    std::vector<uint32_t> halResult(alignedSizeIn<uint32_t>(sizeof(uint32_t) + configSize), 0);
    uint32_t halResultSize = 0;
    return sendCommandReturningStatusAndData(
            EFFECT_CMD_GET_FEATURE_CONFIG, "GET_FEATURE_CONFIG", sizeof(uint32_t), &halCmd,
            &halResultSize, &halResult[0], sizeof(uint32_t), [&] { onSuccess(&halResult[1]); });
}

Result Effect::getParameterImpl(uint32_t paramSize, const void* paramData,
                                uint32_t requestValueSize, uint32_t replyValueSize,
                                GetParameterSuccessCallback onSuccess) {
    // As it is unknown what method HAL uses for copying the provided parameter data,
    // it is safer to make sure that input and output buffers do not overlap.
    std::vector<uint8_t> halCmdBuffer;
    if (!parameterToHal(paramSize, paramData, requestValueSize, nullptr, &halCmdBuffer)) {
        return Result::INVALID_ARGUMENTS;
    }
    const void* valueData = nullptr;
    std::vector<uint8_t> halParamBuffer;
    if (!parameterToHal(paramSize, paramData, replyValueSize, &valueData, &halParamBuffer)) {
        return Result::INVALID_ARGUMENTS;
    }
    uint32_t halParamBufferSize = halParamBuffer.size();

    return sendCommandReturningStatusAndData(
        EFFECT_CMD_GET_PARAM, "GET_PARAM", halCmdBuffer.size(), &halCmdBuffer[0],
        &halParamBufferSize, &halParamBuffer[0], sizeof(effect_param_t), [&] {
            effect_param_t* halParam = reinterpret_cast<effect_param_t*>(&halParamBuffer[0]);
            onSuccess(halParam->vsize, valueData);
        });
}

Result Effect::getSupportedConfigsImpl(uint32_t featureId, uint32_t maxConfigs, uint32_t configSize,
                                       GetSupportedConfigsSuccessCallback onSuccess) {
    if (maxConfigs != 0 && configSize > (kMaxDataSize - 2 * sizeof(uint32_t)) / maxConfigs) {
        ALOGE("%s: Config size is too big: %" PRIu32, __func__, configSize);
        return Result::INVALID_ARGUMENTS;
    }
    uint32_t halCmd[2] = {featureId, maxConfigs};
    uint32_t halResultSize = 2 * sizeof(uint32_t) + maxConfigs * configSize;
    std::vector<uint8_t> halResult(static_cast<size_t>(halResultSize), 0);
    return sendCommandReturningStatusAndData(
        EFFECT_CMD_GET_FEATURE_SUPPORTED_CONFIGS, "GET_FEATURE_SUPPORTED_CONFIGS", sizeof(halCmd),
        halCmd, &halResultSize, &halResult[0], 2 * sizeof(uint32_t), [&] {
            uint32_t* halResult32 = reinterpret_cast<uint32_t*>(&halResult[0]);
            uint32_t supportedConfigs = *(++halResult32);  // skip status field
            if (supportedConfigs > maxConfigs) supportedConfigs = maxConfigs;
            onSuccess(supportedConfigs, ++halResult32);
        });
}

Return<void> Effect::prepareForProcessing(prepareForProcessing_cb _hidl_cb) {
    status_t status;
    // Create message queue.
    if (mStatusMQ) {
        ALOGE("the client attempts to call prepareForProcessing_cb twice");
        _hidl_cb(Result::INVALID_STATE, StatusMQ::Descriptor());
        return Void();
    }
    std::unique_ptr<StatusMQ> tempStatusMQ(new StatusMQ(1, true /*EventFlag*/));
    if (!tempStatusMQ->isValid()) {
        ALOGE_IF(!tempStatusMQ->isValid(), "status MQ is invalid");
        _hidl_cb(Result::INVALID_ARGUMENTS, StatusMQ::Descriptor());
        return Void();
    }
    status = EventFlag::createEventFlag(tempStatusMQ->getEventFlagWord(), &mEfGroup);
    if (status != OK || !mEfGroup) {
        ALOGE("failed creating event flag for status MQ: %s", strerror(-status));
        _hidl_cb(Result::INVALID_ARGUMENTS, StatusMQ::Descriptor());
        return Void();
    }

    // Create and launch the thread.
    mProcessThread = new ProcessThread(&mStopProcessThread, mHandle, &mHalInBufferPtr,
                                       &mHalOutBufferPtr, tempStatusMQ.get(), mEfGroup, this);
    status = mProcessThread->run("effect", PRIORITY_URGENT_AUDIO);
    if (status != OK) {
        ALOGW("failed to start effect processing thread: %s", strerror(-status));
        _hidl_cb(Result::INVALID_ARGUMENTS, MQDescriptorSync<Result>());
        return Void();
    }

    mStatusMQ = std::move(tempStatusMQ);
    _hidl_cb(Result::OK, *mStatusMQ->getDesc());
    return Void();
}

Return<Result> Effect::setProcessBuffers(const AudioBuffer& inBuffer,
                                         const AudioBuffer& outBuffer) {
    AudioBufferManager& manager = AudioBufferManager::getInstance();
    sp<AudioBufferWrapper> tempInBuffer, tempOutBuffer;
    if (!manager.wrap(inBuffer, &tempInBuffer)) {
        ALOGE("Could not map memory of the input buffer");
        return Result::INVALID_ARGUMENTS;
    }
    if (!manager.wrap(outBuffer, &tempOutBuffer)) {
        ALOGE("Could not map memory of the output buffer");
        return Result::INVALID_ARGUMENTS;
    }
    mInBuffer = tempInBuffer;
    mOutBuffer = tempOutBuffer;
    // The processing thread only reads these pointers after waking up by an event flag,
    // so it's OK to update the pair non-atomically.
    mHalInBufferPtr.store(mInBuffer->getHalBuffer(), std::memory_order_release);
    mHalOutBufferPtr.store(mOutBuffer->getHalBuffer(), std::memory_order_release);
    return Result::OK;
}

Result Effect::sendCommand(int commandCode, const char* commandName) {
    return sendCommand(commandCode, commandName, 0, NULL);
}

Result Effect::sendCommand(int commandCode, const char* commandName, uint32_t size, void* data) {
    status_t status = (*mHandle)->command(mHandle, commandCode, size, data, 0, NULL);
    return analyzeCommandStatus(commandName, sContextCallToCommand, status);
}

Result Effect::sendCommandReturningData(int commandCode, const char* commandName,
                                        uint32_t* replySize, void* replyData) {
    return sendCommandReturningData(commandCode, commandName, 0, NULL, replySize, replyData);
}

Result Effect::sendCommandReturningData(int commandCode, const char* commandName, uint32_t size,
                                        void* data, uint32_t* replySize, void* replyData) {
    uint32_t expectedReplySize = *replySize;
    status_t status = (*mHandle)->command(mHandle, commandCode, size, data, replySize, replyData);
    if (status == OK && *replySize != expectedReplySize) {
        status = -ENODATA;
    }
    return analyzeCommandStatus(commandName, sContextCallToCommand, status);
}

Result Effect::sendCommandReturningStatus(int commandCode, const char* commandName) {
    return sendCommandReturningStatus(commandCode, commandName, 0, NULL);
}

Result Effect::sendCommandReturningStatus(int commandCode, const char* commandName, uint32_t size,
                                          void* data) {
    uint32_t replyCmdStatus;
    uint32_t replySize = sizeof(uint32_t);
    return sendCommandReturningStatusAndData(commandCode, commandName, size, data, &replySize,
                                             &replyCmdStatus, replySize, [] {});
}

Result Effect::sendCommandReturningStatusAndData(int commandCode, const char* commandName,
                                                 uint32_t size, void* data, uint32_t* replySize,
                                                 void* replyData, uint32_t minReplySize,
                                                 CommandSuccessCallback onSuccess) {
    status_t status = (*mHandle)->command(mHandle, commandCode, size, data, replySize, replyData);
    Result retval;
    if (status == OK && minReplySize >= sizeof(uint32_t) && *replySize >= minReplySize) {
        uint32_t commandStatus = *reinterpret_cast<uint32_t*>(replyData);
        retval = analyzeCommandStatus(commandName, sContextResultOfCommand, commandStatus);
        if (commandStatus == OK) {
            onSuccess();
        }
    } else {
        retval = analyzeCommandStatus(commandName, sContextCallToCommand, status);
    }
    return retval;
}

Result Effect::setConfigImpl(int commandCode, const char* commandName, const EffectConfig& config,
                             const sp<IEffectBufferProviderCallback>& inputBufferProvider,
                             const sp<IEffectBufferProviderCallback>& outputBufferProvider) {
    effect_config_t halConfig;
    EffectUtils::effectConfigToHal(config, &halConfig);
    if (inputBufferProvider != 0) {
        LOG_FATAL("Using input buffer provider is not supported");
    }
    if (outputBufferProvider != 0) {
        LOG_FATAL("Using output buffer provider is not supported");
    }
    return sendCommandReturningStatus(commandCode, commandName, sizeof(effect_config_t),
                                      &halConfig);
}

Result Effect::setParameterImpl(uint32_t paramSize, const void* paramData, uint32_t valueSize,
                                const void* valueData) {
    std::vector<uint8_t> halParamBuffer;
    if (!parameterToHal(paramSize, paramData, valueSize, &valueData, &halParamBuffer)) {
        return Result::INVALID_ARGUMENTS;
    }
    return sendCommandReturningStatus(EFFECT_CMD_SET_PARAM, "SET_PARAM", halParamBuffer.size(),
                                      &halParamBuffer[0]);
}

// Methods from ::android::hardware::audio::effect::CPP_VERSION::IEffect follow.
Return<Result> Effect::init() {
    return sendCommandReturningStatus(EFFECT_CMD_INIT, "INIT");
}

Return<Result> Effect::setConfig(const EffectConfig& config,
                                 const sp<IEffectBufferProviderCallback>& inputBufferProvider,
                                 const sp<IEffectBufferProviderCallback>& outputBufferProvider) {
    return setConfigImpl(EFFECT_CMD_SET_CONFIG, "SET_CONFIG", config, inputBufferProvider,
                         outputBufferProvider);
}

Return<Result> Effect::reset() {
    return sendCommand(EFFECT_CMD_RESET, "RESET");
}

Return<Result> Effect::enable() {
    return sendCommandReturningStatus(EFFECT_CMD_ENABLE, "ENABLE");
}

Return<Result> Effect::disable() {
    return sendCommandReturningStatus(EFFECT_CMD_DISABLE, "DISABLE");
}

Return<Result> Effect::setAudioSource(
#if MAJOR_VERSION <= 6
        AudioSource source
#else
        const AudioSource& source
#endif
) {
    audio_source_t halSource;
    if (status_t status = HidlUtils::audioSourceToHal(source, &halSource); status == NO_ERROR) {
        uint32_t halSourceParam = static_cast<uint32_t>(halSource);
        return sendCommand(EFFECT_CMD_SET_AUDIO_SOURCE, "SET_AUDIO_SOURCE", sizeof(uint32_t),
                           &halSourceParam);
    } else {
        return analyzeStatus(__func__, "audioSourceToHal", sContextConversion, status);
    }
}

#if MAJOR_VERSION <= 6

Return<Result> Effect::setDevice(AudioDeviceBitfield device) {
    uint32_t halDevice = static_cast<uint32_t>(device);
    return sendCommand(EFFECT_CMD_SET_DEVICE, "SET_DEVICE", sizeof(uint32_t), &halDevice);
}

Return<Result> Effect::setInputDevice(AudioDeviceBitfield device) {
    uint32_t halDevice = static_cast<uint32_t>(device);
    return sendCommand(EFFECT_CMD_SET_INPUT_DEVICE, "SET_INPUT_DEVICE", sizeof(uint32_t),
                       &halDevice);
}

#else  // MAJOR_VERSION <= 6

Return<Result> Effect::setDevice(const DeviceAddress& device) {
    audio_devices_t halDevice;
    char halDeviceAddress[AUDIO_DEVICE_MAX_ADDRESS_LEN];
    if (status_t status = HidlUtils::deviceAddressToHal(device, &halDevice, halDeviceAddress);
        status == NO_ERROR) {
        uint32_t halDeviceParam = static_cast<uint32_t>(halDevice);
        return sendCommand(EFFECT_CMD_SET_DEVICE, "SET_DEVICE", sizeof(uint32_t), &halDeviceParam);
    } else {
        return analyzeStatus(__func__, "deviceAddressToHal", sContextConversion, status);
    }
}

Return<Result> Effect::setInputDevice(const DeviceAddress& device) {
    audio_devices_t halDevice;
    char halDeviceAddress[AUDIO_DEVICE_MAX_ADDRESS_LEN];
    if (status_t status = HidlUtils::deviceAddressToHal(device, &halDevice, halDeviceAddress);
        status == NO_ERROR) {
        uint32_t halDeviceParam = static_cast<uint32_t>(halDevice);
        return sendCommand(EFFECT_CMD_SET_INPUT_DEVICE, "SET_INPUT_DEVICE", sizeof(uint32_t),
                           &halDeviceParam);
    } else {
        return analyzeStatus(__func__, "deviceAddressToHal", sContextConversion, status);
    }
}

#endif  // MAJOR_VERSION <= 6

Return<void> Effect::setAndGetVolume(const hidl_vec<uint32_t>& volumes,
                                     setAndGetVolume_cb _hidl_cb) {
    uint32_t halDataSize;
    std::unique_ptr<uint8_t[]> halData = hidlVecToHal(volumes, &halDataSize);
    uint32_t halResultSize = halDataSize;
    std::vector<uint32_t> halResult(volumes.size(), 0);
    Result retval = sendCommandReturningData(EFFECT_CMD_SET_VOLUME, "SET_VOLUME", halDataSize,
                                             &halData[0], &halResultSize, &halResult[0]);
    hidl_vec<uint32_t> result;
    if (retval == Result::OK) {
        result.setToExternal(&halResult[0], halResultSize);
    }
    _hidl_cb(retval, result);
    return Void();
}

Return<Result> Effect::volumeChangeNotification(const hidl_vec<uint32_t>& volumes) {
    uint32_t halDataSize;
    std::unique_ptr<uint8_t[]> halData = hidlVecToHal(volumes, &halDataSize);
    return sendCommand(EFFECT_CMD_SET_VOLUME, "SET_VOLUME", halDataSize, &halData[0]);
}

Return<Result> Effect::setAudioMode(AudioMode mode) {
    uint32_t halMode = static_cast<uint32_t>(mode);
    return sendCommand(EFFECT_CMD_SET_AUDIO_MODE, "SET_AUDIO_MODE", sizeof(uint32_t), &halMode);
}

Return<Result> Effect::setConfigReverse(
    const EffectConfig& config, const sp<IEffectBufferProviderCallback>& inputBufferProvider,
    const sp<IEffectBufferProviderCallback>& outputBufferProvider) {
    return setConfigImpl(EFFECT_CMD_SET_CONFIG_REVERSE, "SET_CONFIG_REVERSE", config,
                         inputBufferProvider, outputBufferProvider);
}

Return<void> Effect::getConfig(getConfig_cb _hidl_cb) {
    getConfigImpl(EFFECT_CMD_GET_CONFIG, "GET_CONFIG", _hidl_cb);
    return Void();
}

Return<void> Effect::getConfigReverse(getConfigReverse_cb _hidl_cb) {
    getConfigImpl(EFFECT_CMD_GET_CONFIG_REVERSE, "GET_CONFIG_REVERSE", _hidl_cb);
    return Void();
}

Return<void> Effect::getSupportedAuxChannelsConfigs(uint32_t maxConfigs,
                                                    getSupportedAuxChannelsConfigs_cb _hidl_cb) {
    hidl_vec<EffectAuxChannelsConfig> result;
    Result retval = getSupportedConfigsImpl(
        EFFECT_FEATURE_AUX_CHANNELS, maxConfigs, sizeof(channel_config_t),
        [&](uint32_t supportedConfigs, void* configsData) {
            result.resize(supportedConfigs);
            channel_config_t* config = reinterpret_cast<channel_config_t*>(configsData);
            for (size_t i = 0; i < result.size(); ++i) {
                effectAuxChannelsConfigFromHal(*config++, &result[i]);
            }
        });
    _hidl_cb(retval, result);
    return Void();
}

Return<void> Effect::getAuxChannelsConfig(getAuxChannelsConfig_cb _hidl_cb) {
    EffectAuxChannelsConfig result;
    Result retval = getCurrentConfigImpl(
        EFFECT_FEATURE_AUX_CHANNELS, sizeof(channel_config_t), [&](void* configData) {
            effectAuxChannelsConfigFromHal(*reinterpret_cast<channel_config_t*>(configData),
                                           &result);
        });
    _hidl_cb(retval, result);
    return Void();
}

Return<Result> Effect::setAuxChannelsConfig(const EffectAuxChannelsConfig& config) {
    std::vector<uint32_t> halCmd(
            alignedSizeIn<uint32_t>(sizeof(uint32_t) + sizeof(channel_config_t)), 0);
    halCmd[0] = EFFECT_FEATURE_AUX_CHANNELS;
    effectAuxChannelsConfigToHal(config, reinterpret_cast<channel_config_t*>(&halCmd[1]));
    return sendCommandReturningStatus(EFFECT_CMD_SET_FEATURE_CONFIG,
                                      "SET_FEATURE_CONFIG AUX_CHANNELS", halCmd.size(), &halCmd[0]);
}

Return<Result> Effect::offload(const EffectOffloadParameter& param) {
    effect_offload_param_t halParam;
    effectOffloadParamToHal(param, &halParam);
    return sendCommandReturningStatus(EFFECT_CMD_OFFLOAD, "OFFLOAD", sizeof(effect_offload_param_t),
                                      &halParam);
}

Return<void> Effect::getDescriptor(getDescriptor_cb _hidl_cb) {
    effect_descriptor_t halDescriptor;
    memset(&halDescriptor, 0, sizeof(effect_descriptor_t));
    status_t status = (*mHandle)->get_descriptor(mHandle, &halDescriptor);
    EffectDescriptor descriptor;
    if (status == OK) {
        status = EffectUtils::effectDescriptorFromHal(halDescriptor, &descriptor);
    }
    _hidl_cb(analyzeStatus("get_descriptor", "", sContextCallFunction, status), descriptor);
    return Void();
}

Return<void> Effect::command(uint32_t commandId, const hidl_vec<uint8_t>& data,
                             uint32_t resultMaxSize, command_cb _hidl_cb) {
    uint32_t halDataSize;
    std::unique_ptr<uint8_t[]> halData = hidlVecToHal(data, &halDataSize);
    uint32_t halResultSize = resultMaxSize;
    std::unique_ptr<uint8_t[]> halResult(new uint8_t[halResultSize]);
    memset(&halResult[0], 0, halResultSize);

    void* dataPtr = halDataSize > 0 ? &halData[0] : NULL;
    void* resultPtr = halResultSize > 0 ? &halResult[0] : NULL;
    status_t status = BAD_VALUE;
    switch (commandId) {
        case 'gtid':  // retrieve the tid, used for spatializer priority boost
            if (halDataSize == 0 && resultMaxSize == sizeof(int32_t)) {
                auto ptid = (int32_t*)resultPtr;
                ptid[0] = mProcessThread ? mProcessThread->getTid() : -1;
                status = OK;
                break;  // we have handled 'gtid' here.
            }
            [[fallthrough]];  // allow 'gtid' overload (checked halDataSize and resultMaxSize).
        default:
            status = (*mHandle)->command(mHandle, commandId, halDataSize, dataPtr, &halResultSize,
                                         resultPtr);
            break;
    }
    hidl_vec<uint8_t> result;
    if (status == OK && resultPtr != NULL) {
        result.setToExternal(&halResult[0], halResultSize);
    }
    _hidl_cb(status, result);
    return Void();
}

Return<Result> Effect::setParameter(const hidl_vec<uint8_t>& parameter,
                                    const hidl_vec<uint8_t>& value) {
    return setParameterImpl(parameter.size(), &parameter[0], value.size(), &value[0]);
}

Return<void> Effect::getParameter(const hidl_vec<uint8_t>& parameter, uint32_t valueMaxSize,
                                  getParameter_cb _hidl_cb) {
    hidl_vec<uint8_t> value;
    Result retval = getParameterImpl(
        parameter.size(), &parameter[0], valueMaxSize,
        [&](uint32_t valueSize, const void* valueData) {
            value.setToExternal(reinterpret_cast<uint8_t*>(const_cast<void*>(valueData)),
                                valueSize);
        });
    _hidl_cb(retval, value);
    return Void();
}

Return<void> Effect::getSupportedConfigsForFeature(uint32_t featureId, uint32_t maxConfigs,
                                                   uint32_t configSize,
                                                   getSupportedConfigsForFeature_cb _hidl_cb) {
    uint32_t configCount = 0;
    hidl_vec<uint8_t> result;
    Result retval = getSupportedConfigsImpl(featureId, maxConfigs, configSize,
                                            [&](uint32_t supportedConfigs, void* configsData) {
                                                configCount = supportedConfigs;
                                                result.resize(configCount * configSize);
                                                memcpy(&result[0], configsData, result.size());
                                            });
    _hidl_cb(retval, configCount, result);
    return Void();
}

Return<void> Effect::getCurrentConfigForFeature(uint32_t featureId, uint32_t configSize,
                                                getCurrentConfigForFeature_cb _hidl_cb) {
    hidl_vec<uint8_t> result;
    Result retval = getCurrentConfigImpl(featureId, configSize, [&](void* configData) {
        result.resize(configSize);
        memcpy(&result[0], configData, result.size());
    });
    _hidl_cb(retval, result);
    return Void();
}

Return<Result> Effect::setCurrentConfigForFeature(uint32_t featureId,
                                                  const hidl_vec<uint8_t>& configData) {
    std::vector<uint32_t> halCmd(alignedSizeIn<uint32_t>(sizeof(uint32_t) + configData.size()), 0);
    halCmd[0] = featureId;
    memcpy(&halCmd[1], &configData[0], configData.size());
    return sendCommandReturningStatus(EFFECT_CMD_SET_FEATURE_CONFIG, "SET_FEATURE_CONFIG",
                                      halCmd.size(), &halCmd[0]);
}

Return<Result> Effect::close() {
    if (mStopProcessThread.load(std::memory_order_relaxed)) {  // only this thread modifies
        return Result::INVALID_STATE;
    }
    mStopProcessThread.store(true, std::memory_order_release);
    if (mEfGroup) {
        mEfGroup->wake(static_cast<uint32_t>(MessageQueueFlagBits::REQUEST_QUIT));
    }
#if MAJOR_VERSION <= 5
    return Result::OK;
#elif MAJOR_VERSION >= 6
    // No need to join the processing thread, it is part of the API contract that the client
    // must finish processing before closing the effect.
    Result retval =
            analyzeStatus("EffectRelease", "", sContextCallFunction, EffectRelease(mHandle));
    EffectMap::getInstance().remove(mHandle);
    return retval;
#endif
}

Return<void> Effect::debug(const hidl_handle& fd, const hidl_vec<hidl_string>& /* options */) {
    if (fd.getNativeHandle() != nullptr && fd->numFds == 1) {
        uint32_t cmdData = fd->data[0];
        (void)sendCommand(EFFECT_CMD_DUMP, "DUMP", sizeof(cmdData), &cmdData);
        const std::string s = mStatistics->dump();
        if (s.size() != 0) write(cmdData, s.c_str(), s.size());
    }
    return Void();
}

}  // namespace implementation
}  // namespace CPP_VERSION
}  // namespace effect
}  // namespace audio
}  // namespace hardware
}  // namespace android