Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
3.1 kB
5
Indexable
Never
#include <gst/gst.h>

static void on_pad_added(GstElement *element, GstPad *pad, gpointer data) {
        
    GstElement *decoder = (GstElement *)data;
    GstPad *sink_pad = gst_element_get_static_pad(decoder, "sink");
    GstCaps *caps = gst_pad_query_caps(pad, NULL);
    const gchar *media_type = gst_structure_get_name(gst_caps_get_structure(caps, 0));

  /* If our converter is already linked, we have nothing to do here */
  if (gst_pad_is_linked (sink_pad)) {
    g_print ("We are already linked. Ignoring.\n");
    goto exit;
  }
    
    if (g_strrstr(media_type, "audio")) {
        g_print("Audio media type detected\n");
        GstPadLinkReturn ret = gst_pad_link(pad, sink_pad);
        if (GST_PAD_LINK_FAILED(ret)) {
            g_printerr("Failed to link audio pads\n");
        } else {
            g_print("Audio pads linked\n");
        }
    } else if (g_strrstr(media_type, "video")) {
        g_print("Video media type detected\n");
        // Handle video here, for now just print message
    } else {
        g_print("Unsupported media type\n");
    }
exit:
    gst_object_unref(sink_pad);
    gst_caps_unref(caps);
}

int main(int argc, char *argv[]) {
    GMainLoop *loop;
    GstElement *pipeline, *source, *decoder, *audio_convert, *audio_sink, *video_convert, *encoder, *pay, *video_sink;

    gst_init(NULL, NULL);
    loop = g_main_loop_new(NULL, FALSE);

    if (argc != 2) {
        g_printerr("Usage: %s <filename>\n", argv[0]);
        return -1;
    }

    pipeline = gst_pipeline_new("audio-video-player");
    source = gst_element_factory_make("filesrc", "file-source");
    decoder = gst_element_factory_make("decodebin", "decoder");


    gst_bin_add_many(GST_BIN(pipeline), source, decoder, NULL);
    gst_element_link(source, decoder);

    g_signal_connect(decoder, "pad-added", G_CALLBACK(on_pad_added), decoder);

    audio_convert = gst_element_factory_make("audioconvert", "audio-convert");
    audio_sink = gst_element_factory_make("autoaudiosink", "audio-output");

    video_convert = gst_element_factory_make("videoconvert", "video-convert");
    encoder = gst_element_factory_make("x264enc", "encoder");
    pay = gst_element_factory_make("rtph264pay", "payloader");
    video_sink = gst_element_factory_make("udpsink", "udp-output");

    g_object_set(video_sink, "host", "127.0.0.1", "port", 5000, NULL);

    gst_bin_add_many(GST_BIN(pipeline), audio_convert, audio_sink, video_convert, encoder, pay, video_sink, NULL);

    gst_element_link_many(audio_convert, audio_sink, NULL);
    gst_element_link_many(video_convert, encoder, pay, video_sink, NULL);

    g_object_set(source, "location", argv[1], NULL);

    if (gst_element_set_state(pipeline, GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE) {
        g_printerr("Unable to set the pipeline to the playing state.\n");
        gst_object_unref(pipeline);
        return -1;
    }


    g_main_loop_run(loop);

    gst_element_set_state(pipeline, GST_STATE_NULL);
    gst_object_unref(pipeline);
    g_main_loop_unref(loop);

    return 0;
}
Leave a Comment