/*
 * Copyright (c) 2024 Huawei Device Co., Ltd.
 * 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.
 */

#ifndef SERVERDEC_SAMPLE_H
#define SERVERDEC_SAMPLE_H

#include <atomic>
#include <cstdio>
#include <fstream>
#include <iostream>
#include <mutex>
#include <queue>
#include <string>
#include <thread>
#include <unistd.h>
#include <unordered_map>
#include "hevc_decoder.h"
#include "codecbase.h"
#include "surface/window.h"
namespace OHOS {
namespace MediaAVCodec {

class VDecSignal {
public:
    std::mutex inMutex_;
    std::condition_variable inCond_;
    std::queue<uint32_t> inIdxQueue_;
    std::queue<std::shared_ptr<AVBuffer>> inBufferQueue_;
};

class VDecServerSample : public NoCopyable {
public:
    VDecServerSample() = default;
    ~VDecServerSample();

    void RunVideoServerSurfaceDecoder();
    int32_t ConfigServerDecoder();
    int32_t SetParameter();
    int32_t SetCallback();
    void GetOutputFormat();
    void Flush();
    void Stop();
    void Reset();
    void InputFunc();
    void WaitForEos();
    int32_t SetOutputSurface();
    int32_t InitDecoder();
    std::shared_ptr<VDecSignal> signal_;
    const uint8_t *fuzzData;
    size_t fuzzSize;
    int32_t sendFrameIndex = 0;
    std::vector<sptr<Surface>> cs_vector;
    std::vector<sptr<Surface>> ps_vector;
    const char *outDIR = "/data/test/media/VDecTest.yuv";
protected:
    std::shared_ptr<CodecBase> codec_;
    std::atomic<bool> isRunning_ { false };
    std::unique_ptr<std::thread> inputLoop_;
    struct CallBack : public MediaCodecCallback {
        explicit CallBack(VDecServerSample* tester) : tester(tester) {}
        ~CallBack() override = default;
        void OnError(AVCodecErrorType errorType, int32_t errorCode) override;
        void OnOutputFormatChanged(const Format &format) override;
        void OnInputBufferAvailable(uint32_t index, std::shared_ptr<AVBuffer> buffer) override;
        void OnOutputBufferAvailable(uint32_t index, std::shared_ptr<AVBuffer> buffer) override;
    private:
        VDecServerSample* tester;
        int32_t nCount { 0 };
    };
};

class ConsumerListener : public IBufferConsumerListener {
public:
    ConsumerListener(sptr<Surface> cs) : cs_(cs){};
    ~ConsumerListener() {}
    void OnBufferAvailable() override
    {
        sptr<SurfaceBuffer> buffer;
        int32_t flushFence;
        cs_->AcquireBuffer(buffer, flushFence, timestamp_, damage_);
        cs_->ReleaseBuffer(buffer, -1);
    }

private:
    int64_t timestamp_ = 0;
    Rect damage_ = {};
    sptr<Surface> cs_{nullptr};
};
} // namespace Media
} // namespace OHOS

#endif // SERVERDEC_SAMPLE_H