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