#include "tee_manager.hpp"

TeeManager::TeeManager() {}

// Definition of static function
bool TeeManager::create_tee() {
    /* Create tee to render buffer and send message simultaneously */
    tee = gst_element_factory_make("tee", "nvsink-tee");
    if (!tee) {
        g_printerr("tee could not be created. Exiting.\n");
        return false;
    }

    /* Create queues */
    queue1 = gst_element_factory_make("queue", "msg-queue");
    queue2 = gst_element_factory_make("queue", "video-render-queue");
    if (!queue1) {
        g_printerr("queue1 could not be created. Exiting.\n");
        return false;
    }
    if (!queue2) {
        g_printerr("queue2 could not be created. Exiting.\n");
        return false;
    }

    return true;
}

bool TeeManager::create_queue_pads() {
    sink_pad1 = gst_element_get_static_pad(queue1, "sink");
    sink_pad2 = gst_element_get_static_pad(queue2, "sink");
    if (!sink_pad1 || !sink_pad2) {
        g_printerr("Unable to get request pads\n");
        return false;
    }
    return true;
}

bool TeeManager::create_tee_pads() {
    tee_msg_pad = gst_element_request_pad_simple(tee, "src_%u");
    tee_render_pad = gst_element_request_pad_simple(tee, "src_%u");
    // Request pads: pads that do not exist until you ask for them.
    // Some elements (like tee, nvstreammux dynamic sources, nvmsgconv) allow
    // multiple outputs, but don’t create all the pads in advance. You ask the
    // element: “Give me a new pad to connect to something. Return: a new
    // GstPad* you can link to your downstream element.
    if (!tee_msg_pad || !tee_render_pad) {
        g_printerr("Unable to get request pads\n");
        return false;
    }
    return true;
}