gstfdsink.c 15.7 KB
Newer Older
Erik Walthinsen's avatar
Erik Walthinsen committed
1 2 3 4
/* GStreamer
 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
 *                    2000 Wim Taymans <wtay@chello.be>
 *
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
5
 * gstfdsink.c:
Erik Walthinsen's avatar
Erik Walthinsen committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 * 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.
 */

Wim Taymans's avatar
Wim Taymans committed
23 24 25 26 27
/**
 * SECTION:element-fdsink
 * @see_also: #GstFdSrc
 *
 * Write data to a unix file descriptor.
28
 *
29
 * This element will synchronize on the clock before writing the data on the
30
 * socket. For file descriptors where this does not make sense (files, ...) the
31
 * #GstBaseSink:sync property can be used to disable synchronisation.
32 33
 *
 * Last reviewed on 2006-04-28 (0.10.6)
Wim Taymans's avatar
Wim Taymans committed
34 35
 */

36 37 38 39
#ifdef HAVE_CONFIG_H
#  include "config.h"
#endif

Philippe Rouquier's avatar
Philippe Rouquier committed
40 41 42
#include "../../gst/gst-i18n-lib.h"

#include <sys/types.h>
LRN's avatar
LRN committed
43 44 45 46 47 48 49 50 51

#ifdef G_OS_WIN32
#include <io.h>                 /* lseek, open, close, read */
#undef lseek
#define lseek _lseeki64
#undef off_t
#define off_t guint64
#endif

Philippe Rouquier's avatar
Philippe Rouquier committed
52
#include <sys/stat.h>
53
#ifdef HAVE_SYS_SOCKET_H
Philippe Rouquier's avatar
Philippe Rouquier committed
54
#include <sys/socket.h>
55
#endif
Philippe Rouquier's avatar
Philippe Rouquier committed
56 57
#include <fcntl.h>
#include <stdio.h>
58
#ifdef HAVE_UNISTD_H
59
#include <unistd.h>
60
#endif
Philippe Rouquier's avatar
Philippe Rouquier committed
61
#ifdef _MSC_VER
62 63 64 65
#undef stat
#define stat _stat
#define fstat _fstat
#define S_ISREG(m)	(((m)&S_IFREG)==S_IFREG)
Philippe Rouquier's avatar
Philippe Rouquier committed
66 67 68 69 70 71
#endif
#include <errno.h>
#include <string.h>

#include "gstfdsink.h"

72 73 74 75 76
static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
    GST_PAD_SINK,
    GST_PAD_ALWAYS,
    GST_STATIC_CAPS_ANY);

77 78
GST_DEBUG_CATEGORY_STATIC (gst_fd_sink__debug);
#define GST_CAT_DEFAULT gst_fd_sink__debug
Erik Walthinsen's avatar
Erik Walthinsen committed
79 80 81


/* FdSink signals and args */
82 83
enum
{
Erik Walthinsen's avatar
Erik Walthinsen committed
84 85 86 87
  /* FILL ME */
  LAST_SIGNAL
};

88 89
enum
{
Erik Walthinsen's avatar
Erik Walthinsen committed
90 91 92 93
  ARG_0,
  ARG_FD
};

Philippe Rouquier's avatar
Philippe Rouquier committed
94 95
static void gst_fd_sink_uri_handler_init (gpointer g_iface,
    gpointer iface_data);
Erik Walthinsen's avatar
Erik Walthinsen committed
96

97 98
#define _do_init \
  G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER, gst_fd_sink_uri_handler_init); \
Philippe Rouquier's avatar
Philippe Rouquier committed
99
  GST_DEBUG_CATEGORY_INIT (gst_fd_sink__debug, "fdsink", 0, "fdsink element");
100 101
#define gst_fd_sink_parent_class parent_class
G_DEFINE_TYPE_WITH_CODE (GstFdSink, gst_fd_sink, GST_TYPE_BASE_SINK, _do_init);
Erik Walthinsen's avatar
Erik Walthinsen committed
102

Philippe Rouquier's avatar
Philippe Rouquier committed
103
static void gst_fd_sink_set_property (GObject * object, guint prop_id,
104
    const GValue * value, GParamSpec * pspec);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
105
static void gst_fd_sink_get_property (GObject * object, guint prop_id,
106
    GValue * value, GParamSpec * pspec);
