094ed07680a4871e107f99e30886ae1f85149a96
[platform/upstream/gstreamer.git] / gst / elements / gsthttpsrc.c
1 /* Gnome-Streamer
2  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #include <fcntl.h>
23 #include <stdlib.h>
24 #include <unistd.h>
25
26 #include <gsthttpsrc.h>
27
28
29 GstElementDetails gst_httpsrc_details = {
30   "HTTP Source",
31   "Source/Network",
32   "Read data from an HTTP stream",
33   VERSION,
34   "Erik Walthinsen <omega@cse.ogi.edu>",
35   "(C) 1999",
36 };
37
38
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);
44
45
46 /* HttpSrc signals and args */
47 enum {
48   /* FILL ME */
49   LAST_SIGNAL
50 };
51
52 enum {
53   ARG_0,
54   ARG_LOCATION,
55   ARG_BYTESPERREAD,
56   ARG_OFFSET
57 };
58
59
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);
64
65
66 static GstSrcClass *parent_class = NULL;
67 //static guint gst_httpsrc_signals[LAST_SIGNAL] = { 0 };
68
69 GtkType
70 gst_httpsrc_get_type(void) {
71   static GtkType httpsrc_type = 0;
72
73   if (!httpsrc_type) {
74     static const GtkTypeInfo httpsrc_info = {
75       "GstHttpSrc",
76       sizeof(GstHttpSrc),
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,
83     };
84     httpsrc_type = gtk_type_unique(GST_TYPE_SRC,&httpsrc_info);
85   }
86   return httpsrc_type;
87 }
88
89 static void
90 gst_httpsrc_class_init(GstHttpSrcClass *klass) {
91   GtkObjectClass *gtkobject_class;
92   GstElementClass *gstelement_class;
93   GstSrcClass *gstsrc_class;
94
95   gtkobject_class = (GtkObjectClass*)klass;
96   gstelement_class = (GstElementClass*)klass;
97   gstsrc_class = (GstSrcClass*)klass;
98
99   parent_class = gtk_type_class(GST_TYPE_SRC);
100
101
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);
106
107   gtkobject_class->set_arg = gst_httpsrc_set_arg;
108   gtkobject_class->get_arg = gst_httpsrc_get_arg;
109
110   gstelement_class->change_state = gst_httpsrc_change_state;
111
112   gstsrc_class->push = gst_httpsrc_push;
113   gstsrc_class->push_region = NULL;
114 }
115
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);
119
120   httpsrc->url = NULL;
121   httpsrc->request = NULL;
122   httpsrc->fd = 0;
123   httpsrc->curoffset = 0;
124   httpsrc->bytes_per_read = 4096;
125 }
126
127 static void gst_httpsrc_push(GstSrc *src) {
128   GstHttpSrc *httpsrc;
129   GstBuffer *buf;
130   glong readbytes;
131
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);
136
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));
142     return;
143   }
144
145   if (readbytes < httpsrc->bytes_per_read) {
146     // FIXME: set the buffer's EOF bit here
147   }
148   GST_BUFFER_OFFSET(buf) = httpsrc->curoffset;
149   GST_BUFFER_SIZE(buf) = readbytes;
150   httpsrc->curoffset += readbytes;
151
152   gst_pad_push(httpsrc->srcpad,buf);
153 }
154
155 static gboolean gst_httpsrc_open_url(GstHttpSrc *httpsrc) {
156   gint status;
157
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);
161
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);
167
168   /* process everything up to the actual data stream */
169   /* FIXME: should be in preroll, but hey */
170   status = 0;
171   while ((ghttp_get_status(httpsrc->request).proc != ghttp_proc_response)
172          && (status >= 0)) {
173     status = ghttp_process(httpsrc->request);
174   }
175
176   /* get the fd so we can read data ourselves */
177   httpsrc->fd = ghttp_get_socket(httpsrc->request);
178   return TRUE;
179 }
180
181 /* unmap and close the file */
182 static void gst_httpsrc_close_url(GstHttpSrc *src) {  
183   g_return_if_fail(src->fd > 0);
184  
185   close(src->fd);
186   src->fd = 0;
187 }
188
189 static void gst_httpsrc_set_arg(GtkObject *object,GtkArg *arg,guint id) {
190   GstHttpSrc *src;
191
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);
195
196   switch(id) {
197     case ARG_LOCATION:
198       /* the element must be stopped in order to do this */
199       g_return_if_fail(!GST_FLAG_IS_SET(src,GST_STATE_RUNNING));
200
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) {
204         src->url = NULL;
205         gst_element_set_state(GST_ELEMENT(object),~GST_STATE_COMPLETE);
206       /* otherwise set the new url */
207       } else {
208         src->url = g_strdup(GTK_VALUE_STRING(*arg));
209         gst_element_set_state(GST_ELEMENT(object),GST_STATE_COMPLETE);
210       }
211       break;
212     case ARG_BYTESPERREAD:
213       src->bytes_per_read = GTK_VALUE_INT(*arg);
214       break;
215     default:
216       break;
217   }
218 }
219
220 static void gst_httpsrc_get_arg(GtkObject *object,GtkArg *arg,guint id) {
221   GstHttpSrc *httpsrc;
222
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);
226                           
227   switch (id) {
228     case ARG_LOCATION:
229       GTK_VALUE_STRING(*arg) = httpsrc->url;
230       break;
231     case ARG_BYTESPERREAD:
232       GTK_VALUE_INT(*arg) = httpsrc->bytes_per_read;
233       break;
234     default:
235       arg->type = GTK_TYPE_INVALID;
236       break;
237   }
238 }
239
240 static gboolean gst_httpsrc_change_state(GstElement *element,
241                                          GstElementState state) {
242   g_return_val_if_fail(GST_IS_HTTPSRC(element), FALSE);
243
244   switch (state) {
245     case GST_STATE_RUNNING:
246       if (!gst_httpsrc_open_url(GST_HTTPSRC(element)))
247         return FALSE;
248       break;  
249     case ~GST_STATE_RUNNING:
250       gst_httpsrc_close_url(GST_HTTPSRC(element));
251       break;
252     default:   
253       break;
254   }
255  
256   if (GST_ELEMENT_CLASS(parent_class)->change_state)
257     return GST_ELEMENT_CLASS(parent_class)->change_state(element,state);
258   return TRUE;
259 }
260