Blame gst/tcp/gstmultifdsink.c

Packit 0652a1
/* GStreamer
Packit 0652a1
 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
Packit 0652a1
 * Copyright (C) <2004> Thomas Vander Stichele <thomas at apestaart dot org>
Packit 0652a1
 * Copyright (C) 2006 Wim Taymans <wim at fluendo dot com>
Packit 0652a1
 *
Packit 0652a1
 * This library is free software; you can redistribute it and/or
Packit 0652a1
 * modify it under the terms of the GNU Library General Public
Packit 0652a1
 * License as published by the Free Software Foundation; either
Packit 0652a1
 * version 2 of the License, or (at your option) any later version.
Packit 0652a1
 *
Packit 0652a1
 * This library is distributed in the hope that it will be useful,
Packit 0652a1
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 0652a1
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 0652a1
 * Library General Public License for more details.
Packit 0652a1
 *
Packit 0652a1
 * You should have received a copy of the GNU Library General Public
Packit 0652a1
 * License along with this library; if not, write to the
Packit 0652a1
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
Packit 0652a1
 * Boston, MA 02110-1301, USA.
Packit 0652a1
 */
Packit 0652a1
Packit 0652a1
/**
Packit 0652a1
 * SECTION:element-multifdsink
Packit 0652a1
 * @title: multifdsink
Packit 0652a1
 * @see_also: tcpserversink
Packit 0652a1
 *
Packit 0652a1
 * This plugin writes incoming data to a set of file descriptors. The
Packit 0652a1
 * file descriptors can be added to multifdsink by emitting the #GstMultiFdSink::add signal.
Packit 0652a1
 * For each descriptor added, the #GstMultiFdSink::client-added signal will be called.
Packit 0652a1
 *
Packit 0652a1
 * The multifdsink element needs to be set into READY, PAUSED or PLAYING state
Packit 0652a1
 * before operations such as adding clients are possible.
Packit 0652a1
 *
Packit 0652a1
 * A client can also be added with the #GstMultiFdSink::add-full signal
Packit 0652a1
 * that allows for more control over what and how much data a client
Packit 0652a1
 * initially receives.
Packit 0652a1
 *
Packit 0652a1
 * Clients can be removed from multifdsink by emitting the #GstMultiFdSink::remove signal. For
Packit 0652a1
 * each descriptor removed, the #GstMultiFdSink::client-removed signal will be called. The
Packit 0652a1
 * #GstMultiFdSink::client-removed signal can also be fired when multifdsink decides that a
Packit 0652a1
 * client is not active anymore or, depending on the value of the
Packit 0652a1
 * #GstMultiFdSink:recover-policy property, if the client is reading too slowly.
Packit 0652a1
 * In all cases, multifdsink will never close a file descriptor itself.
Packit 0652a1
 * The user of multifdsink is responsible for closing all file descriptors.
Packit 0652a1
 * This can for example be done in response to the #GstMultiFdSink::client-fd-removed signal.
Packit 0652a1
 * Note that multifdsink still has a reference to the file descriptor when the
Packit 0652a1
 * #GstMultiFdSink::client-removed signal is emitted, so that "get-stats" can be performed on
Packit 0652a1
 * the descriptor; it is therefore not safe to close the file descriptor in
Packit 0652a1
 * the #GstMultiFdSink::client-removed signal handler, and you should use the
Packit 0652a1
 * #GstMultiFdSink::client-fd-removed signal to safely close the fd.
Packit 0652a1
 *
Packit 0652a1
 * Multifdsink internally keeps a queue of the incoming buffers and uses a
Packit 0652a1
 * separate thread to send the buffers to the clients. This ensures that no
Packit 0652a1
 * client write can block the pipeline and that clients can read with different
Packit 0652a1
 * speeds.
Packit 0652a1
 *
Packit 0652a1
 * When adding a client to multifdsink, the #GstMultiFdSink:sync-method property will define
Packit 0652a1
 * which buffer in the queued buffers will be sent first to the client. Clients
Packit 0652a1
 * can be sent the most recent buffer (which might not be decodable by the
Packit 0652a1
 * client if it is not a keyframe), the next keyframe received in
Packit 0652a1
 * multifdsink (which can take some time depending on the keyframe rate), or the
Packit 0652a1
 * last received keyframe (which will cause a simple burst-on-connect).
Packit 0652a1
 * Multifdsink will always keep at least one keyframe in its internal buffers
Packit 0652a1
 * when the sync-mode is set to latest-keyframe.
Packit 0652a1
 *
Packit 0652a1
 * There are additional values for the #GstMultiFdSink:sync-method
Packit 0652a1
 * property to allow finer control over burst-on-connect behaviour. By selecting
Packit 0652a1
 * the 'burst' method a minimum burst size can be chosen, 'burst-keyframe'
Packit 0652a1
 * additionally requires that the burst begin with a keyframe, and
Packit 0652a1
 * 'burst-with-keyframe' attempts to burst beginning with a keyframe, but will
Packit 0652a1
 * prefer a minimum burst size even if it requires not starting with a keyframe.
Packit 0652a1
 *
Packit 0652a1
 * Multifdsink can be instructed to keep at least a minimum amount of data
Packit 0652a1
 * expressed in time or byte units in its internal queues with the
Packit 0652a1
 * #GstMultiFdSink:time-min and #GstMultiFdSink:bytes-min properties respectively.
Packit 0652a1
 * These properties are useful if the application adds clients with the
Packit 0652a1
 * #GstMultiFdSink::add-full signal to make sure that a burst connect can
Packit 0652a1
 * actually be honored.
Packit 0652a1
 *
Packit 0652a1
 * When streaming data, clients are allowed to read at a different rate than
Packit 0652a1
 * the rate at which multifdsink receives data. If the client is reading too
Packit 0652a1
 * fast, no data will be send to the client until multifdsink receives more
Packit 0652a1
 * data. If the client, however, reads too slowly, data for that client will be
Packit 0652a1
 * queued up in multifdsink. Two properties control the amount of data
Packit 0652a1
 * (buffers) that is queued in multifdsink: #GstMultiFdSink:buffers-max and
Packit 0652a1
 * #GstMultiFdSink:buffers-soft-max. A client that falls behind by
Packit 0652a1
 * #GstMultiFdSink:buffers-max is removed from multifdsink forcibly.
Packit 0652a1
 *
Packit 0652a1
 * A client with a lag of at least #GstMultiFdSink:buffers-soft-max enters the recovery
Packit 0652a1
 * procedure which is controlled with the #GstMultiFdSink:recover-policy property.
Packit 0652a1
 * A recover policy of NONE will do nothing, RESYNC_LATEST will send the most recently
Packit 0652a1
 * received buffer as the next buffer for the client, RESYNC_SOFT_LIMIT
Packit 0652a1
 * positions the client to the soft limit in the buffer queue and
Packit 0652a1
 * RESYNC_KEYFRAME positions the client at the most recent keyframe in the
Packit 0652a1
 * buffer queue.
Packit 0652a1
 *
Packit 0652a1
 * multifdsink will by default synchronize on the clock before serving the
Packit 0652a1
 * buffers to the clients. This behaviour can be disabled by setting the sync
Packit 0652a1
 * property to FALSE. Multifdsink will by default not do QoS and will never
Packit 0652a1
 * drop late buffers.
Packit 0652a1
 */
