Exemplo n.º 1
0
static void
gst_dv1394src_init (GstDV1394Src * dv1394src, GstDV1394SrcClass * klass)
{
  GstPad *srcpad = GST_BASE_SRC_PAD (dv1394src);

  gst_base_src_set_live (GST_BASE_SRC (dv1394src), TRUE);
  gst_base_src_set_format (GST_BASE_SRC (dv1394src), GST_FORMAT_TIME);
  gst_base_src_set_do_timestamp (GST_BASE_SRC (dv1394src), TRUE);
  gst_pad_use_fixed_caps (srcpad);

  dv1394src->port = DEFAULT_PORT;
  dv1394src->channel = DEFAULT_CHANNEL;

  dv1394src->consecutive = DEFAULT_CONSECUTIVE;
  dv1394src->skip = DEFAULT_SKIP;
  dv1394src->drop_incomplete = DEFAULT_DROP_INCOMPLETE;
  dv1394src->use_avc = DEFAULT_USE_AVC;
  dv1394src->guid = DEFAULT_GUID;
  dv1394src->uri = g_strdup_printf ("dv://%d", dv1394src->port);
  dv1394src->device_name = g_strdup_printf ("Default");

  READ_SOCKET (dv1394src) = -1;
  WRITE_SOCKET (dv1394src) = -1;

  /* initialized when first header received */
  dv1394src->frame_size = 0;

  dv1394src->buf = NULL;
  dv1394src->frame = NULL;
  dv1394src->frame_sequence = 0;

  dv1394src->provided_clock = gst_1394_clock_new ("dv1394clock");
}
static gboolean
gst_hdv1394src_stop (GstBaseSrc * bsrc)
{
    GstHDV1394Src *src = GST_HDV1394SRC (bsrc);

    close (READ_SOCKET (src));
    close (WRITE_SOCKET (src));
    READ_SOCKET (src) = -1;
    WRITE_SOCKET (src) = -1;

    iec61883_mpeg2_close (src->iec61883mpeg2);
#if 0
    raw1394_stop_iso_rcv (src->handle, src->channel);
#endif

    if (src->use_avc) {
        raw1394handle_t avc_handle = raw1394_new_handle_on_port (src->port);

        /* pause and stop the VCR */
        if (avc_handle) {
            if (!avc1394_vcr_is_recording (avc_handle, src->avc_node)
                    && (avc1394_vcr_is_playing (avc_handle, src->avc_node)
                        != AVC1394_VCR_OPERAND_PLAY_FORWARD_PAUSE))
                avc1394_vcr_pause (avc_handle, src->avc_node);
            avc1394_vcr_stop (avc_handle, src->avc_node);
            raw1394_destroy_handle (avc_handle);
        } else {
            GST_WARNING_OBJECT (src, "Starting VCR via avc1394 failed: %s",
                                g_strerror (errno));
        }
    }

    raw1394_destroy_handle (src->handle);

    return TRUE;
}
static void
gst_hdv1394src_init (GstHDV1394Src * dv1394src)
{
    GstPad *srcpad = GST_BASE_SRC_PAD (dv1394src);

    gst_base_src_set_live (GST_BASE_SRC (dv1394src), TRUE);
    gst_pad_use_fixed_caps (srcpad);

    dv1394src->port = DEFAULT_PORT;
    dv1394src->channel = DEFAULT_CHANNEL;

    dv1394src->use_avc = DEFAULT_USE_AVC;
    dv1394src->guid = DEFAULT_GUID;
    dv1394src->uri = g_strdup_printf ("hdv://%d", dv1394src->port);
    dv1394src->device_name = g_strdup_printf ("Default");

    READ_SOCKET (dv1394src) = -1;
    WRITE_SOCKET (dv1394src) = -1;

    dv1394src->frame_sequence = 0;
}
static gboolean
gst_hdv1394src_start (GstBaseSrc * bsrc)
{
    GstHDV1394Src *src = GST_HDV1394SRC (bsrc);
    int control_sock[2];

    src->connected = FALSE;

    if (socketpair (PF_UNIX, SOCK_STREAM, 0, control_sock) < 0)
        goto socket_pair;

    READ_SOCKET (src) = control_sock[0];
    WRITE_SOCKET (src) = control_sock[1];

    if (fcntl (READ_SOCKET (src), F_SETFL, O_NONBLOCK) < 0)
        GST_ERROR_OBJECT (src, "failed to make read socket non-blocking: %s",
                          g_strerror (errno));
    if (fcntl (WRITE_SOCKET (src), F_SETFL, O_NONBLOCK) < 0)
        GST_ERROR_OBJECT (src, "failed to make write socket non-blocking: %s",
                          g_strerror (errno));

    src->handle = raw1394_new_handle ();

    if (!src->handle) {
        if (errno == EACCES)
            goto permission_denied;
        else if (errno == ENOENT)
            goto not_found;
        else
            goto no_handle;
    }

    src->num_ports = raw1394_get_port_info (src->handle, src->pinfo, 16);

    if (src->num_ports == 0)
        goto no_ports;

    if (src->use_avc || src->port == -1)
        src->avc_node = gst_hdv1394src_discover_avc_node (src);

    /* lets destroy handle and create one on port
       this is more reliable than setting port on
       the existing handle */
    raw1394_destroy_handle (src->handle);
    src->handle = raw1394_new_handle_on_port (src->port);
    if (!src->handle)
        goto cannot_set_port;

    raw1394_set_userdata (src->handle, src);
    raw1394_set_bus_reset_handler (src->handle, gst_hdv1394src_bus_reset);

    {
        nodeid_t m_node = (src->avc_node | 0xffc0);
        int m_channel = -1;
        int m_bandwidth = 0;
        int m_outputPort = -1;
        int m_inputPort = -1;

        m_channel = iec61883_cmp_connect (src->handle, m_node, &m_outputPort,
                                          raw1394_get_local_id (src->handle), &m_inputPort, &m_bandwidth);

        if (m_channel >= 0) {
            src->channel = m_channel;
        }
    }


    if ((src->iec61883mpeg2 =
                iec61883_mpeg2_recv_init (src->handle,
                                          gst_hdv1394src_iec61883_receive, src)) == NULL)
        goto cannot_initialise_dv;

#if 0
    raw1394_set_iso_handler (src->handle, src->channel,
                             gst_hdv1394src_iso_receive);
#endif

    GST_DEBUG_OBJECT (src, "successfully opened up 1394 connection");
    src->connected = TRUE;

    if (iec61883_mpeg2_recv_start (src->iec61883mpeg2, src->channel) != 0)
        goto cannot_start;
#if 0
    if (raw1394_start_iso_rcv (src->handle, src->channel) < 0)
        goto cannot_start;
#endif

    if (src->use_avc) {
        raw1394handle_t avc_handle = raw1394_new_handle_on_port (src->port);

        GST_LOG ("We have an avc_handle");

        /* start the VCR */
        if (avc_handle) {
            if (!avc1394_vcr_is_recording (avc_handle, src->avc_node)
                    && avc1394_vcr_is_playing (avc_handle, src->avc_node)
                    != AVC1394_VCR_OPERAND_PLAY_FORWARD) {
                GST_LOG ("Calling avc1394_vcr_play()");
                avc1394_vcr_play (avc_handle, src->avc_node);
            }
            raw1394_destroy_handle (avc_handle);
        } else {
            GST_WARNING_OBJECT (src, "Starting VCR via avc1394 failed: %s",
                                g_strerror (errno));
        }
    }

    return TRUE;

socket_pair:
    {
        GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
                           GST_ERROR_SYSTEM);
        return FALSE;
    }
permission_denied:
    {
        GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL), GST_ERROR_SYSTEM);
        return FALSE;
    }
not_found:
    {
        GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, (NULL), GST_ERROR_SYSTEM);
        return FALSE;
    }
no_handle:
    {
        GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
                           ("can't get raw1394 handle (%s)", g_strerror (errno)));
        return FALSE;
    }
no_ports:
    {
        raw1394_destroy_handle (src->handle);
        src->handle = NULL;
        GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, (NULL),
                           ("no ports available for raw1394"));
        return FALSE;
    }
cannot_set_port:
    {
        GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
                           ("can't set 1394 port %d", src->port));
        return FALSE;
    }
cannot_start:
    {
        raw1394_destroy_handle (src->handle);
        src->handle = NULL;
        iec61883_mpeg2_close (src->iec61883mpeg2);
        src->iec61883mpeg2 = NULL;
        GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
                           ("can't start 1394 iso receive"));
        return FALSE;
    }
cannot_initialise_dv:
    {
        raw1394_destroy_handle (src->handle);
        src->handle = NULL;
        GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
                           ("can't initialise iec61883 hdv"));
        return FALSE;
    }
}