[svn r72] Added some LiveTV performance optimizations.
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 GST_GMYTHTV_CHANNEL_NUM 1000
37 #define GMYTHTV_VERSION_DEFAULT 30
39 #define GMYTHTV_TRANSFER_MAX_WAITS 100
41 #define GMYTHTV_TRANSFER_MAX_BUFFER 4*1024
45 #define MAX_READ_SIZE 4*1024
48 #define GST_FLOW_ERROR_NO_DATA -101
50 /* stablish a maximum iteration value to the IS_RECORDING message */
51 static guint wait_to_transfer = 0;
53 static const GstElementDetails gst_mythtv_src_details =
54 GST_ELEMENT_DETAILS ( "MythTV client source",
56 "Control and receive data as a client over the network via raw socket connections using the MythTV protocol",
57 "Rosfran Borges <rosfran.borges@indt.org.br>" );
59 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
62 GST_STATIC_CAPS ("video/x-nuv") );
65 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
68 GST_STATIC_CAPS ("video/x-nuv") );
76 #ifndef GST_DISABLE_GST_DEBUG
82 PROP_GMYTHTV_LIVE_CHAINID,
83 PROP_GMYTHTV_ENABLE_TIMING_POSITION,
84 PROP_GMYTHTV_CHANNEL_NUM
87 static void gst_mythtv_src_finalize (GObject * gobject);
89 static GstFlowReturn gst_mythtv_src_create (GstBaseSrc * psrc, guint64 offset,
90 guint size, GstBuffer ** outbuf);
92 static gboolean gst_mythtv_src_start (GstBaseSrc * bsrc);
93 static gboolean gst_mythtv_src_stop (GstBaseSrc * bsrc);
94 static gboolean gst_mythtv_src_get_size (GstBaseSrc * bsrc, guint64 * size);
95 static gboolean gst_mythtv_src_is_seekable( GstBaseSrc *push_src );
97 static gboolean gst_mythtv_src_next_program_chain ( GstMythtvSrc *src );
99 static GstStateChangeReturn
100 gst_mythtv_src_change_state (GstElement * element, GstStateChange transition);
102 static void gst_mythtv_src_set_property (GObject * object, guint prop_id,
103 const GValue * value, GParamSpec * pspec);
104 static void gst_mythtv_src_get_property (GObject * object, guint prop_id,
105 GValue * value, GParamSpec * pspec);
107 static void gst_mythtv_src_uri_handler_init (gpointer g_iface, gpointer iface_data);
109 //static gboolean gst_mythtv_src_handle_event (GstPad * pad, GstEvent * event);
110 //static gboolean gst_mythtv_src_query ( GstPad * pad, GstQuery * query );
112 static gint do_read_request_response (GstMythtvSrc *src, guint64 offset,
113 guint size, GstBuffer **outbuf);
116 _urihandler_init (GType type)
118 static const GInterfaceInfo urihandler_info = {
119 gst_mythtv_src_uri_handler_init,
124 g_type_add_interface_static (type, GST_TYPE_URI_HANDLER, &urihandler_info);
126 GST_DEBUG_CATEGORY_INIT (mythtvsrc_debug, "mythtvsrc", 0,
130 GST_BOILERPLATE_FULL (GstMythtvSrc, gst_mythtv_src, GstBaseSrc,
131 GST_TYPE_BASE_SRC, _urihandler_init)
133 //GST_BOILERPLATE_FULL (GstMythtvSrc, gst_mythtv_src, GstPushSrc,
134 // GST_TYPE_PUSH_SRC, _urihandler_init)
137 gst_mythtv_src_base_init (gpointer g_class)
139 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
141 gst_element_class_add_pad_template (element_class,
142 gst_static_pad_template_get (&srctemplate));
144 gst_element_class_set_details (element_class, &gst_mythtv_src_details);
146 element_class->change_state = gst_mythtv_src_change_state;
150 gst_mythtv_src_class_init (GstMythtvSrcClass * klass)
152 GObjectClass *gobject_class;
153 //GstPushSrcClass *gstpushsrc_class;
154 GstBaseSrcClass *gstbasesrc_class;
156 gobject_class = (GObjectClass *) klass;
157 gstbasesrc_class = (GstBaseSrcClass *) klass;
159 gobject_class->set_property = gst_mythtv_src_set_property;
160 gobject_class->get_property = gst_mythtv_src_get_property;
161 gobject_class->finalize = gst_mythtv_src_finalize;
163 g_object_class_install_property
164 (gobject_class, PROP_LOCATION,
165 g_param_spec_string ("location", "Location",
166 "The location. In the form:"
167 "\n\t\t\tmyth://a.com/file.nuv"
168 "\n\t\t\tmyth://a.com:23223/file.nuv "
169 "\n\t\t\ta.com/file.nuv - default scheme 'myth'",
170 "", G_PARAM_READWRITE));
172 g_object_class_install_property
173 (gobject_class, PROP_URI,
174 g_param_spec_string ("uri", "Uri",
175 "The location in form of a URI (deprecated; use location)",
176 "", G_PARAM_READWRITE));
178 g_object_class_install_property
179 (gobject_class, PROP_GMYTHTV_VERSION,
180 g_param_spec_int ("mythtv-version", "mythtv-version",
181 "Change MythTV version",
182 26, 30, 26, G_PARAM_READWRITE));
184 g_object_class_install_property
185 (gobject_class, PROP_GMYTHTV_LIVEID,
186 g_param_spec_int ("mythtv-live-id", "mythtv-live-id",
187 "Change MythTV version",
188 0, 200, GST_GMYTHTV_ID_NUM, G_PARAM_READWRITE));
190 g_object_class_install_property
191 (gobject_class, PROP_GMYTHTV_LIVE_CHAINID,
192 g_param_spec_string ("mythtv-live-chainid", "mythtv-live-chainid",
193 "Sets the MythTV chain ID (from TV Chain)",
194 "", G_PARAM_READWRITE));
196 g_object_class_install_property
197 (gobject_class, PROP_GMYTHTV_LIVE,
198 g_param_spec_boolean ("mythtv-live", "mythtv-live",
199 "Enable MythTV Live TV content streaming",
200 FALSE, G_PARAM_READWRITE));
202 g_object_class_install_property
203 (gobject_class, PROP_GMYTHTV_ENABLE_TIMING_POSITION,
204 g_param_spec_boolean ("mythtv-enable-timing-position", "mythtv-enable-timing-position",
205 "Enable MythTV Live TV content size continuous updating",
206 FALSE, G_PARAM_READWRITE));
208 g_object_class_install_property
209 (gobject_class, PROP_GMYTHTV_CHANNEL_NUM,
210 g_param_spec_int ("mythtv-channel", "mythtv-channel",
211 "Change MythTV channel number",
212 0, 99999, GST_GMYTHTV_CHANNEL_NUM, G_PARAM_READWRITE));
214 #ifndef GST_DISABLE_GST_DEBUG
215 g_object_class_install_property
216 (gobject_class, PROP_GMYTHTV_DBG,
217 g_param_spec_boolean ("mythtv-debug", "mythtv-debug",
218 "Enable MythTV debug messages",
219 FALSE, G_PARAM_READWRITE));
222 gstbasesrc_class->start = gst_mythtv_src_start;
223 gstbasesrc_class->stop = gst_mythtv_src_stop;
224 gstbasesrc_class->get_size = gst_mythtv_src_get_size;
225 gstbasesrc_class->is_seekable = gst_mythtv_src_is_seekable;
227 gstbasesrc_class->create = gst_mythtv_src_create;
229 GST_DEBUG_CATEGORY_INIT (mythtvsrc_debug, "mythtvsrc", 0,
230 "MythTV Client Source");
234 gst_mythtv_src_init (GstMythtvSrc * this, GstMythtvSrcClass * g_class)
236 this->file_transfer = NULL;
238 this->unique_setup = FALSE;
240 this->mythtv_version = GMYTHTV_VERSION_DEFAULT;
242 this->state = GST_MYTHTV_SRC_FILE_TRANSFER;
244 this->bytes_read = 0;
246 this->prev_content_size = 0;
248 this->content_size = 0;
249 this->read_offset = 0;
251 this->content_size_last = 0;
253 this->live_tv = FALSE;
255 this->enable_timing_position = FALSE;
256 this->update_prog_chain = FALSE;
258 this->user_agent = g_strdup ("mythtvsrc");
259 this->mythtv_caps = NULL;
260 this->update_prog_chain = FALSE;
262 this->channel_num = 0;
266 this->adapter = NULL;
268 //this->sinkpad = gst_pad_new_from_static_template (&sinktemplate, "sink");
269 //gst_element_add_pad (GST_ELEMENT (this), this->sinkpad);
272 this->srcpad = gst_pad_new_from_static_template (&srctemplate, "src");
273 gst_element_add_pad (GST_ELEMENT (this), this->srcpad);
276 gst_base_src_set_format( GST_BASE_SRC( this ), GST_FORMAT_BYTES );
278 gst_base_src_set_live ( GST_BASE_SRC( this ), TRUE );
280 // gst_pad_set_event_function ( GST_BASE_SRC_PAD(GST_BASE_SRC(this)),
281 // gst_mythtv_src_handle_event );
283 gst_pad_set_query_function ( GST_BASE_SRC_PAD(GST_BASE_SRC(this)),
284 gst_mythtv_src_query );
290 gst_mythtv_src_finalize (GObject * gobject)
292 GstMythtvSrc *this = GST_MYTHTV_SRC (gobject);
294 if (this->mythtv_caps) {
295 gst_caps_unref (this->mythtv_caps);
296 this->mythtv_caps = NULL;
299 if (this->file_transfer) {
300 g_object_unref (this->file_transfer);
301 this->file_transfer = NULL;
304 if (this->spawn_livetv) {
305 g_object_unref (this->spawn_livetv);
306 this->spawn_livetv = NULL;
309 if (this->uri_name) {
310 g_free (this->uri_name);
313 if (this->user_agent) {
314 g_free (this->user_agent);
317 G_OBJECT_CLASS (parent_class)->finalize (gobject);
321 do_read_request_response (GstMythtvSrc * src, guint64 offset, guint size, GstBuffer **outbuf)
324 guint sizetoread = size;
326 g_print( "Starting: [%s] Reading %d bytes...\n", __FUNCTION__, sizetoread );
328 /* Loop sending the Myth File Transfer request:
329 * Retry whilst authentication fails and we supply it. */
332 GST_OBJECT_LOCK(src);
334 while ( sizetoread > 0 ) {
336 len = gmyth_file_transfer_read( src->file_transfer,
337 GST_BUFFER_DATA( *outbuf ) + read, sizetoread, TRUE );
347 if ( src->live_tv == FALSE )
353 if ( len == GMYTHTV_FILE_TRANSFER_READ_ERROR ) { /* -314 */
354 src->update_prog_chain = TRUE;
357 if ( abs( src->content_size - src->bytes_read ) < GMYTHTV_TRANSFER_MAX_BUFFER ) {
358 src->update_prog_chain = TRUE;
359 if ( src->enable_timing_position ) {
362 size_tmp = gmyth_file_transfer_get_file_position( src->file_transfer );
363 if ( size_tmp > ( src->content_size + GMYTHTV_TRANSFER_MAX_BUFFER ) )
364 src->content_size = size_tmp;
365 else if ( size_tmp > 0 )
367 g_print( "\t[%s]\tGET_POSITION: file_position = %lld\n",
368 __FUNCTION__, size_tmp );
369 } else if ( abs( src->content_size - src->bytes_read ) < GMYTHTV_TRANSFER_MAX_BUFFER ) {
370 src->prev_content_size = src->content_size;
371 gint64 new_offset = gmyth_file_transfer_get_file_position( src->file_transfer );
372 if ( new_offset > 0 && src->content_size <= new_offset ) {
373 src->content_size = new_offset;
375 src->update_prog_chain = TRUE;
385 if ( read == sizetoread )
390 src->read_offset += read;
391 src->bytes_read += read;
392 //src->content_size += src->bytes_read;
394 g_print( "[%s]\tBYTES READ (actual) = %d, BYTES READ (cumulative) = %llu, "\
395 "OFFSET = %llu, CONTENT SIZE = %llu.\n", __FUNCTION__, read, src->bytes_read,
396 src->read_offset, src->content_size );
398 GST_BUFFER_SIZE (*outbuf) = read; //GST_BUFFER_SIZE (buffer) = read;
399 GST_BUFFER_OFFSET (*outbuf) = offset; //GST_BUFFER_OFFSET (buffer) = offset;
400 GST_BUFFER_OFFSET_END (*outbuf) = offset + read;//GST_BUFFER_OFFSET_END (buffer) = offset + read;
402 g_print( "Got buffer: [%s]\t\tBUFFER --->SIZE = %d, OFFSET = %llu, "\
403 "OFFSET_END = %llu.\n\n", __FUNCTION__, GST_BUFFER_SIZE (*outbuf),
404 GST_BUFFER_OFFSET (*outbuf), GST_BUFFER_OFFSET_END (*outbuf) );
406 } else if ( !src->live_tv )
415 GST_OBJECT_UNLOCK(src);
421 gst_mythtv_src_create ( GstBaseSrc * psrc, guint64 offset, guint size, GstBuffer **outbuf)
424 GstFlowReturn ret = GST_FLOW_OK;
427 src = GST_MYTHTV_SRC (psrc);
428 /* The caller should know the number of bytes and not read beyond EOS. */
429 if (G_UNLIKELY (src->eos))
431 if ( G_UNLIKELY (src->update_prog_chain) )
432 goto change_progchain;
434 GST_OBJECT_LOCK(src);
436 if (G_UNLIKELY (src->read_offset != offset)) {
437 gint64 new_offset = gmyth_file_transfer_seek(src->file_transfer, offset, SEEK_SET);
438 g_print( "[%s] SRC Offset = %lld, NEW actual backend SEEK Offset = %lld.\n",
439 __FUNCTION__, src->read_offset, new_offset );
440 if (G_UNLIKELY (new_offset < 0 ) )//|| new_offset != src->read_offset)) {
442 GST_OBJECT_UNLOCK(src);
444 goto change_progchain;
449 src->read_offset = offset;
451 GST_OBJECT_UNLOCK(src);
453 /* Create the buffer. */
454 ret = gst_pad_alloc_buffer (GST_BASE_SRC_PAD (GST_BASE_SRC (psrc)),
455 src->read_offset, size,
456 //src->icy_caps ? src->icy_caps :
457 GST_PAD_CAPS (GST_BASE_SRC_PAD (GST_BASE_SRC (psrc))), outbuf);
459 if (G_UNLIKELY (ret != GST_FLOW_OK)) {
461 goto change_progchain;
466 read = do_read_request_response ( src, src->read_offset, size, outbuf );
468 if (G_UNLIKELY (src->update_prog_chain) )
469 goto change_progchain;
471 if (G_UNLIKELY (read <= 0) || *outbuf == NULL) {
473 goto change_progchain;
480 const gchar *reason = gst_flow_get_name (ret);
482 GST_DEBUG_OBJECT (src, "DONE task, reason %s", reason);
487 const gchar *reason = gst_flow_get_name (ret);
489 GST_DEBUG_OBJECT (src, "pausing task, reason %s", reason);
490 return GST_FLOW_UNEXPECTED;
495 GST_ELEMENT_ERROR (src, RESOURCE, READ,
496 (NULL), ("Could not read any bytes (%i, %s)", read,
498 return GST_FLOW_ERROR;
502 GST_ELEMENT_ERROR (src, RESOURCE, READ,
503 (NULL), ("Seek failed, go to the next program info... (%i, %s)", read,
505 // go to the next program chain
506 src->unique_setup = FALSE;
507 src->update_prog_chain = TRUE;
509 //GST_OBJECT_LOCK(src);
510 gst_mythtv_src_next_program_chain( src );
511 //GST_OBJECT_UNLOCK(src);
513 return GST_FLOW_ERROR_NO_DATA;
519 gst_mythtv_src_get_position ( GstMythtvSrc* src )
524 if (src->live_tv == TRUE && ( abs( src->content_size - src->bytes_read ) <
525 GMYTHTV_TRANSFER_MAX_BUFFER ) ) {
529 size_tmp = gmyth_file_transfer_get_file_position( src->file_transfer );
530 if ( size_tmp > ( src->content_size + GMYTHTV_TRANSFER_MAX_BUFFER ) )
531 src->content_size = size_tmp;
532 else if ( size_tmp > 0 && --max_tries > 0 )
534 g_print( "\t[%s]\tGET_POSITION: file_position = %lld\n",
535 __FUNCTION__, size_tmp );
536 /* sets the last content size amount before it can be updated */
537 src->prev_content_size = src->content_size;
540 return src->content_size;
544 /* create a socket for connecting to remote server */
546 gst_mythtv_src_start ( GstBaseSrc * bsrc )
548 GstMythtvSrc *src = GST_MYTHTV_SRC (bsrc);
550 GString *chain_id_local = NULL;
554 if ( G_UNLIKELY (src->update_prog_chain) )
555 goto change_progchain;
557 if (src->unique_setup == FALSE) {
558 src->unique_setup = TRUE;
563 GST_OBJECT_LOCK(src);
565 if ( src->live_tv ) {
566 src->spawn_livetv = gmyth_livetv_new( );
567 if ( gmyth_livetv_setup( src->spawn_livetv ) == FALSE ) {
569 GST_OBJECT_UNLOCK( src );
573 /* set up the uri variable */
574 src->uri_name = g_strdup( src->spawn_livetv->proginfo->pathname->str );
575 chain_id_local = gmyth_tvchain_get_id( src->spawn_livetv->tvchain );
576 if ( chain_id_local != NULL ) {
577 src->live_chain_id = g_strdup( chain_id_local->str );
578 g_print( "\t[%s]\tLocal chain ID = %s.\n", __FUNCTION__, src->live_chain_id );
580 src->live_tv_id = src->spawn_livetv->recorder->recorder_num;
581 g_print ( "[%s] LiveTV id = %d, URI path = %s.\n", __FUNCTION__, src->live_tv_id, src->uri_name );
584 src->file_transfer = gmyth_file_transfer_new( src->live_tv_id,
585 g_string_new( src->uri_name ), -1, src->mythtv_version );
587 if ( src->file_transfer == NULL ) {
588 GST_OBJECT_UNLOCK(src);
593 /* sets the Playback monitor connection */
594 ret = gmyth_file_transfer_playback_setup( &(src->file_transfer), src->live_tv );
596 if ( src->live_tv == TRUE && ret == TRUE ) {
597 /* loop finished, set the max tries variable to zero again... */
598 wait_to_transfer = 0;
600 while ( wait_to_transfer++ < GMYTHTV_TRANSFER_MAX_WAITS &&
601 ( gmyth_file_transfer_is_recording( src->file_transfer ) == FALSE
602 /*|| ( gmyth_file_transfer_get_file_position( src->file_transfer ) < ( src->content_size + 327680 ) )*/ ) )
606 /* sets the FileTransfer instance connection (video/audio download) */
607 ret = gmyth_file_transfer_setup( &(src->file_transfer), src->live_tv );
609 if ( ret == FALSE ) {
610 GST_OBJECT_UNLOCK(src);
611 #ifndef GST_DISABLE_GST_DEBUG
612 if ( src->mythtv_msgs_dbg )
613 g_printerr( "MythTV FileTransfer request failed when setting up socket connection!\n" );
615 goto begin_req_failed;
618 src->content_size = src->file_transfer->filesize;
620 src->do_start = FALSE;
622 GST_OBJECT_UNLOCK(src);
630 if (src->spawn_livetv != NULL )
631 g_object_unref( src->spawn_livetv );
633 GST_ELEMENT_ERROR (src, LIBRARY, INIT,
634 (NULL), ("Could not initialize MythTV library (%i, %s)", ret, src->uri_name));
639 GST_ELEMENT_ERROR (src, LIBRARY, INIT,
640 (NULL), ("Could not begin request sent to MythTV server (%i, %s)", ret, src->uri_name));
645 GST_ELEMENT_ERROR (src, RESOURCE, READ,
646 (NULL), ("Seek failed, go to the next program info... (%s)",
648 // go to the next program chain
649 src->unique_setup = FALSE;
650 src->update_prog_chain = TRUE;
652 //GST_OBJECT_LOCK(src);
653 gst_mythtv_src_next_program_chain( src );
654 //GST_OBJECT_UNLOCK(src);
660 /* create a new socket for connecting to the next program chain */
662 gst_mythtv_src_next_program_chain ( GstMythtvSrc *src )
664 GString *chain_id_local = NULL;
671 if (src->unique_setup == FALSE) {
672 src->unique_setup = TRUE;
677 if (src->file_transfer) {
678 g_object_unref (src->file_transfer);
679 src->file_transfer = NULL;
683 g_free (src->uri_name);
686 if ( src->live_tv ) {
687 if ( gmyth_livetv_next_program_chain( src->spawn_livetv ) == FALSE ) {
688 g_print( "\n\n[%s]\t\tFailed to go to the next program chain!!!\n\n", __FUNCTION__ );
692 /* set up the uri variable */
693 src->uri_name = g_strdup( src->spawn_livetv->proginfo->pathname->str );
694 chain_id_local = gmyth_tvchain_get_id( src->spawn_livetv->tvchain );
695 if ( chain_id_local != NULL ) {
696 src->live_chain_id = g_strdup( chain_id_local->str );
697 g_print( "\t[%s]\tLocal chain ID = %s.\n", __FUNCTION__, src->live_chain_id );
699 src->live_tv_id = src->spawn_livetv->recorder->recorder_num;
700 g_print ( "[%s] LiveTV id = %d, URI path = %s.\n", __FUNCTION__, src->live_tv_id, src->uri_name );
703 src->file_transfer = gmyth_file_transfer_new( src->live_tv_id,
704 g_string_new( src->uri_name ), -1, src->mythtv_version );
706 if ( src->file_transfer == NULL ) {
710 /* sets the Playback monitor connection */
711 ret = gmyth_file_transfer_playback_setup( &(src->file_transfer), src->live_tv );
713 if ( src->live_tv == TRUE && ret == TRUE ) {
714 /* loop finished, set the max tries variable to zero again... */
715 wait_to_transfer = 0;
719 while ( wait_to_transfer++ < GMYTHTV_TRANSFER_MAX_WAITS &&
720 ( gmyth_file_transfer_is_recording( src->file_transfer ) == FALSE ) )
724 /* sets the FileTransfer instance connection (video/audio download) */
725 ret = gmyth_file_transfer_setup( &(src->file_transfer), src->live_tv );
727 if ( ret == FALSE ) {
728 #ifndef GST_DISABLE_GST_DEBUG
729 if ( src->mythtv_msgs_dbg )
730 g_printerr( "MythTV FileTransfer request failed when setting up socket connection!\n" );
732 goto begin_req_failed;
734 src->content_size_last = src->content_size;
737 if ( src->content_size < src->file_transfer->filesize ) {
738 src->content_size = src->file_transfer->filesize;
740 //gint64 pos = gst_mythtv_src_get_position(src);
741 //if ( pos > src->file_transfer->filesize )
742 // src->content_size = pos;
747 src->content_size = src->file_transfer->filesize;
748 while ( src->content_size < GMYTHTV_TRANSFER_MAX_BUFFER*4 )
749 src->content_size = gst_mythtv_src_get_position( src );
751 src->read_offset = 0;
753 src->update_prog_chain = FALSE;
761 if (src->spawn_livetv != NULL )
762 g_object_unref( src->spawn_livetv );
764 GST_ELEMENT_ERROR (src, LIBRARY, INIT,
765 (NULL), ("Could not initialize MythTV library (%i, %s)", ret, src->uri_name));
770 GST_ELEMENT_ERROR (src, LIBRARY, INIT,
771 (NULL), ("Could not begin request sent to MythTV server (%i, %s)", ret, src->uri_name));
778 /* handles queries for location in the stream in the requested format */
780 gst_mythtv_src_query ( GstPad * pad, GstQuery * query )
783 GstMythtvSrc *mythtv;
787 mythtv = GST_GMYTHTV_SRC( GST_PAD_PARENT (pad) );
789 size = gst_mythtv_src_get_position (mythtv);
791 switch (GST_QUERY_TYPE (query)) {
793 case GST_QUERY_POSITION:
799 /* save requested format */
800 gst_query_parse_position (query, NULL, &cur);
802 /* query peer for current position in time */
803 g_print( "[%s] Actual size is %s than current size from sink. [ %lld, %lld ]\n", __FUNCTION__,
804 ( size > cur ) ? "greater" : "lower", size, cur );
805 gst_query_set_position (query, GST_FORMAT_BYTES, size);
812 case GST_QUERY_DURATION:
817 /* save requested format */
818 gst_query_parse_position (query, NULL, &cur);
820 /* query peer for current position in time */
821 g_print( "[%s] Actual size is %s than current size from sink. [ %lld, %lld ]\n", __FUNCTION__,
822 ( size * GST_SECOND > cur * GST_SECOND ) ? "greater" : "lower", size * GST_SECOND,
824 gst_query_set_position (query, GST_FORMAT_TIME, size * GST_SECOND );
826 if ( size * GST_SECOND < cur * GST_SECOND )
846 gst_mythtv_src_get_size (GstBaseSrc * bsrc, guint64 * size)
848 GstMythtvSrc *src = GST_MYTHTV_SRC (bsrc);
850 g_print( "[%s] Differs from previous content size: %d (max.: %d)\n", __FUNCTION__,
851 abs( src->content_size - src->prev_content_size ), GMYTHTV_TRANSFER_MAX_BUFFER );
853 if (src->content_size <= 0) {
855 } else if ( src->live_tv && ( abs( src->content_size - src->bytes_read ) <
856 GMYTHTV_TRANSFER_MAX_BUFFER ) ) {
857 //g_static_mutex_lock( &update_size_mutex );
858 //GST_OBJECT_LOCK(src);
860 gint64 new_offset = gmyth_file_transfer_get_file_position( src->file_transfer );
861 if ( new_offset > 0 && new_offset > src->content_size ) {
862 src->content_size = new_offset;
863 } else if ( new_offset < src->content_size ) {
864 src->update_prog_chain = TRUE;
867 if ( src->enable_timing_position ) {
869 if (src->live_tv == TRUE) {
872 size_tmp = gmyth_file_transfer_get_file_position( src->file_transfer );
873 if ( size_tmp > ( src->content_size + GMYTHTV_TRANSFER_MAX_BUFFER ) )
874 src->content_size = size_tmp;
875 else if ( size_tmp > 0 )
877 g_print( "\t[%s]\tGET_POSITION: file_position = %lld\n",
878 __FUNCTION__, size_tmp );
882 src->prev_content_size = src->content_size;
884 //GST_OBJECT_UNLOCK(src);
885 //g_static_mutex_unlock( &update_size_mutex );
888 *size = src->content_size;
889 g_print( "[%s] Content size = %lld\n", __FUNCTION__, src->content_size );
895 /* close the socket and associated resources
896 * used both to recover from errors and go to NULL state */
898 gst_mythtv_src_stop (GstBaseSrc * bsrc)
902 src = GST_MYTHTV_SRC (bsrc);
905 g_free (src->uri_name);
906 src->uri_name = NULL;
909 if (src->mythtv_caps) {
910 gst_caps_unref (src->mythtv_caps);
911 src->mythtv_caps = NULL;
921 gst_mythtv_src_handle_event (GstPad * pad, GstEvent * event)
923 GstMythtvSrc *src = GST_MYTHTV_SRC (GST_PAD_PARENT (pad));
924 gint64 cont_size = 0;
926 switch (GST_EVENT_TYPE (event)) {
928 case GST_EVENT_FLUSH_START:
930 g_print( "\n\n\n[%s]\t\tGot FLUSH_START event!!!\n\n\n", __FUNCTION__ );
931 cont_size = gst_mythtv_src_get_position (src);
932 if ( !src->live_tv ) {
933 if ( cont_size > src->content_size ) {
934 src->content_size = cont_size;
938 gst_element_set_state ( GST_ELEMENT (src), GST_STATE_NULL );
939 gst_element_set_locked_state ( GST_ELEMENT (src), FALSE );
942 if ( cont_size <= 0 ) {
943 src->update_prog_chain = TRUE;
945 src->unique_setup = FALSE;
946 src->do_start = TRUE;
950 case GST_EVENT_FLUSH_STOP:
951 src->do_start = TRUE;
953 gst_element_set_state (GST_ELEMENT(src), GST_STATE_NULL);
954 //gst_element_set_locked_state (GST_ELEMENT(src), TRUE);
958 g_print( "[%s] Got EOS event!!!\n", __FUNCTION__ );
960 if ( src->live_tv ) {
961 cont_size = gst_mythtv_src_get_position (src);
962 if ( cont_size > src->content_size ) {
963 src->content_size = cont_size;
967 gst_element_set_state ( GST_ELEMENT (src), GST_STATE_NULL );
968 gst_element_set_locked_state ( GST_ELEMENT (src), FALSE );
974 case GST_EVENT_NEWSEGMENT:
975 g_print( "[%s] Got NEWSEGMENT!!!\n", __FUNCTION__ );
980 g_print( "[%s] Got EVENT_SEEK!!!\n", __FUNCTION__ );
982 //gboolean update = TRUE;
984 GstSeekType cur_type, stop_type;
986 gint64 cur = 0, stop = 0;
987 gst_event_parse_seek ( event, &rate, &format,
988 &flags, &cur_type, &cur,
991 g_print( "[%s] Got EVENT_SEEK.\n", __FUNCTION__ );
992 if ( !( flags & GST_SEEK_FLAG_FLUSH ) ) {
993 g_print( "[%s] Could get the FLAG_FLUSH message.\n", __FUNCTION__ );
995 //gboolean ret = gst_event_parse_new_segment ( event,
996 // &update, &rate, &format, &start, &stop,
998 //GstFlowReturn flow_ret = gst_mythtv_src_create (GST_BASE_SRC( GST_PAD_PARENT( psrc ) ),
999 // cur, stop - cur + 1, GstBuffer)
1003 return gst_pad_event_default (pad, event);
1006 return gst_pad_event_default (pad, event);
1011 gst_mythtv_src_is_seekable( GstBaseSrc *push_src )
1017 static GstFlowReturn
1018 gst_mythtv_src_file_transfer( GstMythtvSrc *src )
1020 GstFlowReturn ret = GST_FLOW_OK;
1021 GstBuffer *buf = NULL;
1024 /* The caller should know the number of bytes and not read beyond EOS. */
1025 if ( G_UNLIKELY (src->eos) )
1027 if ( G_UNLIKELY (src->update_prog_chain) )
1028 goto change_progchain;
1032 /* Create the buffer. */
1033 buf = gst_buffer_new_and_alloc (4096);
1035 ret = gst_mythtv_src_read_bytes( src, GST_BUFFER_SIZE(buf), TRUE, &buf );
1036 if ( ret != GST_FLOW_OK )
1039 read = do_read_request_response ( src, src->read_offset, GST_BUFFER_SIZE(buf), &buf );
1041 if (G_UNLIKELY (src->update_prog_chain) )
1042 goto change_progchain;
1044 if (G_UNLIKELY (read <= 0) || buf == NULL) {
1046 goto change_progchain;
1051 if ( GST_FLOW_OK != ( ret = gst_pad_push ( GST_BASE_SRC_PAD (GST_BASE_SRC (psrc)), buf ) ) ) {
1059 const gchar *reason = gst_flow_get_name (ret);
1061 GST_DEBUG_OBJECT (src, "DONE task, reason %s", reason);
1066 const gchar *reason = gst_flow_get_name (ret);
1068 GST_DEBUG_OBJECT (src, "pausing task, reason %s", reason);
1069 return GST_FLOW_UNEXPECTED;
1074 GST_ELEMENT_ERROR (src, RESOURCE, READ,
1075 (NULL), ("Could not read any bytes (%i, %s)", read,
1077 return GST_FLOW_ERROR;
1081 GST_ELEMENT_ERROR (src, RESOURCE, READ,
1082 (NULL), ("Seek failed, go to the next program info... (%i, %s)", read,
1084 // go to the next program chain
1085 src->unique_setup = FALSE;
1086 src->update_prog_chain = TRUE;
1087 src->mode = GST_MYTHTV_SRC_NEXT_PROGRAM_CHAIN;
1089 //GST_OBJECT_LOCK(src);
1090 //gst_mythtv_src_next_program_chain( src );
1091 //GST_OBJECT_UNLOCK(src);
1092 return GST_FLOW_OK;//GST_FLOW_ERROR_NO_DATA;
1096 static GstFlowReturn
1097 gst_mythtv_src_play (GstPad * pad)
1099 GstFlowReturn res = GST_FLOW_OK;
1100 GstMythtvSrc *src = GST_MYTHTV_SRC (GST_PAD_PARENT (pad));
1102 switch ( src->state ) {
1103 case GST_MYTHTV_SRC_FILE_TRANSFER:
1104 res = gst_mythtv_src_file_transfer( src );
1105 if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
1109 case GST_MYTHTV_SRC_NEXT_PROGRAM_CHAIN:
1110 src->read_offset = 0;
1111 src->bytes_read = 0;
1112 src->unique_setup = FALSE;
1113 if ( !gst_mythtv_src_next_program_chain( src ) ) {
1116 src->state = GST_MYTHTV_SRC_FILE_TRANSFER;
1118 case GST_MYTHTV_SRC_INVALID_DATA:
1122 g_assert_not_reached ();
1125 GST_DEBUG_OBJECT (src, "state: %d res:%s", src->state,
1126 gst_flow_get_name (res));
1131 GST_LOG_OBJECT (src, "pausing task, reason %s", gst_flow_get_name (res));
1132 gst_pad_pause_task (src->srcpad);
1133 if (GST_FLOW_IS_FATAL (res)) {
1134 GST_ELEMENT_ERROR (src, STREAM, FAILED,
1135 ("Internal data stream error."),
1136 ("streaming stopped, reason %s", gst_flow_get_name (res)));
1137 gst_pad_send_event( GST_BASE_SRC_PAD (GST_BASE_SRC (psrc)), gst_event_new_eos() );
1143 static GstStateChangeReturn
1144 gst_mythtv_src_change_state (GstElement * element, GstStateChange transition)
1146 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;//GST_STATE_CHANGE_NO_PREROLL;
1147 GstMythtvSrc *src = GST_MYTHTV_SRC (element);
1149 switch (transition) {
1150 case GST_STATE_CHANGE_NULL_TO_READY:
1151 src->do_start = TRUE;
1152 src->unique_setup = FALSE;
1154 case GST_STATE_CHANGE_READY_TO_PAUSED:
1155 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1162 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1163 if (ret == GST_STATE_CHANGE_FAILURE)
1166 switch (transition) {
1167 case GST_STATE_CHANGE_READY_TO_NULL:
1168 g_print( "[%s] READY to NULL called!\n", __FUNCTION__ );
1170 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1171 g_print( "[%s] PLAYING to PAUSED called!\n", __FUNCTION__ );
1172 case GST_STATE_CHANGE_PAUSED_TO_READY:
1173 g_print( "[%s] PAUSED to READY called!\n", __FUNCTION__ );
1174 if ( src->live_tv && src->update_prog_chain ) {
1176 gst_pad_push_event ( pad_peer = gst_pad_get_peer( GST_BASE_SRC_PAD (GST_BASE_SRC (src)) ),
1177 gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_BYTES, 0, -1, 0 ) );
1178 src->read_offset = 0;
1179 src->bytes_read = 0;
1180 src->unique_setup = FALSE;
1181 GST_OBJECT_LOCK( src );
1182 gst_mythtv_src_next_program_chain( src );
1183 GST_OBJECT_UNLOCK( src );
1194 gst_mythtv_src_set_property (GObject * object, guint prop_id,
1195 const GValue * value, GParamSpec * pspec)
1197 GstMythtvSrc *mythtvsrc = GST_MYTHTV_SRC (object);
1199 GST_OBJECT_LOCK (mythtvsrc);
1204 if (!g_value_get_string (value)) {
1205 GST_WARNING ("location property cannot be NULL");
1209 if (mythtvsrc->uri_name != NULL) {
1210 g_free (mythtvsrc->uri_name);
1211 mythtvsrc->uri_name = NULL;
1213 mythtvsrc->uri_name = g_value_dup_string (value);
1217 #ifndef GST_DISABLE_GST_DEBUG
1218 case PROP_GMYTHTV_DBG:
1220 mythtvsrc->mythtv_msgs_dbg = g_value_get_boolean (value);
1224 case PROP_GMYTHTV_VERSION:
1226 mythtvsrc->mythtv_version = g_value_get_int (value);
1229 case PROP_GMYTHTV_LIVEID:
1231 mythtvsrc->live_tv_id = g_value_get_int (value);
1234 case PROP_GMYTHTV_LIVE:
1236 mythtvsrc->live_tv = g_value_get_boolean (value);
1239 case PROP_GMYTHTV_ENABLE_TIMING_POSITION:
1241 mythtvsrc->enable_timing_position = g_value_get_boolean (value);
1244 case PROP_GMYTHTV_LIVE_CHAINID:
1246 if (!g_value_get_string (value)) {
1247 GST_WARNING ("MythTV Live chainid property cannot be NULL");
1251 if (mythtvsrc->live_chain_id != NULL) {
1252 g_free (mythtvsrc->live_chain_id);
1253 mythtvsrc->live_chain_id = NULL;
1255 mythtvsrc->live_chain_id = g_value_dup_string (value);
1258 case PROP_GMYTHTV_CHANNEL_NUM:
1260 mythtvsrc->channel_num = g_value_get_int (value);
1264 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1267 GST_OBJECT_UNLOCK (mythtvsrc);
1273 gst_mythtv_src_get_property (GObject * object, guint prop_id,
1274 GValue * value, GParamSpec * pspec)
1276 GstMythtvSrc *mythtvsrc = GST_MYTHTV_SRC (object);
1278 GST_OBJECT_LOCK (mythtvsrc);
1283 gchar *str = g_strdup( "" );
1285 if ( mythtvsrc->uri_name == NULL ) {
1286 g_free (mythtvsrc->uri_name);
1287 mythtvsrc->uri_name = NULL;
1289 str = g_strdup( mythtvsrc->uri_name );
1291 g_value_set_string ( value, str );
1294 #ifndef GST_DISABLE_GST_DEBUG
1295 case PROP_GMYTHTV_DBG:
1296 g_value_set_boolean ( value, mythtvsrc->mythtv_msgs_dbg );
1299 case PROP_GMYTHTV_VERSION:
1301 g_value_set_int ( value, mythtvsrc->mythtv_version );
1304 case PROP_GMYTHTV_LIVEID:
1306 g_value_set_int ( value, mythtvsrc->live_tv_id );
1309 case PROP_GMYTHTV_LIVE:
1310 g_value_set_boolean ( value, mythtvsrc->live_tv );
1312 case PROP_GMYTHTV_ENABLE_TIMING_POSITION:
1313 g_value_set_boolean ( value, mythtvsrc->enable_timing_position );
1315 case PROP_GMYTHTV_LIVE_CHAINID:
1317 gchar *str = g_strdup( "" );
1319 if ( mythtvsrc->live_chain_id == NULL ) {
1320 g_free (mythtvsrc->live_chain_id);
1321 mythtvsrc->live_chain_id = NULL;
1323 str = g_strdup( mythtvsrc->live_chain_id );
1325 g_value_set_string ( value, str );
1328 case PROP_GMYTHTV_CHANNEL_NUM:
1330 g_value_set_int ( value, mythtvsrc->channel_num );
1334 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1337 GST_OBJECT_UNLOCK (mythtvsrc);
1340 /* entry point to initialize the plug-in
1341 * initialize the plug-in itself
1342 * register the element factories and pad templates
1343 * register the features
1346 plugin_init (GstPlugin * plugin)
1348 return gst_element_register (plugin, "mythtvsrc", GST_RANK_NONE,
1349 GST_TYPE_MYTHTV_SRC);
1352 /* this is the structure that gst-register looks for
1353 * so keep the name plugin_desc, or you cannot get your plug-in registered */
1354 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1358 plugin_init, VERSION, "LGPL", "GStreamer", "http://gstreamer.net/")
1361 /*** GSTURIHANDLER INTERFACE *************************************************/
1363 gst_mythtv_src_uri_get_type (void)
1369 gst_mythtv_src_uri_get_protocols (void)
1371 static gchar *protocols[] = { "myth", "myths", NULL };
1376 static const gchar *
1377 gst_mythtv_src_uri_get_uri (GstURIHandler * handler)
1379 GstMythtvSrc *src = GST_MYTHTV_SRC (handler);
1381 return src->uri_name;
1385 gst_mythtv_src_uri_set_uri (GstURIHandler * handler, const gchar * uri)
1387 GstMythtvSrc *src = GST_MYTHTV_SRC (handler);
1391 protocol = gst_uri_get_protocol (uri);
1392 if ((strcmp (protocol, "myth") != 0) && (strcmp (protocol, "myths") != 0)) {
1397 g_object_set (src, "location", uri, NULL);
1403 gst_mythtv_src_uri_handler_init (gpointer g_iface, gpointer iface_data)
1405 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
1407 iface->get_type = gst_mythtv_src_uri_get_type;
1408 iface->get_protocols = gst_mythtv_src_uri_get_protocols;
1409 iface->get_uri = gst_mythtv_src_uri_get_uri;
1410 iface->set_uri = gst_mythtv_src_uri_set_uri;
1414 size_header_handler (void *userdata, const char *value)
1416 GstMythtvSrc *src = GST_MYTHTV_SRC (userdata);
1418 //src->content_size = g_ascii_strtoull (value, NULL, 10);
1420 GST_DEBUG_OBJECT (src, "content size = %lld bytes", src->content_size);