First functional version of gstfs, with hardcoded stuff everywhere
[gstfs.git] / xcode.c
blob4def741d12b7b774bdefbdc4921492fb787dadf4
1 /*
2 * gstfs - gstreamer glue routines for transcoding
3 */
5 #include <gst/gst.h>
6 #include <glib.h>
7 #include <unistd.h>
8 #include <sys/wait.h>
9 #include <pthread.h>
11 struct pipe_params
13 int fd;
14 void (*add_data_cb)(char *, size_t, void *);
15 void *user_data;
19 /*
20 static gboolean bus_call (GstBus *bus, GstMessage *msg, gpointer data)
22 gchar *debug;
23 GError *error;
24 done_cb_t callback = (done_cb_t) data;
26 switch (GST_MESSAGE_TYPE (msg)) {
28 case GST_MESSAGE_EOS:
29 g_main_loop_quit (loop);
30 break;
32 case GST_MESSAGE_ERROR:
33 gst_message_parse_error (msg, &error, &debug);
34 g_free (debug);
36 g_printerr ("Error: %s\n", error->message);
37 g_error_free (error);
39 g_main_loop_quit (loop);
40 break;
42 default:
43 break;
45 return TRUE;
49 void close_pipe(void *data)
51 int fd = (int) data;
52 printf("close pipe! %d\n", fd);
53 close(fd);
56 void *send_pipe(void *data)
58 struct pipe_params *param = (struct pipe_params *) data;
59 char buf[PIPE_BUF];
60 size_t sizeread;
62 while ((sizeread = read(param->fd, buf, sizeof(buf))) > 0)
64 param->add_data_cb(buf, sizeread, param->user_data);
66 return NULL;
70 * Transcodes a file into a buffer, blocking until done.
72 int transcode(char *filename, void (*add_data_cb)(char *, size_t, void *),
73 void *user_data)
75 GstElement *pipeline, *source, *dest;
76 GError *error = NULL;
77 GstBus *bus;
78 char *pipeline_str = "filesrc name=\"_source\" ! oggdemux ! vorbisdec ! audioconvert ! wavenc ! fdsink name=\"_dest\" sync=false";
79 int pipefds[2];
81 struct pipe_params thread_params;
82 pthread_t thread;
83 void *thread_status;
85 pipeline = gst_parse_launch(pipeline_str, &error);
86 if (error)
88 fprintf(stderr, "Error parsing pipeline: %s\n", error->message);
89 return -1;
92 source = gst_bin_get_by_name(GST_BIN(pipeline), "_source");
93 dest = gst_bin_get_by_name(GST_BIN(pipeline), "_dest");
95 if (!pipeline || !source || !dest)
97 fprintf(stderr, "Could not initialize pipeline\n");
98 return -2;
101 if (pipe(pipefds))
103 perror("gstfs");
104 return -1;
107 thread_params.fd = pipefds[0];
108 thread_params.add_data_cb = add_data_cb;
109 thread_params.user_data = user_data;
111 pthread_create(&thread, NULL, send_pipe, (void *) &thread_params);
113 g_object_set(G_OBJECT(source), "location", filename, NULL);
114 g_object_set(G_OBJECT(dest), "fd", pipefds[1], NULL);
116 bus = gst_pipeline_get_bus(GST_PIPELINE(pipeline));
117 gst_bus_add_signal_watch(bus);
118 gst_element_set_state(pipeline, GST_STATE_PLAYING);
119 GstMessage *message = gst_bus_poll(bus, GST_MESSAGE_EOS |
120 GST_MESSAGE_ERROR, -1);
121 gst_message_unref(message);
123 // close read-side so pipe will terminate
124 close(pipefds[1]);
125 pthread_join(&thread, thread_status);
127 return 0;