summaryrefslogtreecommitdiff
path: root/plugins/elements/gsthttpsrc.c
blob: 82be7b3f487318f60a6f8bafc53beb1cc957d312 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/* GStreamer
 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
 *                    2000 Wim Taymans <wtay@chello.be>
 *
 * gsthttpsrc.c: 
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>

#include <gsthttpsrc.h>


GstElementDetails gst_httpsrc_details = {
  "HTTP Source",
  "Source/Network",
  "Read data from an HTTP stream",
  VERSION,
  "Erik Walthinsen <omega@cse.ogi.edu>",
  "(C) 1999",
};

/* HttpSrc signals and args */
enum {
  /* FILL ME */
  LAST_SIGNAL
};

enum {
  ARG_0,
  ARG_LOCATION,
  ARG_BYTESPERREAD,
  ARG_OFFSET
};

static void 			gst_httpsrc_class_init	(GstHttpSrcClass *klass);
static void 			gst_httpsrc_init	(GstHttpSrc *httpsrc);

static void 			gst_httpsrc_set_property	(GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec);
static void 			gst_httpsrc_get_property	(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec);
static GstElementStateReturn	gst_httpsrc_change_state	(GstElement *element);

static GstBuffer *		gst_httpsrc_get		(GstPad *pad);

static gboolean			gst_httpsrc_open_url	(GstHttpSrc *src);
static void			gst_httpsrc_close_url	(GstHttpSrc *src);


static GstElementClass *parent_class = NULL;
//static guint gst_httpsrc_signals[LAST_SIGNAL] = { 0 };

GType
gst_httpsrc_get_type (void) 
{
  static GType httpsrc_type = 0;

  if (!httpsrc_type) {
    static const GTypeInfo httpsrc_info = {
      sizeof(GstHttpSrcClass),      NULL,
      NULL,
      (GClassInitFunc)gst_httpsrc_class_init,
      NULL,
      NULL,
      sizeof(GstHttpSrc),
      0,
      (GInstanceInitFunc)gst_httpsrc_init,
    };
    httpsrc_type = g_type_register_static (GST_TYPE_ELEMENT, "GstHttpSrc", &httpsrc_info, 0);
  }
  return httpsrc_type;
}

static void
gst_httpsrc_class_init (GstHttpSrcClass *klass) 
{
  GObjectClass *gobject_class;
  GstElementClass *gstelement_class;

  gobject_class = (GObjectClass*)klass;
  gstelement_class = (GstElementClass*)klass;

  parent_class = g_type_class_ref (GST_TYPE_ELEMENT);


  g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_LOCATION,
    g_param_spec_string("location","location","location",
                        NULL, G_PARAM_READWRITE)); // CHECKME
  g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_BYTESPERREAD,
    g_param_spec_int("bytesperread","bytesperread","bytesperread",
                     G_MININT,G_MAXINT,0,G_PARAM_READWRITE)); // CHECKME

  gobject_class->set_property = gst_httpsrc_set_property;
  gobject_class->get_property = gst_httpsrc_get_property;

  gstelement_class->change_state = gst_httpsrc_change_state;
}

static void gst_httpsrc_init(GstHttpSrc *httpsrc) {
  httpsrc->srcpad = gst_pad_new("src",GST_PAD_SRC);
  gst_pad_set_get_function(httpsrc->srcpad,gst_httpsrc_get);
  gst_element_add_pad(GST_ELEMENT(httpsrc),httpsrc->srcpad);

  httpsrc->url = NULL;
  httpsrc->request = NULL;
  httpsrc->fd = 0;
  httpsrc->curoffset = 0;
  httpsrc->bytes_per_read = 4096;
}

static GstBuffer *
gst_httpsrc_get(GstPad *pad)
{
  GstHttpSrc *src;
  GstBuffer *buf;
  glong readbytes;

  g_return_val_if_fail (pad != NULL, NULL);
  src = GST_HTTPSRC(gst_pad_get_parent (pad));

  buf = gst_buffer_new();
  GST_BUFFER_DATA(buf) = (gpointer)malloc(src->bytes_per_read);
  readbytes = read(src->fd,GST_BUFFER_DATA(buf),src->bytes_per_read);

  if (readbytes == 0) {
    gst_element_signal_eos(GST_ELEMENT(src));
    return NULL;
  }

  if (readbytes < src->bytes_per_read) {
    // FIXME: set the buffer's EOF bit here
  }
  GST_BUFFER_OFFSET(buf) = src->curoffset;
  GST_BUFFER_SIZE(buf) = readbytes;
  src->curoffset += readbytes;

  return buf;
}

