[svn r90] Fixes to the read on "FileTransfer".
1 /* vim: set sw=2: -*- Mode: C; tab-width: 2; indent-tabs-mode: t; c-basic-offset: 2; c-indent-level: 2 -*- */
2 /* GStreamer MythTV Plug-in
3 * Copyright (C) <2006> Rosfran Borges <rosfran.borges@indt.org.br>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library Lesser General
7 * Public License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more
20 #include "gstmythtvsrc.h"
21 #include <gmyth/gmyth_file_transfer.h>
22 #include <gmyth/gmyth_livetv.h>
24 #include <gmyth/gmyth_socket.h>
25 #include <gmyth/gmyth_tvchain.h>
30 GST_DEBUG_CATEGORY_STATIC (mythtvsrc_debug);
31 #define GST_CAT_DEFAULT mythtvsrc_debug
33 #define GST_GMYTHTV_ID_NUM 1
35 #define GST_GMYTHTV_CHANNEL_NUM 1000
37 #define GMYTHTV_VERSION_DEFAULT 30
39 #define GMYTHTV_TRANSFER_MAX_WAITS 100
41 #define GMYTHTV_TRANSFER_MAX_BUFFER 128*1024
45 #define MAX_READ_SIZE 2*1024
48 #define GST_FLOW_ERROR_NO_DATA -101
50 /* stablish a maximum iteration value to the IS_RECORDING message */
51 static guint wait_to_transfer = 0;
53 static const GstElementDetails gst_mythtv_src_details =
54 GST_ELEMENT_DETAILS ( "MythTV client source",
56 "Control and receive data as a client over the network via raw socket connections using the MythTV protocol",
57 "Rosfran Borges <rosfran.borges@indt.org.br>" );
59 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ( "src",
62 GST_STATIC_CAPS ("video/x-nuv") );
69 #ifndef GST_DISABLE_GST_DEBUG
75 PROP_GMYTHTV_LIVE_CHAINID,
76 PROP_GMYTHTV_ENABLE_TIMING_POSITION,
77 PROP_GMYTHTV_CHANNEL_NUM
80 static void gst_mythtv_src_finalize (GObject * gobject);
83 static GstFlowReturn gst_mythtv_src_create (GstBaseSrc * psrc, guint64 offset,
84 guint size, GstBuffer ** outbuf);
87 //static GstFlowReturn gst_mythtv_src_chain ( GstPad* pad, GstBuffer* outbuf );
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 );
95 static gboolean gst_mythtv_src_do_seek( GstBaseSrc *base, GstSegment *segment );
97 static gboolean gst_mythtv_src_next_program_chain ( GstMythtvSrc *src );
99 static GstStateChangeReturn
100 gst_mythtv_src_change_state (GstElement * element, GstStateChange transition);
102 static void gst_mythtv_src_set_property (GObject * object, guint prop_id,
103 const GValue * value, GParamSpec * pspec);
104 static void gst_mythtv_src_get_property (GObject * object, guint prop_id,
105 GValue * value, GParamSpec * pspec);
107 static void gst_mythtv_src_uri_handler_init (gpointer g_iface, gpointer iface_data);
109 static gboolean gst_mythtv_src_handle_query (GstPad * pad, GstQuery * query);
111 //static gboolean gst_mythtv_src_handle_event (GstPad * pad, GstEvent * event);
112 //static gboolean gst_mythtv_src_query ( GstPad * pad, GstQuery * query );
114 static gint do_read_request_response (GstMythtvSrc *src, guint64 offset,
115 guint size, GstBuffer **outbuf);
118 _urihandler_init (GType type)
120 static const GInterfaceInfo urihandler_info = {
121 gst_mythtv_src_uri_handler_init,
126 g_type_add_interface_static (type, GST_TYPE_URI_HANDLER, &urihandler_info);
128 GST_DEBUG_CATEGORY_INIT (mythtvsrc_debug, "mythtvsrc", 0,
132 //GST_BOILERPLATE_FULL (GstMythtvSrc, gst_mythtv_src, GstBaseSrc,
133 // GST_TYPE_BASE_SRC, _urihandler_init)
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->do_seek = gst_mythtv_src_do_seek;
232 gstpushsrc_class->create = gst_mythtv_src_create;
234 GST_DEBUG_CATEGORY_INIT (mythtvsrc_debug, "mythtvsrc", 0,
235 "MythTV Client Source");
239 gst_mythtv_src_init (GstMythtvSrc * this, GstMythtvSrcClass * g_class)
241 this->file_transfer = NULL;
243 this->unique_setup = FALSE;
245 this->mythtv_version = GMYTHTV_VERSION_DEFAULT;
247 this->state = GST_MYTHTV_SRC_FILE_TRANSFER;
249 this->bytes_read = 0;
251 this->prev_content_size = 0;
253 this->content_size = 0;
254 this->read_offset = 0;
256 this->content_size_last = 0;
258 this->live_tv = FALSE;
260 this->enable_timing_position = FALSE;
261 this->update_prog_chain = FALSE;
263 this->user_agent = g_strdup ("mythtvsrc");
264 this->mythtv_caps = NULL;
265 this->update_prog_chain = FALSE;
267 this->channel_num = 0;
271 this->adapter = NULL;
273 //this->th_read_ahead = NULL;
275 this->th_mutex = NULL;
277 this->srcpad = gst_pad_new_from_static_template (&srctemplate, "src");
278 gst_element_add_pad (GST_ELEMENT (this), this->srcpad);
280 gst_base_src_set_format( GST_BASE_SRC( this ), GST_FORMAT_BYTES );
282 //gst_base_src_set_live ( GST_BASE_SRC( this ), TRUE );
284 // gst_pad_set_event_function ( GST_BASE_SRC_PAD(GST_BASE_SRC(this)),
285 // gst_mythtv_src_handle_event );
286 gst_pad_set_query_function ( GST_BASE_SRC_PAD(GST_BASE_SRC(this)),
287 gst_mythtv_src_handle_query );
292 gst_mythtv_src_finalize (GObject * gobject)
294 GstMythtvSrc *this = GST_MYTHTV_SRC (gobject);
296 if ( this->th_mutex != NULL ) {
297 g_static_rec_mutex_free( this->th_mutex );
298 this->th_mutex = NULL;
301 if ( this->th_read_ahead != NULL ) {
302 gst_task_stop( this->th_read_ahead );
303 this->th_read_ahead = NULL;
306 if (this->mythtv_caps) {
307 gst_caps_unref (this->mythtv_caps);
308 this->mythtv_caps = NULL;
311 if (this->file_transfer) {
312 g_object_unref (this->file_transfer);
313 this->file_transfer = NULL;
316 if (this->spawn_livetv) {
317 g_object_unref (this->spawn_livetv);
318 this->spawn_livetv = NULL;
321 if (this->uri_name) {
322 g_free (this->uri_name);
325 if (this->user_agent) {
326 g_free (this->user_agent);
329 G_OBJECT_CLASS (parent_class)->finalize (gobject);
333 do_read_request_response (GstMythtvSrc * src, guint64 offset, guint size, GstBuffer **outbuf)
336 guint sizetoread = size;
338 g_print( "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. */
343 gint8 *data_ptr = g_malloc0( size );
345 GST_OBJECT_LOCK(src);
347 while ( sizetoread > 0 ) {
349 len = gmyth_file_transfer_read( src->file_transfer,
350 data_ptr + read, sizetoread, TRUE );
360 if ( src->live_tv == FALSE )
366 if ( len == GMYTHTV_FILE_TRANSFER_READ_ERROR ) { /* -314 */
367 src->update_prog_chain = TRUE;
370 if ( abs( src->content_size - src->bytes_read ) < GMYTHTV_TRANSFER_MAX_BUFFER ) {
371 src->update_prog_chain = TRUE;
372 if ( src->enable_timing_position ) {
375 size_tmp = gmyth_file_transfer_get_file_position( src->file_transfer );
376 if ( size_tmp > ( src->content_size + GMYTHTV_TRANSFER_MAX_BUFFER ) )
377 src->content_size = size_tmp;
378 else if ( size_tmp > 0 )
380 g_print( "\t[%s]\tGET_POSITION: file_position = %lld\n",
381 __FUNCTION__, size_tmp );
383 gint64 new_offset = gmyth_file_transfer_get_file_position( src->file_transfer );
384 if ( new_offset > 0 && src->content_size <= new_offset ) {
385 src->content_size = new_offset;
387 src->update_prog_chain = TRUE;
389 src->prev_content_size = src->content_size;
397 if ( read == sizetoread )
402 src->read_offset += read;
403 src->bytes_read += read;
405 g_print( "[%s]\tBYTES READ (actual) = %d, BYTES READ (cumulative) = %llu, "\
406 "OFFSET = %llu, CONTENT SIZE = %llu.\n", __FUNCTION__, read, src->bytes_read,
407 src->read_offset, src->content_size );
409 GST_BUFFER_SIZE (*outbuf) = read; //GST_BUFFER_SIZE (buffer) = read;
410 GST_BUFFER_MALLOCDATA( *outbuf ) = g_malloc0( GST_BUFFER_SIZE (*outbuf) );
411 GST_BUFFER_DATA( *outbuf ) = GST_BUFFER_MALLOCDATA( *outbuf );
412 g_memmove( GST_BUFFER_DATA( *outbuf ), data_ptr, read );
413 GST_BUFFER_OFFSET (*outbuf) = offset; //GST_BUFFER_OFFSET (buffer) = offset;
414 GST_BUFFER_OFFSET_END (*outbuf) = offset + read;//GST_BUFFER_OFFSET_END (buffer) = offset + read;
416 g_print( "Got buffer: [%s]\t\tBUFFER --->SIZE = %d, OFFSET = %llu, "\
417 "OFFSET_END = %llu.\n\n", __FUNCTION__, GST_BUFFER_SIZE (*outbuf),
418 GST_BUFFER_OFFSET (*outbuf), GST_BUFFER_OFFSET_END (*outbuf) );
420 } else if ( !src->live_tv )
429 GST_OBJECT_UNLOCK(src);
436 gst_mythtv_src_create ( GstBaseSrc * psrc, guint64 offset, guint size, GstBuffer **outbuf)
439 GstFlowReturn ret = GST_FLOW_OK;
441 gint adapter_size = -1;
443 guint max_adapter_rep = 40;
445 src = GST_MYTHTV_SRC (psrc);
447 /* The caller should know the number of bytes and not read beyond EOS. */
448 if (G_UNLIKELY (src->eos))
450 if ( G_UNLIKELY (src->update_prog_chain) )
451 goto change_progchain;
453 g_static_rec_mutex_lock( src->th_mutex );
455 while ( ( ( adapter_size = gst_adapter_available_fast( src->adapter ) ) < size ) &&
456 --max_adapter_rep > 0 )
458 g_print ( "[%s] %d - Waiting for read_ahead task...\n", __FUNCTION__, max_adapter_rep );
459 GST_TASK_WAIT( src->th_read_ahead );
462 g_static_rec_mutex_unlock( src->th_mutex );
464 gint64 new_offset = -1;
465 /* just get from the adapter, no network effort... */
466 if ( offset > src->adapter_offset && size <= adapter_size )
469 GstBuffer *buf = gst_adapter_take_buffer( src->adapter, size );
470 *outbuf = gst_buffer_create_sub( buf, offset, size );
471 src->read_offset = new_offset = offset;
474 gst_adapter_flush( src->adapter, size );
477 /* no data on adapter... do all these mythtv network calls! */
479 /* verify if it needs to seek */
480 if ( src->read_offset != offset )
483 new_offset = gmyth_file_transfer_seek( src->file_transfer, offset, SEEK_SET );
485 g_print( "[%s] SRC Offset = %lld, NEW actual backend SEEK Offset = %lld.\n",
486 __FUNCTION__, src->read_offset, new_offset );
487 if ( G_UNLIKELY (new_offset < 0 ) )
490 goto change_progchain;
497 src->read_offset = offset;
499 /* Create the buffer. */
500 ret = gst_pad_alloc_buffer ( GST_BASE_SRC_PAD (GST_BASE_SRC (psrc)),
501 src->read_offset, size,
502 GST_PAD_CAPS ( GST_BASE_SRC_PAD (GST_BASE_SRC (psrc)) ), outbuf);
504 if (G_UNLIKELY (ret != GST_FLOW_OK)) {
506 goto change_progchain;
511 read = do_read_request_response ( src, src->read_offset, size, outbuf );
515 if (G_UNLIKELY (src->update_prog_chain) )
516 goto change_progchain;
518 if (G_UNLIKELY (read <= 0) || *outbuf == NULL) {
520 goto change_progchain;
526 src->read_offset += read;
527 src->bytes_read += read;
530 g_print( "[%s]\tBYTES READ (actual) = %d, BYTES READ (cumulative) = %llu, "\
531 "OFFSET = %llu, CONTENT SIZE = %llu.\n", __FUNCTION__, read, src->bytes_read,
532 src->read_offset, src->content_size );
534 GST_BUFFER_SIZE (*outbuf) = read; //GST_BUFFER_SIZE (buffer) = read;
535 //GST_BUFFER_MALLOCDATA( *outbuf ) = g_malloc0( GST_BUFFER_SIZE (*outbuf) );
536 //GST_BUFFER_DATA( *outbuf ) = GST_BUFFER_MALLOCDATA( *outbuf );
537 //g_memmove( GST_BUFFER_DATA( *outbuf ), data_ptr, read );
538 GST_BUFFER_OFFSET (*outbuf) = offset; //GST_BUFFER_OFFSET (buffer) = offset;
539 GST_BUFFER_OFFSET_END (*outbuf) = offset + read;//GST_BUFFER_OFFSET_END (buffer) = offset + read;
541 g_print( "Got buffer: [%s]\t\tBUFFER --->SIZE = %d, OFFSET = %llu, "\
542 "OFFSET_END = %llu.\n\n", __FUNCTION__, GST_BUFFER_SIZE (*outbuf),
543 GST_BUFFER_OFFSET (*outbuf), GST_BUFFER_OFFSET_END (*outbuf) );
550 const gchar *reason = gst_flow_get_name (ret);
552 GST_DEBUG_OBJECT (src, "DONE task, reason %s", reason);
557 const gchar *reason = gst_flow_get_name (ret);
559 GST_DEBUG_OBJECT (src, "pausing task, reason %s", reason);
560 return GST_FLOW_UNEXPECTED;
565 GST_ELEMENT_ERROR (src, RESOURCE, READ,
566 (NULL), ("Could not read any bytes (%i, %s)", read,
568 return GST_FLOW_ERROR;
572 GST_ELEMENT_ERROR (src, RESOURCE, READ,
573 (NULL), ("Seek failed, go to the next program info... (%i, %s)", read,
576 gst_pad_push_event ( GST_BASE_SRC_PAD (GST_BASE_SRC (src)),
577 gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0, -1, 0 ) );
578 // go to the next program chain
579 src->unique_setup = FALSE;
580 src->update_prog_chain = TRUE;
582 gst_mythtv_src_next_program_chain( src );
584 return GST_FLOW_ERROR_NO_DATA;
591 gst_mythtv_src_create ( GstPushSrc* psrc, GstBuffer** outbuf )
594 GstFlowReturn ret = GST_FLOW_OK;
597 src = GST_MYTHTV_SRC ( psrc );
599 /* The caller should know the number of bytes and not read beyond EOS. */
600 if (G_UNLIKELY (src->eos))
602 if ( G_UNLIKELY (src->update_prog_chain) )
603 goto change_progchain;
605 /* just get from the adapter, no network effort... */
606 /* Create the buffer. */
607 ret = gst_pad_alloc_buffer ( GST_BASE_SRC_PAD (GST_BASE_SRC (psrc)),
608 src->read_offset /*GST_BUFFER_OFFSET_NONE*/, MAX_READ_SIZE,
609 GST_PAD_CAPS (GST_BASE_SRC_PAD (GST_BASE_SRC (psrc))), outbuf );
611 if (G_UNLIKELY (ret != GST_FLOW_OK)) {
613 goto change_progchain;
618 read = do_read_request_response ( src, src->read_offset, MAX_READ_SIZE, outbuf );
620 if ( G_UNLIKELY (src->update_prog_chain) )
621 goto change_progchain;
623 if (G_UNLIKELY (read <= 0) || *outbuf == NULL) {
625 goto change_progchain;
632 const gchar *reason = gst_flow_get_name (ret);
634 GST_DEBUG_OBJECT (src, "DONE task, reason %s", reason);
639 const gchar *reason = gst_flow_get_name (ret);
641 GST_DEBUG_OBJECT (src, "pausing task, reason %s", reason);
642 return GST_FLOW_UNEXPECTED;
647 GST_ELEMENT_ERROR (src, RESOURCE, READ,
648 (NULL), ("Could not read any bytes (%i, %s)", read,
650 return GST_FLOW_ERROR;
654 GST_ELEMENT_ERROR (src, RESOURCE, READ,
655 (NULL), ("Seek failed, go to the next program info... (%i, %s)", read,
658 gst_pad_push_event ( GST_BASE_SRC_PAD (GST_BASE_SRC (psrc)),
659 gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0, -1, 0 ) );
660 // go to the next program chain
661 src->unique_setup = FALSE;
662 src->update_prog_chain = TRUE;
664 gst_mythtv_src_next_program_chain( src );
666 return GST_FLOW_ERROR_NO_DATA;
672 gst_mythtv_src_get_position ( GstMythtvSrc* src )
677 if (src->live_tv == TRUE && ( abs( src->content_size - src->bytes_read ) <
678 GMYTHTV_TRANSFER_MAX_BUFFER ) ) {
682 size_tmp = gmyth_file_transfer_get_file_position( src->file_transfer );
683 if ( size_tmp > ( src->content_size + GMYTHTV_TRANSFER_MAX_BUFFER ) )
684 src->content_size = size_tmp;
685 else if ( size_tmp > 0 && --max_tries > 0 )
687 g_print( "\t[%s]\tGET_POSITION: file_position = %lld\n",
688 __FUNCTION__, size_tmp );
689 /* sets the last content size amount before it can be updated */
690 src->prev_content_size = src->content_size;
693 return src->content_size;
698 gst_mythtv_src_do_seek( GstBaseSrc *base, GstSegment *segment )
700 GstMythtvSrc *src = GST_MYTHTV_SRC( base );
701 gint64 new_offset = 0;
704 g_print( "[%s]DO Seek called! (start = %lld, stop = %lld)\n", __FUNCTION__, segment->start, segment->stop );
706 /* verify if it needs to seek */
707 if ( src->read_offset != segment->start )
710 new_offset = gmyth_file_transfer_seek( src->file_transfer, segment->start, SEEK_SET );
712 g_print( "[%s] Segment offset start = %lld, SRC Offset = %lld, NEW actual backend SEEK Offset = %lld.\n",
713 __FUNCTION__, segment->start, src->read_offset, new_offset );
714 if ( G_UNLIKELY (new_offset < 0 ) )
718 goto change_progchain;
723 src->read_offset = new_offset;
732 GST_DEBUG_OBJECT (src, "EOS found on seeking!!!");
733 gst_object_unref( src );
738 GST_ELEMENT_ERROR (src, RESOURCE, READ,
739 (NULL), ("Seek failed, go to the next program info... (%i, %s)", read,
742 gst_pad_push_event ( GST_BASE_SRC_PAD (base),
743 gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0, -1, 0 ) );
744 /* go to the next program chain */
745 src->unique_setup = FALSE;
746 src->update_prog_chain = TRUE;
748 gst_mythtv_src_next_program_chain( src );
757 gst_mythtv_src_read_ahead ( void *data ) {
759 GstMythtvSrc *src = NULL;
761 GstBuffer *buffer = NULL;
767 src = GST_MYTHTV_SRC( data );
769 GST_PAD_STREAM_TRYLOCK( GST_BASE_SRC_PAD (GST_BASE_SRC (src)) );
771 /*if ( gst_adapter_available( src->adapter ) ) && ( read < MAX_READ_SIZE )) */
773 buffer = gst_buffer_new_and_alloc( size );
775 read += do_read_request_response ( src, src->adapter_offset, size, &buffer );
777 gst_adapter_push( src->adapter, buffer );
779 //gst_buffer_unref( buffer );
780 } while ( read < MAX_READ_SIZE );
782 GST_PAD_BLOCK_SIGNAL( GST_BASE_SRC_PAD (GST_BASE_SRC (src)) );
784 GST_PAD_STREAM_UNLOCK( GST_BASE_SRC_PAD (GST_BASE_SRC (src)) );
786 gst_object_unref( src );
792 /* create a socket for connecting to remote server */
794 gst_mythtv_src_start ( GstBaseSrc * bsrc )
796 GstMythtvSrc *src = GST_MYTHTV_SRC (bsrc);
798 GString *chain_id_local = NULL;
802 if ( G_UNLIKELY (src->update_prog_chain) )
803 goto change_progchain;
805 if (src->unique_setup == FALSE) {
806 src->unique_setup = TRUE;
811 //GST_OBJECT_LOCK(src);
813 if ( src->live_tv ) {
814 src->spawn_livetv = gmyth_livetv_new( );
815 if ( gmyth_livetv_setup( src->spawn_livetv ) == FALSE ) {
817 //GST_OBJECT_UNLOCK( src );
821 /* set up the uri variable */
822 src->uri_name = g_strdup( src->spawn_livetv->proginfo->pathname->str );
823 chain_id_local = gmyth_tvchain_get_id( src->spawn_livetv->tvchain );
824 if ( chain_id_local != NULL ) {
825 src->live_chain_id = g_strdup( chain_id_local->str );
826 g_print( "\t[%s]\tLocal chain ID = %s.\n", __FUNCTION__, src->live_chain_id );
828 src->live_tv_id = src->spawn_livetv->recorder->recorder_num;
829 g_print ( "[%s] LiveTV id = %d, URI path = %s.\n", __FUNCTION__, src->live_tv_id, src->uri_name );
832 src->file_transfer = gmyth_file_transfer_new( src->live_tv_id,
833 g_string_new( src->uri_name ), -1, src->mythtv_version );
835 if ( src->file_transfer == NULL ) {
836 //GST_OBJECT_UNLOCK(src);
841 /* sets the Playback monitor connection */
842 ret = gmyth_file_transfer_playback_setup( &(src->file_transfer), src->live_tv );
844 if ( src->live_tv == TRUE && ret == TRUE ) {
845 /* loop finished, set the max tries variable to zero again... */
846 wait_to_transfer = 0;
848 while ( wait_to_transfer++ < GMYTHTV_TRANSFER_MAX_WAITS &&
849 ( gmyth_file_transfer_is_recording( src->file_transfer ) == FALSE
850 /*|| ( gmyth_file_transfer_get_file_position( src->file_transfer ) < ( src->content_size + 327680 ) )*/ ) )
854 /* sets the FileTransfer instance connection (video/audio download) */
855 ret = gmyth_file_transfer_setup( &(src->file_transfer), src->live_tv );
857 if ( ret == FALSE ) {
858 //GST_OBJECT_UNLOCK(src);
859 #ifndef GST_DISABLE_GST_DEBUG
860 if ( src->mythtv_msgs_dbg )
861 g_printerr( "MythTV FileTransfer request failed when setting up socket connection!\n" );
863 goto begin_req_failed;
866 src->content_size = src->file_transfer->filesize;
868 src->do_start = FALSE;
870 if ( src->live_tv ) {
871 //src->adapter = gst_adapter_new();
872 //g_static_rec_mutex_init( src->th_mutex );
873 //src->th_read_ahead = gst_task_create( (GstTaskFunction)gst_mythtv_src_read_ahead, src );
875 //gst_pad_push_event ( GST_BASE_SRC_PAD (bsrc),
876 // gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_BYTES, 0, src->content_size, 0 ) );
878 //GST_OBJECT_UNLOCK(src);
886 if (src->spawn_livetv != NULL )
887 g_object_unref( src->spawn_livetv );
889 GST_ELEMENT_ERROR (src, LIBRARY, INIT,
890 (NULL), ("Could not initialize MythTV library (%i, %s)", ret, src->uri_name));
895 GST_ELEMENT_ERROR (src, LIBRARY, INIT,
896 (NULL), ("Could not begin request sent to MythTV server (%i, %s)", ret, src->uri_name));
901 GST_ELEMENT_ERROR (src, RESOURCE, READ,
902 (NULL), ("Seek failed, go to the next program info... (%s)",
905 gst_pad_push_event ( GST_BASE_SRC_PAD (GST_BASE_SRC (src)),
906 gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0, -1, 0 ) );
908 // go to the next program chain
909 src->unique_setup = FALSE;
910 src->update_prog_chain = TRUE;
912 gst_mythtv_src_next_program_chain( src );
918 /* create a new socket for connecting to the next program chain */
920 gst_mythtv_src_next_program_chain ( GstMythtvSrc *src )
922 GString *chain_id_local = NULL;
929 if (src->unique_setup == FALSE) {
930 src->unique_setup = TRUE;
935 GST_PAD_STREAM_LOCK( GST_BASE_SRC_PAD (GST_BASE_SRC (src)) );
937 if (src->file_transfer) {
938 g_object_unref (src->file_transfer);
939 src->file_transfer = NULL;
943 g_free (src->uri_name);
946 if ( src->live_tv ) {
947 if ( gmyth_livetv_next_program_chain( src->spawn_livetv ) == FALSE ) {
948 g_print( "\n\n[%s]\t\tFailed to go to the next program chain!!!\n\n", __FUNCTION__ );
952 /* set up the uri variable */
953 src->uri_name = g_strdup( src->spawn_livetv->proginfo->pathname->str );
954 chain_id_local = gmyth_tvchain_get_id( src->spawn_livetv->tvchain );
955 if ( chain_id_local != NULL ) {
956 src->live_chain_id = g_strdup( chain_id_local->str );
957 g_print( "\t[%s]\tLocal chain ID = %s.\n", __FUNCTION__, src->live_chain_id );
959 src->live_tv_id = src->spawn_livetv->recorder->recorder_num;
960 g_print ( "[%s] LiveTV id = %d, URI path = %s.\n", __FUNCTION__, src->live_tv_id, src->uri_name );
963 src->file_transfer = gmyth_file_transfer_new( src->live_tv_id,
964 g_string_new( src->uri_name ), -1, src->mythtv_version );
966 if ( src->file_transfer == NULL ) {
970 /* sets the Playback monitor connection */
971 ret = gmyth_file_transfer_playback_setup( &(src->file_transfer), src->live_tv );
973 if ( src->live_tv == TRUE && ret == TRUE ) {
974 /* loop finished, set the max tries variable to zero again... */
975 wait_to_transfer = 0;
979 while ( wait_to_transfer++ < GMYTHTV_TRANSFER_MAX_WAITS &&
980 ( gmyth_file_transfer_is_recording( src->file_transfer ) == FALSE ) )
984 /* sets the FileTransfer instance connection (video/audio download) */
985 ret = gmyth_file_transfer_setup( &(src->file_transfer), src->live_tv );
987 if ( ret == FALSE ) {
988 #ifndef GST_DISABLE_GST_DEBUG
989 if ( src->mythtv_msgs_dbg )
990 g_printerr( "MythTV FileTransfer request failed when setting up socket connection!\n" );
992 goto begin_req_failed;
994 src->content_size_last = src->content_size;
997 if ( src->content_size < src->file_transfer->filesize ) {
998 src->content_size = src->file_transfer->filesize;
1000 //gint64 pos = gst_mythtv_src_get_position(src);
1001 //if ( pos > src->file_transfer->filesize )
1002 // src->content_size = pos;
1007 src->content_size = src->file_transfer->filesize;
1008 if ( src->live_tv ) {
1009 wait_to_transfer = 0;
1010 while ( wait_to_transfer++ < GMYTHTV_TRANSFER_MAX_WAITS && src->content_size < GMYTHTV_TRANSFER_MAX_BUFFER )
1011 src->content_size = gst_mythtv_src_get_position( src );
1014 src->read_offset = 0;
1017 src->update_prog_chain = FALSE;
1019 GST_PAD_STREAM_UNLOCK( GST_BASE_SRC_PAD (GST_BASE_SRC (src)) );
1026 if (src->spawn_livetv != NULL )
1027 g_object_unref( src->spawn_livetv );
1029 GST_ELEMENT_ERROR (src, LIBRARY, INIT,
1030 (NULL), ("Could not initialize MythTV library (%i, %s)", ret, src->uri_name));
1035 GST_ELEMENT_ERROR (src, LIBRARY, INIT,
1036 (NULL), ("Could not begin request sent to MythTV server (%i, %s)", ret, src->uri_name));
1043 gst_mythtv_src_get_size (GstBaseSrc * bsrc, guint64 * size)
1045 GstMythtvSrc *src = GST_MYTHTV_SRC (bsrc);
1046 gboolean ret = TRUE;
1047 g_print( "[%s] Differs from previous content size: %d (max.: %d)\n", __FUNCTION__,
1048 abs( src->content_size - src->prev_content_size ), GMYTHTV_TRANSFER_MAX_BUFFER );
1050 if (src->content_size == -1) {
1052 } else if ( src->live_tv && ( abs( src->content_size - src->bytes_read ) <
1053 GMYTHTV_TRANSFER_MAX_BUFFER ) ) {
1054 //g_static_mutex_lock( &update_size_mutex );
1055 //GST_OBJECT_LOCK(src);
1057 gint64 new_offset = gmyth_file_transfer_get_file_position( src->file_transfer );
1058 if ( new_offset > 0 && new_offset > src->content_size ) {
1059 src->content_size = new_offset;
1060 } else if ( new_offset < src->content_size ) {
1061 src->update_prog_chain = TRUE;
1064 if ( src->enable_timing_position ) {
1065 gint64 size_tmp = 0;
1066 if (src->live_tv == TRUE) {
1069 size_tmp = gmyth_file_transfer_get_file_position( src->file_transfer );
1070 if ( size_tmp > ( src->content_size + GMYTHTV_TRANSFER_MAX_BUFFER ) )
1071 src->content_size = size_tmp;
1072 else if ( size_tmp > 0 )
1074 g_print( "\t[%s]\tGET_POSITION: file_position = %lld\n",
1075 __FUNCTION__, size_tmp );
1079 src->prev_content_size = src->content_size;
1081 //GST_OBJECT_UNLOCK(src);
1082 //g_static_mutex_unlock( &update_size_mutex );
1085 *size = src->content_size;
1086 g_print( "[%s] Content size = %lld\n", __FUNCTION__, src->content_size );
1092 /* close the socket and associated resources
1093 * used both to recover from errors and go to NULL state */
1095 gst_mythtv_src_stop (GstBaseSrc * bsrc)
1099 src = GST_MYTHTV_SRC (bsrc);
1101 if (src->uri_name) {
1102 g_free (src->uri_name);
1103 src->uri_name = NULL;
1106 if (src->mythtv_caps) {
1107 gst_caps_unref (src->mythtv_caps);
1108 src->mythtv_caps = NULL;
1118 gst_mythtv_src_handle_event (GstPad * pad, GstEvent * event)
1120 GstMythtvSrc *src = GST_MYTHTV_SRC (GST_PAD_PARENT (pad));
1121 gint64 cont_size = 0;
1123 switch (GST_EVENT_TYPE (event)) {
1125 case GST_EVENT_FLUSH_START:
1127 g_print( "\n\n\n[%s]\t\tGot FLUSH_START event!!!\n\n\n", __FUNCTION__ );
1128 cont_size = gst_mythtv_src_get_position (src);
1129 if ( !src->live_tv ) {
1130 if ( cont_size > src->content_size ) {
1131 src->content_size = cont_size;
1135 gst_element_set_state ( GST_ELEMENT (src), GST_STATE_NULL );
1136 gst_element_set_locked_state ( GST_ELEMENT (src), FALSE );
1139 if ( cont_size <= 0 ) {
1140 src->update_prog_chain = TRUE;
1142 src->unique_setup = FALSE;
1143 src->do_start = TRUE;
1147 case GST_EVENT_FLUSH_STOP:
1148 src->do_start = TRUE;
1150 gst_element_set_state (GST_ELEMENT(src), GST_STATE_NULL);
1151 //gst_element_set_locked_state (GST_ELEMENT(src), TRUE);
1155 g_print( "[%s] Got EOS event!!!\n", __FUNCTION__ );
1157 if ( src->live_tv ) {
1158 cont_size = gst_mythtv_src_get_position (src);
1159 if ( cont_size > src->content_size ) {
1160 src->content_size = cont_size;
1164 gst_element_set_state ( GST_ELEMENT (src), GST_STATE_NULL );
1165 gst_element_set_locked_state ( GST_ELEMENT (src), FALSE );
1171 case GST_EVENT_NEWSEGMENT:
1172 g_print( "[%s] Got NEWSEGMENT!!!\n", __FUNCTION__ );
1175 case GST_EVENT_SEEK:
1177 g_print( "[%s] Got EVENT_SEEK!!!\n", __FUNCTION__ );
1179 //gboolean update = TRUE;
1181 GstSeekType cur_type, stop_type;
1183 gint64 cur = 0, stop = 0;
1184 gst_event_parse_seek ( event, &rate, &format,
1185 &flags, &cur_type, &cur,
1186 &stop_type, &stop );
1188 g_print( "[%s] Got EVENT_SEEK.\n", __FUNCTION__ );
1189 if ( !( flags & GST_SEEK_FLAG_FLUSH ) ) {
1190 g_print( "[%s] Could get the FLAG_FLUSH message.\n", __FUNCTION__ );
1192 //gboolean ret = gst_event_parse_new_segment ( event,
1193 // &update, &rate, &format, &start, &stop,
1195 //GstFlowReturn flow_ret = gst_mythtv_src_create (GST_BASE_SRC( GST_PAD_PARENT( psrc ) ),
1196 // cur, stop - cur + 1, GstBuffer)
1200 return gst_pad_event_default (pad, event);
1203 return gst_pad_event_default (pad, event);
1208 gst_mythtv_src_is_seekable( GstBaseSrc *push_src )
1214 gst_mythtv_src_handle_query (GstPad * pad, GstQuery * query)
1216 gboolean res = FALSE;
1217 GstMythtvSrc *myth = GST_MYTHTV_SRC (gst_pad_get_parent (pad));
1219 switch (GST_QUERY_TYPE (query)) {
1220 case GST_QUERY_POSITION:
1221 gst_query_set_position (query, GST_FORMAT_BYTES,
1222 myth->read_offset );
1224 GST_DEBUG_OBJECT (myth, "POS %d", myth->read_offset);
1226 case GST_QUERY_DURATION:
1228 if (myth->duration != 0) {
1232 fps = nuv->h->i_fpsn / nuv->h->i_fpsd;
1233 total = gst_util_uint64_scale_int (GST_SECOND, nuv->h->i_video_blocks, fps);
1235 //gst_query_set_duration (query, GST_FORMAT_TIME, myth->content_size);
1236 GST_DEBUG_OBJECT (myth, "DURATION %d", myth->content_size);
1244 gst_object_unref (myth);
1249 static GstStateChangeReturn
1250 gst_mythtv_src_change_state (GstElement * element, GstStateChange transition)
1252 GstStateChangeReturn ret = GST_STATE_CHANGE_FAILURE;//GST_STATE_CHANGE_NO_PREROLL;
1253 GstMythtvSrc *src = GST_MYTHTV_SRC (element);
1255 switch (transition) {
1256 case GST_STATE_CHANGE_NULL_TO_READY:
1257 //src->do_start = TRUE;
1258 //src->unique_setup = FALSE;
1260 case GST_STATE_CHANGE_READY_TO_PAUSED:
1261 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1268 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1269 if (ret == GST_STATE_CHANGE_FAILURE)
1272 switch (transition) {
1273 case GST_STATE_CHANGE_READY_TO_NULL:
1274 g_print( "[%s] READY to NULL called!\n", __FUNCTION__ );
1276 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1277 g_print( "[%s] PLAYING to PAUSED called!\n", __FUNCTION__ );
1278 case GST_STATE_CHANGE_PAUSED_TO_READY:
1279 g_print( "[%s] PAUSED to READY called!\n", __FUNCTION__ );
1280 if ( src->live_tv && src->update_prog_chain ) {
1282 gst_pad_push_event ( GST_BASE_SRC_PAD (GST_BASE_SRC (src)),
1283 gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0, -1, 0 ) );
1285 src->read_offset = 0;
1286 src->bytes_read = 0;
1287 src->unique_setup = FALSE;
1288 gst_mythtv_src_next_program_chain( src );
1299 gst_mythtv_src_set_property (GObject * object, guint prop_id,
1300 const GValue * value, GParamSpec * pspec)
1302 GstMythtvSrc *mythtvsrc = GST_MYTHTV_SRC (object);
1304 GST_OBJECT_LOCK (mythtvsrc);
1309 if (!g_value_get_string (value)) {
1310 GST_WARNING ("location property cannot be NULL");
1314 if (mythtvsrc->uri_name != NULL) {
1315 g_free (mythtvsrc->uri_name);
1316 mythtvsrc->uri_name = NULL;
1318 mythtvsrc->uri_name = g_value_dup_string (value);
1322 #ifndef GST_DISABLE_GST_DEBUG
1323 case PROP_GMYTHTV_DBG:
1325 mythtvsrc->mythtv_msgs_dbg = g_value_get_boolean (value);
1329 case PROP_GMYTHTV_VERSION:
1331 mythtvsrc->mythtv_version = g_value_get_int (value);
1334 case PROP_GMYTHTV_LIVEID:
1336 mythtvsrc->live_tv_id = g_value_get_int (value);
1339 case PROP_GMYTHTV_LIVE:
1341 mythtvsrc->live_tv = g_value_get_boolean (value);
1344 case PROP_GMYTHTV_ENABLE_TIMING_POSITION:
1346 mythtvsrc->enable_timing_position = g_value_get_boolean (value);
1349 case PROP_GMYTHTV_LIVE_CHAINID:
1351 if (!g_value_get_string (value)) {
1352 GST_WARNING ("MythTV Live chainid property cannot be NULL");
1356 if (mythtvsrc->live_chain_id != NULL) {
1357 g_free (mythtvsrc->live_chain_id);
1358 mythtvsrc->live_chain_id = NULL;
1360 mythtvsrc->live_chain_id = g_value_dup_string (value);
1363 case PROP_GMYTHTV_CHANNEL_NUM:
1365 mythtvsrc->channel_num = g_value_get_int (value);
1369 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1372 GST_OBJECT_UNLOCK (mythtvsrc);
1378 gst_mythtv_src_get_property (GObject * object, guint prop_id,
1379 GValue * value, GParamSpec * pspec)
1381 GstMythtvSrc *mythtvsrc = GST_MYTHTV_SRC (object);
1383 GST_OBJECT_LOCK (mythtvsrc);
1388 gchar *str = g_strdup( "" );
1390 if ( mythtvsrc->uri_name == NULL ) {
1391 g_free (mythtvsrc->uri_name);
1392 mythtvsrc->uri_name = NULL;
1394 str = g_strdup( mythtvsrc->uri_name );
1396 g_value_set_string ( value, str );
1399 #ifndef GST_DISABLE_GST_DEBUG
1400 case PROP_GMYTHTV_DBG:
1401 g_value_set_boolean ( value, mythtvsrc->mythtv_msgs_dbg );
1404 case PROP_GMYTHTV_VERSION:
1406 g_value_set_int ( value, mythtvsrc->mythtv_version );
1409 case PROP_GMYTHTV_LIVEID:
1411 g_value_set_int ( value, mythtvsrc->live_tv_id );
1414 case PROP_GMYTHTV_LIVE:
1415 g_value_set_boolean ( value, mythtvsrc->live_tv );
1417 case PROP_GMYTHTV_ENABLE_TIMING_POSITION:
1418 g_value_set_boolean ( value, mythtvsrc->enable_timing_position );
1420 case PROP_GMYTHTV_LIVE_CHAINID:
1422 gchar *str = g_strdup( "" );
1424 if ( mythtvsrc->live_chain_id == NULL ) {
1425 g_free (mythtvsrc->live_chain_id);
1426 mythtvsrc->live_chain_id = NULL;
1428 str = g_strdup( mythtvsrc->live_chain_id );
1430 g_value_set_string ( value, str );
1433 case PROP_GMYTHTV_CHANNEL_NUM:
1435 g_value_set_int ( value, mythtvsrc->channel_num );
1439 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1442 GST_OBJECT_UNLOCK (mythtvsrc);
1445 /* entry point to initialize the plug-in
1446 * initialize the plug-in itself
1447 * register the element factories and pad templates
1448 * register the features
1451 plugin_init (GstPlugin * plugin)
1453 return gst_element_register (plugin, "mythtvsrc", GST_RANK_NONE,
1454 GST_TYPE_MYTHTV_SRC);
1457 /* this is the structure that gst-register looks for
1458 * so keep the name plugin_desc, or you cannot get your plug-in registered */
1459 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1463 plugin_init, VERSION, "LGPL", "GStreamer", "http://gstreamer.net/")
1466 /*** GSTURIHANDLER INTERFACE *************************************************/
1468 gst_mythtv_src_uri_get_type (void)
1474 gst_mythtv_src_uri_get_protocols (void)
1476 static gchar *protocols[] = { "myth", "myths", NULL };
1481 static const gchar *
1482 gst_mythtv_src_uri_get_uri (GstURIHandler * handler)
1484 GstMythtvSrc *src = GST_MYTHTV_SRC (handler);
1486 return src->uri_name;
1490 gst_mythtv_src_uri_set_uri (GstURIHandler * handler, const gchar * uri)
1492 GstMythtvSrc *src = GST_MYTHTV_SRC (handler);
1496 protocol = gst_uri_get_protocol (uri);
1497 if ((strcmp (protocol, "myth") != 0) && (strcmp (protocol, "myths") != 0)) {
1502 g_object_set (src, "location", uri, NULL);
1508 gst_mythtv_src_uri_handler_init (gpointer g_iface, gpointer iface_data)
1510 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
1512 iface->get_type = gst_mythtv_src_uri_get_type;
1513 iface->get_protocols = gst_mythtv_src_uri_get_protocols;
1514 iface->get_uri = gst_mythtv_src_uri_get_uri;
1515 iface->set_uri = gst_mythtv_src_uri_set_uri;
1519 size_header_handler (void *userdata, const char *value)
1521 GstMythtvSrc *src = GST_MYTHTV_SRC (userdata);
1523 //src->content_size = g_ascii_strtoull (value, NULL, 10);
1525 GST_DEBUG_OBJECT (src, "content size = %lld bytes", src->content_size);