[svn r323] Some updates in the LiveTV.
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 * When using the LiveTV content, put the location URI in the following
23 * myth://mythtv:mythtv@xxx.xxx.xxx.xxx:6543/?mythconverg
25 * Where the first field is the protocol (myth), the second and third are user
26 * name (mythtv) and password (mythtv), then backend host name and port number,
27 * and the last field is the database name (mythconverg).
34 #include "gstmythtvsrc.h"
35 #include <gmyth/gmyth_file_transfer.h>
36 #include <gmyth/gmyth_livetv.h>
38 #include <gmyth/gmyth_socket.h>
39 #include <gmyth/gmyth_tvchain.h>
44 GST_DEBUG_CATEGORY_STATIC (mythtvsrc_debug);
45 #define GST_CAT_DEFAULT mythtvsrc_debug
47 #define GST_GMYTHTV_ID_NUM 1
49 #define GST_GMYTHTV_CHANNEL_DEFAULT_NUM (-1)
51 #define GMYTHTV_VERSION_DEFAULT 30
53 #define GMYTHTV_TRANSFER_MAX_WAITS 100
55 #define GMYTHTV_TRANSFER_MAX_RESENDS 2
57 #define GMYTHTV_TRANSFER_MAX_BUFFER (128*1024)
59 #define MAX_READ_SIZE (4*1024)
61 #define GST_FLOW_ERROR_NO_DATA (-101)
63 #define REQUEST_MAX_SIZE (64*1024)
65 #define INTERNAL_BUFFER_SIZE (90*1024)
67 static const GstElementDetails gst_mythtv_src_details =
68 GST_ELEMENT_DETAILS ("MythTV client source",
70 "Control and receive data as a client over the network "
71 "via raw socket connections using the MythTV protocol",
72 "Rosfran Borges <rosfran.borges@indt.org.br>");
74 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
77 GST_STATIC_CAPS ("video/x-nuv"));
83 #ifndef GST_DISABLE_GST_DEBUG
89 PROP_GMYTHTV_LIVE_CHAINID,
90 PROP_GMYTHTV_ENABLE_TIMING_POSITION,
91 PROP_GMYTHTV_CHANNEL_NUM
94 static void gst_mythtv_src_finalize (GObject * gobject);
96 static GstFlowReturn gst_mythtv_src_create (GstPushSrc * psrc,
99 static gboolean gst_mythtv_src_start (GstBaseSrc * bsrc);
100 static gboolean gst_mythtv_src_stop (GstBaseSrc * bsrc);
101 static gboolean gst_mythtv_src_get_size (GstBaseSrc * bsrc, guint64 * size);
102 static gboolean gst_mythtv_src_is_seekable (GstBaseSrc * push_src);
104 static gboolean gst_mythtv_src_do_seek (GstBaseSrc * base,
105 GstSegment * segment);
107 static gboolean gst_mythtv_src_next_program_chain (GstMythtvSrc * src);
109 static GstStateChangeReturn
110 gst_mythtv_src_change_state (GstElement * element, GstStateChange transition);
112 static void gst_mythtv_src_set_property (GObject * object, guint prop_id,
113 const GValue * value, GParamSpec * pspec);
114 static void gst_mythtv_src_get_property (GObject * object, guint prop_id,
115 GValue * value, GParamSpec * pspec);
117 static void gst_mythtv_src_uri_handler_init (gpointer g_iface,
118 gpointer iface_data);
120 static gboolean gst_mythtv_src_handle_query (GstPad * pad, GstQuery * query);
122 static gboolean gst_mythtv_src_handle_event (GstPad * pad, GstEvent * event);
124 static gint do_read_request_response (GstMythtvSrc * src, guint size,
125 GByteArray * data_ptr);
128 _urihandler_init (GType type)
130 static const GInterfaceInfo urihandler_info = {
131 gst_mythtv_src_uri_handler_init,
136 g_type_add_interface_static (type, GST_TYPE_URI_HANDLER, &urihandler_info);
138 GST_DEBUG_CATEGORY_INIT (mythtvsrc_debug, "mythtvsrc", 0, "MythTV src");
141 GST_BOILERPLATE_FULL (GstMythtvSrc, gst_mythtv_src, GstPushSrc,
142 GST_TYPE_PUSH_SRC, _urihandler_init)
144 static void gst_mythtv_src_base_init (gpointer g_class)
146 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
148 gst_element_class_add_pad_template (element_class,
149 gst_static_pad_template_get (&srctemplate));
151 gst_element_class_set_details (element_class, &gst_mythtv_src_details);
153 element_class->change_state = gst_mythtv_src_change_state;
158 gst_mythtv_src_class_init (GstMythtvSrcClass * klass)
160 GObjectClass *gobject_class;
161 GstPushSrcClass *gstpushsrc_class;
162 GstBaseSrcClass *gstbasesrc_class;
164 gobject_class = (GObjectClass *) klass;
165 gstbasesrc_class = (GstBaseSrcClass *) klass;
166 gstpushsrc_class = (GstPushSrcClass *) klass;
168 gobject_class->set_property = gst_mythtv_src_set_property;
169 gobject_class->get_property = gst_mythtv_src_get_property;
170 gobject_class->finalize = gst_mythtv_src_finalize;
172 g_object_class_install_property
173 (gobject_class, PROP_LOCATION,
174 g_param_spec_string ("location", "Location",
175 "The location. In the form:"
176 "\n\t\t\tmyth://a.com/file.nuv"
177 "\n\t\t\tmyth://a.com:23223/file.nuv "
178 "\n\t\t\ta.com/file.nuv - default scheme 'myth'",
179 "", G_PARAM_READWRITE));
181 g_object_class_install_property
182 (gobject_class, PROP_GMYTHTV_VERSION,
183 g_param_spec_int ("mythtv-version", "mythtv-version",
184 "Change MythTV version", 26, 30, 26, G_PARAM_READWRITE));
186 g_object_class_install_property
187 (gobject_class, PROP_GMYTHTV_LIVEID,
188 g_param_spec_int ("mythtv-live-id", "mythtv-live-id",
189 "Change MythTV version",
190 0, 200, GST_GMYTHTV_ID_NUM, G_PARAM_READWRITE));
192 g_object_class_install_property
193 (gobject_class, PROP_GMYTHTV_LIVE_CHAINID,
194 g_param_spec_string ("mythtv-live-chainid", "mythtv-live-chainid",
195 "Sets the MythTV chain ID (from TV Chain)", "", G_PARAM_READWRITE));
197 g_object_class_install_property
198 (gobject_class, PROP_GMYTHTV_LIVE,
199 g_param_spec_boolean ("mythtv-live", "mythtv-live",
200 "Enable MythTV Live TV content streaming", FALSE, G_PARAM_READWRITE));
202 g_object_class_install_property
203 (gobject_class, PROP_GMYTHTV_ENABLE_TIMING_POSITION,
204 g_param_spec_boolean ("mythtv-enable-timing-position",
205 "mythtv-enable-timing-position",
206 "Enable MythTV Live TV content size continuous updating", FALSE,
209 g_object_class_install_property
210 (gobject_class, PROP_GMYTHTV_CHANNEL_NUM,
211 g_param_spec_string ("mythtv-channel", "mythtv-channel",
212 "Change MythTV channel number",
213 "", G_PARAM_READWRITE));
215 #ifndef GST_DISABLE_GST_DEBUG
216 g_object_class_install_property
217 (gobject_class, PROP_GMYTHTV_DBG,
218 g_param_spec_boolean ("mythtv-debug", "mythtv-debug",
219 "Enable MythTV debug messages", FALSE, G_PARAM_READWRITE));
222 gstbasesrc_class->start = gst_mythtv_src_start;
223 gstbasesrc_class->stop = gst_mythtv_src_stop;
224 gstbasesrc_class->get_size = gst_mythtv_src_get_size;
225 gstbasesrc_class->is_seekable = gst_mythtv_src_is_seekable;
227 gstbasesrc_class->do_seek = gst_mythtv_src_do_seek;
228 gstpushsrc_class->create = gst_mythtv_src_create;
230 GST_DEBUG_CATEGORY_INIT (mythtvsrc_debug, "mythtvsrc", 0,
231 "MythTV Client Source");
235 gst_mythtv_src_init (GstMythtvSrc * this, GstMythtvSrcClass * g_class)
237 this->file_transfer = NULL;
239 this->unique_setup = FALSE;
241 this->mythtv_version = GMYTHTV_VERSION_DEFAULT;
243 this->state = GST_MYTHTV_SRC_FILE_TRANSFER;
245 this->bytes_read = 0;
247 this->prev_content_size = 0;
249 this->content_size = 0;
250 this->read_offset = 0;
252 this->content_size_last = 0;
254 this->live_tv = FALSE;
256 this->enable_timing_position = FALSE;
257 this->update_prog_chain = FALSE;
259 this->user_agent = g_strdup ("mythtvsrc");
260 this->mythtv_caps = NULL;
261 this->update_prog_chain = FALSE;
263 this->channel_name = NULL;
267 this->bytes_queue = NULL;
269 this->wait_to_transfer = 0;
271 gst_base_src_set_format (GST_BASE_SRC (this), GST_FORMAT_BYTES);
273 gst_pad_set_event_function (GST_BASE_SRC_PAD (GST_BASE_SRC (this)),
274 gst_mythtv_src_handle_event);
275 gst_pad_set_query_function (GST_BASE_SRC_PAD (GST_BASE_SRC (this)),
276 gst_mythtv_src_handle_query);
281 gst_mythtv_src_finalize (GObject * gobject)
283 GstMythtvSrc *this = GST_MYTHTV_SRC (gobject);
285 if (this->mythtv_caps) {
286 gst_caps_unref (this->mythtv_caps);
287 this->mythtv_caps = NULL;
290 if (this->file_transfer) {
291 g_object_unref (this->file_transfer);
292 this->file_transfer = NULL;
295 if (this->spawn_livetv) {
296 g_object_unref (this->spawn_livetv);
297 this->spawn_livetv = NULL;
300 if (this->backend_info) {
301 g_object_unref (this->backend_info);
302 this->backend_info = NULL;
305 if (this->uri_name) {
306 g_free (this->uri_name);
309 if (this->user_agent) {
310 g_free (this->user_agent);
313 if (this->bytes_queue) {
314 g_byte_array_free (this->bytes_queue, TRUE);
315 this->bytes_queue = NULL;
318 G_OBJECT_CLASS (parent_class)->finalize (gobject);
322 do_read_request_response (GstMythtvSrc * src, guint size, GByteArray * data_ptr)
325 guint sizetoread = size;
326 gint max_iters = GMYTHTV_TRANSFER_MAX_RESENDS;
328 GST_LOG_OBJECT (src, "Starting: Reading %d bytes...", sizetoread);
330 /* Loop sending the Myth File Transfer request:
331 * Retry whilst authentication fails and we supply it. */
334 while (sizetoread == size && --max_iters > 0) {
336 len = gmyth_file_transfer_read (src->file_transfer,
337 data_ptr, sizetoread, TRUE);
342 } else if (len < 0) {
344 if (src->live_tv == FALSE) {
348 if (len == GMYTHTV_FILE_TRANSFER_READ_ERROR) { /* -314 */
349 GST_INFO_OBJECT (src, "[LiveTV] FileTransfer READ_ERROR!");
351 } else if (len == GMYTHTV_FILE_TRANSFER_NEXT_PROG_CHAIN) { /* -315 */
352 GST_INFO_OBJECT (src,
353 "[LiveTV] FileTransfer - Go to the next program chain!");
362 if (read == sizetoread)
366 if ((read < 0 && !src->live_tv) || max_iters == 0)
379 gst_mythtv_src_create (GstPushSrc * psrc, GstBuffer ** outbuf)
382 GstFlowReturn ret = GST_FLOW_OK;
385 src = GST_MYTHTV_SRC (psrc);
387 /* The caller should know the number of bytes and not read beyond EOS. */
388 if (G_UNLIKELY (src->eos))
390 if (G_UNLIKELY (src->update_prog_chain))
391 goto change_progchain;
393 GST_DEBUG_OBJECT (src, "offset = %" G_GUINT64_FORMAT ", size = %d...",
394 src->read_offset, MAX_READ_SIZE);
396 GST_DEBUG_OBJECT (src, "Create: buffer_remain: %d, buffer_size = %d.",
397 (gint) src->buffer_remain, src->bytes_queue->len);
399 /* just get from the byte array, no network effort... */
400 if ((src->buffer_remain = src->bytes_queue->len) < MAX_READ_SIZE) {
401 GByteArray *buffer = NULL;
402 guint buffer_size_inter = (INTERNAL_BUFFER_SIZE - src->buffer_remain);
404 if (buffer_size_inter > REQUEST_MAX_SIZE)
405 buffer_size_inter = REQUEST_MAX_SIZE;
407 buffer = g_byte_array_new ();
409 read = do_read_request_response (src, buffer_size_inter, buffer);
411 if (G_UNLIKELY (read < 0)) {
413 goto change_progchain;
416 } else if (G_UNLIKELY (read == 0)) {
420 goto change_progchain;
423 if (G_UNLIKELY (src->update_prog_chain))
424 goto change_progchain;
427 g_byte_array_append (src->bytes_queue, buffer->data, read);
428 if (read > buffer_size_inter)
429 GST_WARNING_OBJECT (src,
430 "INCREASED buffer size! Backend sent more than we ask him... (%d)",
431 abs (read - buffer_size_inter));
433 src->buffer_remain += read;
435 if (buffer != NULL) {
436 g_byte_array_free (buffer, TRUE);
440 GST_DEBUG_OBJECT (src,
441 "BYTES READ (actual) = %d, BYTES READ (cumulative) = %llu, "
442 "OFFSET = %llu, CONTENT SIZE = %llu.", read,
443 src->bytes_read, src->read_offset, src->content_size);
448 (src->buffer_remain < MAX_READ_SIZE) ? src->buffer_remain : MAX_READ_SIZE;
450 *outbuf = gst_buffer_new ();
452 /* gets the first buffer_size bytes from the byte array buffer variable */
453 /* guint8 *buf = g_memdup( src->bytes_queue->data, buffer_size ); */
455 GST_DEBUG_OBJECT (src, "read from network? %s!, buffer_remain = %d",
457 -1) ? "NO, got from buffer" : "YES, go see the backend's log file",
460 GST_BUFFER_SIZE (*outbuf) = buffer_size;
461 GST_BUFFER_MALLOCDATA (*outbuf) = g_malloc0 (GST_BUFFER_SIZE (*outbuf));
462 GST_BUFFER_DATA (*outbuf) = GST_BUFFER_MALLOCDATA (*outbuf);
463 g_memmove (GST_BUFFER_DATA ((*outbuf)), src->bytes_queue->data,
464 GST_BUFFER_SIZE (*outbuf));
465 GST_BUFFER_OFFSET (*outbuf) = src->read_offset;
466 GST_BUFFER_OFFSET_END (*outbuf) =
467 src->read_offset + GST_BUFFER_SIZE (*outbuf);
469 src->buffer_remain -= GST_BUFFER_SIZE (*outbuf);
471 src->read_offset += GST_BUFFER_SIZE (*outbuf);
472 src->bytes_read += GST_BUFFER_SIZE (*outbuf);
473 GST_DEBUG_OBJECT (src, "Buffer output with size: %d",
474 GST_BUFFER_SIZE (*outbuf));
476 /* flushs the newly buffer got from byte array */
478 g_byte_array_remove_range (src->bytes_queue, 0, buffer_size);
480 GST_DEBUG_OBJECT (src, "Got buffer: BUFFER --->SIZE = %d, OFFSET = %llu, "
481 "OFFSET_END = %llu.", GST_BUFFER_SIZE (*outbuf),
482 GST_BUFFER_OFFSET (*outbuf), GST_BUFFER_OFFSET_END (*outbuf));
484 GST_DEBUG_OBJECT (src, "CONTENT_SIZE = %llu, BYTES_READ = %llu.",
485 src->content_size, src->bytes_read);
487 if (G_UNLIKELY (src->eos) || (!src->live_tv
488 && (src->bytes_read >= src->content_size)))
493 const gchar *reason = gst_flow_get_name (ret);
495 GST_DEBUG_OBJECT (src, "DONE task, reason %s", reason);
500 const gchar *reason = gst_flow_get_name (ret);
502 GST_DEBUG_OBJECT (src, "pausing task, reason %s", reason);
503 return GST_FLOW_UNEXPECTED;
508 GST_ELEMENT_ERROR (src, RESOURCE, READ,
509 (NULL), ("Could not read any bytes (%i, %s)", read, src->uri_name));
510 return GST_FLOW_ERROR;
514 GST_ELEMENT_ERROR (src, RESOURCE, READ,
515 (NULL), ("Seek failed, go to the next program info... (%i, %s)", read,
518 gst_pad_push_event (GST_BASE_SRC_PAD (GST_BASE_SRC (psrc)),
519 gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0, -1, 0));
521 /* go to the next program chain */
522 src->unique_setup = FALSE;
523 src->update_prog_chain = TRUE;
525 gst_mythtv_src_next_program_chain (src);
527 return GST_FLOW_ERROR_NO_DATA;
533 gst_mythtv_src_get_position (GstMythtvSrc * src)
539 if (src->live_tv == TRUE && (abs (src->content_size - src->bytes_read) <
540 GMYTHTV_TRANSFER_MAX_BUFFER)) {
544 size_tmp = gmyth_recorder_get_file_position (src->spawn_livetv->recorder);
545 if (size_tmp > (src->content_size + GMYTHTV_TRANSFER_MAX_BUFFER))
546 src->content_size = size_tmp;
547 else if (size_tmp > 0 && --max_tries > 0)
549 GST_LOG_OBJECT (src, "GET_POSITION: file_position = %lld", size_tmp);
550 /* sets the last content size amount before it can be updated */
551 src->prev_content_size = src->content_size;
554 return src->content_size;
559 gst_mythtv_src_do_seek (GstBaseSrc * base, GstSegment * segment)
561 GstMythtvSrc *src = GST_MYTHTV_SRC (base);
562 gint64 new_offset = -1;
563 gint64 actual_seek = segment->start;
566 GST_LOG_OBJECT (src, "seek, segment: %" GST_SEGMENT_FORMAT, segment);
568 if (segment->format == GST_FORMAT_TIME) {
572 "Trying to seek at the value (actual_seek = %lld, read_offset = %lld)",
573 actual_seek, src->read_offset);
574 /* verify if it needs to seek */
575 if (src->read_offset != actual_seek) {
578 gmyth_file_transfer_seek (src->file_transfer, segment->start, SEEK_SET);
581 "Segment offset start = %lld, SRC Offset = %lld, NEW actual backend SEEK Offset = %lld.",
582 segment->start, src->read_offset, new_offset);
583 if (G_UNLIKELY (new_offset < 0)) {
586 goto change_progchain;
591 src->read_offset = new_offset;
594 GST_INFO_OBJECT (src, "Failed to set the SEEK on segment!");
605 GST_DEBUG_OBJECT (src, "EOS found on seeking!!!");
610 GST_ELEMENT_ERROR (src, RESOURCE, READ,
611 (NULL), ("Seek failed, go to the next program info... (%i, %s)", read,
614 gst_pad_push_event (GST_BASE_SRC_PAD (base),
615 gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0, -1, 0));
616 /* go to the next program chain */
617 src->unique_setup = FALSE;
618 src->update_prog_chain = TRUE;
620 gst_mythtv_src_next_program_chain (src);
627 /* create a socket for connecting to remote server */
629 gst_mythtv_src_start (GstBaseSrc * bsrc)
631 GstMythtvSrc *src = GST_MYTHTV_SRC (bsrc);
633 GString *chain_id_local = NULL;
634 GMythURI *gmyth_uri = NULL;
637 if (G_UNLIKELY (src->update_prog_chain))
638 goto change_progchain;
640 if (src->unique_setup == FALSE) {
641 src->unique_setup = TRUE;
646 gmyth_uri = gmyth_uri_new_with_value( src->uri_name );
648 src->backend_info = gmyth_backend_info_new_with_uri (src->uri_name);
649 src->live_tv |= gmyth_uri_is_livetv( gmyth_uri );
650 /* testing UPnP... */
651 /* gmyth_backend_info_set_hostname( src->backend_info, NULL ); */
652 if ( src->live_tv ) {
653 src->spawn_livetv = gmyth_livetv_new ();
655 gchar* ch = gmyth_uri_get_channel_name( gmyth_uri );
657 src->channel_name = ch;
659 if (src->channel_name != NULL) {
660 if (gmyth_livetv_channel_name_setup (src->spawn_livetv, src->channel_name,
661 src->backend_info) == FALSE) {
662 GST_INFO_OBJECT (src, "LiveTV setup felt down on error");
667 if (gmyth_livetv_setup (src->spawn_livetv, src->backend_info) == FALSE) {
668 GST_INFO_OBJECT (src, "LiveTV setup felt down on error");
674 /* testing change channel... */
675 /* gmyth_recorder_change_channel( src->spawn_livetv->recorder, CHANNEL_DIRECTION_UP ); */
677 src->file_transfer = gmyth_livetv_create_file_transfer (src->spawn_livetv);
679 if (NULL == src->file_transfer) {
680 GST_INFO_OBJECT (src, "[LiveTV] FileTransfer equals to NULL");
686 src->file_transfer = gmyth_file_transfer_new (src->backend_info);
688 ret = gmyth_file_transfer_open (src->file_transfer, src->uri_name);
692 if (NULL == src->file_transfer) {
693 GST_INFO_OBJECT (src, "FileTransfer is NULL");
696 /*GST_INFO_OBJECT( src, "uri = %s", src->spawn_livetv->file_transfer); */
699 #ifndef GST_DISABLE_GST_DEBUG
700 if (src->mythtv_msgs_dbg)
701 GST_INFO_OBJECT (src,
702 "MythTV FileTransfer request failed when setting up socket connection!");
704 goto begin_req_failed;
707 GST_INFO_OBJECT (src,
708 "MythTV FileTransfer filesize = %lld, content_size = %lld!",
709 src->file_transfer->filesize, src->content_size);
711 src->content_size = src->file_transfer->filesize;
713 src->do_start = FALSE;
715 /* this is used for the buffer cache */
716 src->bytes_queue = g_byte_array_sized_new (INTERNAL_BUFFER_SIZE);
717 src->buffer_remain = 0;
719 gst_pad_push_event (GST_BASE_SRC_PAD (GST_BASE_SRC (src)),
720 gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0,
721 src->content_size, 0));
724 /*if ( gmyth_uri != NULL )
726 g_object_unref( gmyth_uri );
730 if (chain_id_local != NULL) {
731 g_string_free (chain_id_local, TRUE);
732 chain_id_local = NULL;
740 if (src->spawn_livetv != NULL)
741 g_object_unref (src->spawn_livetv);
743 GST_ELEMENT_ERROR (src, LIBRARY, INIT,
744 (NULL), ("Could not initialize MythTV library (%i, %s)", ret,
750 GST_ELEMENT_ERROR (src, LIBRARY, INIT,
751 (NULL), ("Could not begin request sent to MythTV server (%i, %s)", ret,
757 GST_ELEMENT_ERROR (src, RESOURCE, READ,
758 (NULL), ("Seek failed, go to the next program info... (%s)",
761 gst_pad_push_event (GST_BASE_SRC_PAD (GST_BASE_SRC (src)),
762 gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0, -1, 0));
764 /* go to the next program chain */
765 src->unique_setup = FALSE;
766 src->update_prog_chain = TRUE;
768 gst_mythtv_src_next_program_chain (src);
774 /* create a new socket for connecting to the next program chain */
776 gst_mythtv_src_next_program_chain (GstMythtvSrc * src)
783 if (src->unique_setup == FALSE) {
784 src->unique_setup = TRUE;
789 GST_PAD_STREAM_LOCK (GST_BASE_SRC_PAD (GST_BASE_SRC (src)));
791 if (src->file_transfer) {
792 g_object_unref (src->file_transfer);
793 src->file_transfer = NULL;
797 g_free (src->uri_name);
800 if (src->backend_info == NULL)
801 src->backend_info = gmyth_backend_info_new_with_uri (src->uri_name);
804 if (gmyth_livetv_next_program_chain (src->spawn_livetv) == FALSE) {
805 GST_INFO_OBJECT (src, "Failed to go to the next program chain!");
809 /* set up the uri variable */
810 src->uri_name = g_strdup (src->spawn_livetv->proginfo->pathname->str);
812 src->file_transfer = gmyth_livetv_create_file_transfer (src->spawn_livetv);
815 src->file_transfer = gmyth_file_transfer_new (src->backend_info);
817 if (src->file_transfer == NULL) {
821 ret = gmyth_file_transfer_open (src->file_transfer, src->uri_name);
825 #ifndef GST_DISABLE_GST_DEBUG
826 if (src->mythtv_msgs_dbg)
827 GST_ERROR_OBJECT (src,
828 "MythTV FileTransfer request failed when setting up socket connection!");
830 goto begin_req_failed;
832 src->content_size_last = src->content_size;
834 src->content_size = src->file_transfer->filesize;
837 src->wait_to_transfer = 0;
838 while (src->wait_to_transfer++ < GMYTHTV_TRANSFER_MAX_WAITS &&
839 src->content_size < GMYTHTV_TRANSFER_MAX_BUFFER)
840 src->content_size = gst_mythtv_src_get_position (src);
844 src->read_offset = 0;
846 if (src->bytes_queue != NULL) {
847 g_byte_array_free (src->bytes_queue, TRUE);
850 src->bytes_queue = g_byte_array_sized_new (INTERNAL_BUFFER_SIZE);
853 src->update_prog_chain = FALSE;
855 GST_PAD_STREAM_UNLOCK (GST_BASE_SRC_PAD (GST_BASE_SRC (src)));
862 if (src->spawn_livetv != NULL)
863 g_object_unref (src->spawn_livetv);
865 GST_ELEMENT_ERROR (src, LIBRARY, INIT,
866 (NULL), ("Could not initialize MythTV library (%i, %s)", ret,
872 GST_ELEMENT_ERROR (src, LIBRARY, INIT,
873 (NULL), ("Could not begin request sent to MythTV server (%i, %s)", ret,
881 gst_mythtv_src_get_size (GstBaseSrc * bsrc, guint64 * size)
883 GstMythtvSrc *src = GST_MYTHTV_SRC (bsrc);
886 GST_LOG_OBJECT (src, "Differs from previous content size: %d (max.: %d)",
887 abs (src->content_size - src->prev_content_size),
888 GMYTHTV_TRANSFER_MAX_BUFFER);
892 } else if (src->live_tv && src->enable_timing_position
893 && (abs (src->content_size - src->bytes_read) <
894 GMYTHTV_TRANSFER_MAX_BUFFER)) {
897 gmyth_recorder_get_file_position (src->spawn_livetv->recorder);
898 if (new_offset > 0 && new_offset > src->content_size) {
899 src->content_size = new_offset;
900 } else if (new_offset < src->content_size) {
901 src->update_prog_chain = TRUE;
906 *size = src->content_size;
907 GST_LOG_OBJECT (src, "Content size = %lld", src->content_size);
913 /* close the socket and associated resources
914 * used both to recover from errors and go to NULL state */
916 gst_mythtv_src_stop (GstBaseSrc * bsrc)
920 src = GST_MYTHTV_SRC (bsrc);
923 g_free (src->uri_name);
924 src->uri_name = NULL;
927 if (src->mythtv_caps) {
928 gst_caps_unref (src->mythtv_caps);
929 src->mythtv_caps = NULL;
938 gst_mythtv_src_handle_event (GstPad * pad, GstEvent * event)
940 GstMythtvSrc *src = GST_MYTHTV_SRC (GST_PAD_PARENT (pad));
941 gint64 cont_size = 0;
942 gboolean ret = FALSE;
944 switch (GST_EVENT_TYPE (event)) {
946 GST_WARNING_OBJECT (src, "Got EOS event");
949 cont_size = gst_mythtv_src_get_position (src);
950 if (cont_size > src->content_size) {
951 src->content_size = cont_size;
955 gst_element_set_state (GST_ELEMENT (src), GST_STATE_NULL);
956 gst_element_set_locked_state (GST_ELEMENT (src), FALSE);
961 ret = gst_pad_event_default (pad, event);
968 gst_mythtv_src_is_seekable (GstBaseSrc * push_src)
974 gst_mythtv_src_handle_query (GstPad * pad, GstQuery * query)
976 gboolean res = FALSE;
977 GstMythtvSrc *myth = GST_MYTHTV_SRC (gst_pad_get_parent (pad));
980 switch (GST_QUERY_TYPE (query)) {
981 case GST_QUERY_POSITION:
983 gst_query_parse_position (query, &formt, NULL);
984 if (formt == GST_FORMAT_BYTES) {
985 gst_query_set_position (query, formt, myth->read_offset);
986 GST_DEBUG_OBJECT (myth, "POS %" G_GINT64_FORMAT, myth->read_offset);
988 } else if (formt == GST_FORMAT_TIME) {
989 res = gst_pad_query_default (pad, query);
993 case GST_QUERY_DURATION:
996 if (myth->duration != 0) {
1000 fps = nuv->h->i_fpsn / nuv->h->i_fpsd;
1002 gst_util_uint64_scale_int (GST_SECOND, nuv->h->i_video_blocks, fps);
1006 gst_query_parse_duration (query, &formt, NULL);
1007 if (formt == GST_FORMAT_BYTES) {
1008 gst_query_set_duration (query, formt, myth->content_size);
1009 GST_DEBUG_OBJECT (myth, "SIZE %" G_GINT64_FORMAT, myth->content_size);
1011 } else if (formt == GST_FORMAT_TIME) {
1012 res = gst_pad_query_default (pad, query);
1018 res = gst_pad_query_default (pad, query);
1023 gst_object_unref (myth);
1028 static GstStateChangeReturn
1029 gst_mythtv_src_change_state (GstElement * element, GstStateChange transition)
1031 GstStateChangeReturn ret = GST_STATE_CHANGE_FAILURE;
1032 GstMythtvSrc *src = GST_MYTHTV_SRC (element);
1034 switch (transition) {
1035 case GST_STATE_CHANGE_NULL_TO_READY:
1037 case GST_STATE_CHANGE_READY_TO_PAUSED:
1038 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1040 if (!gmyth_recorder_send_frontend_ready_command (src->spawn_livetv->
1042 GST_WARNING_OBJECT (src,
1043 "Couldn't send the FRONTEND_READY message to the backend!");
1045 GST_DEBUG_OBJECT (src, "FRONTEND_READY was sent to the backend");
1052 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1053 if (ret == GST_STATE_CHANGE_FAILURE)
1056 switch (transition) {
1057 case GST_STATE_CHANGE_READY_TO_NULL:
1059 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1060 case GST_STATE_CHANGE_PAUSED_TO_READY:
1070 gst_mythtv_src_set_property (GObject * object, guint prop_id,
1071 const GValue * value, GParamSpec * pspec)
1073 GstMythtvSrc *mythtvsrc = GST_MYTHTV_SRC (object);
1075 GST_OBJECT_LOCK (mythtvsrc);
1079 if (!g_value_get_string (value)) {
1080 GST_WARNING ("location property cannot be NULL");
1084 if (mythtvsrc->uri_name != NULL) {
1085 g_free (mythtvsrc->uri_name);
1086 mythtvsrc->uri_name = NULL;
1088 mythtvsrc->uri_name = g_value_dup_string (value);
1092 #ifndef GST_DISABLE_GST_DEBUG
1093 case PROP_GMYTHTV_DBG:
1095 mythtvsrc->mythtv_msgs_dbg = g_value_get_boolean (value);
1099 case PROP_GMYTHTV_VERSION:
1101 mythtvsrc->mythtv_version = g_value_get_int (value);
1104 case PROP_GMYTHTV_LIVEID:
1106 mythtvsrc->live_tv_id = g_value_get_int (value);
1109 case PROP_GMYTHTV_LIVE:
1111 mythtvsrc->live_tv = g_value_get_boolean (value);
1114 case PROP_GMYTHTV_ENABLE_TIMING_POSITION:
1116 mythtvsrc->enable_timing_position = g_value_get_boolean (value);
1119 case PROP_GMYTHTV_LIVE_CHAINID:
1121 if (!g_value_get_string (value)) {
1122 GST_WARNING ("MythTV Live chainid property cannot be NULL");
1126 if (mythtvsrc->live_chain_id != NULL) {
1127 g_free (mythtvsrc->live_chain_id);
1128 mythtvsrc->live_chain_id = NULL;
1130 mythtvsrc->live_chain_id = g_value_dup_string (value);
1133 case PROP_GMYTHTV_CHANNEL_NUM:
1135 mythtvsrc->channel_name = g_value_dup_string (value);
1139 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1143 GST_OBJECT_UNLOCK (mythtvsrc);
1147 gst_mythtv_src_get_property (GObject * object, guint prop_id,
1148 GValue * value, GParamSpec * pspec)
1150 GstMythtvSrc *mythtvsrc = GST_MYTHTV_SRC (object);
1152 GST_OBJECT_LOCK (mythtvsrc);
1156 gchar *str = g_strdup ("");
1158 if (mythtvsrc->uri_name == NULL) {
1159 g_free (mythtvsrc->uri_name);
1160 mythtvsrc->uri_name = NULL;
1162 str = g_strdup (mythtvsrc->uri_name);
1164 g_value_set_string (value, str);
1167 #ifndef GST_DISABLE_GST_DEBUG
1168 case PROP_GMYTHTV_DBG:
1169 g_value_set_boolean (value, mythtvsrc->mythtv_msgs_dbg);
1172 case PROP_GMYTHTV_VERSION:
1174 g_value_set_int (value, mythtvsrc->mythtv_version);
1177 case PROP_GMYTHTV_LIVEID:
1179 g_value_set_int (value, mythtvsrc->live_tv_id);
1182 case PROP_GMYTHTV_LIVE:
1183 g_value_set_boolean (value, mythtvsrc->live_tv);
1185 case PROP_GMYTHTV_ENABLE_TIMING_POSITION:
1186 g_value_set_boolean (value, mythtvsrc->enable_timing_position);
1188 case PROP_GMYTHTV_LIVE_CHAINID:
1190 gchar *str = g_strdup ("");
1192 if (mythtvsrc->live_chain_id == NULL) {
1193 g_free (mythtvsrc->live_chain_id);
1194 mythtvsrc->live_chain_id = NULL;
1196 str = g_strdup (mythtvsrc->live_chain_id);
1198 g_value_set_string (value, str);
1201 case PROP_GMYTHTV_CHANNEL_NUM:
1203 g_value_set_string (value, mythtvsrc->channel_name);
1207 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1210 GST_OBJECT_UNLOCK (mythtvsrc);
1214 plugin_init (GstPlugin * plugin)
1216 return gst_element_register (plugin, "mythtvsrc", GST_RANK_NONE,
1217 GST_TYPE_MYTHTV_SRC);
1220 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1224 plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN);
1227 /*** GSTURIHANDLER INTERFACE *************************************************/
1229 gst_mythtv_src_uri_get_type (void)
1235 gst_mythtv_src_uri_get_protocols (void)
1237 static gchar *protocols[] = { "myth", "myths", NULL };
1242 static const gchar *
1243 gst_mythtv_src_uri_get_uri (GstURIHandler * handler)
1245 GstMythtvSrc *src = GST_MYTHTV_SRC (handler);
1247 return src->uri_name;
1251 gst_mythtv_src_uri_set_uri (GstURIHandler * handler, const gchar * uri)
1253 GstMythtvSrc *src = GST_MYTHTV_SRC (handler);
1257 protocol = gst_uri_get_protocol (uri);
1258 if ((strcmp (protocol, "myth") != 0) && (strcmp (protocol, "myths") != 0)) {
1263 g_object_set (src, "location", uri, NULL);
1269 gst_mythtv_src_uri_handler_init (gpointer g_iface, gpointer iface_data)
1271 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
1273 iface->get_type = gst_mythtv_src_uri_get_type;
1274 iface->get_protocols = gst_mythtv_src_uri_get_protocols;
1275 iface->get_uri = gst_mythtv_src_uri_get_uri;
1276 iface->set_uri = gst_mythtv_src_uri_set_uri;
1280 size_header_handler (void *userdata, const char *value)
1282 GstMythtvSrc *src = GST_MYTHTV_SRC (userdata);
1284 GST_DEBUG_OBJECT (src, "content size = %lld bytes", src->content_size);