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
16 * When using the LiveTV content, put the location URI in the following
19 * myth://mythtv:mythtv@xxx.xxx.xxx.xxx:6543/#mythconverg
21 * Where the first field is the protocol (myth), the second and third are user
22 * name (mythtv) and password (mythtv), then backend host name and port number,
23 * and the last field is the database name (mythconverg).
30 #include "gstmythtvsrc.h"
31 #include <gmyth/gmyth_file_transfer.h>
32 #include <gmyth/gmyth_livetv.h>
34 #include <gmyth/gmyth_socket.h>
35 #include <gmyth/gmyth_tvchain.h>
40 GST_DEBUG_CATEGORY_STATIC (mythtvsrc_debug);
41 #define GST_CAT_DEFAULT mythtvsrc_debug
43 #define GST_GMYTHTV_ID_NUM 1
45 #define GST_GMYTHTV_CHANNEL_NUM 1000
47 #define GMYTHTV_VERSION_DEFAULT 30
49 #define GMYTHTV_TRANSFER_MAX_WAITS 100
51 #define GMYTHTV_TRANSFER_MAX_RESENDS 5
53 #define GMYTHTV_TRANSFER_MAX_BUFFER 128*1024
57 #define MAX_READ_SIZE 4*1024
60 #define GST_FLOW_ERROR_NO_DATA -101
62 #define REQUEST_MAX_SIZE 64*1024
64 #define INTERNAL_BUFFER_SIZE 90*1024
66 /* stablish a maximum iteration value to the IS_RECORDING message */
67 static guint wait_to_transfer = 0;
69 static const GstElementDetails gst_mythtv_src_details =
70 GST_ELEMENT_DETAILS ( "MythTV client source",
72 "Control and receive data as a client over the network via raw socket connections using the MythTV protocol",
73 "Rosfran Borges <rosfran.borges@indt.org.br>" );
75 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ( "src",
78 GST_STATIC_CAPS ("video/x-nuv") );
85 #ifndef GST_DISABLE_GST_DEBUG
91 PROP_GMYTHTV_LIVE_CHAINID,
92 PROP_GMYTHTV_ENABLE_TIMING_POSITION,
93 PROP_GMYTHTV_CHANNEL_NUM
96 static void gst_mythtv_src_finalize (GObject * gobject);
98 static GstFlowReturn gst_mythtv_src_create ( GstPushSrc* psrc, GstBuffer** outbuf );
100 static gboolean gst_mythtv_src_start (GstBaseSrc * bsrc);
101 static gboolean gst_mythtv_src_stop (GstBaseSrc * bsrc);
102 static gboolean gst_mythtv_src_get_size (GstBaseSrc * bsrc, guint64 * size);
103 static gboolean gst_mythtv_src_is_seekable( GstBaseSrc *push_src );
106 static void gst_mythtv_src_get_times (GstBaseSrc * src, GstBuffer * buffer,
107 GstClockTime * start, GstClockTime * end);
110 static gboolean gst_mythtv_src_do_seek( GstBaseSrc *base, GstSegment *segment );
112 static gboolean gst_mythtv_src_next_program_chain ( GstMythtvSrc *src );
114 static GstStateChangeReturn
115 gst_mythtv_src_change_state (GstElement * element, GstStateChange transition);
117 static void gst_mythtv_src_set_property (GObject * object, guint prop_id,
118 const GValue * value, GParamSpec * pspec);
119 static void gst_mythtv_src_get_property (GObject * object, guint prop_id,
120 GValue * value, GParamSpec * pspec);
122 static void gst_mythtv_src_uri_handler_init (gpointer g_iface, gpointer iface_data);
124 static gboolean gst_mythtv_src_handle_query (GstPad * pad, GstQuery * query);
126 static gboolean gst_mythtv_src_handle_event (GstPad * pad, GstEvent * event);
128 static gint do_read_request_response (GstMythtvSrc * src, guint size, GByteArray *data_ptr);
130 GStaticRecMutex th_mutex = G_STATIC_REC_MUTEX_INIT;
133 _urihandler_init (GType type)
135 static const GInterfaceInfo urihandler_info = {
136 gst_mythtv_src_uri_handler_init,
141 g_type_add_interface_static (type, GST_TYPE_URI_HANDLER, &urihandler_info);
143 GST_DEBUG_CATEGORY_INIT (mythtvsrc_debug, "mythtvsrc", 0,
147 GST_BOILERPLATE_FULL (GstMythtvSrc, gst_mythtv_src, GstPushSrc,
148 GST_TYPE_PUSH_SRC, _urihandler_init)
151 gst_mythtv_src_base_init (gpointer g_class)
153 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
155 gst_element_class_add_pad_template (element_class,
156 gst_static_pad_template_get (&srctemplate));
158 gst_element_class_set_details (element_class, &gst_mythtv_src_details);
160 element_class->change_state = gst_mythtv_src_change_state;
165 gst_mythtv_src_class_init (GstMythtvSrcClass * klass)
167 GObjectClass *gobject_class;
168 GstPushSrcClass *gstpushsrc_class;
169 GstBaseSrcClass *gstbasesrc_class;
171 gobject_class = (GObjectClass *) klass;
172 gstbasesrc_class = (GstBaseSrcClass *) klass;
173 gstpushsrc_class = (GstPushSrcClass *) klass;
175 gobject_class->set_property = gst_mythtv_src_set_property;
176 gobject_class->get_property = gst_mythtv_src_get_property;
177 gobject_class->finalize = gst_mythtv_src_finalize;
179 g_object_class_install_property
180 (gobject_class, PROP_LOCATION,
181 g_param_spec_string ("location", "Location",
182 "The location. In the form:"
183 "\n\t\t\tmyth://a.com/file.nuv"
184 "\n\t\t\tmyth://a.com:23223/file.nuv "
185 "\n\t\t\ta.com/file.nuv - default scheme 'myth'",
186 "", G_PARAM_READWRITE));
188 g_object_class_install_property
189 (gobject_class, PROP_URI,
190 g_param_spec_string ("uri", "Uri",
191 "The location in form of a URI (deprecated; use location)",
192 "", G_PARAM_READWRITE));
194 g_object_class_install_property
195 (gobject_class, PROP_GMYTHTV_VERSION,
196 g_param_spec_int ("mythtv-version", "mythtv-version",
197 "Change MythTV version",
198 26, 30, 26, G_PARAM_READWRITE));
200 g_object_class_install_property
201 (gobject_class, PROP_GMYTHTV_LIVEID,
202 g_param_spec_int ("mythtv-live-id", "mythtv-live-id",
203 "Change MythTV version",
204 0, 200, GST_GMYTHTV_ID_NUM, G_PARAM_READWRITE));
206 g_object_class_install_property
207 (gobject_class, PROP_GMYTHTV_LIVE_CHAINID,
208 g_param_spec_string ("mythtv-live-chainid", "mythtv-live-chainid",
209 "Sets the MythTV chain ID (from TV Chain)",
210 "", G_PARAM_READWRITE));
212 g_object_class_install_property
213 (gobject_class, PROP_GMYTHTV_LIVE,
214 g_param_spec_boolean ("mythtv-live", "mythtv-live",
215 "Enable MythTV Live TV content streaming",
216 FALSE, G_PARAM_READWRITE));
218 g_object_class_install_property
219 (gobject_class, PROP_GMYTHTV_ENABLE_TIMING_POSITION,
220 g_param_spec_boolean ("mythtv-enable-timing-position", "mythtv-enable-timing-position",
221 "Enable MythTV Live TV content size continuous updating",
222 FALSE, G_PARAM_READWRITE));
224 g_object_class_install_property
225 (gobject_class, PROP_GMYTHTV_CHANNEL_NUM,
226 g_param_spec_int ("mythtv-channel", "mythtv-channel",
227 "Change MythTV channel number",
228 0, 99999, GST_GMYTHTV_CHANNEL_NUM, G_PARAM_READWRITE));
230 #ifndef GST_DISABLE_GST_DEBUG
231 g_object_class_install_property
232 (gobject_class, PROP_GMYTHTV_DBG,
233 g_param_spec_boolean ("mythtv-debug", "mythtv-debug",
234 "Enable MythTV debug messages",
235 FALSE, G_PARAM_READWRITE));
238 gstbasesrc_class->start = gst_mythtv_src_start;
239 gstbasesrc_class->stop = gst_mythtv_src_stop;
240 gstbasesrc_class->get_size = gst_mythtv_src_get_size;
241 gstbasesrc_class->is_seekable = gst_mythtv_src_is_seekable;
243 //gstbasesrc_class->get_times = gst_mythtv_src_get_times;
245 gstbasesrc_class->do_seek = gst_mythtv_src_do_seek;
246 gstpushsrc_class->create = gst_mythtv_src_create;
248 GST_DEBUG_CATEGORY_INIT (mythtvsrc_debug, "mythtvsrc", 0,
249 "MythTV Client Source");
253 gst_mythtv_src_init (GstMythtvSrc * this, GstMythtvSrcClass * g_class)
255 this->file_transfer = NULL;
257 this->unique_setup = FALSE;
259 this->mythtv_version = GMYTHTV_VERSION_DEFAULT;
261 this->state = GST_MYTHTV_SRC_FILE_TRANSFER;
263 this->bytes_read = 0;
265 this->prev_content_size = 0;
267 this->content_size = 0;
268 this->read_offset = 0;
270 this->content_size_last = 0;
272 this->live_tv = FALSE;
274 this->enable_timing_position = FALSE;
275 this->update_prog_chain = FALSE;
277 this->user_agent = g_strdup ("mythtvsrc");
278 this->mythtv_caps = NULL;
279 this->update_prog_chain = FALSE;
281 this->channel_num = 0;
285 this->bytes_queue = NULL;
287 this->th_mutex = NULL;
289 gst_base_src_set_format( GST_BASE_SRC( this ), GST_FORMAT_BYTES );
291 /* gst_base_src_set_live ( GST_BASE_SRC( this ), TRUE ); */
293 gst_pad_set_event_function ( GST_BASE_SRC_PAD(GST_BASE_SRC(this)),
294 gst_mythtv_src_handle_event );
295 gst_pad_set_query_function ( GST_BASE_SRC_PAD(GST_BASE_SRC(this)),
296 gst_mythtv_src_handle_query );
301 gst_mythtv_src_finalize (GObject * gobject)
303 GstMythtvSrc *this = GST_MYTHTV_SRC (gobject);
305 if ( this->th_read_ahead != NULL ) {
306 gst_task_stop( this->th_read_ahead );
307 this->th_read_ahead = NULL;
310 if (this->mythtv_caps) {
311 gst_caps_unref (this->mythtv_caps);
312 this->mythtv_caps = NULL;
315 if (this->file_transfer) {
316 g_object_unref (this->file_transfer);
317 this->file_transfer = NULL;
320 if (this->spawn_livetv) {
321 g_object_unref (this->spawn_livetv);
322 this->spawn_livetv = NULL;
325 if (this->backend_info) {
326 g_object_unref (this->backend_info);
327 this->backend_info = NULL;
330 if (this->uri_name) {
331 g_free (this->uri_name);
334 if (this->user_agent) {
335 g_free (this->user_agent);
338 if ( this->bytes_queue ) {
339 g_byte_array_free( this->bytes_queue, TRUE );
340 this->bytes_queue = NULL;
343 G_OBJECT_CLASS (parent_class)->finalize (gobject);
347 do_read_request_response (GstMythtvSrc * src, guint size, GByteArray *data_ptr)
350 guint sizetoread = size;
351 gint max_iters = GMYTHTV_TRANSFER_MAX_RESENDS;
353 GST_LOG_OBJECT( src, "Starting: [%s] Reading %d bytes...\n", __FUNCTION__, sizetoread );
355 /* Loop sending the Myth File Transfer request:
356 * Retry whilst authentication fails and we supply it. */
359 while ( sizetoread == size && --max_iters > 0) {
361 len = gmyth_file_transfer_read( src->file_transfer,
362 data_ptr, sizetoread, TRUE );
372 if ( src->live_tv == FALSE )
378 if ( len == GMYTHTV_FILE_TRANSFER_READ_ERROR ) { /* -314 */
379 src->update_prog_chain = TRUE;
385 } else if ( len == 0 )
388 if ( read == sizetoread )
392 if ( ( read < 0 && !src->live_tv ) || max_iters == 0 )
405 gst_mythtv_src_create ( GstPushSrc* psrc, GstBuffer** outbuf )
408 GstFlowReturn ret = GST_FLOW_OK;
411 src = GST_MYTHTV_SRC ( psrc );
413 /* The caller should know the number of bytes and not read beyond EOS. */
414 if (G_UNLIKELY (src->eos))
416 if ( G_UNLIKELY (src->update_prog_chain) )
417 goto change_progchain;
419 //g_static_rec_mutex_lock( &th_mutex );
420 GST_DEBUG_OBJECT( src, "[%s] offset = %llu, size = %d...\n", __FUNCTION__,
421 src->read_offset, MAX_READ_SIZE );
423 GST_DEBUG_OBJECT ( src, "[%s]\t\tCreate: buffer_remain: %d, buffer_size = %d.\n", __FUNCTION__,
424 (gint) src->buffer_remain, src->bytes_queue->len );
426 /* just get from the byte array, no network effort... */
427 if ( ( src->buffer_remain = src->bytes_queue->len ) < MAX_READ_SIZE &&
428 abs( src->content_size - src->bytes_read ) > MAX_READ_SIZE ) {
429 //( ( INTERNAL_BUFFER_SIZE - src->buffer_remain ) >= REQUEST_MAX_SIZE ) ) {
430 GByteArray *buffer = NULL;
431 guint buffer_size_inter = ( INTERNAL_BUFFER_SIZE - src->buffer_remain );
432 if ( buffer_size_inter > REQUEST_MAX_SIZE )
433 buffer_size_inter = REQUEST_MAX_SIZE;
435 buffer = g_byte_array_new();
437 read = do_read_request_response( src, buffer_size_inter, buffer );
439 if (G_UNLIKELY (read < 0)) {
441 goto change_progchain;
444 } else if (G_UNLIKELY (read == 0) && !src->live_tv )
447 if ( G_UNLIKELY (src->update_prog_chain) )
448 goto change_progchain;
450 src->bytes_queue = g_byte_array_append( src->bytes_queue, buffer->data, read );
451 if ( read > buffer_size_inter )
452 GST_WARNING_OBJECT( src, "\n\n\n[%s]\t\tINCREASED buffer size! Backend sent more than we ask him... (%d)\n\n\n",
453 __FUNCTION__, abs( read - buffer_size_inter ) );
455 src->buffer_remain += read;
457 if ( buffer != NULL ) {
458 g_byte_array_free( buffer, TRUE );
462 GST_DEBUG_OBJECT( src, "[%s]\tBYTES READ (actual) = %d, BYTES READ (cumulative) = %llu, "\
463 "OFFSET = %llu, CONTENT SIZE = %llu.\n", __FUNCTION__, read, src->bytes_read,
464 src->read_offset, src->content_size );
468 guint buffer_size = ( src->buffer_remain < MAX_READ_SIZE) ? src->buffer_remain : MAX_READ_SIZE;
470 /* Create the buffer. */
471 ret = gst_pad_alloc_buffer ( GST_BASE_SRC_PAD (GST_BASE_SRC (psrc)),
472 GST_BUFFER_OFFSET_NONE, buffer_size,
473 GST_PAD_CAPS (GST_BASE_SRC_PAD (GST_BASE_SRC (psrc))), outbuf );
475 if (G_UNLIKELY (ret != GST_FLOW_OK)) {
477 goto change_progchain;
481 //*outbuf = gst_buffer_new_and_alloc( buffer_size );
483 /* gets the first buffer_size bytes from the byte array buffer variable */
484 //guint8 *buf = g_memdup( src->bytes_queue->data, buffer_size );
486 GST_DEBUG_OBJECT( src, "[%s] read from network? %s!, buffer_remain = %d\n", __FUNCTION__,
487 read == -1 ? "NO, got from buffer" : "YES, go see the backend's log file", src->buffer_remain );
489 GST_BUFFER_SIZE (*outbuf) = buffer_size;
490 //GST_BUFFER_MALLOCDATA( *outbuf ) = g_malloc0( GST_BUFFER_SIZE (*outbuf) );
491 //GST_BUFFER_DATA( *outbuf ) = GST_BUFFER_MALLOCDATA( *outbuf );
492 g_memmove( GST_BUFFER_DATA( (*outbuf) ), src->bytes_queue->data, GST_BUFFER_SIZE(*outbuf) );
493 GST_BUFFER_OFFSET (*outbuf) = src->read_offset;
494 GST_BUFFER_OFFSET_END (*outbuf) = src->read_offset + GST_BUFFER_SIZE (*outbuf);
496 src->buffer_remain -= GST_BUFFER_SIZE (*outbuf);
498 src->read_offset += GST_BUFFER_SIZE (*outbuf);
499 src->bytes_read += GST_BUFFER_SIZE (*outbuf);
500 GST_DEBUG_OBJECT( src, "[%s]\t\tBuffer output with size: %d\n", __FUNCTION__, GST_BUFFER_SIZE (*outbuf) );
502 /* flushs the newly buffer got from byte array */
503 src->bytes_queue = g_byte_array_remove_range( src->bytes_queue, 0, buffer_size );
505 GST_DEBUG_OBJECT( src, "Got buffer: [%s]\t\tBUFFER --->SIZE = %d, OFFSET = %llu, "\
506 "OFFSET_END = %llu.\n\n", __FUNCTION__, GST_BUFFER_SIZE (*outbuf),
507 GST_BUFFER_OFFSET (*outbuf), GST_BUFFER_OFFSET_END (*outbuf) );
509 GST_DEBUG_OBJECT( src, "[%s]\tCONTENT_SIZE = %llu, BYTES_READ = %llu.\n", __FUNCTION__,
510 src->content_size, src->bytes_read );
512 if ( G_UNLIKELY (src->eos) || ( !src->live_tv && ( src->bytes_read >= src->content_size ) ) )
517 const gchar *reason = gst_flow_get_name (ret);
519 GST_DEBUG_OBJECT (src, "DONE task, reason %s", reason);
524 const gchar *reason = gst_flow_get_name (ret);
526 GST_DEBUG_OBJECT (src, "pausing task, reason %s", reason);
527 return GST_FLOW_UNEXPECTED;
532 GST_ELEMENT_ERROR (src, RESOURCE, READ,
533 (NULL), ("Could not read any bytes (%i, %s)", read,
535 return GST_FLOW_ERROR;
539 GST_ELEMENT_ERROR (src, RESOURCE, READ,
540 (NULL), ("Seek failed, go to the next program info... (%i, %s)", read,
543 gst_pad_push_event ( GST_BASE_SRC_PAD (GST_BASE_SRC (psrc)),
544 gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0, -1, 0 ) );
545 // go to the next program chain
546 src->unique_setup = FALSE;
547 src->update_prog_chain = TRUE;
549 gst_mythtv_src_next_program_chain( src );
551 return GST_FLOW_ERROR_NO_DATA;
557 gst_mythtv_src_get_position ( GstMythtvSrc* src )
562 if (src->live_tv == TRUE && ( abs( src->content_size - src->bytes_read ) <
563 GMYTHTV_TRANSFER_MAX_BUFFER ) ) {
567 size_tmp = gmyth_recorder_get_file_position( src->spawn_livetv->recorder );
568 if ( size_tmp > ( src->content_size + GMYTHTV_TRANSFER_MAX_BUFFER ) )
569 src->content_size = size_tmp;
570 else if ( size_tmp > 0 && --max_tries > 0 )
572 GST_LOG_OBJECT( src, "\t[%s]\tGET_POSITION: file_position = %lld\n",
573 __FUNCTION__, size_tmp );
574 /* sets the last content size amount before it can be updated */
575 src->prev_content_size = src->content_size;
578 return src->content_size;
583 gst_mythtv_src_do_seek( GstBaseSrc *base, GstSegment *segment )
585 GstMythtvSrc *src = GST_MYTHTV_SRC( base );
586 gint64 new_offset = -1;
587 gint64 actual_seek = segment->start;
590 GST_LOG_OBJECT( src, "[%s]DO Seek called! (start = %lld, stop = %lld)\n", __FUNCTION__,
591 segment->start, segment->stop );
593 if ( segment->format == GST_FORMAT_TIME )
596 //actual_seek = ( ( segment->start / 1000 ) * 28 ) * 4000;
598 GST_LOG_OBJECT( src, "[%s]Trying to seek at the value (actual_seek = %lld, read_offset = %lld)\n", __FUNCTION__, actual_seek, src->read_offset );
599 /* verify if it needs to seek */
600 if ( src->read_offset != actual_seek )
603 new_offset = gmyth_file_transfer_seek( src->file_transfer, segment->start, SEEK_SET );
605 GST_LOG_OBJECT( src, "[%s] Segment offset start = %lld, SRC Offset = %lld, NEW actual backend SEEK Offset = %lld.\n",
606 __FUNCTION__, segment->start, src->read_offset, new_offset );
607 if ( G_UNLIKELY (new_offset < 0 ) )
611 goto change_progchain;
616 src->read_offset = new_offset;
618 if ( ret == FALSE ) {
619 GST_INFO_OBJECT( src, "[%s] Failed to set the SEEK on segment!\n", __FUNCTION__ );
630 GST_DEBUG_OBJECT (src, "EOS found on seeking!!!");
635 GST_ELEMENT_ERROR (src, RESOURCE, READ,
636 (NULL), ("Seek failed, go to the next program info... (%i, %s)", read,
639 gst_pad_push_event ( GST_BASE_SRC_PAD (base),
640 gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0, -1, 0 ) );
641 /* go to the next program chain */
642 src->unique_setup = FALSE;
643 src->update_prog_chain = TRUE;
645 gst_mythtv_src_next_program_chain( src );
652 /* create a socket for connecting to remote server */
654 gst_mythtv_src_start ( GstBaseSrc * bsrc )
656 GstMythtvSrc *src = GST_MYTHTV_SRC (bsrc);
657 GMythURI *gmyth_uri = NULL;
658 gchar* filename = NULL;
660 GString *chain_id_local = NULL;
663 if ( G_UNLIKELY (src->update_prog_chain) )
664 goto change_progchain;
666 if (src->unique_setup == FALSE) {
667 src->unique_setup = TRUE;
672 printf ("XXX %s %d\n", __FUNCTION__, __LINE__);
673 src->backend_info = gmyth_backend_info_new_with_uri( src->uri_name );
675 printf ("XXX %s %d\n", __FUNCTION__, __LINE__);
676 if ( src->live_tv ) {
677 src->spawn_livetv = gmyth_livetv_new( );
678 if ( gmyth_livetv_setup( src->spawn_livetv, src->backend_info ) == FALSE ) {
683 printf ("XXX %s %d\n", __FUNCTION__, __LINE__);
684 /* set up the uri variable */
685 src->uri_name = g_strdup( src->spawn_livetv->proginfo->pathname->str );
686 chain_id_local = gmyth_tvchain_get_id( src->spawn_livetv->tvchain );
687 if ( chain_id_local != NULL ) {
688 src->live_chain_id = g_strdup( chain_id_local->str );
689 GST_INFO_OBJECT( src, "\t[%s]\tLocal chain ID = %s.\n", __FUNCTION__, src->live_chain_id );
692 printf ("XXX %s %d\n", __FUNCTION__, __LINE__);
693 src->live_tv_id = src->spawn_livetv->recorder->recorder_num;
694 GST_LOG_OBJECT( src, "[%s] LiveTV id = %d, URI path = %s.\n", __FUNCTION__, src->live_tv_id, src->uri_name );
697 printf ("XXX %s %d\n", __FUNCTION__, __LINE__);
698 src->file_transfer = gmyth_file_transfer_new( src->backend_info );
700 printf ("XXX %s %d\n", __FUNCTION__, __LINE__);
701 if ( src->file_transfer == NULL ) {
704 GST_INFO_OBJECT( src, "[%s] uri = %s.\n", __FUNCTION__, src->uri_name );
705 printf ("XXX %s %d\n", __FUNCTION__, __LINE__);
706 gmyth_uri = gmyth_uri_new_with_value (src->uri_name);
707 printf ("XXX %s %d\n", __FUNCTION__, __LINE__);
709 filename = gmyth_uri_get_path (gmyth_uri);
710 printf ("XXX %s %d\n", __FUNCTION__, __LINE__);
711 ret = gmyth_file_transfer_open( src->file_transfer, filename);
712 printf ("XXX %s %d\n", __FUNCTION__, __LINE__);
713 g_object_unref (gmyth_uri);
714 printf ("XXX %s %d\n", __FUNCTION__, __LINE__);
716 if ( src->live_tv == TRUE && ret == TRUE ) {
717 /* loop finished, set the max tries variable to zero again... */
718 wait_to_transfer = 0;
720 while ( wait_to_transfer++ < GMYTHTV_TRANSFER_MAX_WAITS &&
721 ( gmyth_livetv_is_recording( src->spawn_livetv ) == FALSE ) )
724 /* IS_RECORDING again, just like the MythTV backend does... */
725 gmyth_livetv_is_recording( src->spawn_livetv );
730 /* sets the FileTransfer instance connection (video/audio download) */
731 //ret = gmyth_file_transfer_setup( &(src->file_transfer), src->live_tv );
733 if ( ret == FALSE ) {
734 #ifndef GST_DISABLE_GST_DEBUG
735 if ( src->mythtv_msgs_dbg )
736 GST_INFO_OBJECT( src, "MythTV FileTransfer request failed when setting up socket connection!\n" );
738 goto begin_req_failed;
741 src->content_size = src->file_transfer->filesize;
743 src->do_start = FALSE;
745 /* this is used for the buffer cache */
746 src->bytes_queue = g_byte_array_sized_new( INTERNAL_BUFFER_SIZE );
747 src->buffer_remain = 0;
749 gst_pad_push_event ( GST_BASE_SRC_PAD (GST_BASE_SRC (src)),
750 gst_event_new_new_segment ( TRUE, 1.0, GST_FORMAT_TIME, 0, src->content_size, 0 ) );
758 if (src->spawn_livetv != NULL )
759 g_object_unref( src->spawn_livetv );
761 GST_ELEMENT_ERROR (src, LIBRARY, INIT,
762 (NULL), ("Could not initialize MythTV library (%i, %s)", ret, src->uri_name));
767 GST_ELEMENT_ERROR (src, LIBRARY, INIT,
768 (NULL), ("Could not begin request sent to MythTV server (%i, %s)", ret, src->uri_name));
773 GST_ELEMENT_ERROR (src, RESOURCE, READ,
774 (NULL), ("Seek failed, go to the next program info... (%s)",
777 gst_pad_push_event ( GST_BASE_SRC_PAD (GST_BASE_SRC (src)),
778 gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0, -1, 0 ) );
780 // go to the next program chain
781 src->unique_setup = FALSE;
782 src->update_prog_chain = TRUE;
784 gst_mythtv_src_next_program_chain( src );
790 /* create a new socket for connecting to the next program chain */
792 gst_mythtv_src_next_program_chain ( GstMythtvSrc *src )
794 GString *chain_id_local = NULL;
801 if (src->unique_setup == FALSE) {
802 src->unique_setup = TRUE;
807 GST_PAD_STREAM_LOCK( GST_BASE_SRC_PAD (GST_BASE_SRC (src)) );
809 if (src->file_transfer) {
810 g_object_unref (src->file_transfer);
811 src->file_transfer = NULL;
815 g_free (src->uri_name);
818 if ( src->live_tv ) {
819 if ( gmyth_livetv_next_program_chain( src->spawn_livetv ) == FALSE ) {
820 GST_INFO_OBJECT( src, "\n\n[%s]\t\tFailed to go to the next program chain!!!\n\n", __FUNCTION__ );
824 /* set up the uri variable */
825 src->uri_name = g_strdup( src->spawn_livetv->proginfo->pathname->str );
826 chain_id_local = gmyth_tvchain_get_id( src->spawn_livetv->tvchain );
827 if ( chain_id_local != NULL ) {
828 src->live_chain_id = g_strdup( chain_id_local->str );
829 GST_DEBUG_OBJECT( src, "\t[%s]\tLocal chain ID = %s.\n", __FUNCTION__, src->live_chain_id );
831 src->live_tv_id = src->spawn_livetv->recorder->recorder_num;
832 GST_LOG_OBJECT( src, "[%s] LiveTV id = %d, URI path = %s.\n", __FUNCTION__, src->live_tv_id, src->uri_name );
835 // we do not need to create a new file transfer, just close the previous one and open with the new file name
836 // src->file_transfer = gmyth_file_transfer_new( src->backend_info);
837 gmyth_file_transfer_close (src->backend_info);
839 if ( src->file_transfer == NULL ) {
843 GMythURI *myth_uri = gmyth_uri_new_with_value (src->uri_name);
844 ret = gmyth_file_transfer_open( src->file_transfer, gmyth_uri_get_path (myth_uri) );
845 g_object_unref (myth_uri);
847 if ( src->live_tv == TRUE && ret == TRUE ) {
848 /* loop finished, set the max tries variable to zero again... */
849 wait_to_transfer = 0;
853 while ( wait_to_transfer++ < GMYTHTV_TRANSFER_MAX_WAITS &&
854 ( gmyth_livetv_is_recording( src->spawn_livetv ) == FALSE ) )
859 if ( ret == FALSE ) {
860 #ifndef GST_DISABLE_GST_DEBUG
861 if ( src->mythtv_msgs_dbg )
862 GST_ERROR_OBJECT( src, "MythTV FileTransfer request failed when setting up socket connection!\n" );
864 goto begin_req_failed;
866 src->content_size_last = src->content_size;
868 src->content_size = src->file_transfer->filesize;
869 if ( src->live_tv ) {
870 wait_to_transfer = 0;
871 while ( wait_to_transfer++ < GMYTHTV_TRANSFER_MAX_WAITS && src->content_size < GMYTHTV_TRANSFER_MAX_BUFFER )
872 src->content_size = gst_mythtv_src_get_position( src );
875 src->read_offset = 0;
877 if ( src->bytes_queue != NULL ) {
878 g_byte_array_free( src->bytes_queue, TRUE );
881 src->bytes_queue = g_byte_array_sized_new( INTERNAL_BUFFER_SIZE );
883 //gst_pad_push_event ( GST_BASE_SRC_PAD (GST_BASE_SRC (src)),
884 // gst_event_new_new_segment ( TRUE, 1.0, GST_FORMAT_TIME, 0, src->content_size, 0 ) );
887 src->update_prog_chain = FALSE;
889 GST_PAD_STREAM_UNLOCK( GST_BASE_SRC_PAD (GST_BASE_SRC (src)) );
896 if (src->spawn_livetv != NULL )
897 g_object_unref( src->spawn_livetv );
899 GST_ELEMENT_ERROR (src, LIBRARY, INIT,
900 (NULL), ("Could not initialize MythTV library (%i, %s)", ret, src->uri_name));
905 GST_ELEMENT_ERROR (src, LIBRARY, INIT,
906 (NULL), ("Could not begin request sent to MythTV server (%i, %s)", ret, src->uri_name));
913 gst_mythtv_src_get_size (GstBaseSrc * bsrc, guint64 * size)
915 GstMythtvSrc *src = GST_MYTHTV_SRC (bsrc);
917 GST_LOG_OBJECT( src, "[%s] Differs from previous content size: %d (max.: %d)\n", __FUNCTION__,
918 abs( src->content_size - src->prev_content_size ), GMYTHTV_TRANSFER_MAX_BUFFER );
920 if ( src->live_tv ) {
922 } else if ( src->live_tv && src->enable_timing_position && ( abs( src->content_size - src->bytes_read ) <
923 GMYTHTV_TRANSFER_MAX_BUFFER ) ) {
925 gint64 new_offset = gmyth_recorder_get_file_position( src->spawn_livetv->recorder );
926 if ( new_offset > 0 && new_offset > src->content_size ) {
927 src->content_size = new_offset;
928 } else if ( new_offset < src->content_size ) {
929 src->update_prog_chain = TRUE;
934 *size = src->content_size;
935 GST_LOG_OBJECT( src, "[%s] Content size = %lld\n", __FUNCTION__, src->content_size );
941 /* close the socket and associated resources
942 * used both to recover from errors and go to NULL state */
944 gst_mythtv_src_stop (GstBaseSrc * bsrc)
948 src = GST_MYTHTV_SRC (bsrc);
951 g_free (src->uri_name);
952 src->uri_name = NULL;
955 if (src->mythtv_caps) {
956 gst_caps_unref (src->mythtv_caps);
957 src->mythtv_caps = NULL;
960 if (src->backend_info != NULL) {
961 g_object_unref (src->backend_info);
962 src->backend_info = NULL;
965 if (src->file_transfer != NULL) {
966 gmyth_file_transfer_close (src->file_transfer);
967 g_object_unref (src->file_transfer);
968 src->file_transfer = NULL;
977 gst_mythtv_src_handle_event (GstPad * pad, GstEvent * event)
979 GstMythtvSrc *src = GST_MYTHTV_SRC (GST_PAD_PARENT (pad));
980 gint64 cont_size = 0;
981 gboolean ret = FALSE;
983 switch (GST_EVENT_TYPE (event)) {
985 GST_WARNING_OBJECT( src, "[%s] Got EOS event!!!\n", __FUNCTION__ );
987 if ( src->live_tv ) {
988 cont_size = gst_mythtv_src_get_position (src);
989 if ( cont_size > src->content_size ) {
990 src->content_size = cont_size;
994 gst_element_set_state ( GST_ELEMENT (src), GST_STATE_NULL );
995 gst_element_set_locked_state ( GST_ELEMENT (src), FALSE );
1000 ret = gst_pad_event_default (pad, event);
1007 gst_mythtv_src_is_seekable( GstBaseSrc *push_src )
1013 gst_mythtv_src_handle_query (GstPad * pad, GstQuery * query)
1015 gboolean res = FALSE;
1016 GstMythtvSrc *myth = GST_MYTHTV_SRC (gst_pad_get_parent (pad));
1019 switch (GST_QUERY_TYPE (query)) {
1020 case GST_QUERY_POSITION:
1023 gst_query_parse_position (query, &formt, &pos );
1025 if ( formt == GST_FORMAT_BYTES ) {
1026 gst_query_set_position (query, formt, pos = myth->read_offset );
1027 GST_DEBUG_OBJECT (myth, "POS %lld (BYTES).\n", pos );
1028 } else if ( formt == GST_FORMAT_TIME ) {
1029 GST_DEBUG_OBJECT (myth, "POS %lld (TIME).\n", pos );
1030 res = gst_pad_query_default(pad, query);
1034 case GST_QUERY_DURATION:
1037 if (myth->duration != 0) {
1041 fps = nuv->h->i_fpsn / nuv->h->i_fpsd;
1042 total = gst_util_uint64_scale_int (GST_SECOND, nuv->h->i_video_blocks, fps);
1046 gst_query_parse_duration ( query, &formt, &dur );
1047 if ( formt == GST_FORMAT_BYTES ) {
1048 gst_query_set_duration (query, formt, dur = myth->content_size);
1049 GST_DEBUG_OBJECT (myth, "DURATION %lld (BYTES).\n", dur );
1050 } else if ( formt == GST_FORMAT_TIME ) {
1051 GST_DEBUG_OBJECT (myth, "DURATION %lld (TIME).\n", dur );
1052 gst_query_ref(query);
1053 res = gst_pad_query_default(pad, query);
1060 res = gst_pad_query_default(pad, query);
1065 gst_object_unref (myth);
1070 static GstStateChangeReturn
1071 gst_mythtv_src_change_state (GstElement * element, GstStateChange transition)
1073 GstStateChangeReturn ret = GST_STATE_CHANGE_FAILURE;//GST_STATE_CHANGE_NO_PREROLL;
1074 GstMythtvSrc *src = GST_MYTHTV_SRC (element);
1076 switch (transition) {
1077 case GST_STATE_CHANGE_NULL_TO_READY:
1078 //src->do_start = TRUE;
1079 //src->unique_setup = FALSE;
1081 case GST_STATE_CHANGE_READY_TO_PAUSED:
1082 GST_INFO_OBJECT( src, "[%s] READY to PAUSED called!\n", __FUNCTION__ );
1084 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1085 GST_INFO_OBJECT( src, "[%s] PAUSED to PLAYING called!\n", __FUNCTION__ );
1086 if ( src->live_tv ) {
1087 if ( !gmyth_recorder_send_frontend_ready_command( src->spawn_livetv->recorder ) )
1088 GST_WARNING_OBJECT( src, "[%s] Couldn't send the FRONTEND_READY message to the backend!", __FUNCTION__ );
1090 GST_DEBUG_OBJECT( src, "[%s] Message FRONTEND_READY was sent to the backend!", __FUNCTION__ );
1098 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1099 if (ret == GST_STATE_CHANGE_FAILURE)
1102 switch (transition) {
1103 case GST_STATE_CHANGE_READY_TO_NULL:
1104 GST_INFO_OBJECT( src, "[%s] READY to NULL called!\n", __FUNCTION__ );
1106 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1107 GST_INFO_OBJECT( src, "[%s] PLAYING to PAUSED called!\n", __FUNCTION__ );
1108 case GST_STATE_CHANGE_PAUSED_TO_READY:
1109 GST_INFO_OBJECT( src, "[%s] PAUSED to READY called!\n", __FUNCTION__ );
1110 if ( src->live_tv ) {
1111 if ( !gmyth_recorder_send_frontend_ready_command( src->spawn_livetv->recorder ) )
1112 GST_WARNING_OBJECT( src, "[%s] Couldn't send the FRONTEND_READY message to the backend!", __FUNCTION__ );
1114 GST_DEBUG_OBJECT( src, "[%s] Message FRONTEND_READY was sent to the backend!", __FUNCTION__ );
1118 gst_pad_push_event ( GST_BASE_SRC_PAD (GST_BASE_SRC (src)),
1119 gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0, -1, 0 ) );
1121 src->read_offset = 0;
1122 src->bytes_read = 0;
1123 src->unique_setup = FALSE;
1124 gst_mythtv_src_next_program_chain( src );
1137 gst_mythtv_src_set_property (GObject * object, guint prop_id,
1138 const GValue * value, GParamSpec * pspec)
1140 GstMythtvSrc *mythtvsrc = GST_MYTHTV_SRC (object);
1142 GST_OBJECT_LOCK (mythtvsrc);
1147 if (!g_value_get_string (value)) {
1148 GST_WARNING ("location property cannot be NULL");
1152 if (mythtvsrc->uri_name != NULL) {
1153 g_free (mythtvsrc->uri_name);
1154 mythtvsrc->uri_name = NULL;
1156 mythtvsrc->uri_name = g_value_dup_string (value);
1160 #ifndef GST_DISABLE_GST_DEBUG
1161 case PROP_GMYTHTV_DBG:
1163 mythtvsrc->mythtv_msgs_dbg = g_value_get_boolean (value);
1167 case PROP_GMYTHTV_VERSION:
1169 mythtvsrc->mythtv_version = g_value_get_int (value);
1172 case PROP_GMYTHTV_LIVEID:
1174 mythtvsrc->live_tv_id = g_value_get_int (value);
1177 case PROP_GMYTHTV_LIVE:
1179 mythtvsrc->live_tv = g_value_get_boolean (value);
1182 case PROP_GMYTHTV_ENABLE_TIMING_POSITION:
1184 mythtvsrc->enable_timing_position = g_value_get_boolean (value);
1187 case PROP_GMYTHTV_LIVE_CHAINID:
1189 if (!g_value_get_string (value)) {
1190 GST_WARNING ("MythTV Live chainid property cannot be NULL");
1194 if (mythtvsrc->live_chain_id != NULL) {
1195 g_free (mythtvsrc->live_chain_id);
1196 mythtvsrc->live_chain_id = NULL;
1198 mythtvsrc->live_chain_id = g_value_dup_string (value);
1201 case PROP_GMYTHTV_CHANNEL_NUM:
1203 mythtvsrc->channel_num = g_value_get_int (value);
1207 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1210 GST_OBJECT_UNLOCK (mythtvsrc);
1216 gst_mythtv_src_get_property (GObject * object, guint prop_id,
1217 GValue * value, GParamSpec * pspec)
1219 GstMythtvSrc *mythtvsrc = GST_MYTHTV_SRC (object);
1221 GST_OBJECT_LOCK (mythtvsrc);
1226 gchar *str = g_strdup( "" );
1228 if ( mythtvsrc->uri_name == NULL ) {
1229 g_free (mythtvsrc->uri_name);
1230 mythtvsrc->uri_name = NULL;
1232 str = g_strdup( mythtvsrc->uri_name );
1234 g_value_set_string ( value, str );
1237 #ifndef GST_DISABLE_GST_DEBUG
1238 case PROP_GMYTHTV_DBG:
1239 g_value_set_boolean ( value, mythtvsrc->mythtv_msgs_dbg );
1242 case PROP_GMYTHTV_VERSION:
1244 g_value_set_int ( value, mythtvsrc->mythtv_version );
1247 case PROP_GMYTHTV_LIVEID:
1249 g_value_set_int ( value, mythtvsrc->live_tv_id );
1252 case PROP_GMYTHTV_LIVE:
1253 g_value_set_boolean ( value, mythtvsrc->live_tv );
1255 case PROP_GMYTHTV_ENABLE_TIMING_POSITION:
1256 g_value_set_boolean ( value, mythtvsrc->enable_timing_position );
1258 case PROP_GMYTHTV_LIVE_CHAINID:
1260 gchar *str = g_strdup( "" );
1262 if ( mythtvsrc->live_chain_id == NULL ) {
1263 g_free (mythtvsrc->live_chain_id);
1264 mythtvsrc->live_chain_id = NULL;
1266 str = g_strdup( mythtvsrc->live_chain_id );
1268 g_value_set_string ( value, str );
1271 case PROP_GMYTHTV_CHANNEL_NUM:
1273 g_value_set_int ( value, mythtvsrc->channel_num );
1277 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1280 GST_OBJECT_UNLOCK (mythtvsrc);
1283 /* entry point to initialize the plug-in
1284 * initialize the plug-in itself
1285 * register the element factories and pad templates
1286 * register the features
1289 plugin_init (GstPlugin * plugin)
1291 return gst_element_register (plugin, "mythtvsrc", GST_RANK_NONE,
1292 GST_TYPE_MYTHTV_SRC);
1295 /* this is the structure that gst-register looks for
1296 * so keep the name plugin_desc, or you cannot get your plug-in registered */
1297 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1301 plugin_init, VERSION, "LGPL", "GStreamer", "http://gstreamer.net/")
1304 /*** GSTURIHANDLER INTERFACE *************************************************/
1306 gst_mythtv_src_uri_get_type (void)
1312 gst_mythtv_src_uri_get_protocols (void)
1314 static gchar *protocols[] = { "myth", "myths", NULL };
1319 static const gchar *
1320 gst_mythtv_src_uri_get_uri (GstURIHandler * handler)
1322 GstMythtvSrc *src = GST_MYTHTV_SRC (handler);
1324 return src->uri_name;
1328 gst_mythtv_src_uri_set_uri (GstURIHandler * handler, const gchar * uri)
1330 GstMythtvSrc *src = GST_MYTHTV_SRC (handler);
1334 protocol = gst_uri_get_protocol (uri);
1335 if ((strcmp (protocol, "myth") != 0) && (strcmp (protocol, "myths") != 0)) {
1340 g_object_set (src, "location", uri, NULL);
1346 gst_mythtv_src_uri_handler_init (gpointer g_iface, gpointer iface_data)
1348 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
1350 iface->get_type = gst_mythtv_src_uri_get_type;
1351 iface->get_protocols = gst_mythtv_src_uri_get_protocols;
1352 iface->get_uri = gst_mythtv_src_uri_get_uri;
1353 iface->set_uri = gst_mythtv_src_uri_set_uri;
1357 size_header_handler (void *userdata, const char *value)
1359 GstMythtvSrc *src = GST_MYTHTV_SRC (userdata);
1361 //src->content_size = g_ascii_strtoull (value, NULL, 10);
1363 GST_DEBUG_OBJECT (src, "content size = %lld bytes", src->content_size);