[svn r124] Fixes to the newly added BackendInfo issues.
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_RESENDS 5
43 #define GMYTHTV_TRANSFER_MAX_BUFFER 128*1024
47 #define MAX_READ_SIZE 4*1024
50 #define GST_FLOW_ERROR_NO_DATA -101
52 #define INTERNAL_BUFFER_SIZE 40*1024
54 /* stablish a maximum iteration value to the IS_RECORDING message */
55 static guint wait_to_transfer = 0;
57 static const GstElementDetails gst_mythtv_src_details =
58 GST_ELEMENT_DETAILS ( "MythTV client source",
60 "Control and receive data as a client over the network via raw socket connections using the MythTV protocol",
61 "Rosfran Borges <rosfran.borges@indt.org.br>" );
63 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ( "src",
66 GST_STATIC_CAPS ("video/x-nuv") );
73 #ifndef GST_DISABLE_GST_DEBUG
79 PROP_GMYTHTV_LIVE_CHAINID,
80 PROP_GMYTHTV_ENABLE_TIMING_POSITION,
81 PROP_GMYTHTV_CHANNEL_NUM
84 static void gst_mythtv_src_finalize (GObject * gobject);
86 static GstFlowReturn gst_mythtv_src_create ( GstPushSrc* psrc, GstBuffer** outbuf );
88 static gboolean gst_mythtv_src_start (GstBaseSrc * bsrc);
89 static gboolean gst_mythtv_src_stop (GstBaseSrc * bsrc);
90 static gboolean gst_mythtv_src_get_size (GstBaseSrc * bsrc, guint64 * size);
91 static gboolean gst_mythtv_src_is_seekable( GstBaseSrc *push_src );
94 static void gst_mythtv_src_get_times (GstBaseSrc * src, GstBuffer * buffer,
95 GstClockTime * start, GstClockTime * end);
98 static gboolean gst_mythtv_src_do_seek( GstBaseSrc *base, GstSegment *segment );
100 static gboolean gst_mythtv_src_next_program_chain ( GstMythtvSrc *src );
102 static GstStateChangeReturn
103 gst_mythtv_src_change_state (GstElement * element, GstStateChange transition);
105 static void gst_mythtv_src_set_property (GObject * object, guint prop_id,
106 const GValue * value, GParamSpec * pspec);
107 static void gst_mythtv_src_get_property (GObject * object, guint prop_id,
108 GValue * value, GParamSpec * pspec);
110 static void gst_mythtv_src_uri_handler_init (gpointer g_iface, gpointer iface_data);
112 static gboolean gst_mythtv_src_handle_query (GstPad * pad, GstQuery * query);
114 static gboolean gst_mythtv_src_handle_event (GstPad * pad, GstEvent * event);
116 static gint do_read_request_response (GstMythtvSrc * src, guint size, GByteArray *data_ptr);
118 GStaticRecMutex th_mutex = G_STATIC_REC_MUTEX_INIT;
121 _urihandler_init (GType type)
123 static const GInterfaceInfo urihandler_info = {
124 gst_mythtv_src_uri_handler_init,
129 g_type_add_interface_static (type, GST_TYPE_URI_HANDLER, &urihandler_info);
131 GST_DEBUG_CATEGORY_INIT (mythtvsrc_debug, "mythtvsrc", 0,
135 GST_BOILERPLATE_FULL (GstMythtvSrc, gst_mythtv_src, GstPushSrc,
136 GST_TYPE_PUSH_SRC, _urihandler_init)
139 gst_mythtv_src_base_init (gpointer g_class)
141 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
143 gst_element_class_add_pad_template (element_class,
144 gst_static_pad_template_get (&srctemplate));
146 gst_element_class_set_details (element_class, &gst_mythtv_src_details);
148 element_class->change_state = gst_mythtv_src_change_state;
153 gst_mythtv_src_class_init (GstMythtvSrcClass * klass)
155 GObjectClass *gobject_class;
156 GstPushSrcClass *gstpushsrc_class;
157 GstBaseSrcClass *gstbasesrc_class;
159 gobject_class = (GObjectClass *) klass;
160 gstbasesrc_class = (GstBaseSrcClass *) klass;
161 gstpushsrc_class = (GstPushSrcClass *) klass;
163 gobject_class->set_property = gst_mythtv_src_set_property;
164 gobject_class->get_property = gst_mythtv_src_get_property;
165 gobject_class->finalize = gst_mythtv_src_finalize;
167 g_object_class_install_property
168 (gobject_class, PROP_LOCATION,
169 g_param_spec_string ("location", "Location",
170 "The location. In the form:"
171 "\n\t\t\tmyth://a.com/file.nuv"
172 "\n\t\t\tmyth://a.com:23223/file.nuv "
173 "\n\t\t\ta.com/file.nuv - default scheme 'myth'",
174 "", G_PARAM_READWRITE));
176 g_object_class_install_property
177 (gobject_class, PROP_URI,
178 g_param_spec_string ("uri", "Uri",
179 "The location in form of a URI (deprecated; use location)",
180 "", G_PARAM_READWRITE));
182 g_object_class_install_property
183 (gobject_class, PROP_GMYTHTV_VERSION,
184 g_param_spec_int ("mythtv-version", "mythtv-version",
185 "Change MythTV version",
186 26, 30, 26, G_PARAM_READWRITE));
188 g_object_class_install_property
189 (gobject_class, PROP_GMYTHTV_LIVEID,
190 g_param_spec_int ("mythtv-live-id", "mythtv-live-id",
191 "Change MythTV version",
192 0, 200, GST_GMYTHTV_ID_NUM, G_PARAM_READWRITE));
194 g_object_class_install_property
195 (gobject_class, PROP_GMYTHTV_LIVE_CHAINID,
196 g_param_spec_string ("mythtv-live-chainid", "mythtv-live-chainid",
197 "Sets the MythTV chain ID (from TV Chain)",
198 "", G_PARAM_READWRITE));
200 g_object_class_install_property
201 (gobject_class, PROP_GMYTHTV_LIVE,
202 g_param_spec_boolean ("mythtv-live", "mythtv-live",
203 "Enable MythTV Live TV content streaming",
204 FALSE, G_PARAM_READWRITE));
206 g_object_class_install_property
207 (gobject_class, PROP_GMYTHTV_ENABLE_TIMING_POSITION,
208 g_param_spec_boolean ("mythtv-enable-timing-position", "mythtv-enable-timing-position",
209 "Enable MythTV Live TV content size continuous updating",
210 FALSE, G_PARAM_READWRITE));
212 g_object_class_install_property
213 (gobject_class, PROP_GMYTHTV_CHANNEL_NUM,
214 g_param_spec_int ("mythtv-channel", "mythtv-channel",
215 "Change MythTV channel number",
216 0, 99999, GST_GMYTHTV_CHANNEL_NUM, G_PARAM_READWRITE));
218 #ifndef GST_DISABLE_GST_DEBUG
219 g_object_class_install_property
220 (gobject_class, PROP_GMYTHTV_DBG,
221 g_param_spec_boolean ("mythtv-debug", "mythtv-debug",
222 "Enable MythTV debug messages",
223 FALSE, G_PARAM_READWRITE));
226 gstbasesrc_class->start = gst_mythtv_src_start;
227 gstbasesrc_class->stop = gst_mythtv_src_stop;
228 gstbasesrc_class->get_size = gst_mythtv_src_get_size;
229 gstbasesrc_class->is_seekable = gst_mythtv_src_is_seekable;
231 //gstbasesrc_class->get_times = gst_mythtv_src_get_times;
233 gstbasesrc_class->do_seek = gst_mythtv_src_do_seek;
234 gstpushsrc_class->create = gst_mythtv_src_create;
236 GST_DEBUG_CATEGORY_INIT (mythtvsrc_debug, "mythtvsrc", 0,
237 "MythTV Client Source");
241 gst_mythtv_src_init (GstMythtvSrc * this, GstMythtvSrcClass * g_class)
243 this->file_transfer = NULL;
245 this->unique_setup = FALSE;
247 this->mythtv_version = GMYTHTV_VERSION_DEFAULT;
249 this->state = GST_MYTHTV_SRC_FILE_TRANSFER;
251 this->bytes_read = 0;
253 this->prev_content_size = 0;
255 this->content_size = 0;
256 this->read_offset = 0;
258 this->content_size_last = 0;
260 this->live_tv = FALSE;
262 this->enable_timing_position = FALSE;
263 this->update_prog_chain = FALSE;
265 this->user_agent = g_strdup ("mythtvsrc");
266 this->mythtv_caps = NULL;
267 this->update_prog_chain = FALSE;
269 this->channel_num = 0;
273 this->bytes_queue = NULL;
275 this->th_mutex = NULL;
277 gst_base_src_set_format( GST_BASE_SRC( this ), GST_FORMAT_BYTES );
279 /* gst_base_src_set_live ( GST_BASE_SRC( this ), TRUE ); */
281 gst_pad_set_event_function ( GST_BASE_SRC_PAD(GST_BASE_SRC(this)),
282 gst_mythtv_src_handle_event );
283 gst_pad_set_query_function ( GST_BASE_SRC_PAD(GST_BASE_SRC(this)),
284 gst_mythtv_src_handle_query );
289 gst_mythtv_src_finalize (GObject * gobject)
291 GstMythtvSrc *this = GST_MYTHTV_SRC (gobject);
293 if ( this->th_read_ahead != NULL ) {
294 gst_task_stop( this->th_read_ahead );
295 this->th_read_ahead = NULL;
298 if (this->mythtv_caps) {
299 gst_caps_unref (this->mythtv_caps);
300 this->mythtv_caps = NULL;
303 if (this->file_transfer) {
304 g_object_unref (this->file_transfer);
305 this->file_transfer = NULL;
308 if (this->spawn_livetv) {
309 g_object_unref (this->spawn_livetv);
310 this->spawn_livetv = NULL;
313 if (this->backend_info) {
314 g_object_unref (this->backend_info);
315 this->backend_info = NULL;
318 if (this->uri_name) {
319 g_free (this->uri_name);
322 if (this->user_agent) {
323 g_free (this->user_agent);
326 if ( this->bytes_queue ) {
327 g_byte_array_free( this->bytes_queue, TRUE );
328 this->bytes_queue = NULL;
331 G_OBJECT_CLASS (parent_class)->finalize (gobject);
335 do_read_request_response (GstMythtvSrc * src, guint size, GByteArray *data_ptr)
338 guint sizetoread = size;
339 guint max_iters = GMYTHTV_TRANSFER_MAX_RESENDS;
341 GST_LOG_OBJECT( src, "Starting: [%s] Reading %d bytes...\n", __FUNCTION__, sizetoread );
343 /* Loop sending the Myth File Transfer request:
344 * Retry whilst authentication fails and we supply it. */
347 while ( sizetoread == size && --max_iters > 0) {
349 len = gmyth_file_transfer_read( src->file_transfer,
350 data_ptr, sizetoread, TRUE );
360 if ( src->live_tv == FALSE )
366 if ( len == GMYTHTV_FILE_TRANSFER_READ_ERROR ) { /* -314 */
367 src->update_prog_chain = TRUE;
375 if ( read == sizetoread )
379 if ( ( read < 0 && !src->live_tv ) || max_iters == 0 )
392 gst_mythtv_src_create ( GstPushSrc* psrc, GstBuffer** outbuf )
395 GstFlowReturn ret = GST_FLOW_OK;
398 src = GST_MYTHTV_SRC ( psrc );
400 /* The caller should know the number of bytes and not read beyond EOS. */
401 if (G_UNLIKELY (src->eos))
403 if ( G_UNLIKELY (src->update_prog_chain) )
404 goto change_progchain;
406 //g_static_rec_mutex_lock( &th_mutex );
407 GST_DEBUG_OBJECT( src, "[%s] offset = %llu, size = %d...\n", __FUNCTION__,
408 src->read_offset, MAX_READ_SIZE );
410 GST_DEBUG_OBJECT ( src, "[%s]\t\tCreate: buffer_remain: %d, buffer_size = %d.\n", __FUNCTION__,
411 (gint) src->buffer_remain, src->bytes_queue->len );
413 /* just get from the byte array, no network effort... */
414 if ( ( src->buffer_remain = src->bytes_queue->len ) < MAX_READ_SIZE ) {
415 //if ( src->buffer_remain ) {
416 guint buffer_size_inter = INTERNAL_BUFFER_SIZE - src->buffer_remain;
417 GByteArray *buffer = g_byte_array_new();
419 read = do_read_request_response( src, buffer_size_inter, buffer );
421 if (G_UNLIKELY (read < 0)) {
423 goto change_progchain;
428 if ( G_UNLIKELY (src->update_prog_chain) )
429 goto change_progchain;
431 src->bytes_queue = g_byte_array_append( src->bytes_queue, g_memdup( buffer->data, read ), read );
432 if ( read > buffer_size_inter )
433 GST_WARNING_OBJECT( src, "[%s] INCREASED buffer size! Backend sent more than we ask him... (%d)\n",
434 __FUNCTION__, abs( read - buffer_size_inter ) );
436 src->buffer_remain = src->buffer_remain + read;
438 if ( buffer != NULL ) {
439 g_byte_array_free( buffer, TRUE );
443 GST_DEBUG_OBJECT( src, "[%s]\tBYTES READ (actual) = %d, BYTES READ (cumulative) = %llu, "\
444 "OFFSET = %llu, CONTENT SIZE = %llu.\n", __FUNCTION__, read, src->bytes_read,
445 src->read_offset, src->content_size );
449 guint buffer_size = ( src->buffer_remain < MAX_READ_SIZE) ? src->buffer_remain : MAX_READ_SIZE;
451 /* Create the buffer. */
452 ret = gst_pad_alloc_buffer ( GST_BASE_SRC_PAD (GST_BASE_SRC (psrc)),
453 src->read_offset, buffer_size,
454 GST_PAD_CAPS (GST_BASE_SRC_PAD (GST_BASE_SRC (psrc))), outbuf );
456 if (G_UNLIKELY (ret != GST_FLOW_OK)) {
458 goto change_progchain;
463 /* gets the first buffer_size bytes from the byte array buffer variable */
464 guint8 *buf = g_memdup( src->bytes_queue->data, buffer_size );
466 GST_DEBUG_OBJECT( src, "[%s] read from network? %s!, buffer_remain = %d\n", __FUNCTION__,
467 read == -1 ? "NO, got from buffer" : "YES, go see the backend's log file", src->buffer_remain );
469 GST_BUFFER_SIZE (*outbuf) = buffer_size;
470 GST_BUFFER_MALLOCDATA( *outbuf ) = g_malloc0( GST_BUFFER_SIZE (*outbuf) );
471 GST_BUFFER_DATA( *outbuf ) = GST_BUFFER_MALLOCDATA( *outbuf );
472 g_memmove( GST_BUFFER_DATA( (*outbuf) ), buf, GST_BUFFER_SIZE(*outbuf) );
473 GST_BUFFER_OFFSET (*outbuf) = src->read_offset;
474 GST_BUFFER_OFFSET_END (*outbuf) = src->read_offset + GST_BUFFER_SIZE (*outbuf);
476 src->buffer_remain -= GST_BUFFER_SIZE (*outbuf);
478 src->read_offset += GST_BUFFER_SIZE (*outbuf);
479 src->bytes_read += GST_BUFFER_SIZE (*outbuf);
480 GST_DEBUG_OBJECT( src, "[%s]\t\tBuffer output with size: %d\n", __FUNCTION__, GST_BUFFER_SIZE (*outbuf) );
482 /* flushs the newly buffer got from byte array */
483 src->bytes_queue = g_byte_array_remove_range( src->bytes_queue, 0, buffer_size );
485 GST_DEBUG_OBJECT( src, "Got buffer: [%s]\t\tBUFFER --->SIZE = %d, OFFSET = %llu, "\
486 "OFFSET_END = %llu.\n\n", __FUNCTION__, GST_BUFFER_SIZE (*outbuf),
487 GST_BUFFER_OFFSET (*outbuf), GST_BUFFER_OFFSET_END (*outbuf) );
493 const gchar *reason = gst_flow_get_name (ret);
495 GST_DEBUG_OBJECT (src, "DONE task, reason %s", reason);
500 const gchar *reason = gst_flow_get_name (ret);
502 GST_DEBUG_OBJECT (src, "pausing task, reason %s", reason);
503 return GST_FLOW_UNEXPECTED;
508 GST_ELEMENT_ERROR (src, RESOURCE, READ,
509 (NULL), ("Could not read any bytes (%i, %s)", read,
511 return GST_FLOW_ERROR;
515 GST_ELEMENT_ERROR (src, RESOURCE, READ,
516 (NULL), ("Seek failed, go to the next program info... (%i, %s)", read,
519 gst_pad_push_event ( GST_BASE_SRC_PAD (GST_BASE_SRC (psrc)),
520 gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0, -1, 0 ) );
521 // go to the next program chain
522 src->unique_setup = FALSE;
523 src->update_prog_chain = TRUE;
525 gst_mythtv_src_next_program_chain( src );
527 return GST_FLOW_ERROR_NO_DATA;
533 gst_mythtv_src_get_position ( GstMythtvSrc* src )
538 if (src->live_tv == TRUE && ( abs( src->content_size - src->bytes_read ) <
539 GMYTHTV_TRANSFER_MAX_BUFFER ) ) {
543 size_tmp = gmyth_recorder_get_file_position( src->spawn_livetv->recorder );
544 if ( size_tmp > ( src->content_size + GMYTHTV_TRANSFER_MAX_BUFFER ) )
545 src->content_size = size_tmp;
546 else if ( size_tmp > 0 && --max_tries > 0 )
548 GST_LOG_OBJECT( src, "\t[%s]\tGET_POSITION: file_position = %lld\n",
549 __FUNCTION__, size_tmp );
550 /* sets the last content size amount before it can be updated */
551 src->prev_content_size = src->content_size;
554 return src->content_size;
559 gst_mythtv_src_do_seek( GstBaseSrc *base, GstSegment *segment )
561 GstMythtvSrc *src = GST_MYTHTV_SRC( base );
562 gint64 new_offset = -1;
563 gint64 actual_seek = segment->start;
566 GST_LOG_OBJECT( src, "[%s]DO Seek called! (start = %lld, stop = %lld)\n", __FUNCTION__,
567 segment->start, segment->stop );
569 if ( segment->format == GST_FORMAT_TIME )
572 //actual_seek = ( ( segment->start / 1000 ) * 28 ) * 4000;
574 GST_LOG_OBJECT( src, "[%s]Trying to seek at the value (actual_seek = %lld, read_offset = %lld)\n", __FUNCTION__, actual_seek, src->read_offset );
575 /* verify if it needs to seek */
576 if ( src->read_offset != actual_seek )
579 new_offset = gmyth_file_transfer_seek( src->file_transfer, segment->start, SEEK_SET );
581 GST_LOG_OBJECT( src, "[%s] Segment offset start = %lld, SRC Offset = %lld, NEW actual backend SEEK Offset = %lld.\n",
582 __FUNCTION__, segment->start, src->read_offset, new_offset );
583 if ( G_UNLIKELY (new_offset < 0 ) )
587 goto change_progchain;
592 src->read_offset = new_offset;
594 if ( ret == FALSE ) {
595 GST_INFO_OBJECT( src, "[%s] Failed to set the SEEK on segment!\n", __FUNCTION__ );
606 GST_DEBUG_OBJECT (src, "EOS found on seeking!!!");
611 GST_ELEMENT_ERROR (src, RESOURCE, READ,
612 (NULL), ("Seek failed, go to the next program info... (%i, %s)", read,
615 gst_pad_push_event ( GST_BASE_SRC_PAD (base),
616 gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0, -1, 0 ) );
617 /* go to the next program chain */
618 src->unique_setup = FALSE;
619 src->update_prog_chain = TRUE;
621 gst_mythtv_src_next_program_chain( src );
628 /* create a socket for connecting to remote server */
630 gst_mythtv_src_start ( GstBaseSrc * bsrc )
632 GstMythtvSrc *src = GST_MYTHTV_SRC (bsrc);
634 GString *chain_id_local = NULL;
638 if ( G_UNLIKELY (src->update_prog_chain) )
639 goto change_progchain;
641 if (src->unique_setup == FALSE) {
642 src->unique_setup = TRUE;
647 if ( src->live_tv ) {
648 src->backend_info = gmyth_backend_info_new_with_uri( src->uri_name );
649 src->spawn_livetv = gmyth_livetv_new( );
650 if ( gmyth_livetv_setup( src->spawn_livetv, src->backend_info ) == FALSE ) {
655 /* set up the uri variable */
656 src->uri_name = g_strdup( src->spawn_livetv->proginfo->pathname->str );
657 chain_id_local = gmyth_tvchain_get_id( src->spawn_livetv->tvchain );
658 if ( chain_id_local != NULL ) {
659 src->live_chain_id = g_strdup( chain_id_local->str );
660 GST_INFO_OBJECT( src, "\t[%s]\tLocal chain ID = %s.\n", __FUNCTION__, src->live_chain_id );
662 //if ( src->uri_name == NULL || strlen( src->uri_name ) <= 0 )
663 // gmyth_context_initialize();
665 src->live_tv_id = src->spawn_livetv->recorder->recorder_num;
666 GST_LOG_OBJECT( src, "[%s] LiveTV id = %d, URI path = %s.\n", __FUNCTION__, src->live_tv_id, src->uri_name );
669 //src->file_transfer = gmyth_file_transfer_new( src->live_tv_id,
670 // g_string_new( src->uri_name ), -1, src->mythtv_version );
671 src->file_transfer = gmyth_file_transfer_new( );
673 if ( src->file_transfer == NULL ) {
677 ret = gmyth_file_transfer_open( src->file_transfer, g_string_new( src->uri_name ) );
679 /* sets the Playback monitor connection */
680 //ret = gmyth_file_transfer_playback_setup( &(src->file_transfer), src->live_tv );
682 if ( src->live_tv == TRUE && ret == TRUE ) {
683 /* loop finished, set the max tries variable to zero again... */
684 wait_to_transfer = 0;
686 while ( wait_to_transfer++ < GMYTHTV_TRANSFER_MAX_WAITS &&
687 ( gmyth_livetv_is_recording( src->spawn_livetv ) == FALSE ) )
690 /* IS_RECORDING again, just like the MythTV backend does... */
691 gmyth_livetv_is_recording( src->spawn_livetv );
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 GST_INFO_OBJECT( src, "MythTV FileTransfer request failed when setting up socket connection!\n" );
704 goto begin_req_failed;
707 src->content_size = src->file_transfer->filesize;
709 src->do_start = FALSE;
711 /* this is used for the buffer cache */
712 src->bytes_queue = g_byte_array_sized_new( INTERNAL_BUFFER_SIZE );
713 src->buffer_remain = 0;
715 //gst_pad_push_event ( GST_BASE_SRC_PAD (GST_BASE_SRC (src)),
716 // gst_event_new_new_segment ( TRUE, 1.0, GST_FORMAT_TIME, 0, src->content_size, 0 ) );
724 if (src->spawn_livetv != NULL )
725 g_object_unref( src->spawn_livetv );
727 GST_ELEMENT_ERROR (src, LIBRARY, INIT,
728 (NULL), ("Could not initialize MythTV library (%i, %s)", ret, src->uri_name));
733 GST_ELEMENT_ERROR (src, LIBRARY, INIT,
734 (NULL), ("Could not begin request sent to MythTV server (%i, %s)", ret, src->uri_name));
739 GST_ELEMENT_ERROR (src, RESOURCE, READ,
740 (NULL), ("Seek failed, go to the next program info... (%s)",
743 gst_pad_push_event ( GST_BASE_SRC_PAD (GST_BASE_SRC (src)),
744 gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0, -1, 0 ) );
746 // go to the next program chain
747 src->unique_setup = FALSE;
748 src->update_prog_chain = TRUE;
750 gst_mythtv_src_next_program_chain( src );
756 /* create a new socket for connecting to the next program chain */
758 gst_mythtv_src_next_program_chain ( GstMythtvSrc *src )
760 GString *chain_id_local = NULL;
767 if (src->unique_setup == FALSE) {
768 src->unique_setup = TRUE;
773 GST_PAD_STREAM_LOCK( GST_BASE_SRC_PAD (GST_BASE_SRC (src)) );
775 if (src->file_transfer) {
776 g_object_unref (src->file_transfer);
777 src->file_transfer = NULL;
781 g_free (src->uri_name);
784 if ( src->live_tv ) {
785 if ( gmyth_livetv_next_program_chain( src->spawn_livetv ) == FALSE ) {
786 GST_INFO_OBJECT( src, "\n\n[%s]\t\tFailed to go to the next program chain!!!\n\n", __FUNCTION__ );
790 /* set up the uri variable */
791 src->uri_name = g_strdup( src->spawn_livetv->proginfo->pathname->str );
792 chain_id_local = gmyth_tvchain_get_id( src->spawn_livetv->tvchain );
793 if ( chain_id_local != NULL ) {
794 src->live_chain_id = g_strdup( chain_id_local->str );
795 GST_DEBUG_OBJECT( src, "\t[%s]\tLocal chain ID = %s.\n", __FUNCTION__, src->live_chain_id );
797 src->live_tv_id = src->spawn_livetv->recorder->recorder_num;
798 GST_LOG_OBJECT( src, "[%s] LiveTV id = %d, URI path = %s.\n", __FUNCTION__, src->live_tv_id, src->uri_name );
801 src->file_transfer = gmyth_file_transfer_new();
803 if ( src->file_transfer == NULL ) {
807 ret = gmyth_file_transfer_open( src->file_transfer, g_string_new( src->uri_name ) );
809 /* sets the Playback monitor connection */
810 //ret = gmyth_file_transfer_playback_setup( &(src->file_transfer), src->live_tv );
812 if ( src->live_tv == TRUE && ret == TRUE ) {
813 /* loop finished, set the max tries variable to zero again... */
814 wait_to_transfer = 0;
818 while ( wait_to_transfer++ < GMYTHTV_TRANSFER_MAX_WAITS &&
819 ( gmyth_livetv_is_recording( src->spawn_livetv ) == FALSE ) )
823 /* sets the FileTransfer instance connection (video/audio download) */
824 //ret = gmyth_file_transfer_setup( &(src->file_transfer), src->live_tv );
826 if ( ret == FALSE ) {
827 #ifndef GST_DISABLE_GST_DEBUG
828 if ( src->mythtv_msgs_dbg )
829 GST_ERROR_OBJECT( src, "MythTV FileTransfer request failed when setting up socket connection!\n" );
831 goto begin_req_failed;
833 src->content_size_last = src->content_size;
835 src->content_size = src->file_transfer->filesize;
836 if ( src->live_tv ) {
837 wait_to_transfer = 0;
838 while ( wait_to_transfer++ < GMYTHTV_TRANSFER_MAX_WAITS && src->content_size < GMYTHTV_TRANSFER_MAX_BUFFER )
839 src->content_size = gst_mythtv_src_get_position( src );
842 src->read_offset = 0;
844 if ( src->bytes_queue != NULL ) {
845 g_byte_array_free( src->bytes_queue, TRUE );
848 src->bytes_queue = g_byte_array_sized_new( INTERNAL_BUFFER_SIZE );
850 //gst_pad_push_event ( GST_BASE_SRC_PAD (GST_BASE_SRC (src)),
851 // gst_event_new_new_segment ( TRUE, 1.0, GST_FORMAT_TIME, 0, src->content_size, 0 ) );
854 src->update_prog_chain = FALSE;
856 GST_PAD_STREAM_UNLOCK( GST_BASE_SRC_PAD (GST_BASE_SRC (src)) );
863 if (src->spawn_livetv != NULL )
864 g_object_unref( src->spawn_livetv );
866 GST_ELEMENT_ERROR (src, LIBRARY, INIT,
867 (NULL), ("Could not initialize MythTV library (%i, %s)", ret, src->uri_name));
872 GST_ELEMENT_ERROR (src, LIBRARY, INIT,
873 (NULL), ("Could not begin request sent to MythTV server (%i, %s)", ret, src->uri_name));
880 gst_mythtv_src_get_size (GstBaseSrc * bsrc, guint64 * size)
882 GstMythtvSrc *src = GST_MYTHTV_SRC (bsrc);
884 GST_LOG_OBJECT( src, "[%s] Differs from previous content size: %d (max.: %d)\n", __FUNCTION__,
885 abs( src->content_size - src->prev_content_size ), GMYTHTV_TRANSFER_MAX_BUFFER );
887 if ( src->live_tv ) {
889 } else if ( src->live_tv && src->enable_timing_position && ( abs( src->content_size - src->bytes_read ) <
890 GMYTHTV_TRANSFER_MAX_BUFFER ) ) {
892 gint64 new_offset = gmyth_recorder_get_file_position( src->spawn_livetv->recorder );
893 if ( new_offset > 0 && new_offset > src->content_size ) {
894 src->content_size = new_offset;
895 } else if ( new_offset < src->content_size ) {
896 src->update_prog_chain = TRUE;
901 *size = src->content_size;
902 GST_LOG_OBJECT( src, "[%s] Content size = %lld\n", __FUNCTION__, src->content_size );
908 /* close the socket and associated resources
909 * used both to recover from errors and go to NULL state */
911 gst_mythtv_src_stop (GstBaseSrc * bsrc)
915 src = GST_MYTHTV_SRC (bsrc);
918 g_free (src->uri_name);
919 src->uri_name = NULL;
922 if (src->mythtv_caps) {
923 gst_caps_unref (src->mythtv_caps);
924 src->mythtv_caps = NULL;
933 gst_mythtv_src_handle_event (GstPad * pad, GstEvent * event)
935 GstMythtvSrc *src = GST_MYTHTV_SRC (GST_PAD_PARENT (pad));
936 gint64 cont_size = 0;
937 gboolean ret = FALSE;
939 switch (GST_EVENT_TYPE (event)) {
941 GST_WARNING_OBJECT( src, "[%s] Got EOS event!!!\n", __FUNCTION__ );
943 if ( src->live_tv ) {
944 cont_size = gst_mythtv_src_get_position (src);
945 if ( cont_size > src->content_size ) {
946 src->content_size = cont_size;
950 gst_element_set_state ( GST_ELEMENT (src), GST_STATE_NULL );
951 gst_element_set_locked_state ( GST_ELEMENT (src), FALSE );
956 ret = gst_pad_event_default (pad, event);
963 gst_mythtv_src_is_seekable( GstBaseSrc *push_src )
969 gst_mythtv_src_handle_query (GstPad * pad, GstQuery * query)
971 gboolean res = FALSE;
972 GstMythtvSrc *myth = GST_MYTHTV_SRC (gst_pad_get_parent (pad));
975 switch (GST_QUERY_TYPE (query)) {
976 case GST_QUERY_POSITION:
979 gst_query_parse_position (query, &formt, &pos );
981 if ( formt == GST_FORMAT_BYTES )
982 GST_DEBUG_OBJECT (myth, "POS %lld (BYTES).\n", pos );
983 else if ( formt == GST_FORMAT_TIME )
984 GST_DEBUG_OBJECT (myth, "POS %lld (TIME).\n", pos );
987 case GST_QUERY_DURATION:
990 if (myth->duration != 0) {
994 fps = nuv->h->i_fpsn / nuv->h->i_fpsd;
995 total = gst_util_uint64_scale_int (GST_SECOND, nuv->h->i_video_blocks, fps);
999 gst_query_parse_duration( query, &formt, &dur );
1000 //gst_query_set_duration (query, GST_FORMAT_TIME, myth->content_size);
1001 if ( formt == GST_FORMAT_BYTES )
1002 GST_DEBUG_OBJECT (myth, "DURATION %lld (BYTES).\n", dur );
1003 else if ( formt == GST_FORMAT_TIME )
1004 GST_DEBUG_OBJECT (myth, "DURATION %lld (TIME).\n", dur );
1015 gst_object_unref (myth);
1020 static GstStateChangeReturn
1021 gst_mythtv_src_change_state (GstElement * element, GstStateChange transition)
1023 GstStateChangeReturn ret = GST_STATE_CHANGE_FAILURE;//GST_STATE_CHANGE_NO_PREROLL;
1024 GstMythtvSrc *src = GST_MYTHTV_SRC (element);
1026 switch (transition) {
1027 case GST_STATE_CHANGE_NULL_TO_READY:
1028 //src->do_start = TRUE;
1029 //src->unique_setup = FALSE;
1031 case GST_STATE_CHANGE_READY_TO_PAUSED:
1032 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1039 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1040 if (ret == GST_STATE_CHANGE_FAILURE)
1043 switch (transition) {
1044 case GST_STATE_CHANGE_READY_TO_NULL:
1045 GST_INFO_OBJECT( src, "[%s] READY to NULL called!\n", __FUNCTION__ );
1047 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1048 GST_INFO_OBJECT( src, "[%s] PLAYING to PAUSED called!\n", __FUNCTION__ );
1049 case GST_STATE_CHANGE_PAUSED_TO_READY:
1050 GST_INFO_OBJECT( src, "[%s] PAUSED to READY called!\n", __FUNCTION__ );
1051 if ( src->live_tv && src->update_prog_chain ) {
1052 if ( !gmyth_recorder_send_frontend_ready_command( src->spawn_livetv->recorder ) )
1053 GST_WARNING_OBJECT( src, "[%s] Couldn't send the FRONTEND_READY message to the backend!", __FUNCTION__ );
1055 GST_DEBUG_OBJECT( src, "[%s] Message FRONTEND_READY was sent to the backend!", __FUNCTION__ );
1059 gst_pad_push_event ( GST_BASE_SRC_PAD (GST_BASE_SRC (src)),
1060 gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0, -1, 0 ) );
1062 src->read_offset = 0;
1063 src->bytes_read = 0;
1064 src->unique_setup = FALSE;
1065 gst_mythtv_src_next_program_chain( src );
1078 gst_mythtv_src_set_property (GObject * object, guint prop_id,
1079 const GValue * value, GParamSpec * pspec)
1081 GstMythtvSrc *mythtvsrc = GST_MYTHTV_SRC (object);
1083 GST_OBJECT_LOCK (mythtvsrc);
1088 if (!g_value_get_string (value)) {
1089 GST_WARNING ("location property cannot be NULL");
1093 if (mythtvsrc->uri_name != NULL) {
1094 g_free (mythtvsrc->uri_name);
1095 mythtvsrc->uri_name = NULL;
1097 mythtvsrc->uri_name = g_value_dup_string (value);
1101 #ifndef GST_DISABLE_GST_DEBUG
1102 case PROP_GMYTHTV_DBG:
1104 mythtvsrc->mythtv_msgs_dbg = g_value_get_boolean (value);
1108 case PROP_GMYTHTV_VERSION:
1110 mythtvsrc->mythtv_version = g_value_get_int (value);
1113 case PROP_GMYTHTV_LIVEID:
1115 mythtvsrc->live_tv_id = g_value_get_int (value);
1118 case PROP_GMYTHTV_LIVE:
1120 mythtvsrc->live_tv = g_value_get_boolean (value);
1123 case PROP_GMYTHTV_ENABLE_TIMING_POSITION:
1125 mythtvsrc->enable_timing_position = g_value_get_boolean (value);
1128 case PROP_GMYTHTV_LIVE_CHAINID:
1130 if (!g_value_get_string (value)) {
1131 GST_WARNING ("MythTV Live chainid property cannot be NULL");
1135 if (mythtvsrc->live_chain_id != NULL) {
1136 g_free (mythtvsrc->live_chain_id);
1137 mythtvsrc->live_chain_id = NULL;
1139 mythtvsrc->live_chain_id = g_value_dup_string (value);
1142 case PROP_GMYTHTV_CHANNEL_NUM:
1144 mythtvsrc->channel_num = g_value_get_int (value);
1148 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1151 GST_OBJECT_UNLOCK (mythtvsrc);
1157 gst_mythtv_src_get_property (GObject * object, guint prop_id,
1158 GValue * value, GParamSpec * pspec)
1160 GstMythtvSrc *mythtvsrc = GST_MYTHTV_SRC (object);
1162 GST_OBJECT_LOCK (mythtvsrc);
1167 gchar *str = g_strdup( "" );
1169 if ( mythtvsrc->uri_name == NULL ) {
1170 g_free (mythtvsrc->uri_name);
1171 mythtvsrc->uri_name = NULL;
1173 str = g_strdup( mythtvsrc->uri_name );
1175 g_value_set_string ( value, str );
1178 #ifndef GST_DISABLE_GST_DEBUG
1179 case PROP_GMYTHTV_DBG:
1180 g_value_set_boolean ( value, mythtvsrc->mythtv_msgs_dbg );
1183 case PROP_GMYTHTV_VERSION:
1185 g_value_set_int ( value, mythtvsrc->mythtv_version );
1188 case PROP_GMYTHTV_LIVEID:
1190 g_value_set_int ( value, mythtvsrc->live_tv_id );
1193 case PROP_GMYTHTV_LIVE:
1194 g_value_set_boolean ( value, mythtvsrc->live_tv );
1196 case PROP_GMYTHTV_ENABLE_TIMING_POSITION:
1197 g_value_set_boolean ( value, mythtvsrc->enable_timing_position );
1199 case PROP_GMYTHTV_LIVE_CHAINID:
1201 gchar *str = g_strdup( "" );
1203 if ( mythtvsrc->live_chain_id == NULL ) {
1204 g_free (mythtvsrc->live_chain_id);
1205 mythtvsrc->live_chain_id = NULL;
1207 str = g_strdup( mythtvsrc->live_chain_id );
1209 g_value_set_string ( value, str );
1212 case PROP_GMYTHTV_CHANNEL_NUM:
1214 g_value_set_int ( value, mythtvsrc->channel_num );
1218 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1221 GST_OBJECT_UNLOCK (mythtvsrc);
1224 /* entry point to initialize the plug-in
1225 * initialize the plug-in itself
1226 * register the element factories and pad templates
1227 * register the features
1230 plugin_init (GstPlugin * plugin)
1232 return gst_element_register (plugin, "mythtvsrc", GST_RANK_NONE,
1233 GST_TYPE_MYTHTV_SRC);
1236 /* this is the structure that gst-register looks for
1237 * so keep the name plugin_desc, or you cannot get your plug-in registered */
1238 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1242 plugin_init, VERSION, "LGPL", "GStreamer", "http://gstreamer.net/")
1245 /*** GSTURIHANDLER INTERFACE *************************************************/
1247 gst_mythtv_src_uri_get_type (void)
1253 gst_mythtv_src_uri_get_protocols (void)
1255 static gchar *protocols[] = { "myth", "myths", NULL };
1260 static const gchar *
1261 gst_mythtv_src_uri_get_uri (GstURIHandler * handler)
1263 GstMythtvSrc *src = GST_MYTHTV_SRC (handler);
1265 return src->uri_name;
1269 gst_mythtv_src_uri_set_uri (GstURIHandler * handler, const gchar * uri)
1271 GstMythtvSrc *src = GST_MYTHTV_SRC (handler);
1275 protocol = gst_uri_get_protocol (uri);
1276 if ((strcmp (protocol, "myth") != 0) && (strcmp (protocol, "myths") != 0)) {
1281 g_object_set (src, "location", uri, NULL);
1287 gst_mythtv_src_uri_handler_init (gpointer g_iface, gpointer iface_data)
1289 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
1291 iface->get_type = gst_mythtv_src_uri_get_type;
1292 iface->get_protocols = gst_mythtv_src_uri_get_protocols;
1293 iface->get_uri = gst_mythtv_src_uri_get_uri;
1294 iface->set_uri = gst_mythtv_src_uri_set_uri;
1298 size_header_handler (void *userdata, const char *value)
1300 GstMythtvSrc *src = GST_MYTHTV_SRC (userdata);
1302 //src->content_size = g_ascii_strtoull (value, NULL, 10);
1304 GST_DEBUG_OBJECT (src, "content size = %lld bytes", src->content_size);