/* vokoscreenNG - A desktop recorder
 * Copyright (C) 2017-2026 Volker Kohaupt
 *
 * Author:
 *      Volker Kohaupt <vkohaupt@volkoh.de>
 *
 * This file is free software; you can redistribute it and/or modify
 * it under the terms of version 2 of the GNU General Public License
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * --End_License--
 */

#include "QvkAudioPipewireWatcher_wl.h"
#include "global.h"

#include <QString>

QvkAudioPipewireWatcher_wl::QvkAudioPipewireWatcher_wl( Ui_formMainWindow_wl *ui_mainwindow )
{
    ui = ui_mainwindow;
    startAudioPipewireMonitoring();
}


QvkAudioPipewireWatcher_wl::~QvkAudioPipewireWatcher_wl()
{}


GstBusSyncReply QvkAudioPipewireWatcher_wl::my_AudioPipewire_func(GstBus *bus, GstMessage *message, gpointer user_data)
{
    Q_UNUSED(bus)
    Q_UNUSED(user_data)

    switch ( GST_MESSAGE_TYPE( message ) ) {
    case GST_MESSAGE_DEVICE_ADDED:
    {
        GstDevice *gstDevice;
        gst_message_parse_device_added( message, &gstDevice );
        GstStructure *structure = gst_device_get_properties( gstDevice );
        QString api = QString( gst_structure_get_string( structure, "device.api" ) );
        if ( api == "alsa" ) {
            QString deviceID = QString( gst_structure_get_string( structure, "object.serial" ) );
            QString description = QString( gst_structure_get_string( structure, "node.description" ) );
            QString capture = QString( gst_structure_get_string( structure, "api.alsa.pcm.stream" ) );
            QString type;
            if( capture == "capture" ) {
                type = "Source";
            } else {
                type = "Playback";
            }
            QString action = "[Audio-device-added]";
            QString device = QString( gst_structure_get_string( structure, "node.name" ) );
            global::lineEditAudioPipewireWatcher_wl->setText( deviceID + ":::" +
                                                              description + ":::" +
                                                              type + ":::" +
                                                              api + ":::" +
                                                              action + ":::" +
                                                              device );
        }
        gst_structure_free( structure );
        gst_object_unref( gstDevice );
        break;
    }
    case GST_MESSAGE_DEVICE_REMOVED:
    {
        GstDevice *gstDevice;
        gst_message_parse_device_removed( message, &gstDevice );
        GstStructure *structure = gst_device_get_properties( gstDevice );
        QString deviceID = QString( gst_structure_get_string( structure, "object.serial" ) );
        QString description = QString( gst_structure_get_string( structure, "node.description" ) );
        QString capture = QString( gst_structure_get_string( structure, "api.alsa.pcm.stream" ) );
        QString type;
        if( capture == "capture" ) {
            type = "Source";
        } else {
            type = "Playback";
        }
        QString api = QString( gst_structure_get_string( structure, "device.api" ) );
        QString action = "[Audio-device-removed]";
        QString device = QString( gst_structure_get_string( structure, "node.name" ) );
        global::lineEditAudioPipewireWatcher_wl->setText( deviceID + ":::" +
                                                          description + ":::" +
                                                          type + ":::" +
                                                          api + ":::" +
                                                          action + ":::" +
                                                          device );
        gst_structure_free( structure );
        gst_object_unref( gstDevice );
        break;
    }
    default:
        break;
    }

    return GST_BUS_PASS;
}


void QvkAudioPipewireWatcher_wl::startAudioPipewireMonitoring()
{
    GstDeviceMonitor *monitor = gst_device_monitor_new();
    GstBus *bus = gst_device_monitor_get_bus( monitor );
    GstCaps *caps = gst_caps_new_empty_simple( "audio/x-raw" );
    // Wenn das Source weggelassen wird, werden alle Audiogeräte angezeigt
    gst_device_monitor_add_filter( monitor, "Audio", caps );
    gst_caps_unref( caps );
    gst_bus_set_sync_handler( bus, (GstBusSyncHandler)my_AudioPipewire_func, this, NULL );
    gst_object_unref( bus );
    gst_device_monitor_start( monitor );
}
