#include "start_play_thread.h"

#include "audio_play_thread.h"
#include "video_state.h"

#include <QLoggingCategory>
Q_LOGGING_CATEGORY(playerControllerStartPlayThread, "player.controller.StartPlayThread")

StartPlayThread::StartPlayThread(AudioPlayThread* audioPlayThread, VideoStateData* videoStateData)
    : m_audioPlayThread(audioPlayThread), m_videoStateData(videoStateData)
{}

StartPlayThread::~StartPlayThread() {}

void StartPlayThread::run()
{
    assert(m_videoStateData);
    assert(m_audioPlayThread);

    if (isExit())
        return;
    bool ret = false;

    float vol = 1.0;
    AVSampleFormat sample_fmt = AV_SAMPLE_FMT_S16; // play out format

    VideoStateData* pVideoStateData = m_videoStateData;
    if (pVideoStateData) {
        AVCodecContext* pAudio = pVideoStateData->get_contex(AVMEDIA_TYPE_AUDIO);
        VideoState* pState = pVideoStateData->get_state();
        if (pAudio) {
            auto* pThread = m_audioPlayThread;
            if (pThread) {
                ret = pThread->init_device(pAudio->sample_rate,
                                           pAudio->ch_layout.nb_channels,
                                           sample_fmt,
                                           vol); // pAudio->sample_fmt
                if (ret) {
                    ret = pThread->init_resample_param(pAudio, sample_fmt, pState);
                }
            }
        }
    }
    // 可加回调或日志
    qCDebug(playerControllerStartPlayThread) << "[StartPlayThread] run finished, ret=" << ret;
}
