#include "avpacketqueuemanager.h"

AVPacketQueueManager::AVPacketQueueManager() {
    m_decodeQueue.reserve(QUEUECAPACITY);
    m_saveQueue.reserve(QUEUECAPACITY);
    m_pushQueue.reserve(QUEUECAPACITY);
}

void AVPacketQueueManager::enqueueDecodePacket(AVPacket *pkt) {
    QMutexLocker locker(&m_decodeMutex);
    AVPacket *clonedPacket = av_packet_clone(pkt);
    if (clonedPacket) m_decodeQueue.enqueue(clonedPacket);
}

void AVPacketQueueManager::enqueueSavePacket(AVPacket *pkt) {
    QMutexLocker locker(&m_saveMutex);
    AVPacket *clonedPacket = av_packet_clone(pkt);
    if (clonedPacket) m_saveQueue.enqueue(clonedPacket);
}

void AVPacketQueueManager::enqueuePushPacket(AVPacket *pkt) {
    QMutexLocker locker(&m_pushMutex);
    AVPacket *clonedPacket = av_packet_clone(pkt);
    if (clonedPacket) m_pushQueue.enqueue(clonedPacket);
}

AVPacket *AVPacketQueueManager::dequeueDecodePacket() {
    QMutexLocker locker(&m_decodeMutex);
    if (!m_decodeQueue.isEmpty()) {
        AVPacket *pkt = m_decodeQueue.dequeue();
        return pkt;
    }
    return nullptr;
}

AVPacket *AVPacketQueueManager::dequeueSavePacket() {
    QMutexLocker locker(&m_saveMutex);
    if (!m_saveQueue.isEmpty()) {
        AVPacket *pkt = m_saveQueue.dequeue();
        return pkt;
    }
    return nullptr;
}

AVPacket *AVPacketQueueManager::dequeuePushPacket() {
    QMutexLocker locker(&m_pushMutex);
    if (!m_pushQueue.isEmpty()) {
        AVPacket *pkt = m_pushQueue.dequeue();
        return pkt;
    }
    return nullptr;
}

void AVPacketQueueManager::clearDecodeQueue() {
    QMutexLocker locker(&m_decodeMutex);
    m_decodeQueue.clear();
}

void AVPacketQueueManager::clearSaveQueue() {
    QMutexLocker locker(&m_saveMutex);
    m_saveQueue.clear();
}

void AVPacketQueueManager::clearPushQueue() {
    QMutexLocker locker(&m_pushMutex);
    m_pushQueue.clear();
}

bool AVPacketQueueManager::isEmptySaveQueue() {
    QMutexLocker locker(&m_saveMutex);
    return m_saveQueue.isEmpty();
}
