Changed the way things are scheduled, especially sources. A Src used to have a push...
[platform/upstream/gstreamer.git] / plugins / elements / gstfdsrc.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 <stdio.h>
24 #include <unistd.h>
25 #include <stdlib.h>
26
27 #include <gstfdsrc.h>
28
29
30 GstElementDetails gst_fdsrc_details = {
31   "Disk Source",
32   "Source/File",
33   "Synchronous read from a file",
34   VERSION,
35   "Erik Walthinsen <omega@cse.ogi.edu>",
36   "(C) 1999",
37 };
38
39
40 /* FdSrc signals and args */
41 enum {
42   /* FILL ME */
43   LAST_SIGNAL
44 };
45
46 enum {
47   ARG_0,
48   ARG_LOCATION,
49   ARG_BYTESPERREAD,
50   ARG_OFFSET,
51 };
52
53
54 static void gst_fdsrc_class_init        (GstFdSrcClass *klass);
55 static void gst_fdsrc_init              (GstFdSrc *fdsrc);
56
57 static void gst_fdsrc_set_arg           (GtkObject *object, GtkArg *arg, guint id);
58 static void gst_fdsrc_get_arg           (GtkObject *object, GtkArg *arg, guint id);
59
60 static void gst_fdsrc_pull              (GstPad *pad);
61
62
63 static GstSrcClass *parent_class = NULL;
64 //static guint gst_fdsrc_signals[LAST_SIGNAL] = { 0 };
65
66 GtkType
67 gst_fdsrc_get_type (void) 
68 {
69   static GtkType fdsrc_type = 0;
70
71   if (!fdsrc_type) {
72     static const GtkTypeInfo fdsrc_info = {
73       "GstFdSrc",
74       sizeof(GstFdSrc),
75       sizeof(GstFdSrcClass),
76       (GtkClassInitFunc)gst_fdsrc_class_init,
77       (GtkObjectInitFunc)gst_fdsrc_init,
78       (GtkArgSetFunc)gst_fdsrc_set_arg,
79       (GtkArgGetFunc)gst_fdsrc_get_arg,
80       (GtkClassInitFunc)NULL,
81     };
82     fdsrc_type = gtk_type_unique (GST_TYPE_SRC, &fdsrc_info);
83   }
84   return fdsrc_type;
85 }
86
87 static void
88 gst_fdsrc_class_init (GstFdSrcClass *klass) 
89 {
90   GtkObjectClass *gtkobject_class;
91   GstSrcClass *gstsrc_class;
92
93   gtkobject_class = (GtkObjectClass*)klass;
94   gstsrc_class = (GstSrcClass*)klass;
95
96   parent_class = gtk_type_class(GST_TYPE_SRC);
97
98   gtk_object_add_arg_type ("GstFdSrc::location", GST_TYPE_FILENAME,
99                            GTK_ARG_WRITABLE, ARG_LOCATION);
100   gtk_object_add_arg_type ("GstFdSrc::bytesperread", GTK_TYPE_INT,
101                            GTK_ARG_READWRITE, ARG_BYTESPERREAD);
102   gtk_object_add_arg_type ("GstFdSrc::offset", GTK_TYPE_INT,
103                            GTK_ARG_READABLE, ARG_OFFSET);
104
105   gtkobject_class->set_arg = gst_fdsrc_set_arg;
106   gtkobject_class->get_arg = gst_fdsrc_get_arg;
107 }
108
109 static void gst_fdsrc_init(GstFdSrc *fdsrc) {
110   fdsrc->srcpad = gst_pad_new("src",GST_PAD_SRC);
111   gst_pad_set_pull_function(fdsrc->srcpad,gst_fdsrc_pull);
112   gst_element_add_pad(GST_ELEMENT(fdsrc),fdsrc->srcpad);
113
114   fdsrc->fd = 0;
115   fdsrc->curoffset = 0;
116   fdsrc->bytes_per_read = 4096;
117   fdsrc->seq = 0;
118 }
119
120
121 static void 
122 gst_fdsrc_set_arg (GtkObject *object, GtkArg *arg, guint id) 
123 {
124   GstFdSrc *src;
125   int fd;
126
127   /* it's not null if we got it, but it might not be ours */
128   g_return_if_fail (GST_IS_FDSRC (object));
129   
130   src = GST_FDSRC (object);
131
132   switch(id) {
133     case ARG_LOCATION:
134       /* the element must not be playing in order to do this */
135       g_return_if_fail (GST_STATE (src) < GST_STATE_PLAYING);
136
137       /* if we get a NULL, consider it to be a fd of 0 */
138       if (GTK_VALUE_STRING (*arg) == NULL) {
139         gst_element_set_state (GST_ELEMENT (object), GST_STATE_NULL);
140         src->fd = 0;
141       /* otherwise set the new filename */
142       } else {
143         if (sscanf (GTK_VALUE_STRING (*arg), "%d", &fd))
144           src->fd = fd;
145       }
146       break;
147     case ARG_BYTESPERREAD:
148       src->bytes_per_read = GTK_VALUE_INT (*arg);
149       break;
150     default:
151       break;
152   }
153 }
154
155 static void 
156 gst_fdsrc_get_arg (GtkObject *object, GtkArg *arg, guint id) 
157 {
158   GstFdSrc *src;
159
160   /* it's not null if we got it, but it might not be ours */
161   g_return_if_fail (GST_IS_FDSRC (object));
162   
163   src = GST_FDSRC (object);
164
165   switch (id) {
166     case ARG_BYTESPERREAD:
167       GTK_VALUE_INT (*arg) = src->bytes_per_read;
168       break;
169     case ARG_OFFSET:
170       GTK_VALUE_INT (*arg) = src->curoffset;
171       break;
172     default:
173       arg->type = GTK_TYPE_INVALID;
174       break;
175   }
176 }
177
178 void gst_fdsrc_pull(GstPad *pad) {
179   GstFdSrc *src;
180   GstBuffer *buf;
181   glong readbytes;
182
183   g_return_if_fail(pad != NULL);
184   src = GST_FDSRC(gst_pad_get_parent(pad));
185
186   /* create the buffer */
187   // FIXME: should eventually use a bufferpool for this
188   buf = gst_buffer_new ();
189   g_return_if_fail (buf);
190
191   /* allocate the space for the buffer data */
192   GST_BUFFER_DATA(buf) = g_malloc(src->bytes_per_read);
193   g_return_if_fail(GST_BUFFER_DATA(buf) != NULL);
194
195   /* read it in from the file */
196   readbytes = read(src->fd,GST_BUFFER_DATA(buf),src->bytes_per_read);
197   if (readbytes == 0) {
198     gst_src_signal_eos(GST_SRC(src));
199     return;
200   }
201
202   /* if we didn't get as many bytes as we asked for, we're at EOF */
203   if (readbytes < src->bytes_per_read) {
204     // set the buffer's EOF bit here
205     GST_BUFFER_FLAG_SET (buf, GST_BUFFER_EOS);
206   }
207   GST_BUFFER_OFFSET(buf) = src->curoffset;
208   GST_BUFFER_SIZE(buf) = readbytes;
209   src->curoffset += readbytes;
210
211   /* we're done, push the buffer off now */
212   gst_pad_push(pad,buf);
213 }