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>
27 #include <gmyth/gmyth_context.h>
32 GST_DEBUG_CATEGORY_STATIC (mythtvsrc_debug);
33 #define GST_CAT_DEFAULT mythtvsrc_debug
35 #define GST_GMYTHTV_ID_NUM 1
37 #define GST_GMYTHTV_CHANNEL_NUM 1000
39 #define GMYTHTV_VERSION_DEFAULT 30
41 #define GMYTHTV_TRANSFER_MAX_WAITS 100
43 #define GMYTHTV_TRANSFER_MAX_RESENDS 5
45 #define GMYTHTV_TRANSFER_MAX_BUFFER 128*1024
49 #define MAX_READ_SIZE 4*1024
52 #define GST_FLOW_ERROR_NO_DATA -101
54 #define INTERNAL_BUFFER_SIZE 40*1024
56 /* stablish a maximum iteration value to the IS_RECORDING message */
57 static guint wait_to_transfer = 0;
59 static const GstElementDetails gst_mythtv_src_details =
60 GST_ELEMENT_DETAILS ( "MythTV client source",
62 "Control and receive data as a client over the network via raw socket connections using the MythTV protocol",
63 "Rosfran Borges <rosfran.borges@indt.org.br>" );
65 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ( "src",
68 GST_STATIC_CAPS ("video/x-nuv") );
75 #ifndef GST_DISABLE_GST_DEBUG
81 PROP_GMYTHTV_LIVE_CHAINID,
82 PROP_GMYTHTV_ENABLE_TIMING_POSITION,
83 PROP_GMYTHTV_CHANNEL_NUM
86 static void gst_mythtv_src_finalize (GObject * gobject);
88 static GstFlowReturn gst_mythtv_src_create ( GstPushSrc* psrc, GstBuffer** outbuf );
90 static gboolean gst_mythtv_src_start (GstBaseSrc * bsrc);
91 static gboolean gst_mythtv_src_stop (GstBaseSrc * bsrc);
92 static gboolean gst_mythtv_src_get_size (GstBaseSrc * bsrc, guint64 * size);
93 static gboolean gst_mythtv_src_is_seekable( GstBaseSrc *push_src );
96 static void gst_mythtv_src_get_times (GstBaseSrc * src, GstBuffer * buffer,
97 GstClockTime * start, GstClockTime * end);
100 static gboolean gst_mythtv_src_do_seek( GstBaseSrc *base, GstSegment *segment );
102 static gboolean gst_mythtv_src_next_program_chain ( GstMythtvSrc *src );
104 static GstStateChangeReturn
105 gst_mythtv_src_change_state (GstElement * element, GstStateChange transition);
107 static void gst_mythtv_src_set_property (GObject * object, guint prop_id,
108 const GValue * value, GParamSpec * pspec);
109 static void gst_mythtv_src_get_property (GObject * object, guint prop_id,
110 GValue * value, GParamSpec * pspec);
112 static void gst_mythtv_src_uri_handler_init (gpointer g_iface, gpointer iface_data);
114 static gboolean gst_mythtv_src_handle_query (GstPad * pad, GstQuery * query);
116 static gboolean gst_mythtv_src_handle_event (GstPad * pad, GstEvent * event);
118 static gint do_read_request_response (GstMythtvSrc * src, guint size, GByteArray *data_ptr);
120 GStaticRecMutex th_mutex = G_STATIC_REC_MUTEX_INIT;
123 _urihandler_init (GType type)
125 static const GInterfaceInfo urihandler_info = {
126 gst_mythtv_src_uri_handler_init,
131 g_type_add_interface_static (type, GST_TYPE_URI_HANDLER, &urihandler_info);
133 GST_DEBUG_CATEGORY_INIT (mythtvsrc_debug, "mythtvsrc", 0,
137 GST_BOILERPLATE_FULL (GstMythtvSrc, gst_mythtv_src, GstPushSrc,
138 GST_TYPE_PUSH_SRC, _urihandler_init)
141 gst_mythtv_src_base_init (gpointer g_class)
143 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
145 gst_element_class_add_pad_template (element_class,
146 gst_static_pad_template_get (&srctemplate));
148 gst_element_class_set_details (element_class, &gst_mythtv_src_details);
150 element_class->change_state = gst_mythtv_src_change_state;
155 gst_mythtv_src_class_init (GstMythtvSrcClass * klass)
157 GObjectClass *gobject_class;
158 GstPushSrcClass *gstpushsrc_class;
159 GstBaseSrcClass *gstbasesrc_class;
161 gobject_class = (GObjectClass *) klass;
162 gstbasesrc_class = (GstBaseSrcClass *) klass;
163 gstpushsrc_class = (GstPushSrcClass *) klass;
165 gobject_class->set_property = gst_mythtv_src_set_property;
166 gobject_class->get_property = gst_mythtv_src_get_property;
167 gobject_class->finalize = gst_mythtv_src_finalize;
169 g_object_class_install_property
170 (gobject_class, PROP_LOCATION,
171 g_param_spec_string ("location", "Location",
172 "The location. In the form:"
173 "\n\t\t\tmyth://a.com/file.nuv"
174 "\n\t\t\tmyth://a.com:23223/file.nuv "
175 "\n\t\t\ta.com/file.nuv - default scheme 'myth'",
176 "", G_PARAM_READWRITE));
178 g_object_class_install_property
179 (gobject_class, PROP_URI,
180 g_param_spec_string ("uri", "Uri",
181 "The location in form of a URI (deprecated; use location)",
182 "", G_PARAM_READWRITE));
184 g_object_class_install_property
185 (gobject_class, PROP_GMYTHTV_VERSION,
186 g_param_spec_int ("mythtv-version", "mythtv-version",
187 "Change MythTV version",
188 26, 30, 26, G_PARAM_READWRITE));
190 g_object_class_install_property
191 (gobject_class, PROP_GMYTHTV_LIVEID,
192 g_param_spec_int ("mythtv-live-id", "mythtv-live-id",
193 "Change MythTV version",
194 0, 200, GST_GMYTHTV_ID_NUM, G_PARAM_READWRITE));
196 g_object_class_install_property
197 (gobject_class, PROP_GMYTHTV_LIVE_CHAINID,
198 g_param_spec_string ("mythtv-live-chainid", "mythtv-live-chainid",
199 "Sets the MythTV chain ID (from TV Chain)",
200 "", G_PARAM_READWRITE));
202 g_object_class_install_property
203 (gobject_class, PROP_GMYTHTV_LIVE,
204 g_param_spec_boolean ("mythtv-live", "mythtv-live",
205 "Enable MythTV Live TV content streaming",
206 FALSE, G_PARAM_READWRITE));
208 g_object_class_install_property
209 (gobject_class, PROP_GMYTHTV_ENABLE_TIMING_POSITION,
210 g_param_spec_boolean ("mythtv-enable-timing-position", "mythtv-enable-timing-position",
211 "Enable MythTV Live TV content size continuous updating",
212 FALSE, G_PARAM_READWRITE));
214 g_object_class_install_property
215 (gobject_class, PROP_GMYTHTV_CHANNEL_NUM,
216 g_param_spec_int ("mythtv-channel", "mythtv-channel",
217 "Change MythTV channel number",
218 0, 99999, GST_GMYTHTV_CHANNEL_NUM, G_PARAM_READWRITE));
220 #ifndef GST_DISABLE_GST_DEBUG
221 g_object_class_install_property
222 (gobject_class, PROP_GMYTHTV_DBG,
223 g_param_spec_boolean ("mythtv-debug", "mythtv-debug",
224 "Enable MythTV debug messages",
225 FALSE, G_PARAM_READWRITE));
228 gstbasesrc_class->start = gst_mythtv_src_start;
229 gstbasesrc_class->stop = gst_mythtv_src_stop;
230 gstbasesrc_class->get_size = gst_mythtv_src_get_size;
231 gstbasesrc_class->is_seekable = gst_mythtv_src_is_seekable;
233 //gstbasesrc_class->get_times = gst_mythtv_src_get_times;
235 gstbasesrc_class->do_seek = gst_mythtv_src_do_seek;
236 gstpushsrc_class->create = gst_mythtv_src_create;
238 GST_DEBUG_CATEGORY_INIT (mythtvsrc_debug, "mythtvsrc", 0,
239 "MythTV Client Source");
243 gst_mythtv_src_init (GstMythtvSrc * this, GstMythtvSrcClass * g_class)
245 this->file_transfer = NULL;
247 this->unique_setup = FALSE;
249 this->mythtv_version = GMYTHTV_VERSION_DEFAULT;
251 this->state = GST_MYTHTV_SRC_FILE_TRANSFER;
253 this->bytes_read = 0;
255 this->prev_content_size = 0;
257 this->content_size = 0;
258 this->read_offset = 0;
260 this->content_size_last = 0;
262 this->live_tv = FALSE;
264 this->enable_timing_position = FALSE;
265 this->update_prog_chain = FALSE;
267 this->user_agent = g_strdup ("mythtvsrc");
268 this->mythtv_caps = NULL;
269 this->update_prog_chain = FALSE;
271 this->channel_num = 0;
275 this->bytes_queue = NULL;
277 this->th_mutex = NULL;
279 gst_base_src_set_format( GST_BASE_SRC( this ), GST_FORMAT_BYTES );
281 /* gst_base_src_set_live ( GST_BASE_SRC( this ), TRUE ); */
283 gst_pad_set_event_function ( GST_BASE_SRC_PAD(GST_BASE_SRC(this)),
284 gst_mythtv_src_handle_event );
285 gst_pad_set_query_function ( GST_BASE_SRC_PAD(GST_BASE_SRC(this)),
286 gst_mythtv_src_handle_query );
291 gst_mythtv_src_finalize (GObject * gobject)
293 GstMythtvSrc *this = GST_MYTHTV_SRC (gobject);
295 if ( this->th_read_ahead != NULL ) {
296 gst_task_stop( this->th_read_ahead );
297 this->th_read_ahead = NULL;
300 if (this->mythtv_caps) {
301 gst_caps_unref (this->mythtv_caps);
302 this->mythtv_caps = NULL;
305 if (this->file_transfer) {
306 g_object_unref (this->file_transfer);
307 this->file_transfer = NULL;
310 if (this->spawn_livetv) {
311 g_object_unref (this->spawn_livetv);
312 this->spawn_livetv = NULL;
315 if (this->uri_name) {
316 g_free (this->uri_name);
319 if (this->user_agent) {
320 g_free (this->user_agent);
323 if ( this->bytes_queue ) {
324 g_byte_array_free( this->bytes_queue, TRUE );
325 this->bytes_queue = NULL;
328 G_OBJECT_CLASS (parent_class)->finalize (gobject);
332 do_read_request_response (GstMythtvSrc * src, guint size, GByteArray *data_ptr)
335 guint sizetoread = size;
336 guint max_iters = GMYTHTV_TRANSFER_MAX_RESENDS;
338 GST_LOG_OBJECT( src, "Starting: [%s] Reading %d bytes...\n", __FUNCTION__, sizetoread );
340 /* Loop sending the Myth File Transfer request:
341 * Retry whilst authentication fails and we supply it. */
344 while ( sizetoread == size && --max_iters > 0) {
346 len = gmyth_file_transfer_read( src->file_transfer,
347 data_ptr, sizetoread, TRUE );
357 if ( src->live_tv == FALSE )
363 if ( len == GMYTHTV_FILE_TRANSFER_READ_ERROR ) { /* -314 */
364 src->update_prog_chain = TRUE;
372 if ( read == sizetoread )
376 if ( ( read < 0 && !src->live_tv ) || max_iters == 0 )
389 gst_mythtv_src_create ( GstPushSrc* psrc, GstBuffer** outbuf )
392 GstFlowReturn ret = GST_FLOW_OK;
395 src = GST_MYTHTV_SRC ( psrc );
397 /* The caller should know the number of bytes and not read beyond EOS. */
398 if (G_UNLIKELY (src->eos))
400 if ( G_UNLIKELY (src->update_prog_chain) )
401 goto change_progchain;
403 //g_static_rec_mutex_lock( &th_mutex );
404 GST_DEBUG_OBJECT( src, "[%s] offset = %llu, size = %d...\n", __FUNCTION__,
405 src->read_offset, MAX_READ_SIZE );
407 GST_DEBUG_OBJECT ( src, "[%s]\t\tCreate: buffer_remain: %d\n", __FUNCTION__,
408 (gint) src->buffer_remain);
410 /* just get from the byte array, no network effort... */
411 if ( ( src->buffer_remain = src->bytes_queue->len ) < MAX_READ_SIZE ) {
412 guint buffer_size_inter = INTERNAL_BUFFER_SIZE - src->buffer_remain;
413 GByteArray *buffer = g_byte_array_new();
415 read = do_read_request_response( src, buffer_size_inter, buffer );
417 if (G_UNLIKELY (read < 0)) {
419 goto change_progchain;
424 if ( G_UNLIKELY (src->update_prog_chain) )
425 goto change_progchain;
427 src->bytes_queue = g_byte_array_append( src->bytes_queue, g_memdup( buffer->data, read ), read );
428 if ( read > buffer_size_inter )
429 GST_WARNING_OBJECT( src, "[%s] INCREASED buffer size! Backend sent more than we ask him... (%d)\n",
430 __FUNCTION__, abs( read - buffer_size_inter ) );
432 src->buffer_remain = src->buffer_remain + read;
434 if ( buffer != NULL ) {
435 g_byte_array_free( buffer, TRUE );
439 GST_DEBUG_OBJECT( src, "[%s]\tBYTES READ (actual) = %d, BYTES READ (cumulative) = %llu, "\
440 "OFFSET = %llu, CONTENT SIZE = %llu.\n", __FUNCTION__, read, src->bytes_read,
441 src->read_offset, src->content_size );
445 guint buffer_size = ( src->buffer_remain < MAX_READ_SIZE) ? src->buffer_remain : MAX_READ_SIZE;
447 /* Create the buffer. */
448 ret = gst_pad_alloc_buffer ( GST_BASE_SRC_PAD (GST_BASE_SRC (psrc)),
449 src->read_offset, buffer_size,
450 GST_PAD_CAPS (GST_BASE_SRC_PAD (GST_BASE_SRC (psrc))), outbuf );
452 if (G_UNLIKELY (ret != GST_FLOW_OK)) {
454 goto change_progchain;
459 /* gets the first buffer_size bytes from the byte array buffer variable */
460 guint8 *buf = g_memdup( src->bytes_queue->data, buffer_size );
462 GST_DEBUG_OBJECT( src, "[%s] read from network? %s!, buffer_remain = %d\n", __FUNCTION__,
463 read == -1 ? "NO, got from buffer" : "YES, go see the backend's log file", src->buffer_remain );
465 GST_BUFFER_SIZE (*outbuf) = buffer_size;
466 GST_BUFFER_MALLOCDATA( *outbuf ) = g_malloc0( GST_BUFFER_SIZE (*outbuf) );
467 GST_BUFFER_DATA( *outbuf ) = GST_BUFFER_MALLOCDATA( *outbuf );
468 g_memmove( GST_BUFFER_DATA( (*outbuf) ), buf, GST_BUFFER_SIZE(*outbuf) );
469 GST_BUFFER_OFFSET (*outbuf) = src->read_offset;
470 GST_BUFFER_OFFSET_END (*outbuf) = src->read_offset + GST_BUFFER_SIZE (*outbuf);
472 src->buffer_remain -= GST_BUFFER_SIZE (*outbuf);
474 src->read_offset += GST_BUFFER_SIZE (*outbuf);
475 src->bytes_read += GST_BUFFER_SIZE (*outbuf);
476 GST_DEBUG_OBJECT( src, "[%s]\t\tBuffer output with size: %d\n", __FUNCTION__, GST_BUFFER_SIZE (*outbuf) );
478 /* flushs the newly buffer got from byte array */
479 src->bytes_queue = g_byte_array_remove_range( src->bytes_queue, 0, buffer_size );
481 GST_DEBUG_OBJECT( src, "Got buffer: [%s]\t\tBUFFER --->SIZE = %d, OFFSET = %llu, "\
482 "OFFSET_END = %llu.\n\n", __FUNCTION__, GST_BUFFER_SIZE (*outbuf),
483 GST_BUFFER_OFFSET (*outbuf), GST_BUFFER_OFFSET_END (*outbuf) );
489 const gchar *reason = gst_flow_get_name (ret);
491 GST_DEBUG_OBJECT (src, "DONE task, reason %s", reason);
496 const gchar *reason = gst_flow_get_name (ret);
498 GST_DEBUG_OBJECT (src, "pausing task, reason %s", reason);
499 return GST_FLOW_UNEXPECTED;
504 GST_ELEMENT_ERROR (src, RESOURCE, READ,
505 (NULL), ("Could not read any bytes (%i, %s)", read,
507 return GST_FLOW_ERROR;
511 GST_ELEMENT_ERROR (src, RESOURCE, READ,
512 (NULL), ("Seek failed, go to the next program info... (%i, %s)", read,
515 gst_pad_push_event ( GST_BASE_SRC_PAD (GST_BASE_SRC (psrc)),
516 gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0, -1, 0 ) );
517 // go to the next program chain
518 src->unique_setup = FALSE;
519 src->update_prog_chain = TRUE;
521 gst_mythtv_src_next_program_chain( src );
523 return GST_FLOW_ERROR_NO_DATA;
529 gst_mythtv_src_get_position ( GstMythtvSrc* src )
534 if (src->live_tv == TRUE && ( abs( src->content_size - src->bytes_read ) <
535 GMYTHTV_TRANSFER_MAX_BUFFER ) ) {
539 size_tmp = gmyth_file_transfer_get_file_position( src->file_transfer );
540 if ( size_tmp > ( src->content_size + GMYTHTV_TRANSFER_MAX_BUFFER ) )
541 src->content_size = size_tmp;
542 else if ( size_tmp > 0 && --max_tries > 0 )
544 GST_LOG_OBJECT( src, "\t[%s]\tGET_POSITION: file_position = %lld\n",
545 __FUNCTION__, size_tmp );
546 /* sets the last content size amount before it can be updated */
547 src->prev_content_size = src->content_size;
550 return src->content_size;
555 gst_mythtv_src_do_seek( GstBaseSrc *base, GstSegment *segment )
557 GstMythtvSrc *src = GST_MYTHTV_SRC( base );
558 gint64 new_offset = -1;
559 gint64 actual_seek = segment->start;
562 GST_LOG_OBJECT( src, "[%s]DO Seek called! (start = %lld, stop = %lld)\n", __FUNCTION__,
563 segment->start, segment->stop );
565 if ( segment->format == GST_FORMAT_TIME )
568 //actual_seek = ( ( segment->start / 1000 ) * 28 ) * 4000;
570 GST_LOG_OBJECT( src, "[%s]Trying to seek at the value (actual_seek = %lld, read_offset = %lld)\n", __FUNCTION__, actual_seek, src->read_offset );
571 /* verify if it needs to seek */
572 if ( src->read_offset != actual_seek )
575 new_offset = gmyth_file_transfer_seek( src->file_transfer, segment->start, SEEK_SET );
577 GST_LOG_OBJECT( src, "[%s] Segment offset start = %lld, SRC Offset = %lld, NEW actual backend SEEK Offset = %lld.\n",
578 __FUNCTION__, segment->start, src->read_offset, new_offset );
579 if ( G_UNLIKELY (new_offset < 0 ) )
583 goto change_progchain;
588 src->read_offset = new_offset;
590 if ( ret == FALSE ) {
591 GST_INFO_OBJECT( src, "[%s] Failed to set the SEEK on segment!\n", __FUNCTION__ );
602 GST_DEBUG_OBJECT (src, "EOS found on seeking!!!");
607 GST_ELEMENT_ERROR (src, RESOURCE, READ,
608 (NULL), ("Seek failed, go to the next program info... (%i, %s)", read,
611 gst_pad_push_event ( GST_BASE_SRC_PAD (base),
612 gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0, -1, 0 ) );
613 /* go to the next program chain */
614 src->unique_setup = FALSE;
615 src->update_prog_chain = TRUE;
617 gst_mythtv_src_next_program_chain( src );
624 /* create a socket for connecting to remote server */
626 gst_mythtv_src_start ( GstBaseSrc * bsrc )
628 GstMythtvSrc *src = GST_MYTHTV_SRC (bsrc);
630 GString *chain_id_local = NULL;
634 if ( G_UNLIKELY (src->update_prog_chain) )
635 goto change_progchain;
637 if (src->unique_setup == FALSE) {
638 src->unique_setup = TRUE;
643 if ( src->live_tv ) {
644 gmyth_context_initialize();
645 src->spawn_livetv = gmyth_livetv_new( );
646 if ( gmyth_livetv_setup( src->spawn_livetv ) == FALSE ) {
651 /* set up the uri variable */
652 src->uri_name = g_strdup( src->spawn_livetv->proginfo->pathname->str );
653 chain_id_local = gmyth_tvchain_get_id( src->spawn_livetv->tvchain );
654 if ( chain_id_local != NULL ) {
655 src->live_chain_id = g_strdup( chain_id_local->str );
656 GST_INFO_OBJECT( src, "\t[%s]\tLocal chain ID = %s.\n", __FUNCTION__, src->live_chain_id );
658 src->live_tv_id = src->spawn_livetv->recorder->recorder_num;
659 GST_LOG_OBJECT( src, "[%s] LiveTV id = %d, URI path = %s.\n", __FUNCTION__, src->live_tv_id, src->uri_name );
662 src->file_transfer = gmyth_file_transfer_new( src->live_tv_id,
663 g_string_new( src->uri_name ), -1, src->mythtv_version );
665 if ( src->file_transfer == NULL ) {
669 /* sets the Playback monitor connection */
670 ret = gmyth_file_transfer_playback_setup( &(src->file_transfer), src->live_tv );
672 if ( src->live_tv == TRUE && ret == TRUE ) {
673 /* loop finished, set the max tries variable to zero again... */
674 wait_to_transfer = 0;
676 while ( wait_to_transfer++ < GMYTHTV_TRANSFER_MAX_WAITS &&
677 ( gmyth_livetv_is_recording( src->spawn_livetv ) == FALSE ) )
680 /* IS_RECORDING again, just like the MythTV backend does... */
681 gmyth_livetv_is_recording( src->spawn_livetv );
686 /* sets the FileTransfer instance connection (video/audio download) */
687 ret = gmyth_file_transfer_setup( &(src->file_transfer), src->live_tv );
689 if ( ret == FALSE ) {
690 #ifndef GST_DISABLE_GST_DEBUG
691 if ( src->mythtv_msgs_dbg )
692 GST_INFO_OBJECT( src, "MythTV FileTransfer request failed when setting up socket connection!\n" );
694 goto begin_req_failed;
697 src->content_size = src->file_transfer->filesize;
699 src->do_start = FALSE;
701 src->bytes_queue = g_byte_array_sized_new( INTERNAL_BUFFER_SIZE );
702 //src->th_read_ahead = gst_task_create( (GstTaskFunction)gst_mythtv_src_read_ahead, src );
703 //gst_task_set_lock( src->th_read_ahead, &th_mutex );
704 //gst_task_start( src->th_read_ahead );
705 src->buffer_remain = 0;
713 if (src->spawn_livetv != NULL )
714 g_object_unref( src->spawn_livetv );
716 GST_ELEMENT_ERROR (src, LIBRARY, INIT,
717 (NULL), ("Could not initialize MythTV library (%i, %s)", ret, src->uri_name));
722 GST_ELEMENT_ERROR (src, LIBRARY, INIT,
723 (NULL), ("Could not begin request sent to MythTV server (%i, %s)", ret, src->uri_name));
728 GST_ELEMENT_ERROR (src, RESOURCE, READ,
729 (NULL), ("Seek failed, go to the next program info... (%s)",
732 gst_pad_push_event ( GST_BASE_SRC_PAD (GST_BASE_SRC (src)),
733 gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0, -1, 0 ) );
735 // go to the next program chain
736 src->unique_setup = FALSE;
737 src->update_prog_chain = TRUE;
739 gst_mythtv_src_next_program_chain( src );
745 /* create a new socket for connecting to the next program chain */
747 gst_mythtv_src_next_program_chain ( GstMythtvSrc *src )
749 GString *chain_id_local = NULL;
756 if (src->unique_setup == FALSE) {
757 src->unique_setup = TRUE;
762 GST_PAD_STREAM_LOCK( GST_BASE_SRC_PAD (GST_BASE_SRC (src)) );
764 if (src->file_transfer) {
765 g_object_unref (src->file_transfer);
766 src->file_transfer = NULL;
770 g_free (src->uri_name);
773 if ( src->live_tv ) {
774 if ( gmyth_livetv_next_program_chain( src->spawn_livetv ) == FALSE ) {
775 GST_INFO_OBJECT( src, "\n\n[%s]\t\tFailed to go to the next program chain!!!\n\n", __FUNCTION__ );
779 /* set up the uri variable */
780 src->uri_name = g_strdup( src->spawn_livetv->proginfo->pathname->str );
781 chain_id_local = gmyth_tvchain_get_id( src->spawn_livetv->tvchain );
782 if ( chain_id_local != NULL ) {
783 src->live_chain_id = g_strdup( chain_id_local->str );
784 GST_DEBUG_OBJECT( src, "\t[%s]\tLocal chain ID = %s.\n", __FUNCTION__, src->live_chain_id );
786 src->live_tv_id = src->spawn_livetv->recorder->recorder_num;
787 GST_LOG_OBJECT( src, "[%s] LiveTV id = %d, URI path = %s.\n", __FUNCTION__, src->live_tv_id, src->uri_name );
790 src->file_transfer = gmyth_file_transfer_new( src->live_tv_id,
791 g_string_new( src->uri_name ), -1, src->mythtv_version );
793 if ( src->file_transfer == NULL ) {
797 /* sets the Playback monitor connection */
798 ret = gmyth_file_transfer_playback_setup( &(src->file_transfer), src->live_tv );
800 if ( src->live_tv == TRUE && ret == TRUE ) {
801 /* loop finished, set the max tries variable to zero again... */
802 wait_to_transfer = 0;
806 while ( wait_to_transfer++ < GMYTHTV_TRANSFER_MAX_WAITS &&
807 ( gmyth_livetv_is_recording( src->spawn_livetv ) == FALSE ) )
811 /* sets the FileTransfer instance connection (video/audio download) */
812 ret = gmyth_file_transfer_setup( &(src->file_transfer), src->live_tv );
814 if ( ret == FALSE ) {
815 #ifndef GST_DISABLE_GST_DEBUG
816 if ( src->mythtv_msgs_dbg )
817 GST_ERROR_OBJECT( src, "MythTV FileTransfer request failed when setting up socket connection!\n" );
819 goto begin_req_failed;
821 src->content_size_last = src->content_size;
823 src->content_size = src->file_transfer->filesize;
824 if ( src->live_tv ) {
825 wait_to_transfer = 0;
826 while ( wait_to_transfer++ < GMYTHTV_TRANSFER_MAX_WAITS && src->content_size < GMYTHTV_TRANSFER_MAX_BUFFER )
827 src->content_size = gst_mythtv_src_get_position( src );
830 src->read_offset = 0;
832 if ( src->bytes_queue != NULL ) {
833 g_byte_array_free( src->bytes_queue, TRUE );
836 src->bytes_queue = g_byte_array_sized_new( INTERNAL_BUFFER_SIZE );
839 src->update_prog_chain = FALSE;
841 GST_PAD_STREAM_UNLOCK( GST_BASE_SRC_PAD (GST_BASE_SRC (src)) );
848 if (src->spawn_livetv != NULL )
849 g_object_unref( src->spawn_livetv );
851 GST_ELEMENT_ERROR (src, LIBRARY, INIT,
852 (NULL), ("Could not initialize MythTV library (%i, %s)", ret, src->uri_name));
857 GST_ELEMENT_ERROR (src, LIBRARY, INIT,
858 (NULL), ("Could not begin request sent to MythTV server (%i, %s)", ret, src->uri_name));
865 gst_mythtv_src_get_size (GstBaseSrc * bsrc, guint64 * size)
867 GstMythtvSrc *src = GST_MYTHTV_SRC (bsrc);
869 GST_LOG_OBJECT( src, "[%s] Differs from previous content size: %d (max.: %d)\n", __FUNCTION__,
870 abs( src->content_size - src->prev_content_size ), GMYTHTV_TRANSFER_MAX_BUFFER );
872 if ( src->live_tv ) {
874 } else if ( src->live_tv && src->enable_timing_position && ( abs( src->content_size - src->bytes_read ) <
875 GMYTHTV_TRANSFER_MAX_BUFFER ) ) {
877 gint64 new_offset = gmyth_file_transfer_get_file_position( src->file_transfer );
878 if ( new_offset > 0 && new_offset > src->content_size ) {
879 src->content_size = new_offset;
880 } else if ( new_offset < src->content_size ) {
881 src->update_prog_chain = TRUE;
886 *size = src->content_size;
887 GST_LOG_OBJECT( src, "[%s] Content size = %lld\n", __FUNCTION__, src->content_size );
893 /* close the socket and associated resources
894 * used both to recover from errors and go to NULL state */
896 gst_mythtv_src_stop (GstBaseSrc * bsrc)
900 src = GST_MYTHTV_SRC (bsrc);
903 g_free (src->uri_name);
904 src->uri_name = NULL;
907 if (src->mythtv_caps) {
908 gst_caps_unref (src->mythtv_caps);
909 src->mythtv_caps = NULL;
918 gst_mythtv_src_handle_event (GstPad * pad, GstEvent * event)
920 GstMythtvSrc *src = GST_MYTHTV_SRC (GST_PAD_PARENT (pad));
921 gint64 cont_size = 0;
922 gboolean ret = FALSE;
924 switch (GST_EVENT_TYPE (event)) {
926 GST_WARNING_OBJECT( src, "[%s] Got EOS event!!!\n", __FUNCTION__ );
928 if ( src->live_tv ) {
929 cont_size = gst_mythtv_src_get_position (src);
930 if ( cont_size > src->content_size ) {
931 src->content_size = cont_size;
935 gst_element_set_state ( GST_ELEMENT (src), GST_STATE_NULL );
936 gst_element_set_locked_state ( GST_ELEMENT (src), FALSE );
942 case GST_EVENT_NEWSEGMENT:
943 GST_INFO_OBJECT( src, "[%s] Got NEWSEGMENT!!!\n", __FUNCTION__ );
944 ret = gst_pad_event_default (pad, event);
948 gst_event_ref( event );
952 GstSeekType cur_type, stop_type;
954 gint64 cur = 0, stop = 0;
955 gst_event_parse_seek ( event, &rate, &format,
956 &flags, &cur_type, &cur,
959 GST_INFO_OBJECT( src, "[%s] Got EVENT_SEEK (pos = %lld)!!!\n", __FUNCTION__, cur );
960 if ( !( flags & GST_SEEK_FLAG_FLUSH ) ) {
961 GST_WARNING_OBJECT( src, "[%s] Could get the FLAG_FLUSH message.\n", __FUNCTION__ );
963 if ( format == GST_FORMAT_TIME && ( ret = gst_pad_event_default (pad, event) ) == FALSE ) {
964 gst_event_unref( event );
971 ret = gst_pad_event_default (pad, event);
978 gst_mythtv_src_is_seekable( GstBaseSrc *push_src )
984 gst_mythtv_src_handle_query (GstPad * pad, GstQuery * query)
986 gboolean res = FALSE;
987 GstMythtvSrc *myth = GST_MYTHTV_SRC (gst_pad_get_parent (pad));
989 switch (GST_QUERY_TYPE (query)) {
990 case GST_QUERY_POSITION:
991 gst_query_set_position (query, GST_FORMAT_BYTES,
994 GST_DEBUG_OBJECT (myth, "POS %d", myth->read_offset);
996 case GST_QUERY_DURATION:
998 if (myth->duration != 0) {
1002 fps = nuv->h->i_fpsn / nuv->h->i_fpsd;
1003 total = gst_util_uint64_scale_int (GST_SECOND, nuv->h->i_video_blocks, fps);
1005 //gst_query_set_duration (query, GST_FORMAT_TIME, myth->content_size);
1006 GST_DEBUG_OBJECT (myth, "DURATION %d", myth->content_size);
1014 gst_object_unref (myth);
1019 static GstStateChangeReturn
1020 gst_mythtv_src_change_state (GstElement * element, GstStateChange transition)
1022 GstStateChangeReturn ret = GST_STATE_CHANGE_FAILURE;//GST_STATE_CHANGE_NO_PREROLL;
1023 GstMythtvSrc *src = GST_MYTHTV_SRC (element);
1025 switch (transition) {
1026 case GST_STATE_CHANGE_NULL_TO_READY:
1027 //src->do_start = TRUE;
1028 //src->unique_setup = FALSE;
1030 case GST_STATE_CHANGE_READY_TO_PAUSED:
1031 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1038 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1039 if (ret == GST_STATE_CHANGE_FAILURE)
1042 switch (transition) {
1043 case GST_STATE_CHANGE_READY_TO_NULL:
1044 GST_INFO_OBJECT( src, "[%s] READY to NULL called!\n", __FUNCTION__ );
1046 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1047 GST_INFO_OBJECT( src, "[%s] PLAYING to PAUSED called!\n", __FUNCTION__ );
1048 case GST_STATE_CHANGE_PAUSED_TO_READY:
1049 GST_INFO_OBJECT( src, "[%s] PAUSED to READY called!\n", __FUNCTION__ );
1050 if ( src->live_tv && src->update_prog_chain ) {
1051 if ( !gmyth_recorder_send_frontend_ready_command( src->spawn_livetv->recorder ) )
1052 GST_WARNING_OBJECT( src, "[%s] Couldn't send the FRONTEND_READY message to the backend!", __FUNCTION__ );
1054 GST_DEBUG_OBJECT( src, "[%s] Message FRONTEND_READY was sent to the backend!", __FUNCTION__ );
1058 gst_pad_push_event ( GST_BASE_SRC_PAD (GST_BASE_SRC (src)),
1059 gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0, -1, 0 ) );
1061 src->read_offset = 0;
1062 src->bytes_read = 0;
1063 src->unique_setup = FALSE;
1064 gst_mythtv_src_next_program_chain( src );
1077 gst_mythtv_src_set_property (GObject * object, guint prop_id,
1078 const GValue * value, GParamSpec * pspec)
1080 GstMythtvSrc *mythtvsrc = GST_MYTHTV_SRC (object);
1082 GST_OBJECT_LOCK (mythtvsrc);
1087 if (!g_value_get_string (value)) {
1088 GST_WARNING ("location property cannot be NULL");
1092 if (mythtvsrc->uri_name != NULL) {
1093 g_free (mythtvsrc->uri_name);
1094 mythtvsrc->uri_name = NULL;
1096 mythtvsrc->uri_name = g_value_dup_string (value);
1100 #ifndef GST_DISABLE_GST_DEBUG
1101 case PROP_GMYTHTV_DBG:
1103 mythtvsrc->mythtv_msgs_dbg = g_value_get_boolean (value);
1107 case PROP_GMYTHTV_VERSION:
1109 mythtvsrc->mythtv_version = g_value_get_int (value);
1112 case PROP_GMYTHTV_LIVEID:
1114 mythtvsrc->live_tv_id = g_value_get_int (value);
1117 case PROP_GMYTHTV_LIVE:
1119 mythtvsrc->live_tv = g_value_get_boolean (value);
1122 case PROP_GMYTHTV_ENABLE_TIMING_POSITION:
1124 mythtvsrc->enable_timing_position = g_value_get_boolean (value);
1127 case PROP_GMYTHTV_LIVE_CHAINID:
1129 if (!g_value_get_string (value)) {
1130 GST_WARNING ("MythTV Live chainid property cannot be NULL");
1134 if (mythtvsrc->live_chain_id != NULL) {
1135 g_free (mythtvsrc->live_chain_id);
1136 mythtvsrc->live_chain_id = NULL;
1138 mythtvsrc->live_chain_id = g_value_dup_string (value);
1141 case PROP_GMYTHTV_CHANNEL_NUM:
1143 mythtvsrc->channel_num = g_value_get_int (value);
1147 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1150 GST_OBJECT_UNLOCK (mythtvsrc);
1156 gst_mythtv_src_get_property (GObject * object, guint prop_id,
1157 GValue * value, GParamSpec * pspec)
1159 GstMythtvSrc *mythtvsrc = GST_MYTHTV_SRC (object);
1161 GST_OBJECT_LOCK (mythtvsrc);
1166 gchar *str = g_strdup( "" );
1168 if ( mythtvsrc->uri_name == NULL ) {
1169 g_free (mythtvsrc->uri_name);
1170 mythtvsrc->uri_name = NULL;
1172 str = g_strdup( mythtvsrc->uri_name );
1174 g_value_set_string ( value, str );
1177 #ifndef GST_DISABLE_GST_DEBUG
1178 case PROP_GMYTHTV_DBG:
1179 g_value_set_boolean ( value, mythtvsrc->mythtv_msgs_dbg );
1182 case PROP_GMYTHTV_VERSION:
1184 g_value_set_int ( value, mythtvsrc->mythtv_version );
1187 case PROP_GMYTHTV_LIVEID:
1189 g_value_set_int ( value, mythtvsrc->live_tv_id );
1192 case PROP_GMYTHTV_LIVE:
1193 g_value_set_boolean ( value, mythtvsrc->live_tv );
1195 case PROP_GMYTHTV_ENABLE_TIMING_POSITION:
1196 g_value_set_boolean ( value, mythtvsrc->enable_timing_position );
1198 case PROP_GMYTHTV_LIVE_CHAINID:
1200 gchar *str = g_strdup( "" );
1202 if ( mythtvsrc->live_chain_id == NULL ) {
1203 g_free (mythtvsrc->live_chain_id);
1204 mythtvsrc->live_chain_id = NULL;
1206 str = g_strdup( mythtvsrc->live_chain_id );
1208 g_value_set_string ( value, str );
1211 case PROP_GMYTHTV_CHANNEL_NUM:
1213 g_value_set_int ( value, mythtvsrc->channel_num );
1217 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1220 GST_OBJECT_UNLOCK (mythtvsrc);
1223 /* entry point to initialize the plug-in
1224 * initialize the plug-in itself
1225 * register the element factories and pad templates
1226 * register the features
1229 plugin_init (GstPlugin * plugin)
1231 return gst_element_register (plugin, "mythtvsrc", GST_RANK_NONE,
1232 GST_TYPE_MYTHTV_SRC);
1235 /* this is the structure that gst-register looks for
1236 * so keep the name plugin_desc, or you cannot get your plug-in registered */
1237 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1241 plugin_init, VERSION, "LGPL", "GStreamer", "http://gstreamer.net/")
1244 /*** GSTURIHANDLER INTERFACE *************************************************/
1246 gst_mythtv_src_uri_get_type (void)
1252 gst_mythtv_src_uri_get_protocols (void)
1254 static gchar *protocols[] = { "myth", "myths", NULL };
1259 static const gchar *
1260 gst_mythtv_src_uri_get_uri (GstURIHandler * handler)
1262 GstMythtvSrc *src = GST_MYTHTV_SRC (handler);
1264 return src->uri_name;
1268 gst_mythtv_src_uri_set_uri (GstURIHandler * handler, const gchar * uri)
1270 GstMythtvSrc *src = GST_MYTHTV_SRC (handler);
1274 protocol = gst_uri_get_protocol (uri);
1275 if ((strcmp (protocol, "myth") != 0) && (strcmp (protocol, "myths") != 0)) {
1280 g_object_set (src, "location", uri, NULL);
1286 gst_mythtv_src_uri_handler_init (gpointer g_iface, gpointer iface_data)
1288 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
1290 iface->get_type = gst_mythtv_src_uri_get_type;
1291 iface->get_protocols = gst_mythtv_src_uri_get_protocols;
1292 iface->get_uri = gst_mythtv_src_uri_get_uri;
1293 iface->set_uri = gst_mythtv_src_uri_set_uri;
1297 size_header_handler (void *userdata, const char *value)
1299 GstMythtvSrc *src = GST_MYTHTV_SRC (userdata);
1301 //src->content_size = g_ascii_strtoull (value, NULL, 10);
1303 GST_DEBUG_OBJECT (src, "content size = %lld bytes", src->content_size);