[svn r53] LiveTV working fine, adjustments in some GMyth functions dealing with the TVChain.
1 /* vim: set sw=2: -*- Mode: C; tab-width: 2; indent-tabs-mode: t; c-basic-offset: 2; c-indent-level: 2 -*- */
2 /* GStreamer MythTV Plug-in
3 * Copyright (C) <2006> Rosfran Borges <rosfran.borges@indt.org.br>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library Lesser General
7 * Public License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more
20 #include "gstmythtvsrc.h"
21 #include <gmyth/gmyth_file_transfer.h>
22 #include <gmyth/gmyth_livetv.h>
24 #include <gmyth/gmyth_socket.h>
25 #include <gmyth/gmyth_tvchain.h>
30 GST_DEBUG_CATEGORY_STATIC (mythtvsrc_debug);
31 #define GST_CAT_DEFAULT mythtvsrc_debug
33 #define GST_GMYTHTV_ID_NUM 1
35 #define GMYTHTV_VERSION_DEFAULT 30
37 #define GMYTHTV_TRANSFER_MAX_WAITS 100
39 #define GMYTHTV_TRANSFER_MAX_BUFFER 16*1024
43 #define MAX_READ_SIZE 12*1024
46 /* stablish a maximum iteration value to the IS_RECORDING message */
47 static guint wait_to_transfer = 0;
49 static const GstElementDetails gst_mythtv_src_details =
50 GST_ELEMENT_DETAILS ( "MythTV client source",
52 "Control and receive data as a client over the network via raw socket connections using the MythTV protocol",
53 "Rosfran Borges <rosfran.borges@indt.org.br>" );
55 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
58 GST_STATIC_CAPS ("video/x-nuv") );
60 static GStaticMutex update_size_mutex = G_STATIC_MUTEX_INIT;
67 #ifndef GST_DISABLE_GST_DEBUG
73 PROP_GMYTHTV_LIVE_CHAINID,
74 PROP_GMYTHTV_ENABLE_TIMING_POSITION
77 static void gst_mythtv_src_finalize (GObject * gobject);
79 static GstFlowReturn gst_mythtv_src_create (GstBaseSrc * psrc, guint64 offset,
80 guint size, GstBuffer ** outbuf);
82 static gboolean gst_mythtv_src_start (GstBaseSrc * bsrc);
83 static gboolean gst_mythtv_src_stop (GstBaseSrc * bsrc);
84 static gboolean gst_mythtv_src_get_size (GstBaseSrc * bsrc, guint64 * size);
85 static gboolean gst_mythtv_src_is_seekable( GstBaseSrc *push_src );
87 static gboolean gst_mythtv_src_next_program_chain ( GstMythtvSrc *src );
89 static GstStateChangeReturn
90 gst_mythtv_src_change_state (GstElement * element, GstStateChange transition);
92 static void gst_mythtv_src_set_property (GObject * object, guint prop_id,
93 const GValue * value, GParamSpec * pspec);
94 static void gst_mythtv_src_get_property (GObject * object, guint prop_id,
95 GValue * value, GParamSpec * pspec);
97 static void gst_mythtv_src_uri_handler_init (gpointer g_iface, gpointer iface_data);
99 static gboolean gst_mythtv_src_handle_event (GstPad * pad, GstEvent * event);
100 //static gboolean gst_mythtv_src_query ( GstPad * pad, GstQuery * query );
102 static gint do_read_request_response (GstMythtvSrc *src, guint64 offset,
103 guint size, GstBuffer **outbuf);
104 //static gboolean gst_mythtv_src_sink_activate_pull (GstPad * srcpad, gboolean active);
107 _urihandler_init (GType type)
109 static const GInterfaceInfo urihandler_info = {
110 gst_mythtv_src_uri_handler_init,
115 g_type_add_interface_static (type, GST_TYPE_URI_HANDLER, &urihandler_info);
117 GST_DEBUG_CATEGORY_INIT (mythtvsrc_debug, "mythtvsrc", 0,
121 GST_BOILERPLATE_FULL (GstMythtvSrc, gst_mythtv_src, GstBaseSrc,
122 GST_TYPE_BASE_SRC, _urihandler_init)
124 //GST_BOILERPLATE_FULL (GstMythtvSrc, gst_mythtv_src, GstPushSrc,
125 // GST_TYPE_PUSH_SRC, _urihandler_init)
128 gst_mythtv_src_base_init (gpointer g_class)
130 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
132 gst_element_class_add_pad_template (element_class,
133 gst_static_pad_template_get (&srctemplate));
135 gst_element_class_set_details (element_class, &gst_mythtv_src_details);
137 element_class->change_state = gst_mythtv_src_change_state;
141 gst_mythtv_src_class_init (GstMythtvSrcClass * klass)
143 GObjectClass *gobject_class;
144 //GstPushSrcClass *gstpushsrc_class;
145 GstBaseSrcClass *gstbasesrc_class;
147 gobject_class = (GObjectClass *) klass;
148 gstbasesrc_class = (GstBaseSrcClass *) klass;
150 gobject_class->set_property = gst_mythtv_src_set_property;
151 gobject_class->get_property = gst_mythtv_src_get_property;
152 gobject_class->finalize = gst_mythtv_src_finalize;
154 g_object_class_install_property
155 (gobject_class, PROP_LOCATION,
156 g_param_spec_string ("location", "Location",
157 "The location. In the form:"
158 "\n\t\t\tmyth://a.com/file.nuv"
159 "\n\t\t\tmyth://a.com:23223/file.nuv "
160 "\n\t\t\ta.com/file.nuv - default scheme 'myth'",
161 "", G_PARAM_READWRITE));
163 g_object_class_install_property
164 (gobject_class, PROP_URI,
165 g_param_spec_string ("uri", "Uri",
166 "The location in form of a URI (deprecated; use location)",
167 "", G_PARAM_READWRITE));
169 g_object_class_install_property
170 (gobject_class, PROP_GMYTHTV_VERSION,
171 g_param_spec_int ("mythtv-version", "mythtv-version",
172 "Change Myth TV version",
173 26, 30, 26, G_PARAM_READWRITE));
175 g_object_class_install_property
176 (gobject_class, PROP_GMYTHTV_LIVEID,
177 g_param_spec_int ("mythtv-live-id", "mythtv-live-id",
178 "Change Myth TV version",
179 0, 200, GST_GMYTHTV_ID_NUM, G_PARAM_READWRITE));
181 g_object_class_install_property
182 (gobject_class, PROP_GMYTHTV_LIVE_CHAINID,
183 g_param_spec_string ("mythtv-live-chainid", "mythtv-live-chainid",
184 "Sets the Myth TV chain ID (from TV Chain)",
185 "", G_PARAM_READWRITE));
187 g_object_class_install_property
188 (gobject_class, PROP_GMYTHTV_LIVE,
189 g_param_spec_boolean ("mythtv-live", "mythtv-live",
190 "Enable MythTV Live TV content streaming",
191 FALSE, G_PARAM_READWRITE));
193 g_object_class_install_property
194 (gobject_class, PROP_GMYTHTV_ENABLE_TIMING_POSITION,
195 g_param_spec_boolean ("mythtv-enable-timing-position", "mythtv-enable-timing-position",
196 "Enable MythTV Live TV content size continuous updating",
197 FALSE, G_PARAM_READWRITE));
199 #ifndef GST_DISABLE_GST_DEBUG
200 g_object_class_install_property
201 (gobject_class, PROP_GMYTHTV_DBG,
202 g_param_spec_boolean ("mythtv-debug", "mythtv-debug",
203 "Enable MythTV debug messages",
204 FALSE, G_PARAM_READWRITE));
207 gstbasesrc_class->start = gst_mythtv_src_start;
208 gstbasesrc_class->stop = gst_mythtv_src_stop;
209 gstbasesrc_class->get_size = gst_mythtv_src_get_size;
210 gstbasesrc_class->is_seekable = gst_mythtv_src_is_seekable;
212 gstbasesrc_class->create = gst_mythtv_src_create;
214 GST_DEBUG_CATEGORY_INIT (mythtvsrc_debug, "mythtvsrc", 0,
215 "MythTV Client Source");
219 gst_mythtv_src_init (GstMythtvSrc * this, GstMythtvSrcClass * g_class)
221 this->file_transfer = NULL;
223 this->unique_setup = FALSE;
225 this->mythtv_version = GMYTHTV_VERSION_DEFAULT;
227 this->bytes_read = 0;
229 this->content_size = 0;
230 this->read_offset = 0;
232 this->live_tv = FALSE;
234 this->enable_timing_position = FALSE;
235 this->update_prog_chain = FALSE;
237 this->user_agent = g_strdup ("mythtvsrc");
238 this->mythtv_caps = NULL;
239 this->update_prog_chain = FALSE;
243 gst_base_src_set_format( GST_BASE_SRC( this ), GST_FORMAT_BYTES );
245 gst_base_src_set_live ( GST_BASE_SRC( this ), TRUE );
247 gst_pad_set_event_function ( GST_BASE_SRC_PAD(GST_BASE_SRC(this)),
248 gst_mythtv_src_handle_event );
250 gst_pad_set_query_function ( GST_BASE_SRC_PAD(GST_BASE_SRC(this)),
251 gst_mythtv_src_query );
257 gst_mythtv_src_finalize (GObject * gobject)
259 GstMythtvSrc *this = GST_MYTHTV_SRC (gobject);
261 if (this->mythtv_caps) {
262 gst_caps_unref (this->mythtv_caps);
263 this->mythtv_caps = NULL;
266 if (this->file_transfer) {
267 g_object_unref (this->file_transfer);
268 this->file_transfer = NULL;
271 if (this->spawn_livetv) {
272 //g_object_unref (this->spawn_livetv);
273 this->spawn_livetv = NULL;
276 if (this->uri_name) {
277 g_free (this->uri_name);
280 if (this->user_agent) {
281 g_free (this->user_agent);
284 G_OBJECT_CLASS (parent_class)->finalize (gobject);
288 do_read_request_response (GstMythtvSrc * src, guint64 offset, guint size, GstBuffer **outbuf)
291 guint sizetoread = size;
293 g_print( "Starting: [%s] Reading %d bytes...\n", __FUNCTION__, sizetoread );
295 /* Loop sending the Myth File Transfer request:
296 * Retry whilst authentication fails and we supply it. */
299 GST_OBJECT_LOCK(src);
301 while ( sizetoread > 0 ) {
303 len = gmyth_file_transfer_read( src->file_transfer,
304 GST_BUFFER_DATA( *outbuf ) + read, sizetoread, TRUE );
313 if ( src->live_tv == FALSE )
319 if ( len == GMYTHTV_FILE_TRANSFER_READ_ERROR ) { /* -314 */
320 src->update_prog_chain = TRUE;
321 if ( gst_mythtv_src_next_program_chain ( src ) )
326 if ( src->content_size <=
327 ( src->read_offset + size + GMYTHTV_TRANSFER_MAX_BUFFER ) ) {
328 src->update_prog_chain = TRUE;
329 if ( src->enable_timing_position ) {
331 if (src->live_tv == TRUE) {
333 size_tmp = gmyth_file_transfer_get_file_position( src->file_transfer );
334 if ( size_tmp > ( src->content_size + GMYTHTV_TRANSFER_MAX_BUFFER ) )
335 src->content_size = size_tmp;
336 else if ( size_tmp > 0 )
338 g_print( "\t[%s]\tGET_POSITION: file_position = %lld\n",
339 __FUNCTION__, size_tmp );
342 gint64 new_offset = gmyth_file_transfer_get_file_position( src->file_transfer );
343 if ( new_offset > 0 ) {
344 if ( src->content_size < new_offset ) {
345 src->content_size = new_offset;
348 src->update_prog_chain = TRUE;
357 if ( read == sizetoread )
362 src->read_offset += read;
363 src->bytes_read += read;
365 g_print( "[%s]\tBYTES READ (actual) = %d, BYTES READ (cumulative) = %llu, "\
366 "OFFSET = %llu, CONTENT SIZE = %llu.\n", __FUNCTION__, read, src->bytes_read,
367 src->read_offset, src->content_size );
369 GST_BUFFER_SIZE (*outbuf) = read; //GST_BUFFER_SIZE (buffer) = read;
370 GST_BUFFER_OFFSET (*outbuf) = offset; //GST_BUFFER_OFFSET (buffer) = offset;
371 GST_BUFFER_OFFSET_END (*outbuf) = offset + read;//GST_BUFFER_OFFSET_END (buffer) = offset + read;
373 g_print( "Stopping: [%s]\t\tBUFFER --->SIZE = %d, OFFSET = %llu, "\
374 "OFFSET_END = %llu.\n\n", __FUNCTION__, GST_BUFFER_SIZE (*outbuf),
375 GST_BUFFER_OFFSET (*outbuf), GST_BUFFER_OFFSET_END (*outbuf) );
377 } else if ( !src->live_tv )
386 GST_OBJECT_UNLOCK(src);
392 gst_mythtv_src_create ( GstBaseSrc * psrc, guint64 offset, guint size, GstBuffer **outbuf)
395 GstFlowReturn ret = GST_FLOW_OK;
398 src = GST_MYTHTV_SRC (psrc);
399 /* The caller should know the number of bytes and not read beyond EOS. */
400 if (G_UNLIKELY (src->eos))
402 if ( G_UNLIKELY (src->update_prog_chain) )
403 goto change_progchain;
405 GST_OBJECT_LOCK(src);
407 if (G_UNLIKELY (src->read_offset != offset)) {
408 gint64 new_offset = gmyth_file_transfer_seek(src->file_transfer, offset, SEEK_SET);
409 g_print( "[%s] SRC Offset = %lld, NEW actual backend SEEK Offset = %lld.\n",
410 __FUNCTION__, src->read_offset, new_offset );
411 if (G_UNLIKELY (new_offset < 0 ) )//|| new_offset != src->read_offset)) {
413 GST_OBJECT_UNLOCK(src);
415 goto change_progchain;
420 src->read_offset = offset;
422 GST_OBJECT_UNLOCK(src);
424 /* Create the buffer. */
425 ret = gst_pad_alloc_buffer (GST_BASE_SRC_PAD (GST_BASE_SRC (psrc)),
426 src->read_offset, size,
427 //src->icy_caps ? src->icy_caps :
428 GST_PAD_CAPS (GST_BASE_SRC_PAD (GST_BASE_SRC (psrc))), outbuf);
430 if (G_UNLIKELY (ret != GST_FLOW_OK))
433 //g_static_mutex_lock( &update_size_mutex );
434 read = do_read_request_response ( src, src->read_offset, size, outbuf );
435 //g_static_mutex_unlock( &update_size_mutex );
437 if (G_UNLIKELY (src->update_prog_chain) )
438 goto change_progchain;
440 if (G_UNLIKELY (read <= 0) || *outbuf == NULL) {
442 goto change_progchain;
449 const gchar *reason = gst_flow_get_name (ret);
451 GST_DEBUG_OBJECT (src, "DONE task, reason %s", reason);
456 const gchar *reason = gst_flow_get_name (ret);
458 GST_DEBUG_OBJECT (src, "pausing task, reason %s", reason);
459 return GST_FLOW_UNEXPECTED;
464 GST_ELEMENT_ERROR (src, RESOURCE, READ,
465 (NULL), ("Could not read any bytes (%i, %s)", read,
467 return GST_FLOW_ERROR;
471 GST_ELEMENT_ERROR (src, RESOURCE, READ,
472 (NULL), ("Seek failed, go to the next program info... (%i, %s)", read,
474 // go to the next program chain
475 src->unique_setup = FALSE;
476 src->update_prog_chain = TRUE;
477 gst_mythtv_src_next_program_chain( src );
484 update_size_func( void *mythtv_data )
488 g_return_if_fail( mythtv_data != NULL );
490 src = GST_MYTHTV_SRC ( mythtv_data );
492 g_static_mutex_lock( &update_size_mutex );
494 if ( src->do_start ) {
495 if ( src->enable_timing_position ) {
497 if (src->live_tv == TRUE) {
500 size_tmp = gmyth_file_transfer_get_file_position( src->file_transfer );
501 if ( size_tmp > ( src->content_size + GMYTHTV_TRANSFER_MAX_BUFFER ) )
502 src->content_size = size_tmp;
503 else if ( size_tmp > 0 )
505 g_print( "\t[%s]\tGET_POSITION: file_position = %lld\n",
506 __FUNCTION__, size_tmp );
510 g_static_mutex_unlock( &update_size_mutex );
515 gst_mythtv_src_get_position ( GstMythtvSrc* src )
519 if (src->live_tv == TRUE) {
522 size_tmp = gmyth_file_transfer_get_file_position( src->file_transfer );
523 if ( size_tmp > ( src->content_size + GMYTHTV_TRANSFER_MAX_BUFFER ) )
524 src->content_size = size_tmp;
525 else if ( size_tmp > 0 )
527 g_print( "\t[%s]\tGET_POSITION: file_position = %lld\n",
528 __FUNCTION__, size_tmp );
531 return src->content_size;
535 /* create a socket for connecting to remote server */
537 gst_mythtv_src_start ( GstBaseSrc * bsrc )
539 GstMythtvSrc *src = GST_MYTHTV_SRC (bsrc);
541 GString *chain_id_local = NULL;
545 if ( !src->do_start )
548 if (src->unique_setup == FALSE) {
549 src->unique_setup = TRUE;
554 GST_OBJECT_LOCK(src);
556 if ( src->live_tv ) {
557 src->spawn_livetv = gmyth_livetv_new( );
558 if ( gmyth_livetv_setup( src->spawn_livetv ) == FALSE ) {
560 GST_OBJECT_UNLOCK( src );
564 /* set up the uri variable */
565 src->uri_name = g_strdup( src->spawn_livetv->proginfo->pathname->str );
566 chain_id_local = gmyth_tvchain_get_id( src->spawn_livetv->tvchain );
567 if ( chain_id_local != NULL ) {
568 src->live_chain_id = g_strdup( chain_id_local->str );
569 g_print( "\t[%s]\tLocal chain ID = %s.\n", __FUNCTION__, src->live_chain_id );
571 src->live_tv_id = src->spawn_livetv->remote_encoder->recorder_num;
572 g_print ( "[%s] LiveTV id = %d, URI path = %s.\n", __FUNCTION__, src->live_tv_id, src->uri_name );
575 src->file_transfer = gmyth_file_transfer_new( src->live_tv_id,
576 g_string_new( src->uri_name ), -1, src->mythtv_version );
578 if ( src->file_transfer == NULL ) {
579 GST_OBJECT_UNLOCK(src);
584 /* sets the Playback monitor connection */
585 ret = gmyth_file_transfer_playback_setup( &(src->file_transfer), src->live_tv );
587 if ( src->live_tv == TRUE && ret == TRUE ) {
588 /* loop finished, set the max tries variable to zero again... */
589 wait_to_transfer = 0;
591 while ( wait_to_transfer++ < GMYTHTV_TRANSFER_MAX_WAITS && ( gmyth_file_transfer_is_recording( src->file_transfer ) == FALSE
592 /*|| ( gmyth_file_transfer_get_file_position( src->file_transfer ) < ( src->content_size + 327680 ) )*/ ) )
596 /* sets the FileTransfer instance connection (video/audio download) */
597 ret = gmyth_file_transfer_setup( &(src->file_transfer), src->live_tv );
599 if ( ret == FALSE ) {
600 GST_OBJECT_UNLOCK(src);
601 #ifndef GST_DISABLE_GST_DEBUG
602 if ( src->mythtv_msgs_dbg )
603 g_printerr( "MythTV FileTransfer request failed when setting up socket connection!\n" );
605 goto begin_req_failed;
608 src->content_size = src->file_transfer->filesize;
610 GST_OBJECT_UNLOCK(src);
612 src->do_start = FALSE;
620 if (src->spawn_livetv != NULL )
621 g_object_unref( src->spawn_livetv );
623 GST_ELEMENT_ERROR (src, LIBRARY, INIT,
624 (NULL), ("Could not initialize MythTV library (%i, %s)", ret, src->uri_name));
629 GST_ELEMENT_ERROR (src, LIBRARY, INIT,
630 (NULL), ("Could not begin request sent to MythTV server (%i, %s)", ret, src->uri_name));
635 /* create a new socket for connecting to the next program chain */
637 gst_mythtv_src_next_program_chain ( GstMythtvSrc *src )
639 GString *chain_id_local = NULL;
646 if (src->file_transfer) {
647 g_object_unref (src->file_transfer);
648 src->file_transfer = NULL;
652 g_free (src->uri_name);
655 if ( src->live_tv ) {
656 if ( src->update_prog_chain )
658 if ( gmyth_livetv_next_program_chain( src->spawn_livetv ) == FALSE ) {
662 src->update_prog_chain = FALSE;
664 /* set up the uri variable */
665 src->uri_name = g_strdup( src->spawn_livetv->proginfo->pathname->str );
666 chain_id_local = gmyth_tvchain_get_id( src->spawn_livetv->tvchain );
667 if ( chain_id_local != NULL ) {
668 src->live_chain_id = g_strdup( chain_id_local->str );
669 g_print( "\t[%s]\tLocal chain ID = %s.\n", __FUNCTION__, src->live_chain_id );
671 src->live_tv_id = src->spawn_livetv->remote_encoder->recorder_num;
672 g_print ( "[%s] LiveTV id = %d, URI path = %s.\n", __FUNCTION__, src->live_tv_id, src->uri_name );
675 src->file_transfer = gmyth_file_transfer_new( src->live_tv_id,
676 g_string_new( src->uri_name ), -1, src->mythtv_version );
678 if ( src->file_transfer == NULL ) {
682 /* sets the Playback monitor connection */
683 ret = gmyth_file_transfer_playback_setup( &(src->file_transfer), src->live_tv );
685 if ( src->live_tv == TRUE && ret == TRUE ) {
686 /* loop finished, set the max tries variable to zero again... */
687 wait_to_transfer = 0;
691 while ( wait_to_transfer++ < GMYTHTV_TRANSFER_MAX_WAITS &&
692 ( gmyth_file_transfer_is_recording( src->file_transfer ) == FALSE ) )
696 /* sets the FileTransfer instance connection (video/audio download) */
697 ret = gmyth_file_transfer_setup( &(src->file_transfer), src->live_tv );
699 if ( ret == FALSE ) {
700 #ifndef GST_DISABLE_GST_DEBUG
701 if ( src->mythtv_msgs_dbg )
702 g_printerr( "MythTV FileTransfer request failed when setting up socket connection!\n" );
704 goto begin_req_failed;
707 if ( src->content_size < src->file_transfer->filesize ) {
708 src->content_size = src->file_transfer->filesize;
710 gint64 pos = gst_mythtv_src_get_position(src);
711 if ( pos > src->file_transfer->filesize )
712 src->content_size = pos;
715 src->update_prog_chain = FALSE;
722 if (src->spawn_livetv != NULL )
723 g_object_unref( src->spawn_livetv );
725 GST_ELEMENT_ERROR (src, LIBRARY, INIT,
726 (NULL), ("Could not initialize MythTV library (%i, %s)", ret, src->uri_name));
731 GST_ELEMENT_ERROR (src, LIBRARY, INIT,
732 (NULL), ("Could not begin request sent to MythTV server (%i, %s)", ret, src->uri_name));
739 /* handles queries for location in the stream in the requested format */
741 gst_mythtv_src_query ( GstPad * pad, GstQuery * query )
744 GstMythtvSrc *mythtv;
748 mythtv = GST_GMYTHTV_SRC( GST_PAD_PARENT (pad) );
750 size = gst_mythtv_src_get_position (mythtv);
752 switch (GST_QUERY_TYPE (query)) {
754 case GST_QUERY_POSITION:
760 /* save requested format */
761 gst_query_parse_position (query, NULL, &cur);
763 /* query peer for current position in time */
764 g_print( "[%s] Actual size is %s than current size from sink. [ %lld, %lld ]\n", __FUNCTION__,
765 ( size > cur ) ? "greater" : "lower", size, cur );
766 gst_query_set_position (query, GST_FORMAT_BYTES, size);
773 case GST_QUERY_DURATION:
778 /* save requested format */
779 gst_query_parse_position (query, NULL, &cur);
781 /* query peer for current position in time */
782 g_print( "[%s] Actual size is %s than current size from sink. [ %lld, %lld ]\n", __FUNCTION__,
783 ( size * GST_SECOND > cur * GST_SECOND ) ? "greater" : "lower", size * GST_SECOND,
785 gst_query_set_position (query, GST_FORMAT_TIME, size * GST_SECOND );
787 if ( size * GST_SECOND < cur * GST_SECOND )
807 gst_mythtv_src_get_size (GstBaseSrc * bsrc, guint64 * size)
809 GstMythtvSrc *src = GST_MYTHTV_SRC (bsrc);
812 if (src->content_size <= 0) {
814 } else if ( abs ( src->content_size - src->read_offset ) <= GMYTHTV_TRANSFER_MAX_BUFFER ) {
815 //g_static_mutex_lock( &update_size_mutex );
816 //GST_OBJECT_LOCK(src);
818 gint64 new_offset = gmyth_file_transfer_get_file_position( src->file_transfer );
819 if ( new_offset > 0 ) {
820 if ( src->content_size < new_offset ) {
821 src->content_size = new_offset;
825 src->update_prog_chain = TRUE;
828 if ( src->enable_timing_position ) {
830 if (src->live_tv == TRUE) {
833 size_tmp = gmyth_file_transfer_get_file_position( src->file_transfer );
834 if ( size_tmp > ( src->content_size + GMYTHTV_TRANSFER_MAX_BUFFER ) )
835 src->content_size = size_tmp;
836 else if ( size_tmp > 0 )
838 g_print( "\t[%s]\tGET_POSITION: file_position = %lld\n",
839 __FUNCTION__, size_tmp );
842 //GST_OBJECT_UNLOCK(src);
843 //g_static_mutex_unlock( &update_size_mutex );
846 *size = src->content_size;
847 g_print( "[%s] Content size = %lld\n", __FUNCTION__, src->content_size );
853 /* close the socket and associated resources
854 * used both to recover from errors and go to NULL state */
856 gst_mythtv_src_stop (GstBaseSrc * bsrc)
860 src = GST_MYTHTV_SRC (bsrc);
863 g_free (src->uri_name);
864 src->uri_name = NULL;
867 if (src->mythtv_caps) {
868 gst_caps_unref (src->mythtv_caps);
869 src->mythtv_caps = NULL;
878 gst_mythtv_src_handle_event (GstPad * pad, GstEvent * event)
880 GstMythtvSrc *src = GST_MYTHTV_SRC (GST_PAD_PARENT (pad));
881 gint64 cont_size = 0;
883 switch (GST_EVENT_TYPE (event)) {
885 case GST_EVENT_FLUSH_START:
887 g_print( "\n\n\n[%s]\t\tGot FLUSH_START event!!!\n\n\n", __FUNCTION__ );
888 cont_size = gst_mythtv_src_get_position (src);
889 if ( !src->live_tv ) {
890 if ( cont_size > src->content_size ) {
891 src->content_size = cont_size;
895 gst_element_set_state ( GST_ELEMENT (src), GST_STATE_NULL );
896 gst_element_set_locked_state ( GST_ELEMENT (src), FALSE );
899 if ( cont_size <= 0 ) {
900 src->update_prog_chain = TRUE;
902 src->unique_setup = FALSE;
903 src->do_start = TRUE;
907 case GST_EVENT_FLUSH_STOP:
908 src->do_start = TRUE;
910 gst_element_set_state (GST_ELEMENT(src), GST_STATE_NULL);
911 //gst_element_set_locked_state (GST_ELEMENT(src), TRUE);
915 g_print( "[%s] Got EOS event!!!\n", __FUNCTION__ );
916 cont_size = gst_mythtv_src_get_position (src);
917 if ( !src->live_tv ) {
918 if ( cont_size > src->content_size ) {
919 src->content_size = cont_size;
923 gst_element_set_state ( GST_ELEMENT (src), GST_STATE_NULL );
924 gst_element_set_locked_state ( GST_ELEMENT (src), FALSE );
930 case GST_EVENT_NEWSEGMENT:
931 g_print( "[%s] Got NEWSEGMENT!!!\n", __FUNCTION__ );
936 g_print( "[%s] Got EVENT_SEEK!!!\n", __FUNCTION__ );
938 //gboolean update = TRUE;
940 GstSeekType cur_type, stop_type;
942 gint64 cur = 0, stop = 0;
943 gst_event_parse_seek ( event, &rate, &format,
944 &flags, &cur_type, &cur,
947 g_print( "[%s] Got EVENT_SEEK.\n", __FUNCTION__ );
948 if ( !( flags & GST_SEEK_FLAG_FLUSH ) ) {
949 g_print( "[%s] Could get the FLAG_FLUSH message.\n", __FUNCTION__ );
951 //gboolean ret = gst_event_parse_new_segment ( event,
952 // &update, &rate, &format, &start, &stop,
954 //GstFlowReturn flow_ret = gst_mythtv_src_create (GST_BASE_SRC( GST_PAD_PARENT( psrc ) ),
955 // cur, stop - cur + 1, GstBuffer)
959 return gst_pad_event_default (pad, event);
962 return gst_pad_event_default (pad, event);
966 gst_mythtv_src_is_seekable( GstBaseSrc *push_src )
971 static GstStateChangeReturn
972 gst_mythtv_src_change_state (GstElement * element, GstStateChange transition)
974 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
975 GstMythtvSrc *src = GST_MYTHTV_SRC (element);
977 switch (transition) {
978 case GST_STATE_CHANGE_NULL_TO_READY:
979 src->content_size = 0;
980 src->read_offset = 0;
982 src->do_start = TRUE;
983 src->unique_setup = FALSE;
986 case GST_STATE_CHANGE_READY_TO_PAUSED:
987 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
994 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
995 if (ret == GST_STATE_CHANGE_FAILURE)
998 switch (transition) {
999 case GST_STATE_CHANGE_READY_TO_NULL:
1001 case GST_STATE_CHANGE_PAUSED_TO_READY:
1002 if ( src->live_tv && src->update_prog_chain )
1003 gst_mythtv_src_next_program_chain( src );
1013 gst_mythtv_src_set_property (GObject * object, guint prop_id,
1014 const GValue * value, GParamSpec * pspec)
1016 GstMythtvSrc *mythtvsrc = GST_MYTHTV_SRC (object);
1018 GST_OBJECT_LOCK (mythtvsrc);
1023 if (!g_value_get_string (value)) {
1024 GST_WARNING ("location property cannot be NULL");
1028 if (mythtvsrc->uri_name != NULL) {
1029 g_free (mythtvsrc->uri_name);
1030 mythtvsrc->uri_name = NULL;
1032 mythtvsrc->uri_name = g_value_dup_string (value);
1036 #ifndef GST_DISABLE_GST_DEBUG
1037 case PROP_GMYTHTV_DBG:
1039 mythtvsrc->mythtv_msgs_dbg = g_value_get_boolean (value);
1043 case PROP_GMYTHTV_VERSION:
1045 mythtvsrc->mythtv_version = g_value_get_int (value);
1048 case PROP_GMYTHTV_LIVEID:
1050 mythtvsrc->live_tv_id = g_value_get_int (value);
1053 case PROP_GMYTHTV_LIVE:
1055 mythtvsrc->live_tv = g_value_get_boolean (value);
1058 case PROP_GMYTHTV_ENABLE_TIMING_POSITION:
1060 mythtvsrc->enable_timing_position = g_value_get_boolean (value);
1063 case PROP_GMYTHTV_LIVE_CHAINID:
1065 if (!g_value_get_string (value)) {
1066 GST_WARNING ("MythTV Live chainid property cannot be NULL");
1070 if (mythtvsrc->live_chain_id != NULL) {
1071 g_free (mythtvsrc->live_chain_id);
1072 mythtvsrc->live_chain_id = NULL;
1074 mythtvsrc->live_chain_id = g_value_dup_string (value);
1079 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1082 GST_OBJECT_UNLOCK (mythtvsrc);
1088 gst_mythtv_src_get_property (GObject * object, guint prop_id,
1089 GValue * value, GParamSpec * pspec)
1091 GstMythtvSrc *mythtvsrc = GST_MYTHTV_SRC (object);
1093 GST_OBJECT_LOCK (mythtvsrc);
1098 gchar *str = g_strdup( "" );
1100 if ( mythtvsrc->uri_name == NULL ) {
1101 g_free (mythtvsrc->uri_name);
1102 mythtvsrc->uri_name = NULL;
1104 str = g_strdup( mythtvsrc->uri_name );
1106 g_value_set_string ( value, str );
1109 #ifndef GST_DISABLE_GST_DEBUG
1110 case PROP_GMYTHTV_DBG:
1111 g_value_set_boolean ( value, mythtvsrc->mythtv_msgs_dbg );
1114 case PROP_GMYTHTV_VERSION:
1116 g_value_set_int ( value, mythtvsrc->mythtv_version );
1119 case PROP_GMYTHTV_LIVEID:
1121 g_value_set_int ( value, mythtvsrc->live_tv_id );
1124 case PROP_GMYTHTV_LIVE:
1125 g_value_set_boolean ( value, mythtvsrc->live_tv );
1127 case PROP_GMYTHTV_ENABLE_TIMING_POSITION:
1128 g_value_set_boolean ( value, mythtvsrc->enable_timing_position );
1130 case PROP_GMYTHTV_LIVE_CHAINID:
1132 gchar *str = g_strdup( "" );
1134 if ( mythtvsrc->live_chain_id == NULL ) {
1135 g_free (mythtvsrc->live_chain_id);
1136 mythtvsrc->live_chain_id = NULL;
1138 str = g_strdup( mythtvsrc->live_chain_id );
1140 g_value_set_string ( value, str );
1144 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1147 GST_OBJECT_UNLOCK (mythtvsrc);
1150 /* entry point to initialize the plug-in
1151 * initialize the plug-in itself
1152 * register the element factories and pad templates
1153 * register the features
1156 plugin_init (GstPlugin * plugin)
1158 return gst_element_register (plugin, "mythtvsrc", GST_RANK_NONE,
1159 GST_TYPE_MYTHTV_SRC);
1162 /* this is the structure that gst-register looks for
1163 * so keep the name plugin_desc, or you cannot get your plug-in registered */
1164 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1168 plugin_init, VERSION, "LGPL", "GStreamer", "http://gstreamer.net/")
1171 /*** GSTURIHANDLER INTERFACE *************************************************/
1173 gst_mythtv_src_uri_get_type (void)
1179 gst_mythtv_src_uri_get_protocols (void)
1181 static gchar *protocols[] = { "myth", "myths", NULL };
1186 static const gchar *
1187 gst_mythtv_src_uri_get_uri (GstURIHandler * handler)
1189 GstMythtvSrc *src = GST_MYTHTV_SRC (handler);
1191 return src->uri_name;
1195 gst_mythtv_src_uri_set_uri (GstURIHandler * handler, const gchar * uri)
1197 GstMythtvSrc *src = GST_MYTHTV_SRC (handler);
1201 protocol = gst_uri_get_protocol (uri);
1202 if ((strcmp (protocol, "myth") != 0) && (strcmp (protocol, "myths") != 0)) {
1207 g_object_set (src, "location", uri, NULL);
1213 gst_mythtv_src_uri_handler_init (gpointer g_iface, gpointer iface_data)
1215 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
1217 iface->get_type = gst_mythtv_src_uri_get_type;
1218 iface->get_protocols = gst_mythtv_src_uri_get_protocols;
1219 iface->get_uri = gst_mythtv_src_uri_get_uri;
1220 iface->set_uri = gst_mythtv_src_uri_set_uri;
1224 size_header_handler (void *userdata, const char *value)
1226 GstMythtvSrc *src = GST_MYTHTV_SRC (userdata);
1228 //src->content_size = g_ascii_strtoull (value, NULL, 10);
1230 GST_DEBUG_OBJECT (src, "content size = %lld bytes", src->content_size);