[svn r695] Fixed erroneous maximum iteration count on reading zero-sized buffers on FileTransfer.
1 /* GStreamer MythTV Plug-in
2 * Copyright (C) <2006> Rosfran Borges <rosfran.borges@indt.org.br>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
20 * SECTION:element-mythtvsrc
24 * MythTVSrc allows to access a remote MythTV backend streaming Video/Audio server,
25 * and to render audio and video content through a TCP/IP connection to a specific
26 * port on this server, and based on a known MythTV protocol that is based on
27 * some message passing, such as REQUEST_BLOCK on a specified number of bytes, to get
28 * some chunk of remote file data.
29 * You should pass the information aboute the remote MythTV backend server
30 * through the <link linkend="GstMythTVSrc--location">location</link> property.
32 * <title>Examples</title>
34 * If you want to get the LiveTV content (set channel, TV tuner, RemoteEncoder,
36 * put the following URI:
39 * myth://xxx.xxx.xxx.xxx:6543/livetv?channel=BBC
42 * This URI will say to the gmyth library to configure the Recorder instance (used to
43 * change the channel, start the TV multimedia content transmition, etc.), using
44 * the IP address (xxx.xxx.xxx.xxx) and port number (6543) of the MythTV backend
45 * server, and setting the channel name to "BBC".
47 * To get a already recorded the MythTV NUV file, put the following URI:
50 * myth://xxx.xxx.xxx.xxx:6543/filename.nuv
53 * This URI will say to the gmyth library to configure the Recorder instance (used to
54 * change the channel, start the TV multimedia content transmition, etc.), using
55 * the IP address (xxx.xxx.xxx.xxx) and port number (6543) of the MythTV backend
56 * server, and setting the channel name to "BBC".
58 * Another possible way to use the LiveTV content, and just in the case you want to
59 * use the mysql database, put the location URI in the following format:
62 * myth://mythtv:mythtv@xxx.xxx.xxx.xxx:6543/?mythconverg&channel=9
65 * Where the first field is the protocol (myth), the second and third are user
66 * name (mythtv) and password (mythtv), then backend host name and port number,
67 * and the last field is the database name (mythconverg).
75 #include "gstmythtvsrc.h"
76 #include <gmyth/gmyth_file.h>
77 #include <gmyth/gmyth_file_transfer.h>
78 #include <gmyth/gmyth_file_local.h>
79 #include <gmyth/gmyth_livetv.h>
81 #include <gmyth/gmyth_socket.h>
82 #include <gmyth/gmyth_tvchain.h>
87 GST_DEBUG_CATEGORY_STATIC (mythtvsrc_debug);
88 #define GST_CAT_DEFAULT mythtvsrc_debug
90 #define GST_GMYTHTV_ID_NUM 1
92 #define GST_GMYTHTV_CHANNEL_DEFAULT_NUM (-1)
94 #define GMYTHTV_VERSION_DEFAULT 30
96 #define GMYTHTV_TRANSFER_MAX_WAITS 100
98 #define GMYTHTV_TRANSFER_MAX_RESENDS 2
100 #define GMYTHTV_TRANSFER_MAX_BUFFER (128*1024)
102 #define MAX_READ_SIZE (4*1024)
104 #define GST_FLOW_ERROR_NO_DATA (-101)
106 #define REQUEST_MAX_SIZE (64*1024)
108 #define INTERNAL_BUFFER_SIZE (90*1024)
110 static const GstElementDetails gst_mythtv_src_details =
111 GST_ELEMENT_DETAILS ("MythTV client source",
113 "Control and receive data as a client over the network "
114 "via raw socket connections using the MythTV protocol",
115 "Rosfran Borges <rosfran.borges@indt.org.br>");
117 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
120 GST_STATIC_CAPS ("video/x-nuv"));
126 #ifndef GST_DISABLE_GST_DEBUG
129 PROP_GMYTHTV_VERSION,
132 PROP_GMYTHTV_LIVE_CHAINID,
133 PROP_GMYTHTV_ENABLE_TIMING_POSITION,
134 PROP_GMYTHTV_CHANNEL_NUM,
138 static void gst_mythtv_src_clear (GstMythtvSrc *mythtv_src);
140 static void gst_mythtv_src_finalize (GObject * gobject);
142 static GstFlowReturn gst_mythtv_src_create (GstPushSrc * psrc,
143 GstBuffer ** outbuf);
145 static gboolean gst_mythtv_src_start (GstBaseSrc * bsrc);
146 static gboolean gst_mythtv_src_stop (GstBaseSrc * bsrc);
147 static gboolean gst_mythtv_src_get_size (GstBaseSrc * bsrc, guint64 * size);
148 static gboolean gst_mythtv_src_is_seekable (GstBaseSrc * push_src);
150 static gboolean gst_mythtv_src_do_seek (GstBaseSrc * base,
151 GstSegment * segment);
153 static GstStateChangeReturn
154 gst_mythtv_src_change_state (GstElement * element, GstStateChange transition);
156 static void gst_mythtv_src_set_property (GObject * object, guint prop_id,
157 const GValue * value, GParamSpec * pspec);
158 static void gst_mythtv_src_get_property (GObject * object, guint prop_id,
159 GValue * value, GParamSpec * pspec);
161 static void gst_mythtv_src_uri_handler_init (gpointer g_iface,
162 gpointer iface_data);
164 static gboolean gst_mythtv_src_handle_query (GstPad * pad, GstQuery * query);
166 static gboolean gst_mythtv_src_handle_event (GstPad * pad, GstEvent * event);
168 static GMythFileReadResult do_read_request_response (GstMythtvSrc * src, guint size,
169 GByteArray * data_ptr);
172 _urihandler_init (GType type)
174 static const GInterfaceInfo urihandler_info = {
175 gst_mythtv_src_uri_handler_init,
180 g_type_add_interface_static (type, GST_TYPE_URI_HANDLER, &urihandler_info);
182 GST_DEBUG_CATEGORY_INIT (mythtvsrc_debug, "mythtvsrc", 0, "MythTV src");
185 GST_BOILERPLATE_FULL (GstMythtvSrc, gst_mythtv_src, GstPushSrc,
186 GST_TYPE_PUSH_SRC, _urihandler_init)
188 static void gst_mythtv_src_base_init (gpointer g_class)
190 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
192 gst_element_class_add_pad_template (element_class,
193 gst_static_pad_template_get (&srctemplate));
195 gst_element_class_set_details (element_class, &gst_mythtv_src_details);
197 element_class->change_state = gst_mythtv_src_change_state;
202 gst_mythtv_src_class_init (GstMythtvSrcClass * klass)
204 GObjectClass *gobject_class;
205 GstPushSrcClass *gstpushsrc_class;
206 GstBaseSrcClass *gstbasesrc_class;
208 gobject_class = (GObjectClass *) klass;
209 gstbasesrc_class = (GstBaseSrcClass *) klass;
210 gstpushsrc_class = (GstPushSrcClass *) klass;
212 gobject_class->set_property = gst_mythtv_src_set_property;
213 gobject_class->get_property = gst_mythtv_src_get_property;
214 gobject_class->finalize = gst_mythtv_src_finalize;
216 g_object_class_install_property
217 (gobject_class, PROP_LOCATION,
218 g_param_spec_string ("location", "Location",
219 "The location. In the form:"
220 "\n\t\t\tmyth://a.com/file.nuv"
221 "\n\t\t\tmyth://a.com:23223/file.nuv "
222 "\n\t\t\ta.com/file.nuv - default scheme 'myth'",
223 "", G_PARAM_READWRITE));
225 g_object_class_install_property
226 (gobject_class, PROP_GMYTHTV_VERSION,
227 g_param_spec_int ("mythtv-version", "mythtv-version",
228 "Change MythTV version", 26, 30, 26, G_PARAM_READWRITE));
230 g_object_class_install_property
231 (gobject_class, PROP_GMYTHTV_LIVEID,
232 g_param_spec_int ("mythtv-live-id", "mythtv-live-id",
233 "Change MythTV version",
234 0, 200, GST_GMYTHTV_ID_NUM, G_PARAM_READWRITE));
236 g_object_class_install_property
237 (gobject_class, PROP_GMYTHTV_LIVE_CHAINID,
238 g_param_spec_string ("mythtv-live-chainid", "mythtv-live-chainid",
239 "Sets the MythTV chain ID (from TV Chain)", "", G_PARAM_READWRITE));
241 g_object_class_install_property
242 (gobject_class, PROP_GMYTHTV_LIVE,
243 g_param_spec_boolean ("mythtv-live", "mythtv-live",
244 "Enable MythTV Live TV content streaming", FALSE, G_PARAM_READWRITE));
246 g_object_class_install_property
247 (gobject_class, PROP_GMYTHTV_ENABLE_TIMING_POSITION,
248 g_param_spec_boolean ("mythtv-enable-timing-position",
249 "mythtv-enable-timing-position",
250 "Enable MythTV Live TV content size continuous updating", FALSE,
253 g_object_class_install_property
254 (gobject_class, PROP_GMYTHTV_CHANNEL_NUM,
255 g_param_spec_string ("mythtv-channel", "mythtv-channel",
256 "Change MythTV channel number",
257 "", G_PARAM_READWRITE));
259 g_object_class_install_property
260 (gobject_class, PROP_GMYTHTV_MAX_TRY,
261 g_param_spec_int ("max-try", "max-try",
262 "Set the max try for get MythTV free recorder",
263 0, G_MAXINT, 10, G_PARAM_READWRITE));
266 #ifndef GST_DISABLE_GST_DEBUG
267 g_object_class_install_property
268 (gobject_class, PROP_GMYTHTV_DBG,
269 g_param_spec_boolean ("mythtv-debug", "mythtv-debug",
270 "Enable MythTV debug messages", FALSE, G_PARAM_READWRITE));
273 gstbasesrc_class->start = gst_mythtv_src_start;
274 gstbasesrc_class->stop = gst_mythtv_src_stop;
275 gstbasesrc_class->get_size = gst_mythtv_src_get_size;
276 gstbasesrc_class->is_seekable = gst_mythtv_src_is_seekable;
278 gstbasesrc_class->do_seek = gst_mythtv_src_do_seek;
279 gstpushsrc_class->create = gst_mythtv_src_create;
281 GST_DEBUG_CATEGORY_INIT (mythtvsrc_debug, "mythtvsrc", 0,
282 "MythTV Client Source");
286 gst_mythtv_src_init (GstMythtvSrc * this, GstMythtvSrcClass * g_class)
290 this->unique_setup = FALSE;
292 this->mythtv_version = GMYTHTV_VERSION_DEFAULT;
294 this->state = GST_MYTHTV_SRC_FILE_TRANSFER;
296 this->bytes_read = 0;
298 this->prev_content_size = 0;
300 this->content_size = 0;
301 this->read_offset = 0;
303 this->content_size_last = 0;
305 this->live_tv = FALSE;
307 this->enable_timing_position = FALSE;
308 this->update_prog_chain = FALSE;
310 this->user_agent = g_strdup ("mythtvsrc");
311 this->update_prog_chain = FALSE;
313 this->channel_name = NULL;
317 this->bytes_queue = NULL;
319 this->wait_to_transfer = 0;
321 gst_base_src_set_format (GST_BASE_SRC (this), GST_FORMAT_BYTES);
323 gst_pad_set_event_function (GST_BASE_SRC_PAD (GST_BASE_SRC (this)),
324 gst_mythtv_src_handle_event);
325 gst_pad_set_query_function (GST_BASE_SRC_PAD (GST_BASE_SRC (this)),
326 gst_mythtv_src_handle_query);
331 gst_mythtv_src_clear (GstMythtvSrc *mythtv_src)
333 mythtv_src->unique_setup = FALSE;
337 if (mythtv_src->spawn_livetv) {
338 g_object_unref (mythtv_src->spawn_livetv);
339 mythtv_src->spawn_livetv = NULL;
342 if (mythtv_src->file) {
343 g_object_unref (mythtv_src->file);
344 mythtv_src->file = NULL;
347 if (mythtv_src->backend_info) {
348 g_object_unref (mythtv_src->backend_info);
349 mythtv_src->backend_info = NULL;
352 if (mythtv_src->bytes_queue) {
353 g_byte_array_free (mythtv_src->bytes_queue, TRUE);
354 mythtv_src->bytes_queue = NULL;
358 g_debug ("CLEAR END");
363 gst_mythtv_src_finalize (GObject * gobject)
365 GstMythtvSrc *this = GST_MYTHTV_SRC (gobject);
367 gst_mythtv_src_clear (this);
369 if (this->uri_name) {
370 g_free (this->uri_name);
371 this->uri_name = NULL;
374 if (this->user_agent) {
375 g_free (this->user_agent);
376 this->user_agent = NULL;
379 G_OBJECT_CLASS (parent_class)->finalize (gobject);
382 static GMythFileReadResult
383 do_read_request_response (GstMythtvSrc * src, guint size, GByteArray *data_ptr)
386 guint sizetoread = size;
387 gint max_iters = GMYTHTV_TRANSFER_MAX_RESENDS;
388 GMythFileReadResult result;
390 GST_LOG_OBJECT (src, "Starting: Reading %d bytes...", sizetoread);
392 /* Loop sending the Myth File Transfer request:
393 * Retry whilst authentication fails and we supply it. */
395 while (sizetoread == size && --max_iters > 0) {
396 /* if ( gmyth_backend_info_is_local_file(src->backend_info) ) */
397 if ( IS_GMYTH_FILE_LOCAL(src->file) )
398 result = gmyth_file_local_read ( GMYTH_FILE_LOCAL(src->file),
399 data_ptr, sizetoread, src->live_tv);
400 else if ( IS_GMYTH_FILE_TRANSFER(src->file) )
401 result = gmyth_file_transfer_read ( GMYTH_FILE_TRANSFER(src->file),
402 data_ptr, sizetoread, src->live_tv);
404 if (data_ptr->len > 0) {
405 read += data_ptr->len;
406 sizetoread -= data_ptr->len;
407 } else if (data_ptr->len < 0) {
408 if (src->live_tv == FALSE) {
409 result = GMYTH_FILE_READ_EOF;
412 if (result == GMYTH_FILE_READ_ERROR) { /* -314 */
413 GST_INFO_OBJECT (src, "[LiveTV] FileTransfer READ_ERROR!");
415 } else if (result == GMYTH_FILE_READ_NEXT_PROG_CHAIN) { /* -315 */
416 GST_INFO_OBJECT (src,
417 "[LiveTV] FileTransfer - Go to the next program chain!");
423 } else if (data_ptr->len == 0)
426 if (read == sizetoread)
430 if ((read < 0 && !src->live_tv) || max_iters == 0){
431 result = GMYTH_FILE_READ_EOF;
445 gst_mythtv_src_create (GstPushSrc * psrc, GstBuffer ** outbuf)
448 GstFlowReturn ret = GST_FLOW_OK;
449 guint buffer_size_inter = 0;
451 src = GST_MYTHTV_SRC (psrc);
453 /* The caller should know the number of bytes and not read beyond EOS. */
454 if (G_UNLIKELY (src->eos))
456 if (G_UNLIKELY (src->update_prog_chain))
457 goto change_progchain;
460 GST_DEBUG_OBJECT (src, "offset = %" G_GUINT64_FORMAT ", size = %d...",
461 src->read_offset, MAX_READ_SIZE);
463 GST_DEBUG_OBJECT (src, "Create: buffer_remain: %d, buffer_size = %d.",
464 (gint) src->buffer_remain, src->bytes_queue->len);
467 program_chain_changed:
468 /* just get from the byte array, no network effort... */
469 if ((src->buffer_remain = src->bytes_queue->len) < MAX_READ_SIZE) {
471 GMythFileReadResult result = GMYTH_FILE_READ_OK;
474 buffer_size_inter = (INTERNAL_BUFFER_SIZE - src->buffer_remain);
476 if (buffer_size_inter > REQUEST_MAX_SIZE)
477 buffer_size_inter = REQUEST_MAX_SIZE;
479 buffer = g_byte_array_new ();
481 result = do_read_request_response (src, buffer_size_inter, buffer);
483 if (G_UNLIKELY (buffer->len < 0)) {
485 if (buffer != NULL) {
486 g_byte_array_free (buffer, TRUE);
490 if (src->live_tv || ( result == GMYTH_FILE_READ_NEXT_PROG_CHAIN ))
491 goto change_progchain;
494 } else if (G_UNLIKELY (read == 0)) {
496 if (buffer != NULL) {
497 g_byte_array_free (buffer, TRUE);
504 goto program_chain_changed;
507 if (G_UNLIKELY (src->update_prog_chain))
509 if (buffer != NULL) {
510 g_byte_array_free (buffer, TRUE);
513 goto change_progchain;
517 g_byte_array_append (src->bytes_queue, buffer->data, buffer->len);
518 if (buffer->len > buffer_size_inter)
519 GST_WARNING_OBJECT (src,
520 "INCREASED buffer size! Backend sent more than we ask him... (%d)",
521 abs (buffer->len - buffer_size_inter));
523 src->buffer_remain += buffer->len;
525 if (buffer != NULL) {
526 g_byte_array_free (buffer, TRUE);
531 GST_DEBUG_OBJECT (src,
532 "BYTES READ (actual) = %d, BYTES READ (cumulative) = %llu, "
533 "OFFSET = %llu, CONTENT SIZE = %llu.", read,
534 src->bytes_read, src->read_offset, src->content_size);
540 (src->buffer_remain < MAX_READ_SIZE) ? src->buffer_remain : MAX_READ_SIZE;
542 *outbuf = gst_buffer_new ();
544 /* gets the first buffer_size bytes from the byte array buffer variable */
545 /* guint8 *buf = g_memdup( src->bytes_queue->data, buffer_size ); */
548 GST_DEBUG_OBJECT (src, "read from network? %s!, buffer_remain = %d",
549 (buffer_size_inter ==
550 0) ? "NO, got from buffer" : "YES, go see the backend's log file",
554 GST_BUFFER_SIZE (*outbuf) = buffer_size;
555 GST_BUFFER_MALLOCDATA (*outbuf) = g_malloc0 (GST_BUFFER_SIZE (*outbuf));
556 GST_BUFFER_DATA (*outbuf) = GST_BUFFER_MALLOCDATA (*outbuf);
557 g_memmove (GST_BUFFER_DATA ((*outbuf)), src->bytes_queue->data,
558 GST_BUFFER_SIZE (*outbuf));
559 GST_BUFFER_OFFSET (*outbuf) = src->read_offset;
560 GST_BUFFER_OFFSET_END (*outbuf) =
561 src->read_offset + GST_BUFFER_SIZE (*outbuf);
563 src->buffer_remain -= GST_BUFFER_SIZE (*outbuf);
565 src->read_offset += GST_BUFFER_SIZE (*outbuf);
566 src->bytes_read += GST_BUFFER_SIZE (*outbuf);
567 //GST_DEBUG_OBJECT (src, "Buffer output with size: %d",
568 // GST_BUFFER_SIZE (*outbuf));
570 /* flushs the newly buffer got from byte array */
572 g_byte_array_remove_range (src->bytes_queue, 0, buffer_size);
574 if ( G_UNLIKELY (src->eos) || ( !src->live_tv
575 && ( src->bytes_read >= src->content_size ) ) )
580 const gchar *reason = gst_flow_get_name (ret);
585 const gchar *reason = gst_flow_get_name (ret);
587 GST_DEBUG_OBJECT (src, "pausing task, reason %s", reason);
588 return GST_FLOW_UNEXPECTED;
593 GST_ELEMENT_ERROR (src, RESOURCE, READ,
594 (NULL), ("Could not read any bytes (%i, %s)", read, src->uri_name));
595 return GST_FLOW_ERROR;
599 GST_ELEMENT_ERROR (src, RESOURCE, READ,
600 (NULL), ("Seek failed, go to the next program info... (%i, %s)", read,
604 TODO: need to send a new segment event to NUVDemux?
605 gst_pad_push_event (GST_BASE_SRC_PAD (GST_BASE_SRC (psrc)),
606 gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0, -1, 0));
609 goto program_chain_changed;
615 gst_mythtv_src_get_position (GstMythtvSrc * src)
621 if (src->live_tv == TRUE && (abs (src->content_size - src->bytes_read) <
622 GMYTHTV_TRANSFER_MAX_BUFFER)) {
626 size_tmp = gmyth_recorder_get_file_position (src->spawn_livetv->recorder);
627 if (size_tmp > (src->content_size + GMYTHTV_TRANSFER_MAX_BUFFER))
628 src->content_size = size_tmp;
629 else if (size_tmp > 0 && --max_tries > 0)
631 GST_LOG_OBJECT (src, "GET_POSITION: file_position = %lld", size_tmp);
632 /* sets the last content size amount before it can be updated */
633 src->prev_content_size = src->content_size;
636 return src->content_size;
641 gst_mythtv_src_do_seek (GstBaseSrc * base, GstSegment * segment)
643 GstMythtvSrc *src = GST_MYTHTV_SRC (base);
644 gint64 new_offset = -1;
645 gint64 actual_seek = segment->start;
648 GST_LOG_OBJECT (src, "seek, segment: %" GST_SEGMENT_FORMAT, segment);
650 if (segment->format == GST_FORMAT_TIME) {
654 "Trying to seek at the value (actual_seek = %lld, read_offset = %lld)",
655 actual_seek, src->read_offset);
656 /* verify if it needs to seek */
657 if (src->read_offset != actual_seek) {
659 /* if ( gmyth_backend_info_is_local_file(src->backend_info) ) */
660 if ( IS_GMYTH_FILE_LOCAL(src->file) )
662 gmyth_file_local_seek ( GMYTH_FILE_LOCAL(src->file), segment->start, G_SEEK_SET);
663 else if ( IS_GMYTH_FILE_TRANSFER(src->file) )
665 gmyth_file_transfer_seek ( GMYTH_FILE_TRANSFER(src->file), segment->start, SEEK_SET);
668 "Segment offset start = %lld, SRC Offset = %lld, NEW actual backend SEEK Offset = %lld.",
669 segment->start, src->read_offset, new_offset);
670 if (G_UNLIKELY (new_offset < 0)) {
676 src->read_offset = new_offset;
679 GST_INFO_OBJECT (src, "Failed to set the SEEK on segment!");
689 GST_DEBUG_OBJECT (src, "EOS found on seeking!!!");
695 /* create a socket for connecting to remote server */
697 gst_mythtv_src_start (GstBaseSrc * bsrc)
699 GstMythtvSrc *src = GST_MYTHTV_SRC (bsrc);
701 GString *chain_id_local = NULL;
702 GMythURI *gmyth_uri = NULL;
706 if (src->unique_setup == FALSE) {
707 src->unique_setup = TRUE;
712 gmyth_uri = gmyth_uri_new_with_value (src->uri_name);
713 src->backend_info = gmyth_backend_info_new_with_uri (src->uri_name);
714 src->live_tv = gmyth_uri_is_livetv( gmyth_uri );
715 /* testing UPnP... */
716 /* gmyth_backend_info_set_hostname( src->backend_info, NULL ); */
717 if ( src->live_tv ) {
718 src->spawn_livetv = gmyth_livetv_new (src->backend_info);
720 gchar* ch = gmyth_uri_get_channel_name( gmyth_uri );
722 src->channel_name = ch;
724 if (src->channel_name != NULL) {
729 result = gmyth_livetv_channel_name_setup (src->spawn_livetv, src->channel_name);
730 if (result == TRUE) {
731 g_debug ("LIVE STARTED");
735 g_debug ("FAIL TO START LIVE");
736 g_usleep (0.5 * G_USEC_PER_SEC);
740 if (result == FALSE) {
741 GST_INFO_OBJECT (src, "LiveTV setup felt down on error");
747 if (gmyth_livetv_setup (src->spawn_livetv) == FALSE) {
748 GST_INFO_OBJECT (src, "LiveTV setup felt down on error");
754 /* testing change channel... */
755 /* gmyth_recorder_change_channel( src->spawn_livetv->recorder, CHANNEL_DIRECTION_UP ); */
757 src->file = GMYTH_FILE( gmyth_livetv_create_file_transfer (src->spawn_livetv) );
759 if (NULL == src->file) {
760 GST_INFO_OBJECT (src, "[LiveTV] FileTransfer equals to NULL");
765 /* Check if the file is local to this specific client renderer */
766 if ( gmyth_uri_is_local_file(gmyth_uri) )
767 ret = gmyth_file_local_open( GMYTH_FILE_LOCAL(src->file) );
769 ret = gmyth_file_transfer_open( GMYTH_FILE_TRANSFER(src->file), src->spawn_livetv->uri != NULL ?
770 gmyth_uri_get_path(src->spawn_livetv->uri) :
771 src->spawn_livetv->proginfo->pathname->str );
773 /* sets the mythtvsrc "location" property */
774 g_object_set (src, "location", gmyth_file_get_uri (src->file), NULL);
778 GST_INFO_OBJECT (src, "Error: couldn't open the FileTransfer from LiveTV source!" );
779 g_object_unref( src->file );
785 /* Check if the file is local to this specific client renderer, and tries to open
788 if ( gmyth_uri_is_local_file(gmyth_uri) )
790 src->file = GMYTH_FILE(gmyth_file_local_new(src->backend_info));
791 ret = gmyth_file_local_open ( GMYTH_FILE_LOCAL( src->file ) );
793 src->file = GMYTH_FILE(gmyth_file_transfer_new(src->backend_info));
794 ret = gmyth_file_transfer_open ( GMYTH_FILE_TRANSFER(src->file), src->uri_name );
797 } /* if (else) - recorded FileTransfer */
799 if (NULL == src->file) {
800 GST_INFO_OBJECT (src, "FileTransfer is NULL");
803 /*GST_INFO_OBJECT( src, "uri = %s", src->spawn_livetv->file); */
806 #ifndef GST_DISABLE_GST_DEBUG
807 if (src->mythtv_msgs_dbg)
808 GST_INFO_OBJECT (src,
809 "MythTV FileTransfer request failed when setting up socket connection!");
811 goto begin_req_failed;
814 GST_INFO_OBJECT (src,
815 "MythTV FileTransfer filesize = %lld, content_size = %lld!",
816 gmyth_file_get_filesize( src->file ), src->content_size);
818 src->content_size = gmyth_file_get_filesize (src->file);
820 msg = gst_message_new_duration (GST_OBJECT (src), GST_FORMAT_BYTES, src->content_size);
821 gst_element_post_message (GST_ELEMENT (src), msg);
824 src->do_start = FALSE;
826 /* this is used for the buffer cache */
827 src->bytes_queue = g_byte_array_sized_new (INTERNAL_BUFFER_SIZE);
828 src->buffer_remain = 0;
830 gst_pad_push_event (GST_BASE_SRC_PAD (GST_BASE_SRC (src)),
831 gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0,
832 src->content_size, 0));
835 if (gmyth_uri != NULL)
837 g_object_unref (gmyth_uri);
841 if (chain_id_local != NULL) {
842 g_string_free (chain_id_local, TRUE);
843 chain_id_local = NULL;
850 if (gmyth_uri != NULL)
852 g_object_unref (gmyth_uri);
856 if (src->spawn_livetv != NULL) {
857 g_object_unref (src->spawn_livetv);
858 src->spawn_livetv = NULL;
861 GST_ELEMENT_ERROR (src, LIBRARY, INIT,
862 (NULL), ("Could not initialize MythTV library (%i, %s)", ret,
866 gst_mythtv_src_clear (src);
870 if (gmyth_uri != NULL)
872 g_object_unref (gmyth_uri);
876 GST_ELEMENT_ERROR (src, LIBRARY, INIT,
877 (NULL), ("Could not begin request sent to MythTV server (%i, %s)", ret,
884 gst_mythtv_src_get_size (GstBaseSrc * bsrc, guint64 * size)
886 GstMythtvSrc *src = GST_MYTHTV_SRC (bsrc);
889 GST_LOG_OBJECT (src, "Differs from previous content size: %d (max.: %d)",
890 abs (src->content_size - src->prev_content_size),
891 GMYTHTV_TRANSFER_MAX_BUFFER);
895 } else if (src->live_tv && src->enable_timing_position
896 && (abs (src->content_size - src->bytes_read) <
897 GMYTHTV_TRANSFER_MAX_BUFFER)) {
900 gmyth_recorder_get_file_position (src->spawn_livetv->recorder);
901 if (new_offset > 0 && new_offset > src->content_size) {
902 src->content_size = new_offset;
903 } else if (new_offset < src->content_size) {
904 src->update_prog_chain = TRUE;
909 *size = src->content_size;
910 GST_LOG_OBJECT (src, "Content size = %lld", src->content_size);
916 /* close the socket and associated resources
917 * used both to recover from errors and go to NULL state */
919 gst_mythtv_src_stop (GstBaseSrc * bsrc)
921 GstMythtvSrc *src = GST_MYTHTV_SRC (bsrc);
923 gst_mythtv_src_clear (src);
925 /* src->eos = FALSE; */
931 gst_mythtv_src_handle_event (GstPad * pad, GstEvent * event)
933 GstMythtvSrc *src = GST_MYTHTV_SRC (GST_PAD_PARENT (pad));
934 gint64 cont_size = 0;
935 gboolean ret = FALSE;
937 switch (GST_EVENT_TYPE (event)) {
939 GST_WARNING_OBJECT (src, "Got EOS event");
942 cont_size = gst_mythtv_src_get_position (src);
943 if (cont_size > src->content_size) {
944 src->content_size = cont_size;
948 gst_element_set_state (GST_ELEMENT (src), GST_STATE_NULL);
949 gst_element_set_locked_state (GST_ELEMENT (src), FALSE);
954 ret = gst_pad_event_default (pad, event);
961 gst_mythtv_src_is_seekable (GstBaseSrc * push_src)
967 gst_mythtv_src_handle_query (GstPad * pad, GstQuery * query)
969 gboolean res = FALSE;
970 GstMythtvSrc *myth = GST_MYTHTV_SRC (gst_pad_get_parent (pad));
974 switch (GST_QUERY_TYPE (query)) {
975 case GST_QUERY_POSITION:
977 gst_query_parse_position (query, &formt, NULL);
978 if (formt == GST_FORMAT_BYTES) {
979 gst_query_set_position (query, formt, myth->read_offset);
980 GST_DEBUG_OBJECT (myth, "POS %" G_GINT64_FORMAT, myth->read_offset);
982 } else if (formt == GST_FORMAT_TIME) {
983 res = gst_pad_query_default (pad, query);
987 case GST_QUERY_DURATION:
989 gst_query_parse_duration (query, &formt, NULL);
990 if (formt == GST_FORMAT_BYTES) {
991 gint64 size = myth->content_size;
992 gst_query_set_duration (query, GST_FORMAT_BYTES, 10);
993 GST_DEBUG_OBJECT (myth, "SIZE %" G_GINT64_FORMAT, size);
995 } else if (formt == GST_FORMAT_TIME) {
996 res = gst_pad_query_default (pad, query);
1002 res = gst_pad_query_default (pad, query);
1007 gst_object_unref (myth);
1012 static GstStateChangeReturn
1013 gst_mythtv_src_change_state (GstElement * element, GstStateChange transition)
1015 GstStateChangeReturn ret = GST_STATE_CHANGE_FAILURE;
1016 GstMythtvSrc *src = GST_MYTHTV_SRC (element);
1018 switch (transition) {
1019 case GST_STATE_CHANGE_NULL_TO_READY:
1021 case GST_STATE_CHANGE_READY_TO_PAUSED:
1022 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1024 if (!gmyth_recorder_send_frontend_ready_command (src->spawn_livetv->
1026 GST_WARNING_OBJECT (src,
1027 "Couldn't send the FRONTEND_READY message to the backend!");
1029 GST_DEBUG_OBJECT (src, "FRONTEND_READY was sent to the backend");
1036 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1037 if (ret == GST_STATE_CHANGE_FAILURE)
1040 switch (transition) {
1041 case GST_STATE_CHANGE_READY_TO_NULL:
1042 g_debug ("STAAAAAAAAAAAATTTTTTTTTEEEEEEEEE NULLLLLLLLLLLLLLLL");
1043 gst_mythtv_src_clear (src);
1045 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1046 case GST_STATE_CHANGE_PAUSED_TO_READY:
1056 gst_mythtv_src_set_property (GObject * object, guint prop_id,
1057 const GValue * value, GParamSpec * pspec)
1059 GstMythtvSrc *mythtvsrc = GST_MYTHTV_SRC (object);
1061 GST_OBJECT_LOCK (mythtvsrc);
1065 if (!g_value_get_string (value)) {
1066 GST_WARNING ("location property cannot be NULL");
1070 if (mythtvsrc->uri_name != NULL) {
1071 g_free (mythtvsrc->uri_name);
1072 mythtvsrc->uri_name = NULL;
1074 mythtvsrc->uri_name = g_value_dup_string (value);
1078 #ifndef GST_DISABLE_GST_DEBUG
1079 case PROP_GMYTHTV_DBG:
1081 mythtvsrc->mythtv_msgs_dbg = g_value_get_boolean (value);
1085 case PROP_GMYTHTV_VERSION:
1087 mythtvsrc->mythtv_version = g_value_get_int (value);
1090 case PROP_GMYTHTV_LIVEID:
1092 mythtvsrc->live_tv_id = g_value_get_int (value);
1095 case PROP_GMYTHTV_LIVE:
1097 mythtvsrc->live_tv = g_value_get_boolean (value);
1100 case PROP_GMYTHTV_ENABLE_TIMING_POSITION:
1102 mythtvsrc->enable_timing_position = g_value_get_boolean (value);
1105 case PROP_GMYTHTV_LIVE_CHAINID:
1107 if (!g_value_get_string (value)) {
1108 GST_WARNING ("MythTV Live chainid property cannot be NULL");
1112 if (mythtvsrc->live_chain_id != NULL) {
1113 g_free (mythtvsrc->live_chain_id);
1114 mythtvsrc->live_chain_id = NULL;
1116 mythtvsrc->live_chain_id = g_value_dup_string (value);
1119 case PROP_GMYTHTV_CHANNEL_NUM:
1121 mythtvsrc->channel_name = g_value_dup_string (value);
1124 case PROP_GMYTHTV_MAX_TRY:
1126 mythtvsrc->max_try = g_value_get_int (value);
1130 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1134 GST_OBJECT_UNLOCK (mythtvsrc);
1138 gst_mythtv_src_get_property (GObject * object, guint prop_id,
1139 GValue * value, GParamSpec * pspec)
1141 GstMythtvSrc *mythtvsrc = GST_MYTHTV_SRC (object);
1143 GST_OBJECT_LOCK (mythtvsrc);
1147 g_value_set_string (value, mythtvsrc->uri_name);
1150 #ifndef GST_DISABLE_GST_DEBUG
1151 case PROP_GMYTHTV_DBG:
1152 g_value_set_boolean (value, mythtvsrc->mythtv_msgs_dbg);
1155 case PROP_GMYTHTV_VERSION:
1157 g_value_set_int (value, mythtvsrc->mythtv_version);
1160 case PROP_GMYTHTV_LIVEID:
1162 g_value_set_int (value, mythtvsrc->live_tv_id);
1165 case PROP_GMYTHTV_LIVE:
1166 g_value_set_boolean (value, mythtvsrc->live_tv);
1168 case PROP_GMYTHTV_ENABLE_TIMING_POSITION:
1169 g_value_set_boolean (value, mythtvsrc->enable_timing_position);
1171 case PROP_GMYTHTV_LIVE_CHAINID:
1173 g_value_set_string (value, mythtvsrc->live_chain_id);
1176 case PROP_GMYTHTV_CHANNEL_NUM:
1178 g_value_set_string (value, mythtvsrc->channel_name);
1181 case PROP_GMYTHTV_MAX_TRY:
1183 g_value_set_int (value, mythtvsrc->max_try);
1188 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1191 GST_OBJECT_UNLOCK (mythtvsrc);
1195 plugin_init (GstPlugin * plugin)
1197 return gst_element_register (plugin, "mythtvsrc", GST_RANK_NONE,
1198 GST_TYPE_MYTHTV_SRC);
1201 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1205 plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN);
1208 /*** GSTURIHANDLER INTERFACE *************************************************/
1210 gst_mythtv_src_uri_get_type (void)
1216 gst_mythtv_src_uri_get_protocols (void)
1218 static gchar *protocols[] = { "myth", "myths", NULL };
1223 static const gchar *
1224 gst_mythtv_src_uri_get_uri (GstURIHandler * handler)
1226 GstMythtvSrc *src = GST_MYTHTV_SRC (handler);
1228 return src->uri_name;
1232 gst_mythtv_src_uri_set_uri (GstURIHandler * handler, const gchar * uri)
1234 GstMythtvSrc *src = GST_MYTHTV_SRC (handler);
1238 protocol = gst_uri_get_protocol (uri);
1239 if ((strcmp (protocol, "myth") != 0) && (strcmp (protocol, "myths") != 0)) {
1244 g_object_set (src, "location", uri, NULL);
1250 gst_mythtv_src_uri_handler_init (gpointer g_iface, gpointer iface_data)
1252 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
1254 iface->get_type = gst_mythtv_src_uri_get_type;
1255 iface->get_protocols = gst_mythtv_src_uri_get_protocols;
1256 iface->get_uri = gst_mythtv_src_uri_get_uri;
1257 iface->set_uri = gst_mythtv_src_uri_set_uri;
1261 size_header_handler (void *userdata, const char *value)
1263 GstMythtvSrc *src = GST_MYTHTV_SRC (userdata);
1265 GST_DEBUG_OBJECT (src, "content size = %lld bytes", src->content_size);