From: yu.dongliang <18588496441@163.com> Date: Sun, 16 Apr 2023 12:08:24 +0000 (+0800) Subject: gtk_gl X-Git-Url: http://baseworks.info/?a=commitdiff_plain;h=459e2d263d9ed18fde8337db9e073b35a41ab892;p=simplay.git gtk_gl --- diff --git a/Makefile b/Makefile index 37b2e30..4f2909a 100644 --- 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 3671a23..d8545f8 100644 --- a/main.c +++ b/main.c @@ -1,8 +1,6 @@ #include #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 5fa4a6d..0cf4557 100644 --- 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 8cce555..1767a8c 100644 --- 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 diff --git a/simp_gl.c b/simp_gtk_gl.c similarity index 90% rename from simp_gl.c rename to simp_gtk_gl.c index 1021b27..1862081 100644 --- a/simp_gl.c +++ b/simp_gtk_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, };