[svn r117] Made changes related with the new adjustments in buffer storage using GByteArray.
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_recorder_get_file_position( src->spawn_livetv->recorder );
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 if ( src->uri_name == NULL || strlen( src->uri_name ) <= 0 )
659 gmyth_context_initialize();
661 src->live_tv_id = src->spawn_livetv->recorder->recorder_num;
662 GST_LOG_OBJECT( src, "[%s] LiveTV id = %d, URI path = %s.\n", __FUNCTION__, src->live_tv_id, src->uri_name );
665 //src->file_transfer = gmyth_file_transfer_new( src->live_tv_id,
666 // g_string_new( src->uri_name ), -1, src->mythtv_version );
667 src->file_transfer = gmyth_file_transfer_new( );
669 if ( src->file_transfer == NULL ) {
673 ret = gmyth_file_transfer_open( src->file_transfer, g_string_new( src->uri_name ) );
675 /* sets the Playback monitor connection */
676 //ret = gmyth_file_transfer_playback_setup( &(src->file_transfer), src->live_tv );
678 if ( src->live_tv == TRUE && ret == TRUE ) {
679 /* loop finished, set the max tries variable to zero again... */
680 wait_to_transfer = 0;
682 while ( wait_to_transfer++ < GMYTHTV_TRANSFER_MAX_WAITS &&
683 ( gmyth_livetv_is_recording( src->spawn_livetv ) == FALSE ) )
686 /* IS_RECORDING again, just like the MythTV backend does... */
687 gmyth_livetv_is_recording( src->spawn_livetv );
692 /* sets the FileTransfer instance connection (video/audio download) */
693 //ret = gmyth_file_transfer_setup( &(src->file_transfer), src->live_tv );
695 if ( ret == FALSE ) {
696 #ifndef GST_DISABLE_GST_DEBUG
697 if ( src->mythtv_msgs_dbg )
698 GST_INFO_OBJECT( src, "MythTV FileTransfer request failed when setting up socket connection!\n" );
700 goto begin_req_failed;
703 src->content_size = src->file_transfer->filesize;
705 src->do_start = FALSE;
707 /* this is used for the buffer cache */
708 src->bytes_queue = g_byte_array_sized_new( INTERNAL_BUFFER_SIZE );
709 src->buffer_remain = 0;
711 //gst_pad_push_event ( GST_BASE_SRC_PAD (GST_BASE_SRC (src)),
712 // gst_event_new_new_segment ( TRUE, 1.0, GST_FORMAT_TIME, 0, src->content_size, 0 ) );
720 if (src->spawn_livetv != NULL )
721 g_object_unref( src->spawn_livetv );
723 GST_ELEMENT_ERROR (src, LIBRARY, INIT,
724 (NULL), ("Could not initialize MythTV library (%i, %s)", ret, src->uri_name));
729 GST_ELEMENT_ERROR (src, LIBRARY, INIT,
730 (NULL), ("Could not begin request sent to MythTV server (%i, %s)", ret, src->uri_name));
735 GST_ELEMENT_ERROR (src, RESOURCE, READ,
736 (NULL), ("Seek failed, go to the next program info... (%s)",
739 gst_pad_push_event ( GST_BASE_SRC_PAD (GST_BASE_SRC (src)),
740 gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0, -1, 0 ) );
742 // go to the next program chain
743 src->unique_setup = FALSE;
744 src->update_prog_chain = TRUE;
746 gst_mythtv_src_next_program_chain( src );
752 /* create a new socket for connecting to the next program chain */
754 gst_mythtv_src_next_program_chain ( GstMythtvSrc *src )
756 GString *chain_id_local = NULL;
763 if (src->unique_setup == FALSE) {
764 src->unique_setup = TRUE;
769 GST_PAD_STREAM_LOCK( GST_BASE_SRC_PAD (GST_BASE_SRC (src)) );
771 if (src->file_transfer) {
772 g_object_unref (src->file_transfer);
773 src->file_transfer = NULL;
777 g_free (src->uri_name);
780 if ( src->live_tv ) {
781 if ( gmyth_livetv_next_program_chain( src->spawn_livetv ) == FALSE ) {
782 GST_INFO_OBJECT( src, "\n\n[%s]\t\tFailed to go to the next program chain!!!\n\n", __FUNCTION__ );
786 /* set up the uri variable */
787 src->uri_name = g_strdup( src->spawn_livetv->proginfo->pathname->str );
788 chain_id_local = gmyth_tvchain_get_id( src->spawn_livetv->tvchain );
789 if ( chain_id_local != NULL ) {
790 src->live_chain_id = g_strdup( chain_id_local->str );
791 GST_DEBUG_OBJECT( src, "\t[%s]\tLocal chain ID = %s.\n", __FUNCTION__, src->live_chain_id );
793 src->live_tv_id = src->spawn_livetv->recorder->recorder_num;
794 GST_LOG_OBJECT( src, "[%s] LiveTV id = %d, URI path = %s.\n", __FUNCTION__, src->live_tv_id, src->uri_name );
797 src->file_transfer = gmyth_file_transfer_new();
799 if ( src->file_transfer == NULL ) {
803 ret = gmyth_file_transfer_open( src->file_transfer, g_string_new( src->uri_name ) );
805 /* sets the Playback monitor connection */
806 //ret = gmyth_file_transfer_playback_setup( &(src->file_transfer), src->live_tv );
808 if ( src->live_tv == TRUE && ret == TRUE ) {
809 /* loop finished, set the max tries variable to zero again... */
810 wait_to_transfer = 0;
814 while ( wait_to_transfer++ < GMYTHTV_TRANSFER_MAX_WAITS &&
815 ( gmyth_livetv_is_recording( src->spawn_livetv ) == FALSE ) )
819 /* sets the FileTransfer instance connection (video/audio download) */
820 //ret = gmyth_file_transfer_setup( &(src->file_transfer), src->live_tv );
822 if ( ret == FALSE ) {
823 #ifndef GST_DISABLE_GST_DEBUG
824 if ( src->mythtv_msgs_dbg )
825 GST_ERROR_OBJECT( src, "MythTV FileTransfer request failed when setting up socket connection!\n" );
827 goto begin_req_failed;
829 src->content_size_last = src->content_size;
831 src->content_size = src->file_transfer->filesize;
832 if ( src->live_tv ) {
833 wait_to_transfer = 0;
834 while ( wait_to_transfer++ < GMYTHTV_TRANSFER_MAX_WAITS && src->content_size < GMYTHTV_TRANSFER_MAX_BUFFER )
835 src->content_size = gst_mythtv_src_get_position( src );
838 src->read_offset = 0;
840 if ( src->bytes_queue != NULL ) {
841 g_byte_array_free( src->bytes_queue, TRUE );
844 src->bytes_queue = g_byte_array_sized_new( INTERNAL_BUFFER_SIZE );
846 //gst_pad_push_event ( GST_BASE_SRC_PAD (GST_BASE_SRC (src)),
847 // gst_event_new_new_segment ( TRUE, 1.0, GST_FORMAT_TIME, 0, src->content_size, 0 ) );
850 src->update_prog_chain = FALSE;
852 GST_PAD_STREAM_UNLOCK( GST_BASE_SRC_PAD (GST_BASE_SRC (src)) );
859 if (src->spawn_livetv != NULL )
860 g_object_unref( src->spawn_livetv );
862 GST_ELEMENT_ERROR (src, LIBRARY, INIT,
863 (NULL), ("Could not initialize MythTV library (%i, %s)", ret, src->uri_name));
868 GST_ELEMENT_ERROR (src, LIBRARY, INIT,
869 (NULL), ("Could not begin request sent to MythTV server (%i, %s)", ret, src->uri_name));
876 gst_mythtv_src_get_size (GstBaseSrc * bsrc, guint64 * size)
878 GstMythtvSrc *src = GST_MYTHTV_SRC (bsrc);
880 GST_LOG_OBJECT( src, "[%s] Differs from previous content size: %d (max.: %d)\n", __FUNCTION__,
881 abs( src->content_size - src->prev_content_size ), GMYTHTV_TRANSFER_MAX_BUFFER );
883 if ( src->live_tv ) {
885 } else if ( src->live_tv && src->enable_timing_position && ( abs( src->content_size - src->bytes_read ) <
886 GMYTHTV_TRANSFER_MAX_BUFFER ) ) {
888 gint64 new_offset = gmyth_recorder_get_file_position( src->spawn_livetv->recorder );
889 if ( new_offset > 0 && new_offset > src->content_size ) {
890 src->content_size = new_offset;
891 } else if ( new_offset < src->content_size ) {
892 src->update_prog_chain = TRUE;
897 *size = src->content_size;
898 GST_LOG_OBJECT( src, "[%s] Content size = %lld\n", __FUNCTION__, src->content_size );
904 /* close the socket and associated resources
905 * used both to recover from errors and go to NULL state */
907 gst_mythtv_src_stop (GstBaseSrc * bsrc)
911 src = GST_MYTHTV_SRC (bsrc);
914 g_free (src->uri_name);
915 src->uri_name = NULL;
918 if (src->mythtv_caps) {
919 gst_caps_unref (src->mythtv_caps);
920 src->mythtv_caps = NULL;
929 gst_mythtv_src_handle_event (GstPad * pad, GstEvent * event)
931 GstMythtvSrc *src = GST_MYTHTV_SRC (GST_PAD_PARENT (pad));
932 gint64 cont_size = 0;
933 gboolean ret = FALSE;
935 switch (GST_EVENT_TYPE (event)) {
937 GST_WARNING_OBJECT( src, "[%s] Got EOS event!!!\n", __FUNCTION__ );
939 if ( src->live_tv ) {
940 cont_size = gst_mythtv_src_get_position (src);
941 if ( cont_size > src->content_size ) {
942 src->content_size = cont_size;
946 gst_element_set_state ( GST_ELEMENT (src), GST_STATE_NULL );
947 gst_element_set_locked_state ( GST_ELEMENT (src), FALSE );
952 ret = gst_pad_event_default (pad, event);
959 gst_mythtv_src_is_seekable( GstBaseSrc *push_src )
965 gst_mythtv_src_handle_query (GstPad * pad, GstQuery * query)
967 gboolean res = FALSE;
968 GstMythtvSrc *myth = GST_MYTHTV_SRC (gst_pad_get_parent (pad));
971 switch (GST_QUERY_TYPE (query)) {
972 case GST_QUERY_POSITION:
975 gst_query_parse_position (query, &formt, &pos );
977 if ( formt == GST_FORMAT_BYTES )
978 GST_DEBUG_OBJECT (myth, "POS %lld (BYTES).\n", pos );
979 else if ( formt == GST_FORMAT_TIME )
980 GST_DEBUG_OBJECT (myth, "POS %lld (TIME).\n", pos );
983 case GST_QUERY_DURATION:
986 if (myth->duration != 0) {
990 fps = nuv->h->i_fpsn / nuv->h->i_fpsd;
991 total = gst_util_uint64_scale_int (GST_SECOND, nuv->h->i_video_blocks, fps);
995 gst_query_parse_duration( query, &formt, &dur );
996 //gst_query_set_duration (query, GST_FORMAT_TIME, myth->content_size);
997 if ( formt == GST_FORMAT_BYTES )
998 GST_DEBUG_OBJECT (myth, "DURATION %lld (BYTES).\n", dur );
999 else if ( formt == GST_FORMAT_TIME )
1000 GST_DEBUG_OBJECT (myth, "DURATION %lld (TIME).\n", dur );
1011 gst_object_unref (myth);
1016 static GstStateChangeReturn
1017 gst_mythtv_src_change_state (GstElement * element, GstStateChange transition)
1019 GstStateChangeReturn ret = GST_STATE_CHANGE_FAILURE;//GST_STATE_CHANGE_NO_PREROLL;
1020 GstMythtvSrc *src = GST_MYTHTV_SRC (element);
1022 switch (transition) {
1023 case GST_STATE_CHANGE_NULL_TO_READY:
1024 //src->do_start = TRUE;
1025 //src->unique_setup = FALSE;
1027 case GST_STATE_CHANGE_READY_TO_PAUSED:
1028 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1035 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1036 if (ret == GST_STATE_CHANGE_FAILURE)
1039 switch (transition) {
1040 case GST_STATE_CHANGE_READY_TO_NULL:
1041 GST_INFO_OBJECT( src, "[%s] READY to NULL called!\n", __FUNCTION__ );
1043 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1044 GST_INFO_OBJECT( src, "[%s] PLAYING to PAUSED called!\n", __FUNCTION__ );
1045 case GST_STATE_CHANGE_PAUSED_TO_READY:
1046 GST_INFO_OBJECT( src, "[%s] PAUSED to READY called!\n", __FUNCTION__ );
1047 if ( src->live_tv && src->update_prog_chain ) {
1048 if ( !gmyth_recorder_send_frontend_ready_command( src->spawn_livetv->recorder ) )
1049 GST_WARNING_OBJECT( src, "[%s] Couldn't send the FRONTEND_READY message to the backend!", __FUNCTION__ );
1051 GST_DEBUG_OBJECT( src, "[%s] Message FRONTEND_READY was sent to the backend!", __FUNCTION__ );
1055 gst_pad_push_event ( GST_BASE_SRC_PAD (GST_BASE_SRC (src)),
1056 gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0, -1, 0 ) );
1058 src->read_offset = 0;
1059 src->bytes_read = 0;
1060 src->unique_setup = FALSE;
1061 gst_mythtv_src_next_program_chain( src );
1074 gst_mythtv_src_set_property (GObject * object, guint prop_id,
1075 const GValue * value, GParamSpec * pspec)
1077 GstMythtvSrc *mythtvsrc = GST_MYTHTV_SRC (object);
1079 GST_OBJECT_LOCK (mythtvsrc);
1084 if (!g_value_get_string (value)) {
1085 GST_WARNING ("location property cannot be NULL");
1089 if (mythtvsrc->uri_name != NULL) {
1090 g_free (mythtvsrc->uri_name);
1091 mythtvsrc->uri_name = NULL;
1093 mythtvsrc->uri_name = g_value_dup_string (value);
1097 #ifndef GST_DISABLE_GST_DEBUG
1098 case PROP_GMYTHTV_DBG:
1100 mythtvsrc->mythtv_msgs_dbg = g_value_get_boolean (value);
1104 case PROP_GMYTHTV_VERSION:
1106 mythtvsrc->mythtv_version = g_value_get_int (value);
1109 case PROP_GMYTHTV_LIVEID:
1111 mythtvsrc->live_tv_id = g_value_get_int (value);
1114 case PROP_GMYTHTV_LIVE:
1116 mythtvsrc->live_tv = g_value_get_boolean (value);
1119 case PROP_GMYTHTV_ENABLE_TIMING_POSITION:
1121 mythtvsrc->enable_timing_position = g_value_get_boolean (value);
1124 case PROP_GMYTHTV_LIVE_CHAINID:
1126 if (!g_value_get_string (value)) {
1127 GST_WARNING ("MythTV Live chainid property cannot be NULL");
1131 if (mythtvsrc->live_chain_id != NULL) {
1132 g_free (mythtvsrc->live_chain_id);
1133 mythtvsrc->live_chain_id = NULL;
1135 mythtvsrc->live_chain_id = g_value_dup_string (value);
1138 case PROP_GMYTHTV_CHANNEL_NUM:
1140 mythtvsrc->channel_num = g_value_get_int (value);
1144 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1147 GST_OBJECT_UNLOCK (mythtvsrc);
1153 gst_mythtv_src_get_property (GObject * object, guint prop_id,
1154 GValue * value, GParamSpec * pspec)
1156 GstMythtvSrc *mythtvsrc = GST_MYTHTV_SRC (object);
1158 GST_OBJECT_LOCK (mythtvsrc);
1163 gchar *str = g_strdup( "" );
1165 if ( mythtvsrc->uri_name == NULL ) {
1166 g_free (mythtvsrc->uri_name);
1167 mythtvsrc->uri_name = NULL;
1169 str = g_strdup( mythtvsrc->uri_name );
1171 g_value_set_string ( value, str );
1174 #ifndef GST_DISABLE_GST_DEBUG
1175 case PROP_GMYTHTV_DBG:
1176 g_value_set_boolean ( value, mythtvsrc->mythtv_msgs_dbg );
1179 case PROP_GMYTHTV_VERSION:
1181 g_value_set_int ( value, mythtvsrc->mythtv_version );
1184 case PROP_GMYTHTV_LIVEID:
1186 g_value_set_int ( value, mythtvsrc->live_tv_id );
1189 case PROP_GMYTHTV_LIVE:
1190 g_value_set_boolean ( value, mythtvsrc->live_tv );
1192 case PROP_GMYTHTV_ENABLE_TIMING_POSITION:
1193 g_value_set_boolean ( value, mythtvsrc->enable_timing_position );
1195 case PROP_GMYTHTV_LIVE_CHAINID:
1197 gchar *str = g_strdup( "" );
1199 if ( mythtvsrc->live_chain_id == NULL ) {
1200 g_free (mythtvsrc->live_chain_id);
1201 mythtvsrc->live_chain_id = NULL;
1203 str = g_strdup( mythtvsrc->live_chain_id );
1205 g_value_set_string ( value, str );
1208 case PROP_GMYTHTV_CHANNEL_NUM:
1210 g_value_set_int ( value, mythtvsrc->channel_num );
1214 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1217 GST_OBJECT_UNLOCK (mythtvsrc);
1220 /* entry point to initialize the plug-in
1221 * initialize the plug-in itself
1222 * register the element factories and pad templates
1223 * register the features
1226 plugin_init (GstPlugin * plugin)
1228 return gst_element_register (plugin, "mythtvsrc", GST_RANK_NONE,
1229 GST_TYPE_MYTHTV_SRC);
1232 /* this is the structure that gst-register looks for
1233 * so keep the name plugin_desc, or you cannot get your plug-in registered */
1234 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1238 plugin_init, VERSION, "LGPL", "GStreamer", "http://gstreamer.net/")
1241 /*** GSTURIHANDLER INTERFACE *************************************************/
1243 gst_mythtv_src_uri_get_type (void)
1249 gst_mythtv_src_uri_get_protocols (void)
1251 static gchar *protocols[] = { "myth", "myths", NULL };
1256 static const gchar *
1257 gst_mythtv_src_uri_get_uri (GstURIHandler * handler)
1259 GstMythtvSrc *src = GST_MYTHTV_SRC (handler);
1261 return src->uri_name;
1265 gst_mythtv_src_uri_set_uri (GstURIHandler * handler, const gchar * uri)
1267 GstMythtvSrc *src = GST_MYTHTV_SRC (handler);
1271 protocol = gst_uri_get_protocol (uri);
1272 if ((strcmp (protocol, "myth") != 0) && (strcmp (protocol, "myths") != 0)) {
1277 g_object_set (src, "location", uri, NULL);
1283 gst_mythtv_src_uri_handler_init (gpointer g_iface, gpointer iface_data)
1285 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
1287 iface->get_type = gst_mythtv_src_uri_get_type;
1288 iface->get_protocols = gst_mythtv_src_uri_get_protocols;
1289 iface->get_uri = gst_mythtv_src_uri_get_uri;
1290 iface->set_uri = gst_mythtv_src_uri_set_uri;
1294 size_header_handler (void *userdata, const char *value)
1296 GstMythtvSrc *src = GST_MYTHTV_SRC (userdata);
1298 //src->content_size = g_ascii_strtoull (value, NULL, 10);
1300 GST_DEBUG_OBJECT (src, "content size = %lld bytes", src->content_size);