Packit 0652a1
Packit 0652a1
#ifdef HAVE_CONFIG_H
Packit 0652a1
#include "config.h"
Packit 0652a1
#endif
Packit 0652a1
Packit 0652a1
#include <gst/gst-i18n-plugin.h>
Packit 0652a1
Packit 0652a1
#include <sys/ioctl.h>
Packit 0652a1
Packit 0652a1
#ifdef HAVE_UNISTD_H
Packit 0652a1
#include <unistd.h>
Packit 0652a1
#endif
Packit 0652a1
Packit 0652a1
#include <string.h>
Packit 0652a1
#include <fcntl.h>
Packit 0652a1
#include <sys/types.h>
Packit 0652a1
#include <sys/socket.h>
Packit 0652a1
#include <sys/stat.h>
Packit 0652a1
#include <netinet/in.h>
Packit 0652a1
Packit 0652a1
#ifdef HAVE_FIONREAD_IN_SYS_FILIO
Packit 0652a1
#include <sys/filio.h>
Packit 0652a1
#endif
Packit 0652a1
Packit 0652a1
#include "gstmultifdsink.h"
Packit 0652a1
Packit 0652a1
#define NOT_IMPLEMENTED 0
Packit 0652a1
Packit 0652a1
GST_DEBUG_CATEGORY_STATIC (multifdsink_debug);
Packit 0652a1
#define GST_CAT_DEFAULT (multifdsink_debug)
Packit 0652a1
Packit 0652a1
/* MultiFdSink signals and args */
Packit 0652a1
enum
Packit 0652a1
{
Packit 0652a1
  /* methods */
Packit 0652a1
  SIGNAL_ADD,
Packit 0652a1
  SIGNAL_ADD_BURST,
Packit 0652a1
  SIGNAL_REMOVE,
Packit 0652a1
  SIGNAL_REMOVE_FLUSH,
Packit 0652a1
  SIGNAL_GET_STATS,
Packit 0652a1
Packit 0652a1
  /* signals */
Packit 0652a1
  SIGNAL_CLIENT_ADDED,
Packit 0652a1
  SIGNAL_CLIENT_REMOVED,
Packit 0652a1
  SIGNAL_CLIENT_FD_REMOVED,
Packit 0652a1
Packit 0652a1
  LAST_SIGNAL
Packit 0652a1
};
Packit 0652a1
Packit 0652a1
/* this is really arbitrarily chosen */
Packit 0652a1
#define DEFAULT_HANDLE_READ             TRUE
Packit 0652a1
Packit 0652a1
enum
Packit 0652a1
{
Packit 0652a1
  PROP_0,
Packit 0652a1
  PROP_HANDLE_READ
Packit 0652a1
};
Packit 0652a1
Packit 0652a1
static void gst_multi_fd_sink_stop_pre (GstMultiHandleSink * mhsink);
Packit 0652a1
static void gst_multi_fd_sink_stop_post (GstMultiHandleSink * mhsink);
Packit 0652a1
static gboolean gst_multi_fd_sink_start_pre (GstMultiHandleSink * mhsink);
Packit 0652a1
static gpointer gst_multi_fd_sink_thread (GstMultiHandleSink * mhsink);
Packit 0652a1
Packit 0652a1
static void gst_multi_fd_sink_add (GstMultiFdSink * sink, int fd);
Packit 0652a1
static void gst_multi_fd_sink_add_full (GstMultiFdSink * sink, int fd,
Packit 0652a1
    GstSyncMethod sync, GstFormat min_format, guint64 min_value,
Packit 0652a1
    GstFormat max_format, guint64 max_value);
Packit 0652a1
static void gst_multi_fd_sink_remove (GstMultiFdSink * sink, int fd);
Packit 0652a1
static void gst_multi_fd_sink_remove_flush (GstMultiFdSink * sink, int fd);
Packit 0652a1
static GstStructure *gst_multi_fd_sink_get_stats (GstMultiFdSink * sink,
Packit 0652a1
    int fd);
Packit 0652a1
Packit 0652a1
static void gst_multi_fd_sink_emit_client_added (GstMultiHandleSink * mhsink,
Packit 0652a1
    GstMultiSinkHandle handle);
Packit 0652a1
static void gst_multi_fd_sink_emit_client_removed (GstMultiHandleSink * mhsink,
Packit 0652a1
    GstMultiSinkHandle handle, GstClientStatus status);
Packit 0652a1
Packit 0652a1
static GstMultiHandleClient *gst_multi_fd_sink_new_client (GstMultiHandleSink *
Packit 0652a1
    mhsink, GstMultiSinkHandle handle, GstSyncMethod sync_method);
Packit 0652a1
static void gst_multi_fd_sink_client_free (GstMultiHandleSink * m,
Packit 0652a1
    GstMultiHandleClient * client);
Packit 0652a1
static int gst_multi_fd_sink_client_get_fd (GstMultiHandleClient * client);
Packit 0652a1
static void gst_multi_fd_sink_handle_debug (GstMultiSinkHandle handle,
Packit 0652a1
    gchar debug[30]);
Packit 0652a1
static gpointer gst_multi_fd_sink_handle_hash_key (GstMultiSinkHandle handle);
Packit 0652a1
static void gst_multi_fd_sink_hash_adding (GstMultiHandleSink * mhsink,
Packit 0652a1
    GstMultiHandleClient * mhclient);
Packit 0652a1
static void gst_multi_fd_sink_hash_removing (GstMultiHandleSink * mhsink,
Packit 0652a1
    GstMultiHandleClient * mhclient);
Packit 0652a1
Packit 0652a1
static void gst_multi_fd_sink_hash_changed (GstMultiHandleSink * mhsink);
Packit 0652a1
Packit 0652a1
static void gst_multi_fd_sink_set_property (GObject * object, guint prop_id,
Packit 0652a1
    const GValue * value, GParamSpec * pspec);
Packit 0652a1
static void gst_multi_fd_sink_get_property (GObject * object, guint prop_id,
Packit 0652a1
    GValue * value, GParamSpec * pspec);
