gtk_gl
authoryu.dongliang <18588496441@163.com>
Sun, 16 Apr 2023 12:08:24 +0000 (20:08 +0800)
committeryu.dongliang <18588496441@163.com>
Sun, 16 Apr 2023 12:08:24 +0000 (20:08 +0800)
Makefile
main.c
simp.c
simp.h
simp_gtk_gl.c [moved from simp_gl.c with 90% similarity]

index 37b2e305d8fe7e95662dd1070b0d2b50fca53177..4f2909a6196a96baa92389ac5c9763041a4b5be3 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -3,6 +3,7 @@ CFILES += simp.c
 CFILES += simp_ffmpeg_input.c
 CFILES += simp_ffmpeg_output.c
 CFILES += simp_ffmpeg_alsa.c
+CFILES += simp_gtk_gl.c
 CFILES += simp_filter.c
 
 CFLAGS += -g -O3
@@ -11,7 +12,7 @@ CFLAGS += -I./
 CFLAGS += `pkg-config --cflags gtk+-3.0`
 
 LDFLAGS += -lavdevice -lavformat -lavcodec -lavfilter -lavutil
-LDFLAGS += -lGL
+LDFLAGS += -lGL -lm
 LDFLAGS += `pkg-config --libs gtk+-3.0`
 
 all:
diff --git a/main.c b/main.c
index 3671a232b6f9dd94d5d6cdacb760565e5db640fe..d8545f8f785d2c6e219e1523fd8c7d801b7d213d 100644 (file)
--- a/main.c
+++ b/main.c
@@ -1,8 +1,6 @@
 #include <gtk/gtk.h>
 #include "simp.h"
 
-GtkWidget*  gl_area = NULL;
-
 void simp_file_add(GtkMenuButton* self, gpointer user_data)
 {
        printf("%s(),%d\n", __func__, __LINE__);
@@ -35,7 +33,7 @@ void simp_edit_play(GtkMenuButton* self, gpointer user_data)
 {
        printf("%s(),%d\n", __func__, __LINE__);
 
-       gtk_gl_area_queue_render(GTK_GL_AREA(gl_area));
+       gtk_gl_area_queue_render(GTK_GL_AREA(user_data));
 }
 
 static void resize (GtkGLArea* self, gint width, gint height, gpointer user_data)
@@ -65,9 +63,11 @@ static void unrealize(GtkWidget *widget)
        printf("%s(),%d\n", __func__, __LINE__);
 }
 
