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 rosfran@30: * modify it under the terms of the GNU Library Lesser General rosfran@30: * Public 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: */ rosfran@124: /** rosfran@124: * When using the LiveTV content, put the location URI in the following rosfran@124: * format: rosfran@124: * rosfran@124: * myth://mythtv:mythtv@xxx.xxx.xxx.xxx:6543/#mythconverg rosfran@124: * rosfran@124: * Where the first field is the protocol (myth), the second and third are user rosfran@124: * name (mythtv) and password (mythtv), then backend host name and port number, rosfran@124: * and the last field is the database name (mythconverg). rosfran@124: */ 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" rosfran@52: #include rosfran@52: #include 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: rosfran@95: #define GST_GMYTHTV_ID_NUM 1 rosfran@70: rosfran@70: #define GST_GMYTHTV_CHANNEL_NUM 1000 leo_sobral@2: rosfran@52: #define GMYTHTV_VERSION_DEFAULT 30 rosfran@61: rosfran@90: #define GMYTHTV_TRANSFER_MAX_WAITS 100 leo_sobral@2: rosfran@102: #define GMYTHTV_TRANSFER_MAX_RESENDS 5 rosfran@102: rosfran@90: #define GMYTHTV_TRANSFER_MAX_BUFFER 128*1024 rosfran@30: //( 32*1024 ) leo_sobral@2: leo_sobral@2: /* 4*1024 ??? */ rosfran@98: #define MAX_READ_SIZE 4*1024 rosfran@30: //( 32*1024 ) leo_sobral@2: rosfran@98: #define GST_FLOW_ERROR_NO_DATA -101 rosfran@70: rosfran@124: #define REQUEST_MAX_SIZE 64*1024 rosfran@124: rosfran@142: #define INTERNAL_BUFFER_SIZE 90*1024 rosfran@90: 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 = rosfran@30: 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", rosfran@30: "Rosfran Borges " ); leo_sobral@2: rosfran@81: static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ( "src", leo_sobral@2: GST_PAD_SRC, leo_sobral@2: GST_PAD_ALWAYS, rosfran@30: GST_STATIC_CAPS ("video/x-nuv") ); rosfran@81: 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 rosfran@40: PROP_GMYTHTV_DBG, leo_sobral@2: #endif rosfran@40: PROP_GMYTHTV_VERSION, rosfran@40: PROP_GMYTHTV_LIVE, rosfran@40: PROP_GMYTHTV_LIVEID, rosfran@52: PROP_GMYTHTV_LIVE_CHAINID, rosfran@70: PROP_GMYTHTV_ENABLE_TIMING_POSITION, rosfran@70: PROP_GMYTHTV_CHANNEL_NUM leo_sobral@2: }; leo_sobral@2: leo_sobral@2: static void gst_mythtv_src_finalize (GObject * gobject); leo_sobral@2: rosfran@100: static GstFlowReturn gst_mythtv_src_create ( GstPushSrc* psrc, GstBuffer** outbuf ); rosfran@52: 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); rosfran@87: static gboolean gst_mythtv_src_is_seekable( GstBaseSrc *push_src ); rosfran@87: rosfran@106: /* rosfran@106: static void gst_mythtv_src_get_times (GstBaseSrc * src, GstBuffer * buffer, rosfran@106: GstClockTime * start, GstClockTime * end); rosfran@106: */ rosfran@98: rosfran@100: static gboolean gst_mythtv_src_do_seek( GstBaseSrc *base, GstSegment *segment ); rosfran@52: rosfran@52: static gboolean gst_mythtv_src_next_program_chain ( GstMythtvSrc *src ); rosfran@52: rosfran@52: static GstStateChangeReturn rosfran@52: gst_mythtv_src_change_state (GstElement * element, GstStateChange transition); 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: rosfran@30: static void gst_mythtv_src_uri_handler_init (gpointer g_iface, gpointer iface_data); rosfran@30: rosfran@87: static gboolean gst_mythtv_src_handle_query (GstPad * pad, GstQuery * query); rosfran@87: rosfran@90: static gboolean gst_mythtv_src_handle_event (GstPad * pad, GstEvent * event); rosfran@30: rosfran@102: static gint do_read_request_response (GstMythtvSrc * src, guint size, GByteArray *data_ptr); rosfran@90: rosfran@90: GStaticRecMutex th_mutex = G_STATIC_REC_MUTEX_INIT; leo_sobral@2: rosfran@52: 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: rosfran@100: GST_BOILERPLATE_FULL (GstMythtvSrc, gst_mythtv_src, GstPushSrc, rosfran@100: GST_TYPE_PUSH_SRC, _urihandler_init) rosfran@30: rosfran@87: 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)); rosfran@81: leo_sobral@2: gst_element_class_set_details (element_class, &gst_mythtv_src_details); rosfran@52: rosfran@52: element_class->change_state = gst_mythtv_src_change_state; rosfran@81: leo_sobral@2: } leo_sobral@2: rosfran@30: static void leo_sobral@2: gst_mythtv_src_class_init (GstMythtvSrcClass * klass) leo_sobral@2: { rosfran@63: GObjectClass *gobject_class; rosfran@100: GstPushSrcClass *gstpushsrc_class; leo_sobral@2: GstBaseSrcClass *gstbasesrc_class; leo_sobral@2: leo_sobral@2: gobject_class = (GObjectClass *) klass; leo_sobral@2: gstbasesrc_class = (GstBaseSrcClass *) klass; rosfran@100: gstpushsrc_class = (GstPushSrcClass *) 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 rosfran@40: (gobject_class, PROP_GMYTHTV_VERSION, leo_sobral@2: g_param_spec_int ("mythtv-version", "mythtv-version", rosfran@70: "Change MythTV version", leo_sobral@2: 26, 30, 26, G_PARAM_READWRITE)); leo_sobral@2: leo_sobral@2: g_object_class_install_property rosfran@40: (gobject_class, PROP_GMYTHTV_LIVEID, leo_sobral@2: g_param_spec_int ("mythtv-live-id", "mythtv-live-id", rosfran@70: "Change MythTV version", rosfran@40: 0, 200, GST_GMYTHTV_ID_NUM, G_PARAM_READWRITE)); leo_sobral@2: leo_sobral@2: g_object_class_install_property rosfran@40: (gobject_class, PROP_GMYTHTV_LIVE_CHAINID, leo_sobral@2: g_param_spec_string ("mythtv-live-chainid", "mythtv-live-chainid", rosfran@70: "Sets the MythTV chain ID (from TV Chain)", leo_sobral@2: "", G_PARAM_READWRITE)); leo_sobral@2: leo_sobral@2: g_object_class_install_property rosfran@40: (gobject_class, PROP_GMYTHTV_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: rosfran@52: g_object_class_install_property rosfran@52: (gobject_class, PROP_GMYTHTV_ENABLE_TIMING_POSITION, rosfran@52: g_param_spec_boolean ("mythtv-enable-timing-position", "mythtv-enable-timing-position", rosfran@52: "Enable MythTV Live TV content size continuous updating", rosfran@52: FALSE, G_PARAM_READWRITE)); rosfran@70: rosfran@70: g_object_class_install_property rosfran@70: (gobject_class, PROP_GMYTHTV_CHANNEL_NUM, rosfran@70: g_param_spec_int ("mythtv-channel", "mythtv-channel", rosfran@70: "Change MythTV channel number", rosfran@71: 0, 99999, GST_GMYTHTV_CHANNEL_NUM, G_PARAM_READWRITE)); rosfran@52: leo_sobral@2: #ifndef GST_DISABLE_GST_DEBUG leo_sobral@2: g_object_class_install_property rosfran@40: (gobject_class, PROP_GMYTHTV_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; rosfran@87: gstbasesrc_class->is_seekable = gst_mythtv_src_is_seekable; rosfran@81: rosfran@98: //gstbasesrc_class->get_times = gst_mythtv_src_get_times; rosfran@98: rosfran@100: gstbasesrc_class->do_seek = gst_mythtv_src_do_seek; rosfran@100: gstpushsrc_class->create = gst_mythtv_src_create; rosfran@52: leo_sobral@2: GST_DEBUG_CATEGORY_INIT (mythtvsrc_debug, "mythtvsrc", 0, leo_sobral@2: "MythTV Client Source"); leo_sobral@2: } leo_sobral@2: rosfran@30: 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: rosfran@40: this->mythtv_version = GMYTHTV_VERSION_DEFAULT; rosfran@70: rosfran@70: this->state = GST_MYTHTV_SRC_FILE_TRANSFER; leo_sobral@2: leo_sobral@2: this->bytes_read = 0; rosfran@63: rosfran@63: this->prev_content_size = 0; leo_sobral@2: rosfran@52: this->content_size = 0; leo_sobral@2: this->read_offset = 0; leo_sobral@2: rosfran@61: this->content_size_last = 0; rosfran@61: leo_sobral@2: this->live_tv = FALSE; rosfran@52: rosfran@52: this->enable_timing_position = FALSE; rosfran@52: this->update_prog_chain = FALSE; leo_sobral@2: leo_sobral@2: this->user_agent = g_strdup ("mythtvsrc"); rosfran@52: this->mythtv_caps = NULL; rosfran@52: this->update_prog_chain = FALSE; rosfran@52: rosfran@70: this->channel_num = 0; rosfran@70: rosfran@52: this->eos = FALSE; rosfran@30: rosfran@98: this->bytes_queue = NULL; rosfran@70: rosfran@81: this->th_mutex = NULL; rosfran@81: rosfran@87: gst_base_src_set_format( GST_BASE_SRC( this ), GST_FORMAT_BYTES ); leo_sobral@2: rosfran@106: /* gst_base_src_set_live ( GST_BASE_SRC( this ), TRUE ); */ rosfran@81: rosfran@90: gst_pad_set_event_function ( GST_BASE_SRC_PAD(GST_BASE_SRC(this)), rosfran@90: gst_mythtv_src_handle_event ); rosfran@87: gst_pad_set_query_function ( GST_BASE_SRC_PAD(GST_BASE_SRC(this)), rosfran@87: gst_mythtv_src_handle_query ); rosfran@30: leo_sobral@2: } leo_sobral@2: rosfran@30: static void leo_sobral@2: gst_mythtv_src_finalize (GObject * gobject) leo_sobral@2: { leo_sobral@2: GstMythtvSrc *this = GST_MYTHTV_SRC (gobject); rosfran@114: rosfran@81: if ( this->th_read_ahead != NULL ) { rosfran@114: gst_task_stop( this->th_read_ahead ); rosfran@114: this->th_read_ahead = NULL; rosfran@81: } 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: rosfran@114: if (this->spawn_livetv) { rosfran@70: g_object_unref (this->spawn_livetv); rosfran@52: this->spawn_livetv = NULL; rosfran@52: } rosfran@118: rosfran@118: if (this->backend_info) { rosfran@118: g_object_unref (this->backend_info); rosfran@118: this->backend_info = NULL; rosfran@118: } rosfran@52: 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: } rosfran@114: rosfran@100: if ( this->bytes_queue ) { rosfran@114: g_byte_array_free( this->bytes_queue, TRUE ); rosfran@114: this->bytes_queue = NULL; rosfran@100: } leo_sobral@2: leo_sobral@2: G_OBJECT_CLASS (parent_class)->finalize (gobject); leo_sobral@2: } leo_sobral@2: rosfran@37: static gint rosfran@102: do_read_request_response (GstMythtvSrc * src, guint size, GByteArray *data_ptr) leo_sobral@2: { rosfran@37: gint read = 0; rosfran@52: guint sizetoread = size; rosfran@142: gint max_iters = GMYTHTV_TRANSFER_MAX_RESENDS; rosfran@102: rosfran@106: GST_LOG_OBJECT( src, "Starting: [%s] Reading %d bytes...\n", __FUNCTION__, sizetoread ); leo_sobral@2: rosfran@30: /* Loop sending the Myth File Transfer request: leo_sobral@2: * Retry whilst authentication fails and we supply it. */ rosfran@37: gint len = 0; rosfran@70: rosfran@102: while ( sizetoread == size && --max_iters > 0) { rosfran@61: rosfran@61: len = gmyth_file_transfer_read( src->file_transfer, rosfran@102: data_ptr, sizetoread, TRUE ); leo_sobral@2: leo_sobral@2: if ( len > 0 ) { rosfran@81: read += len; leo_sobral@2: sizetoread -= len; rosfran@52: } rosfran@90: else if ( len < 0 ) rosfran@52: { rosfran@90: read = -1; rosfran@61: rosfran@52: if ( src->live_tv == FALSE ) rosfran@52: { rosfran@90: goto eos; rosfran@52: } rosfran@52: else rosfran@52: { rosfran@90: if ( len == GMYTHTV_FILE_TRANSFER_READ_ERROR ) { /* -314 */ rosfran@90: src->update_prog_chain = TRUE; rosfran@90: goto done; rosfran@100: } rosfran@90: goto done; rosfran@90: } rosfran@30: rosfran@142: } else if ( len == 0 ) rosfran@142: goto done; rosfran@61: rosfran@30: if ( read == sizetoread ) rosfran@90: goto done; rosfran@100: } rosfran@61: rosfran@102: if ( ( read < 0 && !src->live_tv ) || max_iters == 0 ) rosfran@61: goto eos; rosfran@61: rosfran@30: goto done; leo_sobral@2: leo_sobral@2: eos: leo_sobral@2: src->eos = TRUE; rosfran@61: leo_sobral@2: done: leo_sobral@2: return read; leo_sobral@2: } leo_sobral@2: rosfran@30: static GstFlowReturn rosfran@81: gst_mythtv_src_create ( GstPushSrc* psrc, GstBuffer** outbuf ) rosfran@81: { rosfran@81: GstMythtvSrc *src; rosfran@81: GstFlowReturn ret = GST_FLOW_OK; rosfran@81: gint read = -1; rosfran@90: rosfran@81: src = GST_MYTHTV_SRC ( psrc ); rosfran@90: rosfran@81: /* The caller should know the number of bytes and not read beyond EOS. */ rosfran@81: if (G_UNLIKELY (src->eos)) rosfran@81: goto eos; rosfran@81: if ( G_UNLIKELY (src->update_prog_chain) ) rosfran@81: goto change_progchain; rosfran@95: rosfran@90: //g_static_rec_mutex_lock( &th_mutex ); rosfran@106: GST_DEBUG_OBJECT( src, "[%s] offset = %llu, size = %d...\n", __FUNCTION__, rosfran@100: src->read_offset, MAX_READ_SIZE ); rosfran@95: rosfran@118: GST_DEBUG_OBJECT ( src, "[%s]\t\tCreate: buffer_remain: %d, buffer_size = %d.\n", __FUNCTION__, rosfran@118: (gint) src->buffer_remain, src->bytes_queue->len ); rosfran@95: rosfran@100: /* just get from the byte array, no network effort... */ rosfran@174: if ( ( src->buffer_remain = src->bytes_queue->len ) < MAX_READ_SIZE ) { rosfran@124: //( ( INTERNAL_BUFFER_SIZE - src->buffer_remain ) >= REQUEST_MAX_SIZE ) ) { rosfran@124: GByteArray *buffer = NULL; rosfran@142: guint buffer_size_inter = ( INTERNAL_BUFFER_SIZE - src->buffer_remain ); rosfran@142: if ( buffer_size_inter > REQUEST_MAX_SIZE ) rosfran@142: buffer_size_inter = REQUEST_MAX_SIZE; rosfran@124: rosfran@142: buffer = g_byte_array_new(); rosfran@95: rosfran@142: read = do_read_request_response( src, buffer_size_inter, buffer ); rosfran@142: rosfran@142: if (G_UNLIKELY (read < 0)) { rosfran@142: if ( src->live_tv ) rosfran@142: goto change_progchain; rosfran@142: else rosfran@142: goto read_error; rosfran@174: } else if (G_UNLIKELY (read == 0)) rosfran@174: { rosfran@174: if (!src->live_tv ) rosfran@174: goto eos; rosfran@174: else rosfran@174: goto done; rosfran@174: } rosfran@142: rosfran@142: if ( G_UNLIKELY (src->update_prog_chain) ) rosfran@142: goto change_progchain; rosfran@142: rosfran@151: src->bytes_queue = g_byte_array_append( src->bytes_queue, buffer->data, read ); rosfran@142: if ( read > buffer_size_inter ) rosfran@142: GST_WARNING_OBJECT( src, "\n\n\n[%s]\t\tINCREASED buffer size! Backend sent more than we ask him... (%d)\n\n\n", rosfran@142: __FUNCTION__, abs( read - buffer_size_inter ) ); rosfran@142: rosfran@142: src->buffer_remain += read; rosfran@142: rosfran@142: if ( buffer != NULL ) { rosfran@142: g_byte_array_free( buffer, TRUE ); rosfran@142: buffer = NULL; rosfran@142: } rosfran@124: rosfran@124: GST_DEBUG_OBJECT( src, "[%s]\tBYTES READ (actual) = %d, BYTES READ (cumulative) = %llu, "\ rosfran@124: "OFFSET = %llu, CONTENT SIZE = %llu.\n", __FUNCTION__, read, src->bytes_read, rosfran@124: src->read_offset, src->content_size ); rosfran@100: rosfran@81: } rosfran@98: rosfran@100: guint buffer_size = ( src->buffer_remain < MAX_READ_SIZE) ? src->buffer_remain : MAX_READ_SIZE; rosfran@100: rosfran@100: /* Create the buffer. */ rosfran@100: ret = gst_pad_alloc_buffer ( GST_BASE_SRC_PAD (GST_BASE_SRC (psrc)), rosfran@152: GST_BUFFER_OFFSET_NONE, buffer_size, rosfran@100: GST_PAD_CAPS (GST_BASE_SRC_PAD (GST_BASE_SRC (psrc))), outbuf ); rosfran@81: rosfran@100: if (G_UNLIKELY (ret != GST_FLOW_OK)) { rosfran@100: if ( src->live_tv ) rosfran@100: goto change_progchain; rosfran@100: else rosfran@100: goto done; rosfran@100: } rosfran@152: //*outbuf = gst_buffer_new_and_alloc( buffer_size ); rosfran@100: rosfran@100: /* gets the first buffer_size bytes from the byte array buffer variable */ rosfran@151: //guint8 *buf = g_memdup( src->bytes_queue->data, buffer_size ); rosfran@95: rosfran@106: GST_DEBUG_OBJECT( src, "[%s] read from network? %s!, buffer_remain = %d\n", __FUNCTION__, rosfran@100: read == -1 ? "NO, got from buffer" : "YES, go see the backend's log file", src->buffer_remain ); rosfran@95: rosfran@100: GST_BUFFER_SIZE (*outbuf) = buffer_size; rosfran@174: GST_BUFFER_MALLOCDATA( *outbuf ) = g_malloc0( GST_BUFFER_SIZE (*outbuf) ); rosfran@174: GST_BUFFER_DATA( *outbuf ) = GST_BUFFER_MALLOCDATA( *outbuf ); rosfran@151: g_memmove( GST_BUFFER_DATA( (*outbuf) ), src->bytes_queue->data, GST_BUFFER_SIZE(*outbuf) ); rosfran@95: GST_BUFFER_OFFSET (*outbuf) = src->read_offset; rosfran@95: GST_BUFFER_OFFSET_END (*outbuf) = src->read_offset + GST_BUFFER_SIZE (*outbuf); rosfran@95: rosfran@95: src->buffer_remain -= GST_BUFFER_SIZE (*outbuf); rosfran@95: rosfran@95: src->read_offset += GST_BUFFER_SIZE (*outbuf); rosfran@95: src->bytes_read += GST_BUFFER_SIZE (*outbuf); rosfran@106: GST_DEBUG_OBJECT( src, "[%s]\t\tBuffer output with size: %d\n", __FUNCTION__, GST_BUFFER_SIZE (*outbuf) ); rosfran@100: rosfran@100: /* flushs the newly buffer got from byte array */ rosfran@100: src->bytes_queue = g_byte_array_remove_range( src->bytes_queue, 0, buffer_size ); rosfran@95: rosfran@106: GST_DEBUG_OBJECT( src, "Got buffer: [%s]\t\tBUFFER --->SIZE = %d, OFFSET = %llu, "\ rosfran@95: "OFFSET_END = %llu.\n\n", __FUNCTION__, GST_BUFFER_SIZE (*outbuf), rosfran@95: GST_BUFFER_OFFSET (*outbuf), GST_BUFFER_OFFSET_END (*outbuf) ); rosfran@95: rosfran@142: GST_DEBUG_OBJECT( src, "[%s]\tCONTENT_SIZE = %llu, BYTES_READ = %llu.\n", __FUNCTION__, rosfran@142: src->content_size, src->bytes_read ); rosfran@142: rosfran@142: if ( G_UNLIKELY (src->eos) || ( !src->live_tv && ( src->bytes_read >= src->content_size ) ) ) rosfran@142: goto eos; rosfran@90: rosfran@81: done: rosfran@90: { rosfran@81: const gchar *reason = gst_flow_get_name (ret); rosfran@81: rosfran@81: GST_DEBUG_OBJECT (src, "DONE task, reason %s", reason); rosfran@90: return ret; rosfran@90: } rosfran@81: eos: rosfran@81: { rosfran@81: const gchar *reason = gst_flow_get_name (ret); rosfran@81: rosfran@81: GST_DEBUG_OBJECT (src, "pausing task, reason %s", reason); rosfran@81: return GST_FLOW_UNEXPECTED; rosfran@81: } rosfran@81: /* ERRORS */ rosfran@81: read_error: rosfran@81: { rosfran@81: GST_ELEMENT_ERROR (src, RESOURCE, READ, rosfran@81: (NULL), ("Could not read any bytes (%i, %s)", read, rosfran@90: src->uri_name)); rosfran@81: return GST_FLOW_ERROR; rosfran@81: } rosfran@81: change_progchain: rosfran@81: { rosfran@81: GST_ELEMENT_ERROR (src, RESOURCE, READ, rosfran@90: (NULL), ("Seek failed, go to the next program info... (%i, %s)", read, rosfran@90: src->uri_name)); rosfran@90: rosfran@90: gst_pad_push_event ( GST_BASE_SRC_PAD (GST_BASE_SRC (psrc)), rosfran@90: gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0, -1, 0 ) ); rosfran@90: // go to the next program chain rosfran@90: src->unique_setup = FALSE; rosfran@90: src->update_prog_chain = TRUE; rosfran@90: rosfran@90: gst_mythtv_src_next_program_chain( src ); rosfran@90: rosfran@70: return GST_FLOW_ERROR_NO_DATA; rosfran@37: } rosfran@30: leo_sobral@2: } rosfran@98: rosfran@52: gint64 rosfran@30: gst_mythtv_src_get_position ( GstMythtvSrc* src ) leo_sobral@2: { rosfran@30: rosfran@52: gint64 size_tmp = 0; rosfran@71: guint max_tries = 2; rosfran@71: if (src->live_tv == TRUE && ( abs( src->content_size - src->bytes_read ) < rosfran@71: GMYTHTV_TRANSFER_MAX_BUFFER ) ) { rosfran@71: rosfran@30: get_file_pos: rosfran@61: g_usleep( 10 ); rosfran@114: size_tmp = gmyth_recorder_get_file_position( src->spawn_livetv->recorder ); rosfran@52: if ( size_tmp > ( src->content_size + GMYTHTV_TRANSFER_MAX_BUFFER ) ) rosfran@61: src->content_size = size_tmp; rosfran@61: else if ( size_tmp > 0 && --max_tries > 0 ) rosfran@61: goto get_file_pos; rosfran@106: GST_LOG_OBJECT( src, "\t[%s]\tGET_POSITION: file_position = %lld\n", rosfran@61: __FUNCTION__, size_tmp ); rosfran@71: /* sets the last content size amount before it can be updated */ rosfran@71: src->prev_content_size = src->content_size; rosfran@30: } rosfran@52: rosfran@30: return src->content_size; rosfran@30: leo_sobral@2: } leo_sobral@2: rosfran@87: static gboolean rosfran@87: gst_mythtv_src_do_seek( GstBaseSrc *base, GstSegment *segment ) rosfran@87: { rosfran@87: GstMythtvSrc *src = GST_MYTHTV_SRC( base ); rosfran@90: gint64 new_offset = -1; rosfran@90: gint64 actual_seek = segment->start; rosfran@87: gboolean ret = TRUE; rosfran@87: rosfran@106: GST_LOG_OBJECT( src, "[%s]DO Seek called! (start = %lld, stop = %lld)\n", __FUNCTION__, rosfran@106: segment->start, segment->stop ); rosfran@90: rosfran@90: if ( segment->format == GST_FORMAT_TIME ) rosfran@90: { rosfran@90: goto done; rosfran@90: //actual_seek = ( ( segment->start / 1000 ) * 28 ) * 4000; rosfran@90: } rosfran@106: GST_LOG_OBJECT( src, "[%s]Trying to seek at the value (actual_seek = %lld, read_offset = %lld)\n", __FUNCTION__, actual_seek, src->read_offset ); rosfran@87: /* verify if it needs to seek */ rosfran@90: if ( src->read_offset != actual_seek ) rosfran@87: { rosfran@87: rosfran@87: new_offset = gmyth_file_transfer_seek( src->file_transfer, segment->start, SEEK_SET ); rosfran@87: rosfran@106: GST_LOG_OBJECT( src, "[%s] Segment offset start = %lld, SRC Offset = %lld, NEW actual backend SEEK Offset = %lld.\n", rosfran@106: __FUNCTION__, segment->start, src->read_offset, new_offset ); rosfran@87: if ( G_UNLIKELY (new_offset < 0 ) ) rosfran@87: { rosfran@87: ret = FALSE; rosfran@87: if ( src->live_tv ) rosfran@87: goto change_progchain; rosfran@87: else rosfran@87: goto eos; rosfran@87: } rosfran@87: rosfran@87: src->read_offset = new_offset; rosfran@87: rosfran@90: if ( ret == FALSE ) { rosfran@106: GST_INFO_OBJECT( src, "[%s] Failed to set the SEEK on segment!\n", __FUNCTION__ ); rosfran@90: } rosfran@90: rosfran@87: } rosfran@90: rosfran@90: done: rosfran@87: return ret; rosfran@87: rosfran@87: eos: rosfran@87: { rosfran@87: rosfran@87: GST_DEBUG_OBJECT (src, "EOS found on seeking!!!"); rosfran@87: return FALSE; rosfran@87: } rosfran@87: change_progchain: rosfran@87: { rosfran@87: GST_ELEMENT_ERROR (src, RESOURCE, READ, rosfran@87: (NULL), ("Seek failed, go to the next program info... (%i, %s)", read, rosfran@87: src->uri_name)); rosfran@87: rosfran@87: gst_pad_push_event ( GST_BASE_SRC_PAD (base), rosfran@87: gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0, -1, 0 ) ); rosfran@87: /* go to the next program chain */ rosfran@87: src->unique_setup = FALSE; rosfran@87: src->update_prog_chain = TRUE; rosfran@87: rosfran@87: gst_mythtv_src_next_program_chain( src ); rosfran@87: rosfran@87: return TRUE; rosfran@87: } rosfran@87: rosfran@87: } rosfran@87: leo_sobral@2: /* create a socket for connecting to remote server */ rosfran@30: 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; rosfran@174: leo_sobral@2: gboolean ret = TRUE; rosfran@90: rosfran@71: if ( G_UNLIKELY (src->update_prog_chain) ) rosfran@71: goto change_progchain; rosfran@30: 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: } rosfran@174: melunko@159: src->backend_info = gmyth_backend_info_new_with_uri( src->uri_name ); rosfran@174: if ( src->live_tv ) { rosfran@40: src->spawn_livetv = gmyth_livetv_new( ); rosfran@118: if ( gmyth_livetv_setup( src->spawn_livetv, src->backend_info ) == FALSE ) { rosfran@30: ret = FALSE; rosfran@30: goto init_failed; rosfran@70: } rosfran@52: 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 ); rosfran@114: GST_INFO_OBJECT( src, "\t[%s]\tLocal chain ID = %s.\n", __FUNCTION__, src->live_chain_id ); leo_sobral@2: } rosfran@114: rosfran@70: src->live_tv_id = src->spawn_livetv->recorder->recorder_num; rosfran@106: GST_LOG_OBJECT( src, "[%s] LiveTV id = %d, URI path = %s.\n", __FUNCTION__, src->live_tv_id, src->uri_name ); leo_sobral@2: } leo_sobral@2: rosfran@174: //src->file_transfer = gmyth_file_transfer_new( src->live_tv_id, rosfran@174: // g_string_new( src->uri_name ), -1, src->mythtv_version ); rosfran@174: src->file_transfer = gmyth_file_transfer_new(src->backend_info); leo_sobral@2: leo_sobral@2: if ( src->file_transfer == NULL ) { leo_sobral@2: goto init_failed; leo_sobral@2: } rosfran@124: GST_INFO_OBJECT( src, "[%s] uri = %s.\n", __FUNCTION__, src->uri_name ); rosfran@174: rosfran@185: ret = gmyth_file_transfer_open( src->file_transfer, src->uri_name ); rosfran@174: rosfran@174: /* sets the Playback monitor connection */ rosfran@174: //ret = gmyth_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: rosfran@70: while ( wait_to_transfer++ < GMYTHTV_TRANSFER_MAX_WAITS && rosfran@108: ( gmyth_livetv_is_recording( src->spawn_livetv ) == FALSE ) ) rosfran@110: g_usleep( 500 ); rosfran@110: rosfran@110: /* IS_RECORDING again, just like the MythTV backend does... */ rosfran@110: gmyth_livetv_is_recording( src->spawn_livetv ); rosfran@110: rosfran@124: sleep( 3 ); rosfran@110: leo_sobral@2: } leo_sobral@2: /* sets the FileTransfer instance connection (video/audio download) */ rosfran@114: //ret = gmyth_file_transfer_setup( &(src->file_transfer), src->live_tv ); leo_sobral@2: leo_sobral@2: if ( ret == FALSE ) { leo_sobral@2: #ifndef GST_DISABLE_GST_DEBUG leo_sobral@2: if ( src->mythtv_msgs_dbg ) rosfran@106: GST_INFO_OBJECT( src, "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: rosfran@71: src->do_start = FALSE; rosfran@114: rosfran@114: /* this is used for the buffer cache */ rosfran@100: src->bytes_queue = g_byte_array_sized_new( INTERNAL_BUFFER_SIZE ); rosfran@90: src->buffer_remain = 0; rosfran@114: rosfran@124: gst_pad_push_event ( GST_BASE_SRC_PAD (GST_BASE_SRC (src)), rosfran@124: gst_event_new_new_segment ( TRUE, 1.0, GST_FORMAT_TIME, 0, src->content_size, 0 ) ); rosfran@90: rosfran@30: done: rosfran@30: return TRUE; rosfran@30: rosfran@30: /* ERRORS */ rosfran@30: init_failed: rosfran@30: { rosfran@30: if (src->spawn_livetv != NULL ) rosfran@30: g_object_unref( src->spawn_livetv ); rosfran@30: rosfran@30: GST_ELEMENT_ERROR (src, LIBRARY, INIT, rosfran@30: (NULL), ("Could not initialize MythTV library (%i, %s)", ret, src->uri_name)); rosfran@30: return FALSE; rosfran@30: } rosfran@30: begin_req_failed: rosfran@30: { rosfran@30: GST_ELEMENT_ERROR (src, LIBRARY, INIT, rosfran@30: (NULL), ("Could not begin request sent to MythTV server (%i, %s)", ret, src->uri_name)); rosfran@30: return FALSE; rosfran@30: } rosfran@71: change_progchain: rosfran@71: { rosfran@71: GST_ELEMENT_ERROR (src, RESOURCE, READ, rosfran@90: (NULL), ("Seek failed, go to the next program info... (%s)", rosfran@90: src->uri_name)); rosfran@90: rosfran@90: gst_pad_push_event ( GST_BASE_SRC_PAD (GST_BASE_SRC (src)), rosfran@90: gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0, -1, 0 ) ); rosfran@90: rosfran@90: // go to the next program chain rosfran@90: src->unique_setup = FALSE; rosfran@90: src->update_prog_chain = TRUE; rosfran@90: rosfran@90: gst_mythtv_src_next_program_chain( src ); rosfran@90: rosfran@71: return TRUE; rosfran@71: } rosfran@30: } leo_sobral@2: rosfran@52: /* create a new socket for connecting to the next program chain */ rosfran@52: static gboolean rosfran@52: gst_mythtv_src_next_program_chain ( GstMythtvSrc *src ) rosfran@52: { rosfran@52: GString *chain_id_local = NULL; rosfran@52: rosfran@52: gboolean ret = TRUE; rosfran@61: rosfran@52: if ( !src->live_tv ) rosfran@61: goto init_failed; rosfran@70: rosfran@70: if (src->unique_setup == FALSE) { rosfran@70: src->unique_setup = TRUE; rosfran@70: } else { rosfran@70: goto done; rosfran@70: } rosfran@81: rosfran@81: GST_PAD_STREAM_LOCK( GST_BASE_SRC_PAD (GST_BASE_SRC (src)) ); rosfran@61: rosfran@52: if (src->file_transfer) { rosfran@52: g_object_unref (src->file_transfer); rosfran@52: src->file_transfer = NULL; rosfran@52: } rosfran@61: rosfran@52: if (src->uri_name) { rosfran@52: g_free (src->uri_name); rosfran@52: } rosfran@174: rosfran@174: if ( src->backend_info == NULL ) rosfran@174: src->backend_info = gmyth_backend_info_new_with_uri( src->uri_name ); rosfran@61: rosfran@52: if ( src->live_tv ) { rosfran@61: if ( gmyth_livetv_next_program_chain( src->spawn_livetv ) == FALSE ) { rosfran@106: GST_INFO_OBJECT( src, "\n\n[%s]\t\tFailed to go to the next program chain!!!\n\n", __FUNCTION__ ); rosfran@61: ret = FALSE; rosfran@61: goto init_failed; rosfran@52: } rosfran@52: /* set up the uri variable */ rosfran@52: src->uri_name = g_strdup( src->spawn_livetv->proginfo->pathname->str ); rosfran@52: chain_id_local = gmyth_tvchain_get_id( src->spawn_livetv->tvchain ); rosfran@52: if ( chain_id_local != NULL ) { rosfran@52: src->live_chain_id = g_strdup( chain_id_local->str ); rosfran@106: GST_DEBUG_OBJECT( src, "\t[%s]\tLocal chain ID = %s.\n", __FUNCTION__, src->live_chain_id ); rosfran@52: } rosfran@70: src->live_tv_id = src->spawn_livetv->recorder->recorder_num; rosfran@106: GST_LOG_OBJECT( src, "[%s] LiveTV id = %d, URI path = %s.\n", __FUNCTION__, src->live_tv_id, src->uri_name ); rosfran@52: } rosfran@52: rosfran@174: src->file_transfer = gmyth_file_transfer_new(src->backend_info); rosfran@52: rosfran@52: if ( src->file_transfer == NULL ) { rosfran@52: goto init_failed; rosfran@52: } rosfran@114: rosfran@185: ret = gmyth_file_transfer_open( src->file_transfer, src->uri_name ); rosfran@174: rosfran@174: /* sets the Playback monitor connection */ rosfran@174: //ret = gmyth_file_transfer_playback_setup( &(src->file_transfer), src->live_tv ); rosfran@52: rosfran@52: if ( src->live_tv == TRUE && ret == TRUE ) { rosfran@52: /* loop finished, set the max tries variable to zero again... */ rosfran@52: wait_to_transfer = 0; rosfran@61: rosfran@61: g_usleep( 200 ); rosfran@52: rosfran@52: while ( wait_to_transfer++ < GMYTHTV_TRANSFER_MAX_WAITS && rosfran@108: ( gmyth_livetv_is_recording( src->spawn_livetv ) == FALSE ) ) rosfran@61: g_usleep( 1000 ); rosfran@52: } rosfran@52: rosfran@174: /* sets the FileTransfer instance connection (video/audio download) */ rosfran@174: //ret = gmyth_file_transfer_setup( &(src->file_transfer), src->live_tv ); rosfran@52: rosfran@52: if ( ret == FALSE ) { rosfran@52: #ifndef GST_DISABLE_GST_DEBUG rosfran@52: if ( src->mythtv_msgs_dbg ) rosfran@106: GST_ERROR_OBJECT( src, "MythTV FileTransfer request failed when setting up socket connection!\n" ); rosfran@52: #endif rosfran@52: goto begin_req_failed; rosfran@52: } rosfran@61: src->content_size_last = src->content_size; rosfran@61: rosfran@61: src->content_size = src->file_transfer->filesize; rosfran@87: if ( src->live_tv ) { rosfran@87: wait_to_transfer = 0; rosfran@87: while ( wait_to_transfer++ < GMYTHTV_TRANSFER_MAX_WAITS && src->content_size < GMYTHTV_TRANSFER_MAX_BUFFER ) rosfran@87: src->content_size = gst_mythtv_src_get_position( src ); rosfran@87: } rosfran@63: rosfran@81: src->read_offset = 0; rosfran@70: rosfran@100: if ( src->bytes_queue != NULL ) { rosfran@100: g_byte_array_free( src->bytes_queue, TRUE ); rosfran@100: } rosfran@100: rosfran@100: src->bytes_queue = g_byte_array_sized_new( INTERNAL_BUFFER_SIZE ); rosfran@100: rosfran@114: //gst_pad_push_event ( GST_BASE_SRC_PAD (GST_BASE_SRC (src)), rosfran@114: // gst_event_new_new_segment ( TRUE, 1.0, GST_FORMAT_TIME, 0, src->content_size, 0 ) ); rosfran@114: rosfran@70: done: rosfran@81: src->update_prog_chain = FALSE; rosfran@81: rosfran@81: GST_PAD_STREAM_UNLOCK( GST_BASE_SRC_PAD (GST_BASE_SRC (src)) ); rosfran@81: rosfran@52: return TRUE; rosfran@52: rosfran@52: /* ERRORS */ rosfran@52: init_failed: rosfran@52: { rosfran@52: if (src->spawn_livetv != NULL ) rosfran@52: g_object_unref( src->spawn_livetv ); rosfran@52: rosfran@52: GST_ELEMENT_ERROR (src, LIBRARY, INIT, rosfran@52: (NULL), ("Could not initialize MythTV library (%i, %s)", ret, src->uri_name)); rosfran@52: return FALSE; rosfran@52: } rosfran@52: begin_req_failed: rosfran@52: { rosfran@52: GST_ELEMENT_ERROR (src, LIBRARY, INIT, rosfran@52: (NULL), ("Could not begin request sent to MythTV server (%i, %s)", ret, src->uri_name)); rosfran@52: return FALSE; rosfran@52: } rosfran@61: rosfran@52: } rosfran@52: rosfran@30: static gboolean leo_sobral@2: gst_mythtv_src_get_size (GstBaseSrc * bsrc, guint64 * size) leo_sobral@2: { rosfran@31: GstMythtvSrc *src = GST_MYTHTV_SRC (bsrc); rosfran@30: gboolean ret = TRUE; rosfran@106: GST_LOG_OBJECT( src, "[%s] Differs from previous content size: %d (max.: %d)\n", __FUNCTION__, rosfran@63: abs( src->content_size - src->prev_content_size ), GMYTHTV_TRANSFER_MAX_BUFFER ); rosfran@98: rosfran@100: if ( src->live_tv ) { rosfran@98: ret = FALSE; rosfran@100: } else if ( src->live_tv && src->enable_timing_position && ( abs( src->content_size - src->bytes_read ) < rosfran@70: GMYTHTV_TRANSFER_MAX_BUFFER ) ) { rosfran@70: rosfran@114: gint64 new_offset = gmyth_recorder_get_file_position( src->spawn_livetv->recorder ); rosfran@70: if ( new_offset > 0 && new_offset > src->content_size ) { rosfran@70: src->content_size = new_offset; rosfran@70: } else if ( new_offset < src->content_size ) { rosfran@70: src->update_prog_chain = TRUE; rosfran@70: } rosfran@61: leo_sobral@2: } leo_sobral@2: leo_sobral@2: *size = src->content_size; rosfran@106: GST_LOG_OBJECT( src, "[%s] Content size = %lld\n", __FUNCTION__, src->content_size ); rosfran@87: rosfran@30: return ret; rosfran@30: 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 */ rosfran@52: 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: rosfran@30: 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)); rosfran@52: gint64 cont_size = 0; rosfran@90: gboolean ret = FALSE; leo_sobral@2: leo_sobral@2: switch (GST_EVENT_TYPE (event)) { rosfran@30: case GST_EVENT_EOS: rosfran@106: GST_WARNING_OBJECT( src, "[%s] Got EOS event!!!\n", __FUNCTION__ ); rosfran@61: rosfran@61: if ( src->live_tv ) { rosfran@61: cont_size = gst_mythtv_src_get_position (src); rosfran@61: if ( cont_size > src->content_size ) { rosfran@61: src->content_size = cont_size; rosfran@61: src->eos = FALSE; rosfran@61: } else { rosfran@61: src->eos = TRUE; rosfran@61: gst_element_set_state ( GST_ELEMENT (src), GST_STATE_NULL ); rosfran@61: gst_element_set_locked_state ( GST_ELEMENT (src), FALSE ); rosfran@61: } rosfran@114: } rosfran@30: break; leo_sobral@2: default: rosfran@90: ret = gst_pad_event_default (pad, event); leo_sobral@2: } leo_sobral@2: rosfran@90: return ret; leo_sobral@2: } leo_sobral@2: rosfran@30: static gboolean rosfran@30: gst_mythtv_src_is_seekable( GstBaseSrc *push_src ) leo_sobral@2: { rosfran@100: return TRUE; leo_sobral@2: } rosfran@87: rosfran@87: static gboolean rosfran@87: gst_mythtv_src_handle_query (GstPad * pad, GstQuery * query) rosfran@87: { rosfran@87: gboolean res = FALSE; rosfran@87: GstMythtvSrc *myth = GST_MYTHTV_SRC (gst_pad_get_parent (pad)); rosfran@114: GstFormat formt; rosfran@87: rosfran@87: switch (GST_QUERY_TYPE (query)) { rosfran@87: case GST_QUERY_POSITION: rosfran@114: { rosfran@114: gint64 pos = -1; rosfran@114: gst_query_parse_position (query, &formt, &pos ); rosfran@87: res = TRUE; rosfran@124: if ( formt == GST_FORMAT_BYTES ) { rosfran@124: gst_query_set_position (query, formt, pos = myth->read_offset ); rosfran@114: GST_DEBUG_OBJECT (myth, "POS %lld (BYTES).\n", pos ); rosfran@124: } else if ( formt == GST_FORMAT_TIME ) { rosfran@114: GST_DEBUG_OBJECT (myth, "POS %lld (TIME).\n", pos ); rosfran@124: res = gst_pad_query_default(pad, query); rosfran@124: } rosfran@87: break; rosfran@114: } rosfran@87: case GST_QUERY_DURATION: rosfran@114: { rosfran@87: #if 0 rosfran@87: if (myth->duration != 0) { rosfran@87: gint64 total; rosfran@87: gint64 fps; rosfran@87: rosfran@87: fps = nuv->h->i_fpsn / nuv->h->i_fpsd; rosfran@87: total = gst_util_uint64_scale_int (GST_SECOND, nuv->h->i_video_blocks, fps); rosfran@114: } rosfran@81: #endif rosfran@114: gint64 dur = -1; rosfran@124: gst_query_parse_duration ( query, &formt, &dur ); rosfran@124: if ( formt == GST_FORMAT_BYTES ) { rosfran@124: gst_query_set_duration (query, formt, dur = myth->content_size); rosfran@114: GST_DEBUG_OBJECT (myth, "DURATION %lld (BYTES).\n", dur ); rosfran@124: } else if ( formt == GST_FORMAT_TIME ) { rosfran@114: GST_DEBUG_OBJECT (myth, "DURATION %lld (TIME).\n", dur ); rosfran@124: gst_query_ref(query); rosfran@124: res = gst_pad_query_default(pad, query); rosfran@124: } rosfran@114: res = TRUE; rosfran@70: break; rosfran@114: } rosfran@70: default: rosfran@114: { rosfran@124: res = gst_pad_query_default(pad, query); rosfran@87: break; rosfran@114: } rosfran@70: } rosfran@61: rosfran@87: gst_object_unref (myth); rosfran@61: rosfran@70: return res; rosfran@61: } rosfran@61: rosfran@52: static GstStateChangeReturn rosfran@52: gst_mythtv_src_change_state (GstElement * element, GstStateChange transition) rosfran@52: { rosfran@87: GstStateChangeReturn ret = GST_STATE_CHANGE_FAILURE;//GST_STATE_CHANGE_NO_PREROLL; rosfran@52: GstMythtvSrc *src = GST_MYTHTV_SRC (element); rosfran@52: rosfran@52: switch (transition) { rosfran@52: case GST_STATE_CHANGE_NULL_TO_READY: rosfran@87: //src->do_start = TRUE; rosfran@87: //src->unique_setup = FALSE; rosfran@52: break; rosfran@52: case GST_STATE_CHANGE_READY_TO_PAUSED: rosfran@124: GST_INFO_OBJECT( src, "[%s] READY to PAUSED called!\n", __FUNCTION__ ); rosfran@124: break; rosfran@52: case GST_STATE_CHANGE_PAUSED_TO_PLAYING: rosfran@124: GST_INFO_OBJECT( src, "[%s] PAUSED to PLAYING called!\n", __FUNCTION__ ); rosfran@124: if ( src->live_tv ) { rosfran@124: if ( !gmyth_recorder_send_frontend_ready_command( src->spawn_livetv->recorder ) ) rosfran@124: GST_WARNING_OBJECT( src, "[%s] Couldn't send the FRONTEND_READY message to the backend!", __FUNCTION__ ); rosfran@124: else rosfran@124: GST_DEBUG_OBJECT( src, "[%s] Message FRONTEND_READY was sent to the backend!", __FUNCTION__ ); rosfran@124: } rosfran@124: rosfran@52: break; rosfran@52: default: rosfran@52: break; rosfran@52: } rosfran@61: rosfran@61: ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition); rosfran@52: if (ret == GST_STATE_CHANGE_FAILURE) rosfran@52: return ret; rosfran@52: rosfran@52: switch (transition) { rosfran@52: case GST_STATE_CHANGE_READY_TO_NULL: rosfran@106: GST_INFO_OBJECT( src, "[%s] READY to NULL called!\n", __FUNCTION__ ); rosfran@52: break; rosfran@61: case GST_STATE_CHANGE_PLAYING_TO_PAUSED: rosfran@106: GST_INFO_OBJECT( src, "[%s] PLAYING to PAUSED called!\n", __FUNCTION__ ); rosfran@52: case GST_STATE_CHANGE_PAUSED_TO_READY: rosfran@106: GST_INFO_OBJECT( src, "[%s] PAUSED to READY called!\n", __FUNCTION__ ); rosfran@124: if ( src->live_tv ) { rosfran@110: if ( !gmyth_recorder_send_frontend_ready_command( src->spawn_livetv->recorder ) ) rosfran@110: GST_WARNING_OBJECT( src, "[%s] Couldn't send the FRONTEND_READY message to the backend!", __FUNCTION__ ); rosfran@110: else rosfran@110: GST_DEBUG_OBJECT( src, "[%s] Message FRONTEND_READY was sent to the backend!", __FUNCTION__ ); rosfran@81: rosfran@98: /* rosfran@98: rosfran@81: gst_pad_push_event ( GST_BASE_SRC_PAD (GST_BASE_SRC (src)), rosfran@81: gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0, -1, 0 ) ); rosfran@81: rosfran@70: src->read_offset = 0; rosfran@70: src->bytes_read = 0; rosfran@81: src->unique_setup = FALSE; rosfran@70: gst_mythtv_src_next_program_chain( src ); rosfran@98: */ rosfran@61: } rosfran@98: rosfran@61: break; rosfran@52: default: rosfran@52: break; rosfran@52: } rosfran@52: rosfran@52: return ret; rosfran@52: } rosfran@52: rosfran@30: 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 rosfran@40: case PROP_GMYTHTV_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 rosfran@40: case PROP_GMYTHTV_VERSION: leo_sobral@2: { leo_sobral@2: mythtvsrc->mythtv_version = g_value_get_int (value); leo_sobral@2: break; leo_sobral@2: } rosfran@40: case PROP_GMYTHTV_LIVEID: leo_sobral@2: { leo_sobral@2: mythtvsrc->live_tv_id = g_value_get_int (value); leo_sobral@2: break; leo_sobral@2: } rosfran@40: case PROP_GMYTHTV_LIVE: leo_sobral@2: { leo_sobral@2: mythtvsrc->live_tv = g_value_get_boolean (value); leo_sobral@2: break; leo_sobral@2: } rosfran@52: case PROP_GMYTHTV_ENABLE_TIMING_POSITION: rosfran@52: { rosfran@52: mythtvsrc->enable_timing_position = g_value_get_boolean (value); rosfran@52: break; rosfran@52: } rosfran@40: case PROP_GMYTHTV_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: break; leo_sobral@2: } rosfran@70: case PROP_GMYTHTV_CHANNEL_NUM: rosfran@70: { rosfran@70: mythtvsrc->channel_num = g_value_get_int (value); rosfran@70: break; rosfran@70: } 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: rosfran@100: 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 rosfran@40: case PROP_GMYTHTV_DBG: leo_sobral@2: g_value_set_boolean ( value, mythtvsrc->mythtv_msgs_dbg ); leo_sobral@2: break; leo_sobral@2: #endif rosfran@40: case PROP_GMYTHTV_VERSION: leo_sobral@2: { leo_sobral@2: g_value_set_int ( value, mythtvsrc->mythtv_version ); leo_sobral@2: break; leo_sobral@2: } rosfran@40: case PROP_GMYTHTV_LIVEID: leo_sobral@2: { leo_sobral@2: g_value_set_int ( value, mythtvsrc->live_tv_id ); leo_sobral@2: break; leo_sobral@2: } rosfran@40: case PROP_GMYTHTV_LIVE: leo_sobral@2: g_value_set_boolean ( value, mythtvsrc->live_tv ); leo_sobral@2: break; rosfran@52: case PROP_GMYTHTV_ENABLE_TIMING_POSITION: rosfran@52: g_value_set_boolean ( value, mythtvsrc->enable_timing_position ); rosfran@52: break; rosfran@40: case PROP_GMYTHTV_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: } rosfran@70: case PROP_GMYTHTV_CHANNEL_NUM: rosfran@70: { rosfran@70: g_value_set_int ( value, mythtvsrc->channel_num ); rosfran@70: break; rosfran@70: } 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: */ rosfran@40: 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: rosfran@87: 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: }