Created
February 11, 2019 23:52
-
-
Save cfoch/a2c354ed88dc6d1494fa566cd94e1228 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <gst/gst.h> | |
#include <gtk/gtk.h> | |
//gcc $(pkg-config --cflags --libs gstreamer-1.0 glib-2.0 gtk+-3.0) simple3.c -o simple3 | |
struct _BusData { | |
GstElement *pipeline; | |
GMainLoop *loop; | |
}; | |
typedef struct _BusData BusData; | |
static gboolean | |
eos_cb(GstBus * bus, GstMessage * message, gpointer user_data) | |
{ | |
BusData *data = (BusData *) user_data; | |
g_print ("EOS\n"); | |
gst_element_set_state (data->pipeline, GST_STATE_NULL); | |
gst_bus_remove_signal_watch (bus); | |
gst_object_unref (data->pipeline); | |
gst_object_unref (bus); | |
g_main_loop_quit (data->loop); | |
g_free (data); | |
return TRUE; | |
} | |
void | |
play(GThreadPool *pool) | |
{ | |
GstElement *pipeline; | |
GstBus *bus; | |
BusData *data = g_new0 (BusData, 1); | |
GMainContext *context; | |
GSource *source; | |
GMainLoop *loop; | |
gint id; | |
pipeline = gst_parse_launch("filesrc location=key.webm ! decodebin ! autoaudiosink", NULL); | |
bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline)); | |
context = g_main_context_new (); | |
loop = g_main_loop_new (context, FALSE); | |
source = gst_bus_create_watch (bus); | |
g_source_set_callback (source, (GSourceFunc) gst_bus_async_signal_func, NULL, | |
NULL); | |
id = g_source_attach (source, context); | |
g_source_unref (source); | |
g_main_context_push_thread_default (context); | |
gst_bus_add_signal_watch (bus); | |
g_main_context_pop_thread_default (context); | |
data->pipeline = pipeline; | |
data->loop = loop; | |
g_signal_connect (bus, "message::eos", (GCallback) eos_cb, data); | |
gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_PLAYING); | |
g_thread_pool_push (pool, loop, NULL); | |
} | |
void | |
run_bus_watcher (gpointer data, gpointer common) | |
{ | |
GMainLoop *loop = (GMainLoop *) data; | |
g_main_loop_run (loop); | |
} | |
void | |
insert_text_cb(GtkTextBuffer * buf, GtkTextIter * iter, const gchar * text, | |
gint unused_len, gpointer user_data) | |
{ | |
GThreadPool *thread_pool = (GThreadPool *) user_data; | |
play(thread_pool); | |
} | |
int | |
main(int argc, char ** argv) | |
{ | |
GtkWidget *window; | |
GtkWidget *text_view; | |
GtkTextBuffer *buf; | |
GThreadPool *thread_pool; | |
gst_init (&argc, &argv); | |
gtk_init (&argc, &argv); | |
thread_pool = | |
g_thread_pool_new((GFunc) run_bus_watcher, NULL, 50, TRUE, NULL); | |
window = gtk_window_new (GTK_WINDOW_TOPLEVEL); | |
text_view = gtk_text_view_new (); | |
gtk_container_add (GTK_CONTAINER (window), text_view); | |
g_signal_connect (G_OBJECT (window), "destroy", gtk_main_quit, NULL); | |
buf = gtk_text_view_get_buffer (GTK_TEXT_VIEW (text_view)); | |
g_signal_connect (G_OBJECT (buf), "insert-text", insert_text_cb, thread_pool); | |
gtk_widget_show_all (GTK_WIDGET (window)); | |
gtk_main(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment