Fix whitespace in README
[gstfs.git] / xcode.c
blob2d69181dc33c15f7003c6c49dec61cd4feeedb7c
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;
18 void *send_pipe(void *data)
20 struct pipe_params *param = (struct pipe_params *) data;
21 char buf[PIPE_BUF];
22 size_t sizeread;
24 while ((sizeread = read(param->fd, buf, sizeof(buf))) > 0)
26 param->add_data_cb(buf, sizeread, param->user_data);
28 return NULL;
32 * Transcodes a file into a buffer, blocking until done.
34 int transcode(char *pipeline_str, char *filename,
35 void (*add_data_cb)(char *, size_t, void *), void *user_data)
37 GstElement *pipeline, *source, *dest;
38 GError *error = NULL;
39 GstBus *bus;
40 int pipefds[2];
42 struct pipe_params thread_params;
43 pthread_t thread;
44 void *thread_status;
46 pipeline = gst_parse_launch(pipeline_str, &error);
47 if (error)
49 fprintf(stderr, "Error parsing pipeline: %s\n", error->message);
50 return -1;
53 source = gst_bin_get_by_name(GST_BIN(pipeline), "_source");
54 dest = gst_bin_get_by_name(GST_BIN(pipeline), "_dest");
56 if (!pipeline || !source || !dest)
58 fprintf(stderr, "Could not initialize pipeline\n");
59 return -2;
62 if (pipe(pipefds))
64 perror("gstfs");
65 return -1;
68 thread_params.fd = pipefds[0];
69 thread_params.add_data_cb = add_data_cb;
70 thread_params.user_data = user_data;
72 pthread_create(&thread, NULL, send_pipe, (void *) &thread_params);
74 g_object_set(G_OBJECT(source), "location", filename, NULL);
75 g_object_set(G_OBJECT(dest), "fd", pipefds[1], NULL);
77 bus = gst_pipeline_get_bus(GST_PIPELINE(pipeline));
78 gst_bus_add_signal_watch(bus);
79 gst_element_set_state(pipeline, GST_STATE_PLAYING);
80 GstMessage *message = gst_bus_poll(bus, GST_MESSAGE_EOS |
81 GST_MESSAGE_ERROR, -1);
82 gst_message_unref(message);
84 // close read-side so pipe will terminate
85 close(pipefds[1]);
86 pthread_join(thread, thread_status);
88 return 0;