gst/gstutils.h: GST_PARENT_CALL and
[platform/upstream/gstreamer.git] / plugins / elements / gstfdsrc.c
1 /* GStreamer
2  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3  *                    2000 Wim Taymans <wtay@chello.be>
4  *
5  * gstfdsrc.c: 
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <fcntl.h>
26 #include <stdio.h>
27 #include <unistd.h>
28 #include <stdlib.h>
29
30 #ifdef HAVE_CONFIG_H
31 #  include "config.h"
32 #endif
33
34 #include "gstfdsrc.h"
35
36 #define DEFAULT_BLOCKSIZE       4096
37
38 GST_DEBUG_CATEGORY_STATIC (gst_fdsrc_debug);
39 #define GST_CAT_DEFAULT gst_fdsrc_debug
40
41 GstElementDetails gst_fdsrc_details = GST_ELEMENT_DETAILS (
42   "Disk Source",
43   "Source/File",
44   "Synchronous read from a file",
45   "Erik Walthinsen <omega@cse.ogi.edu>"
46 );
47
48
49 /* FdSrc signals and args */
50 enum {
51   /* FILL ME */
52   LAST_SIGNAL
53 };
54
55 enum {
56   ARG_0,
57   ARG_FD,
58   ARG_BLOCKSIZE,
59 };
60
61 #define _do_init(bla) \
62     GST_DEBUG_CATEGORY_INIT (gst_fdsrc_debug, "fdsrc", 0, "fdsrc element");
63
64 GST_BOILERPLATE_FULL (GstFdSrc, gst_fdsrc, GstElement, GST_TYPE_ELEMENT, _do_init);
65
66 static void             gst_fdsrc_set_property  (GObject *object, guint prop_id, 
67                                                  const GValue *value, GParamSpec *pspec);
68 static void             gst_fdsrc_get_property  (GObject *object, guint prop_id, 
69                                                  GValue *value, GParamSpec *pspec);
70
71 static GstData *        gst_fdsrc_get           (GstPad *pad);
72
73
74 static void
75 gst_fdsrc_base_init (gpointer g_class)
76 {
77   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
78   
79   gst_element_class_set_details (gstelement_class, &gst_fdsrc_details);
80 }
81 static void
82 gst_fdsrc_class_init (GstFdSrcClass *klass) 
83 {
84   GObjectClass *gobject_class;
85
86   gobject_class = G_OBJECT_CLASS (klass);
87   
88
89   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_FD,
90     g_param_spec_int ("fd", "fd", "An open file descriptor to read from",
91                       0, G_MAXINT, 0, G_PARAM_READWRITE));
92   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_BLOCKSIZE,
93     g_param_spec_ulong ("blocksize", "Block size", "Size in bytes to read per buffer",
94                         1, G_MAXULONG, DEFAULT_BLOCKSIZE, G_PARAM_READWRITE));
95
96   gobject_class->set_property = gst_fdsrc_set_property;
97   gobject_class->get_property = gst_fdsrc_get_property;
98 }
99
100 static void gst_fdsrc_init(GstFdSrc *fdsrc) {
101   fdsrc->srcpad = gst_pad_new ("src", GST_PAD_SRC);
102   
103   gst_pad_set_get_function (fdsrc->srcpad, gst_fdsrc_get);
104   gst_element_add_pad (GST_ELEMENT (fdsrc), fdsrc->srcpad);
105
106   fdsrc->fd = 0;
107   fdsrc->curoffset = 0;
108   fdsrc->blocksize = DEFAULT_BLOCKSIZE;
109   fdsrc->seq = 0;
110 }
111
112
113 static void 
114 gst_fdsrc_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec) 
115 {
116   GstFdSrc *src;
117
118   /* it's not null if we got it, but it might not be ours */
119   g_return_if_fail (GST_IS_FDSRC (object));
120   
121   src = GST_FDSRC (object);
122
123   switch (prop_id) {
124     case ARG_FD:
125       src->fd = g_value_get_int (value);
126       break;
127     case ARG_BLOCKSIZE:
128       src->blocksize = g_value_get_ulong (value);
129       break;
130     default:
131       break;
132   }
133 }
134
135 static void 
136 gst_fdsrc_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec) 
137 {
138   GstFdSrc *src;
139
140   /* it's not null if we got it, but it might not be ours */
141   g_return_if_fail (GST_IS_FDSRC (object));
142   
143   src = GST_FDSRC (object);
144
145   switch (prop_id) {
146     case ARG_BLOCKSIZE:
147       g_value_set_ulong (value, src->blocksize);
148       break;
149     case ARG_FD:
150       g_value_set_int (value, src->fd);
151       break;
152     default:
153       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
154       break;
155   }
156 }
157
158 static GstData *
159 gst_fdsrc_get(GstPad *pad)
160 {
161   GstFdSrc *src;
162   GstBuffer *buf;
163   glong readbytes;
164
165   src = GST_FDSRC (gst_pad_get_parent (pad));
166
167   /* create the buffer */
168   buf = gst_buffer_new_and_alloc (src->blocksize);
169
170   /* read it in from the file */
171   readbytes = read (src->fd, GST_BUFFER_DATA (buf), src->blocksize);
172
173   /* if nothing was read, we're in eos */
174   if (readbytes == 0) {
175     gst_element_set_eos (GST_ELEMENT (src));
176     return GST_DATA (gst_event_new (GST_EVENT_EOS));
177   }
178
179   if (readbytes == -1) {
180     g_error ("Error reading from file descriptor. Ending stream.\n");
181     gst_element_set_eos (GST_ELEMENT (src));
182     return GST_DATA (gst_event_new (GST_EVENT_EOS));
183   }
184   
185   GST_BUFFER_OFFSET (buf) = src->curoffset;
186   GST_BUFFER_SIZE (buf) = readbytes;
187   GST_BUFFER_TIMESTAMP (buf) = GST_CLOCK_TIME_NONE;
188   src->curoffset += readbytes;
189
190   /* we're done, return the buffer */
191   return GST_DATA (buf);
192 }