2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
20 #include <sys/types.h>
26 #include <gsthttpsrc.h>
29 GstElementDetails gst_httpsrc_details = {
32 "Read data from an HTTP stream",
34 "Erik Walthinsen <omega@cse.ogi.edu>",
39 static void gst_httpsrc_push(GstSrc *src);
40 static gboolean gst_httpsrc_open_url(GstHttpSrc *src);
41 static void gst_httpsrc_close_url(GstHttpSrc *src);
42 static gboolean gst_httpsrc_change_state(GstElement *element,
43 GstElementState state);
46 /* HttpSrc signals and args */
60 static void gst_httpsrc_class_init(GstHttpSrcClass *klass);
61 static void gst_httpsrc_init(GstHttpSrc *httpsrc);
62 static void gst_httpsrc_set_arg(GtkObject *object,GtkArg *arg,guint id);
63 static void gst_httpsrc_get_arg(GtkObject *object,GtkArg *arg,guint id);
66 static GstSrcClass *parent_class = NULL;
67 //static guint gst_httpsrc_signals[LAST_SIGNAL] = { 0 };
70 gst_httpsrc_get_type(void) {
71 static GtkType httpsrc_type = 0;
74 static const GtkTypeInfo httpsrc_info = {
77 sizeof(GstHttpSrcClass),
78 (GtkClassInitFunc)gst_httpsrc_class_init,
79 (GtkObjectInitFunc)gst_httpsrc_init,
80 (GtkArgSetFunc)gst_httpsrc_set_arg,
81 (GtkArgGetFunc)gst_httpsrc_get_arg,
82 (GtkClassInitFunc)NULL,
84 httpsrc_type = gtk_type_unique(GST_TYPE_SRC,&httpsrc_info);
90 gst_httpsrc_class_init(GstHttpSrcClass *klass) {
91 GtkObjectClass *gtkobject_class;
92 GstElementClass *gstelement_class;
93 GstSrcClass *gstsrc_class;
95 gtkobject_class = (GtkObjectClass*)klass;
96 gstelement_class = (GstElementClass*)klass;
97 gstsrc_class = (GstSrcClass*)klass;
99 parent_class = gtk_type_class(GST_TYPE_SRC);
102 gtk_object_add_arg_type("GstHttpSrc::location", GTK_TYPE_STRING,
103 GTK_ARG_READWRITE, ARG_LOCATION);
104 gtk_object_add_arg_type("GstHttpSrc::bytesperread", GTK_TYPE_INT,
105 GTK_ARG_READWRITE, ARG_BYTESPERREAD);
107 gtkobject_class->set_arg = gst_httpsrc_set_arg;
108 gtkobject_class->get_arg = gst_httpsrc_get_arg;
110 gstelement_class->change_state = gst_httpsrc_change_state;
112 gstsrc_class->push = gst_httpsrc_push;
113 gstsrc_class->push_region = NULL;
116 static void gst_httpsrc_init(GstHttpSrc *httpsrc) {
117 httpsrc->srcpad = gst_pad_new("src",GST_PAD_SRC);
118 gst_element_add_pad(GST_ELEMENT(httpsrc),httpsrc->srcpad);
121 httpsrc->request = NULL;
123 httpsrc->curoffset = 0;
124 httpsrc->bytes_per_read = 4096;
127 static void gst_httpsrc_push(GstSrc *src) {
132 g_return_if_fail(src != NULL);
133 g_return_if_fail(GST_IS_HTTPSRC(src));
134 // g_return_if_fail(GST_FLAG_IS_SET(src,GST_));
135 httpsrc = GST_HTTPSRC(src);
137 buf = gst_buffer_new();
138 GST_BUFFER_DATA(buf) = (gpointer)malloc(httpsrc->bytes_per_read);
139 readbytes = read(httpsrc->fd,GST_BUFFER_DATA(buf),httpsrc->bytes_per_read);
140 if (readbytes == 0) {
141 gst_src_signal_eos(GST_SRC(httpsrc));
145 if (readbytes < httpsrc->bytes_per_read) {
146 // FIXME: set the buffer's EOF bit here
148 GST_BUFFER_OFFSET(buf) = httpsrc->curoffset;
149 GST_BUFFER_SIZE(buf) = readbytes;
150 httpsrc->curoffset += readbytes;
152 gst_pad_push(httpsrc->srcpad,buf);
155 static gboolean gst_httpsrc_open_url(GstHttpSrc *httpsrc) {
158 g_return_val_if_fail(httpsrc != NULL, FALSE);
159 g_return_val_if_fail(GST_IS_HTTPSRC(httpsrc), FALSE);
160 g_return_val_if_fail(httpsrc->url != NULL, FALSE);
162 httpsrc->request = ghttp_request_new();
163 ghttp_set_uri(httpsrc->request,httpsrc->url);
164 ghttp_set_sync(httpsrc->request,ghttp_async);
165 ghttp_set_header(httpsrc->request,"User-Agent","GstHttpSrc");
166 ghttp_prepare(httpsrc->request);
168 /* process everything up to the actual data stream */
169 /* FIXME: should be in preroll, but hey */
171 while ((ghttp_get_status(httpsrc->request).proc != ghttp_proc_response)
173 status = ghttp_process(httpsrc->request);
176 /* get the fd so we can read data ourselves */
177 httpsrc->fd = ghttp_get_socket(httpsrc->request);
181 /* unmap and close the file */
182 static void gst_httpsrc_close_url(GstHttpSrc *src) {
183 g_return_if_fail(src->fd > 0);
189 static void gst_httpsrc_set_arg(GtkObject *object,GtkArg *arg,guint id) {
192 /* it's not null if we got it, but it might not be ours */
193 g_return_if_fail(GST_IS_HTTPSRC(object));
194 src = GST_HTTPSRC(object);
198 /* the element must be stopped in order to do this */
199 g_return_if_fail(!GST_FLAG_IS_SET(src,GST_STATE_RUNNING));
201 if (src->url) g_free(src->url);
202 /* clear the url if we get a NULL (is that possible?) */
203 if (GTK_VALUE_STRING(*arg) == NULL) {
205 gst_element_set_state(GST_ELEMENT(object),~GST_STATE_COMPLETE);
206 /* otherwise set the new url */
208 src->url = g_strdup(GTK_VALUE_STRING(*arg));
209 gst_element_set_state(GST_ELEMENT(object),GST_STATE_COMPLETE);
212 case ARG_BYTESPERREAD:
213 src->bytes_per_read = GTK_VALUE_INT(*arg);
220 static void gst_httpsrc_get_arg(GtkObject *object,GtkArg *arg,guint id) {
223 /* it's not null if we got it, but it might not be ours */
224 g_return_if_fail(GST_IS_HTTPSRC(object));
225 httpsrc = GST_HTTPSRC(object);
229 GTK_VALUE_STRING(*arg) = httpsrc->url;
231 case ARG_BYTESPERREAD:
232 GTK_VALUE_INT(*arg) = httpsrc->bytes_per_read;
235 arg->type = GTK_TYPE_INVALID;
240 static gboolean gst_httpsrc_change_state(GstElement *element,
241 GstElementState state) {
242 g_return_val_if_fail(GST_IS_HTTPSRC(element), FALSE);
245 case GST_STATE_RUNNING:
246 if (!gst_httpsrc_open_url(GST_HTTPSRC(element)))
249 case ~GST_STATE_RUNNING:
250 gst_httpsrc_close_url(GST_HTTPSRC(element));
256 if (GST_ELEMENT_CLASS(parent_class)->change_state)
257 return GST_ELEMENT_CLASS(parent_class)->change_state(element,state);