[svn r498] Added function to get the current frame rate.
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 * If you want to get the LiveTV content (set channel, TV tuner, RemoteEncoder,
22 * put the following URI:
24 * myth://xxx.xxx.xxx.xxx:6543/livetv?channel=BBC
26 * This URI will say to the gmyth library to configure the Recorder instance (used to
27 * change the channel, start the TV multimedia content transmition, etc.), using
28 * the IP address (xxx.xxx.xxx.xxx) and port number (6543) of the MythTV backend
29 * server, and setting the channel name to "BBC".
31 * To get a already recorded the MythTV NUV file, put the following URI:
33 * myth://xxx.xxx.xxx.xxx:6543/filename.nuv
35 * This URI will say to the gmyth library to configure the Recorder instance (used to
36 * change the channel, start the TV multimedia content transmition, etc.), using
37 * the IP address (xxx.xxx.xxx.xxx) and port number (6543) of the MythTV backend
38 * server, and setting the channel name to "BBC".
40 * Another possible way to use the LiveTV content, and just in the case you want to
41 * use the mysql database, put the location URI in the following format:
43 * myth://mythtv:mythtv@xxx.xxx.xxx.xxx:6543/?mythconverg&channel=9
45 * Where the first field is the protocol (myth), the second and third are user
46 * name (mythtv) and password (mythtv), then backend host name and port number,
47 * and the last field is the database name (mythconverg).
54 #include "gstmythtvsrc.h"
55 #include <gmyth/gmyth_file_transfer.h>
56 #include <gmyth/gmyth_livetv.h>
58 #include <gmyth/gmyth_socket.h>
59 #include <gmyth/gmyth_tvchain.h>
64 GST_DEBUG_CATEGORY_STATIC (mythtvsrc_debug);
65 #define GST_CAT_DEFAULT mythtvsrc_debug
67 #define GST_GMYTHTV_ID_NUM 1
69 #define GST_GMYTHTV_CHANNEL_DEFAULT_NUM (-1)
71 #define GMYTHTV_VERSION_DEFAULT 30
73 #define GMYTHTV_TRANSFER_MAX_WAITS 100
75 #define GMYTHTV_TRANSFER_MAX_RESENDS 2
77 #define GMYTHTV_TRANSFER_MAX_BUFFER (128*1024)
79 #define MAX_READ_SIZE (4*1024)
81 #define GST_FLOW_ERROR_NO_DATA (-101)
83 #define REQUEST_MAX_SIZE (64*1024)
85 #define INTERNAL_BUFFER_SIZE (90*1024)
87 static const GstElementDetails gst_mythtv_src_details =
88 GST_ELEMENT_DETAILS ("MythTV client source",
90 "Control and receive data as a client over the network "
91 "via raw socket connections using the MythTV protocol",
92 "Rosfran Borges <rosfran.borges@indt.org.br>");
94 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
97 GST_STATIC_CAPS ("video/x-nuv"));
103 #ifndef GST_DISABLE_GST_DEBUG
106 PROP_GMYTHTV_VERSION,
109 PROP_GMYTHTV_LIVE_CHAINID,
110 PROP_GMYTHTV_ENABLE_TIMING_POSITION,
111 PROP_GMYTHTV_CHANNEL_NUM
114 static void gst_mythtv_src_finalize (GObject * gobject);
116 static GstFlowReturn gst_mythtv_src_create (GstPushSrc * psrc,
117 GstBuffer ** outbuf);
119 static gboolean gst_mythtv_src_start (GstBaseSrc * bsrc);
120 static gboolean gst_mythtv_src_stop (GstBaseSrc * bsrc);
121 static gboolean gst_mythtv_src_get_size (GstBaseSrc * bsrc, guint64 * size);
122 static gboolean gst_mythtv_src_is_seekable (GstBaseSrc * push_src);
124 static gboolean gst_mythtv_src_do_seek (GstBaseSrc * base,
125 GstSegment * segment);
127 static GstStateChangeReturn
128 gst_mythtv_src_change_state (GstElement * element, GstStateChange transition);
130 static void gst_mythtv_src_set_property (GObject * object, guint prop_id,
131 const GValue * value, GParamSpec * pspec);
132 static void gst_mythtv_src_get_property (GObject * object, guint prop_id,
133 GValue * value, GParamSpec * pspec);
135 static void gst_mythtv_src_uri_handler_init (gpointer g_iface,
136 gpointer iface_data);
138 static gboolean gst_mythtv_src_handle_query (GstPad * pad, GstQuery * query);
140 static gboolean gst_mythtv_src_handle_event (GstPad * pad, GstEvent * event);
142 static gint do_read_request_response (GstMythtvSrc * src, guint size,
143 GByteArray * data_ptr);
146 _urihandler_init (GType type)
148 static const GInterfaceInfo urihandler_info = {
149 gst_mythtv_src_uri_handler_init,
154 g_type_add_interface_static (type, GST_TYPE_URI_HANDLER, &urihandler_info);
156 GST_DEBUG_CATEGORY_INIT (mythtvsrc_debug, "mythtvsrc", 0, "MythTV src");
159 GST_BOILERPLATE_FULL (GstMythtvSrc, gst_mythtv_src, GstPushSrc,
160 GST_TYPE_PUSH_SRC, _urihandler_init)
162 static void gst_mythtv_src_base_init (gpointer g_class)
164 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
166 gst_element_class_add_pad_template (element_class,
167 gst_static_pad_template_get (&srctemplate));
169 gst_element_class_set_details (element_class, &gst_mythtv_src_details);
171 element_class->change_state = gst_mythtv_src_change_state;
176 gst_mythtv_src_class_init (GstMythtvSrcClass * klass)
178 GObjectClass *gobject_class;
179 GstPushSrcClass *gstpushsrc_class;
180 GstBaseSrcClass *gstbasesrc_class;
182 gobject_class = (GObjectClass *) klass;
183 gstbasesrc_class = (GstBaseSrcClass *) klass;
184 gstpushsrc_class = (GstPushSrcClass *) klass;
186 gobject_class->set_property = gst_mythtv_src_set_property;
187 gobject_class->get_property = gst_mythtv_src_get_property;
188 gobject_class->finalize = gst_mythtv_src_finalize;
190 g_object_class_install_property
191 (gobject_class, PROP_LOCATION,
192 g_param_spec_string ("location", "Location",
193 "The location. In the form:"
194 "\n\t\t\tmyth://a.com/file.nuv"
195 "\n\t\t\tmyth://a.com:23223/file.nuv "
196 "\n\t\t\ta.com/file.nuv - default scheme 'myth'",
197 "", G_PARAM_READWRITE));
199 g_object_class_install_property
200 (gobject_class, PROP_GMYTHTV_VERSION,
201 g_param_spec_int ("mythtv-version", "mythtv-version",
202 "Change MythTV version", 26, 30, 26, G_PARAM_READWRITE));
204 g_object_class_install_property
205 (gobject_class, PROP_GMYTHTV_LIVEID,
206 g_param_spec_int ("mythtv-live-id", "mythtv-live-id",
207 "Change MythTV version",
208 0, 200, GST_GMYTHTV_ID_NUM, G_PARAM_READWRITE));
210 g_object_class_install_property
211 (gobject_class, PROP_GMYTHTV_LIVE_CHAINID,
212 g_param_spec_string ("mythtv-live-chainid", "mythtv-live-chainid",
213 "Sets the MythTV chain ID (from TV Chain)", "", G_PARAM_READWRITE));
215 g_object_class_install_property
216 (gobject_class, PROP_GMYTHTV_LIVE,
217 g_param_spec_boolean ("mythtv-live", "mythtv-live",
218 "Enable MythTV Live TV content streaming", FALSE, G_PARAM_READWRITE));
220 g_object_class_install_property
221 (gobject_class, PROP_GMYTHTV_ENABLE_TIMING_POSITION,
222 g_param_spec_boolean ("mythtv-enable-timing-position",
223 "mythtv-enable-timing-position",
224 "Enable MythTV Live TV content size continuous updating", FALSE,
227 g_object_class_install_property
228 (gobject_class, PROP_GMYTHTV_CHANNEL_NUM,
229 g_param_spec_string ("mythtv-channel", "mythtv-channel",
230 "Change MythTV channel number",
231 "", G_PARAM_READWRITE));
233 #ifndef GST_DISABLE_GST_DEBUG
234 g_object_class_install_property
235 (gobject_class, PROP_GMYTHTV_DBG,
236 g_param_spec_boolean ("mythtv-debug", "mythtv-debug",
237 "Enable MythTV debug messages", FALSE, G_PARAM_READWRITE));
240 gstbasesrc_class->start = gst_mythtv_src_start;
241 gstbasesrc_class->stop = gst_mythtv_src_stop;
242 gstbasesrc_class->get_size = gst_mythtv_src_get_size;
243 gstbasesrc_class->is_seekable = gst_mythtv_src_is_seekable;
245 gstbasesrc_class->do_seek = gst_mythtv_src_do_seek;
246 gstpushsrc_class->create = gst_mythtv_src_create;
248 GST_DEBUG_CATEGORY_INIT (mythtvsrc_debug, "mythtvsrc", 0,
249 "MythTV Client Source");
253 gst_mythtv_src_init (GstMythtvSrc * this, GstMythtvSrcClass * g_class)
255 this->file_transfer = NULL;
257 this->unique_setup = FALSE;
259 this->mythtv_version = GMYTHTV_VERSION_DEFAULT;
261 this->state = GST_MYTHTV_SRC_FILE_TRANSFER;
263 this->bytes_read = 0;
265 this->prev_content_size = 0;
267 this->content_size = 0;
268 this->read_offset = 0;
270 this->content_size_last = 0;
272 this->live_tv = FALSE;
274 this->enable_timing_position = FALSE;
275 this->update_prog_chain = FALSE;
277 this->user_agent = g_strdup ("mythtvsrc");
278 this->update_prog_chain = FALSE;
280 this->channel_name = NULL;
284 this->bytes_queue = NULL;
286 this->wait_to_transfer = 0;
288 gst_base_src_set_format (GST_BASE_SRC (this), GST_FORMAT_BYTES);
290 gst_pad_set_event_function (GST_BASE_SRC_PAD (GST_BASE_SRC (this)),
291 gst_mythtv_src_handle_event);
292 gst_pad_set_query_function (GST_BASE_SRC_PAD (GST_BASE_SRC (this)),
293 gst_mythtv_src_handle_query);
298 gst_mythtv_src_finalize (GObject * gobject)
300 GstMythtvSrc *this = GST_MYTHTV_SRC (gobject);
302 if (this->spawn_livetv) {
303 g_object_unref (this->spawn_livetv);
304 this->spawn_livetv = NULL;
307 if (this->file_transfer) {
308 g_object_unref (this->file_transfer);
309 this->file_transfer = NULL;
312 if (this->backend_info) {
313 g_object_unref (this->backend_info);
314 this->backend_info = NULL;
317 if (this->uri_name) {
318 g_free (this->uri_name);
321 if (this->user_agent) {
322 g_free (this->user_agent);
325 if (this->bytes_queue) {
326 g_byte_array_free (this->bytes_queue, TRUE);
327 this->bytes_queue = NULL;
330 G_OBJECT_CLASS (parent_class)->finalize (gobject);
334 do_read_request_response (GstMythtvSrc * src, guint size, GByteArray * data_ptr)
337 guint sizetoread = size;
338 gint max_iters = GMYTHTV_TRANSFER_MAX_RESENDS;
340 GST_LOG_OBJECT (src, "Starting: Reading %d bytes...", sizetoread);
342 /* Loop sending the Myth File Transfer request:
343 * Retry whilst authentication fails and we supply it. */
346 while (sizetoread == size && --max_iters > 0) {
348 len = gmyth_file_transfer_read (src->file_transfer,
349 data_ptr, sizetoread, src->live_tv);
354 } else if (len < 0) {
356 if (src->live_tv == FALSE) {
360 if (len == GMYTHTV_FILE_TRANSFER_READ_ERROR) { /* -314 */
361 GST_INFO_OBJECT (src, "[LiveTV] FileTransfer READ_ERROR!");
363 } else if (len == GMYTHTV_FILE_TRANSFER_NEXT_PROG_CHAIN) { /* -315 */
364 GST_INFO_OBJECT (src,
365 "[LiveTV] FileTransfer - Go to the next program chain!");
374 if (read == sizetoread)
378 if ((read < 0 && !src->live_tv) || max_iters == 0)
391 gst_mythtv_src_create (GstPushSrc * psrc, GstBuffer ** outbuf)
394 GstFlowReturn ret = GST_FLOW_OK;
397 src = GST_MYTHTV_SRC (psrc);
399 /* The caller should know the number of bytes and not read beyond EOS. */
400 if (G_UNLIKELY (src->eos))
402 if (G_UNLIKELY (src->update_prog_chain))
403 goto change_progchain;
405 GST_DEBUG_OBJECT (src, "offset = %" G_GUINT64_FORMAT ", size = %d...",
406 src->read_offset, MAX_READ_SIZE);
408 GST_DEBUG_OBJECT (src, "Create: buffer_remain: %d, buffer_size = %d.",
409 (gint) src->buffer_remain, src->bytes_queue->len);
411 program_chain_changed:
412 /* just get from the byte array, no network effort... */
413 if ((src->buffer_remain = src->bytes_queue->len) < MAX_READ_SIZE) {
417 guint buffer_size_inter = (INTERNAL_BUFFER_SIZE - src->buffer_remain);
419 if (buffer_size_inter > REQUEST_MAX_SIZE)
420 buffer_size_inter = REQUEST_MAX_SIZE;
422 buffer = g_byte_array_new ();
424 read = do_read_request_response (src, buffer_size_inter, buffer);
426 if (G_UNLIKELY (read < 0)) {
428 if (buffer != NULL) {
429 g_byte_array_free (buffer, TRUE);
433 if (src->live_tv || ( read == GMYTHTV_FILE_TRANSFER_NEXT_PROG_CHAIN ))
434 goto change_progchain;
437 } else if (G_UNLIKELY (read == 0)) {
439 if (buffer != NULL) {
440 g_byte_array_free (buffer, TRUE);
447 goto program_chain_changed;
450 if (G_UNLIKELY (src->update_prog_chain))
452 if (buffer != NULL) {
453 g_byte_array_free (buffer, TRUE);
456 goto change_progchain;
460 g_byte_array_append (src->bytes_queue, buffer->data, read);
461 if (read > buffer_size_inter)
462 GST_WARNING_OBJECT (src,
463 "INCREASED buffer size! Backend sent more than we ask him... (%d)",
464 abs (read - buffer_size_inter));
466 src->buffer_remain += read;
468 if (buffer != NULL) {
469 g_byte_array_free (buffer, TRUE);
473 GST_DEBUG_OBJECT (src,
474 "BYTES READ (actual) = %d, BYTES READ (cumulative) = %llu, "
475 "OFFSET = %llu, CONTENT SIZE = %llu.", read,
476 src->bytes_read, src->read_offset, src->content_size);
481 (src->buffer_remain < MAX_READ_SIZE) ? src->buffer_remain : MAX_READ_SIZE;
483 *outbuf = gst_buffer_new ();
485 /* gets the first buffer_size bytes from the byte array buffer variable */
486 /* guint8 *buf = g_memdup( src->bytes_queue->data, buffer_size ); */
488 GST_DEBUG_OBJECT (src, "read from network? %s!, buffer_remain = %d",
490 -1) ? "NO, got from buffer" : "YES, go see the backend's log file",
493 GST_BUFFER_SIZE (*outbuf) = buffer_size;
494 GST_BUFFER_MALLOCDATA (*outbuf) = g_malloc0 (GST_BUFFER_SIZE (*outbuf));
495 GST_BUFFER_DATA (*outbuf) = GST_BUFFER_MALLOCDATA (*outbuf);
496 g_memmove (GST_BUFFER_DATA ((*outbuf)), src->bytes_queue->data,
497 GST_BUFFER_SIZE (*outbuf));
498 GST_BUFFER_OFFSET (*outbuf) = src->read_offset;
499 GST_BUFFER_OFFSET_END (*outbuf) =
500 src->read_offset + GST_BUFFER_SIZE (*outbuf);
502 src->buffer_remain -= GST_BUFFER_SIZE (*outbuf);
504 src->read_offset += GST_BUFFER_SIZE (*outbuf);
505 src->bytes_read += GST_BUFFER_SIZE (*outbuf);
506 GST_DEBUG_OBJECT (src, "Buffer output with size: %d",
507 GST_BUFFER_SIZE (*outbuf));
509 /* flushs the newly buffer got from byte array */
511 g_byte_array_remove_range (src->bytes_queue, 0, buffer_size);
513 GST_DEBUG_OBJECT ( src, "Got buffer: BUFFER --->SIZE = %d, OFFSET = %llu, "
514 "OFFSET_END = %llu.", GST_BUFFER_SIZE (*outbuf),
515 GST_BUFFER_OFFSET (*outbuf), GST_BUFFER_OFFSET_END (*outbuf) );
517 GST_DEBUG_OBJECT (src, "CONTENT_SIZE = %llu, BYTES_READ = %llu.",
518 src->content_size, src->bytes_read);
520 if ( G_UNLIKELY (src->eos) || ( !src->live_tv
521 && ( src->bytes_read >= src->content_size ) ) )
526 const gchar *reason = gst_flow_get_name (ret);
528 GST_DEBUG_OBJECT (src, "DONE task, reason %s", reason);
533 const gchar *reason = gst_flow_get_name (ret);
535 GST_DEBUG_OBJECT (src, "pausing task, reason %s", reason);
536 return GST_FLOW_UNEXPECTED;
541 GST_ELEMENT_ERROR (src, RESOURCE, READ,
542 (NULL), ("Could not read any bytes (%i, %s)", read, src->uri_name));
543 return GST_FLOW_ERROR;
547 GST_ELEMENT_ERROR (src, RESOURCE, READ,
548 (NULL), ("Seek failed, go to the next program info... (%i, %s)", read,
552 TODO: need to send a new segment event to NUVDemux?
553 gst_pad_push_event (GST_BASE_SRC_PAD (GST_BASE_SRC (psrc)),
554 gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0, -1, 0));
557 goto program_chain_changed;
563 gst_mythtv_src_get_position (GstMythtvSrc * src)
569 if (src->live_tv == TRUE && (abs (src->content_size - src->bytes_read) <
570 GMYTHTV_TRANSFER_MAX_BUFFER)) {
574 size_tmp = gmyth_recorder_get_file_position (src->spawn_livetv->recorder);
575 if (size_tmp > (src->content_size + GMYTHTV_TRANSFER_MAX_BUFFER))
576 src->content_size = size_tmp;
577 else if (size_tmp > 0 && --max_tries > 0)
579 GST_LOG_OBJECT (src, "GET_POSITION: file_position = %lld", size_tmp);
580 /* sets the last content size amount before it can be updated */
581 src->prev_content_size = src->content_size;
584 return src->content_size;
589 gst_mythtv_src_do_seek (GstBaseSrc * base, GstSegment * segment)
591 GstMythtvSrc *src = GST_MYTHTV_SRC (base);
592 gint64 new_offset = -1;
593 gint64 actual_seek = segment->start;
596 GST_LOG_OBJECT (src, "seek, segment: %" GST_SEGMENT_FORMAT, segment);
598 if (segment->format == GST_FORMAT_TIME) {
602 "Trying to seek at the value (actual_seek = %lld, read_offset = %lld)",
603 actual_seek, src->read_offset);
604 /* verify if it needs to seek */
605 if (src->read_offset != actual_seek) {
608 gmyth_file_transfer_seek (src->file_transfer, segment->start, SEEK_SET);
611 "Segment offset start = %lld, SRC Offset = %lld, NEW actual backend SEEK Offset = %lld.",
612 segment->start, src->read_offset, new_offset);
613 if (G_UNLIKELY (new_offset < 0)) {
619 src->read_offset = new_offset;
622 GST_INFO_OBJECT (src, "Failed to set the SEEK on segment!");
633 GST_DEBUG_OBJECT (src, "EOS found on seeking!!!");
639 /* create a socket for connecting to remote server */
641 gst_mythtv_src_start (GstBaseSrc * bsrc)
643 GstMythtvSrc *src = GST_MYTHTV_SRC (bsrc);
645 GString *chain_id_local = NULL;
646 GMythURI *gmyth_uri = NULL;
649 if (src->unique_setup == FALSE) {
650 src->unique_setup = TRUE;
655 gmyth_uri = gmyth_uri_new_with_value( src->uri_name );
657 src->backend_info = gmyth_backend_info_new_with_uri (src->uri_name);
658 src->live_tv |= gmyth_uri_is_livetv( gmyth_uri );
659 /* testing UPnP... */
660 /* gmyth_backend_info_set_hostname( src->backend_info, NULL ); */
661 if ( src->live_tv ) {
662 src->spawn_livetv = gmyth_livetv_new (src->backend_info);
664 gchar* ch = gmyth_uri_get_channel_name( gmyth_uri );
666 src->channel_name = ch;
668 if (src->channel_name != NULL) {
669 if (gmyth_livetv_channel_name_setup (src->spawn_livetv, src->channel_name) == FALSE) {
670 GST_INFO_OBJECT (src, "LiveTV setup felt down on error");
675 if (gmyth_livetv_setup (src->spawn_livetv) == FALSE) {
676 GST_INFO_OBJECT (src, "LiveTV setup felt down on error");
682 /* testing change channel... */
683 /* gmyth_recorder_change_channel( src->spawn_livetv->recorder, CHANNEL_DIRECTION_UP ); */
685 src->file_transfer = gmyth_livetv_create_file_transfer (src->spawn_livetv);
687 if (NULL == src->file_transfer) {
688 GST_INFO_OBJECT (src, "[LiveTV] FileTransfer equals to NULL");
693 if ( !gmyth_file_transfer_open( src->file_transfer, src->spawn_livetv->uri != NULL ?
694 gmyth_uri_get_path(src->spawn_livetv->uri) :
695 src->spawn_livetv->proginfo->pathname->str ) )
697 GST_INFO_OBJECT (src, "Error: couldn't open the FileTransfer from LiveTV source!" );
698 g_object_unref( src->file_transfer );
699 src->file_transfer = NULL;
704 src->file_transfer = gmyth_file_transfer_new (src->backend_info);
706 ret = gmyth_file_transfer_open (src->file_transfer, src->uri_name);
710 if (NULL == src->file_transfer) {
711 GST_INFO_OBJECT (src, "FileTransfer is NULL");
714 /*GST_INFO_OBJECT( src, "uri = %s", src->spawn_livetv->file_transfer); */
717 #ifndef GST_DISABLE_GST_DEBUG
718 if (src->mythtv_msgs_dbg)
719 GST_INFO_OBJECT (src,
720 "MythTV FileTransfer request failed when setting up socket connection!");
722 goto begin_req_failed;
725 GST_INFO_OBJECT (src,
726 "MythTV FileTransfer filesize = %lld, content_size = %lld!",
727 src->file_transfer->filesize, src->content_size);
729 src->content_size = src->file_transfer->filesize;
731 src->do_start = FALSE;
733 /* this is used for the buffer cache */
734 src->bytes_queue = g_byte_array_sized_new (INTERNAL_BUFFER_SIZE);
735 src->buffer_remain = 0;
737 gst_pad_push_event (GST_BASE_SRC_PAD (GST_BASE_SRC (src)),
738 gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0,
739 src->content_size, 0));
742 /*if ( gmyth_uri != NULL )
744 g_object_unref( gmyth_uri );
748 if (chain_id_local != NULL) {
749 g_string_free (chain_id_local, TRUE);
750 chain_id_local = NULL;
758 if (src->spawn_livetv != NULL)
759 g_object_unref (src->spawn_livetv);
761 GST_ELEMENT_ERROR (src, LIBRARY, INIT,
762 (NULL), ("Could not initialize MythTV library (%i, %s)", ret,
768 GST_ELEMENT_ERROR (src, LIBRARY, INIT,
769 (NULL), ("Could not begin request sent to MythTV server (%i, %s)", ret,
777 gst_mythtv_src_get_size (GstBaseSrc * bsrc, guint64 * size)
779 GstMythtvSrc *src = GST_MYTHTV_SRC (bsrc);
782 GST_LOG_OBJECT (src, "Differs from previous content size: %d (max.: %d)",
783 abs (src->content_size - src->prev_content_size),
784 GMYTHTV_TRANSFER_MAX_BUFFER);
788 } else if (src->live_tv && src->enable_timing_position
789 && (abs (src->content_size - src->bytes_read) <
790 GMYTHTV_TRANSFER_MAX_BUFFER)) {
793 gmyth_recorder_get_file_position (src->spawn_livetv->recorder);
794 if (new_offset > 0 && new_offset > src->content_size) {
795 src->content_size = new_offset;
796 } else if (new_offset < src->content_size) {
797 src->update_prog_chain = TRUE;
802 *size = src->content_size;
803 GST_LOG_OBJECT (src, "Content size = %lld", src->content_size);
809 /* close the socket and associated resources
810 * used both to recover from errors and go to NULL state */
812 gst_mythtv_src_stop (GstBaseSrc * bsrc)
816 src = GST_MYTHTV_SRC (bsrc);
819 g_free (src->uri_name);
820 src->uri_name = NULL;
829 gst_mythtv_src_handle_event (GstPad * pad, GstEvent * event)
831 GstMythtvSrc *src = GST_MYTHTV_SRC (GST_PAD_PARENT (pad));
832 gint64 cont_size = 0;
833 gboolean ret = FALSE;
835 switch (GST_EVENT_TYPE (event)) {
837 GST_WARNING_OBJECT (src, "Got EOS event");
840 cont_size = gst_mythtv_src_get_position (src);
841 if (cont_size > src->content_size) {
842 src->content_size = cont_size;
846 gst_element_set_state (GST_ELEMENT (src), GST_STATE_NULL);
847 gst_element_set_locked_state (GST_ELEMENT (src), FALSE);
852 ret = gst_pad_event_default (pad, event);
859 gst_mythtv_src_is_seekable (GstBaseSrc * push_src)
865 gst_mythtv_src_handle_query (GstPad * pad, GstQuery * query)
867 gboolean res = FALSE;
868 GstMythtvSrc *myth = GST_MYTHTV_SRC (gst_pad_get_parent (pad));
871 switch (GST_QUERY_TYPE (query)) {
872 case GST_QUERY_POSITION:
874 gst_query_parse_position (query, &formt, NULL);
875 if (formt == GST_FORMAT_BYTES) {
876 gst_query_set_position (query, formt, myth->read_offset);
877 GST_DEBUG_OBJECT (myth, "POS %" G_GINT64_FORMAT, myth->read_offset);
879 } else if (formt == GST_FORMAT_TIME) {
880 res = gst_pad_query_default (pad, query);
884 case GST_QUERY_DURATION:
887 if (myth->duration != 0) {
891 fps = nuv->h->i_fpsn / nuv->h->i_fpsd;
893 gst_util_uint64_scale_int (GST_SECOND, nuv->h->i_video_blocks, fps);
897 gst_query_parse_duration (query, &formt, NULL);
898 if (formt == GST_FORMAT_BYTES) {
899 gst_query_set_duration (query, formt, myth->content_size);
900 GST_DEBUG_OBJECT (myth, "SIZE %" G_GINT64_FORMAT, myth->content_size);
902 } else if (formt == GST_FORMAT_TIME) {
903 res = gst_pad_query_default (pad, query);
909 res = gst_pad_query_default (pad, query);
914 gst_object_unref (myth);
919 static GstStateChangeReturn
920 gst_mythtv_src_change_state (GstElement * element, GstStateChange transition)
922 GstStateChangeReturn ret = GST_STATE_CHANGE_FAILURE;
923 GstMythtvSrc *src = GST_MYTHTV_SRC (element);
925 switch (transition) {
926 case GST_STATE_CHANGE_NULL_TO_READY:
928 case GST_STATE_CHANGE_READY_TO_PAUSED:
929 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
931 if (!gmyth_recorder_send_frontend_ready_command (src->spawn_livetv->
933 GST_WARNING_OBJECT (src,
934 "Couldn't send the FRONTEND_READY message to the backend!");
936 GST_DEBUG_OBJECT (src, "FRONTEND_READY was sent to the backend");
943 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
944 if (ret == GST_STATE_CHANGE_FAILURE)
947 switch (transition) {
948 case GST_STATE_CHANGE_READY_TO_NULL:
950 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
951 case GST_STATE_CHANGE_PAUSED_TO_READY:
961 gst_mythtv_src_set_property (GObject * object, guint prop_id,
962 const GValue * value, GParamSpec * pspec)
964 GstMythtvSrc *mythtvsrc = GST_MYTHTV_SRC (object);
966 GST_OBJECT_LOCK (mythtvsrc);
970 if (!g_value_get_string (value)) {
971 GST_WARNING ("location property cannot be NULL");
975 if (mythtvsrc->uri_name != NULL) {
976 g_free (mythtvsrc->uri_name);
977 mythtvsrc->uri_name = NULL;
979 mythtvsrc->uri_name = g_value_dup_string (value);
983 #ifndef GST_DISABLE_GST_DEBUG
984 case PROP_GMYTHTV_DBG:
986 mythtvsrc->mythtv_msgs_dbg = g_value_get_boolean (value);
990 case PROP_GMYTHTV_VERSION:
992 mythtvsrc->mythtv_version = g_value_get_int (value);
995 case PROP_GMYTHTV_LIVEID:
997 mythtvsrc->live_tv_id = g_value_get_int (value);
1000 case PROP_GMYTHTV_LIVE:
1002 mythtvsrc->live_tv = g_value_get_boolean (value);
1005 case PROP_GMYTHTV_ENABLE_TIMING_POSITION:
1007 mythtvsrc->enable_timing_position = g_value_get_boolean (value);
1010 case PROP_GMYTHTV_LIVE_CHAINID:
1012 if (!g_value_get_string (value)) {
1013 GST_WARNING ("MythTV Live chainid property cannot be NULL");
1017 if (mythtvsrc->live_chain_id != NULL) {
1018 g_free (mythtvsrc->live_chain_id);
1019 mythtvsrc->live_chain_id = NULL;
1021 mythtvsrc->live_chain_id = g_value_dup_string (value);
1024 case PROP_GMYTHTV_CHANNEL_NUM:
1026 mythtvsrc->channel_name = g_value_dup_string (value);
1030 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1034 GST_OBJECT_UNLOCK (mythtvsrc);
1038 gst_mythtv_src_get_property (GObject * object, guint prop_id,
1039 GValue * value, GParamSpec * pspec)
1041 GstMythtvSrc *mythtvsrc = GST_MYTHTV_SRC (object);
1043 GST_OBJECT_LOCK (mythtvsrc);
1047 gchar *str = g_strdup ("");
1049 if (mythtvsrc->uri_name == NULL) {
1050 g_free (mythtvsrc->uri_name);
1051 mythtvsrc->uri_name = NULL;
1053 str = g_strdup (mythtvsrc->uri_name);
1055 g_value_set_string (value, str);
1058 #ifndef GST_DISABLE_GST_DEBUG
1059 case PROP_GMYTHTV_DBG:
1060 g_value_set_boolean (value, mythtvsrc->mythtv_msgs_dbg);
1063 case PROP_GMYTHTV_VERSION:
1065 g_value_set_int (value, mythtvsrc->mythtv_version);
1068 case PROP_GMYTHTV_LIVEID:
1070 g_value_set_int (value, mythtvsrc->live_tv_id);
1073 case PROP_GMYTHTV_LIVE:
1074 g_value_set_boolean (value, mythtvsrc->live_tv);
1076 case PROP_GMYTHTV_ENABLE_TIMING_POSITION:
1077 g_value_set_boolean (value, mythtvsrc->enable_timing_position);
1079 case PROP_GMYTHTV_LIVE_CHAINID:
1081 gchar *str = g_strdup ("");
1083 if (mythtvsrc->live_chain_id == NULL) {
1084 g_free (mythtvsrc->live_chain_id);
1085 mythtvsrc->live_chain_id = NULL;
1087 str = g_strdup (mythtvsrc->live_chain_id);
1089 g_value_set_string (value, str);
1092 case PROP_GMYTHTV_CHANNEL_NUM:
1094 g_value_set_string (value, mythtvsrc->channel_name);
1098 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1101 GST_OBJECT_UNLOCK (mythtvsrc);
1105 plugin_init (GstPlugin * plugin)
1107 return gst_element_register (plugin, "mythtvsrc", GST_RANK_NONE,
1108 GST_TYPE_MYTHTV_SRC);
1111 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1115 plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN);
1118 /*** GSTURIHANDLER INTERFACE *************************************************/
1120 gst_mythtv_src_uri_get_type (void)
1126 gst_mythtv_src_uri_get_protocols (void)
1128 static gchar *protocols[] = { "myth", "myths", NULL };
1133 static const gchar *
1134 gst_mythtv_src_uri_get_uri (GstURIHandler * handler)
1136 GstMythtvSrc *src = GST_MYTHTV_SRC (handler);
1138 return src->uri_name;
1142 gst_mythtv_src_uri_set_uri (GstURIHandler * handler, const gchar * uri)
1144 GstMythtvSrc *src = GST_MYTHTV_SRC (handler);
1148 protocol = gst_uri_get_protocol (uri);
1149 if ((strcmp (protocol, "myth") != 0) && (strcmp (protocol, "myths") != 0)) {
1154 g_object_set (src, "location", uri, NULL);
1160 gst_mythtv_src_uri_handler_init (gpointer g_iface, gpointer iface_data)
1162 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
1164 iface->get_type = gst_mythtv_src_uri_get_type;
1165 iface->get_protocols = gst_mythtv_src_uri_get_protocols;
1166 iface->get_uri = gst_mythtv_src_uri_get_uri;
1167 iface->set_uri = gst_mythtv_src_uri_set_uri;
1171 size_header_handler (void *userdata, const char *value)
1173 GstMythtvSrc *src = GST_MYTHTV_SRC (userdata);
1175 GST_DEBUG_OBJECT (src, "content size = %lld bytes", src->content_size);