-static gboolean render(GtkGLArea *area, GdkGLContext *context)
+static gboolean render(GtkGLArea* self, GdkGLContext* context, gpointer user_data)
 {
-       if (gtk_gl_area_get_error (area) != NULL)
+       simp_avio_t* gl = user_data;
+
+       if (gtk_gl_area_get_error (self) != NULL)
                return FALSE;
 
        glViewport(0, 0, 1920, 1080);
@@ -76,6 +76,7 @@ static gboolean render(GtkGLArea *area, GdkGLContext *context)
        glClear (GL_COLOR_BUFFER_BIT);
 
        // draw
+       simp_avio_run(gl);
 
        glFlush ();
 
@@ -84,7 +85,7 @@ static gboolean render(GtkGLArea *area, GdkGLContext *context)
 
 static gboolean button_release_event (GtkWidget* self, GdkEventButton* event, gpointer user_data)
 {
-       gtk_gl_area_queue_render(GTK_GL_AREA(gl_area));
+       gtk_gl_area_queue_render(GTK_GL_AREA(user_data));
        return TRUE;
 }
 
@@ -110,20 +111,30 @@ int main(int argc, char *argv[])
 {
        printf("%s(),%d\n", __func__, __LINE__);
 
-       GtkBuilder *builder;
-       GObject    *window;
+       simp_filter_t* f    = NULL;
+       simp_avio_t*   in   = NULL;
+       simp_avio_t*   out  = NULL;
+       simp_avio_t*   alsa = NULL;
+       simp_avio_t*   gl   = NULL;
+
+       GtkBuilder*    builder;
+       GObject*       window;
 
-       GObject    *menu_file_quit;
-       GObject    *menu_file_add;
+       GObject*       menu_file_quit;
+       GObject*       menu_file_add;
 
-       GObject    *button_record;
-       GObject    *button_pause;
-       GObject    *button_stop;
+       GObject*       button_record;
+       GObject*       button_pause;
+       GObject*       button_stop;
 
-       GError     *error = NULL;
+       GObject*       gl_area;
+//     GtkWidget*     gl_area = NULL;
+       GError*        error = NULL;
 
        gtk_init (&argc, &argv);
 
+       avdevice_register_all();
+
        builder = gtk_builder_new();
        if (gtk_builder_add_from_file (builder, "simp.glade", &error) == 0)
        {   
@@ -141,22 +152,65 @@ int main(int argc, char *argv[])
        button_pause   = gtk_builder_get_object (builder, "button_pause");
        button_stop    = gtk_builder_get_object (builder, "button_stop");
 
-       gl_area        = GTK_WIDGET(gtk_builder_get_object(builder, "gl_area"));
+       gl_area        = gtk_builder_get_object (builder, "gl_area");
+//     gl_area        = GTK_WIDGET(gtk_builder_get_object(builder, "gl_area"));
+
+       if (simp_avio_open(&in, "ffmpeg_input", argv[1], NULL) < 0) {
+               scf_loge("\n");
+               return -1;
+       }
+
+       if (simp_avio_open(&alsa, "ffmpeg_alsa", NULL, "hw:0") < 0) {
+               scf_loge("\n");
+               return -1;
+       }
+
+       if (simp_avio_open(&gl, "gl", NULL, NULL) < 0) {
+               scf_loge("\n");
+               return -1;
+       }
+
+       if (simp_filter_open(&f) < 0) {
+               scf_loge("\n");
+               return -1;
+       }
+
+       simp_filter_add_input (f, in);
+       simp_filter_add_output(f, alsa);
+       simp_filter_add_output(f, gl);
 
        g_signal_connect(window,         "destroy",   G_CALLBACK(gtk_main_quit), NULL);
 
        g_signal_connect(menu_file_quit, "activate",  G_CALLBACK(gtk_main_quit), NULL);
        g_signal_connect(menu_file_add,  "activate",  G_CALLBACK(simp_file_add), builder);
 
-       g_signal_connect(button_record,  "clicked",   G_CALLBACK(simp_record),   builder);
-       g_signal_connect(button_pause,   "clicked",   G_CALLBACK(simp_pause),    builder);
-       g_signal_connect(button_stop,    "clicked",   G_CALLBACK(simp_stop),     builder);
+       g_signal_connect(button_record,  "clicked",   G_CALLBACK(simp_record),   f);
+       g_signal_connect(button_pause,   "clicked",   G_CALLBACK(simp_pause),    f);
+       g_signal_connect(button_stop,    "clicked",   G_CALLBACK(simp_stop),     f);
+
+       g_signal_connect(gl_area,        "resize",    G_CALLBACK(resize),        gl);
+       g_signal_connect(gl_area,        "unrealize", G_CALLBACK(unrealize),     gl);
+       g_signal_connect(gl_area,        "render",    G_CALLBACK(render),        gl);
+
+       g_signal_connect(window, "button-release-event", G_CALLBACK(button_release_event), f);
 
-       g_signal_connect(gl_area,        "resize",    G_CALLBACK(resize),        NULL);
-       g_signal_connect(gl_area,        "unrealize", G_CALLBACK(unrealize),     NULL);
-       g_signal_connect(gl_area,        "render",    G_CALLBACK(render),        NULL);
+       f->speed.num = 1;
+       f->speed.den = 1;
 
-       g_signal_connect(window, "button-release-event", G_CALLBACK(button_release_event), NULL);
+       if (simp_filter_run(f) < 0) {
+               scf_loge("\n");
+               return -1;
+       }
+
+       if (simp_avio_run(alsa) < 0) {
+               scf_loge("\n");
+               return -1;
+       }
+
+       if (simp_avio_run(in) < 0) {
+               scf_loge("\n");
+               return -1;
+       }
 
        gtk_widget_show_all(GTK_WIDGET(window));
 
diff --git a/simp.c b/simp.c
index 5fa4a6d32a33256541938fd451a5c5e0e933b351..0cf455724747bec4e802c568d7cc067003bad25d 100644 (file)
--- a/simp.c
+++ b/simp.c
@@ -3,12 +3,14 @@
 extern simp_avio_ops_t   simp_avio_ffmpeg_input;
 extern simp_avio_ops_t   simp_avio_ffmpeg_output;
 extern simp_avio_ops_t   simp_avio_ffmpeg_alsa;
+extern simp_avio_ops_t   simp_avio_gtk_gl;
 
 static simp_avio_ops_t*  avio_array[] =
 {
        &simp_avio_ffmpeg_input,
        &simp_avio_ffmpeg_output,
        &simp_avio_ffmpeg_alsa,
+       &simp_avio_gtk_gl,
        NULL,
 };
 
diff --git a/simp.h b/simp.h
index 8cce555fed40b8aad04dec14a7cc3d81bd021914..1767a8cde302e4d79275bfa500591a07225ed935 100644 (file)
--- a/simp.h
+++ b/simp.h
@@ -99,9 +99,13 @@ typedef struct {
        int                  width;
        int                  height;
 
+       int                  vopen;
+       int                  init;
+
        uint8_t              y[SIMP_GL_WIDTH * SIMP_GL_HEIGHT];
        uint8_t              u[SIMP_GL_WIDTH * SIMP_GL_HEIGHT / 4];
        uint8_t              v[SIMP_GL_WIDTH * SIMP_GL_HEIGHT / 4];
+
 } simp_gl_t;
 
 struct  simp_avio_s
similarity index 90%
rename from simp_gl.c
rename to simp_gtk_gl.c
index 1021b27e009489583061577da7d8936e8583a20b..1862081a08b687a8766d8333886a31bae417b796 100644 (file)
--- a/simp_gl.c
@@ -1,5 +1,4 @@
-#include"chess_gl.h"
-#include"pieces.h"
+#include"simp.h"
 
 const float G_PI = 3.1415926;
 
@@ -132,12 +131,18 @@ static int init_texture(GLuint* ptex, GLenum format, int w, int h, uint8_t* data
        return 0;
 }
 
-int _gl_open(simp_avio_t* io, const char* in, const char* out);
+int _gtk_gl_open(simp_avio_t* io, const char* in, const char* out)
 {
        simp_gl_t* gl = calloc(1, sizeof(simp_gl_t));
        if (!gl)
                return -ENOMEM;
 
+       io->priv = gl;
+       return 0;
+}
+
+static int _gl_init(simp_gl_t* gl)
+{
        init_buffers(gl);
 
        GLint status = 0;
@@ -156,24 +161,28 @@ int _gl_open(simp_avio_t* io, const char* in, const char* out);
        glGetShaderiv  (fragment_shader, GL_COMPILE_STATUS, &status);
        printf("#1, status: %d\n", status);
 
-       program = glCreateProgram();
-       glAttachShader(program, vertex_shader);
-       glAttachShader(program, fragment_shader);
-       glLinkProgram (program);
-       glGetProgramiv(program, GL_LINK_STATUS, &status);
+       gl->program = glCreateProgram();
+       glAttachShader(gl->program, vertex_shader);
+       glAttachShader(gl->program, fragment_shader);
+       glLinkProgram (gl->program);
+       glGetProgramiv(gl->program, GL_LINK_STATUS, &status);
        printf("#2, status: %d\n", status);
 
        init_texture(&gl->texture_y, GL_RED, SIMP_GL_WIDTH,     SIMP_GL_HEIGHT,     gl->y);
        init_texture(&gl->texture_u, GL_RED, SIMP_GL_WIDTH / 2, SIMP_GL_HEIGHT / 2, gl->u);
        init_texture(&gl->texture_v, GL_RED, SIMP_GL_WIDTH / 2, SIMP_GL_HEIGHT / 2, gl->v);
 
-       io->priv = gl;
        return 0;
 }
 
-int simp_gl_draw(simp_gl_t* gl)
+static int _gtk_gl_run(simp_avio_t* io)
 {
-       float mvp[16];
+       simp_gl_t* gl = io->priv;
+
+       float      mvp[16];
+
+       if (!gl->init)
+               _gl_init(gl);
 
        compute_mvp(mvp, 0, 0, 0);
 
@@ -213,11 +222,9 @@ int simp_gl_draw(simp_gl_t* gl)
        return 0;
 }
 
-simp_avio_ops_t  simp_avio_gl =
+simp_avio_ops_t  simp_avio_gtk_gl =
 {
-       .type     =  "gl",
-       .open     =  _gl_open,
-       .close    =  _gl_close,
-       .run      =  _gl_run,
-       .stop     =  _gl_stop,
+       .type     =  "gtk_gl",
+       .open     =  _gtk_gl_open,
+       .run      =  _gtk_gl_run,
 };