# HG changeset patch # User melunko # Date 1177613402 -3600 # Node ID 5555b47e102b7156ea100ce37d9dfdfbf9a35b0f # Parent b66392e80922dc559921b59adbf3ad9dc13f1597 [svn r605] added livetv support to gmyth-cat diff -r b66392e80922 -r 5555b47e102b gmyth/samples/Makefile.am --- a/gmyth/samples/Makefile.am Thu Apr 26 16:30:23 2007 +0100 +++ b/gmyth/samples/Makefile.am Thu Apr 26 19:50:02 2007 +0100 @@ -6,6 +6,8 @@ LDADD = \ $(top_builddir)/src/.libs/libgmyth.la +AM_CFLAGS = -g + AM_LDFLAGS = \ @GLIB_LIBS@ @GOBJECT_LIBS@ @GTHREAD_LIBS@ @LIBCURL_LIBS@ diff -r b66392e80922 -r 5555b47e102b gmyth/samples/gmyth_cat.c --- a/gmyth/samples/gmyth_cat.c Thu Apr 26 16:30:23 2007 +0100 +++ b/gmyth/samples/gmyth_cat.c Thu Apr 26 19:50:02 2007 +0100 @@ -8,26 +8,59 @@ #include "gmyth_backendinfo.h" #include "gmyth_file_transfer.h" +#include "gmyth_livetv.h" #include "gmyth_util.h" #include "gmyth_common.h" +typedef struct { + GMythBackendInfo *b_info; + + char* filename; + char* channel; +} cat_options_t; + +static cat_options_t* +_cat_options_new () +{ + cat_options_t *options = g_new0 (cat_options_t, 1); + options->b_info = gmyth_backend_info_new (); + + return options; +} + +static void +_cat_options_free (cat_options_t *options) +{ + g_return_if_fail (options != NULL); + + if (options->b_info) + g_object_unref (options->b_info); + g_free (options->filename); + g_free (options->channel); +} + static gboolean -_parse_args (int argc, char *argv[], GMythBackendInfo *b_info, char** filename) +_parse_args (int argc, char *argv[], cat_options_t *options) { GError *error = NULL; GOptionContext *context; gchar *host_ip = NULL; - gint host_port = 0; + gint host_port = 0; + gchar *filename = NULL; + gchar *channel = NULL; GOptionEntry entries[] = { { "hostname", 'h', 0, G_OPTION_ARG_STRING, &host_ip, "Mythtv backend hostname or IP address", "IP_ADDRESS" }, { "port", 'p', 0, G_OPTION_ARG_INT, &host_port, "Mythtv backend port", "PORT" }, - { "filename", 'f', 0, G_OPTION_ARG_STRING, filename, "Recorded file name available in the Mythtv backend", "FILE" }, + { "filename", 'f', 0, G_OPTION_ARG_STRING, &filename, "Recorded file name available in the Mythtv backend", "FILE" }, + { "channel", 'c', 0, G_OPTION_ARG_STRING, &channel, "Mythtv channel number", "CHANNEL" }, { NULL } }; + g_return_val_if_fail (options != NULL, FALSE); + context = g_option_context_new ("- loads a mythtv backend recorded file and prints it on the standard output\n"); g_option_context_add_main_entries (context, entries, NULL); g_option_context_parse (context, &argc, &argv, &error); @@ -35,44 +68,47 @@ g_option_context_free (context); - gmyth_backend_info_set_hostname (b_info, host_ip); - gmyth_backend_info_set_port (b_info, host_port); + if ((!host_ip) || (host_port == 0) ) { + g_free (host_ip); + g_free (filename); + g_free (channel); + return FALSE; + } + + gmyth_backend_info_set_hostname (options->b_info, host_ip); + gmyth_backend_info_set_port (options->b_info, host_port); + if (filename) + options->filename = g_strdup (filename); + if (channel) + options->channel = g_strdup (channel); g_free (host_ip); + g_free (filename); + g_free (channel); return TRUE; } -int -main (int argc, char *argv[]) +static gboolean +_cat_recorded_file (cat_options_t *options) { - gboolean res = FALSE; - gchar *filename = NULL; - GMythBackendInfo *b_info; GByteArray *array = NULL; GMythFileTransfer *transfer; guint64 size = 0, total = 0; + + g_return_val_if_fail (options != NULL, FALSE); + g_return_val_if_fail (options->b_info != NULL, FALSE); + g_return_val_if_fail (options->filename != NULL, FALSE); - g_type_init (); - g_thread_init (NULL); - - b_info = gmyth_backend_info_new (); - - res = _parse_args (argc, argv, b_info, &filename); - if (!res) { - g_printerr ("Argument invalid. Type --help\n"); - return 1; + if (!gmyth_util_file_exists (options->b_info, options->filename)) { + g_printerr ("File %s was not found in the mythtv server\n", options->filename); + return FALSE; } - if (!gmyth_util_file_exists (b_info, filename)) { - g_printerr ("File %s was not found in the mythtv server\n", filename); - return 1; - } - - transfer = gmyth_file_transfer_new (b_info); - if (!gmyth_file_transfer_open (transfer, filename)) { - g_printerr ("File %s could not be opened\n", filename); - return 1; + transfer = gmyth_file_transfer_new (options->b_info); + if (!gmyth_file_transfer_open (transfer, options->filename)) { + g_printerr ("File %s could not be opened\n", options->filename); + return FALSE; } size = gmyth_file_transfer_get_filesize (transfer); @@ -96,12 +132,109 @@ g_byte_array_free (array, TRUE); } - g_free (filename); - gmyth_file_transfer_close (transfer); g_object_unref (transfer); - g_object_unref (b_info); + return TRUE; +} + +static gboolean +_cat_channel (cat_options_t *options) +{ + GMythLiveTV * livetv = NULL; + GMythFile *gmyth_file = NULL; + GByteArray *array = NULL; + gint tries = 0; + GMythFileReadResult res; + + g_return_val_if_fail (options != NULL, FALSE); + g_return_val_if_fail (options->b_info != NULL, FALSE); + g_return_val_if_fail (options->channel != NULL, FALSE); + livetv = gmyth_livetv_new (options->b_info); + if (gmyth_livetv_channel_name_setup (livetv, options->channel) == FALSE) { + g_printerr ("coult not setup remote livetv"); + g_object_unref (livetv); + return FALSE; + } + + gmyth_file = GMYTH_FILE( gmyth_livetv_create_file_transfer (livetv) ); + if (gmyth_file == NULL) { + g_printerr ("Could not open livetv recording file for transfer"); + g_object_unref (livetv); + return FALSE; + } + + if (!gmyth_file_transfer_open ( GMYTH_FILE_TRANSFER(gmyth_file), + livetv->uri != NULL ? + gmyth_uri_get_path (livetv->uri) : + livetv->proginfo->pathname->str)) { + + g_printerr ("Couldn't open MythTV FileTransfer is NULL!\n"); + return FALSE; + } + + while (TRUE) { + array = g_byte_array_new (); + + res = gmyth_file_transfer_read ( GMYTH_FILE_TRANSFER(gmyth_file), + array, 64000, TRUE); + if (res == GMYTH_FILE_READ_OK) { + tries = 0; + fwrite (array->data, array->len, 1, stdout); + fflush (stdout); + + if (array->len == 64000) { + tries = 0; + } else { + tries += 1; + // after 20 tries without any data, we give up + g_printerr ("-----> 20 read tries exceeded\n"); + if (tries > 20) { + g_byte_array_free (array, TRUE); + goto error; + } + g_usleep (10000); + } + } else { + g_printerr ("Error while file transfer read\n"); + goto error; + } + g_byte_array_free (array, TRUE); + } + +error: + g_object_unref (gmyth_file); + g_object_unref (livetv); + + return TRUE; +} + +int +main (int argc, char *argv[]) +{ + gboolean res = FALSE; + cat_options_t *options; + + g_type_init (); + g_thread_init (NULL); + + options = _cat_options_new (); + res = _parse_args (argc, argv, options); + if (!res) { + g_printerr ("Argument invalid. Type --help\n"); + return 1; + } + + if (options->filename) { + res = _cat_recorded_file (options); + } else if (options->channel) { + res = _cat_channel (options); + } else { + g_printerr ("Argument invalid. You must specify --filename or --channel.\nType --help for more information.\n"); + res = FALSE; + } + + _cat_options_free (options); return 0; }