Philippe Rouquier's avatar
Philippe Rouquier committed
107
static void gst_fd_sink_dispose (GObject * obj);
Wim Taymans's avatar
Wim Taymans committed
108

109
static gboolean gst_fd_sink_query (GstBaseSink * bsink, GstQuery * query);
Philippe Rouquier's avatar
Philippe Rouquier committed
110 111 112 113 114
static GstFlowReturn gst_fd_sink_render (GstBaseSink * sink,
    GstBuffer * buffer);
static gboolean gst_fd_sink_start (GstBaseSink * basesink);
static gboolean gst_fd_sink_stop (GstBaseSink * basesink);
static gboolean gst_fd_sink_unlock (GstBaseSink * basesink);
115
static gboolean gst_fd_sink_unlock_stop (GstBaseSink * basesink);
116 117 118
static gboolean gst_fd_sink_event (GstBaseSink * sink, GstEvent * event);

static gboolean gst_fd_sink_do_seek (GstFdSink * fdsink, guint64 new_offset);
Erik Walthinsen's avatar
Erik Walthinsen committed
119 120

static void
Philippe Rouquier's avatar
Philippe Rouquier committed
121
gst_fd_sink_class_init (GstFdSinkClass * klass)
Wim Taymans's avatar
Wim Taymans committed
122
{
123
  GObjectClass *gobject_class;
124
  GstElementClass *gstelement_class;
Philippe Rouquier's avatar
Philippe Rouquier committed
125
  GstBaseSinkClass *gstbasesink_class;
Erik Walthinsen's avatar
Erik Walthinsen committed
126

127
  gobject_class = G_OBJECT_CLASS (klass);
128
  gstelement_class = GST_ELEMENT_CLASS (klass);
Philippe Rouquier's avatar
Philippe Rouquier committed
129
  gstbasesink_class = GST_BASE_SINK_CLASS (klass);
Erik Walthinsen's avatar
Erik Walthinsen committed
130

Philippe Rouquier's avatar
Philippe Rouquier committed
131
  gobject_class->set_property = gst_fd_sink_set_property;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
132
  gobject_class->get_property = gst_fd_sink_get_property;
Philippe Rouquier's avatar
Philippe Rouquier committed
133 134
  gobject_class->dispose = gst_fd_sink_dispose;

135 136 137 138 139 140 141
  gst_element_class_set_details_simple (gstelement_class,
      "Filedescriptor Sink",
      "Sink/File",
      "Write data to a file descriptor", "Erik Walthinsen <omega@cse.ogi.edu>");
  gst_element_class_add_pad_template (gstelement_class,
      gst_static_pad_template_get (&sinktemplate));

Philippe Rouquier's avatar
Philippe Rouquier committed
142 143 144 145
  gstbasesink_class->render = GST_DEBUG_FUNCPTR (gst_fd_sink_render);
  gstbasesink_class->start = GST_DEBUG_FUNCPTR (gst_fd_sink_start);
  gstbasesink_class->stop = GST_DEBUG_FUNCPTR (gst_fd_sink_stop);
  gstbasesink_class->unlock = GST_DEBUG_FUNCPTR (gst_fd_sink_unlock);
146
  gstbasesink_class->unlock_stop = GST_DEBUG_FUNCPTR (gst_fd_sink_unlock_stop);
147
  gstbasesink_class->event = GST_DEBUG_FUNCPTR (gst_fd_sink_event);
148
  gstbasesink_class->query = GST_DEBUG_FUNCPTR (gst_fd_sink_query);
Erik Walthinsen's avatar
Erik Walthinsen committed
149

150
  g_object_class_install_property (gobject_class, ARG_FD,
151
      g_param_spec_int ("fd", "fd", "An open file descriptor to write to",
152
          0, G_MAXINT, 1, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
Erik Walthinsen's avatar
Erik Walthinsen committed
153 154
}

155
static void
156
gst_fd_sink_init (GstFdSink * fdsink)
Wim Taymans's avatar
Wim Taymans committed
157
{
Erik Walthinsen's avatar
Erik Walthinsen committed
158
  fdsink->fd = 1;
Philippe Rouquier's avatar
Philippe Rouquier committed
159 160
  fdsink->uri = g_strdup_printf ("fd://%d", fdsink->fd);
  fdsink->bytes_written = 0;
161
  fdsink->current_pos = 0;
Philippe Rouquier's avatar
Philippe Rouquier committed
162

163
  gst_base_sink_set_sync (GST_BASE_SINK (fdsink), FALSE);
Erik Walthinsen's avatar
Erik Walthinsen committed
164 165
}

166
static void
Philippe Rouquier's avatar
Philippe Rouquier committed
167 168 169 170 171 172 173 174 175 176 177
gst_fd_sink_dispose (GObject * obj)
{
  GstFdSink *fdsink = GST_FD_SINK (obj);

  g_free (fdsink->uri);
  fdsink->uri = NULL;

  G_OBJECT_CLASS (parent_class)->dispose (obj);
}

static gboolean
178
gst_fd_sink_query (GstBaseSink * bsink, GstQuery * query)
Philippe Rouquier's avatar
Philippe Rouquier committed
179
{
180
  gboolean res = FALSE;
Philippe Rouquier's avatar
Philippe Rouquier committed
181 182
  GstFdSink *fdsink;

183
  fdsink = GST_FD_SINK (bsink);
Philippe Rouquier's avatar
Philippe Rouquier committed
184

185
  switch (GST_QUERY_TYPE (query)) {
Philippe Rouquier's avatar
Philippe Rouquier committed
186
    case GST_QUERY_POSITION:
187 188 189
    {
      GstFormat format;

190
      gst_query_parse_position (query, &format, NULL);
191

Philippe Rouquier's avatar
Philippe Rouquier committed
192 193 194
      switch (format) {
        case GST_FORMAT_DEFAULT:
        case GST_FORMAT_BYTES:
195
          gst_query_set_position (query, GST_FORMAT_BYTES, fdsink->current_pos);
196 197
          res = TRUE;
          break;
Philippe Rouquier's avatar
Philippe Rouquier committed
198
        default:
199
          break;
Philippe Rouquier's avatar
Philippe Rouquier committed
200
      }
201 202
      break;
    }
Philippe Rouquier's avatar
Philippe Rouquier committed
203
    case GST_QUERY_FORMATS:
204
      gst_query_set_formats (query, 2, GST_FORMAT_DEFAULT, GST_FORMAT_BYTES);
205 206
      res = TRUE;
      break;
207
    case GST_QUERY_URI:
208
      gst_query_set_uri (query, fdsink->uri);
209 210
      res = TRUE;
      break;
Philippe Rouquier's avatar
Philippe Rouquier committed
211
    default:
212 213 214
      res = GST_BASE_SINK_CLASS (parent_class)->query (bsink, query);
      break;

Philippe Rouquier's avatar
Philippe Rouquier committed
215
  }
216
  return res;
Philippe Rouquier's avatar
Philippe Rouquier committed
217 218 219 220 221 222
}

static GstFlowReturn
gst_fd_sink_render (GstBaseSink * sink, GstBuffer * buffer)
{
  GstFdSink *fdsink;
Wim Taymans's avatar
Wim Taymans committed
223 224 225
  GstMapInfo info;
  guint8 *ptr;
  gsize left;
226
  gint written;
Philippe Rouquier's avatar
Philippe Rouquier committed
227 228 229 230 231 232 233 234 235

#ifndef HAVE_WIN32
  gint retval;
#endif

  fdsink = GST_FD_SINK (sink);

  g_return_val_if_fail (fdsink->fd >= 0, GST_FLOW_ERROR);

Wim Taymans's avatar
Wim Taymans committed
236
  gst_buffer_map (buffer, &info, GST_MAP_READ);
237

Wim Taymans's avatar
Wim Taymans committed
238 239
  ptr = info.data;
  left = info.size;
240 241

again:
Philippe Rouquier's avatar
Philippe Rouquier committed
242 243
#ifndef HAVE_WIN32
  do {
Josep Torra's avatar
Josep Torra committed
244
    GST_DEBUG_OBJECT (fdsink, "going into select, have %" G_GSIZE_FORMAT
Wim Taymans's avatar
Wim Taymans committed
245
        " bytes to write", info.size);
246
    retval = gst_poll_wait (fdsink->fdset, GST_CLOCK_TIME_NONE);
247
  } while (retval == -1 && (errno == EINTR || errno == EAGAIN));
248 249 250 251 252 253 254

  if (retval == -1) {
    if (errno == EBUSY)
      goto stopped;
    else
      goto select_error;
  }
Philippe Rouquier's avatar
Philippe Rouquier committed
255 256
#endif

Josep Torra's avatar
Josep Torra committed
257
  GST_DEBUG_OBJECT (fdsink, "writing %" G_GSIZE_FORMAT " bytes to"
Wim Taymans's avatar
Wim Taymans committed
258
      " file descriptor %d", info.size, fdsink->fd);
259

260
  written = write (fdsink->fd, ptr, left);
261 262 263 264 265 266 267 268 269

  /* check for errors */
  if (G_UNLIKELY (written < 0)) {
    /* try to write again on non-fatal errors */
    if (errno == EAGAIN || errno == EINTR)
      goto again;

    /* else go to our error handler */
    goto write_error;
Philippe Rouquier's avatar
Philippe Rouquier committed
270 271
  }

272
  /* all is fine when we get here */
273 274
  left -= written;
  ptr += written;
275
  fdsink->bytes_written += written;
276
  fdsink->current_pos += written;
277

278 279
  GST_DEBUG_OBJECT (fdsink, "wrote %d bytes, %" G_GSIZE_FORMAT " left", written,
      left);
280 281

  /* short write, select and try to write the remainder */
282
  if (G_UNLIKELY (left > 0))
283 284
    goto again;

Wim Taymans's avatar
Wim Taymans committed
285
  gst_buffer_unmap (buffer, &info);
286

Philippe Rouquier's avatar
Philippe Rouquier committed
287 288 289 290 291 292 293 294
  return GST_FLOW_OK;

#ifndef HAVE_WIN32
select_error:
  {
    GST_ELEMENT_ERROR (fdsink, RESOURCE, READ, (NULL),
        ("select on file descriptor: %s.", g_strerror (errno)));
    GST_DEBUG_OBJECT (fdsink, "Error during select");
Wim Taymans's avatar
Wim Taymans committed
295
    gst_buffer_unmap (buffer, &info);
Philippe Rouquier's avatar
Philippe Rouquier committed
296 297 298 299 300
    return GST_FLOW_ERROR;
  }
stopped:
  {
    GST_DEBUG_OBJECT (fdsink, "Select stopped");
Wim Taymans's avatar
Wim Taymans committed
301
    gst_buffer_unmap (buffer, &info);
302
    return GST_FLOW_FLUSHING;
Philippe Rouquier's avatar
Philippe Rouquier committed
303 304
  }
#endif
305 306 307 308 309 310 311 312

write_error:
  {
    switch (errno) {
      case ENOSPC:
        GST_ELEMENT_ERROR (fdsink, RESOURCE, NO_SPACE_LEFT, (NULL), (NULL));
        break;
      default:{
313 314 315
        GST_ELEMENT_ERROR (fdsink, RESOURCE, WRITE, (NULL),
            ("Error while writing to file descriptor %d: %s",
                fdsink->fd, g_strerror (errno)));
316 317
      }
    }
Wim Taymans's avatar
Wim Taymans committed
318
    gst_buffer_unmap (buffer, &info);
319 320
    return GST_FLOW_ERROR;
  }
Philippe Rouquier's avatar
Philippe Rouquier committed
321 322 323
}

static gboolean
324
gst_fd_sink_check_fd (GstFdSink * fdsink, int fd, GError ** error)
Philippe Rouquier's avatar
Philippe Rouquier committed
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
{
  struct stat stat_results;
  off_t result;

  /* see that it is a valid file descriptor */
  if (fstat (fd, &stat_results) < 0)
    goto invalid;

  if (!S_ISREG (stat_results.st_mode))
    goto not_seekable;

  /* see if it is a seekable stream */
  result = lseek (fd, 0, SEEK_CUR);
  if (result == -1) {
    switch (errno) {
      case EINVAL:
      case EBADF:
        goto invalid;

      case ESPIPE:
        goto not_seekable;
    }
  } else
348
    GST_DEBUG_OBJECT (fdsink, "File descriptor %d is seekable", fd);
Philippe Rouquier's avatar
Philippe Rouquier committed
349 350 351 352

  return TRUE;

invalid:
353
  {
354 355
    GST_ELEMENT_ERROR (fdsink, RESOURCE, WRITE, (NULL),
        ("File descriptor %d is not valid: %s", fd, g_strerror (errno)));
356 357
    g_set_error (error, GST_URI_ERROR, GST_URI_ERROR_BAD_REFERENCE,
        "File descriptor %d is not valid: %s", fd, g_strerror (errno));
358 359
    return FALSE;
  }
Philippe Rouquier's avatar
Philippe Rouquier committed
360
not_seekable:
361
  {
362
    GST_DEBUG_OBJECT (fdsink, "File descriptor %d is a pipe", fd);
363 364
    return TRUE;
  }
Philippe Rouquier's avatar
Philippe Rouquier committed
365 366 367 368
}

static gboolean
gst_fd_sink_start (GstBaseSink * basesink)
Wim Taymans's avatar
Wim Taymans committed
369
{
Erik Walthinsen's avatar
Erik Walthinsen committed
370
  GstFdSink *fdsink;
371
  GstPollFD fd = GST_POLL_FD_INIT;
Philippe Rouquier's avatar
Philippe Rouquier committed
372 373

  fdsink = GST_FD_SINK (basesink);
374
  if (!gst_fd_sink_check_fd (fdsink, fdsink->fd, NULL))
Philippe Rouquier's avatar
Philippe Rouquier committed
375 376
    return FALSE;

377
  if ((fdsink->fdset = gst_poll_new (TRUE)) == NULL)
Philippe Rouquier's avatar
Philippe Rouquier committed
378 379
    goto socket_pair;

380 381 382
  fd.fd = fdsink->fd;
  gst_poll_add_fd (fdsink->fdset, &fd);
  gst_poll_fd_ctl_write (fdsink->fdset, &fd, TRUE);
Philippe Rouquier's avatar
Philippe Rouquier committed
383

384 385 386
  fdsink->bytes_written = 0;
  fdsink->current_pos = 0;

Philippe Rouquier's avatar
Philippe Rouquier committed
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
  return TRUE;

  /* ERRORS */
socket_pair:
  {
    GST_ELEMENT_ERROR (fdsink, RESOURCE, OPEN_READ_WRITE, (NULL),
        GST_ERROR_SYSTEM);
    return FALSE;
  }
}

static gboolean
gst_fd_sink_stop (GstBaseSink * basesink)
{
  GstFdSink *fdsink = GST_FD_SINK (basesink);

403 404 405 406
  if (fdsink->fdset) {
    gst_poll_free (fdsink->fdset);
    fdsink->fdset = NULL;
  }
Philippe Rouquier's avatar
Philippe Rouquier committed
407 408 409 410 411 412 413 414

  return TRUE;
}

static gboolean
gst_fd_sink_unlock (GstBaseSink * basesink)
{
  GstFdSink *fdsink = GST_FD_SINK (basesink);
Erik Walthinsen's avatar
Erik Walthinsen committed
415

416 417 418 419
  GST_LOG_OBJECT (fdsink, "Flushing");
  GST_OBJECT_LOCK (fdsink);
  gst_poll_set_flushing (fdsink->fdset, TRUE);
  GST_OBJECT_UNLOCK (fdsink);
Wim Taymans's avatar
Wim Taymans committed
420

Philippe Rouquier's avatar
Philippe Rouquier committed
421 422 423
  return TRUE;
}

424 425 426 427 428
static gboolean
gst_fd_sink_unlock_stop (GstBaseSink * basesink)
{
  GstFdSink *fdsink = GST_FD_SINK (basesink);

429 430 431 432
  GST_LOG_OBJECT (fdsink, "No longer flushing");
  GST_OBJECT_LOCK (fdsink);
  gst_poll_set_flushing (fdsink->fdset, FALSE);
  GST_OBJECT_UNLOCK (fdsink);
433 434 435 436

  return TRUE;
}

Philippe Rouquier's avatar
Philippe Rouquier committed
437
static gboolean
438
gst_fd_sink_update_fd (GstFdSink * fdsink, int new_fd, GError ** error)
Philippe Rouquier's avatar
Philippe Rouquier committed
439
{
440 441 442
  if (new_fd < 0) {
    g_set_error (error, GST_URI_ERROR, GST_URI_ERROR_BAD_REFERENCE,
        "File descriptor %d is not valid", new_fd);
Philippe Rouquier's avatar
Philippe Rouquier committed
443
    return FALSE;
444
  }
445

446
  if (!gst_fd_sink_check_fd (fdsink, new_fd, error))
447
    goto invalid;
448

449 450
  /* assign the fd */
  GST_OBJECT_LOCK (fdsink);
451
  if (fdsink->fdset) {
Wim Taymans's avatar
Wim Taymans committed
452
    GstPollFD fd = GST_POLL_FD_INIT;
453 454 455 456 457 458 459 460

    fd.fd = fdsink->fd;
    gst_poll_remove_fd (fdsink->fdset, &fd);

    fd.fd = new_fd;
    gst_poll_add_fd (fdsink->fdset, &fd);
    gst_poll_fd_ctl_write (fdsink->fdset, &fd, TRUE);
  }
Philippe Rouquier's avatar
Philippe Rouquier committed
461 462 463
  fdsink->fd = new_fd;
  g_free (fdsink->uri);
  fdsink->uri = g_strdup_printf ("fd://%d", fdsink->fd);
464

Philippe Rouquier's avatar
Philippe Rouquier committed
465
  GST_OBJECT_UNLOCK (fdsink);
466

Philippe Rouquier's avatar
Philippe Rouquier committed
467
  return TRUE;
468 469 470 471 472

invalid:
  {
    return FALSE;
  }
Erik Walthinsen's avatar
Erik Walthinsen committed
473 474
}

475
static void
Philippe Rouquier's avatar
Philippe Rouquier committed
476
gst_fd_sink_set_property (GObject * object, guint prop_id,
477
    const GValue * value, GParamSpec * pspec)
Wim Taymans's avatar
Wim Taymans committed
478
{
Erik Walthinsen's avatar
Erik Walthinsen committed
479
  GstFdSink *fdsink;
480

481
  fdsink = GST_FD_SINK (object);
Erik Walthinsen's avatar
Erik Walthinsen committed
482

483
  switch (prop_id) {
Philippe Rouquier's avatar
Philippe Rouquier committed
484 485 486 487
    case ARG_FD:{
      int fd;

      fd = g_value_get_int (value);
488
      gst_fd_sink_update_fd (fdsink, fd, NULL);
Erik Walthinsen's avatar
Erik Walthinsen committed
489
      break;
Philippe Rouquier's avatar
Philippe Rouquier committed
490
    }
Erik Walthinsen's avatar
Erik Walthinsen committed
491
    default:
492
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
Erik Walthinsen's avatar
Erik Walthinsen committed
493 494 495 496
      break;
  }
}

497
static void
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
498
gst_fd_sink_get_property (GObject * object, guint prop_id, GValue * value,
499
    GParamSpec * pspec)
Wim Taymans's avatar
Wim Taymans committed
500
{
Erik Walthinsen's avatar
Erik Walthinsen committed
501
  GstFdSink *fdsink;
502

503
  fdsink = GST_FD_SINK (object);
Erik Walthinsen's avatar
Erik Walthinsen committed
504

505
  switch (prop_id) {
Erik Walthinsen's avatar
Erik Walthinsen committed
506
    case ARG_FD:
507
      g_value_set_int (value, fdsink->fd);
Erik Walthinsen's avatar
Erik Walthinsen committed
508 509
      break;
    default:
510
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
Erik Walthinsen's avatar
Erik Walthinsen committed
511 512 513
      break;
  }
}
Philippe Rouquier's avatar
Philippe Rouquier committed
514

515 516 517 518 519 520 521 522 523 524 525 526
static gboolean
gst_fd_sink_do_seek (GstFdSink * fdsink, guint64 new_offset)
{
  off_t result;

  result = lseek (fdsink->fd, new_offset, SEEK_SET);

  if (result == -1)
    goto seek_failed;

  fdsink->current_pos = new_offset;

Zhang Wanming's avatar
Zhang Wanming committed
527
  GST_DEBUG_OBJECT (fdsink, "File descriptor %d to seek to position "
528
      "%" G_GUINT64_FORMAT, fdsink->fd, fdsink->current_pos);
529 530 531 532 533 534

  return TRUE;

  /* ERRORS */
seek_failed:
  {
Zhang Wanming's avatar
Zhang Wanming committed
535
    GST_DEBUG_OBJECT (fdsink, "File descriptor %d failed to seek to position "
536
        "%" G_GUINT64_FORMAT, fdsink->fd, new_offset);
537 538 539 540 541 542 543 544 545 546 547 548 549 550 551
    return FALSE;
  }
}

static gboolean
gst_fd_sink_event (GstBaseSink * sink, GstEvent * event)
{
  GstEventType type;
  GstFdSink *fdsink;

  fdsink = GST_FD_SINK (sink);

  type = GST_EVENT_TYPE (event);

  switch (type) {
Wim Taymans's avatar
Wim Taymans committed
552
    case GST_EVENT_SEGMENT:
553
    {
554
      const GstSegment *segment;
555

Wim Taymans's avatar
Wim Taymans committed
556 557
      gst_event_parse_segment (event, &segment);

558
      if (segment->format == GST_FORMAT_BYTES) {
559 560
        /* only try to seek and fail when we are going to a different
         * position */
561
        if (fdsink->current_pos != segment->start) {
562 563 564 565
          /* FIXME, the seek should be performed on the pos field, start/stop are
           * just boundaries for valid bytes offsets. We should also fill the file
           * with zeroes if the new position extends the current EOF (sparse streams
           * and segment accumulation). */
566
          if (!gst_fd_sink_do_seek (fdsink, (guint64) segment->start))
567 568 569 570
            goto seek_failed;
        }
      } else {
        GST_DEBUG_OBJECT (fdsink,
571 572
            "Ignored SEGMENT event of format %u (%s)", (guint) segment->format,
            gst_format_get_name (segment->format));
573 574 575 576 577 578 579
      }
      break;
    }
    default:
      break;
  }

580
  return GST_BASE_SINK_CLASS (parent_class)->event (sink, event);
581 582 583

seek_failed:
  {
584 585 586
    GST_ELEMENT_ERROR (fdsink, RESOURCE, SEEK, (NULL),
        ("Error while seeking on file descriptor %d: %s",
            fdsink->fd, g_strerror (errno)));
587
    gst_event_unref (event);
588 589 590 591 592
    return FALSE;
  }

}

Philippe Rouquier's avatar
Philippe Rouquier committed
593 594
/*** GSTURIHANDLER INTERFACE *************************************************/

595
static GstURIType
Wim Taymans's avatar
Wim Taymans committed
596
gst_fd_sink_uri_get_type (GType type)
Philippe Rouquier's avatar
Philippe Rouquier committed
597 598 599
{
  return GST_URI_SINK;
}
600

601
static const gchar *const *
Wim Taymans's avatar
Wim Taymans committed
602
gst_fd_sink_uri_get_protocols (GType type)
Philippe Rouquier's avatar
Philippe Rouquier committed
603
{
604
  static const gchar *protocols[] = { "fd", NULL };
Philippe Rouquier's avatar
Philippe Rouquier committed
605 606 607

  return protocols;
}
608

609
static gchar *
Philippe Rouquier's avatar
Philippe Rouquier committed
610 611 612 613
gst_fd_sink_uri_get_uri (GstURIHandler * handler)
{
  GstFdSink *sink = GST_FD_SINK (handler);

614 615
  /* FIXME: make thread-safe */
  return g_strdup (sink->uri);
Philippe Rouquier's avatar
Philippe Rouquier committed
616 617 618
}

static gboolean
619 620
gst_fd_sink_uri_set_uri (GstURIHandler * handler, const gchar * uri,
    GError ** error)
Philippe Rouquier's avatar
Philippe Rouquier committed
621 622 623 624
{
  GstFdSink *sink = GST_FD_SINK (handler);
  gint fd;

625 626 627
  if (sscanf (uri, "fd://%d", &fd) != 1) {
    g_set_error (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
        "File descriptor URI could not be parsed");
Philippe Rouquier's avatar
Philippe Rouquier committed
628 629 630
    return FALSE;
  }

631
  return gst_fd_sink_update_fd (sink, fd, error);
Philippe Rouquier's avatar
Philippe Rouquier committed
632 633 634 635 636 637 638 639 640 641 642 643
}

static void
gst_fd_sink_uri_handler_init (gpointer g_iface, gpointer iface_data)
{
  GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;

  iface->get_type = gst_fd_sink_uri_get_type;
  iface->get_protocols = gst_fd_sink_uri_get_protocols;
  iface->get_uri = gst_fd_sink_uri_get_uri;
  iface->set_uri = gst_fd_sink_uri_set_uri;
}