soho123

gstfdsrc

Mar 19th, 2014
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.47 KB | None | 0 0
  1. /* GStreamer
  2. * Copyright (C) 1999,2000 Erik Walthinsen <[email protected]>
  3. * 2000 Wim Taymans <[email protected]>
  4. * 2005 Philippe Khalaf <[email protected]>
  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., 59 Temple Place - Suite 330,
  21. * Boston, MA 02111-1307, USA.
  22. */
  23. /**
  24. * SECTION:element-fdsrc
  25. * @see_also: #GstFdSink
  26. *
  27. * Read data from a unix file descriptor.
  28. *
  29. * To generate data, enter some data on the console folowed by enter.
  30. * The above mentioned pipeline should dump data packets to the console.
  31. *
  32. * If the #GstFdSrc:timeout property is set to a value bigger than 0, fdsrc will
  33. * generate an element message named
  34. * <classname>&quot;GstFdSrcTimeout&quot;</classname>
  35. * if no data was recieved in the given timeout.
  36. * The message's structure contains one field:
  37. * <itemizedlist>
  38. * <listitem>
  39. * <para>
  40. * #guint64
  41. * <classname>&quot;timeout&quot;</classname>: the timeout in microseconds that
  42. * expired when waiting for data.
  43. * </para>
  44. * </listitem>
  45. * </itemizedlist>
  46. *
  47. * <refsect2>
  48. * <title>Example launch line</title>
  49. * |[
  50. * echo "Hello GStreamer" | gst-launch -v fdsrc ! fakesink dump=true
  51. * ]| A simple pipeline to read from the standard input and dump the data
  52. * with a fakesink as hex ascii block.
  53. * </refsect2>
  54. *
  55. * Last reviewed on 2008-06-20 (0.10.21)
  56. */
  57.  
  58. #ifdef HAVE_CONFIG_H
  59. # include "config.h"
  60. #endif
  61. #include "gst/gst_private.h"
  62. #include <unistd.h>
  63. #include <sys/types.h>
  64. #include <sys/socket.h>
  65. #include <sys/stat.h>
  66. #include <unistd.h>
  67. #include <stdio.h>
  68. #include <sys/ioctl.h>
  69. #ifdef G_OS_WIN32
  70. #include <io.h> /* lseek, open, close, read */
  71. #undef lseek
  72. #define lseek _lseeki64
  73. #undef off_t
  74. #define off_t guint64
  75. #endif
  76.  
  77. #include <sys/stat.h>
  78. #ifdef HAVE_SYS_SOCKET_H
  79. #include <sys/socket.h>
  80. #endif
  81. #include <fcntl.h>
  82. #include <stdio.h>
  83. #ifdef HAVE_UNISTD_H
  84. #include <unistd.h>
  85. #endif
  86. #ifdef _MSC_VER
  87. #undef stat
  88. #define stat _stat
  89. #define fstat _fstat
  90. #define S_ISREG(m) (((m)&S_IFREG)==S_IFREG)
  91. #endif
  92. #include <stdlib.h>
  93. #include <errno.h>
  94.  
  95. #include "gstfdsrc.h"
  96. unsigned int isSeek=0;
  97. static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
  98. GST_PAD_SRC,
  99. GST_PAD_ALWAYS,
  100. GST_STATIC_CAPS_ANY);
  101.  
  102. GST_DEBUG_CATEGORY_STATIC (gst_fd_src_debug);
  103. #define GST_CAT_DEFAULT gst_fd_src_debug
  104.  
  105. #define DEFAULT_FD 0
  106. #define DEFAULT_TIMEOUT 0
  107.  
  108. enum
  109. {
  110. PROP_0,
  111.  
  112. PROP_FD,
  113. PROP_TIMEOUT,
  114. PROP_NETSOCK_FD,
  115.  
  116. PROP_LAST
  117. };
  118.  
  119. static void gst_fd_src_uri_handler_init (gpointer g_iface, gpointer iface_data);
  120.  
  121. static void
  122. _do_init (GType fd_src_type)
  123. {
  124. static const GInterfaceInfo urihandler_info = {
  125. gst_fd_src_uri_handler_init,
  126. NULL,
  127. NULL
  128. };
  129.  
  130. g_type_add_interface_static (fd_src_type, GST_TYPE_URI_HANDLER,
  131. &urihandler_info);
  132.  
  133. GST_DEBUG_CATEGORY_INIT (gst_fd_src_debug, "fdsrc", 0, "fdsrc element");
  134. }
  135.  
  136. GST_BOILERPLATE_FULL (GstFdSrc, gst_fd_src, GstPushSrc, GST_TYPE_PUSH_SRC,
  137. _do_init);
  138.  
  139. static void gst_fd_src_set_property (GObject * object, guint prop_id,
  140. const GValue * value, GParamSpec * pspec);
  141. static void gst_fd_src_get_property (GObject * object, guint prop_id,
  142. GValue * value, GParamSpec * pspec);
  143. static void gst_fd_src_dispose (GObject * obj);
  144.  
  145. static gboolean gst_fd_src_start (GstBaseSrc * bsrc);
  146. static gboolean gst_fd_src_stop (GstBaseSrc * bsrc);
  147. static gboolean gst_fd_src_unlock (GstBaseSrc * bsrc);
  148. static gboolean gst_fd_src_unlock_stop (GstBaseSrc * bsrc);
  149. static gboolean gst_fd_src_is_seekable (GstBaseSrc * bsrc);
  150. static gboolean gst_fd_src_get_size (GstBaseSrc * src, guint64 * size);
  151. static gboolean gst_fd_src_do_seek (GstBaseSrc * src, GstSegment * segment);
  152. static gboolean gst_fd_src_query (GstBaseSrc * src, GstQuery * query);
  153.  
  154. static GstFlowReturn gst_fd_src_create (GstPushSrc * psrc, GstBuffer ** outbuf);
  155.  
  156. static void
  157. gst_fd_src_base_init (gpointer g_class)
  158. {
  159. GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
  160. gst_element_class_set_details_simple (gstelement_class,
  161. "Filedescriptor Source",
  162. "Source/File",
  163. "Read from a file descriptor", "Erik Walthinsen <[email protected]>");
  164. gst_element_class_add_pad_template (gstelement_class,
  165. gst_static_pad_template_get (&srctemplate));
  166. }
  167.  
  168. static void
  169. gst_fd_src_class_init (GstFdSrcClass * klass)
  170. {
  171. GObjectClass *gobject_class;
  172. GstBaseSrcClass *gstbasesrc_class;
  173. GstPushSrcClass *gstpush_src_class;
  174.  
  175. gobject_class = G_OBJECT_CLASS (klass);
  176. gstbasesrc_class = GST_BASE_SRC_CLASS (klass);
  177. gstpush_src_class = GST_PUSH_SRC_CLASS (klass);
  178.  
  179. gobject_class->set_property = gst_fd_src_set_property;
  180. gobject_class->get_property = gst_fd_src_get_property;
  181. gobject_class->dispose = gst_fd_src_dispose;
  182.  
  183. g_object_class_install_property (gobject_class, PROP_FD,
  184. g_param_spec_int ("fd", "fd", "An open file descriptor to read from",
  185. 0, G_MAXINT, DEFAULT_FD, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
  186.  
  187. g_object_class_install_property (gobject_class, PROP_NETSOCK_FD,
  188. g_param_spec_int ("is-net", "is-net", "Act like a live source",
  189. 0, 1, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
  190. /**
  191. * GstFdSrc:timeout
  192. *
  193. * Post a message after timeout microseconds
  194. *
  195. * Since: 0.10.21
  196. */
  197. g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_TIMEOUT,
  198. g_param_spec_uint64 ("timeout", "Timeout",
  199. "Post a message after timeout microseconds (0 = disabled)", 0,
  200. G_MAXUINT64, DEFAULT_TIMEOUT,
  201. G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
  202.  
  203. gstbasesrc_class->start = GST_DEBUG_FUNCPTR (gst_fd_src_start);
  204. gstbasesrc_class->stop = GST_DEBUG_FUNCPTR (gst_fd_src_stop);
  205. gstbasesrc_class->unlock = GST_DEBUG_FUNCPTR (gst_fd_src_unlock);
  206. gstbasesrc_class->unlock_stop = GST_DEBUG_FUNCPTR (gst_fd_src_unlock_stop);
  207. gstbasesrc_class->is_seekable = GST_DEBUG_FUNCPTR (gst_fd_src_is_seekable);
  208. gstbasesrc_class->get_size = GST_DEBUG_FUNCPTR (gst_fd_src_get_size);
  209. gstbasesrc_class->do_seek = GST_DEBUG_FUNCPTR (gst_fd_src_do_seek);
  210. gstbasesrc_class->query = GST_DEBUG_FUNCPTR (gst_fd_src_query);
  211.  
  212. gstpush_src_class->create = GST_DEBUG_FUNCPTR (gst_fd_src_create);
  213. }
  214.  
  215. static void
  216. gst_fd_src_init (GstFdSrc * fdsrc, GstFdSrcClass * klass)
  217. {
  218. fdsrc->new_fd = DEFAULT_FD;
  219. fdsrc->seekable_fd = FALSE;
  220. fdsrc->fd = -1;
  221. fdsrc->size = -1;
  222. fdsrc->timeout = DEFAULT_TIMEOUT;
  223. fdsrc->uri = g_strdup_printf ("fd://0");
  224. fdsrc->curoffset = 0;
  225. fdsrc->isNetfd=0;
  226. }
  227.  
  228. static void
  229. gst_fd_src_dispose (GObject * obj)
  230. {
  231. GstFdSrc *src = GST_FD_SRC (obj);
  232. g_free (src->uri);
  233. src->uri = NULL;
  234.  
  235. G_OBJECT_CLASS (parent_class)->dispose (obj);
  236. }
  237.  
  238. static void
  239. gst_fd_src_update_fd (GstFdSrc * src, guint64 size)
  240. {
  241. struct stat stat_results;
  242.  
  243. GST_DEBUG_OBJECT (src, "fdset %p, old_fd %d, new_fd %d", src->fdset, src->fd,
  244. src->new_fd);
  245. /* we need to always update the fdset since it may not have existed when
  246. * gst_fd_src_update_fd () was called earlier */
  247. if (src->fdset != NULL) {
  248. GstPollFD fd = GST_POLL_FD_INIT;
  249.  
  250. if (src->fd >= 0) {
  251. fd.fd = src->fd;
  252. /* this will log a harmless warning, if it was never added */
  253. gst_poll_remove_fd (src->fdset, &fd);
  254. }
  255.  
  256. fd.fd = src->new_fd;
  257. gst_poll_add_fd (src->fdset, &fd);
  258. gst_poll_fd_ctl_read (src->fdset, &fd, TRUE);
  259. }
  260.  
  261.  
  262. if (src->fd != src->new_fd) {
  263. GST_INFO_OBJECT (src, "Updating to fd %d", src->new_fd);
  264.  
  265. src->fd = src->new_fd;
  266.  
  267. GST_INFO_OBJECT (src, "Setting size to fd %" G_GUINT64_FORMAT, size);
  268. src->size = size;
  269.  
  270. g_free (src->uri);
  271. src->uri = g_strdup_printf ("fd://%d", src->fd);
  272. if(src->isNetfd == 0){
  273. if (fstat (src->fd, &stat_results) < 0)
  274. goto not_seekable;
  275.  
  276. if (!S_ISREG (stat_results.st_mode))
  277. goto not_seekable;
  278.  
  279. /* Try a seek of 0 bytes offset to check for seekability */
  280. if (lseek (src->fd, 0, SEEK_CUR) < 0)
  281. goto not_seekable;
  282. }
  283. GST_INFO_OBJECT (src, "marking fd %d as seekable", src->fd);
  284. src->seekable_fd = TRUE;
  285.  
  286. gst_base_src_set_dynamic_size (GST_BASE_SRC (src), TRUE);
  287. }
  288. return;
  289.  
  290. not_seekable:
  291. {
  292. GST_INFO_OBJECT (src, "marking fd %d as NOT seekable", src->fd);
  293. src->seekable_fd = FALSE;
  294. gst_base_src_set_dynamic_size (GST_BASE_SRC (src), FALSE);
  295. }
  296. }
  297.  
  298. static gboolean
  299. gst_fd_src_start (GstBaseSrc * bsrc)
  300. {
  301. GstFdSrc *src = GST_FD_SRC (bsrc);
  302. src->curoffset = 0;
  303.  
  304. if ((src->fdset = gst_poll_new (TRUE)) == NULL)
  305. goto socket_pair;
  306.  
  307. gst_fd_src_update_fd (src, -1);
  308.  
  309. return TRUE;
  310.  
  311. /* ERRORS */
  312. socket_pair:
  313. {
  314. GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
  315. GST_ERROR_SYSTEM);
  316. return FALSE;
  317. }
  318. }
  319.  
  320. static gboolean
  321. gst_fd_src_stop (GstBaseSrc * bsrc)
  322. {
  323. GstFdSrc *src = GST_FD_SRC (bsrc);
  324. if (src->fdset) {
  325. gst_poll_free (src->fdset);
  326. src->fdset = NULL;
  327. }
  328.  
  329. return TRUE;
  330. }
  331.  
  332. static gboolean
  333. gst_fd_src_unlock (GstBaseSrc * bsrc)
  334. {
  335. GstFdSrc *src = GST_FD_SRC (bsrc);
  336. GST_LOG_OBJECT (src, "Flushing");
  337. GST_OBJECT_LOCK (src);
  338. gst_poll_set_flushing (src->fdset, TRUE);
  339. GST_OBJECT_UNLOCK (src);
  340.  
  341. return TRUE;
  342. }
  343.  
  344. static gboolean
  345. gst_fd_src_unlock_stop (GstBaseSrc * bsrc)
  346. {
  347. GstFdSrc *src = GST_FD_SRC (bsrc);
  348.  
  349. GST_LOG_OBJECT (src, "No longer flushing");
  350. GST_OBJECT_LOCK (src);
  351. gst_poll_set_flushing (src->fdset, FALSE);
  352. GST_OBJECT_UNLOCK (src);
  353.  
  354. return TRUE;
  355. }
  356.  
  357. static void
  358. gst_fd_src_set_property (GObject * object, guint prop_id, const GValue * value,
  359. GParamSpec * pspec)
  360. {
  361. GstFdSrc *src = GST_FD_SRC (object);
  362.  
  363. switch (prop_id) {
  364. case PROP_FD:
  365. src->new_fd = g_value_get_int (value);
  366.  
  367. /* If state is ready or below, update the current fd immediately
  368. * so it is reflected in get_properties and uri */
  369. GST_OBJECT_LOCK (object);
  370. if (GST_STATE (GST_ELEMENT (src)) <= GST_STATE_READY) {
  371. GST_DEBUG_OBJECT (src, "state ready or lower, updating to use new fd");
  372. gst_fd_src_update_fd (src, -1);
  373. } else {
  374. GST_DEBUG_OBJECT (src, "state above ready, not updating to new fd yet");
  375. }
  376. GST_OBJECT_UNLOCK (object);
  377. break;
  378. case PROP_TIMEOUT:
  379. src->timeout = g_value_get_uint64 (value);
  380. GST_DEBUG_OBJECT (src, "poll timeout set to %" GST_TIME_FORMAT,
  381. GST_TIME_ARGS (src->timeout));
  382. break;
  383. case PROP_NETSOCK_FD:
  384. src->isNetfd = g_value_get_uint (value);
  385. break;
  386. default:
  387. G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
  388. break;
  389. }
  390. }
  391.  
  392. static void
  393. gst_fd_src_get_property (GObject * object, guint prop_id, GValue * value,
  394. GParamSpec * pspec)
  395. {
  396. GstFdSrc *src = GST_FD_SRC (object);
  397.  
  398. switch (prop_id) {
  399. case PROP_FD:
  400. g_value_set_int (value, src->fd);
  401. break;
  402. case PROP_TIMEOUT:
  403. g_value_set_uint64 (value, src->timeout);
  404. break;
  405. default:
  406. G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
  407. break;
  408. }
  409. }
  410.  
  411. int CheckIsSeekPointReceived(int sock_fd)
  412. {
  413. //check if the special char '$' arrived
  414. //here should put the code to check
  415. }
  416.  
  417.  
  418. static GstFlowReturn
  419. gst_fd_src_create (GstPushSrc * psrc, GstBuffer ** outbuf)
  420. {
  421. GstFdSrc *src;
  422. GstBuffer *buf;
  423. gssize readbytes=0;
  424. guint blocksize;
  425. GstClockTime timeout;
  426. int reset_data=0;
  427. #ifndef HAVE_WIN32
  428. gboolean try_again;
  429. gint retval;
  430. #endif
  431.  
  432. src = GST_FD_SRC (psrc);
  433.  
  434. if (src->timeout > 0) {
  435. timeout = src->timeout * GST_USECOND;
  436. } else {
  437. timeout = GST_CLOCK_TIME_NONE;
  438. }
  439.  
  440. #ifndef HAVE_WIN32
  441. do {
  442. try_again = FALSE;
  443.  
  444. GST_LOG_OBJECT (src, "doing poll, timeout %" GST_TIME_FORMAT,
  445. GST_TIME_ARGS (src->timeout));
  446.  
  447. retval = gst_poll_wait (src->fdset, timeout);
  448. GST_LOG_OBJECT (src, "poll returned %d", retval);
  449.  
  450. if (G_UNLIKELY (retval == -1)) {
  451. if (errno == EINTR || errno == EAGAIN) {
  452. /* retry if interrupted */
  453. try_again = TRUE;
  454. } else if (errno == EBUSY) {
  455. goto stopped;
  456. } else {
  457. goto poll_error;
  458. }
  459. } else if (G_UNLIKELY (retval == 0)) {
  460. try_again = TRUE;
  461. /* timeout, post element message */
  462. gst_element_post_message (GST_ELEMENT_CAST (src),
  463. gst_message_new_element (GST_OBJECT_CAST (src),
  464. gst_structure_new ("GstFdSrcTimeout",
  465. "timeout", G_TYPE_UINT64, src->timeout, NULL)));
  466. }
  467. } while (G_UNLIKELY (try_again)); /* retry if interrupted or timeout */
  468. #endif
  469.  
  470. blocksize = GST_BASE_SRC (src)->blocksize;
  471. /* create the buffer */
  472. buf = gst_buffer_try_new_and_alloc (blocksize);
  473. if (G_UNLIKELY (buf == NULL)) {
  474. GST_ERROR_OBJECT (src, "Failed to allocate %u bytes", blocksize);
  475. return GST_FLOW_ERROR;
  476. }
  477.  
  478.  
  479. SearchSeek:
  480. if(src->isNetfd == 1&& isSeek == 1){
  481. do {
  482. readbytes = read (src->fd, GST_BUFFER_DATA (buf), blocksize);
  483. GST_LOG_OBJECT (src, "read %" G_GSSIZE_FORMAT, readbytes);
  484. } while (readbytes == -1 && errno == EINTR); /* retry if interrupted */
  485.  
  486. if (readbytes < 0){
  487. goto read_error;
  488. }
  489.  
  490. if (readbytes == 0)
  491. goto eos;
  492. }
  493. else{
  494. do {
  495. readbytes = read (src->fd, GST_BUFFER_DATA (buf), blocksize);
  496. GST_LOG_OBJECT (src, "read %" G_GSSIZE_FORMAT, readbytes);
  497. } while (readbytes == -1 && errno == EINTR); /* retry if interrupted */
  498.  
  499. if (readbytes < 0){
  500. goto read_error;
  501. }
  502.  
  503. if (readbytes == 0)
  504. goto eos;
  505.  
  506. }
  507. if(src->isNetfd == 1&& isSeek == 1){
  508. if(!CheckIsSeekPointReceived(src->fd)){
  509. reset_data = 1;
  510. }else{
  511. reset_data = 0;
  512. isSeek = 0;
  513. }
  514. }
  515. if(src->isNetfd == 1 && isSeek == 1){
  516. goto SearchSeek;
  517. }
  518.  
  519. GST_BUFFER_OFFSET (buf) = src->curoffset;
  520. GST_BUFFER_SIZE (buf) = readbytes;
  521. GST_BUFFER_TIMESTAMP (buf) = GST_CLOCK_TIME_NONE;
  522. src->curoffset += readbytes;
  523. *outbuf = buf;
  524. GST_LOG_OBJECT (psrc, "Read buffer of size %" G_GSSIZE_FORMAT, readbytes);
  525. /* we're done, return the buffer */
  526.  
  527.  
  528. return GST_FLOW_OK;
  529.  
  530. /* ERRORS */
  531. #ifndef HAVE_WIN32
  532. poll_error:
  533. {
  534. GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
  535. ("poll on file descriptor: %s.", g_strerror (errno)));
  536. GST_DEBUG_OBJECT (psrc, "Error during poll");
  537. return GST_FLOW_ERROR;
  538. }
  539. stopped:
  540. {
  541. GST_DEBUG_OBJECT (psrc, "Poll stopped");
  542. return GST_FLOW_WRONG_STATE;
  543. }
  544. #endif
  545. eos:
  546. {
  547. GST_DEBUG_OBJECT (psrc, "Read 0 bytes. EOS.");
  548. gst_buffer_unref (buf);
  549. return GST_FLOW_UNEXPECTED;
  550. }
  551. read_error:
  552. {
  553. GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
  554. ("read on file descriptor: %s.", g_strerror (errno)));
  555. GST_DEBUG_OBJECT (psrc, "Error reading from fd");
  556. gst_buffer_unref (buf);
  557. return GST_FLOW_ERROR;
  558. }
  559. }
  560.  
  561. static gboolean
  562. gst_fd_src_query (GstBaseSrc * basesrc, GstQuery * query)
  563. {
  564. gboolean ret = FALSE;
  565. GstFdSrc *src = GST_FD_SRC (basesrc);
  566. switch (GST_QUERY_TYPE (query)) {
  567. case GST_QUERY_URI:
  568. gst_query_set_uri (query, src->uri);
  569. ret = TRUE;
  570. break;
  571. default:
  572. ret = FALSE;
  573. break;
  574. }
  575.  
  576. if (!ret)
  577. ret = GST_BASE_SRC_CLASS (parent_class)->query (basesrc, query);
  578.  
  579. return ret;
  580. }
  581.  
  582. static gboolean
  583. gst_fd_src_is_seekable (GstBaseSrc * bsrc)
  584. {
  585. GstFdSrc *src = GST_FD_SRC (bsrc);
  586.  
  587. return src->seekable_fd;
  588. }
  589.  
  590. static gboolean
  591. gst_fd_src_get_size (GstBaseSrc * bsrc, guint64 * size)
  592. {
  593. GstFdSrc *src = GST_FD_SRC (bsrc);
  594. struct stat stat_results;
  595.  
  596. if (src->size != -1) {
  597. *size = src->size;
  598. return TRUE;
  599. }
  600.  
  601. if (!src->seekable_fd) {
  602. /* If it isn't seekable, we won't know the length (but fstat will still
  603. * succeed, and wrongly say our length is zero. */
  604. return FALSE;
  605. }
  606.  
  607. if (fstat (src->fd, &stat_results) < 0)
  608. goto could_not_stat;
  609.  
  610. *size = stat_results.st_size;
  611.  
  612. return TRUE;
  613.  
  614. /* ERROR */
  615. could_not_stat:
  616. {
  617. return FALSE;
  618. }
  619. }
  620.  
  621.  
  622. static gboolean
  623. gst_fd_src_do_seek (GstBaseSrc * bsrc, GstSegment * segment)
  624. {
  625. gint res;
  626. gint64 offset;
  627. GstFdSrc *src = GST_FD_SRC (bsrc);
  628. char syscmd[64]={0};
  629. offset = segment->start;
  630.  
  631. /* No need to seek to the current position */
  632. if (offset == src->curoffset)
  633. return TRUE;
  634.  
  635.  
  636.  
  637. if(src->isNetfd == 0){
  638. res = lseek (src->fd, offset, SEEK_SET);
  639. if (G_UNLIKELY (res < 0 || res != offset))
  640. goto seek_failed;
  641. }
  642. src->curoffset = segment->start;
  643. segment->last_stop = segment->start;
  644. segment->time = segment->start;
  645. if(src->isNetfd == 1){
  646. isSeek = 1;
  647. }
  648.  
  649. return TRUE;
  650.  
  651. seek_failed:
  652. GST_DEBUG_OBJECT (src, "lseek returned %" G_GINT64_FORMAT, offset);
  653. return FALSE;
  654. }
  655.  
  656. /*** GSTURIHANDLER INTERFACE *************************************************/
  657.  
  658. static GstURIType
  659. gst_fd_src_uri_get_type (void)
  660. {
  661. return GST_URI_SRC;
  662. }
  663.  
  664. static gchar **
  665. gst_fd_src_uri_get_protocols (void)
  666. {
  667. static gchar *protocols[] = { (char *) "fd", NULL };
  668.  
  669. return protocols;
  670. }
  671.  
  672. static const gchar *
  673. gst_fd_src_uri_get_uri (GstURIHandler * handler)
  674. {
  675. GstFdSrc *src = GST_FD_SRC (handler);
  676.  
  677. return src->uri;
  678. }
  679.  
  680. static gboolean
  681. gst_fd_src_uri_set_uri (GstURIHandler * handler, const gchar * uri)
  682. {
  683. gchar *protocol, *q;
  684. GstFdSrc *src = GST_FD_SRC (handler);
  685. gint fd;
  686. guint64 size = (guint64) - 1;
  687. guint isNet;
  688. GST_INFO_OBJECT (src, "checking uri %s", uri);
  689. protocol = gst_uri_get_protocol (uri);
  690. if (strcmp (protocol, "fd") != 0) {
  691. g_free (protocol);
  692. return FALSE;
  693. }
  694. g_free (protocol);
  695.  
  696. if (sscanf (uri, "fd://%d", &fd) != 1 || fd < 0)
  697. return FALSE;
  698.  
  699. if ((q = g_strstr_len (uri, -1, "?"))) {
  700. gchar *sp;
  701.  
  702. GST_INFO_OBJECT (src, "found ?");
  703.  
  704. if ((sp = g_strstr_len (q, -1, "size="))) {
  705. if (sscanf (sp, "size=%" G_GUINT64_FORMAT, &size) != 1) {
  706. GST_INFO_OBJECT (src, "parsing size failed");
  707. size = -1;
  708. } else {
  709. GST_INFO_OBJECT (src, "found size %" G_GUINT64_FORMAT, size);
  710. }
  711. }
  712. if ((sp = g_strstr_len (q, -1, "is-net="))) {
  713. if (sscanf (sp, "is-net=%d", &isNet) != 1) {
  714. GST_INFO_OBJECT (src, "parsing isNet failed");
  715. isNet = 0;
  716. } else {
  717. GST_INFO_OBJECT (src, "found size %d", isNet);
  718. }
  719. }
  720. }
  721.  
  722. src->new_fd = fd;
  723. src->isNetfd = isNet;
  724. GST_OBJECT_LOCK (src);
  725. if (GST_STATE (GST_ELEMENT (src)) <= GST_STATE_READY) {
  726. gst_fd_src_update_fd (src, size);
  727. }
  728. GST_OBJECT_UNLOCK (src);
  729.  
  730. return TRUE;
  731. }
  732.  
  733. static void
  734. gst_fd_src_uri_handler_init (gpointer g_iface, gpointer iface_data)
  735. {
  736. GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
  737.  
  738. iface->get_type = gst_fd_src_uri_get_type;
  739. iface->get_protocols = gst_fd_src_uri_get_protocols;
  740. iface->get_uri = gst_fd_src_uri_get_uri;
  741. iface->set_uri = gst_fd_src_uri_set_uri;
  742. }
Advertisement
Add Comment
Please, Sign In to add comment