2 * GStreamer MythTV Plug-in Copyright (C) <2006> Rosfran Borges
3 * <rosfran.borges@indt.org.br> This library is free software; you can
4 * redistribute it and/or modify it under the terms of the GNU Library
5 * General Public License as published by the Free Software Foundation;
6 * either version 2 of the License, or (at your option) any later version.
7 * This library is distributed in the hope that it will be useful, but
8 * WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library
10 * General Public License for more details. You should have received a copy
11 * of the GNU Library General Public License along with this library; if
12 * not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite
13 * 330, Boston, MA 02111-1307, USA.
16 * SECTION:element-mythtvsrc
20 * MythTVSrc allows to access a remote MythTV backend streaming Video/Audio server,
21 * and to render audio and video content through a TCP/IP connection to a specific
22 * port on this server, and based on a known MythTV protocol that is based on
23 * some message passing, such as REQUEST_BLOCK on a specified number of bytes, to get
24 * some chunk of remote file data.
25 * You should pass the information aboute the remote MythTV backend server
26 * through the <link linkend="GstMythTVSrc--location">location</link> property.
28 * <title>Examples</title>
30 * If you want to get the LiveTV content (set channel, TV tuner, RemoteEncoder,
32 * put the following URI:
35 * myth://xxx.xxx.xxx.xxx:6543/livetv?channel=BBC
38 * This URI will say to the gmyth library to configure the Recorder instance (used to
39 * change the channel, start the TV multimedia content transmition, etc.), using
40 * the IP address (xxx.xxx.xxx.xxx) and port number (6543) of the MythTV backend
41 * server, and setting the channel name to "BBC".
43 * To get a already recorded the MythTV NUV file, put the following URI:
46 * myth://xxx.xxx.xxx.xxx:6543/filename.nuv
49 * This URI will say to the gmyth library to configure the Recorder instance (used to
50 * change the channel, start the TV multimedia content transmition, etc.), using
51 * the IP address (xxx.xxx.xxx.xxx) and port number (6543) of the MythTV backend
52 * server, and setting the channel name to "BBC".
54 * Another possible way to use the LiveTV content, and just in the case you want to
55 * use the mysql database, put the location URI in the following format:
58 * myth://mythtv:mythtv@xxx.xxx.xxx.xxx:6543/?mythconverg&channel=9
61 * Where the first field is the protocol (myth), the second and third are user
62 * name (mythtv) and password (mythtv), then backend host name and port number,
63 * and the last field is the database name (mythconverg).
71 #include "gstmythtvsrc.h"
72 #include <gmyth/gmyth_file.h>
73 #include <gmyth/gmyth_file_transfer.h>
74 #include <gmyth/gmyth_file_local.h>
75 #include <gmyth/gmyth_livetv.h>
77 #include <gmyth/gmyth_socket.h>
78 #include <gmyth/gmyth_tvchain.h>
83 GST_DEBUG_CATEGORY_STATIC(mythtvsrc_debug);
84 #define GST_GMYTHTV_ID_NUM 1
85 #define GST_GMYTHTV_CHANNEL_DEFAULT_NUM (-1)
86 #define GMYTHTV_VERSION_DEFAULT 30
87 #define GMYTHTV_TRANSFER_MAX_WAITS 100
88 #define GMYTHTV_TRANSFER_MAX_RESENDS 2
89 #define GMYTHTV_TRANSFER_MAX_BUFFER (128*1024)
90 #define MAX_READ_SIZE (4*1024)
91 #define GST_FLOW_ERROR_NO_DATA (-101)
92 #define REQUEST_MAX_SIZE (64*1024)
93 #define INTERNAL_BUFFER_SIZE (90*1024)
95 static const GstElementDetails gst_mythtv_src_details =
96 GST_ELEMENT_DETAILS("MythTV client source",
98 "Control and receive data as a client over the network "
99 "via raw socket connections using the MythTV protocol",
100 "Rosfran Borges <rosfran.borges@indt.org.br>");
102 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE("src",
105 GST_STATIC_CAPS_ANY);
107 * GST_STATIC_CAPS ("video/x-nuv"));
113 PROP_GMYTHTV_VERSION,
116 PROP_GMYTHTV_LIVE_CHAINID,
117 PROP_GMYTHTV_ENABLE_TIMING_POSITION,
118 PROP_GMYTHTV_CHANNEL_NUM
121 static void gst_mythtv_src_clear(GstMythtvSrc * mythtv_src);
123 static void gst_mythtv_src_finalize(GObject * gobject);
125 static GstFlowReturn gst_mythtv_src_create(GstPushSrc * psrc,
126 GstBuffer ** outbuf);
128 static gboolean gst_mythtv_src_start(GstBaseSrc * bsrc);
129 static gboolean gst_mythtv_src_stop(GstBaseSrc * bsrc);
130 static gboolean gst_mythtv_src_get_size(GstBaseSrc * bsrc, guint64 * size);
131 static gboolean gst_mythtv_src_is_seekable(GstBaseSrc * push_src);
133 static gboolean gst_mythtv_src_do_seek(GstBaseSrc * base,
134 GstSegment * segment);
136 static GstStateChangeReturn
137 gst_mythtv_src_change_state(GstElement * element,
138 GstStateChange transition);
140 static void gst_mythtv_src_set_property(GObject * object,
142 const GValue * value,
144 static void gst_mythtv_src_get_property(GObject * object,
145 guint prop_id, GValue * value,
148 static void gst_mythtv_src_uri_handler_init(gpointer g_iface,
149 gpointer iface_data);
151 static gboolean gst_mythtv_src_handle_query(GstPad * pad,
154 static gboolean gst_mythtv_src_handle_event(GstPad * pad,
157 static GMythFileReadResult do_read_request_response(GstMythtvSrc * src,
159 GByteArray * data_ptr);
162 _urihandler_init(GType type)
164 static const GInterfaceInfo urihandler_info = {
165 gst_mythtv_src_uri_handler_init,
170 g_type_add_interface_static(type, GST_TYPE_URI_HANDLER,
173 GST_DEBUG_CATEGORY_INIT(mythtvsrc_debug, "mythtvsrc", 0, "MythTV src");
176 GST_BOILERPLATE_FULL(GstMythtvSrc, gst_mythtv_src, GstPushSrc,
177 GST_TYPE_PUSH_SRC, _urihandler_init)
178 static void gst_mythtv_src_base_init(gpointer g_class)
180 GstElementClass *element_class = GST_ELEMENT_CLASS(g_class);
182 gst_element_class_add_pad_template(element_class,
183 gst_static_pad_template_get
186 gst_element_class_set_details(element_class, &gst_mythtv_src_details);
188 element_class->change_state = gst_mythtv_src_change_state;
193 gst_mythtv_src_class_init(GstMythtvSrcClass * klass)
195 GObjectClass *gobject_class;
196 GstPushSrcClass *gstpushsrc_class;
197 GstBaseSrcClass *gstbasesrc_class;
199 gobject_class = (GObjectClass *) klass;
200 gstbasesrc_class = (GstBaseSrcClass *) klass;
201 gstpushsrc_class = (GstPushSrcClass *) klass;
203 gobject_class->set_property = gst_mythtv_src_set_property;
204 gobject_class->get_property = gst_mythtv_src_get_property;
205 gobject_class->finalize = gst_mythtv_src_finalize;
207 g_object_class_install_property
208 (gobject_class, PROP_LOCATION,
209 g_param_spec_string("location", "Location",
210 "The location. In the form:"
211 "\n\t\t\tmyth://a.com/file.nuv"
212 "\n\t\t\tmyth://a.com:23223/file.nuv "
213 "\n\t\t\ta.com/file.nuv - default scheme 'myth'",
214 "", G_PARAM_READWRITE));
216 g_object_class_install_property
217 (gobject_class, PROP_GMYTHTV_VERSION,
218 g_param_spec_int("mythtv-version", "mythtv-version",
219 "Change MythTV version", 26, 30, 26,
222 g_object_class_install_property
223 (gobject_class, PROP_GMYTHTV_LIVEID,
224 g_param_spec_int("mythtv-live-id", "mythtv-live-id",
225 "Change MythTV version",
226 0, 200, GST_GMYTHTV_ID_NUM, G_PARAM_READWRITE));
228 g_object_class_install_property
229 (gobject_class, PROP_GMYTHTV_LIVE_CHAINID,
230 g_param_spec_string("mythtv-live-chainid", "mythtv-live-chainid",
231 "Sets the MythTV chain ID (from TV Chain)",
232 "", G_PARAM_READWRITE));
234 g_object_class_install_property
235 (gobject_class, PROP_GMYTHTV_LIVE,
236 g_param_spec_boolean("mythtv-live", "mythtv-live",
237 "Enable MythTV Live TV content streaming",
238 FALSE, G_PARAM_READWRITE));
240 g_object_class_install_property
241 (gobject_class, PROP_GMYTHTV_ENABLE_TIMING_POSITION,
242 g_param_spec_boolean("mythtv-enable-timing-position",
243 "mythtv-enable-timing-position",
244 "Enable MythTV Live TV content size continuous updating",
245 FALSE, G_PARAM_READWRITE));
247 g_object_class_install_property
248 (gobject_class, PROP_GMYTHTV_CHANNEL_NUM,
249 g_param_spec_string("mythtv-channel", "mythtv-channel",
250 "Change MythTV channel number",
251 "", G_PARAM_READWRITE));
253 gstbasesrc_class->start = gst_mythtv_src_start;
254 gstbasesrc_class->stop = gst_mythtv_src_stop;
255 gstbasesrc_class->get_size = gst_mythtv_src_get_size;
256 gstbasesrc_class->is_seekable = gst_mythtv_src_is_seekable;
258 gstbasesrc_class->do_seek = gst_mythtv_src_do_seek;
259 gstpushsrc_class->create = gst_mythtv_src_create;
261 GST_DEBUG_CATEGORY_INIT(mythtvsrc_debug, "mythtvsrc", 0,
262 "MythTV Client Source");
266 gst_mythtv_src_init(GstMythtvSrc * this, GstMythtvSrcClass * g_class)
269 this->unique_setup = FALSE;
270 this->mythtv_version = GMYTHTV_VERSION_DEFAULT;
271 this->state = GST_MYTHTV_SRC_FILE_TRANSFER;
272 this->bytes_read = 0;
273 this->prev_content_size = 0;
274 this->content_size = 0;
275 this->read_offset = 0;
276 this->content_size_last = 0;
277 this->live_tv = FALSE;
278 this->enable_timing_position = FALSE;
279 this->update_prog_chain = FALSE;
280 this->user_agent = g_strdup("mythtvsrc");
281 this->update_prog_chain = FALSE;
282 this->channel_name = NULL;
284 this->bytes_queue = NULL;
285 this->wait_to_transfer = 0;
286 gst_base_src_set_format(GST_BASE_SRC(this), GST_FORMAT_BYTES);
287 gst_pad_set_event_function(GST_BASE_SRC_PAD(GST_BASE_SRC(this)),
288 gst_mythtv_src_handle_event);
289 gst_pad_set_query_function(GST_BASE_SRC_PAD(GST_BASE_SRC(this)),
290 gst_mythtv_src_handle_query);
295 gst_mythtv_src_clear(GstMythtvSrc * mythtv_src)
297 mythtv_src->unique_setup = FALSE;
299 if (mythtv_src->spawn_livetv) {
300 g_object_unref(mythtv_src->spawn_livetv);
301 mythtv_src->spawn_livetv = NULL;
304 if (mythtv_src->file) {
305 g_object_unref(mythtv_src->file);
306 mythtv_src->file = NULL;
309 if (mythtv_src->backend_info) {
310 g_object_unref(mythtv_src->backend_info);
311 mythtv_src->backend_info = NULL;
314 if (mythtv_src->bytes_queue) {
315 g_byte_array_free(mythtv_src->bytes_queue, TRUE);
316 mythtv_src->bytes_queue = NULL;
321 gst_mythtv_src_finalize(GObject * gobject)
323 GstMythtvSrc *this = GST_MYTHTV_SRC(gobject);
325 gst_mythtv_src_clear(this);
327 if (this->uri_name) {
328 g_free(this->uri_name);
329 this->uri_name = NULL;
332 if (this->user_agent) {
333 g_free(this->user_agent);
334 this->user_agent = NULL;
337 G_OBJECT_CLASS(parent_class)->finalize(gobject);
340 static GMythFileReadResult
341 do_read_request_response(GstMythtvSrc * src, guint size,
342 GByteArray * data_ptr)
345 guint sizetoread = size;
346 gint max_iters = GMYTHTV_TRANSFER_MAX_RESENDS;
347 GMythFileReadResult result;
349 GST_LOG_OBJECT(src, "Starting: Reading %d bytes...", sizetoread);
352 * Loop sending the Myth File Transfer request: Retry whilst
353 * authentication fails and we supply it.
356 while (sizetoread == size && --max_iters > 0) {
358 * 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,
364 else if (IS_GMYTH_FILE_TRANSFER(src->file))
366 gmyth_file_transfer_read(GMYTH_FILE_TRANSFER(src->file),
367 data_ptr, sizetoread,
370 if (data_ptr->len > 0) {
371 read += data_ptr->len;
372 sizetoread -= data_ptr->len;
373 } else if (data_ptr->len <= 0) {
374 if (src->live_tv == FALSE) {
375 result = GMYTH_FILE_READ_EOF;
378 if (result == GMYTH_FILE_READ_ERROR) { /* -314 */
380 "[LiveTV] FileTransfer READ_ERROR!");
382 } else if (result == GMYTH_FILE_READ_NEXT_PROG_CHAIN) { /* -315
385 "[LiveTV] FileTransfer - Go to athe next program chain!");
386 src->update_prog_chain = TRUE;
394 * else if (data_ptr->len == 0) goto done;
396 if (read == sizetoread)
400 if ((read < 0 && !src->live_tv) || max_iters == 0) {
401 result = GMYTH_FILE_READ_EOF;
415 gst_mythtv_src_create(GstPushSrc * psrc, GstBuffer ** outbuf)
418 GstFlowReturn ret = GST_FLOW_OK;
419 guint buffer_size_inter = 0;
421 src = GST_MYTHTV_SRC(psrc);
424 * The caller should know the number of bytes and not read beyond EOS.
426 if (G_UNLIKELY(src->eos))
428 GST_DEBUG_OBJECT(src, "offset = %" G_GUINT64_FORMAT ", size = %d...",
429 src->read_offset, MAX_READ_SIZE);
431 GST_DEBUG_OBJECT(src, "Create: buffer_remain: %d, buffer_size = %d.",
432 (gint) src->buffer_remain, src->bytes_queue->len);
434 program_chain_changed:
436 * just get from the byte array, no network effort...
438 if ((src->buffer_remain = src->bytes_queue->len) < MAX_READ_SIZE) {
440 GMythFileReadResult result = GMYTH_FILE_READ_OK;
443 buffer_size_inter = (INTERNAL_BUFFER_SIZE - src->buffer_remain);
445 if (buffer_size_inter > REQUEST_MAX_SIZE)
446 buffer_size_inter = REQUEST_MAX_SIZE;
448 buffer = g_byte_array_new();
450 result = do_read_request_response(src, buffer_size_inter, buffer);
453 * got the next program info?
455 if (G_UNLIKELY(src->update_prog_chain) ||
456 (result == GMYTH_FILE_READ_NEXT_PROG_CHAIN)) {
457 GST_DEBUG_OBJECT(src,
458 "Update PROGRAM CHAIN!!! buffer_size = %d.",
459 src->bytes_queue->len);
460 gst_pad_push_event(GST_BASE_SRC_PAD(GST_BASE_SRC(psrc)),
462 (GST_EVENT_CUSTOM_DOWNSTREAM, NULL));
464 * gst_pad_push_event (GST_BASE_SRC_PAD (GST_BASE_SRC (psrc)),
465 * gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_BYTES, 0,
468 src->update_prog_chain = FALSE;
471 if (result == GMYTH_FILE_READ_NEXT_PROG_CHAIN) {
473 * if (buffer != NULL) { g_byte_array_free (buffer, TRUE);
474 * buffer = NULL; } goto program_chain_changed;
476 } else if (result == GMYTH_FILE_READ_OK) {
478 * remove wasteful, NUV file header data
481 * buffer = g_byte_array_remove_range( buffer, 0, 512 );
484 * TODO: need to send a new segment event to NUVDemux?
486 // gst_pad_push_event (GST_BASE_SRC_PAD (GST_BASE_SRC
488 // gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_BYTES,
492 * goto change_progchain;
499 if (G_UNLIKELY(buffer->len < 0)) {
500 if (buffer != NULL) {
501 g_byte_array_free(buffer, TRUE);
506 (result == GMYTH_FILE_READ_NEXT_PROG_CHAIN))
507 goto change_progchain;
510 } else if (G_UNLIKELY(buffer->len == 0)) {
512 if (buffer != NULL) {
513 g_byte_array_free(buffer, TRUE);
524 goto program_chain_changed;
528 g_byte_array_append(src->bytes_queue, buffer->data,
530 if (buffer->len > buffer_size_inter)
531 GST_WARNING_OBJECT(src,
532 "INCREASED buffer size! Backend sent more than we ask him... (%d)",
533 abs(buffer->len - buffer_size_inter));
535 src->buffer_remain += buffer->len;
537 if (buffer != NULL) {
538 g_byte_array_free(buffer, TRUE);
543 * GST_DEBUG_OBJECT (src, "BYTES READ (actual) = %d, BYTES READ
544 * (cumulative) = %llu, " "OFFSET = %llu, CONTENT SIZE = %llu.",
545 * read, src->bytes_read, src->read_offset, src->content_size);
550 buffer_size = (src->buffer_remain < MAX_READ_SIZE) ?
554 *outbuf = gst_buffer_new();
557 * GST_DEBUG_OBJECT (src, "read from network? %s!, buffer_remain =
558 * %d", (buffer_size_inter == 0) ? "NO, got from buffer" : "YES, go
559 * see the backend's log file", src->buffer_remain);
562 GST_BUFFER_SIZE(*outbuf) = buffer_size;
563 GST_BUFFER_MALLOCDATA(*outbuf) = g_malloc0(GST_BUFFER_SIZE(*outbuf));
564 GST_BUFFER_DATA(*outbuf) = GST_BUFFER_MALLOCDATA(*outbuf);
565 g_memmove(GST_BUFFER_DATA((*outbuf)), src->bytes_queue->data,
566 GST_BUFFER_SIZE(*outbuf));
567 GST_BUFFER_OFFSET(*outbuf) = src->read_offset;
568 GST_BUFFER_OFFSET_END(*outbuf) =
569 src->read_offset + GST_BUFFER_SIZE(*outbuf);
571 src->buffer_remain -= GST_BUFFER_SIZE(*outbuf);
573 src->read_offset += GST_BUFFER_SIZE(*outbuf);
574 src->bytes_read += GST_BUFFER_SIZE(*outbuf);
575 // GST_DEBUG_OBJECT (src, "Buffer output with size: %d",
576 // GST_BUFFER_SIZE (*outbuf));
579 * flushs the newly buffer got from byte array
582 g_byte_array_remove_range(src->bytes_queue, 0, buffer_size);
585 (!src->live_tv && (src->bytes_read >= src->content_size))
595 return GST_FLOW_UNEXPECTED;
602 GST_ELEMENT_ERROR(src, RESOURCE, READ,
603 (NULL), ("Could not read any bytes (%i, %s)",
604 read, src->uri_name));
605 return GST_FLOW_ERROR;
609 GST_ELEMENT_ERROR(src, RESOURCE, READ,
611 ("Seek failed, go to the next program info... (%i, %s)",
612 read, src->uri_name));
615 * TODO: need to send a new segment event to NUVDemux?
617 gst_pad_push_event(GST_BASE_SRC_PAD(GST_BASE_SRC(psrc)),
618 gst_event_new_new_segment(TRUE, 1.0,
622 goto program_chain_changed;
628 gst_mythtv_src_get_position(GstMythtvSrc * src)
634 if (src->live_tv == TRUE && (abs(src->content_size - src->bytes_read) <
635 GMYTHTV_TRANSFER_MAX_BUFFER)) {
640 gmyth_recorder_get_file_position(src->spawn_livetv->recorder);
641 if (size_tmp > (src->content_size + GMYTHTV_TRANSFER_MAX_BUFFER))
642 src->content_size = size_tmp;
643 else if (size_tmp > 0 && --max_tries > 0)
645 GST_LOG_OBJECT(src, "GET_POSITION: file_position = %lld",
648 * sets the last content size amount before it can be updated
650 src->prev_content_size = src->content_size;
653 return src->content_size;
658 gst_mythtv_src_do_seek(GstBaseSrc * base, GstSegment * segment)
660 GstMythtvSrc *src = GST_MYTHTV_SRC(base);
661 gint64 new_offset = -1;
662 gint64 actual_seek = segment->start;
665 GST_LOG_OBJECT(src, "seek, segment: %" GST_SEGMENT_FORMAT, segment);
667 if (segment->format == GST_FORMAT_TIME) {
671 "Trying to seek at the value (actual_seek = %lld, read_offset = %lld)",
672 actual_seek, src->read_offset);
674 * verify if it needs to seek
676 if (src->read_offset != actual_seek) {
679 * if ( gmyth_backend_info_is_local_file(src->backend_info) )
681 if (IS_GMYTH_FILE_LOCAL(src->file))
683 gmyth_file_local_seek(GMYTH_FILE_LOCAL(src->file),
684 segment->start, G_SEEK_SET);
685 else if (IS_GMYTH_FILE_TRANSFER(src->file))
687 gmyth_file_transfer_seek(GMYTH_FILE_TRANSFER(src->file),
688 segment->start, SEEK_SET);
691 "Segment offset start = %lld, SRC Offset = %lld, NEW actual backend SEEK Offset = %lld.",
692 segment->start, src->read_offset, new_offset);
693 if (G_UNLIKELY(new_offset < 0)) {
699 src->read_offset = new_offset;
702 GST_INFO_OBJECT(src, "Failed to set the SEEK on segment!");
712 GST_DEBUG_OBJECT(src, "EOS found on seeking!!!");
719 * create a socket for connecting to remote server
722 gst_mythtv_src_start(GstBaseSrc * bsrc)
724 GstMythtvSrc *src = GST_MYTHTV_SRC(bsrc);
726 GString *chain_id_local = NULL;
727 GMythURI *gmyth_uri = NULL;
731 if (src->unique_setup == FALSE) {
732 src->unique_setup = TRUE;
737 gmyth_uri = gmyth_uri_new_with_value(src->uri_name);
738 src->backend_info = gmyth_backend_info_new_with_uri(src->uri_name);
739 src->live_tv = gmyth_uri_is_livetv(gmyth_uri);
744 * gmyth_backend_info_set_hostname( src->backend_info, NULL );
747 src->spawn_livetv = gmyth_livetv_new(src->backend_info);
749 gchar *ch = gmyth_uri_get_channel_name(gmyth_uri);
751 src->channel_name = ch;
753 if (src->channel_name != NULL) {
756 gmyth_livetv_channel_name_setup(src->spawn_livetv,
758 if (result == FALSE) {
759 GST_INFO_OBJECT(src, "LiveTV setup felt down on error");
765 if (gmyth_livetv_setup(src->spawn_livetv) == FALSE) {
766 GST_INFO_OBJECT(src, "LiveTV setup felt down on error");
773 * testing change channel...
776 * gmyth_recorder_change_channel( src->spawn_livetv->recorder,
777 * CHANNEL_DIRECTION_UP );
781 GMYTH_FILE(gmyth_livetv_create_file_transfer
782 (src->spawn_livetv));
784 if (NULL == src->file) {
785 GST_INFO_OBJECT(src, "[LiveTV] FileTransfer equals to NULL");
791 * Check if the file is local to this specific client renderer
793 if (gmyth_uri_is_local_file(gmyth_uri))
794 ret = gmyth_file_local_open(GMYTH_FILE_LOCAL(src->file));
797 gmyth_file_transfer_open(GMYTH_FILE_TRANSFER(src->file),
798 src->spawn_livetv->uri !=
799 NULL ? gmyth_uri_get_path(src->
802 src->spawn_livetv->proginfo->
806 * sets the mythtvsrc "location" property
808 g_object_set(src, "location", gmyth_file_get_uri(src->file), NULL);
812 "Error: couldn't open the FileTransfer from LiveTV source!");
813 g_object_unref(src->file);
820 * Check if the file is local to this specific client renderer,
821 * and tries to open a local connection
823 if (gmyth_uri_is_local_file(gmyth_uri)) {
825 GMYTH_FILE(gmyth_file_local_new(src->backend_info));
826 ret = gmyth_file_local_open(GMYTH_FILE_LOCAL(src->file));
829 GMYTH_FILE(gmyth_file_transfer_new(src->backend_info));
831 gmyth_file_transfer_open(GMYTH_FILE_TRANSFER(src->file),
835 } /* if (else) - recorded FileTransfer */
837 if (NULL == src->file) {
838 GST_INFO_OBJECT(src, "FileTransfer is NULL");
842 * GST_INFO_OBJECT( src, "uri = %s", src->spawn_livetv->file);
847 "MythTV FileTransfer request failed when setting up socket connection!");
848 goto begin_req_failed;
852 "MythTV FileTransfer filesize = %lld, content_size = %lld!",
853 gmyth_file_get_filesize(src->file), src->content_size);
855 src->content_size = gmyth_file_get_filesize(src->file);
858 gst_message_new_duration(GST_OBJECT(src), GST_FORMAT_BYTES,
860 gst_element_post_message(GST_ELEMENT(src), msg);
863 src->do_start = FALSE;
866 * this is used for the buffer cache
868 src->bytes_queue = g_byte_array_sized_new(INTERNAL_BUFFER_SIZE);
869 src->buffer_remain = 0;
871 gst_pad_push_event(GST_BASE_SRC_PAD(GST_BASE_SRC(src)),
872 gst_event_new_new_segment(TRUE, 1.0,
874 src->content_size, 0));
877 if (gmyth_uri != NULL) {
878 g_object_unref(gmyth_uri);
882 if (chain_id_local != NULL) {
883 g_string_free(chain_id_local, TRUE);
884 chain_id_local = NULL;
893 if (gmyth_uri != NULL) {
894 g_object_unref(gmyth_uri);
898 if (src->spawn_livetv != NULL) {
899 g_object_unref(src->spawn_livetv);
900 src->spawn_livetv = NULL;
903 GST_ELEMENT_ERROR(src, LIBRARY, INIT,
905 ("Could not initialize MythTV library (%i, %s)", ret,
909 gst_mythtv_src_clear(src);
913 if (gmyth_uri != NULL) {
914 g_object_unref(gmyth_uri);
918 GST_ELEMENT_ERROR(src, LIBRARY, INIT,
920 ("Could not begin request sent to MythTV server (%i, %s)",
921 ret, src->uri_name));
927 gst_mythtv_src_get_size(GstBaseSrc * bsrc, guint64 * size)
929 GstMythtvSrc *src = GST_MYTHTV_SRC(bsrc);
933 "Differs from previous content size: %d (max.: %d)",
934 abs(src->content_size - src->prev_content_size),
935 GMYTHTV_TRANSFER_MAX_BUFFER);
939 } else if (src->live_tv && src->enable_timing_position
940 && (abs(src->content_size - src->bytes_read) <
941 GMYTHTV_TRANSFER_MAX_BUFFER)) {
944 gmyth_recorder_get_file_position(src->spawn_livetv->recorder);
945 if (new_offset > 0 && new_offset > src->content_size) {
946 src->content_size = new_offset;
947 } else if (new_offset < src->content_size) {
948 src->update_prog_chain = TRUE;
953 *size = src->content_size;
954 GST_LOG_OBJECT(src, "Content size = %lld", src->content_size);
961 * close the socket and associated resources used both to recover from
962 * errors and go to NULL state
965 gst_mythtv_src_stop(GstBaseSrc * bsrc)
967 GstMythtvSrc *src = GST_MYTHTV_SRC(bsrc);
969 gst_mythtv_src_clear(src);
979 gst_mythtv_src_handle_event(GstPad * pad, GstEvent * event)
981 GstMythtvSrc *src = GST_MYTHTV_SRC(GST_PAD_PARENT(pad));
982 gint64 cont_size = 0;
985 switch (GST_EVENT_TYPE(event)) {
988 cont_size = gst_mythtv_src_get_position(src);
989 if (cont_size > src->content_size) {
990 src->content_size = cont_size;
994 gst_element_set_state(GST_ELEMENT(src), GST_STATE_NULL);
995 gst_element_set_locked_state(GST_ELEMENT(src), FALSE);
1000 ret = gst_pad_event_default(pad, event);
1003 GST_DEBUG_OBJECT (src, "HANDLE EVENT %d", ret);
1008 gst_mythtv_src_is_seekable(GstBaseSrc * push_src)
1014 gst_mythtv_src_handle_query(GstPad * pad, GstQuery * query)
1016 gboolean res = FALSE;
1017 GstMythtvSrc *myth = GST_MYTHTV_SRC(gst_pad_get_parent(pad));
1021 switch (GST_QUERY_TYPE(query)) {
1022 case GST_QUERY_POSITION:
1023 gst_query_parse_position(query, &formt, NULL);
1024 if (formt == GST_FORMAT_BYTES) {
1025 gst_query_set_position(query, formt, myth->read_offset);
1026 GST_DEBUG_OBJECT(myth, "POS %" G_GINT64_FORMAT,
1029 } else if (formt == GST_FORMAT_TIME) {
1030 res = gst_pad_query_default(pad, query);
1033 case GST_QUERY_DURATION:
1034 gst_query_parse_duration(query, &formt, NULL);
1035 if (formt == GST_FORMAT_BYTES) {
1036 gint64 size = myth->content_size;
1038 gst_query_set_duration(query, GST_FORMAT_BYTES, 10);
1039 GST_DEBUG_OBJECT(myth, "SIZE %" G_GINT64_FORMAT, size);
1041 } else if (formt == GST_FORMAT_TIME) {
1042 res = gst_pad_query_default(pad, query);
1046 res = gst_pad_query_default(pad, query);
1050 gst_object_unref(myth);
1055 static GstStateChangeReturn
1056 gst_mythtv_src_change_state(GstElement * element,
1057 GstStateChange transition)
1059 GstStateChangeReturn ret = GST_STATE_CHANGE_FAILURE;
1060 GstMythtvSrc *src = GST_MYTHTV_SRC(element);
1062 switch (transition) {
1063 case GST_STATE_CHANGE_NULL_TO_READY:
1065 case GST_STATE_CHANGE_READY_TO_PAUSED:
1066 if (!src->uri_name) {
1067 GST_WARNING_OBJECT (src, "Invalid location");
1071 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1073 if (!gmyth_recorder_send_frontend_ready_command
1074 (src->spawn_livetv->recorder))
1075 GST_WARNING_OBJECT(src,
1076 "Couldn't send the FRONTEND_READY message to the backend!");
1078 GST_DEBUG_OBJECT(src, "FRONTEND_READY was sent to the backend");
1085 ret = GST_ELEMENT_CLASS(parent_class)->change_state(element, transition);
1086 if (ret == GST_STATE_CHANGE_FAILURE) {
1091 switch (transition) {
1092 case GST_STATE_CHANGE_READY_TO_NULL:
1093 gst_mythtv_src_clear(src);
1095 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1096 case GST_STATE_CHANGE_PAUSED_TO_READY:
1106 gst_mythtv_src_set_property(GObject * object, guint prop_id,
1107 const GValue * value, GParamSpec * pspec)
1109 GstMythtvSrc *mythtvsrc = GST_MYTHTV_SRC(object);
1111 GST_OBJECT_LOCK(mythtvsrc);
1114 if (!g_value_get_string(value)) {
1115 GST_WARNING("location property cannot be NULL");
1119 if (mythtvsrc->uri_name != NULL) {
1120 g_free(mythtvsrc->uri_name);
1121 mythtvsrc->uri_name = NULL;
1123 mythtvsrc->uri_name = g_value_dup_string(value);
1125 case PROP_GMYTHTV_VERSION:
1126 mythtvsrc->mythtv_version = g_value_get_int(value);
1128 case PROP_GMYTHTV_LIVEID:
1129 mythtvsrc->live_tv_id = g_value_get_int(value);
1131 case PROP_GMYTHTV_LIVE:
1132 mythtvsrc->live_tv = g_value_get_boolean(value);
1134 case PROP_GMYTHTV_ENABLE_TIMING_POSITION:
1135 mythtvsrc->enable_timing_position = g_value_get_boolean(value);
1137 case PROP_GMYTHTV_LIVE_CHAINID:
1138 if (!g_value_get_string(value)) {
1139 GST_WARNING_OBJECT(object, "MythTV Live chainid property cannot be NULL");
1143 if (mythtvsrc->live_chain_id != NULL) {
1144 g_free(mythtvsrc->live_chain_id);
1145 mythtvsrc->live_chain_id = NULL;
1147 mythtvsrc->live_chain_id = g_value_dup_string(value);
1149 case PROP_GMYTHTV_CHANNEL_NUM:
1150 mythtvsrc->channel_name = g_value_dup_string(value);
1153 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
1157 GST_OBJECT_UNLOCK(mythtvsrc);
1161 gst_mythtv_src_get_property(GObject * object, guint prop_id,
1162 GValue * value, GParamSpec * pspec)
1164 GstMythtvSrc *mythtvsrc = GST_MYTHTV_SRC(object);
1166 GST_OBJECT_LOCK(mythtvsrc);
1169 g_value_set_string(value, mythtvsrc->uri_name);
1171 case PROP_GMYTHTV_VERSION:
1172 g_value_set_int(value, mythtvsrc->mythtv_version);
1174 case PROP_GMYTHTV_LIVEID:
1175 g_value_set_int(value, mythtvsrc->live_tv_id);
1177 case PROP_GMYTHTV_LIVE:
1178 g_value_set_boolean(value, mythtvsrc->live_tv);
1180 case PROP_GMYTHTV_ENABLE_TIMING_POSITION:
1181 g_value_set_boolean(value, mythtvsrc->enable_timing_position);
1183 case PROP_GMYTHTV_LIVE_CHAINID:
1184 g_value_set_string(value, mythtvsrc->live_chain_id);
1186 case PROP_GMYTHTV_CHANNEL_NUM:
1187 g_value_set_string(value, mythtvsrc->channel_name);
1190 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
1193 GST_OBJECT_UNLOCK(mythtvsrc);
1197 plugin_init(GstPlugin * plugin)
1199 return gst_element_register(plugin, "mythtvsrc", GST_RANK_NONE,
1200 GST_TYPE_MYTHTV_SRC);
1203 GST_PLUGIN_DEFINE(GST_VERSION_MAJOR,
1207 plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME,
1208 GST_PACKAGE_ORIGIN);
1211 /*** GSTURIHANDLER INTERFACE *************************************************/
1213 gst_mythtv_src_uri_get_type(void)
1219 gst_mythtv_src_uri_get_protocols(void)
1221 static gchar *protocols[] = { "myth", "myths", NULL };
1226 static const gchar *
1227 gst_mythtv_src_uri_get_uri(GstURIHandler * handler)
1229 GstMythtvSrc *src = GST_MYTHTV_SRC(handler);
1231 return src->uri_name;
1235 gst_mythtv_src_uri_set_uri(GstURIHandler * handler, const gchar * uri)
1237 GstMythtvSrc *src = GST_MYTHTV_SRC(handler);
1241 protocol = gst_uri_get_protocol(uri);
1242 if ((strcmp(protocol, "myth") != 0)
1243 && (strcmp(protocol, "myths") != 0)) {
1248 g_object_set(src, "location", uri, NULL);
1254 gst_mythtv_src_uri_handler_init(gpointer g_iface, gpointer iface_data)
1256 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
1258 iface->get_type = gst_mythtv_src_uri_get_type;
1259 iface->get_protocols = gst_mythtv_src_uri_get_protocols;
1260 iface->get_uri = gst_mythtv_src_uri_get_uri;
1261 iface->set_uri = gst_mythtv_src_uri_set_uri;
1265 size_header_handler(void *userdata, const char *value)
1267 GstMythtvSrc *src = GST_MYTHTV_SRC(userdata);
1269 GST_DEBUG_OBJECT(src, "content size = %lld bytes", src->content_size);