leo_sobral@2: /* vim: set sw=2: -*- Mode: C; tab-width: 2; indent-tabs-mode: t; c-basic-offset: 2; c-indent-level: 2 -*- */ leo_sobral@2: /* GStreamer MythTV Plug-in leo_sobral@2: * Copyright (C) <2006> Rosfran Borges leo_sobral@2: * leo_sobral@2: * This library is free software; you can redistribute it and/or leo_sobral@2: * modify it under the terms of the GNU Library General Public leo_sobral@2: * License as published by the Free Software Foundation; either leo_sobral@2: * version 2 of the License, or (at your option) any later version. leo_sobral@2: * leo_sobral@2: * This library is distributed in the hope that it will be useful, leo_sobral@2: * but WITHOUT ANY WARRANTY; without even the implied warranty of leo_sobral@2: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU leo_sobral@2: * Library General Public License for more leo_sobral@2: */ leo_sobral@2: leo_sobral@2: #ifdef HAVE_CONFIG_H leo_sobral@2: #include "config.h" leo_sobral@2: #endif leo_sobral@2: leo_sobral@2: #include "gstmythtvsrc.h" leo_sobral@2: #include "myth_file_transfer.h" leo_sobral@2: #include "myth_livetv.h" leo_sobral@2: leo_sobral@2: #include leo_sobral@2: #include leo_sobral@2: leo_sobral@2: #include leo_sobral@2: #include leo_sobral@2: leo_sobral@2: GST_DEBUG_CATEGORY_STATIC (mythtvsrc_debug); leo_sobral@2: #define GST_CAT_DEFAULT mythtvsrc_debug leo_sobral@2: leo_sobral@2: #define GST_MYTHTV_ID_NUM 1 leo_sobral@2: leo_sobral@2: #define MYTHTV_VERSION_DEFAULT 30 leo_sobral@2: leo_sobral@2: #define MYTHTV_TRANSFER_MAX_WAITS 100 leo_sobral@2: leo_sobral@2: #define MYTHTV_TRANSFER_MAX_BUFFER ( 32*1024 ) leo_sobral@2: leo_sobral@2: /* 4*1024 ??? */ rosfran@5: #define MAX_READ_SIZE ( 16*1024 ) leo_sobral@2: leo_sobral@2: #define ENABLE_TIMING_POSITION 1 leo_sobral@2: leo_sobral@2: /* stablish a maximum iteration value to the IS_RECORDING message */ leo_sobral@2: static guint wait_to_transfer = 0; leo_sobral@2: leo_sobral@2: static const GstElementDetails gst_mythtv_src_details = leo_sobral@2: GST_ELEMENT_DETAILS ("MythTV client source", leo_sobral@2: "Source/Network", leo_sobral@2: "Control and receive data as a client over the network via raw socket connections using the MythTV protocol", leo_sobral@2: "Rosfran Borges "); leo_sobral@2: leo_sobral@2: static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src", leo_sobral@2: GST_PAD_SRC, leo_sobral@2: GST_PAD_ALWAYS, leo_sobral@2: GST_STATIC_CAPS_ANY); leo_sobral@2: leo_sobral@2: enum leo_sobral@2: { leo_sobral@2: PROP_0, leo_sobral@2: PROP_LOCATION, leo_sobral@2: PROP_URI, leo_sobral@2: #ifndef GST_DISABLE_GST_DEBUG leo_sobral@2: PROP_MYTHTV_DBG, leo_sobral@2: #endif leo_sobral@2: PROP_MYTHTV_VERSION, leo_sobral@2: PROP_MYTHTV_LIVE, leo_sobral@2: PROP_MYTHTV_LIVEID, leo_sobral@2: PROP_MYTHTV_LIVE_CHAINID leo_sobral@2: }; leo_sobral@2: leo_sobral@2: static void gst_mythtv_src_finalize (GObject * gobject); leo_sobral@2: leo_sobral@2: static GstFlowReturn gst_mythtv_src_create (GstBaseSrc * psrc, leo_sobral@2: guint64 offset, guint size, GstBuffer ** outbuf); leo_sobral@2: static gboolean gst_mythtv_src_start (GstBaseSrc * bsrc); leo_sobral@2: static gboolean gst_mythtv_src_stop (GstBaseSrc * bsrc); leo_sobral@2: static gboolean gst_mythtv_src_get_size (GstBaseSrc * bsrc, guint64 * size); leo_sobral@2: static gboolean gst_mythtv_src_is_seekable( GstBaseSrc *base_src ); leo_sobral@2: leo_sobral@2: static void gst_mythtv_src_set_property (GObject * object, guint prop_id, leo_sobral@2: const GValue * value, GParamSpec * pspec); leo_sobral@2: static void gst_mythtv_src_get_property (GObject * object, guint prop_id, leo_sobral@2: GValue * value, GParamSpec * pspec); leo_sobral@2: leo_sobral@2: static void leo_sobral@2: gst_mythtv_src_uri_handler_init (gpointer g_iface, gpointer iface_data); leo_sobral@2: leo_sobral@2: static gboolean leo_sobral@2: gst_mythtv_src_handle_event (GstPad * pad, GstEvent * event); leo_sobral@2: leo_sobral@2: static void leo_sobral@2: _urihandler_init (GType type) leo_sobral@2: { leo_sobral@2: static const GInterfaceInfo urihandler_info = { leo_sobral@2: gst_mythtv_src_uri_handler_init, leo_sobral@2: NULL, leo_sobral@2: NULL leo_sobral@2: }; leo_sobral@2: leo_sobral@2: g_type_add_interface_static (type, GST_TYPE_URI_HANDLER, &urihandler_info); leo_sobral@2: leo_sobral@2: GST_DEBUG_CATEGORY_INIT (mythtvsrc_debug, "mythtvsrc", 0, leo_sobral@2: "MythTV src"); leo_sobral@2: } leo_sobral@2: leo_sobral@2: GST_BOILERPLATE_FULL (GstMythtvSrc, gst_mythtv_src, GstBaseSrc, leo_sobral@2: GST_TYPE_BASE_SRC, _urihandler_init); leo_sobral@2: leo_sobral@2: static void leo_sobral@2: gst_mythtv_src_base_init (gpointer g_class) leo_sobral@2: { leo_sobral@2: GstElementClass *element_class = GST_ELEMENT_CLASS (g_class); leo_sobral@2: leo_sobral@2: gst_element_class_add_pad_template (element_class, leo_sobral@2: gst_static_pad_template_get (&srctemplate)); leo_sobral@2: leo_sobral@2: gst_element_class_set_details (element_class, &gst_mythtv_src_details); leo_sobral@2: } leo_sobral@2: leo_sobral@2: static void leo_sobral@2: gst_mythtv_src_class_init (GstMythtvSrcClass * klass) leo_sobral@2: { leo_sobral@2: GObjectClass *gobject_class; leo_sobral@2: GstBaseSrcClass *gstbasesrc_class; leo_sobral@2: leo_sobral@2: gobject_class = (GObjectClass *) klass; leo_sobral@2: gstbasesrc_class = (GstBaseSrcClass *) klass; leo_sobral@2: leo_sobral@2: gobject_class->set_property = gst_mythtv_src_set_property; leo_sobral@2: gobject_class->get_property = gst_mythtv_src_get_property; leo_sobral@2: gobject_class->finalize = gst_mythtv_src_finalize; leo_sobral@2: leo_sobral@2: g_object_class_install_property leo_sobral@2: (gobject_class, PROP_LOCATION, leo_sobral@2: g_param_spec_string ("location", "Location", leo_sobral@2: "The location. In the form:" leo_sobral@2: "\n\t\t\tmyth://a.com/file.nuv" leo_sobral@2: "\n\t\t\tmyth://a.com:23223/file.nuv " leo_sobral@2: "\n\t\t\ta.com/file.nuv - default scheme 'myth'", leo_sobral@2: "", G_PARAM_READWRITE)); leo_sobral@2: leo_sobral@2: g_object_class_install_property leo_sobral@2: (gobject_class, PROP_URI, leo_sobral@2: g_param_spec_string ("uri", "Uri", leo_sobral@2: "The location in form of a URI (deprecated; use location)", leo_sobral@2: "", G_PARAM_READWRITE)); leo_sobral@2: leo_sobral@2: g_object_class_install_property leo_sobral@2: (gobject_class, PROP_MYTHTV_VERSION, leo_sobral@2: g_param_spec_int ("mythtv-version", "mythtv-version", leo_sobral@2: "Change Myth TV version", leo_sobral@2: 26, 30, 26, G_PARAM_READWRITE)); leo_sobral@2: leo_sobral@2: g_object_class_install_property leo_sobral@2: (gobject_class, PROP_MYTHTV_LIVEID, leo_sobral@2: g_param_spec_int ("mythtv-live-id", "mythtv-live-id", leo_sobral@2: "Change Myth TV version", leo_sobral@2: 0, 200, GST_MYTHTV_ID_NUM, G_PARAM_READWRITE)); leo_sobral@2: leo_sobral@2: g_object_class_install_property leo_sobral@2: (gobject_class, PROP_MYTHTV_LIVE_CHAINID, leo_sobral@2: g_param_spec_string ("mythtv-live-chainid", "mythtv-live-chainid", leo_sobral@2: "Sets the Myth TV chain ID (from TV Chain)", leo_sobral@2: "", G_PARAM_READWRITE)); leo_sobral@2: leo_sobral@2: g_object_class_install_property leo_sobral@2: (gobject_class, PROP_MYTHTV_LIVE, leo_sobral@2: g_param_spec_boolean ("mythtv-live", "mythtv-live", leo_sobral@2: "Enable MythTV Live TV content streaming", leo_sobral@2: FALSE, G_PARAM_READWRITE)); leo_sobral@2: leo_sobral@2: #ifndef GST_DISABLE_GST_DEBUG leo_sobral@2: g_object_class_install_property leo_sobral@2: (gobject_class, PROP_MYTHTV_DBG, leo_sobral@2: g_param_spec_boolean ("mythtv-debug", "mythtv-debug", leo_sobral@2: "Enable MythTV debug messages", leo_sobral@2: FALSE, G_PARAM_READWRITE)); leo_sobral@2: #endif leo_sobral@2: leo_sobral@2: gstbasesrc_class->start = gst_mythtv_src_start; leo_sobral@2: gstbasesrc_class->stop = gst_mythtv_src_stop; leo_sobral@2: gstbasesrc_class->get_size = gst_mythtv_src_get_size; leo_sobral@2: gstbasesrc_class->is_seekable = gst_mythtv_src_is_seekable; leo_sobral@2: leo_sobral@2: gstbasesrc_class->create = gst_mythtv_src_create; leo_sobral@2: leo_sobral@2: leo_sobral@2: GST_DEBUG_CATEGORY_INIT (mythtvsrc_debug, "mythtvsrc", 0, leo_sobral@2: "MythTV Client Source"); leo_sobral@2: } leo_sobral@2: leo_sobral@2: static void leo_sobral@2: gst_mythtv_src_init (GstMythtvSrc * this, GstMythtvSrcClass * g_class) leo_sobral@2: { leo_sobral@2: this->file_transfer = NULL; leo_sobral@2: leo_sobral@2: this->unique_setup = FALSE; leo_sobral@2: leo_sobral@2: this->mythtv_version = MYTHTV_VERSION_DEFAULT; leo_sobral@2: leo_sobral@2: this->bytes_read = 0; leo_sobral@2: leo_sobral@2: this->content_size = -1; leo_sobral@2: this->read_offset = 0; leo_sobral@2: leo_sobral@2: this->live_tv = FALSE; leo_sobral@2: leo_sobral@2: this->user_agent = g_strdup ("mythtvsrc"); leo_sobral@2: this->mythtv_caps = NULL; leo_sobral@2: leo_sobral@2: gst_pad_set_event_function (GST_BASE_SRC_PAD(GST_BASE_SRC(this)), leo_sobral@2: GST_DEBUG_FUNCPTR (gst_mythtv_src_handle_event)); leo_sobral@2: leo_sobral@2: } leo_sobral@2: leo_sobral@2: static void leo_sobral@2: gst_mythtv_src_finalize (GObject * gobject) leo_sobral@2: { leo_sobral@2: GstMythtvSrc *this = GST_MYTHTV_SRC (gobject); leo_sobral@2: leo_sobral@2: g_free (this->user_agent); leo_sobral@2: leo_sobral@2: if (this->mythtv_caps) { leo_sobral@2: gst_caps_unref (this->mythtv_caps); leo_sobral@2: this->mythtv_caps = NULL; leo_sobral@2: } leo_sobral@2: leo_sobral@2: if (this->file_transfer) { leo_sobral@2: g_object_unref (this->file_transfer); leo_sobral@2: this->file_transfer = NULL; leo_sobral@2: } leo_sobral@2: leo_sobral@2: if (this->uri_name) { leo_sobral@2: g_free (this->uri_name); leo_sobral@2: } leo_sobral@2: leo_sobral@2: if (this->user_agent) { leo_sobral@2: g_free (this->user_agent); leo_sobral@2: } leo_sobral@2: leo_sobral@2: G_OBJECT_CLASS (parent_class)->finalize (gobject); leo_sobral@2: } leo_sobral@2: leo_sobral@2: #if 0 leo_sobral@2: static guint leo_sobral@2: do_seek( GstMythtvSrc *src, guint64 offset, guint size, GstBuffer *outbuf ) leo_sobral@2: { leo_sobral@2: guint64 off_uint64 = myth_file_transfer_seek(src->file_transfer, offset, 1); leo_sobral@2: leo_sobral@2: g_print( "[%s] Call MythTV SEEK with offset %llu, got a new one %llu...\n", __FUNCTION__, leo_sobral@2: offset, off_uint64 ); leo_sobral@2: leo_sobral@2: return off_uint64; leo_sobral@2: leo_sobral@2: } leo_sobral@2: #endif leo_sobral@2: leo_sobral@2: static guint leo_sobral@2: do_read_request_response (GstMythtvSrc * src, guint64 offset, guint size, GstBuffer * outbuf) leo_sobral@2: { leo_sobral@2: guint read = 0; leo_sobral@2: guint sizetoread = size; //GST_BUFFER_SIZE (outbuf); leo_sobral@2: leo_sobral@2: g_print( "[%s] Reading %d bytes...\n", __FUNCTION__, sizetoread ); leo_sobral@2: leo_sobral@2: /* Loop sending the request: leo_sobral@2: * Retry whilst authentication fails and we supply it. */ leo_sobral@2: leo_sobral@2: ssize_t len = 0; leo_sobral@2: leo_sobral@2: GST_OBJECT_LOCK(src); leo_sobral@2: leo_sobral@2: while ( sizetoread > 0 ) { leo_sobral@2: leo_sobral@2: len = myth_file_transfer_read( src->file_transfer, leo_sobral@2: GST_BUFFER_DATA (outbuf) + read, sizetoread, TRUE ); leo_sobral@2: leo_sobral@2: if ( len > 0 ) { leo_sobral@2: read += len; leo_sobral@2: src->read_offset += read; leo_sobral@2: sizetoread -= len; leo_sobral@2: } else if ( len < 0 ) { leo_sobral@2: goto done; leo_sobral@2: } leo_sobral@2: /*else if ( len == 0 ) { leo_sobral@2: goto eos; leo_sobral@2: }*/ leo_sobral@2: leo_sobral@2: if ( len == sizetoread ) leo_sobral@2: break; leo_sobral@2: leo_sobral@2: } leo_sobral@2: leo_sobral@2: if ( read > 0 ) { leo_sobral@2: src->bytes_read += read; leo_sobral@2: leo_sobral@2: GST_BUFFER_SIZE (outbuf) = read; leo_sobral@2: } else if ( read <= 0 || len <= 0 ) { leo_sobral@2: if ( src->live_tv == FALSE ) leo_sobral@2: goto eos; leo_sobral@2: else leo_sobral@2: goto done; leo_sobral@2: } leo_sobral@2: //GST_BUFFER_OFFSET (outbuf) = src->read_offset; leo_sobral@2: leo_sobral@2: g_print( "[%s]\tBYTES READ (actual) = %d, BYTES READ (cumulative) = %llu, "\ leo_sobral@2: "OFFSET = %llu, CONTENT SIZE = %llu.\n", __FUNCTION__, read, src->bytes_read, leo_sobral@2: src->read_offset, src->content_size ); leo_sobral@2: leo_sobral@2: GST_OBJECT_UNLOCK(src); leo_sobral@2: leo_sobral@2: if ( len < 0 ) { leo_sobral@2: read = len; leo_sobral@2: if ( src->live_tv == FALSE ) leo_sobral@2: goto eos; leo_sobral@2: else leo_sobral@2: goto done; leo_sobral@2: } leo_sobral@2: leo_sobral@2: if ( src->bytes_read < src->content_size ) leo_sobral@2: goto done; leo_sobral@2: leo_sobral@2: eos: leo_sobral@2: GST_OBJECT_UNLOCK(src); leo_sobral@2: leo_sobral@2: src->eos = TRUE; leo_sobral@2: done: leo_sobral@2: GST_OBJECT_UNLOCK(src); leo_sobral@2: leo_sobral@2: return read; leo_sobral@2: } leo_sobral@2: leo_sobral@2: static GstFlowReturn leo_sobral@2: gst_mythtv_src_create ( GstBaseSrc * psrc, guint64 offset, leo_sobral@2: guint size, GstBuffer **outbuf ) leo_sobral@2: { leo_sobral@2: GstMythtvSrc *src; leo_sobral@2: GstFlowReturn ret = GST_FLOW_OK; leo_sobral@2: guint read; leo_sobral@2: guint64 size_tmp = 0; leo_sobral@2: leo_sobral@2: src = GST_MYTHTV_SRC (psrc); leo_sobral@2: leo_sobral@2: g_print( "[%s]\tBUFFER OFFSET = %llu, BUFFER SIZE = %d.\n", __FUNCTION__, offset, leo_sobral@2: size ); leo_sobral@2: leo_sobral@2: leo_sobral@2: /* The caller should know the number of bytes and not read beyond EOS. */ leo_sobral@2: if (G_UNLIKELY (src->eos)) leo_sobral@2: goto eos; leo_sobral@2: leo_sobral@2: /* Create the buffer. */ leo_sobral@2: ret = gst_pad_alloc_buffer ( GST_BASE_SRC_PAD (GST_BASE_SRC (psrc)), leo_sobral@2: // GST_BUFFER_OFFSET_NONE, GST_BASE_SRC (psrc)->blocksize, leo_sobral@2: offset, size, leo_sobral@2: src->mythtv_caps ? src->mythtv_caps : leo_sobral@2: GST_PAD_CAPS (GST_BASE_SRC_PAD (GST_BASE_SRC (psrc))), outbuf ); leo_sobral@2: leo_sobral@2: if (G_UNLIKELY (ret == GST_FLOW_UNEXPECTED)) leo_sobral@2: goto eos; leo_sobral@2: leo_sobral@2: if (G_UNLIKELY (ret != GST_FLOW_OK)) leo_sobral@2: goto done; leo_sobral@2: leo_sobral@2: read = do_read_request_response ( src, offset, size, *outbuf ); leo_sobral@2: leo_sobral@2: #if ENABLE_TIMING_POSITION == 1 leo_sobral@2: if (src->live_tv == TRUE) { rosfran@5: //g_usleep( 1000 ); leo_sobral@2: get_file_pos: rosfran@5: //g_usleep( 100 ); leo_sobral@2: size_tmp = myth_file_transfer_get_file_position( src->file_transfer ); leo_sobral@2: if ( size_tmp > ( src->content_size + MYTHTV_TRANSFER_MAX_BUFFER ) ) leo_sobral@2: src->content_size = size_tmp; leo_sobral@2: else leo_sobral@2: goto get_file_pos; leo_sobral@2: g_print( "\t[%s]\tGET_POSITION: file_position = %llu\n", leo_sobral@2: __FUNCTION__, size_tmp); leo_sobral@2: leo_sobral@2: } leo_sobral@2: #endif leo_sobral@2: leo_sobral@2: if (G_UNLIKELY (read < 0)) leo_sobral@2: goto read_error; leo_sobral@2: leo_sobral@2: if (G_UNLIKELY(src->eos)) leo_sobral@2: goto eos; leo_sobral@2: leo_sobral@2: done: leo_sobral@2: return ret; leo_sobral@2: eos: leo_sobral@2: #if ENABLE_TIMING_POSITION == 1 rosfran@5: if ( src->live_tv == TRUE ) { rosfran@5: //g_usleep( 1000 ); leo_sobral@2: guint64 size_tmp = 0; leo_sobral@2: get_file_pos_eos: rosfran@5: //g_usleep( 100 ); leo_sobral@2: size_tmp = myth_file_transfer_get_file_position( src->file_transfer ); leo_sobral@2: if ( size_tmp > ( src->content_size + MYTHTV_TRANSFER_MAX_BUFFER ) ) leo_sobral@2: src->content_size = size_tmp; leo_sobral@2: else leo_sobral@2: goto get_file_pos_eos; leo_sobral@2: g_print( "\t[%s]\tGET_POSITION: file_position = %llu\n", leo_sobral@2: __FUNCTION__, size_tmp); leo_sobral@2: goto done; leo_sobral@2: } else leo_sobral@2: #endif leo_sobral@2: { leo_sobral@2: GST_DEBUG_OBJECT (src, "EOS reached"); leo_sobral@2: return GST_FLOW_UNEXPECTED; leo_sobral@2: } leo_sobral@2: /* ERRORS */ leo_sobral@2: read_error: leo_sobral@2: { leo_sobral@2: GST_ELEMENT_ERROR (src, RESOURCE, READ, leo_sobral@2: (NULL), ("Could not read any bytes (%i, %s)", read, leo_sobral@2: src->uri_name)); leo_sobral@2: return GST_FLOW_ERROR; leo_sobral@2: } leo_sobral@2: } leo_sobral@2: leo_sobral@2: #if 0 leo_sobral@2: /* The following two charset mangling functions were copied from gnomevfssrc. leo_sobral@2: * Preserve them under the unverified assumption that they do something vaguely leo_sobral@2: * worthwhile. leo_sobral@2: */ leo_sobral@2: static char * leo_sobral@2: unicodify (const char *str, int len, ...) leo_sobral@2: { leo_sobral@2: char *ret = NULL, *cset; leo_sobral@2: va_list args; leo_sobral@2: gsize bytes_read, bytes_written; leo_sobral@2: leo_sobral@2: if (g_utf8_validate (str, len, NULL)) leo_sobral@2: return g_strndup (str, len >= 0 ? len : strlen (str)); leo_sobral@2: leo_sobral@2: va_start (args, len); leo_sobral@2: while ((cset = va_arg (args, char *)) != NULL) leo_sobral@2: { leo_sobral@2: if (!strcmp (cset, "locale")) leo_sobral@2: ret = g_locale_to_utf8 (str, len, &bytes_read, &bytes_written, NULL); leo_sobral@2: else leo_sobral@2: ret = g_convert (str, len, "UTF-8", cset, leo_sobral@2: &bytes_read, &bytes_written, NULL); leo_sobral@2: if (ret) leo_sobral@2: break; leo_sobral@2: } leo_sobral@2: va_end (args); leo_sobral@2: leo_sobral@2: return ret; leo_sobral@2: } leo_sobral@2: leo_sobral@2: static char * leo_sobral@2: gst_mythtv_src_unicodify (const char *str) leo_sobral@2: { leo_sobral@2: return unicodify (str, -1, "locale", "ISO-8859-1", NULL); leo_sobral@2: } leo_sobral@2: #endif leo_sobral@2: leo_sobral@2: /* create a socket for connecting to remote server */ leo_sobral@2: static gboolean leo_sobral@2: gst_mythtv_src_start ( GstBaseSrc * bsrc ) leo_sobral@2: { leo_sobral@2: GstMythtvSrc *src = GST_MYTHTV_SRC (bsrc); leo_sobral@2: leo_sobral@2: GString *chain_id_local = NULL; leo_sobral@2: leo_sobral@2: gboolean ret = TRUE; leo_sobral@2: #if 0 leo_sobral@2: if (src->live_tv == TRUE && src->file_transfer != NULL) { leo_sobral@2: guint64 size_tmp = myth_file_transfer_get_file_position( src->file_transfer ); leo_sobral@2: if (size_tmp > src->content_size) leo_sobral@2: src->content_size = size_tmp; leo_sobral@2: g_print( "\t[%s]\tGET_POSITION: file_position = %llu\n", leo_sobral@2: __FUNCTION__, size_tmp); leo_sobral@2: } leo_sobral@2: #endif leo_sobral@2: if (src->unique_setup == FALSE) { leo_sobral@2: src->unique_setup = TRUE; leo_sobral@2: } else { leo_sobral@2: goto done; leo_sobral@2: } leo_sobral@2: leo_sobral@2: GST_OBJECT_LOCK(src); leo_sobral@2: leo_sobral@2: if ( src->live_tv ) { leo_sobral@2: src->spawn_livetv = myth_livetv_new( ); leo_sobral@2: if ( myth_livetv_setup( src->spawn_livetv ) == FALSE ) { leo_sobral@2: ret = FALSE; leo_sobral@2: goto init_failed; leo_sobral@2: } leo_sobral@2: /* set up the uri variable */ leo_sobral@2: src->uri_name = g_strdup( src->spawn_livetv->proginfo->pathname->str ); leo_sobral@2: chain_id_local = gmyth_tvchain_get_id( src->spawn_livetv->tvchain ); leo_sobral@2: if ( chain_id_local != NULL ) { leo_sobral@2: src->live_chain_id = g_strdup( chain_id_local->str ); leo_sobral@2: g_print( "\t[%s]\tLocal chain ID = %s.\n", __FUNCTION__, src->live_chain_id ); leo_sobral@2: } leo_sobral@2: src->live_tv_id = src->spawn_livetv->remote_encoder->recorder_num; leo_sobral@2: g_print ( "[%s] LiveTV id = %d, URI path = %s.\n", __FUNCTION__, src->live_tv_id, src->uri_name ); leo_sobral@2: } leo_sobral@2: leo_sobral@2: src->file_transfer = myth_file_transfer_new( src->live_tv_id, leo_sobral@2: g_string_new( src->uri_name ), -1, src->mythtv_version ); leo_sobral@2: leo_sobral@2: if ( src->file_transfer == NULL ) { leo_sobral@2: GST_OBJECT_UNLOCK(src); leo_sobral@2: leo_sobral@2: goto init_failed; leo_sobral@2: } leo_sobral@2: leo_sobral@2: if ( src->live_tv ) { leo_sobral@2: g_print ( "[%s] GST MYTHTVSRC: live_chain_id = %s\n", __FUNCTION__, src->live_chain_id ); leo_sobral@2: /* sets the MythSocket to the FileTransfer */ leo_sobral@2: //ret = myth_file_transfer_livetv_setup( &(src->file_transfer), src->spawn_livetv->remote_encoder->myth_socket ); leo_sobral@2: } leo_sobral@2: /* sets the Playback monitor connection */ leo_sobral@2: ret = myth_file_transfer_playback_setup( &(src->file_transfer), src->live_tv ); leo_sobral@2: leo_sobral@2: if ( src->live_tv == TRUE && ret == TRUE ) { leo_sobral@2: /* loop finished, set the max tries variable to zero again... */ leo_sobral@2: wait_to_transfer = 0; leo_sobral@2: leo_sobral@2: while ( wait_to_transfer++ < MYTHTV_TRANSFER_MAX_WAITS && ( myth_file_transfer_is_recording( src->file_transfer ) == FALSE leo_sobral@2: /*|| ( myth_file_transfer_get_file_position( src->file_transfer ) < ( src->content_size + 327680 ) )*/ ) ) leo_sobral@2: g_usleep( 100 ); leo_sobral@2: } leo_sobral@2: leo_sobral@2: /* sets the FileTransfer instance connection (video/audio download) */ leo_sobral@2: ret = myth_file_transfer_setup( &(src->file_transfer), src->live_tv ); leo_sobral@2: leo_sobral@2: if ( ret == FALSE ) { leo_sobral@2: GST_OBJECT_UNLOCK(src); leo_sobral@2: #ifndef GST_DISABLE_GST_DEBUG leo_sobral@2: if ( src->mythtv_msgs_dbg ) leo_sobral@2: g_printerr( "MythTV FileTransfer request failed when setting up socket connection!\n" ); leo_sobral@2: #endif leo_sobral@2: goto begin_req_failed; leo_sobral@2: } leo_sobral@2: leo_sobral@2: src->content_size = src->file_transfer->filesize; leo_sobral@2: leo_sobral@2: GST_OBJECT_UNLOCK(src); leo_sobral@2: leo_sobral@2: #if 0 leo_sobral@2: const char *str_value; leo_sobral@2: gint gint_value; leo_sobral@2: leo_sobral@2: str_value = ne_get_response_header (src->request, "myth-metaint"); leo_sobral@2: if (str_value) { leo_sobral@2: if ( sscanf (str_value, "%d", &gint_value) == 1 ) { leo_sobral@2: if (src->myth_caps) { leo_sobral@2: gst_caps_unref (src->myth_caps); leo_sobral@2: src->myth_caps = NULL; leo_sobral@2: } leo_sobral@2: src->myth_metaint = gint_value; leo_sobral@2: #endif leo_sobral@2: //src->mythtv_caps = gst_caps_new_simple ("application/x-gst_ff-nuv", NULL); leo_sobral@2: // } leo_sobral@2: // } leo_sobral@2: done: leo_sobral@2: return TRUE; leo_sobral@2: leo_sobral@2: /* ERRORS */ leo_sobral@2: init_failed: leo_sobral@2: { leo_sobral@2: if (src->spawn_livetv != NULL ) leo_sobral@2: g_object_unref( src->spawn_livetv ); leo_sobral@2: leo_sobral@2: GST_ELEMENT_ERROR (src, LIBRARY, INIT, leo_sobral@2: (NULL), ("Could not initialize MythTV library (%i, %s)", ret, src->uri_name)); leo_sobral@2: return FALSE; leo_sobral@2: } leo_sobral@2: begin_req_failed: leo_sobral@2: { leo_sobral@2: GST_ELEMENT_ERROR (src, LIBRARY, INIT, leo_sobral@2: (NULL), ("Could not begin request sent to MythTV server (%i, %s)", ret, src->uri_name)); leo_sobral@2: return FALSE; leo_sobral@2: } leo_sobral@2: } leo_sobral@2: leo_sobral@2: static gboolean leo_sobral@2: gst_mythtv_src_get_size (GstBaseSrc * bsrc, guint64 * size) leo_sobral@2: { leo_sobral@2: GstMythtvSrc *src; leo_sobral@2: leo_sobral@2: src = GST_MYTHTV_SRC (bsrc); leo_sobral@2: #if ENABLE_TIMING_POSITION == 1 rosfran@5: guint64 size_tmp = 0; leo_sobral@2: if (src->live_tv == TRUE) { leo_sobral@2: get_file_pos: rosfran@5: //g_usleep( 100 ); rosfran@5: size_tmp = myth_file_transfer_get_file_position( src->file_transfer ); leo_sobral@2: if ( size_tmp > ( src->content_size + MYTHTV_TRANSFER_MAX_BUFFER ) ) leo_sobral@2: src->content_size = size_tmp; leo_sobral@2: else leo_sobral@2: goto get_file_pos; leo_sobral@2: g_print( "\t[%s]\tGET_POSITION: file_position = %llu\n", leo_sobral@2: __FUNCTION__, size_tmp); leo_sobral@2: } leo_sobral@2: #endif leo_sobral@2: if (src->content_size <= 0) leo_sobral@2: return FALSE; leo_sobral@2: leo_sobral@2: *size = src->content_size; leo_sobral@2: leo_sobral@2: return TRUE; leo_sobral@2: } leo_sobral@2: leo_sobral@2: /* close the socket and associated resources leo_sobral@2: * used both to recover from errors and go to NULL state */ leo_sobral@2: static gboolean leo_sobral@2: gst_mythtv_src_stop (GstBaseSrc * bsrc) leo_sobral@2: { leo_sobral@2: GstMythtvSrc *src; leo_sobral@2: leo_sobral@2: src = GST_MYTHTV_SRC (bsrc); leo_sobral@2: leo_sobral@2: if (src->uri_name) { leo_sobral@2: g_free (src->uri_name); leo_sobral@2: src->uri_name = NULL; leo_sobral@2: } leo_sobral@2: leo_sobral@2: if (src->mythtv_caps) { leo_sobral@2: gst_caps_unref (src->mythtv_caps); leo_sobral@2: src->mythtv_caps = NULL; leo_sobral@2: } leo_sobral@2: leo_sobral@2: src->eos = FALSE; leo_sobral@2: leo_sobral@2: return TRUE; leo_sobral@2: } leo_sobral@2: leo_sobral@2: static gboolean leo_sobral@2: gst_mythtv_src_handle_event (GstPad * pad, GstEvent * event) leo_sobral@2: { leo_sobral@2: GstMythtvSrc *src = GST_MYTHTV_SRC (GST_PAD_PARENT (pad)); leo_sobral@2: leo_sobral@2: switch (GST_EVENT_TYPE (event)) { leo_sobral@2: case GST_EVENT_FLUSH_START: leo_sobral@2: src->eos = FALSE; leo_sobral@2: break; leo_sobral@2: //return TRUE; leo_sobral@2: case GST_EVENT_FLUSH_STOP: leo_sobral@2: src->do_start = TRUE; leo_sobral@2: src->eos = FALSE; leo_sobral@2: gst_element_set_state (GST_ELEMENT(src), GST_STATE_NULL); leo_sobral@2: gst_element_set_locked_state (GST_ELEMENT(src), TRUE); leo_sobral@2: break; leo_sobral@2: case GST_EVENT_SEEK: leo_sobral@2: { leo_sobral@2: gdouble rate; leo_sobral@2: //gboolean update = TRUE; leo_sobral@2: GstFormat format; leo_sobral@2: GstSeekType cur_type, stop_type; leo_sobral@2: GstSeekFlags flags; leo_sobral@2: gint64 cur = 0, stop = 0; leo_sobral@2: gst_event_parse_seek ( event, &rate, &format, leo_sobral@2: &flags, &cur_type, &cur, leo_sobral@2: &stop_type, &stop ); leo_sobral@2: leo_sobral@2: g_print( "[%s] Got EVENT_SEEK.\n", __FUNCTION__ ); leo_sobral@2: if ( !( flags & GST_SEEK_FLAG_FLUSH ) ) { leo_sobral@2: g_print( "[%s] Could get the FLAG_FLUSH message.\n", __FUNCTION__ ); leo_sobral@2: } leo_sobral@2: //gboolean ret = gst_event_parse_new_segment ( event, leo_sobral@2: // &update, &rate, &format, &start, &stop, leo_sobral@2: // &position ); leo_sobral@2: //GstFlowReturn flow_ret = gst_mythtv_src_create (GST_BASE_SRC( GST_PAD_PARENT( psrc ) ), leo_sobral@2: // cur, stop - cur + 1, GstBuffer) leo_sobral@2: leo_sobral@2: } leo_sobral@2: default: leo_sobral@2: return gst_pad_event_default (pad, event); leo_sobral@2: } leo_sobral@2: leo_sobral@2: return gst_pad_event_default (pad, event); leo_sobral@2: } leo_sobral@2: leo_sobral@2: static gboolean leo_sobral@2: gst_mythtv_src_is_seekable( GstBaseSrc *base_src ) leo_sobral@2: { leo_sobral@2: return TRUE; leo_sobral@2: } leo_sobral@2: leo_sobral@2: static void leo_sobral@2: gst_mythtv_src_set_property (GObject * object, guint prop_id, leo_sobral@2: const GValue * value, GParamSpec * pspec) leo_sobral@2: { leo_sobral@2: GstMythtvSrc *mythtvsrc = GST_MYTHTV_SRC (object); leo_sobral@2: leo_sobral@2: GST_OBJECT_LOCK (mythtvsrc); leo_sobral@2: switch (prop_id) { leo_sobral@2: case PROP_URI: leo_sobral@2: case PROP_LOCATION: leo_sobral@2: { leo_sobral@2: if (!g_value_get_string (value)) { leo_sobral@2: GST_WARNING ("location property cannot be NULL"); leo_sobral@2: goto done; leo_sobral@2: } leo_sobral@2: leo_sobral@2: if (mythtvsrc->uri_name != NULL) { leo_sobral@2: g_free (mythtvsrc->uri_name); leo_sobral@2: mythtvsrc->uri_name = NULL; leo_sobral@2: } leo_sobral@2: mythtvsrc->uri_name = g_value_dup_string (value); leo_sobral@2: leo_sobral@2: break; leo_sobral@2: } leo_sobral@2: #ifndef GST_DISABLE_GST_DEBUG leo_sobral@2: case PROP_MYTHTV_DBG: leo_sobral@2: { leo_sobral@2: mythtvsrc->mythtv_msgs_dbg = g_value_get_boolean (value); leo_sobral@2: break; leo_sobral@2: } leo_sobral@2: #endif leo_sobral@2: case PROP_MYTHTV_VERSION: leo_sobral@2: { leo_sobral@2: mythtvsrc->mythtv_version = g_value_get_int (value); leo_sobral@2: break; leo_sobral@2: } leo_sobral@2: case PROP_MYTHTV_LIVEID: leo_sobral@2: { leo_sobral@2: mythtvsrc->live_tv_id = g_value_get_int (value); leo_sobral@2: break; leo_sobral@2: } leo_sobral@2: case PROP_MYTHTV_LIVE: leo_sobral@2: { leo_sobral@2: mythtvsrc->live_tv = g_value_get_boolean (value); leo_sobral@2: break; leo_sobral@2: } leo_sobral@2: case PROP_MYTHTV_LIVE_CHAINID: leo_sobral@2: { leo_sobral@2: if (!g_value_get_string (value)) { leo_sobral@2: GST_WARNING ("MythTV Live chainid property cannot be NULL"); leo_sobral@2: goto done; leo_sobral@2: } leo_sobral@2: leo_sobral@2: if (mythtvsrc->live_chain_id != NULL) { leo_sobral@2: g_free (mythtvsrc->live_chain_id); leo_sobral@2: mythtvsrc->live_chain_id = NULL; leo_sobral@2: } leo_sobral@2: mythtvsrc->live_chain_id = g_value_dup_string (value); leo_sobral@2: leo_sobral@2: break; leo_sobral@2: } leo_sobral@2: leo_sobral@2: default: leo_sobral@2: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); leo_sobral@2: break; leo_sobral@2: } leo_sobral@2: GST_OBJECT_UNLOCK (mythtvsrc); leo_sobral@2: done: leo_sobral@2: return; leo_sobral@2: } leo_sobral@2: leo_sobral@2: static void leo_sobral@2: gst_mythtv_src_get_property (GObject * object, guint prop_id, leo_sobral@2: GValue * value, GParamSpec * pspec) leo_sobral@2: { leo_sobral@2: GstMythtvSrc *mythtvsrc = GST_MYTHTV_SRC (object); leo_sobral@2: leo_sobral@2: GST_OBJECT_LOCK (mythtvsrc); leo_sobral@2: switch (prop_id) { leo_sobral@2: case PROP_URI: leo_sobral@2: case PROP_LOCATION: leo_sobral@2: { leo_sobral@2: gchar *str = g_strdup( "" ); leo_sobral@2: leo_sobral@2: if ( mythtvsrc->uri_name == NULL ) { leo_sobral@2: g_free (mythtvsrc->uri_name); leo_sobral@2: mythtvsrc->uri_name = NULL; leo_sobral@2: } else { leo_sobral@2: str = g_strdup( mythtvsrc->uri_name ); leo_sobral@2: } leo_sobral@2: g_value_set_string ( value, str ); leo_sobral@2: break; leo_sobral@2: } leo_sobral@2: #ifndef GST_DISABLE_GST_DEBUG leo_sobral@2: case PROP_MYTHTV_DBG: leo_sobral@2: g_value_set_boolean ( value, mythtvsrc->mythtv_msgs_dbg ); leo_sobral@2: break; leo_sobral@2: #endif leo_sobral@2: case PROP_MYTHTV_VERSION: leo_sobral@2: { leo_sobral@2: g_value_set_int ( value, mythtvsrc->mythtv_version ); leo_sobral@2: break; leo_sobral@2: } leo_sobral@2: case PROP_MYTHTV_LIVEID: leo_sobral@2: { leo_sobral@2: g_value_set_int ( value, mythtvsrc->live_tv_id ); leo_sobral@2: break; leo_sobral@2: } leo_sobral@2: case PROP_MYTHTV_LIVE: leo_sobral@2: g_value_set_boolean ( value, mythtvsrc->live_tv ); leo_sobral@2: break; leo_sobral@2: case PROP_MYTHTV_LIVE_CHAINID: leo_sobral@2: { leo_sobral@2: gchar *str = g_strdup( "" ); leo_sobral@2: leo_sobral@2: if ( mythtvsrc->live_chain_id == NULL ) { leo_sobral@2: g_free (mythtvsrc->live_chain_id); leo_sobral@2: mythtvsrc->live_chain_id = NULL; leo_sobral@2: } else { leo_sobral@2: str = g_strdup( mythtvsrc->live_chain_id ); leo_sobral@2: } leo_sobral@2: g_value_set_string ( value, str ); leo_sobral@2: break; leo_sobral@2: } leo_sobral@2: default: leo_sobral@2: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); leo_sobral@2: break; leo_sobral@2: } leo_sobral@2: GST_OBJECT_UNLOCK (mythtvsrc); leo_sobral@2: } leo_sobral@2: leo_sobral@2: /* entry point to initialize the plug-in leo_sobral@2: * initialize the plug-in itself leo_sobral@2: * register the element factories and pad templates leo_sobral@2: * register the features leo_sobral@2: */ leo_sobral@2: static gboolean leo_sobral@2: plugin_init (GstPlugin * plugin) leo_sobral@2: { leo_sobral@2: return gst_element_register (plugin, "mythtvsrc", GST_RANK_NONE, leo_sobral@2: GST_TYPE_MYTHTV_SRC); leo_sobral@2: } leo_sobral@2: leo_sobral@2: /* this is the structure that gst-register looks for leo_sobral@2: * so keep the name plugin_desc, or you cannot get your plug-in registered */ leo_sobral@2: GST_PLUGIN_DEFINE (GST_VERSION_MAJOR, leo_sobral@2: GST_VERSION_MINOR, leo_sobral@2: "mythtv", leo_sobral@2: "lib MythTV src", leo_sobral@2: plugin_init, VERSION, "LGPL", "GStreamer", "http://gstreamer.net/") leo_sobral@2: leo_sobral@2: leo_sobral@2: /*** GSTURIHANDLER INTERFACE *************************************************/ leo_sobral@2: static guint leo_sobral@2: gst_mythtv_src_uri_get_type (void) leo_sobral@2: { leo_sobral@2: return GST_URI_SRC; leo_sobral@2: } leo_sobral@2: leo_sobral@2: static gchar ** leo_sobral@2: gst_mythtv_src_uri_get_protocols (void) leo_sobral@2: { leo_sobral@2: static gchar *protocols[] = { "myth", "myths", NULL }; leo_sobral@2: leo_sobral@2: return protocols; leo_sobral@2: } leo_sobral@2: leo_sobral@2: static const gchar * leo_sobral@2: gst_mythtv_src_uri_get_uri (GstURIHandler * handler) leo_sobral@2: { leo_sobral@2: GstMythtvSrc *src = GST_MYTHTV_SRC (handler); leo_sobral@2: leo_sobral@2: return src->uri_name; leo_sobral@2: } leo_sobral@2: leo_sobral@2: static gboolean leo_sobral@2: gst_mythtv_src_uri_set_uri (GstURIHandler * handler, const gchar * uri) leo_sobral@2: { leo_sobral@2: GstMythtvSrc *src = GST_MYTHTV_SRC (handler); leo_sobral@2: leo_sobral@2: gchar *protocol; leo_sobral@2: leo_sobral@2: protocol = gst_uri_get_protocol (uri); leo_sobral@2: if ((strcmp (protocol, "myth") != 0) && (strcmp (protocol, "myths") != 0)) { leo_sobral@2: g_free (protocol); leo_sobral@2: return FALSE; leo_sobral@2: } leo_sobral@2: g_free (protocol); leo_sobral@2: g_object_set (src, "location", uri, NULL); leo_sobral@2: leo_sobral@2: return TRUE; leo_sobral@2: } leo_sobral@2: leo_sobral@2: static void leo_sobral@2: gst_mythtv_src_uri_handler_init (gpointer g_iface, gpointer iface_data) leo_sobral@2: { leo_sobral@2: GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface; leo_sobral@2: leo_sobral@2: iface->get_type = gst_mythtv_src_uri_get_type; leo_sobral@2: iface->get_protocols = gst_mythtv_src_uri_get_protocols; leo_sobral@2: iface->get_uri = gst_mythtv_src_uri_get_uri; leo_sobral@2: iface->set_uri = gst_mythtv_src_uri_set_uri; leo_sobral@2: } leo_sobral@2: leo_sobral@2: void leo_sobral@2: size_header_handler (void *userdata, const char *value) leo_sobral@2: { leo_sobral@2: GstMythtvSrc *src = GST_MYTHTV_SRC (userdata); leo_sobral@2: leo_sobral@2: //src->content_size = g_ascii_strtoull (value, NULL, 10); leo_sobral@2: leo_sobral@2: GST_DEBUG_OBJECT (src, "content size = %lld bytes", src->content_size); leo_sobral@2: }