Packit 0652a1
Packit 0652a1
#define gst_multi_fd_sink_parent_class parent_class
Packit 0652a1
G_DEFINE_TYPE (GstMultiFdSink, gst_multi_fd_sink, GST_TYPE_MULTI_HANDLE_SINK);
Packit 0652a1
Packit 0652a1
static guint gst_multi_fd_sink_signals[LAST_SIGNAL] = { 0 };
Packit 0652a1
Packit 0652a1
static void
Packit 0652a1
gst_multi_fd_sink_class_init (GstMultiFdSinkClass * klass)
Packit 0652a1
{
Packit 0652a1
  GObjectClass *gobject_class;
Packit 0652a1
  GstElementClass *gstelement_class;
Packit 0652a1
  GstMultiHandleSinkClass *gstmultihandlesink_class;
Packit 0652a1
Packit 0652a1
  gobject_class = (GObjectClass *) klass;
Packit 0652a1
  gstelement_class = (GstElementClass *) klass;
Packit 0652a1
  gstmultihandlesink_class = (GstMultiHandleSinkClass *) klass;
Packit 0652a1
Packit 0652a1
  gobject_class->set_property = gst_multi_fd_sink_set_property;
Packit 0652a1
  gobject_class->get_property = gst_multi_fd_sink_get_property;
Packit 0652a1
Packit 0652a1
  /**
Packit 0652a1
   * GstMultiFdSink::handle-read
Packit 0652a1
   *
Packit 0652a1
   * Handle read requests from clients and discard the data.
Packit 0652a1
   */
Packit 0652a1
  g_object_class_install_property (gobject_class, PROP_HANDLE_READ,
Packit 0652a1
      g_param_spec_boolean ("handle-read", "Handle Read",
Packit 0652a1
          "Handle client reads and discard the data",
Packit 0652a1
          DEFAULT_HANDLE_READ, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
Packit 0652a1
Packit 0652a1
  /**
Packit 0652a1
   * GstMultiFdSink::add:
Packit 0652a1
   * @gstmultifdsink: the multifdsink element to emit this signal on
Packit 0652a1
   * @fd:             the file descriptor to add to multifdsink
Packit 0652a1
   *
Packit 0652a1
   * Hand the given open file descriptor to multifdsink to write to.
Packit 0652a1
   */
Packit 0652a1
  gst_multi_fd_sink_signals[SIGNAL_ADD] =
Packit 0652a1
      g_signal_new ("add", G_TYPE_FROM_CLASS (klass),
Packit 0652a1
      G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
Packit 0652a1
      G_STRUCT_OFFSET (GstMultiFdSinkClass, add), NULL, NULL,
Packit 0652a1
      g_cclosure_marshal_generic, G_TYPE_NONE, 1, G_TYPE_INT);
Packit 0652a1
  /**
Packit 0652a1
   * GstMultiFdSink::add-full:
Packit 0652a1
   * @gstmultifdsink:  the multifdsink element to emit this signal on
Packit 0652a1
   * @fd:              the file descriptor to add to multifdsink
Packit 0652a1
   * @sync:            the sync method to use
Packit 0652a1
   * @format_min:      the format of @value_min
Packit 0652a1
   * @value_min:       the minimum amount of data to burst expressed in
Packit 0652a1
   *                   @format_min units.
Packit 0652a1
   * @format_max:      the format of @value_max
Packit 0652a1
   * @value_max:       the maximum amount of data to burst expressed in
Packit 0652a1
   *                   @format_max units.
Packit 0652a1
   *
Packit 0652a1
   * Hand the given open file descriptor to multifdsink to write to and
Packit 0652a1
   * specify the burst parameters for the new connection.
Packit 0652a1
   */
Packit 0652a1
  gst_multi_fd_sink_signals[SIGNAL_ADD_BURST] =
Packit 0652a1
      g_signal_new ("add-full", G_TYPE_FROM_CLASS (klass),
Packit 0652a1
      G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
Packit 0652a1
      G_STRUCT_OFFSET (GstMultiFdSinkClass, add_full), NULL, NULL,
Packit 0652a1
      g_cclosure_marshal_generic, G_TYPE_NONE, 6,
Packit 0652a1
      G_TYPE_INT, GST_TYPE_SYNC_METHOD, GST_TYPE_FORMAT, G_TYPE_UINT64,
Packit 0652a1
      GST_TYPE_FORMAT, G_TYPE_UINT64);
Packit 0652a1
  /**
Packit 0652a1
   * GstMultiFdSink::remove:
Packit 0652a1
   * @gstmultifdsink: the multifdsink element to emit this signal on
Packit 0652a1
   * @fd:             the file descriptor to remove from multifdsink
Packit 0652a1
   *
Packit 0652a1
   * Remove the given open file descriptor from multifdsink.
Packit 0652a1
   */
Packit 0652a1
  gst_multi_fd_sink_signals[SIGNAL_REMOVE] =
Packit 0652a1
      g_signal_new ("remove", G_TYPE_FROM_CLASS (klass),
Packit 0652a1
      G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
Packit 0652a1
      G_STRUCT_OFFSET (GstMultiFdSinkClass, remove), NULL, NULL,
Packit 0652a1
      g_cclosure_marshal_generic, G_TYPE_NONE, 1, G_TYPE_INT);
Packit 0652a1
  /**
Packit 0652a1
   * GstMultiFdSink::remove-flush:
Packit 0652a1
   * @gstmultifdsink: the multifdsink element to emit this signal on
Packit 0652a1
   * @fd:             the file descriptor to remove from multifdsink
Packit 0652a1
   *
Packit 0652a1
   * Remove the given open file descriptor from multifdsink after flushing all
Packit 0652a1
   * the pending data to the fd.
Packit 0652a1
   */
Packit 0652a1
  gst_multi_fd_sink_signals[SIGNAL_REMOVE_FLUSH] =
Packit 0652a1
      g_signal_new ("remove-flush", G_TYPE_FROM_CLASS (klass),
Packit 0652a1
      G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
Packit 0652a1
      G_STRUCT_OFFSET (GstMultiFdSinkClass, remove_flush), NULL, NULL,
Packit 0652a1
      g_cclosure_marshal_generic, G_TYPE_NONE, 1, G_TYPE_INT);
Packit 0652a1
Packit 0652a1
  /**
Packit 0652a1
   * GstMultiFdSink::get-stats:
Packit 0652a1
   * @gstmultifdsink: the multifdsink element to emit this signal on
Packit 0652a1
   * @fd:             the file descriptor to get stats of from multifdsink
Packit 0652a1
   *
Packit 0652a1
   * Get statistics about @fd. This function returns a #GstStructure to ease
Packit 0652a1
   * automatic wrapping for bindings.
Packit 0652a1
   *
Packit 0652a1
   * Returns: a #GstStructure with the statistics. The structures
Packit 0652a1
   *     contains guint64 values that represent respectively: total
Packit 0652a1
   *     number of bytes sent (bytes-sent), time when the client was
Packit 0652a1
   *     added (connect-time), time when the client was
Packit 0652a1
   *     disconnected/removed (disconnect-time), time the client
Packit 0652a1
   *     is/was active (connect-duration), last activity time (in
Packit 0652a1
   *     epoch seconds) (last-activity-time), number of buffers
Packit 0652a1
   *     dropped (buffers-dropped), the timestamp of the first buffer
Packit 0652a1
   *     (first-buffer-ts) and of the last buffer (last-buffer-ts).
Packit 0652a1
   *     All times are expressed in nanoseconds (GstClockTime).  The
Packit 0652a1
   *     structure can be empty if the client was not found.
Packit 0652a1
   */
Packit 0652a1
  gst_multi_fd_sink_signals[SIGNAL_GET_STATS] =
Packit 0652a1
      g_signal_new ("get-stats", G_TYPE_FROM_CLASS (klass),
Packit 0652a1
      G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
Packit 0652a1
      G_STRUCT_OFFSET (GstMultiFdSinkClass, get_stats), NULL, NULL,
Packit 0652a1
      g_cclosure_marshal_generic, GST_TYPE_STRUCTURE, 1, G_TYPE_INT);
Packit 0652a1
Packit 0652a1
  /**
Packit 0652a1
   * GstMultiFdSink::client-added:
Packit 0652a1
   * @gstmultifdsink: the multifdsink element that emitted this signal
Packit 0652a1
   * @fd:             the file descriptor that was added to multifdsink
Packit 0652a1
   *
Packit 0652a1
   * The given file descriptor was added to multifdsink. This signal will
Packit 0652a1
   * be emitted from the streaming thread so application should be prepared
Packit 0652a1
   * for that.
Packit 0652a1
   */
Packit 0652a1
  gst_multi_fd_sink_signals[SIGNAL_CLIENT_ADDED] =
Packit 0652a1
      g_signal_new ("client-added", G_TYPE_FROM_CLASS (klass),
Packit 0652a1
      G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE,
Packit 0652a1
      1, G_TYPE_INT);
Packit 0652a1
  /**
Packit 0652a1
   * GstMultiFdSink::client-removed:
Packit 0652a1
   * @gstmultifdsink: the multifdsink element that emitted this signal
Packit 0652a1
   * @fd:             the file descriptor that is to be removed from multifdsink
Packit 0652a1
   * @status:         the reason why the client was removed
Packit 0652a1
   *
Packit 0652a1
   * The given file descriptor is about to be removed from multifdsink. This
Packit 0652a1
   * signal will be emitted from the streaming thread so applications should
Packit 0652a1
   * be prepared for that.
Packit 0652a1
   *
Packit 0652a1
   * @gstmultifdsink still holds a handle to @fd so it is possible to call
Packit 0652a1
   * the get-stats signal from this callback. For the same reason it is
Packit 0652a1
   * not safe to close() and reuse @fd in this callback.
Packit 0652a1
   */
Packit 0652a1
  gst_multi_fd_sink_signals[SIGNAL_CLIENT_REMOVED] =
Packit 0652a1
      g_signal_new ("client-removed", G_TYPE_FROM_CLASS (klass),
Packit 0652a1
      G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_generic,
Packit 0652a1
      G_TYPE_NONE, 2, G_TYPE_INT, GST_TYPE_CLIENT_STATUS);
Packit 0652a1
  /**
Packit 0652a1
   * GstMultiFdSink::client-fd-removed:
Packit 0652a1
   * @gstmultifdsink: the multifdsink element that emitted this signal
Packit 0652a1
   * @fd:             the file descriptor that was removed from multifdsink
Packit 0652a1
   *
Packit 0652a1
   * The given file descriptor was removed from multifdsink. This signal will
Packit 0652a1
   * be emitted from the streaming thread so applications should be prepared
Packit 0652a1
   * for that.
Packit 0652a1
   *
Packit 0652a1
   * In this callback, @gstmultifdsink has removed all the information
Packit 0652a1
   * associated with @fd and it is therefore not possible to call get-stats
Packit 0652a1
   * with @fd. It is however safe to close() and reuse @fd in the callback.
Packit 0652a1
   */
Packit 0652a1
  gst_multi_fd_sink_signals[SIGNAL_CLIENT_FD_REMOVED] =
Packit 0652a1
      g_signal_new ("client-fd-removed", G_TYPE_FROM_CLASS (klass),
Packit 0652a1
      G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_generic,
Packit 0652a1
      G_TYPE_NONE, 1, G_TYPE_INT);
Packit 0652a1
Packit 0652a1
  gst_element_class_set_static_metadata (gstelement_class,
Packit 0652a1
      "Multi filedescriptor sink", "Sink/Network",
Packit 0652a1
      "Send data to multiple filedescriptors",
Packit 0652a1
      "Thomas Vander Stichele <thomas at apestaart dot org>, "
Packit 0652a1
      "Wim Taymans <wim@fluendo.com>");
Packit 0652a1
Packit 0652a1
  klass->add = GST_DEBUG_FUNCPTR (gst_multi_fd_sink_add);
Packit 0652a1
  klass->add_full = GST_DEBUG_FUNCPTR (gst_multi_fd_sink_add_full);
Packit 0652a1
  klass->remove = GST_DEBUG_FUNCPTR (gst_multi_fd_sink_remove);
Packit 0652a1
  klass->remove_flush = GST_DEBUG_FUNCPTR (gst_multi_fd_sink_remove_flush);
Packit 0652a1
  klass->get_stats = GST_DEBUG_FUNCPTR (gst_multi_fd_sink_get_stats);
Packit 0652a1
Packit 0652a1
  gstmultihandlesink_class->emit_client_added =
Packit 0652a1
      gst_multi_fd_sink_emit_client_added;
Packit 0652a1
  gstmultihandlesink_class->emit_client_removed =
Packit 0652a1
      gst_multi_fd_sink_emit_client_removed;
Packit 0652a1
Packit 0652a1
  gstmultihandlesink_class->stop_pre =
Packit 0652a1
      GST_DEBUG_FUNCPTR (gst_multi_fd_sink_stop_pre);
Packit 0652a1
  gstmultihandlesink_class->stop_post =
Packit 0652a1
      GST_DEBUG_FUNCPTR (gst_multi_fd_sink_stop_post);
Packit 0652a1
  gstmultihandlesink_class->start_pre =
Packit 0652a1
      GST_DEBUG_FUNCPTR (gst_multi_fd_sink_start_pre);
Packit 0652a1
  gstmultihandlesink_class->thread =
Packit 0652a1
      GST_DEBUG_FUNCPTR (gst_multi_fd_sink_thread);
Packit 0652a1
  gstmultihandlesink_class->new_client =
Packit 0652a1
      GST_DEBUG_FUNCPTR (gst_multi_fd_sink_new_client);
Packit 0652a1
  gstmultihandlesink_class->client_free = gst_multi_fd_sink_client_free;
Packit 0652a1
  gstmultihandlesink_class->client_get_fd =
Packit 0652a1
      GST_DEBUG_FUNCPTR (gst_multi_fd_sink_client_get_fd);
Packit 0652a1
  gstmultihandlesink_class->handle_debug =
Packit 0652a1
      GST_DEBUG_FUNCPTR (gst_multi_fd_sink_handle_debug);
Packit 0652a1
  gstmultihandlesink_class->handle_hash_key =
Packit 0652a1
      GST_DEBUG_FUNCPTR (gst_multi_fd_sink_handle_hash_key);
Packit 0652a1
  gstmultihandlesink_class->hash_changed =
Packit 0652a1
      GST_DEBUG_FUNCPTR (gst_multi_fd_sink_hash_changed);
Packit 0652a1
  gstmultihandlesink_class->hash_adding =
Packit 0652a1
      GST_DEBUG_FUNCPTR (gst_multi_fd_sink_hash_adding);
Packit 0652a1
  gstmultihandlesink_class->hash_removing =
Packit 0652a1
      GST_DEBUG_FUNCPTR (gst_multi_fd_sink_hash_removing);
Packit 0652a1
Packit 0652a1
  GST_DEBUG_CATEGORY_INIT (multifdsink_debug, "multifdsink", 0, "FD sink");
Packit 0652a1
}
Packit 0652a1
Packit 0652a1
static void
Packit 0652a1
gst_multi_fd_sink_init (GstMultiFdSink * this)
Packit 0652a1
{
Packit 0652a1
  GstMultiHandleSink *mhsink = GST_MULTI_HANDLE_SINK (this);
Packit 0652a1
Packit 0652a1
  mhsink->handle_hash = g_hash_table_new (g_direct_hash, g_direct_equal);
Packit 0652a1
Packit 0652a1
  this->handle_read = DEFAULT_HANDLE_READ;
Packit 0652a1
}
Packit 0652a1
Packit 0652a1
/* methods to emit signals */
Packit 0652a1
Packit 0652a1
static void
Packit 0652a1
gst_multi_fd_sink_emit_client_added (GstMultiHandleSink * mhsink,
Packit 0652a1
    GstMultiSinkHandle handle)
Packit 0652a1
{
Packit 0652a1
  g_signal_emit (mhsink, gst_multi_fd_sink_signals[SIGNAL_CLIENT_ADDED], 0,
Packit 0652a1
      handle.fd);
Packit 0652a1
}
Packit 0652a1
Packit 0652a1
static void
Packit 0652a1
gst_multi_fd_sink_emit_client_removed (GstMultiHandleSink * mhsink,
Packit 0652a1
    GstMultiSinkHandle handle, GstClientStatus status)
Packit 0652a1
{
Packit 0652a1
  g_signal_emit (mhsink, gst_multi_fd_sink_signals[SIGNAL_CLIENT_REMOVED], 0,
Packit 0652a1
      handle.fd, status);
Packit 0652a1
}
Packit 0652a1
Packit 0652a1
static void
Packit 0652a1
gst_multi_fd_sink_client_free (GstMultiHandleSink * mhsink,
Packit 0652a1
    GstMultiHandleClient * client)
Packit 0652a1
{
Packit 0652a1
  g_signal_emit (mhsink, gst_multi_fd_sink_signals[SIGNAL_CLIENT_FD_REMOVED],
Packit 0652a1
      0, client->handle.fd);
Packit 0652a1
}
Packit 0652a1
Packit 0652a1
/* action signals */
Packit 0652a1
Packit 0652a1
static void
Packit 0652a1
gst_multi_fd_sink_add (GstMultiFdSink * sink, int fd)
Packit 0652a1
{
Packit 0652a1
  GstMultiSinkHandle handle;
Packit 0652a1
Packit 0652a1
  handle.fd = fd;
Packit 0652a1
  gst_multi_handle_sink_add (GST_MULTI_HANDLE_SINK_CAST (sink), handle);
Packit 0652a1
}
Packit 0652a1
Packit 0652a1
static void
Packit 0652a1
gst_multi_fd_sink_add_full (GstMultiFdSink * sink, int fd,
Packit 0652a1
    GstSyncMethod sync, GstFormat min_format, guint64 min_value,
Packit 0652a1
    GstFormat max_format, guint64 max_value)
Packit 0652a1
{
Packit 0652a1
  GstMultiSinkHandle handle;
Packit 0652a1
Packit 0652a1
  handle.fd = fd;
Packit 0652a1
  gst_multi_handle_sink_add_full (GST_MULTI_HANDLE_SINK_CAST (sink), handle,
Packit 0652a1
      sync, min_format, min_value, max_format, max_value);
Packit 0652a1
}
Packit 0652a1
Packit 0652a1
static void
Packit 0652a1
gst_multi_fd_sink_remove (GstMultiFdSink * sink, int fd)
Packit 0652a1
{
Packit 0652a1
  GstMultiSinkHandle handle;
Packit 0652a1
Packit 0652a1
  handle.fd = fd;
Packit 0652a1
  gst_multi_handle_sink_remove (GST_MULTI_HANDLE_SINK_CAST (sink), handle);
Packit 0652a1
}
Packit 0652a1
Packit 0652a1
static void
Packit 0652a1
gst_multi_fd_sink_remove_flush (GstMultiFdSink * sink, int fd)
Packit 0652a1
{
Packit 0652a1
  GstMultiSinkHandle handle;
Packit 0652a1
Packit 0652a1
  handle.fd = fd;
Packit 0652a1
  gst_multi_handle_sink_remove_flush (GST_MULTI_HANDLE_SINK_CAST (sink),
Packit 0652a1
      handle);
Packit 0652a1
}
Packit 0652a1
Packit 0652a1
static GstStructure *
Packit 0652a1
gst_multi_fd_sink_get_stats (GstMultiFdSink * sink, int fd)
Packit 0652a1
{
Packit 0652a1
  GstMultiSinkHandle handle;
Packit 0652a1
Packit 0652a1
  handle.fd = fd;
Packit 0652a1
  return gst_multi_handle_sink_get_stats (GST_MULTI_HANDLE_SINK_CAST (sink),
Packit 0652a1
      handle);
Packit 0652a1
}
Packit 0652a1
Packit 0652a1
/* vfuncs */
Packit 0652a1
Packit 0652a1
static GstMultiHandleClient *
Packit 0652a1
gst_multi_fd_sink_new_client (GstMultiHandleSink * mhsink,
Packit 0652a1
    GstMultiSinkHandle handle, GstSyncMethod sync_method)
Packit 0652a1
{
Packit 0652a1
  struct stat statbuf;
Packit 0652a1
  GstTCPClient *client;
Packit 0652a1
  GstMultiHandleClient *mhclient;
Packit 0652a1
  GstMultiFdSink *sink = GST_MULTI_FD_SINK (mhsink);
Packit 0652a1
  GstMultiHandleSinkClass *mhsinkclass =
Packit 0652a1
      GST_MULTI_HANDLE_SINK_GET_CLASS (mhsink);
Packit 0652a1
Packit 0652a1
  /* create client datastructure */
Packit 0652a1
  client = g_new0 (GstTCPClient, 1);
Packit 0652a1
  mhclient = (GstMultiHandleClient *) client;
Packit 0652a1
Packit 0652a1
  mhclient->handle = handle;
Packit 0652a1
Packit 0652a1
  gst_poll_fd_init (&client->gfd);
Packit 0652a1
  client->gfd.fd = mhclient->handle.fd;
Packit 0652a1
Packit 0652a1
  gst_multi_handle_sink_client_init (mhclient, sync_method);
Packit 0652a1
  mhsinkclass->handle_debug (handle, mhclient->debug);
Packit 0652a1
Packit 0652a1
  /* set the socket to non blocking */
Packit 0652a1
  if (fcntl (handle.fd, F_SETFL, O_NONBLOCK) < 0) {
Packit 0652a1
    GST_ERROR_OBJECT (mhsink, "failed to make socket %s non-blocking: %s",
Packit 0652a1
        mhclient->debug, g_strerror (errno));
Packit 0652a1
  }
Packit 0652a1
Packit 0652a1
  /* we always read from a client */
Packit 0652a1
  gst_poll_add_fd (sink->fdset, &client->gfd);
Packit 0652a1
Packit 0652a1
  /* we don't try to read from write only fds */
Packit 0652a1
  if (sink->handle_read) {
Packit 0652a1
    gint flags;
Packit 0652a1
Packit 0652a1
    flags = fcntl (handle.fd, F_GETFL, 0);
Packit 0652a1
    if ((flags & O_ACCMODE) != O_WRONLY) {
Packit 0652a1
      gst_poll_fd_ctl_read (sink->fdset, &client->gfd, TRUE);
Packit 0652a1
    }
Packit 0652a1
  }
Packit 0652a1
  /* figure out the mode, can't use send() for non sockets */
Packit 0652a1
  if (fstat (handle.fd, &statbuf) == 0 && S_ISSOCK (statbuf.st_mode)) {
Packit 0652a1
    client->is_socket = TRUE;
Packit 0652a1
    gst_multi_handle_sink_setup_dscp_client (mhsink, mhclient);
Packit 0652a1
  }
Packit 0652a1
Packit 0652a1
  return mhclient;
Packit 0652a1
}
Packit 0652a1
Packit 0652a1
static int
Packit 0652a1
gst_multi_fd_sink_client_get_fd (GstMultiHandleClient * client)
Packit 0652a1
{
Packit 0652a1
  GstTCPClient *tclient = (GstTCPClient *) client;
Packit 0652a1
Packit 0652a1
  return tclient->gfd.fd;
Packit 0652a1
}
Packit 0652a1
Packit 0652a1
static void
Packit 0652a1
gst_multi_fd_sink_handle_debug (GstMultiSinkHandle handle, gchar debug[30])
Packit 0652a1
{
Packit 0652a1
  g_snprintf (debug, 30, "[fd %5d]", handle.fd);
Packit 0652a1
}
Packit 0652a1
Packit 0652a1
static gpointer
Packit 0652a1
gst_multi_fd_sink_handle_hash_key (GstMultiSinkHandle handle)
Packit 0652a1
{
Packit 0652a1
  return GINT_TO_POINTER (handle.fd);
Packit 0652a1
}
Packit 0652a1
Packit 0652a1
static void
Packit 0652a1
gst_multi_fd_sink_hash_changed (GstMultiHandleSink * mhsink)
Packit 0652a1
{
Packit 0652a1
  GstMultiFdSink *sink = GST_MULTI_FD_SINK (mhsink);
Packit 0652a1
Packit 0652a1
  gst_poll_restart (sink->fdset);
Packit 0652a1
}
Packit 0652a1
Packit 0652a1
/* handle a read on a client fd,
Packit 0652a1
 * which either indicates a close or should be ignored
Packit 0652a1
 * returns FALSE if some error occured or the client closed. */
Packit 0652a1
static gboolean
Packit 0652a1
gst_multi_fd_sink_handle_client_read (GstMultiFdSink * sink,
Packit 0652a1
    GstTCPClient * client)
Packit 0652a1
{
Packit 0652a1
  int avail, fd;
Packit 0652a1
  gboolean ret;
Packit 0652a1
  GstMultiHandleClient *mhclient = (GstMultiHandleClient *) client;
Packit 0652a1
Packit 0652a1
  fd = client->gfd.fd;
Packit 0652a1
Packit 0652a1
  if (ioctl (fd, FIONREAD, &avail) < 0)
Packit 0652a1
    goto ioctl_failed;
Packit 0652a1
Packit 0652a1
  GST_DEBUG_OBJECT (sink, "%s select reports client read of %d bytes",
Packit 0652a1
      mhclient->debug, avail);
Packit 0652a1
Packit 0652a1
  ret = TRUE;
Packit 0652a1
Packit 0652a1
  if (avail == 0) {
Packit 0652a1
    /* client sent close, so remove it */
Packit 0652a1
    GST_DEBUG_OBJECT (sink, "%s client asked for close, removing",
Packit 0652a1
        mhclient->debug);
Packit 0652a1
    mhclient->status = GST_CLIENT_STATUS_CLOSED;
Packit 0652a1
    ret = FALSE;
Packit 0652a1
  } else if (avail < 0) {
Packit 0652a1
    GST_WARNING_OBJECT (sink, "%s avail < 0, removing", mhclient->debug);
Packit 0652a1
    mhclient->status = GST_CLIENT_STATUS_ERROR;
Packit 0652a1
    ret = FALSE;
Packit 0652a1
  } else {
Packit 0652a1
    guint8 dummy[512];
Packit 0652a1
    gint nread;
Packit 0652a1
Packit 0652a1
    /* just Read 'n' Drop, could also just drop the client as it's not supposed
Packit 0652a1
     * to write to us except for closing the socket, I guess it's because we
Packit 0652a1
     * like to listen to our customers. */
Packit 0652a1
    do {
Packit 0652a1
      /* this is the maximum we can read */
Packit 0652a1
      gint to_read = MIN (avail, 512);
Packit 0652a1
Packit 0652a1
      GST_DEBUG_OBJECT (sink, "%s client wants us to read %d bytes",
Packit 0652a1
          mhclient->debug, to_read);
Packit 0652a1
Packit 0652a1
      nread = read (fd, dummy, to_read);
Packit 0652a1
      if (nread < -1) {
Packit 0652a1
        GST_WARNING_OBJECT (sink, "%s could not read %d bytes: %s (%d)",
Packit 0652a1
            mhclient->debug, to_read, g_strerror (errno), errno);
Packit 0652a1
        mhclient->status = GST_CLIENT_STATUS_ERROR;
Packit 0652a1
        ret = FALSE;
Packit 0652a1
        break;
Packit 0652a1
      } else if (nread == 0) {
Packit 0652a1
        GST_WARNING_OBJECT (sink, "%s 0 bytes in read, removing",
Packit 0652a1
            mhclient->debug);
Packit 0652a1
        mhclient->status = GST_CLIENT_STATUS_ERROR;
Packit 0652a1
        ret = FALSE;
Packit 0652a1
        break;
Packit 0652a1
      }
Packit 0652a1
      avail -= nread;
Packit 0652a1
    }
Packit 0652a1
    while (avail > 0);
Packit 0652a1
  }
Packit 0652a1
  return ret;
Packit 0652a1
Packit 0652a1
  /* ERRORS */
Packit 0652a1
ioctl_failed:
Packit 0652a1
  {
Packit 0652a1
    GST_WARNING_OBJECT (sink, "%s ioctl failed: %s (%d)",
Packit 0652a1
        mhclient->debug, g_strerror (errno), errno);
Packit 0652a1
    mhclient->status = GST_CLIENT_STATUS_ERROR;
Packit 0652a1
    return FALSE;
Packit 0652a1
  }
Packit 0652a1
}
Packit 0652a1
Packit 0652a1
/* Handle a write on a client,
Packit 0652a1
 * which indicates a read request from a client.
Packit 0652a1
 *
Packit 0652a1
 * For each client we maintain a queue of GstBuffers that contain the raw bytes
Packit 0652a1
 * we need to send to the client.
Packit 0652a1
 *
Packit 0652a1
 * We first check to see if we need to send streamheaders. If so, we queue them.
Packit 0652a1
 *
Packit 0652a1
 * Then we run into the main loop that tries to send as many buffers as
Packit 0652a1
 * possible. It will first exhaust the mhclient->sending queue and if the queue
Packit 0652a1
 * is empty, it will pick a buffer from the global queue.
Packit 0652a1
 *
Packit 0652a1
 * Sending the buffers from the mhclient->sending queue is basically writing
Packit 0652a1
 * the bytes to the socket and maintaining a count of the bytes that were
Packit 0652a1
 * sent. When the buffer is completely sent, it is removed from the
Packit 0652a1
 * mhclient->sending queue and we try to pick a new buffer for sending.
Packit 0652a1
 *
Packit 0652a1
 * When the sending returns a partial buffer we stop sending more data as
Packit 0652a1
 * the next send operation could block.
Packit 0652a1
 *
Packit 0652a1
 * This functions returns FALSE if some error occured.
Packit 0652a1
 */
Packit 0652a1
static gboolean
Packit 0652a1
gst_multi_fd_sink_handle_client_write (GstMultiFdSink * sink,
Packit 0652a1
    GstTCPClient * client)
Packit 0652a1
{
Packit 0652a1
  gboolean more;
Packit 0652a1
  gboolean flushing;
Packit 0652a1
  GstClockTime now;
Packit 0652a1
  GTimeVal nowtv;
Packit 0652a1
  GstMultiHandleSink *mhsink = GST_MULTI_HANDLE_SINK (sink);
Packit 0652a1
  GstMultiHandleSinkClass *mhsinkclass =
Packit 0652a1
      GST_MULTI_HANDLE_SINK_GET_CLASS (mhsink);
Packit 0652a1
  GstMultiHandleClient *mhclient = (GstMultiHandleClient *) client;
Packit 0652a1
  int fd = mhclient->handle.fd;
Packit 0652a1
Packit 0652a1
  flushing = mhclient->status == GST_CLIENT_STATUS_FLUSHING;
Packit 0652a1
Packit 0652a1
  more = TRUE;
Packit 0652a1
  do {
Packit 0652a1
    gint maxsize;
Packit 0652a1
Packit 0652a1
    g_get_current_time (&nowtv);
Packit 0652a1
    now = GST_TIMEVAL_TO_TIME (nowtv);
Packit 0652a1
Packit 0652a1
    if (!mhclient->sending) {
Packit 0652a1
      /* client is not working on a buffer */
Packit 0652a1
      if (mhclient->bufpos == -1) {
Packit 0652a1
        /* client is too fast, remove from write queue until new buffer is
Packit 0652a1
         * available */
Packit 0652a1
        /* FIXME: specific */
Packit 0652a1
        gst_poll_fd_ctl_write (sink->fdset, &client->gfd, FALSE);
Packit 0652a1
Packit 0652a1
        /* if we flushed out all of the client buffers, we can stop */
Packit 0652a1
        if (mhclient->flushcount == 0)
Packit 0652a1
          goto flushed;
Packit 0652a1
Packit 0652a1
        return TRUE;
Packit 0652a1
      } else {
Packit 0652a1
        /* client can pick a buffer from the global queue */
Packit 0652a1
        GstBuffer *buf;
Packit 0652a1
        GstClockTime timestamp;
Packit 0652a1
Packit 0652a1
        /* for new connections, we need to find a good spot in the
Packit 0652a1
         * bufqueue to start streaming from */
Packit 0652a1
        if (mhclient->new_connection && !flushing) {
Packit 0652a1
          gint position =
Packit 0652a1
              gst_multi_handle_sink_new_client_position (mhsink, mhclient);
Packit 0652a1
Packit 0652a1
          if (position >= 0) {
Packit 0652a1
            /* we got a valid spot in the queue */
Packit 0652a1
            mhclient->new_connection = FALSE;
Packit 0652a1
            mhclient->bufpos = position;
Packit 0652a1
          } else {
Packit 0652a1
            /* cannot send data to this client yet */
Packit 0652a1
            /* FIXME: specific */
Packit 0652a1
            gst_poll_fd_ctl_write (sink->fdset, &client->gfd, FALSE);
Packit 0652a1
            return TRUE;
Packit 0652a1
          }
Packit 0652a1
        }
Packit 0652a1
Packit 0652a1
        /* we flushed all remaining buffers, no need to get a new one */
Packit 0652a1
        if (mhclient->flushcount == 0)
Packit 0652a1
          goto flushed;
Packit 0652a1
Packit 0652a1
        /* grab buffer */
Packit 0652a1
        buf = g_array_index (mhsink->bufqueue, GstBuffer *, mhclient->bufpos);
Packit 0652a1
        mhclient->bufpos--;
Packit 0652a1
Packit 0652a1
        /* update stats */
Packit 0652a1
        timestamp = GST_BUFFER_TIMESTAMP (buf);
Packit 0652a1
        if (mhclient->first_buffer_ts == GST_CLOCK_TIME_NONE)
Packit 0652a1
          mhclient->first_buffer_ts = timestamp;
Packit 0652a1
        if (timestamp != -1)
Packit 0652a1
          mhclient->last_buffer_ts = timestamp;
Packit 0652a1
Packit 0652a1
        /* decrease flushcount */
Packit 0652a1
        if (mhclient->flushcount != -1)
Packit 0652a1
          mhclient->flushcount--;
Packit 0652a1
Packit 0652a1
        GST_LOG_OBJECT (sink, "%s client %p at position %d",
Packit 0652a1
            mhclient->debug, client, mhclient->bufpos);
Packit 0652a1
Packit 0652a1
        /* queueing a buffer will ref it */
Packit 0652a1
        mhsinkclass->client_queue_buffer (mhsink, mhclient, buf);
Packit 0652a1
Packit 0652a1
        /* need to start from the first byte for this new buffer */
Packit 0652a1
        mhclient->bufoffset = 0;
Packit 0652a1
      }
Packit 0652a1
    }
Packit 0652a1
Packit 0652a1
    /* see if we need to send something */
Packit 0652a1
    if (mhclient->sending) {
Packit 0652a1
      ssize_t wrote;
Packit 0652a1
      GstBuffer *head;
Packit 0652a1
      GstMapInfo info;
Packit 0652a1
      guint8 *data;
Packit 0652a1
Packit 0652a1
      /* pick first buffer from list */
Packit 0652a1
      head = GST_BUFFER (mhclient->sending->data);
Packit 0652a1
Packit 0652a1
      if (!gst_buffer_map (head, &info, GST_MAP_READ))
Packit 0652a1
        g_return_val_if_reached (FALSE);
Packit 0652a1
Packit 0652a1
      data = info.data;
Packit 0652a1
      maxsize = info.size - mhclient->bufoffset;
Packit 0652a1
Packit 0652a1
      /* FIXME: specific */
Packit 0652a1
      /* try to write the complete buffer */
Packit 0652a1
#ifdef MSG_NOSIGNAL
Packit 0652a1
#define FLAGS MSG_NOSIGNAL
Packit 0652a1
#else
Packit 0652a1
#define FLAGS 0
Packit 0652a1
#endif
Packit 0652a1
      if (client->is_socket) {
Packit 0652a1
        wrote = send (fd, data + mhclient->bufoffset, maxsize, FLAGS);
Packit 0652a1
      } else {
Packit 0652a1
        wrote = write (fd, data + mhclient->bufoffset, maxsize);
Packit 0652a1
      }
Packit 0652a1
      gst_buffer_unmap (head, &info;;
Packit 0652a1
Packit 0652a1
      if (wrote < 0) {
Packit 0652a1
        /* hmm error.. */
Packit 0652a1
        if (errno == EAGAIN) {
Packit 0652a1
          /* nothing serious, resource was unavailable, try again later */
Packit 0652a1
          more = FALSE;
Packit 0652a1
        } else if (errno == ECONNRESET) {
Packit 0652a1
          goto connection_reset;
Packit 0652a1
        } else {
Packit 0652a1
          goto write_error;
Packit 0652a1
        }
Packit 0652a1
      } else {
Packit 0652a1
        if (wrote < maxsize) {
Packit 0652a1
          /* partial write means that the client cannot read more and we should
Packit 0652a1
           * stop sending more */
Packit 0652a1
          GST_LOG_OBJECT (sink,
Packit 0652a1
              "partial write on %s of %" G_GSSIZE_FORMAT " bytes",
Packit 0652a1
              mhclient->debug, wrote);
Packit 0652a1
          mhclient->bufoffset += wrote;
Packit 0652a1
          more = FALSE;
Packit 0652a1
        } else {
Packit 0652a1
          /* complete buffer was written, we can proceed to the next one */
Packit 0652a1
          mhclient->sending = g_slist_remove (mhclient->sending, head);
Packit 0652a1
          gst_buffer_unref (head);
Packit 0652a1
          /* make sure we start from byte 0 for the next buffer */
Packit 0652a1
          mhclient->bufoffset = 0;
Packit 0652a1
        }
Packit 0652a1
        /* update stats */
Packit 0652a1
        mhclient->bytes_sent += wrote;
Packit 0652a1
        mhclient->last_activity_time = now;
Packit 0652a1
        mhsink->bytes_served += wrote;
Packit 0652a1
      }
Packit 0652a1
    }
Packit 0652a1
  } while (more);
Packit 0652a1
Packit 0652a1
  return TRUE;
Packit 0652a1
Packit 0652a1
  /* ERRORS */
Packit 0652a1
flushed:
Packit 0652a1
  {
Packit 0652a1
    GST_DEBUG_OBJECT (sink, "%s flushed, removing", mhclient->debug);
Packit 0652a1
    mhclient->status = GST_CLIENT_STATUS_REMOVED;
Packit 0652a1
    return FALSE;
Packit 0652a1
  }
Packit 0652a1
connection_reset:
Packit 0652a1
  {
Packit 0652a1
    GST_DEBUG_OBJECT (sink, "%s connection reset by peer, removing",
Packit 0652a1
        mhclient->debug);
Packit 0652a1
    mhclient->status = GST_CLIENT_STATUS_CLOSED;
Packit 0652a1
    return FALSE;
Packit 0652a1
  }
Packit 0652a1
write_error:
Packit 0652a1
  {
Packit 0652a1
    GST_WARNING_OBJECT (sink,
Packit 0652a1
        "%s could not write, removing client: %s (%d)", mhclient->debug,
Packit 0652a1
        g_strerror (errno), errno);
Packit 0652a1
    mhclient->status = GST_CLIENT_STATUS_ERROR;
Packit 0652a1
    return FALSE;
Packit 0652a1
  }
Packit 0652a1
}
Packit 0652a1
Packit 0652a1
static void
Packit 0652a1
gst_multi_fd_sink_hash_adding (GstMultiHandleSink * mhsink,
Packit 0652a1
    GstMultiHandleClient * mhclient)
Packit 0652a1
{
Packit 0652a1
  GstMultiFdSink *sink = GST_MULTI_FD_SINK (mhsink);
Packit 0652a1
  GstTCPClient *client = (GstTCPClient *) mhclient;
Packit 0652a1
Packit 0652a1
  gst_poll_fd_ctl_write (sink->fdset, &client->gfd, TRUE);
Packit 0652a1
}
Packit 0652a1
Packit 0652a1
static void
Packit 0652a1
gst_multi_fd_sink_hash_removing (GstMultiHandleSink * mhsink,
Packit 0652a1
    GstMultiHandleClient * mhclient)
Packit 0652a1
{
Packit 0652a1
  GstMultiFdSink *sink = GST_MULTI_FD_SINK (mhsink);
Packit 0652a1
  GstTCPClient *client = (GstTCPClient *) mhclient;
Packit 0652a1
Packit 0652a1
  gst_poll_remove_fd (sink->fdset, &client->gfd);
Packit 0652a1
}
Packit 0652a1
Packit 0652a1
Packit 0652a1
/* Handle the clients. Basically does a blocking select for one
Packit 0652a1
 * of the client fds to become read or writable. We also have a
Packit 0652a1
 * filedescriptor to receive commands on that we need to check.
Packit 0652a1
 *
Packit 0652a1
 * After going out of the select call, we read and write to all
Packit 0652a1
 * clients that can do so. Badly behaving clients are put on a
Packit 0652a1
 * garbage list and removed.
Packit 0652a1
 */
Packit 0652a1
static void
Packit 0652a1
gst_multi_fd_sink_handle_clients (GstMultiFdSink * sink)
Packit 0652a1
{
Packit 0652a1
  int result;
Packit 0652a1
  GList *clients, *next;
Packit 0652a1
  gboolean try_again;
Packit 0652a1
  GstMultiFdSinkClass *fclass;
Packit 0652a1
  guint cookie;
Packit 0652a1
  GstMultiHandleSink *mhsink = GST_MULTI_HANDLE_SINK (sink);
Packit 0652a1
  int fd;
Packit 0652a1
Packit 0652a1
Packit 0652a1
  fclass = GST_MULTI_FD_SINK_GET_CLASS (sink);
Packit 0652a1
Packit 0652a1
  do {
Packit 0652a1
    try_again = FALSE;
Packit 0652a1
Packit 0652a1
    /* check for:
Packit 0652a1
     * - server socket input (ie, new client connections)
Packit 0652a1
     * - client socket input (ie, clients saying goodbye)
Packit 0652a1
     * - client socket output (ie, client reads)          */
Packit 0652a1
    GST_LOG_OBJECT (sink, "waiting on action on fdset");
Packit 0652a1
Packit 0652a1
    result =
Packit 0652a1
        gst_poll_wait (sink->fdset,
Packit 0652a1
        mhsink->timeout != 0 ? mhsink->timeout : GST_CLOCK_TIME_NONE);
Packit 0652a1
Packit 0652a1
    /* Handle the special case in which the sink is not receiving more buffers
Packit 0652a1
     * and will not disconnect inactive client in the streaming thread. */
Packit 0652a1
    if (G_UNLIKELY (result == 0)) {
Packit 0652a1
      GstClockTime now;
Packit 0652a1
      GTimeVal nowtv;
Packit 0652a1
Packit 0652a1
      g_get_current_time (&nowtv);
Packit 0652a1
      now = GST_TIMEVAL_TO_TIME (nowtv);
Packit 0652a1
Packit 0652a1
      CLIENTS_LOCK (mhsink);
Packit 0652a1
      for (clients = mhsink->clients; clients; clients = next) {
Packit 0652a1
        GstTCPClient *client;
Packit 0652a1
        GstMultiHandleClient *mhclient;
Packit 0652a1
Packit 0652a1
        client = (GstTCPClient *) clients->data;
Packit 0652a1
        mhclient = (GstMultiHandleClient *) client;
Packit 0652a1
        next = g_list_next (clients);
Packit 0652a1
        if (mhsink->timeout > 0
Packit 0652a1
            && now - mhclient->last_activity_time > mhsink->timeout) {
Packit 0652a1
          mhclient->status = GST_CLIENT_STATUS_SLOW;
Packit 0652a1
          gst_multi_handle_sink_remove_client_link (mhsink, clients);
Packit 0652a1
        }
Packit 0652a1
      }
Packit 0652a1
      CLIENTS_UNLOCK (mhsink);
Packit 0652a1
      return;
Packit 0652a1
    } else if (result < 0) {
Packit 0652a1
      GST_WARNING_OBJECT (sink, "wait failed: %s (%d)", g_strerror (errno),
Packit 0652a1
          errno);
Packit 0652a1
      if (errno == EBADF) {
Packit 0652a1
        /* ok, so one or more of the fds is invalid. We loop over them to find
Packit 0652a1
         * the ones that give an error to the F_GETFL fcntl. */
Packit 0652a1
        CLIENTS_LOCK (mhsink);
Packit 0652a1
      restart:
Packit 0652a1
        cookie = mhsink->clients_cookie;
Packit 0652a1
        for (clients = mhsink->clients; clients; clients = next) {
Packit 0652a1
          GstTCPClient *client;
Packit 0652a1
          GstMultiHandleClient *mhclient;
Packit 0652a1
          long flags;
Packit 0652a1
          int res;
Packit 0652a1
Packit 0652a1
          if (cookie != mhsink->clients_cookie) {
Packit 0652a1
            GST_DEBUG_OBJECT (sink, "Cookie changed finding bad fd");
Packit 0652a1
            goto restart;
Packit 0652a1
          }
Packit 0652a1
Packit 0652a1
          client = (GstTCPClient *) clients->data;
Packit 0652a1
          mhclient = (GstMultiHandleClient *) client;
Packit 0652a1
          next = g_list_next (clients);
Packit 0652a1
Packit 0652a1
          fd = client->gfd.fd;
Packit 0652a1
Packit 0652a1
          res = fcntl (fd, F_GETFL, &flags);
Packit 0652a1
          if (res == -1) {
Packit 0652a1
            GST_WARNING_OBJECT (sink, "fcntl failed for %d, removing: %s (%d)",
Packit 0652a1
                fd, g_strerror (errno), errno);
Packit 0652a1
            if (errno == EBADF) {
Packit 0652a1
              mhclient->status = GST_CLIENT_STATUS_ERROR;
Packit 0652a1
              /* releases the CLIENTS lock */
Packit 0652a1
              gst_multi_handle_sink_remove_client_link (mhsink, clients);
Packit 0652a1
            }
Packit 0652a1
          }
Packit 0652a1
        }
Packit 0652a1
        CLIENTS_UNLOCK (mhsink);
Packit 0652a1
        /* after this, go back in the select loop as the read/writefds
Packit 0652a1
         * are not valid */
Packit 0652a1
        try_again = TRUE;
Packit 0652a1
      } else if (errno == EINTR) {
Packit 0652a1
        /* interrupted system call, just redo the wait */
Packit 0652a1
        try_again = TRUE;
Packit 0652a1
      } else if (errno == EBUSY) {
Packit 0652a1
        /* the call to gst_poll_wait() was flushed */
Packit 0652a1
        return;
Packit 0652a1
      } else {
Packit 0652a1
        /* this is quite bad... */
Packit 0652a1
        GST_ELEMENT_ERROR (sink, RESOURCE, READ, (NULL),
Packit 0652a1
            ("select failed: %s (%d)", g_strerror (errno), errno));
Packit 0652a1
        return;
Packit 0652a1
      }
Packit 0652a1
    } else {
Packit 0652a1
      GST_LOG_OBJECT (sink, "wait done: %d sockets with events", result);
Packit 0652a1
    }
Packit 0652a1
  } while (try_again);
Packit 0652a1
Packit 0652a1
  /* subclasses can check fdset with this virtual function */
Packit 0652a1
  if (fclass->wait)
Packit 0652a1
    fclass->wait (sink, sink->fdset);
Packit 0652a1
Packit 0652a1
  /* Check the clients */
Packit 0652a1
  CLIENTS_LOCK (mhsink);
Packit 0652a1
Packit 0652a1
restart2:
Packit 0652a1
  cookie = mhsink->clients_cookie;
Packit 0652a1
  for (clients = mhsink->clients; clients; clients = next) {
Packit 0652a1
    GstTCPClient *client;
Packit 0652a1
    GstMultiHandleClient *mhclient;
Packit 0652a1
Packit 0652a1
    if (mhsink->clients_cookie != cookie) {
Packit 0652a1
      GST_DEBUG_OBJECT (sink, "Restarting loop, cookie out of date");
Packit 0652a1
      goto restart2;
Packit 0652a1
    }
Packit 0652a1
Packit 0652a1
    client = (GstTCPClient *) clients->data;
Packit 0652a1
    mhclient = (GstMultiHandleClient *) client;
Packit 0652a1
    next = g_list_next (clients);
Packit 0652a1
Packit 0652a1
    if (mhclient->status != GST_CLIENT_STATUS_FLUSHING
Packit 0652a1
        && mhclient->status != GST_CLIENT_STATUS_OK) {
Packit 0652a1
      gst_multi_handle_sink_remove_client_link (mhsink, clients);
Packit 0652a1
      continue;
Packit 0652a1
    }
Packit 0652a1
Packit 0652a1
    if (gst_poll_fd_has_closed (sink->fdset, &client->gfd)) {
Packit 0652a1
      mhclient->status = GST_CLIENT_STATUS_CLOSED;
Packit 0652a1
      gst_multi_handle_sink_remove_client_link (mhsink, clients);
Packit 0652a1
      continue;
Packit 0652a1
    }
Packit 0652a1
    if (gst_poll_fd_has_error (sink->fdset, &client->gfd)) {
Packit 0652a1
      GST_WARNING_OBJECT (sink, "gst_poll_fd_has_error for %d", client->gfd.fd);
Packit 0652a1
      mhclient->status = GST_CLIENT_STATUS_ERROR;
Packit 0652a1
      gst_multi_handle_sink_remove_client_link (mhsink, clients);
Packit 0652a1
      continue;
Packit 0652a1
    }
Packit 0652a1
    if (gst_poll_fd_can_read (sink->fdset, &client->gfd)) {
Packit 0652a1
      /* handle client read */
Packit 0652a1
      if (!gst_multi_fd_sink_handle_client_read (sink, client)) {
Packit 0652a1
        gst_multi_handle_sink_remove_client_link (mhsink, clients);
Packit 0652a1
        continue;
Packit 0652a1
      }
Packit 0652a1
    }
Packit 0652a1
    if (gst_poll_fd_can_write (sink->fdset, &client->gfd)) {
Packit 0652a1
      /* handle client write */
Packit 0652a1
      if (!gst_multi_fd_sink_handle_client_write (sink, client)) {
Packit 0652a1
        gst_multi_handle_sink_remove_client_link (mhsink, clients);
Packit 0652a1
        continue;
Packit 0652a1
      }
Packit 0652a1
    }
Packit 0652a1
  }
Packit 0652a1
  CLIENTS_UNLOCK (mhsink);
Packit 0652a1
}
Packit 0652a1
Packit 0652a1
/* we handle the client communication in another thread so that we do not block
Packit 0652a1
 * the gstreamer thread while we select() on the client fds */
Packit 0652a1
static gpointer
Packit 0652a1
gst_multi_fd_sink_thread (GstMultiHandleSink * mhsink)
Packit 0652a1
{
Packit 0652a1
  GstMultiFdSink *sink = GST_MULTI_FD_SINK (mhsink);
Packit 0652a1
Packit 0652a1
  while (mhsink->running) {
Packit 0652a1
    gst_multi_fd_sink_handle_clients (sink);
Packit 0652a1
  }
Packit 0652a1
  return NULL;
Packit 0652a1
}
Packit 0652a1
Packit 0652a1
static void
Packit 0652a1
gst_multi_fd_sink_set_property (GObject * object, guint prop_id,
Packit 0652a1
    const GValue * value, GParamSpec * pspec)
Packit 0652a1
{
Packit 0652a1
  GstMultiFdSink *multifdsink;
Packit 0652a1
Packit 0652a1
  multifdsink = GST_MULTI_FD_SINK (object);
Packit 0652a1
Packit 0652a1
  switch (prop_id) {
Packit 0652a1
    case PROP_HANDLE_READ:
Packit 0652a1
      multifdsink->handle_read = g_value_get_boolean (value);
Packit 0652a1
      break;
Packit 0652a1
Packit 0652a1
    default:
Packit 0652a1
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
Packit 0652a1
      break;
Packit 0652a1
  }
Packit 0652a1
}
Packit 0652a1
Packit 0652a1
static void
Packit 0652a1
gst_multi_fd_sink_get_property (GObject * object, guint prop_id, GValue * value,
Packit 0652a1
    GParamSpec * pspec)
Packit 0652a1
{
Packit 0652a1
  GstMultiFdSink *multifdsink;
Packit 0652a1
Packit 0652a1
  multifdsink = GST_MULTI_FD_SINK (object);
Packit 0652a1
Packit 0652a1
  switch (prop_id) {
Packit 0652a1
    case PROP_HANDLE_READ:
Packit 0652a1
      g_value_set_boolean (value, multifdsink->handle_read);
Packit 0652a1
      break;
Packit 0652a1
Packit 0652a1
    default:
Packit 0652a1
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
Packit 0652a1
      break;
Packit 0652a1
  }
Packit 0652a1
}
Packit 0652a1
Packit 0652a1
static gboolean
Packit 0652a1
gst_multi_fd_sink_start_pre (GstMultiHandleSink * mhsink)
Packit 0652a1
{
Packit 0652a1
  GstMultiFdSink *mfsink = GST_MULTI_FD_SINK (mhsink);
Packit 0652a1
Packit 0652a1
  GST_INFO_OBJECT (mfsink, "starting");
Packit 0652a1
  if ((mfsink->fdset = gst_poll_new (TRUE)) == NULL)
Packit 0652a1
    goto socket_pair;
Packit 0652a1
Packit 0652a1
  return TRUE;
Packit 0652a1
Packit 0652a1
  /* ERRORS */
Packit 0652a1
socket_pair:
Packit 0652a1
  {
Packit 0652a1
    GST_ELEMENT_ERROR (mfsink, RESOURCE, OPEN_READ_WRITE, (NULL),
Packit 0652a1
        GST_ERROR_SYSTEM);
Packit 0652a1
    return FALSE;
Packit 0652a1
  }
Packit 0652a1
}
Packit 0652a1
Packit 0652a1
static gboolean
Packit 0652a1
multifdsink_hash_remove (gpointer key, gpointer value, gpointer data)
Packit 0652a1
{
Packit 0652a1
  return TRUE;
Packit 0652a1
}
Packit 0652a1
Packit 0652a1
static void
Packit 0652a1
gst_multi_fd_sink_stop_pre (GstMultiHandleSink * mhsink)
Packit 0652a1
{
Packit 0652a1
  GstMultiFdSink *mfsink = GST_MULTI_FD_SINK (mhsink);
Packit 0652a1
Packit 0652a1
  gst_poll_set_flushing (mfsink->fdset, TRUE);
Packit 0652a1
}
Packit 0652a1
Packit 0652a1
static void
Packit 0652a1
gst_multi_fd_sink_stop_post (GstMultiHandleSink * mhsink)
Packit 0652a1
{
Packit 0652a1
  GstMultiFdSink *mfsink = GST_MULTI_FD_SINK (mhsink);
Packit 0652a1
Packit 0652a1
  if (mfsink->fdset) {
Packit 0652a1
    gst_poll_free (mfsink->fdset);
Packit 0652a1
    mfsink->fdset = NULL;
Packit 0652a1
  }
Packit 0652a1
  g_hash_table_foreach_remove (mhsink->handle_hash, multifdsink_hash_remove,
Packit 0652a1
      mfsink);
Packit 0652a1
}