/****************************************************************************
 Copyright (c) 2016 Chukong Technologies Inc.
 Copyright (c) 2017-2023 Xiamen Yaji Software Co., Ltd.

 http://www.cocos2d-x.org

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights to
 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
 of the Software, and to permit persons to whom the Software is furnished to do so,
 subject to the following conditions:

 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
****************************************************************************/

#define LOG_TAG "PcmAudioService"

#include "base/Macros.h"
#include "audio/android/PcmAudioService.h"
#include "audio/android/AudioMixerController.h"
#include "audio/android/utils/Compat.h"

namespace cc {

static ccstd::vector<char> __silenceData;//NOLINT(bugprone-reserved-identifier, readability-identifier-naming)

#define AUDIO_PLAYER_BUFFER_COUNT (2)

class SLPcmAudioPlayerCallbackProxy {
public:
#if CC_PLATFORM == CC_PLATFORM_ANDROID
    static void samplePlayerCallback(CCSLBufferQueueItf bq, void *context) {
#elif CC_PLATFORM == CC_PLATFORM_OPENHARMONY
    static void samplePlayerCallback(CCSLBufferQueueItf bq, void *context, SLuint32 size) {
#endif
        auto *thiz = reinterpret_cast<PcmAudioService *>(context);
        thiz->bqFetchBufferCallback(bq);
    }
};

PcmAudioService::PcmAudioService(SLEngineItf engineItf, SLObjectItf outputMixObject)
: _engineItf(engineItf), _outputMixObj(outputMixObject), _playObj(nullptr), _playItf(nullptr), _volumeItf(nullptr), _bufferQueueItf(nullptr), _numChannels(-1), _sampleRate(-1), _bufferSizeInBytes(0), _controller(nullptr) {
}

PcmAudioService::~PcmAudioService() {
    ALOGV("PcmAudioServicee() (%p), before destroy play object", this);
    SL_DESTROY_OBJ(_playObj);
    ALOGV("PcmAudioServicee() end");
}

bool PcmAudioService::enqueue() {
    #if CC_PLATFORM == CC_PLATFORM_OPENHARMONY
        // We need to call this interface in openharmony, otherwise there will be noise
        SLuint8 *buffer = nullptr;
        SLuint32 size = 0;
        (*_bufferQueueItf)->GetBuffer(_bufferQueueItf, &buffer, &size);
    #endif
    if (_controller->hasPlayingTacks()) {
        if (_controller->isPaused()) {
            SLresult r = (*_bufferQueueItf)->Enqueue(_bufferQueueItf, __silenceData.data(), __silenceData.size());
            SL_RETURN_VAL_IF_FAILED(r, false, "enqueue silent data failed!");
        } else {
            _controller->mixOneFrame();

            auto *current = _controller->current();
            ALOG_ASSERT(current != nullptr, "current buffer is nullptr ...");
            SLresult r = (*_bufferQueueItf)->Enqueue(_bufferQueueItf, current->buf, current->size);
            SL_RETURN_VAL_IF_FAILED(r, false, "enqueue failed!");
        }
    } else {
        SLresult r = (*_bufferQueueItf)->Enqueue(_bufferQueueItf, __silenceData.data(), __silenceData.size());
        SL_RETURN_VAL_IF_FAILED(r, false, "enqueue silent data failed!");
    }

    return true;
}

void PcmAudioService::bqFetchBufferCallback(CCSLBufferQueueItf bq) {
    CC_UNUSED_PARAM(bq);
    // IDEA: PcmAudioService instance may be destroyed, we need to find a way to wait...
    // It's in sub thread
    enqueue();
}

bool PcmAudioService::init(AudioMixerController *controller, int numChannels, int sampleRate, int bufferSizeInBytes) {
    _controller = controller;
    _numChannels = numChannels;
    _sampleRate = sampleRate;
    _bufferSizeInBytes = bufferSizeInBytes;

    SLuint32 channelMask = SL_SPEAKER_FRONT_CENTER;

    if (numChannels > 1) {
        channelMask = SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT;
    }

    SLDataFormat_PCM formatPcm = {
        SL_DATAFORMAT_PCM,
        static_cast<SLuint32>(numChannels),
        static_cast<SLuint32>(sampleRate * 1000),
        SL_PCMSAMPLEFORMAT_FIXED_16,
        SL_PCMSAMPLEFORMAT_FIXED_16,
        channelMask,
        SL_BYTEORDER_LITTLEENDIAN};

#if CC_PLATFORM == CC_PLATFORM_ANDROID
    SLDataLocator_AndroidSimpleBufferQueue locBufQueue = {
        SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE,
        AUDIO_PLAYER_BUFFER_COUNT};
#elif CC_PLATFORM == CC_PLATFORM_OPENHARMONY
    SLDataLocator_BufferQueue locBufQueue = {SL_DATALOCATOR_BUFFERQUEUE, AUDIO_PLAYER_BUFFER_COUNT};
#endif
    SLDataSource source = {&locBufQueue, &formatPcm};

    SLDataLocator_OutputMix locOutmix = {
        SL_DATALOCATOR_OUTPUTMIX,
        _outputMixObj};
    SLDataSink sink = {&locOutmix, nullptr};

    const SLInterfaceID ids[] = {
        SL_IID_PLAY,
        SL_IID_VOLUME,
        CC_SL_IDD_BUFFER_QUEUE,
    };

    const SLboolean req[] = {
        SL_BOOLEAN_TRUE,
        SL_BOOLEAN_TRUE,
        SL_BOOLEAN_TRUE,
    };

    SLresult r;

    r = (*_engineItf)->CreateAudioPlayer(_engineItf, &_playObj, &source, &sink, sizeof(ids) / sizeof(ids[0]), ids, req);//NOLINT(bugprone-sizeof-expression)
    SL_RETURN_VAL_IF_FAILED(r, false, "CreateAudioPlayer failed");

    r = (*_playObj)->Realize(_playObj, SL_BOOLEAN_FALSE);
    SL_RETURN_VAL_IF_FAILED(r, false, "Realize failed");

    r = (*_playObj)->GetInterface(_playObj, SL_IID_PLAY, &_playItf);
    SL_RETURN_VAL_IF_FAILED(r, false, "GetInterface SL_IID_PLAY failed");

    r = (*_playObj)->GetInterface(_playObj, SL_IID_VOLUME, &_volumeItf);
    SL_RETURN_VAL_IF_FAILED(r, false, "GetInterface SL_IID_VOLUME failed");

    r = (*_playObj)->GetInterface(_playObj, CC_SL_IDD_BUFFER_QUEUE, &_bufferQueueItf);
    SL_RETURN_VAL_IF_FAILED(r, false, "GetInterface CC_SL_IDD_BUFFER_QUEUE failed");

    r = (*_bufferQueueItf)->RegisterCallback(_bufferQueueItf, SLPcmAudioPlayerCallbackProxy::samplePlayerCallback, this);
    SL_RETURN_VAL_IF_FAILED(r, false, "_bufferQueueItf RegisterCallback failed");

    if (__silenceData.empty()) {
        __silenceData.resize(_numChannels * _bufferSizeInBytes, 0x00);
    }
    #if CC_PLATFORM == CC_PLATFORM_OPENHARMONY
        // We need to call this interface in openharmony, otherwise there will be noise
        SLuint8 *buffer = nullptr;
        SLuint32 size = 0;
        (*_bufferQueueItf)->GetBuffer(_bufferQueueItf, &buffer, &size);
    #endif
    r = (*_bufferQueueItf)->Enqueue(_bufferQueueItf, __silenceData.data(), __silenceData.size());
    SL_RETURN_VAL_IF_FAILED(r, false, "_bufferQueueItf Enqueue failed");

    r = (*_playItf)->SetPlayState(_playItf, SL_PLAYSTATE_PLAYING);
    SL_RETURN_VAL_IF_FAILED(r, false, "SetPlayState failed");

    return true;
}

void PcmAudioService::pause() {
    SLresult r = (*_playItf)->SetPlayState(_playItf, SL_PLAYSTATE_PAUSED);
    SL_RETURN_IF_FAILED(r, "PcmAudioService::pause failed");
}

void PcmAudioService::resume() {
    SLresult r = (*_playItf)->SetPlayState(_playItf, SL_PLAYSTATE_PLAYING);
    SL_RETURN_IF_FAILED(r, "PcmAudioService::resume failed");
}

} // namespace cc
