Change pipeline to use lame plugin
[gstfs.git] / xcode.c
blob427efa817e8bcd64f9ec6e18dde74eddf5114e64
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 ! "
79 "vorbisdec ! audioconvert ! lame bitrate=160 ! "
80 "fdsink name=\"_dest\" sync=false";
82 int pipefds[2];
84 struct pipe_params thread_params;
85 pthread_t thread;
86 void *thread_status;
88 pipeline = gst_parse_launch(pipeline_str, &error);
89 if (error)
91 fprintf(stderr, "Error parsing pipeline: %s\n", error->message);
92 return -1;
95 source = gst_bin_get_by_name(GST_BIN(pipeline), "_source");
96 dest = gst_bin_get_by_name(GST_BIN(pipeline), "_dest");
98 if (!pipeline || !source || !dest)
100 fprintf(stderr, "Could not initialize pipeline\n");
101 return -2;
104 if (pipe(pipefds))
106 perror("gstfs");
107 return -1;
110 thread_params.fd = pipefds[0];
111 thread_params.add_data_cb = add_data_cb;
112 thread_params.user_data = user_data;
114 pthread_create(&thread, NULL, send_pipe, (void *) &thread_params);
116 g_object_set(G_OBJECT(source), "location", filename, NULL);
117 g_object_set(G_OBJECT(dest), "fd", pipefds[1], NULL);
119 bus = gst_pipeline_get_bus(GST_PIPELINE(pipeline));
120 gst_bus_add_signal_watch(bus);
121 gst_element_set_state(pipeline, GST_STATE_PLAYING);
122 GstMessage *message = gst_bus_poll(bus, GST_MESSAGE_EOS |
123 GST_MESSAGE_ERROR, -1);
124 gst_message_unref(message);
126 // close read-side so pipe will terminate
127 close(pipefds[1]);
128 pthread_join(thread, thread_status);
130 return 0;