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_clear (GstMythtvSrc *mythtv_src);
118 static void gst_mythtv_src_finalize (GObject * gobject);
120 static GstFlowReturn gst_mythtv_src_create (GstPushSrc * psrc,
121 GstBuffer ** outbuf);
123 static gboolean gst_mythtv_src_start (GstBaseSrc * bsrc);
124 static gboolean gst_mythtv_src_stop (GstBaseSrc * bsrc);
125 static gboolean gst_mythtv_src_get_size (GstBaseSrc * bsrc, guint64 * size);
126 static gboolean gst_mythtv_src_is_seekable (GstBaseSrc * push_src);
128 static gboolean gst_mythtv_src_do_seek (GstBaseSrc * base,
129 GstSegment * segment);
131 static GstStateChangeReturn
132 gst_mythtv_src_change_state (GstElement * element, GstStateChange transition);
134 static void gst_mythtv_src_set_property (GObject * object, guint prop_id,
135 const GValue * value, GParamSpec * pspec);
136 static void gst_mythtv_src_get_property (GObject * object, guint prop_id,
137 GValue * value, GParamSpec * pspec);
139 static void gst_mythtv_src_uri_handler_init (gpointer g_iface,
140 gpointer iface_data);
142 static gboolean gst_mythtv_src_handle_query (GstPad * pad, GstQuery * query);
144 static gboolean gst_mythtv_src_handle_event (GstPad * pad, GstEvent * event);
146 static GMythFileReadResult do_read_request_response (GstMythtvSrc * src, guint size,
147 GByteArray * data_ptr);
150 _urihandler_init (GType type)
152 static const GInterfaceInfo urihandler_info = {
153 gst_mythtv_src_uri_handler_init,
158 g_type_add_interface_static (type, GST_TYPE_URI_HANDLER, &urihandler_info);
160 GST_DEBUG_CATEGORY_INIT (mythtvsrc_debug, "mythtvsrc", 0, "MythTV src");
163 GST_BOILERPLATE_FULL (GstMythtvSrc, gst_mythtv_src, GstPushSrc,
164 GST_TYPE_PUSH_SRC, _urihandler_init)
166 static void gst_mythtv_src_base_init (gpointer g_class)
168 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
170 gst_element_class_add_pad_template (element_class,
171 gst_static_pad_template_get (&srctemplate));
173 gst_element_class_set_details (element_class, &gst_mythtv_src_details);
175 element_class->change_state = gst_mythtv_src_change_state;
180 gst_mythtv_src_class_init (GstMythtvSrcClass * klass)
182 GObjectClass *gobject_class;
183 GstPushSrcClass *gstpushsrc_class;
184 GstBaseSrcClass *gstbasesrc_class;
186 gobject_class = (GObjectClass *) klass;
187 gstbasesrc_class = (GstBaseSrcClass *) klass;
188 gstpushsrc_class = (GstPushSrcClass *) klass;
190 gobject_class->set_property = gst_mythtv_src_set_property;
191 gobject_class->get_property = gst_mythtv_src_get_property;
192 gobject_class->finalize = gst_mythtv_src_finalize;
194 g_object_class_install_property
195 (gobject_class, PROP_LOCATION,
196 g_param_spec_string ("location", "Location",
197 "The location. In the form:"
198 "\n\t\t\tmyth://a.com/file.nuv"
199 "\n\t\t\tmyth://a.com:23223/file.nuv "
200 "\n\t\t\ta.com/file.nuv - default scheme 'myth'",
201 "", G_PARAM_READWRITE));
203 g_object_class_install_property
204 (gobject_class, PROP_GMYTHTV_VERSION,
205 g_param_spec_int ("mythtv-version", "mythtv-version",
206 "Change MythTV version", 26, 30, 26, G_PARAM_READWRITE));
208 g_object_class_install_property
209 (gobject_class, PROP_GMYTHTV_LIVEID,
210 g_param_spec_int ("mythtv-live-id", "mythtv-live-id",
211 "Change MythTV version",
212 0, 200, GST_GMYTHTV_ID_NUM, G_PARAM_READWRITE));
214 g_object_class_install_property
215 (gobject_class, PROP_GMYTHTV_LIVE_CHAINID,
216 g_param_spec_string ("mythtv-live-chainid", "mythtv-live-chainid",
217 "Sets the MythTV chain ID (from TV Chain)", "", G_PARAM_READWRITE));
219 g_object_class_install_property
220 (gobject_class, PROP_GMYTHTV_LIVE,
221 g_param_spec_boolean ("mythtv-live", "mythtv-live",
222 "Enable MythTV Live TV content streaming", FALSE, G_PARAM_READWRITE));
224 g_object_class_install_property
225 (gobject_class, PROP_GMYTHTV_ENABLE_TIMING_POSITION,
226 g_param_spec_boolean ("mythtv-enable-timing-position",
227 "mythtv-enable-timing-position",
228 "Enable MythTV Live TV content size continuous updating", FALSE,
231 g_object_class_install_property
232 (gobject_class, PROP_GMYTHTV_CHANNEL_NUM,
233 g_param_spec_string ("mythtv-channel", "mythtv-channel",
234 "Change MythTV channel number",
235 "", G_PARAM_READWRITE));
237 #ifndef GST_DISABLE_GST_DEBUG
238 g_object_class_install_property
239 (gobject_class, PROP_GMYTHTV_DBG,
240 g_param_spec_boolean ("mythtv-debug", "mythtv-debug",
241 "Enable MythTV debug messages", FALSE, G_PARAM_READWRITE));
244 gstbasesrc_class->start = gst_mythtv_src_start;
245 gstbasesrc_class->stop = gst_mythtv_src_stop;
246 gstbasesrc_class->get_size = gst_mythtv_src_get_size;
247 gstbasesrc_class->is_seekable = gst_mythtv_src_is_seekable;
249 gstbasesrc_class->do_seek = gst_mythtv_src_do_seek;
250 gstpushsrc_class->create = gst_mythtv_src_create;
252 GST_DEBUG_CATEGORY_INIT (mythtvsrc_debug, "mythtvsrc", 0,
253 "MythTV Client Source");
257 gst_mythtv_src_init (GstMythtvSrc * this, GstMythtvSrcClass * g_class)
261 this->unique_setup = FALSE;
263 this->mythtv_version = GMYTHTV_VERSION_DEFAULT;
265 this->state = GST_MYTHTV_SRC_FILE_TRANSFER;
267 this->bytes_read = 0;
269 this->prev_content_size = 0;
271 this->content_size = 0;
272 this->read_offset = 0;
274 this->content_size_last = 0;
276 this->live_tv = FALSE;
278 this->enable_timing_position = FALSE;
279 this->update_prog_chain = FALSE;
281 this->user_agent = g_strdup ("mythtvsrc");
282 this->update_prog_chain = FALSE;
284 this->channel_name = NULL;
288 this->bytes_queue = NULL;
290 this->wait_to_transfer = 0;
292 gst_base_src_set_format (GST_BASE_SRC (this), GST_FORMAT_BYTES);
294 gst_pad_set_event_function (GST_BASE_SRC_PAD (GST_BASE_SRC (this)),
295 gst_mythtv_src_handle_event);
296 gst_pad_set_query_function (GST_BASE_SRC_PAD (GST_BASE_SRC (this)),
297 gst_mythtv_src_handle_query);
302 gst_mythtv_src_clear (GstMythtvSrc *mythtv_src)
305 if (mythtv_src->spawn_livetv) {
306 g_object_unref (mythtv_src->spawn_livetv);
307 mythtv_src->spawn_livetv = NULL;
310 if (mythtv_src->file) {
311 g_object_unref (mythtv_src->file);
312 mythtv_src->file = NULL;
315 if (mythtv_src->backend_info) {
316 g_object_unref (mythtv_src->backend_info);
317 mythtv_src->backend_info = NULL;
320 if (mythtv_src->uri_name) {
321 g_free (mythtv_src->uri_name);
324 if (mythtv_src->user_agent) {
325 g_free (mythtv_src->user_agent);
328 if (mythtv_src->bytes_queue) {
329 g_byte_array_free (mythtv_src->bytes_queue, TRUE);
330 mythtv_src->bytes_queue = NULL;
336 gst_mythtv_src_finalize (GObject * gobject)
338 GstMythtvSrc *this = GST_MYTHTV_SRC (gobject);
340 gst_mythtv_src_clear (this);
342 G_OBJECT_CLASS (parent_class)->finalize (gobject);
345 static GMythFileReadResult
346 do_read_request_response (GstMythtvSrc * src, guint size, GByteArray *data_ptr)
349 guint sizetoread = size;
350 gint max_iters = GMYTHTV_TRANSFER_MAX_RESENDS;
351 GMythFileReadResult result;
353 GST_LOG_OBJECT (src, "Starting: Reading %d bytes...", sizetoread);
355 /* Loop sending the Myth File Transfer request:
356 * Retry whilst authentication fails and we supply it. */
358 while (sizetoread == size && --max_iters > 0) {
359 /* if ( gmyth_backend_info_is_local_file(src->backend_info) ) */
360 if ( IS_GMYTH_FILE_LOCAL(src->file) )
361 result = gmyth_file_local_read ( GMYTH_FILE_LOCAL(src->file),
362 data_ptr, sizetoread, src->live_tv);
363 else if ( IS_GMYTH_FILE_TRANSFER(src->file) )
364 result = gmyth_file_transfer_read ( GMYTH_FILE_TRANSFER(src->file),
365 data_ptr, sizetoread, src->live_tv);
367 if (data_ptr->len > 0) {
368 read += data_ptr->len;
369 sizetoread -= data_ptr->len;
370 } else if (data_ptr->len < 0) {
371 if (src->live_tv == FALSE) {
372 result = GMYTH_FILE_READ_EOF;
375 if (result == GMYTH_FILE_READ_ERROR) { /* -314 */
376 GST_INFO_OBJECT (src, "[LiveTV] FileTransfer READ_ERROR!");
378 } else if (result == GMYTH_FILE_READ_NEXT_PROG_CHAIN) { /* -315 */
379 GST_INFO_OBJECT (src,
380 "[LiveTV] FileTransfer - Go to the next program chain!");
386 } else if (data_ptr->len == 0)
389 if (read == sizetoread)
393 if ((read < 0 && !src->live_tv) || max_iters == 0){
394 result = GMYTH_FILE_READ_EOF;
408 gst_mythtv_src_create (GstPushSrc * psrc, GstBuffer ** outbuf)
411 GstFlowReturn ret = GST_FLOW_OK;
412 guint buffer_size_inter = 0;
414 src = GST_MYTHTV_SRC (psrc);
416 /* The caller should know the number of bytes and not read beyond EOS. */
417 if (G_UNLIKELY (src->eos))
419 if (G_UNLIKELY (src->update_prog_chain))
420 goto change_progchain;
422 GST_DEBUG_OBJECT (src, "offset = %" G_GUINT64_FORMAT ", size = %d...",
423 src->read_offset, MAX_READ_SIZE);
425 GST_DEBUG_OBJECT (src, "Create: buffer_remain: %d, buffer_size = %d.",
426 (gint) src->buffer_remain, src->bytes_queue->len);
428 program_chain_changed:
429 /* just get from the byte array, no network effort... */
430 if ((src->buffer_remain = src->bytes_queue->len) < MAX_READ_SIZE) {
432 GMythFileReadResult result = GMYTH_FILE_READ_OK;
435 buffer_size_inter = (INTERNAL_BUFFER_SIZE - src->buffer_remain);
437 if (buffer_size_inter > REQUEST_MAX_SIZE)
438 buffer_size_inter = REQUEST_MAX_SIZE;
440 buffer = g_byte_array_new ();
442 result = do_read_request_response (src, buffer_size_inter, buffer);
444 if (G_UNLIKELY (buffer->len < 0)) {
446 if (buffer != NULL) {
447 g_byte_array_free (buffer, TRUE);
451 if (src->live_tv || ( result == GMYTH_FILE_READ_NEXT_PROG_CHAIN ))
452 goto change_progchain;
455 } else if (G_UNLIKELY (read == 0)) {
457 if (buffer != NULL) {
458 g_byte_array_free (buffer, TRUE);
465 goto program_chain_changed;
468 if (G_UNLIKELY (src->update_prog_chain))
470 if (buffer != NULL) {
471 g_byte_array_free (buffer, TRUE);
474 goto change_progchain;
478 g_byte_array_append (src->bytes_queue, buffer->data, buffer->len);
479 if (buffer->len > buffer_size_inter)
480 GST_WARNING_OBJECT (src,
481 "INCREASED buffer size! Backend sent more than we ask him... (%d)",
482 abs (buffer->len - buffer_size_inter));
484 src->buffer_remain += buffer->len;
486 if (buffer != NULL) {
487 g_byte_array_free (buffer, TRUE);
491 GST_DEBUG_OBJECT (src,
492 "BYTES READ (actual) = %d, BYTES READ (cumulative) = %llu, "
493 "OFFSET = %llu, CONTENT SIZE = %llu.", read,
494 src->bytes_read, src->read_offset, src->content_size);
499 (src->buffer_remain < MAX_READ_SIZE) ? src->buffer_remain : MAX_READ_SIZE;
501 *outbuf = gst_buffer_new ();
503 /* gets the first buffer_size bytes from the byte array buffer variable */
504 /* guint8 *buf = g_memdup( src->bytes_queue->data, buffer_size ); */
506 GST_DEBUG_OBJECT (src, "read from network? %s!, buffer_remain = %d",
507 (buffer_size_inter ==
508 0) ? "NO, got from buffer" : "YES, go see the backend's log file",
511 GST_BUFFER_SIZE (*outbuf) = buffer_size;
512 GST_BUFFER_MALLOCDATA (*outbuf) = g_malloc0 (GST_BUFFER_SIZE (*outbuf));
513 GST_BUFFER_DATA (*outbuf) = GST_BUFFER_MALLOCDATA (*outbuf);
514 g_memmove (GST_BUFFER_DATA ((*outbuf)), src->bytes_queue->data,
515 GST_BUFFER_SIZE (*outbuf));
516 GST_BUFFER_OFFSET (*outbuf) = src->read_offset;
517 GST_BUFFER_OFFSET_END (*outbuf) =
518 src->read_offset + GST_BUFFER_SIZE (*outbuf);
520 src->buffer_remain -= GST_BUFFER_SIZE (*outbuf);
522 src->read_offset += GST_BUFFER_SIZE (*outbuf);
523 src->bytes_read += GST_BUFFER_SIZE (*outbuf);
524 GST_DEBUG_OBJECT (src, "Buffer output with size: %d",
525 GST_BUFFER_SIZE (*outbuf));
527 /* flushs the newly buffer got from byte array */
529 g_byte_array_remove_range (src->bytes_queue, 0, buffer_size);
531 GST_DEBUG_OBJECT ( src, "Got buffer: BUFFER --->SIZE = %d, OFFSET = %llu, "
532 "OFFSET_END = %llu.", GST_BUFFER_SIZE (*outbuf),
533 GST_BUFFER_OFFSET (*outbuf), GST_BUFFER_OFFSET_END (*outbuf) );
535 GST_DEBUG_OBJECT (src, "CONTENT_SIZE = %llu, BYTES_READ = %llu.",
536 src->content_size, src->bytes_read);
538 if ( G_UNLIKELY (src->eos) || ( !src->live_tv
539 && ( src->bytes_read >= src->content_size ) ) )
544 const gchar *reason = gst_flow_get_name (ret);
546 GST_DEBUG_OBJECT (src, "DONE task, reason %s", reason);
551 const gchar *reason = gst_flow_get_name (ret);
553 GST_DEBUG_OBJECT (src, "pausing task, reason %s", reason);
554 return GST_FLOW_UNEXPECTED;
559 GST_ELEMENT_ERROR (src, RESOURCE, READ,
560 (NULL), ("Could not read any bytes (%i, %s)", read, src->uri_name));
561 return GST_FLOW_ERROR;
565 GST_ELEMENT_ERROR (src, RESOURCE, READ,
566 (NULL), ("Seek failed, go to the next program info... (%i, %s)", read,
570 TODO: need to send a new segment event to NUVDemux?
571 gst_pad_push_event (GST_BASE_SRC_PAD (GST_BASE_SRC (psrc)),
572 gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0, -1, 0));
575 goto program_chain_changed;
581 gst_mythtv_src_get_position (GstMythtvSrc * src)
587 if (src->live_tv == TRUE && (abs (src->content_size - src->bytes_read) <
588 GMYTHTV_TRANSFER_MAX_BUFFER)) {
592 size_tmp = gmyth_recorder_get_file_position (src->spawn_livetv->recorder);
593 if (size_tmp > (src->content_size + GMYTHTV_TRANSFER_MAX_BUFFER))
594 src->content_size = size_tmp;
595 else if (size_tmp > 0 && --max_tries > 0)
597 GST_LOG_OBJECT (src, "GET_POSITION: file_position = %lld", size_tmp);
598 /* sets the last content size amount before it can be updated */
599 src->prev_content_size = src->content_size;
602 return src->content_size;
607 gst_mythtv_src_do_seek (GstBaseSrc * base, GstSegment * segment)
609 GstMythtvSrc *src = GST_MYTHTV_SRC (base);
610 gint64 new_offset = -1;
611 gint64 actual_seek = segment->start;
614 GST_LOG_OBJECT (src, "seek, segment: %" GST_SEGMENT_FORMAT, segment);
616 if (segment->format == GST_FORMAT_TIME) {
620 "Trying to seek at the value (actual_seek = %lld, read_offset = %lld)",
621 actual_seek, src->read_offset);
622 /* verify if it needs to seek */
623 if (src->read_offset != actual_seek) {
625 /* if ( gmyth_backend_info_is_local_file(src->backend_info) ) */
626 if ( IS_GMYTH_FILE_LOCAL(src->file) )
628 gmyth_file_local_seek ( GMYTH_FILE_LOCAL(src->file), segment->start, G_SEEK_SET);
629 else if ( IS_GMYTH_FILE_TRANSFER(src->file) )
631 gmyth_file_transfer_seek ( GMYTH_FILE_TRANSFER(src->file), segment->start, SEEK_SET);
634 "Segment offset start = %lld, SRC Offset = %lld, NEW actual backend SEEK Offset = %lld.",
635 segment->start, src->read_offset, new_offset);
636 if (G_UNLIKELY (new_offset < 0)) {
642 src->read_offset = new_offset;
645 GST_INFO_OBJECT (src, "Failed to set the SEEK on segment!");
655 GST_DEBUG_OBJECT (src, "EOS found on seeking!!!");
661 /* create a socket for connecting to remote server */
663 gst_mythtv_src_start (GstBaseSrc * bsrc)
665 GstMythtvSrc *src = GST_MYTHTV_SRC (bsrc);
667 GString *chain_id_local = NULL;
668 GMythURI *gmyth_uri = NULL;
671 if (src->unique_setup == FALSE) {
672 src->unique_setup = TRUE;
677 gmyth_uri = gmyth_uri_new_with_value( src->uri_name );
679 src->backend_info = gmyth_backend_info_new_with_uri (src->uri_name);
680 src->live_tv |= gmyth_uri_is_livetv( gmyth_uri );
681 /* testing UPnP... */
682 /* gmyth_backend_info_set_hostname( src->backend_info, NULL ); */
683 if ( src->live_tv ) {
684 src->spawn_livetv = gmyth_livetv_new (src->backend_info);
686 gchar* ch = gmyth_uri_get_channel_name( gmyth_uri );
688 src->channel_name = ch;
690 if (src->channel_name != NULL) {
691 if (gmyth_livetv_channel_name_setup (src->spawn_livetv, src->channel_name) == FALSE) {
692 GST_INFO_OBJECT (src, "LiveTV setup felt down on error");
697 if (gmyth_livetv_setup (src->spawn_livetv) == FALSE) {
698 GST_INFO_OBJECT (src, "LiveTV setup felt down on error");
704 /* testing change channel... */
705 /* gmyth_recorder_change_channel( src->spawn_livetv->recorder, CHANNEL_DIRECTION_UP ); */
707 src->file = GMYTH_FILE( gmyth_livetv_create_file_transfer (src->spawn_livetv) );
709 if (NULL == src->file) {
710 GST_INFO_OBJECT (src, "[LiveTV] FileTransfer equals to NULL");
715 if ( !gmyth_file_transfer_open( GMYTH_FILE_TRANSFER(src->file), src->spawn_livetv->uri != NULL ?
716 gmyth_uri_get_path(src->spawn_livetv->uri) :
717 src->spawn_livetv->proginfo->pathname->str ) )
719 GST_INFO_OBJECT (src, "Error: couldn't open the FileTransfer from LiveTV source!" );
720 g_object_unref( src->file );
726 if ( gmyth_uri_is_local_file(gmyth_uri) )
728 src->file = GMYTH_FILE(gmyth_file_local_new(src->backend_info));
729 ret = gmyth_file_local_open ( GMYTH_FILE_LOCAL( src->file ) );
731 src->file = GMYTH_FILE(gmyth_file_transfer_new(src->backend_info));
732 ret = gmyth_file_transfer_open ( GMYTH_FILE_TRANSFER(src->file), src->uri_name);
735 } /* if (else) - recorded FileTransfer */
737 if (NULL == src->file) {
738 GST_INFO_OBJECT (src, "FileTransfer is NULL");
741 /*GST_INFO_OBJECT( src, "uri = %s", src->spawn_livetv->file); */
744 #ifndef GST_DISABLE_GST_DEBUG
745 if (src->mythtv_msgs_dbg)
746 GST_INFO_OBJECT (src,
747 "MythTV FileTransfer request failed when setting up socket connection!");
749 goto begin_req_failed;
752 GST_INFO_OBJECT (src,
753 "MythTV FileTransfer filesize = %lld, content_size = %lld!",
754 gmyth_file_get_filesize( src->file ), src->content_size);
756 src->content_size = gmyth_file_get_filesize (src->file);
758 src->do_start = FALSE;
760 /* this is used for the buffer cache */
761 src->bytes_queue = g_byte_array_sized_new (INTERNAL_BUFFER_SIZE);
762 src->buffer_remain = 0;
764 gst_pad_push_event (GST_BASE_SRC_PAD (GST_BASE_SRC (src)),
765 gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0,
766 src->content_size, 0));
769 /*if ( gmyth_uri != NULL )
771 g_object_unref( gmyth_uri );
775 if (chain_id_local != NULL) {
776 g_string_free (chain_id_local, TRUE);
777 chain_id_local = NULL;
785 if (src->spawn_livetv != NULL)
786 g_object_unref (src->spawn_livetv);
788 GST_ELEMENT_ERROR (src, LIBRARY, INIT,
789 (NULL), ("Could not initialize MythTV library (%i, %s)", ret,
795 GST_ELEMENT_ERROR (src, LIBRARY, INIT,
796 (NULL), ("Could not begin request sent to MythTV server (%i, %s)", ret,
804 gst_mythtv_src_get_size (GstBaseSrc * bsrc, guint64 * size)
806 GstMythtvSrc *src = GST_MYTHTV_SRC (bsrc);
809 GST_LOG_OBJECT (src, "Differs from previous content size: %d (max.: %d)",
810 abs (src->content_size - src->prev_content_size),
811 GMYTHTV_TRANSFER_MAX_BUFFER);
815 } else if (src->live_tv && src->enable_timing_position
816 && (abs (src->content_size - src->bytes_read) <
817 GMYTHTV_TRANSFER_MAX_BUFFER)) {
820 gmyth_recorder_get_file_position (src->spawn_livetv->recorder);
821 if (new_offset > 0 && new_offset > src->content_size) {
822 src->content_size = new_offset;
823 } else if (new_offset < src->content_size) {
824 src->update_prog_chain = TRUE;
829 *size = src->content_size;
830 GST_LOG_OBJECT (src, "Content size = %lld", src->content_size);
836 /* close the socket and associated resources
837 * used both to recover from errors and go to NULL state */
839 gst_mythtv_src_stop (GstBaseSrc * bsrc)
841 GstMythtvSrc *src = GST_MYTHTV_SRC (bsrc);
843 gst_mythtv_src_clear (src);
845 /* src->eos = FALSE; */
851 gst_mythtv_src_handle_event (GstPad * pad, GstEvent * event)
853 GstMythtvSrc *src = GST_MYTHTV_SRC (GST_PAD_PARENT (pad));
854 gint64 cont_size = 0;
855 gboolean ret = FALSE;
857 switch (GST_EVENT_TYPE (event)) {
859 GST_WARNING_OBJECT (src, "Got EOS event");
862 cont_size = gst_mythtv_src_get_position (src);
863 if (cont_size > src->content_size) {
864 src->content_size = cont_size;
868 gst_element_set_state (GST_ELEMENT (src), GST_STATE_NULL);
869 gst_element_set_locked_state (GST_ELEMENT (src), FALSE);
874 ret = gst_pad_event_default (pad, event);
881 gst_mythtv_src_is_seekable (GstBaseSrc * push_src)
887 gst_mythtv_src_handle_query (GstPad * pad, GstQuery * query)
889 gboolean res = FALSE;
890 GstMythtvSrc *myth = GST_MYTHTV_SRC (gst_pad_get_parent (pad));
893 switch (GST_QUERY_TYPE (query)) {
894 case GST_QUERY_POSITION:
896 gst_query_parse_position (query, &formt, NULL);
897 if (formt == GST_FORMAT_BYTES) {
898 gst_query_set_position (query, formt, myth->read_offset);
899 GST_DEBUG_OBJECT (myth, "POS %" G_GINT64_FORMAT, myth->read_offset);
901 } else if (formt == GST_FORMAT_TIME) {
902 res = gst_pad_query_default (pad, query);
906 case GST_QUERY_DURATION:
909 if (myth->duration != 0) {
913 fps = nuv->h->i_fpsn / nuv->h->i_fpsd;
915 gst_util_uint64_scale_int (GST_SECOND, nuv->h->i_video_blocks, fps);
919 gst_query_parse_duration (query, &formt, NULL);
920 if (formt == GST_FORMAT_BYTES) {
921 gst_query_set_duration (query, formt, myth->content_size);
922 GST_DEBUG_OBJECT (myth, "SIZE %" G_GINT64_FORMAT, myth->content_size);
924 } else if (formt == GST_FORMAT_TIME) {
925 res = gst_pad_query_default (pad, query);
931 res = gst_pad_query_default (pad, query);
936 gst_object_unref (myth);
941 static GstStateChangeReturn
942 gst_mythtv_src_change_state (GstElement * element, GstStateChange transition)
944 GstStateChangeReturn ret = GST_STATE_CHANGE_FAILURE;
945 GstMythtvSrc *src = GST_MYTHTV_SRC (element);
947 switch (transition) {
948 case GST_STATE_CHANGE_NULL_TO_READY:
950 case GST_STATE_CHANGE_READY_TO_PAUSED:
951 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
953 if (!gmyth_recorder_send_frontend_ready_command (src->spawn_livetv->
955 GST_WARNING_OBJECT (src,
956 "Couldn't send the FRONTEND_READY message to the backend!");
958 GST_DEBUG_OBJECT (src, "FRONTEND_READY was sent to the backend");
965 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
966 if (ret == GST_STATE_CHANGE_FAILURE)
969 switch (transition) {
970 case GST_STATE_CHANGE_READY_TO_NULL:
972 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
973 case GST_STATE_CHANGE_PAUSED_TO_READY:
983 gst_mythtv_src_set_property (GObject * object, guint prop_id,
984 const GValue * value, GParamSpec * pspec)
986 GstMythtvSrc *mythtvsrc = GST_MYTHTV_SRC (object);
988 GST_OBJECT_LOCK (mythtvsrc);
992 if (!g_value_get_string (value)) {
993 GST_WARNING ("location property cannot be NULL");
997 if (mythtvsrc->uri_name != NULL) {
998 g_free (mythtvsrc->uri_name);
999 mythtvsrc->uri_name = NULL;
1001 mythtvsrc->uri_name = g_value_dup_string (value);
1005 #ifndef GST_DISABLE_GST_DEBUG
1006 case PROP_GMYTHTV_DBG:
1008 mythtvsrc->mythtv_msgs_dbg = g_value_get_boolean (value);
1012 case PROP_GMYTHTV_VERSION:
1014 mythtvsrc->mythtv_version = g_value_get_int (value);
1017 case PROP_GMYTHTV_LIVEID:
1019 mythtvsrc->live_tv_id = g_value_get_int (value);
1022 case PROP_GMYTHTV_LIVE:
1024 mythtvsrc->live_tv = g_value_get_boolean (value);
1027 case PROP_GMYTHTV_ENABLE_TIMING_POSITION:
1029 mythtvsrc->enable_timing_position = g_value_get_boolean (value);
1032 case PROP_GMYTHTV_LIVE_CHAINID:
1034 if (!g_value_get_string (value)) {
1035 GST_WARNING ("MythTV Live chainid property cannot be NULL");
1039 if (mythtvsrc->live_chain_id != NULL) {
1040 g_free (mythtvsrc->live_chain_id);
1041 mythtvsrc->live_chain_id = NULL;
1043 mythtvsrc->live_chain_id = g_value_dup_string (value);
1046 case PROP_GMYTHTV_CHANNEL_NUM:
1048 mythtvsrc->channel_name = g_value_dup_string (value);
1052 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1056 GST_OBJECT_UNLOCK (mythtvsrc);
1060 gst_mythtv_src_get_property (GObject * object, guint prop_id,
1061 GValue * value, GParamSpec * pspec)
1063 GstMythtvSrc *mythtvsrc = GST_MYTHTV_SRC (object);
1065 GST_OBJECT_LOCK (mythtvsrc);
1069 g_value_set_string (value, mythtvsrc->uri_name);
1072 #ifndef GST_DISABLE_GST_DEBUG
1073 case PROP_GMYTHTV_DBG:
1074 g_value_set_boolean (value, mythtvsrc->mythtv_msgs_dbg);
1077 case PROP_GMYTHTV_VERSION:
1079 g_value_set_int (value, mythtvsrc->mythtv_version);
1082 case PROP_GMYTHTV_LIVEID:
1084 g_value_set_int (value, mythtvsrc->live_tv_id);
1087 case PROP_GMYTHTV_LIVE:
1088 g_value_set_boolean (value, mythtvsrc->live_tv);
1090 case PROP_GMYTHTV_ENABLE_TIMING_POSITION:
1091 g_value_set_boolean (value, mythtvsrc->enable_timing_position);
1093 case PROP_GMYTHTV_LIVE_CHAINID:
1095 g_value_set_string (value, mythtvsrc->live_chain_id);
1098 case PROP_GMYTHTV_CHANNEL_NUM:
1100 g_value_set_string (value, mythtvsrc->channel_name);
1104 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1107 GST_OBJECT_UNLOCK (mythtvsrc);
1111 plugin_init (GstPlugin * plugin)
1113 return gst_element_register (plugin, "mythtvsrc", GST_RANK_NONE,
1114 GST_TYPE_MYTHTV_SRC);
1117 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1121 plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN);
1124 /*** GSTURIHANDLER INTERFACE *************************************************/
1126 gst_mythtv_src_uri_get_type (void)
1132 gst_mythtv_src_uri_get_protocols (void)
1134 static gchar *protocols[] = { "myth", "myths", NULL };
1139 static const gchar *
1140 gst_mythtv_src_uri_get_uri (GstURIHandler * handler)
1142 GstMythtvSrc *src = GST_MYTHTV_SRC (handler);
1144 return src->uri_name;
1148 gst_mythtv_src_uri_set_uri (GstURIHandler * handler, const gchar * uri)
1150 GstMythtvSrc *src = GST_MYTHTV_SRC (handler);
1154 protocol = gst_uri_get_protocol (uri);
1155 if ((strcmp (protocol, "myth") != 0) && (strcmp (protocol, "myths") != 0)) {
1160 g_object_set (src, "location", uri, NULL);
1166 gst_mythtv_src_uri_handler_init (gpointer g_iface, gpointer iface_data)
1168 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
1170 iface->get_type = gst_mythtv_src_uri_get_type;
1171 iface->get_protocols = gst_mythtv_src_uri_get_protocols;
1172 iface->get_uri = gst_mythtv_src_uri_get_uri;
1173 iface->set_uri = gst_mythtv_src_uri_set_uri;
1177 size_header_handler (void *userdata, const char *value)
1179 GstMythtvSrc *src = GST_MYTHTV_SRC (userdata);
1181 GST_DEBUG_OBJECT (src, "content size = %lld bytes", src->content_size);