1 /* GStreamer
2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2000 Wim Taymans <wtay@chello.be>
4 * 2005 Philippe Khalaf <burger@speedy.org>
5 *
6 * gstfdsrc.c:
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
17 *
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
22 */
23 /**
24 * SECTION:element-fdsrc
25 * @title: fdsrc
26 * @see_also: #GstFdSink
27 *
28 * Read data from a unix file descriptor.
29 *
30 * To generate data, enter some data on the console followed by enter.
31 * The above mentioned pipeline should dump data packets to the console.
32 *
33 * If the #GstFdSrc:timeout property is set to a value bigger than 0, fdsrc will
34 * generate an element message named `GstFdSrcTimeout`
35 * if no data was received in the given timeout.
36 *
37 * The message's structure contains one field:
38 *
39 * * #guint64 `timeout`: the timeout in microseconds that
40 * expired when waiting for data.
41 *
42 * ## Example launch line
43 * |[
44 * echo "Hello GStreamer" | gst-launch-1.0 -v fdsrc ! fakesink dump=true
45 * ]| A simple pipeline to read from the standard input and dump the data
46 * with a fakesink as hex ascii block.
47 *
48 */
49
50 #ifdef HAVE_CONFIG_H
51 # include "config.h"
52 #endif
53 #include "gst/gst_private.h"
54
55 #include <sys/types.h>
56
57 #ifdef G_OS_WIN32
58 #include <io.h> /* lseek, open, close, read */
59 #undef lseek
60 #define lseek _lseeki64
61 #endif
62
63 #include <sys/stat.h>
64 #ifdef HAVE_SYS_SOCKET_H
65 #include <sys/socket.h>
66 #endif
67 #include <fcntl.h>
68 #include <stdio.h>
69 #ifdef HAVE_UNISTD_H
70 #include <unistd.h>
71 #endif
72 #ifdef _MSC_VER
73 #undef stat
74 #define stat _stat
75 #define fstat _fstat
76 #define S_ISREG(m) (((m)&S_IFREG)==S_IFREG)
77 #endif
78 #include <stdlib.h>
79 #include <errno.h>
80
81 #include "gstfdsrc.h"
82 #include "gstcoreelementselements.h"
83
84 #define struct_stat struct stat
85
86 #ifdef __BIONIC__ /* Android */
87 #if defined(__ANDROID_API__) && __ANDROID_API__ >= 21
88 #undef fstat
89 #define fstat fstat64
90 #undef struct_stat
91 #define struct_stat struct stat64
92 #endif
93 #endif
94
95 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
96 GST_PAD_SRC,
97 GST_PAD_ALWAYS,
98 GST_STATIC_CAPS_ANY);
99
100 GST_DEBUG_CATEGORY_STATIC (gst_fd_src_debug);
101 #define GST_CAT_DEFAULT gst_fd_src_debug
102
103 #define DEFAULT_FD 0
104 #define DEFAULT_TIMEOUT 0
105
106 enum
107 {
108 PROP_0,
109
110 PROP_FD,
111 PROP_TIMEOUT,
112
113 PROP_LAST
114 };
115
116 static void gst_fd_src_uri_handler_init (gpointer g_iface, gpointer iface_data);
117
118 #define _do_init \
119 G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER, gst_fd_src_uri_handler_init); \
120 GST_DEBUG_CATEGORY_INIT (gst_fd_src_debug, "fdsrc", 0, "fdsrc element");
121 #define gst_fd_src_parent_class parent_class
122 G_DEFINE_TYPE_WITH_CODE (GstFdSrc, gst_fd_src, GST_TYPE_PUSH_SRC, _do_init);
123 #if defined(HAVE_SYS_SOCKET_H) || defined(_MSC_VER)
124 GST_ELEMENT_REGISTER_DEFINE (fdsrc, "fdsrc", GST_RANK_NONE, GST_TYPE_FD_SRC);
125 #endif
126
127 static void gst_fd_src_set_property (GObject * object, guint prop_id,
128 const GValue * value, GParamSpec * pspec);
129 static void gst_fd_src_get_property (GObject * object, guint prop_id,
130 GValue * value, GParamSpec * pspec);
131 static void gst_fd_src_dispose (GObject * obj);
132
133 static gboolean gst_fd_src_start (GstBaseSrc * bsrc);
134 static gboolean gst_fd_src_stop (GstBaseSrc * bsrc);
135 static gboolean gst_fd_src_unlock (GstBaseSrc * bsrc);
136 static gboolean gst_fd_src_unlock_stop (GstBaseSrc * bsrc);
137 static gboolean gst_fd_src_is_seekable (GstBaseSrc * bsrc);
138 static gboolean gst_fd_src_get_size (GstBaseSrc * src, guint64 * size);
139 static gboolean gst_fd_src_do_seek (GstBaseSrc * src, GstSegment * segment);
140 static gboolean gst_fd_src_query (GstBaseSrc * src, GstQuery * query);
141 #ifdef OHOS_EXT_FUNC
142 // ohos.ext.func.0021
143 static GstFlowReturn gst_fd_src_create_base (GstBaseSrc * bsrc, guint64 offset, guint length, GstBuffer ** ret);
144 #endif
145 static GstFlowReturn gst_fd_src_create (GstPushSrc * psrc, GstBuffer ** outbuf);
146
147 static void
gst_fd_src_class_init(GstFdSrcClass * klass)148 gst_fd_src_class_init (GstFdSrcClass * klass)
149 {
150 GObjectClass *gobject_class;
151 GstElementClass *gstelement_class;
152 GstBaseSrcClass *gstbasesrc_class;
153 GstPushSrcClass *gstpush_src_class;
154
155 gobject_class = G_OBJECT_CLASS (klass);
156 gstelement_class = GST_ELEMENT_CLASS (klass);
157 gstbasesrc_class = GST_BASE_SRC_CLASS (klass);
158 gstpush_src_class = GST_PUSH_SRC_CLASS (klass);
159
160 gobject_class->set_property = gst_fd_src_set_property;
161 gobject_class->get_property = gst_fd_src_get_property;
162 gobject_class->dispose = gst_fd_src_dispose;
163
164 g_object_class_install_property (gobject_class, PROP_FD,
165 g_param_spec_int ("fd", "fd", "An open file descriptor to read from",
166 0, G_MAXINT, DEFAULT_FD, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
167 /**
168 * GstFdSrc:timeout
169 *
170 * Post a message after timeout microseconds
171 */
172 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_TIMEOUT,
173 g_param_spec_uint64 ("timeout", "Timeout",
174 "Post a message after timeout microseconds (0 = disabled)", 0,
175 G_MAXUINT64, DEFAULT_TIMEOUT,
176 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
177
178 gst_element_class_set_static_metadata (gstelement_class,
179 "Filedescriptor Source",
180 "Source/File",
181 "Read from a file descriptor", "Erik Walthinsen <omega@cse.ogi.edu>");
182 gst_element_class_add_static_pad_template (gstelement_class, &srctemplate);
183
184 gstbasesrc_class->start = GST_DEBUG_FUNCPTR (gst_fd_src_start);
185 gstbasesrc_class->stop = GST_DEBUG_FUNCPTR (gst_fd_src_stop);
186 gstbasesrc_class->unlock = GST_DEBUG_FUNCPTR (gst_fd_src_unlock);
187 gstbasesrc_class->unlock_stop = GST_DEBUG_FUNCPTR (gst_fd_src_unlock_stop);
188 gstbasesrc_class->is_seekable = GST_DEBUG_FUNCPTR (gst_fd_src_is_seekable);
189 gstbasesrc_class->get_size = GST_DEBUG_FUNCPTR (gst_fd_src_get_size);
190 gstbasesrc_class->do_seek = GST_DEBUG_FUNCPTR (gst_fd_src_do_seek);
191 gstbasesrc_class->query = GST_DEBUG_FUNCPTR (gst_fd_src_query);
192 #ifdef OHOS_EXT_FUNC
193 // ohos.ext.func.0021
194 gstbasesrc_class->create = GST_DEBUG_FUNCPTR (gst_fd_src_create_base);
195 #endif
196 gstpush_src_class->create = GST_DEBUG_FUNCPTR (gst_fd_src_create);
197 }
198
199 static void
gst_fd_src_init(GstFdSrc * fdsrc)200 gst_fd_src_init (GstFdSrc * fdsrc)
201 {
202 fdsrc->new_fd = DEFAULT_FD;
203 fdsrc->seekable_fd = FALSE;
204 fdsrc->fd = -1;
205 fdsrc->size = -1;
206 fdsrc->timeout = DEFAULT_TIMEOUT;
207 fdsrc->uri = g_strdup_printf ("fd://0");
208 fdsrc->curoffset = 0;
209 #ifdef OHOS_EXT_FUNC
210 /**
211 * ohos.ext.func.0020
212 * Add start offset handling in gstfdsrc.
213 */
214 fdsrc->start_offset = 0;
215 #endif
216 }
217
218 static void
gst_fd_src_dispose(GObject * obj)219 gst_fd_src_dispose (GObject * obj)
220 {
221 GstFdSrc *src = GST_FD_SRC (obj);
222
223 g_free (src->uri);
224 src->uri = NULL;
225
226 G_OBJECT_CLASS (parent_class)->dispose (obj);
227 }
228
229 static void
gst_fd_src_update_fd(GstFdSrc * src,guint64 size)230 gst_fd_src_update_fd (GstFdSrc * src, guint64 size)
231 {
232 struct_stat stat_results;
233
234 GST_DEBUG_OBJECT (src, "fdset %p, old_fd %d, new_fd %d", src->fdset, src->fd,
235 src->new_fd);
236
237 /* we need to always update the fdset since it may not have existed when
238 * gst_fd_src_update_fd () was called earlier */
239 if (src->fdset != NULL) {
240 GstPollFD fd = GST_POLL_FD_INIT;
241
242 if (src->fd >= 0) {
243 fd.fd = src->fd;
244 /* this will log a harmless warning, if it was never added */
245 gst_poll_remove_fd (src->fdset, &fd);
246 }
247
248 fd.fd = src->new_fd;
249 gst_poll_add_fd (src->fdset, &fd);
250 gst_poll_fd_ctl_read (src->fdset, &fd, TRUE);
251 }
252
253
254 if (src->fd != src->new_fd) {
255 GST_INFO_OBJECT (src, "Updating to fd %d", src->new_fd);
256
257 src->fd = src->new_fd;
258
259 GST_INFO_OBJECT (src, "Setting size to fd %" G_GUINT64_FORMAT, size);
260 src->size = size;
261
262 g_free (src->uri);
263 src->uri = g_strdup_printf ("fd://%d", src->fd);
264
265 if (fstat (src->fd, &stat_results) < 0)
266 goto not_seekable;
267
268 if (!S_ISREG (stat_results.st_mode))
269 goto not_seekable;
270
271 /* Try a seek of 0 bytes offset to check for seekability */
272 if (lseek (src->fd, 0, SEEK_CUR) < 0)
273 goto not_seekable;
274
275 GST_INFO_OBJECT (src, "marking fd %d as seekable", src->fd);
276 src->seekable_fd = TRUE;
277
278 gst_base_src_set_dynamic_size (GST_BASE_SRC (src), TRUE);
279 }
280 return;
281
282 not_seekable:
283 {
284 GST_INFO_OBJECT (src, "marking fd %d as NOT seekable", src->fd);
285 src->seekable_fd = FALSE;
286 gst_base_src_set_dynamic_size (GST_BASE_SRC (src), FALSE);
287 }
288 }
289
290 static gboolean
gst_fd_src_start(GstBaseSrc * bsrc)291 gst_fd_src_start (GstBaseSrc * bsrc)
292 {
293 GstFdSrc *src = GST_FD_SRC (bsrc);
294
295 #ifdef OHOS_EXT_FUNC
296 /**
297 * ohos.ext.func.0020
298 * Add start offset handling in gstfdsrc.
299 */
300 if (src->seekable_fd) {
301 gint64 res = lseek (src->fd, src->start_offset, SEEK_SET);
302 if (G_UNLIKELY ((res < 0) || (res != (gint64) src->start_offset))) {
303 return FALSE;
304 }
305 }
306 #endif
307
308 src->curoffset = 0;
309
310 if ((src->fdset = gst_poll_new (TRUE)) == NULL)
311 goto socket_pair;
312
313 gst_fd_src_update_fd (src, -1);
314
315 return TRUE;
316
317 /* ERRORS */
318 socket_pair:
319 {
320 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
321 GST_ERROR_SYSTEM);
322 return FALSE;
323 }
324 }
325
326 static gboolean
gst_fd_src_stop(GstBaseSrc * bsrc)327 gst_fd_src_stop (GstBaseSrc * bsrc)
328 {
329 GstFdSrc *src = GST_FD_SRC (bsrc);
330
331 if (src->fdset) {
332 gst_poll_free (src->fdset);
333 src->fdset = NULL;
334 }
335
336 return TRUE;
337 }
338
339 static gboolean
gst_fd_src_unlock(GstBaseSrc * bsrc)340 gst_fd_src_unlock (GstBaseSrc * bsrc)
341 {
342 GstFdSrc *src = GST_FD_SRC (bsrc);
343
344 GST_LOG_OBJECT (src, "Flushing");
345 GST_OBJECT_LOCK (src);
346 gst_poll_set_flushing (src->fdset, TRUE);
347 GST_OBJECT_UNLOCK (src);
348
349 return TRUE;
350 }
351
352 static gboolean
gst_fd_src_unlock_stop(GstBaseSrc * bsrc)353 gst_fd_src_unlock_stop (GstBaseSrc * bsrc)
354 {
355 GstFdSrc *src = GST_FD_SRC (bsrc);
356
357 GST_LOG_OBJECT (src, "No longer flushing");
358 GST_OBJECT_LOCK (src);
359 gst_poll_set_flushing (src->fdset, FALSE);
360 GST_OBJECT_UNLOCK (src);
361
362 return TRUE;
363 }
364
365 static void
gst_fd_src_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)366 gst_fd_src_set_property (GObject * object, guint prop_id, const GValue * value,
367 GParamSpec * pspec)
368 {
369 GstFdSrc *src = GST_FD_SRC (object);
370
371 switch (prop_id) {
372 case PROP_FD:
373 src->new_fd = g_value_get_int (value);
374
375 /* If state is ready or below, update the current fd immediately
376 * so it is reflected in get_properties and uri */
377 GST_OBJECT_LOCK (object);
378 if (GST_STATE (GST_ELEMENT (src)) <= GST_STATE_READY) {
379 GST_DEBUG_OBJECT (src, "state ready or lower, updating to use new fd");
380 gst_fd_src_update_fd (src, -1);
381 } else {
382 GST_DEBUG_OBJECT (src, "state above ready, not updating to new fd yet");
383 }
384 GST_OBJECT_UNLOCK (object);
385 break;
386 case PROP_TIMEOUT:
387 src->timeout = g_value_get_uint64 (value);
388 GST_DEBUG_OBJECT (src, "poll timeout set to %" GST_TIME_FORMAT,
389 GST_TIME_ARGS (src->timeout));
390 break;
391 default:
392 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
393 break;
394 }
395 }
396
397 static void
gst_fd_src_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)398 gst_fd_src_get_property (GObject * object, guint prop_id, GValue * value,
399 GParamSpec * pspec)
400 {
401 GstFdSrc *src = GST_FD_SRC (object);
402
403 switch (prop_id) {
404 case PROP_FD:
405 g_value_set_int (value, src->fd);
406 break;
407 case PROP_TIMEOUT:
408 g_value_set_uint64 (value, src->timeout);
409 break;
410 default:
411 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
412 break;
413 }
414 }
415
416 #ifdef OHOS_EXT_FUNC
417 /**
418 * ohos.ext.func.0021
419 * Enable pull mode for gstfdsrc. In pull mode, demux will pull buffer from function gst_fd_src_create_offset().
420 */
421 static GstFlowReturn
gst_fd_src_create_offset(GstBaseSrc * bsrc,guint64 offset,guint length,GstBuffer ** outbuf)422 gst_fd_src_create_offset (GstBaseSrc * bsrc, guint64 offset, guint length, GstBuffer ** outbuf)
423 {
424 GstFdSrc *src = GST_FD_SRC (bsrc);
425 GstBuffer *buf = NULL;
426 gssize readbytes;
427 guint blocksize;
428 GstMapInfo info;
429
430 GstClockTime timeout;
431 gboolean try_again = FALSE;
432 gint retval;
433
434 if (src->timeout > 0) {
435 timeout = src->timeout * GST_USECOND;
436 } else {
437 timeout = GST_CLOCK_TIME_NONE;
438 }
439
440 do {
441 try_again = FALSE;
442
443 GST_DEBUG_OBJECT (src, "doing poll, timeout %" GST_TIME_FORMAT, GST_TIME_ARGS (src->timeout));
444
445 retval = gst_poll_wait (src->fdset, timeout);
446 GST_DEBUG_OBJECT (src, "poll returned %d", retval);
447
448 if (G_UNLIKELY (retval == -1)) {
449 if (errno == EINTR || errno == EAGAIN) {
450 /* retry if interrupted */
451 try_again = TRUE;
452 } else if (errno == EBUSY) {
453 goto stopped;
454 } else {
455 goto poll_error;
456 }
457 } else if (G_UNLIKELY (retval == 0)) {
458 try_again = TRUE;
459 /* timeout, post element message */
460 gst_element_post_message (GST_ELEMENT_CAST (src), gst_message_new_element (GST_OBJECT_CAST (src),
461 gst_structure_new ("GstFdSrcTimeout", "timeout", G_TYPE_UINT64, src->timeout, NULL)));
462 }
463 } while (G_UNLIKELY (try_again)); /* retry if interrupted or timeout */
464
465 blocksize = length;
466
467 if (offset != src->curoffset) {
468 gint64 res_seek = lseek64 (src->fd, offset + src->start_offset, SEEK_SET);
469 if (G_UNLIKELY ((res_seek < 0) || (res_seek != (offset + src->start_offset)))) {
470 goto alloc_failed;
471 }
472 src->curoffset = offset;
473 }
474
475 /* create the buffer */
476 buf = gst_buffer_new_allocate (NULL, blocksize, NULL);
477 if (G_UNLIKELY (buf == NULL))
478 goto alloc_failed;
479
480 if (!gst_buffer_map (buf, &info, GST_MAP_WRITE))
481 goto buffer_read_error;
482
483 do {
484 readbytes = read (src->fd, info.data, blocksize);
485 GST_LOG_OBJECT (src, "read %" G_GSSIZE_FORMAT, readbytes);
486 } while (readbytes == -1 && errno == EINTR); /* retry if interrupted */
487
488 if (readbytes < 0)
489 goto read_error;
490
491 gst_buffer_unmap (buf, &info);
492 gst_buffer_resize (buf, 0, readbytes);
493
494 if (readbytes == 0)
495 goto eos;
496
497 GST_BUFFER_OFFSET (buf) = src->curoffset;
498 GST_BUFFER_TIMESTAMP (buf) = GST_CLOCK_TIME_NONE;
499 src->curoffset += readbytes;
500
501 GST_DEBUG_OBJECT (src, "Read buffer of size %" G_GSSIZE_FORMAT, readbytes);
502
503 /* we're done, return the buffer */
504 *outbuf = buf;
505
506 return GST_FLOW_OK;
507
508 /* ERRORS */
509 poll_error:
510 {
511 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL), ("poll on file descriptor: %s.", g_strerror (errno)));
512 GST_DEBUG_OBJECT (src, "Error during poll");
513 return GST_FLOW_ERROR;
514 }
515 stopped:
516 {
517 GST_DEBUG_OBJECT (src, "Poll stopped");
518 return GST_FLOW_FLUSHING;
519 }
520 alloc_failed:
521 {
522 GST_ERROR_OBJECT (src, "Failed to allocate %u bytes", blocksize);
523 return GST_FLOW_ERROR;
524 }
525 eos:
526 {
527 GST_DEBUG_OBJECT (src, "Read 0 bytes. EOS.");
528 gst_buffer_unref (buf);
529 return GST_FLOW_EOS;
530 }
531 read_error:
532 {
533 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL), ("read on file descriptor: %s.", g_strerror (errno)));
534 GST_ERROR_OBJECT (src, "Error reading from fd");
535 gst_buffer_unmap (buf, &info);
536 gst_buffer_unref (buf);
537 return GST_FLOW_ERROR;
538 }
539 buffer_read_error:
540 {
541 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL), ("Can't write to buffer"));
542 gst_buffer_unref (buf);
543 return GST_FLOW_ERROR;
544 }
545 }
546
547 static GstFlowReturn
gst_fd_src_create_base(GstBaseSrc * bsrc,guint64 offset,guint length,GstBuffer ** ret)548 gst_fd_src_create_base (GstBaseSrc * bsrc, guint64 offset, guint length, GstBuffer ** ret)
549 {
550 GstFlowReturn fret;
551 GstFdSrc *src = NULL;
552
553 src = GST_FD_SRC (bsrc);
554 if (src->seekable_fd) {
555 fret = gst_fd_src_create_offset (bsrc, offset, length, ret);
556 } else {
557 fret = GST_BASE_SRC_CLASS (parent_class)->create (bsrc, offset, length, ret);
558 }
559
560 return fret;
561 }
562 #endif
563
564 static GstFlowReturn
gst_fd_src_create(GstPushSrc * psrc,GstBuffer ** outbuf)565 gst_fd_src_create (GstPushSrc * psrc, GstBuffer ** outbuf)
566 {
567 GstFdSrc *src;
568 GstBuffer *buf;
569 gssize readbytes;
570 guint blocksize;
571 GstMapInfo info;
572
573 #ifndef HAVE_WIN32
574 GstClockTime timeout;
575 gboolean try_again;
576 gint retval;
577 #endif
578
579 src = GST_FD_SRC (psrc);
580
581 #ifndef HAVE_WIN32
582 if (src->timeout > 0) {
583 timeout = src->timeout * GST_USECOND;
584 } else {
585 timeout = GST_CLOCK_TIME_NONE;
586 }
587
588 do {
589 try_again = FALSE;
590
591 GST_LOG_OBJECT (src, "doing poll, timeout %" GST_TIME_FORMAT,
592 GST_TIME_ARGS (src->timeout));
593
594 retval = gst_poll_wait (src->fdset, timeout);
595 GST_LOG_OBJECT (src, "poll returned %d", retval);
596
597 if (G_UNLIKELY (retval == -1)) {
598 if (errno == EINTR || errno == EAGAIN) {
599 /* retry if interrupted */
600 try_again = TRUE;
601 } else if (errno == EBUSY) {
602 goto stopped;
603 } else {
604 goto poll_error;
605 }
606 } else if (G_UNLIKELY (retval == 0)) {
607 try_again = TRUE;
608 /* timeout, post element message */
609 gst_element_post_message (GST_ELEMENT_CAST (src),
610 gst_message_new_element (GST_OBJECT_CAST (src),
611 gst_structure_new ("GstFdSrcTimeout",
612 "timeout", G_TYPE_UINT64, src->timeout, NULL)));
613 }
614 } while (G_UNLIKELY (try_again)); /* retry if interrupted or timeout */
615 #endif
616
617 blocksize = GST_BASE_SRC (src)->blocksize;
618
619 /* create the buffer */
620 buf = gst_buffer_new_allocate (NULL, blocksize, NULL);
621 if (G_UNLIKELY (buf == NULL))
622 goto alloc_failed;
623
624 if (!gst_buffer_map (buf, &info, GST_MAP_WRITE))
625 goto buffer_read_error;
626
627 do {
628 readbytes = read (src->fd, info.data, blocksize);
629 GST_LOG_OBJECT (src, "read %" G_GSSIZE_FORMAT, readbytes);
630 } while (readbytes == -1 && errno == EINTR); /* retry if interrupted */
631
632 if (readbytes < 0)
633 goto read_error;
634
635 gst_buffer_unmap (buf, &info);
636 gst_buffer_resize (buf, 0, readbytes);
637
638 if (readbytes == 0)
639 goto eos;
640
641 GST_BUFFER_OFFSET (buf) = src->curoffset;
642 GST_BUFFER_TIMESTAMP (buf) = GST_CLOCK_TIME_NONE;
643 src->curoffset += readbytes;
644
645 GST_LOG_OBJECT (psrc, "Read buffer of size %" G_GSSIZE_FORMAT, readbytes);
646
647 /* we're done, return the buffer */
648 *outbuf = buf;
649
650 return GST_FLOW_OK;
651
652 /* ERRORS */
653 #ifndef HAVE_WIN32
654 poll_error:
655 {
656 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
657 ("poll on file descriptor: %s.", g_strerror (errno)));
658 GST_DEBUG_OBJECT (psrc, "Error during poll");
659 return GST_FLOW_ERROR;
660 }
661 stopped:
662 {
663 GST_DEBUG_OBJECT (psrc, "Poll stopped");
664 return GST_FLOW_FLUSHING;
665 }
666 #endif
667 alloc_failed:
668 {
669 GST_ERROR_OBJECT (src, "Failed to allocate %u bytes", blocksize);
670 return GST_FLOW_ERROR;
671 }
672 eos:
673 {
674 GST_DEBUG_OBJECT (psrc, "Read 0 bytes. EOS.");
675 gst_buffer_unref (buf);
676 return GST_FLOW_EOS;
677 }
678 read_error:
679 {
680 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
681 ("read on file descriptor: %s.", g_strerror (errno)));
682 GST_DEBUG_OBJECT (psrc, "Error reading from fd");
683 gst_buffer_unmap (buf, &info);
684 gst_buffer_unref (buf);
685 return GST_FLOW_ERROR;
686 }
687 buffer_read_error:
688 {
689 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL), ("Can't write to buffer"));
690 gst_buffer_unref (buf);
691 return GST_FLOW_ERROR;
692 }
693 }
694
695 static gboolean
gst_fd_src_query(GstBaseSrc * basesrc,GstQuery * query)696 gst_fd_src_query (GstBaseSrc * basesrc, GstQuery * query)
697 {
698 gboolean ret = FALSE;
699 GstFdSrc *src = GST_FD_SRC (basesrc);
700
701 switch (GST_QUERY_TYPE (query)) {
702 case GST_QUERY_URI:
703 gst_query_set_uri (query, src->uri);
704 ret = TRUE;
705 break;
706 #ifdef OHOS_EXT_FUNC
707 /**
708 * ohos.ext.func.0021
709 * Enable pull mode for gstfdsrc.
710 */
711 case GST_QUERY_SCHEDULING:
712 GST_INFO ("src->seekable_fd = %d", src->seekable_fd);
713 if (src->seekable_fd) {
714 gst_query_set_scheduling (query, GST_SCHEDULING_FLAG_SEEKABLE, 1, -1, 0);
715 gst_query_add_scheduling_mode (query, GST_PAD_MODE_PULL);
716 gst_query_add_scheduling_mode (query, GST_PAD_MODE_PUSH);
717 ret = TRUE;
718 } else {
719 /* use parent implement */
720 ret = FALSE;
721 }
722 break;
723 #endif
724 default:
725 ret = FALSE;
726 break;
727 }
728
729 if (!ret)
730 ret = GST_BASE_SRC_CLASS (parent_class)->query (basesrc, query);
731
732 return ret;
733 }
734
735 static gboolean
gst_fd_src_is_seekable(GstBaseSrc * bsrc)736 gst_fd_src_is_seekable (GstBaseSrc * bsrc)
737 {
738 GstFdSrc *src = GST_FD_SRC (bsrc);
739
740 return src->seekable_fd;
741 }
742
743 static gboolean
gst_fd_src_get_size(GstBaseSrc * bsrc,guint64 * size)744 gst_fd_src_get_size (GstBaseSrc * bsrc, guint64 * size)
745 {
746 GstFdSrc *src = GST_FD_SRC (bsrc);
747 struct_stat stat_results;
748
749 if (src->size != -1) {
750 *size = src->size;
751 return TRUE;
752 }
753
754 if (!src->seekable_fd) {
755 /* If it isn't seekable, we won't know the length (but fstat will still
756 * succeed, and wrongly say our length is zero. */
757 return FALSE;
758 }
759
760 if (fstat (src->fd, &stat_results) < 0)
761 goto could_not_stat;
762
763 *size = stat_results.st_size;
764
765 return TRUE;
766
767 /* ERROR */
768 could_not_stat:
769 {
770 return FALSE;
771 }
772 }
773
774 static gboolean
gst_fd_src_do_seek(GstBaseSrc * bsrc,GstSegment * segment)775 gst_fd_src_do_seek (GstBaseSrc * bsrc, GstSegment * segment)
776 {
777 gint res;
778 gint64 offset;
779 GstFdSrc *src = GST_FD_SRC (bsrc);
780
781 offset = segment->start;
782
783 /* No need to seek to the current position */
784 if (offset == src->curoffset)
785 return TRUE;
786
787 #ifdef OHOS_EXT_FUNC
788 /**
789 * ohos.ext.func.0020
790 * Add start offset handling in gstfdsrc.
791 */
792 res = lseek (src->fd, offset + src->start_offset, SEEK_SET);
793 if (G_UNLIKELY ((res < 0) || (res != (offset + src->start_offset)))) {
794 goto seek_failed;
795 }
796
797 /**
798 * ohos.ext.func.0021
799 * Enable pull mode for gstfdsrc. When demux pulls buffer from this element,
800 * we need to ensure the requested position and current position equaled.
801 * Thus, we set current position to seeked position when seeking.
802 */
803 if (src->seekable_fd) {
804 GST_INFO ("gst_fd_src_do_seek, offset=%" G_GINT64_FORMAT, offset);
805 src->curoffset = offset;
806 }
807 #else
808 res = lseek (src->fd, offset, SEEK_SET);
809 if (G_UNLIKELY (res < 0 || res != offset))
810 goto seek_failed;
811 #endif
812
813 segment->position = segment->start;
814 segment->time = segment->start;
815
816 return TRUE;
817
818 seek_failed:
819 GST_DEBUG_OBJECT (src, "lseek returned %" G_GINT64_FORMAT, offset);
820 return FALSE;
821 }
822
823 /*** GSTURIHANDLER INTERFACE *************************************************/
824
825 static GstURIType
gst_fd_src_uri_get_type(GType type)826 gst_fd_src_uri_get_type (GType type)
827 {
828 return GST_URI_SRC;
829 }
830
831 static const gchar *const *
gst_fd_src_uri_get_protocols(GType type)832 gst_fd_src_uri_get_protocols (GType type)
833 {
834 static const gchar *protocols[] = { "fd", NULL };
835
836 return protocols;
837 }
838
839 static gchar *
gst_fd_src_uri_get_uri(GstURIHandler * handler)840 gst_fd_src_uri_get_uri (GstURIHandler * handler)
841 {
842 GstFdSrc *src = GST_FD_SRC (handler);
843
844 /* FIXME: make thread-safe */
845 return g_strdup (src->uri);
846 }
847
848 static gboolean
gst_fd_src_uri_set_uri(GstURIHandler * handler,const gchar * uri,GError ** err)849 gst_fd_src_uri_set_uri (GstURIHandler * handler, const gchar * uri,
850 GError ** err)
851 {
852 gchar *protocol, *q;
853 GstFdSrc *src = GST_FD_SRC (handler);
854 gint fd;
855 guint64 size = (guint64) - 1;
856
857 GST_INFO_OBJECT (src, "checking uri %s", uri);
858
859 protocol = gst_uri_get_protocol (uri);
860 if (strcmp (protocol, "fd") != 0) {
861 g_set_error (err, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
862 "Wrong protocol for fdsrc in uri: '%s'", uri);
863 g_free (protocol);
864 return FALSE;
865 }
866 g_free (protocol);
867
868 if (sscanf (uri, "fd://%d", &fd) != 1 || fd < 0) {
869 g_set_error (err, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
870 "Bad file descriptor number in uri: '%s'", uri);
871 return FALSE;
872 }
873
874 if ((q = g_strstr_len (uri, -1, "?"))) {
875 gchar *sp, *end = NULL;
876
877 GST_INFO_OBJECT (src, "found ?");
878
879 if ((sp = g_strstr_len (q, -1, "size="))) {
880 sp += strlen ("size=");
881 size = g_ascii_strtoull (sp, &end, 10);
882 if ((size == 0 && errno == EINVAL) || size == G_MAXUINT64 || end == sp) {
883 GST_INFO_OBJECT (src, "parsing size failed");
884 size = -1;
885 } else {
886 GST_INFO_OBJECT (src, "found size %" G_GUINT64_FORMAT, size);
887 }
888 }
889 #ifdef OHOS_EXT_FUNC
890 /**
891 * ohos.ext.func.0020
892 * Add start offset handling in gstfdsrc.
893 */
894 sp = g_strstr_len (q, -1, "offset=");
895 if (sp != NULL) {
896 if (sscanf (sp, "offset=%" G_GUINT64_FORMAT, &src->start_offset) != 1) {
897 GST_INFO_OBJECT (src, "parsing start_offset failed");
898 src->start_offset = 0;
899 } else {
900 GST_INFO_OBJECT (src, "found start_offset %" G_GUINT64_FORMAT, src->start_offset);
901 }
902 }
903 #endif
904 }
905
906 src->new_fd = fd;
907
908 GST_OBJECT_LOCK (src);
909 if (GST_STATE (GST_ELEMENT (src)) <= GST_STATE_READY) {
910 gst_fd_src_update_fd (src, size);
911 }
912 GST_OBJECT_UNLOCK (src);
913
914 return TRUE;
915 }
916
917 static void
gst_fd_src_uri_handler_init(gpointer g_iface,gpointer iface_data)918 gst_fd_src_uri_handler_init (gpointer g_iface, gpointer iface_data)
919 {
920 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
921
922 iface->get_type = gst_fd_src_uri_get_type;
923 iface->get_protocols = gst_fd_src_uri_get_protocols;
924 iface->get_uri = gst_fd_src_uri_get_uri;
925 iface->set_uri = gst_fd_src_uri_set_uri;
926 }
927