[svn r550] A lot of GObject fixes (using the GType casting), added inheritance to the GMythFile* modules, locks all the the STOP_RECORDING messages.
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.h>
56 #include <gmyth/gmyth_file_transfer.h>
57 #include <gmyth/gmyth_file_local.h>
58 #include <gmyth/gmyth_livetv.h>
60 #include <gmyth/gmyth_socket.h>
61 #include <gmyth/gmyth_tvchain.h>
66 GST_DEBUG_CATEGORY_STATIC (mythtvsrc_debug);
67 #define GST_CAT_DEFAULT mythtvsrc_debug
69 #define GST_GMYTHTV_ID_NUM 1
71 #define GST_GMYTHTV_CHANNEL_DEFAULT_NUM (-1)
73 #define GMYTHTV_VERSION_DEFAULT 30
75 #define GMYTHTV_TRANSFER_MAX_WAITS 100
77 #define GMYTHTV_TRANSFER_MAX_RESENDS 2
79 #define GMYTHTV_TRANSFER_MAX_BUFFER (128*1024)
81 #define MAX_READ_SIZE (4*1024)
83 #define GST_FLOW_ERROR_NO_DATA (-101)
85 #define REQUEST_MAX_SIZE (64*1024)
87 #define INTERNAL_BUFFER_SIZE (90*1024)
89 static const GstElementDetails gst_mythtv_src_details =
90 GST_ELEMENT_DETAILS ("MythTV client source",
92 "Control and receive data as a client over the network "
93 "via raw socket connections using the MythTV protocol",
94 "Rosfran Borges <rosfran.borges@indt.org.br>");
96 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
99 GST_STATIC_CAPS ("video/x-nuv"));
105 #ifndef GST_DISABLE_GST_DEBUG
108 PROP_GMYTHTV_VERSION,
111 PROP_GMYTHTV_LIVE_CHAINID,
112 PROP_GMYTHTV_ENABLE_TIMING_POSITION,
113 PROP_GMYTHTV_CHANNEL_NUM
116 static void gst_mythtv_src_finalize (GObject * gobject);
118 static GstFlowReturn gst_mythtv_src_create (GstPushSrc * psrc,
119 GstBuffer ** outbuf);
121 static gboolean gst_mythtv_src_start (GstBaseSrc * bsrc);
122 static gboolean gst_mythtv_src_stop (GstBaseSrc * bsrc);
123 static gboolean gst_mythtv_src_get_size (GstBaseSrc * bsrc, guint64 * size);
124 static gboolean gst_mythtv_src_is_seekable (GstBaseSrc * push_src);
126 static gboolean gst_mythtv_src_do_seek (GstBaseSrc * base,
127 GstSegment * segment);
129 static GstStateChangeReturn
130 gst_mythtv_src_change_state (GstElement * element, GstStateChange transition);
132 static void gst_mythtv_src_set_property (GObject * object, guint prop_id,
133 const GValue * value, GParamSpec * pspec);
134 static void gst_mythtv_src_get_property (GObject * object, guint prop_id,
135 GValue * value, GParamSpec * pspec);
137 static void gst_mythtv_src_uri_handler_init (gpointer g_iface,
138 gpointer iface_data);
140 static gboolean gst_mythtv_src_handle_query (GstPad * pad, GstQuery * query);
142 static gboolean gst_mythtv_src_handle_event (GstPad * pad, GstEvent * event);
144 static GMythFileReadResult do_read_request_response (GstMythtvSrc * src, guint size,
145 GByteArray * data_ptr);
148 _urihandler_init (GType type)
150 static const GInterfaceInfo urihandler_info = {
151 gst_mythtv_src_uri_handler_init,
156 g_type_add_interface_static (type, GST_TYPE_URI_HANDLER, &urihandler_info);
158 GST_DEBUG_CATEGORY_INIT (mythtvsrc_debug, "mythtvsrc", 0, "MythTV src");
161 GST_BOILERPLATE_FULL (GstMythtvSrc, gst_mythtv_src, GstPushSrc,
162 GST_TYPE_PUSH_SRC, _urihandler_init)
164 static void gst_mythtv_src_base_init (gpointer g_class)
166 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
168 gst_element_class_add_pad_template (element_class,
169 gst_static_pad_template_get (&srctemplate));
171 gst_element_class_set_details (element_class, &gst_mythtv_src_details);
173 element_class->change_state = gst_mythtv_src_change_state;
178 gst_mythtv_src_class_init (GstMythtvSrcClass * klass)
180 GObjectClass *gobject_class;
181 GstPushSrcClass *gstpushsrc_class;
182 GstBaseSrcClass *gstbasesrc_class;
184 gobject_class = (GObjectClass *) klass;
185 gstbasesrc_class = (GstBaseSrcClass *) klass;
186 gstpushsrc_class = (GstPushSrcClass *) klass;
188 gobject_class->set_property = gst_mythtv_src_set_property;
189 gobject_class->get_property = gst_mythtv_src_get_property;
190 gobject_class->finalize = gst_mythtv_src_finalize;
192 g_object_class_install_property
193 (gobject_class, PROP_LOCATION,
194 g_param_spec_string ("location", "Location",
195 "The location. In the form:"
196 "\n\t\t\tmyth://a.com/file.nuv"
197 "\n\t\t\tmyth://a.com:23223/file.nuv "
198 "\n\t\t\ta.com/file.nuv - default scheme 'myth'",
199 "", G_PARAM_READWRITE));
201 g_object_class_install_property
202 (gobject_class, PROP_GMYTHTV_VERSION,
203 g_param_spec_int ("mythtv-version", "mythtv-version",
204 "Change MythTV version", 26, 30, 26, G_PARAM_READWRITE));
206 g_object_class_install_property
207 (gobject_class, PROP_GMYTHTV_LIVEID,
208 g_param_spec_int ("mythtv-live-id", "mythtv-live-id",
209 "Change MythTV version",
210 0, 200, GST_GMYTHTV_ID_NUM, G_PARAM_READWRITE));
212 g_object_class_install_property
213 (gobject_class, PROP_GMYTHTV_LIVE_CHAINID,
214 g_param_spec_string ("mythtv-live-chainid", "mythtv-live-chainid",
215 "Sets the MythTV chain ID (from TV Chain)", "", G_PARAM_READWRITE));
217 g_object_class_install_property
218 (gobject_class, PROP_GMYTHTV_LIVE,
219 g_param_spec_boolean ("mythtv-live", "mythtv-live",
220 "Enable MythTV Live TV content streaming", FALSE, G_PARAM_READWRITE));
222 g_object_class_install_property
223 (gobject_class, PROP_GMYTHTV_ENABLE_TIMING_POSITION,
224 g_param_spec_boolean ("mythtv-enable-timing-position",
225 "mythtv-enable-timing-position",
226 "Enable MythTV Live TV content size continuous updating", FALSE,
229 g_object_class_install_property
230 (gobject_class, PROP_GMYTHTV_CHANNEL_NUM,
231 g_param_spec_string ("mythtv-channel", "mythtv-channel",
232 "Change MythTV channel number",
233 "", G_PARAM_READWRITE));
235 #ifndef GST_DISABLE_GST_DEBUG
236 g_object_class_install_property
237 (gobject_class, PROP_GMYTHTV_DBG,
238 g_param_spec_boolean ("mythtv-debug", "mythtv-debug",
239 "Enable MythTV debug messages", FALSE, G_PARAM_READWRITE));
242 gstbasesrc_class->start = gst_mythtv_src_start;
243 gstbasesrc_class->stop = gst_mythtv_src_stop;
244 gstbasesrc_class->get_size = gst_mythtv_src_get_size;
245 gstbasesrc_class->is_seekable = gst_mythtv_src_is_seekable;
247 gstbasesrc_class->do_seek = gst_mythtv_src_do_seek;
248 gstpushsrc_class->create = gst_mythtv_src_create;
250 GST_DEBUG_CATEGORY_INIT (mythtvsrc_debug, "mythtvsrc", 0,
251 "MythTV Client Source");
255 gst_mythtv_src_init (GstMythtvSrc * this, GstMythtvSrcClass * g_class)
257 this->file_transfer = NULL;
259 this->unique_setup = FALSE;
261 this->mythtv_version = GMYTHTV_VERSION_DEFAULT;
263 this->state = GST_MYTHTV_SRC_FILE_TRANSFER;
265 this->bytes_read = 0;
267 this->prev_content_size = 0;
269 this->content_size = 0;
270 this->read_offset = 0;
272 this->content_size_last = 0;
274 this->live_tv = FALSE;
276 this->enable_timing_position = FALSE;
277 this->update_prog_chain = FALSE;
279 this->user_agent = g_strdup ("mythtvsrc");
280 this->update_prog_chain = FALSE;
282 this->channel_name = NULL;
286 this->bytes_queue = NULL;
288 this->wait_to_transfer = 0;
290 gst_base_src_set_format (GST_BASE_SRC (this), GST_FORMAT_BYTES);
292 gst_pad_set_event_function (GST_BASE_SRC_PAD (GST_BASE_SRC (this)),
293 gst_mythtv_src_handle_event);
294 gst_pad_set_query_function (GST_BASE_SRC_PAD (GST_BASE_SRC (this)),
295 gst_mythtv_src_handle_query);
300 gst_mythtv_src_finalize (GObject * gobject)
302 GstMythtvSrc *this = GST_MYTHTV_SRC (gobject);
304 if (this->spawn_livetv) {
305 g_object_unref (this->spawn_livetv);
306 this->spawn_livetv = NULL;
309 if (this->file_transfer) {
310 g_object_unref (this->file_transfer);
311 this->file_transfer = NULL;
314 if (this->backend_info) {
315 g_object_unref (this->backend_info);
316 this->backend_info = NULL;
319 if (this->uri_name) {
320 g_free (this->uri_name);
323 if (this->user_agent) {
324 g_free (this->user_agent);
327 if (this->bytes_queue) {
328 g_byte_array_free (this->bytes_queue, TRUE);
329 this->bytes_queue = NULL;
332 G_OBJECT_CLASS (parent_class)->finalize (gobject);
335 static GMythFileReadResult
336 do_read_request_response (GstMythtvSrc * src, guint size, GByteArray *data_ptr)
339 guint sizetoread = size;
340 gint max_iters = GMYTHTV_TRANSFER_MAX_RESENDS;
341 GMythFileReadResult result;
343 GST_LOG_OBJECT (src, "Starting: Reading %d bytes...", sizetoread);
345 /* Loop sending the Myth File Transfer request:
346 * Retry whilst authentication fails and we supply it. */
348 while (sizetoread == size && --max_iters > 0) {
349 /*if ( gmyth_backend_info_is_local_file(src->backend_info) )
350 result = gmyth_file_local_read ( GMYTH_FILE_LOCAL(src->file_transfer),
351 data_ptr, sizetoread, src->live_tv);
353 result = gmyth_file_transfer_read ( GMYTH_FILE_TRANSFER(src->file_transfer),
354 data_ptr, sizetoread, src->live_tv);
356 if (data_ptr->len > 0) {
357 read += data_ptr->len;
358 sizetoread -= data_ptr->len;
359 } else if (data_ptr->len < 0) {
360 if (src->live_tv == FALSE) {
361 result = GMYTH_FILE_READ_EOF;
364 if (result == GMYTH_FILE_READ_ERROR) { /* -314 */
365 GST_INFO_OBJECT (src, "[LiveTV] FileTransfer READ_ERROR!");
367 } else if (result == GMYTH_FILE_READ_NEXT_PROG_CHAIN) { /* -315 */
368 GST_INFO_OBJECT (src,
369 "[LiveTV] FileTransfer - Go to the next program chain!");
375 } else if (data_ptr->len == 0)
378 if (read == sizetoread)
382 if ((read < 0 && !src->live_tv) || max_iters == 0){
383 result = GMYTH_FILE_READ_EOF;
397 gst_mythtv_src_create (GstPushSrc * psrc, GstBuffer ** outbuf)
400 GstFlowReturn ret = GST_FLOW_OK;
401 guint buffer_size_inter = 0;
403 src = GST_MYTHTV_SRC (psrc);
405 /* The caller should know the number of bytes and not read beyond EOS. */
406 if (G_UNLIKELY (src->eos))
408 if (G_UNLIKELY (src->update_prog_chain))
409 goto change_progchain;
411 GST_DEBUG_OBJECT (src, "offset = %" G_GUINT64_FORMAT ", size = %d...",
412 src->read_offset, MAX_READ_SIZE);
414 GST_DEBUG_OBJECT (src, "Create: buffer_remain: %d, buffer_size = %d.",
415 (gint) src->buffer_remain, src->bytes_queue->len);
417 program_chain_changed:
418 /* just get from the byte array, no network effort... */
419 if ((src->buffer_remain = src->bytes_queue->len) < MAX_READ_SIZE) {
421 GMythFileReadResult result = GMYTH_FILE_READ_OK;
424 buffer_size_inter = (INTERNAL_BUFFER_SIZE - src->buffer_remain);
426 if (buffer_size_inter > REQUEST_MAX_SIZE)
427 buffer_size_inter = REQUEST_MAX_SIZE;
429 buffer = g_byte_array_new ();
431 result = do_read_request_response (src, buffer_size_inter, buffer);
433 if (G_UNLIKELY (buffer->len < 0)) {
435 if (buffer != NULL) {
436 g_byte_array_free (buffer, TRUE);
440 if (src->live_tv || ( result == GMYTH_FILE_READ_NEXT_PROG_CHAIN ))
441 goto change_progchain;
444 } else if (G_UNLIKELY (read == 0)) {
446 if (buffer != NULL) {
447 g_byte_array_free (buffer, TRUE);
454 goto program_chain_changed;
457 if (G_UNLIKELY (src->update_prog_chain))
459 if (buffer != NULL) {
460 g_byte_array_free (buffer, TRUE);
463 goto change_progchain;
467 g_byte_array_append (src->bytes_queue, buffer->data, buffer->len);
468 if (buffer->len > buffer_size_inter)
469 GST_WARNING_OBJECT (src,
470 "INCREASED buffer size! Backend sent more than we ask him... (%d)",
471 abs (buffer->len - buffer_size_inter));
473 src->buffer_remain += buffer->len;
475 if (buffer != NULL) {
476 g_byte_array_free (buffer, TRUE);
480 GST_DEBUG_OBJECT (src,
481 "BYTES READ (actual) = %d, BYTES READ (cumulative) = %llu, "
482 "OFFSET = %llu, CONTENT SIZE = %llu.", read,
483 src->bytes_read, src->read_offset, src->content_size);
488 (src->buffer_remain < MAX_READ_SIZE) ? src->buffer_remain : MAX_READ_SIZE;
490 *outbuf = gst_buffer_new ();
492 /* gets the first buffer_size bytes from the byte array buffer variable */
493 /* guint8 *buf = g_memdup( src->bytes_queue->data, buffer_size ); */
495 GST_DEBUG_OBJECT (src, "read from network? %s!, buffer_remain = %d",
496 (buffer_size_inter ==
497 0) ? "NO, got from buffer" : "YES, go see the backend's log file",
500 GST_BUFFER_SIZE (*outbuf) = buffer_size;
501 GST_BUFFER_MALLOCDATA (*outbuf) = g_malloc0 (GST_BUFFER_SIZE (*outbuf));
502 GST_BUFFER_DATA (*outbuf) = GST_BUFFER_MALLOCDATA (*outbuf);
503 g_memmove (GST_BUFFER_DATA ((*outbuf)), src->bytes_queue->data,
504 GST_BUFFER_SIZE (*outbuf));
505 GST_BUFFER_OFFSET (*outbuf) = src->read_offset;
506 GST_BUFFER_OFFSET_END (*outbuf) =
507 src->read_offset + GST_BUFFER_SIZE (*outbuf);
509 src->buffer_remain -= GST_BUFFER_SIZE (*outbuf);
511 src->read_offset += GST_BUFFER_SIZE (*outbuf);
512 src->bytes_read += GST_BUFFER_SIZE (*outbuf);
513 GST_DEBUG_OBJECT (src, "Buffer output with size: %d",
514 GST_BUFFER_SIZE (*outbuf));
516 /* flushs the newly buffer got from byte array */
518 g_byte_array_remove_range (src->bytes_queue, 0, buffer_size);
520 GST_DEBUG_OBJECT ( src, "Got buffer: BUFFER --->SIZE = %d, OFFSET = %llu, "
521 "OFFSET_END = %llu.", GST_BUFFER_SIZE (*outbuf),
522 GST_BUFFER_OFFSET (*outbuf), GST_BUFFER_OFFSET_END (*outbuf) );
524 GST_DEBUG_OBJECT (src, "CONTENT_SIZE = %llu, BYTES_READ = %llu.",
525 src->content_size, src->bytes_read);
527 if ( G_UNLIKELY (src->eos) || ( !src->live_tv
528 && ( src->bytes_read >= src->content_size ) ) )
533 const gchar *reason = gst_flow_get_name (ret);
535 GST_DEBUG_OBJECT (src, "DONE task, reason %s", reason);
540 const gchar *reason = gst_flow_get_name (ret);
542 GST_DEBUG_OBJECT (src, "pausing task, reason %s", reason);
543 return GST_FLOW_UNEXPECTED;
548 GST_ELEMENT_ERROR (src, RESOURCE, READ,
549 (NULL), ("Could not read any bytes (%i, %s)", read, src->uri_name));
550 return GST_FLOW_ERROR;
554 GST_ELEMENT_ERROR (src, RESOURCE, READ,
555 (NULL), ("Seek failed, go to the next program info... (%i, %s)", read,
559 TODO: need to send a new segment event to NUVDemux?
560 gst_pad_push_event (GST_BASE_SRC_PAD (GST_BASE_SRC (psrc)),
561 gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0, -1, 0));
564 goto program_chain_changed;
570 gst_mythtv_src_get_position (GstMythtvSrc * src)
576 if (src->live_tv == TRUE && (abs (src->content_size - src->bytes_read) <
577 GMYTHTV_TRANSFER_MAX_BUFFER)) {
581 size_tmp = gmyth_recorder_get_file_position (src->spawn_livetv->recorder);
582 if (size_tmp > (src->content_size + GMYTHTV_TRANSFER_MAX_BUFFER))
583 src->content_size = size_tmp;
584 else if (size_tmp > 0 && --max_tries > 0)
586 GST_LOG_OBJECT (src, "GET_POSITION: file_position = %lld", size_tmp);
587 /* sets the last content size amount before it can be updated */
588 src->prev_content_size = src->content_size;
591 return src->content_size;
596 gst_mythtv_src_do_seek (GstBaseSrc * base, GstSegment * segment)
598 GstMythtvSrc *src = GST_MYTHTV_SRC (base);
599 gint64 new_offset = -1;
600 gint64 actual_seek = segment->start;
603 GST_LOG_OBJECT (src, "seek, segment: %" GST_SEGMENT_FORMAT, segment);
605 if (segment->format == GST_FORMAT_TIME) {
609 "Trying to seek at the value (actual_seek = %lld, read_offset = %lld)",
610 actual_seek, src->read_offset);
611 /* verify if it needs to seek */
612 if (src->read_offset != actual_seek) {
614 /*if ( gmyth_backend_info_is_local_file(src->backend_info) )
616 gmyth_file_local_seek (src->file_transfer, segment->start, G_SEEK_SET);
619 gmyth_file_transfer_seek (src->file_transfer, segment->start, SEEK_SET);
622 "Segment offset start = %lld, SRC Offset = %lld, NEW actual backend SEEK Offset = %lld.",
623 segment->start, src->read_offset, new_offset);
624 if (G_UNLIKELY (new_offset < 0)) {
630 src->read_offset = new_offset;
633 GST_INFO_OBJECT (src, "Failed to set the SEEK on segment!");
644 GST_DEBUG_OBJECT (src, "EOS found on seeking!!!");
650 /* create a socket for connecting to remote server */
652 gst_mythtv_src_start (GstBaseSrc * bsrc)
654 GstMythtvSrc *src = GST_MYTHTV_SRC (bsrc);
656 GString *chain_id_local = NULL;
657 GMythURI *gmyth_uri = NULL;
660 if (src->unique_setup == FALSE) {
661 src->unique_setup = TRUE;
666 gmyth_uri = gmyth_uri_new_with_value( src->uri_name );
668 src->backend_info = gmyth_backend_info_new_with_uri (src->uri_name);
669 src->live_tv |= gmyth_uri_is_livetv( gmyth_uri );
670 /* testing UPnP... */
671 /* gmyth_backend_info_set_hostname( src->backend_info, NULL ); */
672 if ( src->live_tv ) {
673 src->spawn_livetv = gmyth_livetv_new (src->backend_info);
675 gchar* ch = gmyth_uri_get_channel_name( gmyth_uri );
677 src->channel_name = ch;
679 if (src->channel_name != NULL) {
680 if (gmyth_livetv_channel_name_setup (src->spawn_livetv, src->channel_name) == FALSE) {
681 GST_INFO_OBJECT (src, "LiveTV setup felt down on error");
686 if (gmyth_livetv_setup (src->spawn_livetv) == FALSE) {
687 GST_INFO_OBJECT (src, "LiveTV setup felt down on error");
693 /* testing change channel... */
694 /* gmyth_recorder_change_channel( src->spawn_livetv->recorder, CHANNEL_DIRECTION_UP ); */
696 src->file_transfer = gmyth_livetv_create_file_transfer (src->spawn_livetv);
698 if (NULL == src->file_transfer) {
699 GST_INFO_OBJECT (src, "[LiveTV] FileTransfer equals to NULL");
704 if ( !gmyth_file_transfer_open( src->file_transfer, src->spawn_livetv->uri != NULL ?
705 gmyth_uri_get_path(src->spawn_livetv->uri) :
706 src->spawn_livetv->proginfo->pathname->str ) )
708 GST_INFO_OBJECT (src, "Error: couldn't open the FileTransfer from LiveTV source!" );
709 g_object_unref( src->file_transfer );
710 src->file_transfer = NULL;
715 /*if ( gmyth_uri_is_local_file(gmyth_uri) )
717 src->file_transfer = gmyth_file_local_new(src->backend_info);
718 ret = gmyth_file_local_open (src->file_transfer);
720 src->file_transfer = gmyth_file_transfer_new(src->backend_info);
721 ret = gmyth_file_transfer_open (src->file_transfer, src->uri_name);
724 } /* if (else) - recorded FileTransfer */
726 if (NULL == src->file_transfer) {
727 GST_INFO_OBJECT (src, "FileTransfer is NULL");
730 /*GST_INFO_OBJECT( src, "uri = %s", src->spawn_livetv->file_transfer); */
733 #ifndef GST_DISABLE_GST_DEBUG
734 if (src->mythtv_msgs_dbg)
735 GST_INFO_OBJECT (src,
736 "MythTV FileTransfer request failed when setting up socket connection!");
738 goto begin_req_failed;
741 GST_INFO_OBJECT (src,
742 "MythTV FileTransfer filesize = %lld, content_size = %lld!",
743 gmyth_file_transfer_get_filesize( src->file_transfer ), src->content_size);
745 src->content_size = gmyth_file_transfer_get_filesize (src->file_transfer);
747 src->do_start = FALSE;
749 /* this is used for the buffer cache */
750 src->bytes_queue = g_byte_array_sized_new (INTERNAL_BUFFER_SIZE);
751 src->buffer_remain = 0;
753 gst_pad_push_event (GST_BASE_SRC_PAD (GST_BASE_SRC (src)),
754 gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0,
755 src->content_size, 0));
758 /*if ( gmyth_uri != NULL )
760 g_object_unref( gmyth_uri );
764 if (chain_id_local != NULL) {
765 g_string_free (chain_id_local, TRUE);
766 chain_id_local = NULL;
774 if (src->spawn_livetv != NULL)
775 g_object_unref (src->spawn_livetv);
777 GST_ELEMENT_ERROR (src, LIBRARY, INIT,
778 (NULL), ("Could not initialize MythTV library (%i, %s)", ret,
784 GST_ELEMENT_ERROR (src, LIBRARY, INIT,
785 (NULL), ("Could not begin request sent to MythTV server (%i, %s)", ret,
793 gst_mythtv_src_get_size (GstBaseSrc * bsrc, guint64 * size)
795 GstMythtvSrc *src = GST_MYTHTV_SRC (bsrc);
798 GST_LOG_OBJECT (src, "Differs from previous content size: %d (max.: %d)",
799 abs (src->content_size - src->prev_content_size),
800 GMYTHTV_TRANSFER_MAX_BUFFER);
804 } else if (src->live_tv && src->enable_timing_position
805 && (abs (src->content_size - src->bytes_read) <
806 GMYTHTV_TRANSFER_MAX_BUFFER)) {
809 gmyth_recorder_get_file_position (src->spawn_livetv->recorder);
810 if (new_offset > 0 && new_offset > src->content_size) {
811 src->content_size = new_offset;
812 } else if (new_offset < src->content_size) {
813 src->update_prog_chain = TRUE;
818 *size = src->content_size;
819 GST_LOG_OBJECT (src, "Content size = %lld", src->content_size);
825 /* close the socket and associated resources
826 * used both to recover from errors and go to NULL state */
828 gst_mythtv_src_stop (GstBaseSrc * bsrc)
832 src = GST_MYTHTV_SRC (bsrc);
835 g_free (src->uri_name);
836 src->uri_name = NULL;
845 gst_mythtv_src_handle_event (GstPad * pad, GstEvent * event)
847 GstMythtvSrc *src = GST_MYTHTV_SRC (GST_PAD_PARENT (pad));
848 gint64 cont_size = 0;
849 gboolean ret = FALSE;
851 switch (GST_EVENT_TYPE (event)) {
853 GST_WARNING_OBJECT (src, "Got EOS event");
856 cont_size = gst_mythtv_src_get_position (src);
857 if (cont_size > src->content_size) {
858 src->content_size = cont_size;
862 gst_element_set_state (GST_ELEMENT (src), GST_STATE_NULL);
863 gst_element_set_locked_state (GST_ELEMENT (src), FALSE);
868 ret = gst_pad_event_default (pad, event);
875 gst_mythtv_src_is_seekable (GstBaseSrc * push_src)
881 gst_mythtv_src_handle_query (GstPad * pad, GstQuery * query)
883 gboolean res = FALSE;
884 GstMythtvSrc *myth = GST_MYTHTV_SRC (gst_pad_get_parent (pad));
887 switch (GST_QUERY_TYPE (query)) {
888 case GST_QUERY_POSITION:
890 gst_query_parse_position (query, &formt, NULL);
891 if (formt == GST_FORMAT_BYTES) {
892 gst_query_set_position (query, formt, myth->read_offset);
893 GST_DEBUG_OBJECT (myth, "POS %" G_GINT64_FORMAT, myth->read_offset);
895 } else if (formt == GST_FORMAT_TIME) {
896 res = gst_pad_query_default (pad, query);
900 case GST_QUERY_DURATION:
903 if (myth->duration != 0) {
907 fps = nuv->h->i_fpsn / nuv->h->i_fpsd;
909 gst_util_uint64_scale_int (GST_SECOND, nuv->h->i_video_blocks, fps);
913 gst_query_parse_duration (query, &formt, NULL);
914 if (formt == GST_FORMAT_BYTES) {
915 gst_query_set_duration (query, formt, myth->content_size);
916 GST_DEBUG_OBJECT (myth, "SIZE %" G_GINT64_FORMAT, myth->content_size);
918 } else if (formt == GST_FORMAT_TIME) {
919 res = gst_pad_query_default (pad, query);
925 res = gst_pad_query_default (pad, query);
930 gst_object_unref (myth);
935 static GstStateChangeReturn
936 gst_mythtv_src_change_state (GstElement * element, GstStateChange transition)
938 GstStateChangeReturn ret = GST_STATE_CHANGE_FAILURE;
939 GstMythtvSrc *src = GST_MYTHTV_SRC (element);
941 switch (transition) {
942 case GST_STATE_CHANGE_NULL_TO_READY:
944 case GST_STATE_CHANGE_READY_TO_PAUSED:
945 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
947 if (!gmyth_recorder_send_frontend_ready_command (src->spawn_livetv->
949 GST_WARNING_OBJECT (src,
950 "Couldn't send the FRONTEND_READY message to the backend!");
952 GST_DEBUG_OBJECT (src, "FRONTEND_READY was sent to the backend");
959 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
960 if (ret == GST_STATE_CHANGE_FAILURE)
963 switch (transition) {
964 case GST_STATE_CHANGE_READY_TO_NULL:
966 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
967 case GST_STATE_CHANGE_PAUSED_TO_READY:
977 gst_mythtv_src_set_property (GObject * object, guint prop_id,
978 const GValue * value, GParamSpec * pspec)
980 GstMythtvSrc *mythtvsrc = GST_MYTHTV_SRC (object);
982 GST_OBJECT_LOCK (mythtvsrc);
986 if (!g_value_get_string (value)) {
987 GST_WARNING ("location property cannot be NULL");
991 if (mythtvsrc->uri_name != NULL) {
992 g_free (mythtvsrc->uri_name);
993 mythtvsrc->uri_name = NULL;
995 mythtvsrc->uri_name = g_value_dup_string (value);
999 #ifndef GST_DISABLE_GST_DEBUG
1000 case PROP_GMYTHTV_DBG:
1002 mythtvsrc->mythtv_msgs_dbg = g_value_get_boolean (value);
1006 case PROP_GMYTHTV_VERSION:
1008 mythtvsrc->mythtv_version = g_value_get_int (value);
1011 case PROP_GMYTHTV_LIVEID:
1013 mythtvsrc->live_tv_id = g_value_get_int (value);
1016 case PROP_GMYTHTV_LIVE:
1018 mythtvsrc->live_tv = g_value_get_boolean (value);
1021 case PROP_GMYTHTV_ENABLE_TIMING_POSITION:
1023 mythtvsrc->enable_timing_position = g_value_get_boolean (value);
1026 case PROP_GMYTHTV_LIVE_CHAINID:
1028 if (!g_value_get_string (value)) {
1029 GST_WARNING ("MythTV Live chainid property cannot be NULL");
1033 if (mythtvsrc->live_chain_id != NULL) {
1034 g_free (mythtvsrc->live_chain_id);
1035 mythtvsrc->live_chain_id = NULL;
1037 mythtvsrc->live_chain_id = g_value_dup_string (value);
1040 case PROP_GMYTHTV_CHANNEL_NUM:
1042 mythtvsrc->channel_name = g_value_dup_string (value);
1046 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1050 GST_OBJECT_UNLOCK (mythtvsrc);
1054 gst_mythtv_src_get_property (GObject * object, guint prop_id,
1055 GValue * value, GParamSpec * pspec)
1057 GstMythtvSrc *mythtvsrc = GST_MYTHTV_SRC (object);
1059 GST_OBJECT_LOCK (mythtvsrc);
1063 g_value_set_string (value, mythtvsrc->uri_name);
1066 #ifndef GST_DISABLE_GST_DEBUG
1067 case PROP_GMYTHTV_DBG:
1068 g_value_set_boolean (value, mythtvsrc->mythtv_msgs_dbg);
1071 case PROP_GMYTHTV_VERSION:
1073 g_value_set_int (value, mythtvsrc->mythtv_version);
1076 case PROP_GMYTHTV_LIVEID:
1078 g_value_set_int (value, mythtvsrc->live_tv_id);
1081 case PROP_GMYTHTV_LIVE:
1082 g_value_set_boolean (value, mythtvsrc->live_tv);
1084 case PROP_GMYTHTV_ENABLE_TIMING_POSITION:
1085 g_value_set_boolean (value, mythtvsrc->enable_timing_position);
1087 case PROP_GMYTHTV_LIVE_CHAINID:
1089 g_value_set_string (value, mythtvsrc->live_chain_id);
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);