static gboolean 
gst_httpsrc_open_url (GstHttpSrc *httpsrc) 
{
  gint status;

  g_return_val_if_fail (!GST_FLAG_IS_SET (httpsrc, GST_HTTPSRC_OPEN), FALSE);
  g_return_val_if_fail (httpsrc->url != NULL, FALSE);

  httpsrc->request = ghttp_request_new ();
  
  ghttp_set_uri (httpsrc->request, httpsrc->url);
  ghttp_set_sync (httpsrc->request, ghttp_async);
  ghttp_set_header (httpsrc->request, "User-Agent", "GstHttpSrc");
  ghttp_prepare (httpsrc->request);

  /* process everything up to the actual data stream */
  /* FIXME: should be in preroll, but hey */
  status = 0;
  while ((ghttp_get_status (httpsrc->request).proc != ghttp_proc_response)
         && (status >= 0)) {
    status = ghttp_process (httpsrc->request);
  }

  /* get the fd so we can read data ourselves */
  httpsrc->fd = ghttp_get_socket (httpsrc->request);
  
  GST_FLAG_SET (httpsrc, GST_HTTPSRC_OPEN);
  
  return TRUE;
}

/* unmap and close the file */
static void 
gst_httpsrc_close_url (GstHttpSrc *src) 
{  
  g_return_if_fail (GST_FLAG_IS_SET (src, GST_HTTPSRC_OPEN));
  g_return_if_fail (src->fd > 0);
 
  close(src->fd);
  src->fd = 0;

  GST_FLAG_UNSET (src, GST_HTTPSRC_OPEN);
}

static void 
gst_httpsrc_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec) 
{
  GstHttpSrc *src;

  /* it's not null if we got it, but it might not be ours */
  g_return_if_fail (GST_IS_HTTPSRC (object));
  
  src = GST_HTTPSRC (object);

  switch (prop_id) {
    case ARG_LOCATION:
      /* the element must not be playing in order to do this */
      g_return_if_fail (GST_STATE (src) < GST_STATE_PLAYING);

      if (src->url) g_free (src->url);
      /* clear the url if we get a NULL (is that possible?) */
      if (g_value_get_string (value) == NULL) {
        gst_element_set_state (GST_ELEMENT (object),GST_STATE_NULL);
        src->url = NULL;
      /* otherwise set the new url */
      } else {
        src->url = g_strdup (g_value_get_string (value));
      }
      break;
    case ARG_BYTESPERREAD:
      src->bytes_per_read = g_value_get_int (value);
      break;
    default:
      break;
  }
}

static void 
gst_httpsrc_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec) 
{
  GstHttpSrc *httpsrc;

  /* it's not null if we got it, but it might not be ours */
  g_return_if_fail (GST_IS_HTTPSRC (object));
  
  httpsrc = GST_HTTPSRC (object);
                          
  switch (prop_id) {
    case ARG_LOCATION:
      g_value_set_string (value, httpsrc->url);
      break;
    case ARG_BYTESPERREAD:
      g_value_set_int (value, httpsrc->bytes_per_read);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

static GstElementStateReturn 
gst_httpsrc_change_state (GstElement *element) 
{
  g_return_val_if_fail (GST_IS_HTTPSRC (element), GST_STATE_FAILURE);

  if (GST_STATE_PENDING (element) == GST_STATE_NULL) {
    if (GST_FLAG_IS_SET (element, GST_HTTPSRC_OPEN))
      gst_httpsrc_close_url (GST_HTTPSRC (element));
  } else {
    if (!GST_FLAG_IS_SET (element, GST_HTTPSRC_OPEN)) {
      if (!gst_httpsrc_open_url (GST_HTTPSRC (element)))
        return GST_STATE_FAILURE;
    }
  }
 
  if (GST_ELEMENT_CLASS (parent_class)->change_state)
    return GST_ELEMENT_CLASS (parent_class)->change_state (element);
  
  return GST_STATE_SUCCESS;
}