[svn r605] added livetv support to gmyth-cat trunk
authormelunko
Thu Apr 26 19:50:02 2007 +0100 (2007-04-26)
branchtrunk
changeset 5995555b47e102b
parent 598 b66392e80922
child 600 7da2a5e32fa6
[svn r605] added livetv support to gmyth-cat
gmyth/samples/Makefile.am
gmyth/samples/gmyth_cat.c
     1.1 --- a/gmyth/samples/Makefile.am	Thu Apr 26 16:30:23 2007 +0100
     1.2 +++ b/gmyth/samples/Makefile.am	Thu Apr 26 19:50:02 2007 +0100
     1.3 @@ -6,6 +6,8 @@
     1.4  LDADD = \
     1.5          $(top_builddir)/src/.libs/libgmyth.la
     1.6  
     1.7 +AM_CFLAGS = -g
     1.8 +
     1.9  AM_LDFLAGS = \
    1.10          @GLIB_LIBS@ @GOBJECT_LIBS@ @GTHREAD_LIBS@ @LIBCURL_LIBS@
    1.11  
     2.1 --- a/gmyth/samples/gmyth_cat.c	Thu Apr 26 16:30:23 2007 +0100
     2.2 +++ b/gmyth/samples/gmyth_cat.c	Thu Apr 26 19:50:02 2007 +0100
     2.3 @@ -8,26 +8,59 @@
     2.4  
     2.5  #include "gmyth_backendinfo.h"
     2.6  #include "gmyth_file_transfer.h"
     2.7 +#include "gmyth_livetv.h"
     2.8  #include "gmyth_util.h"
     2.9  #include "gmyth_common.h"
    2.10  
    2.11 +typedef struct {
    2.12 +    GMythBackendInfo *b_info;
    2.13 +
    2.14 +    char* filename;
    2.15 +    char* channel;
    2.16 +} cat_options_t;
    2.17 +
    2.18 +static cat_options_t*
    2.19 +_cat_options_new ()
    2.20 +{
    2.21 +    cat_options_t *options = g_new0 (cat_options_t, 1);
    2.22 +    options->b_info = gmyth_backend_info_new ();
    2.23 +
    2.24 +    return options;
    2.25 +}
    2.26 +
    2.27 +static void
    2.28 +_cat_options_free (cat_options_t *options)
    2.29 +{
    2.30 +    g_return_if_fail (options != NULL);
    2.31 +
    2.32 +    if (options->b_info)
    2.33 +        g_object_unref (options->b_info);
    2.34 +    g_free (options->filename);
    2.35 +    g_free (options->channel);
    2.36 +}
    2.37 +
    2.38  static gboolean
    2.39 -_parse_args (int argc, char *argv[], GMythBackendInfo *b_info, char** filename)
    2.40 +_parse_args (int argc, char *argv[], cat_options_t *options)
    2.41  {
    2.42      GError *error = NULL;
    2.43      GOptionContext *context;
    2.44  
    2.45      gchar *host_ip = NULL;
    2.46 -    gint host_port = 0;
    2.47 +    gint   host_port = 0;
    2.48 +    gchar *filename = NULL;
    2.49 +    gchar *channel = NULL;
    2.50  
    2.51      GOptionEntry entries[] = 
    2.52      {
    2.53          { "hostname", 'h', 0, G_OPTION_ARG_STRING, &host_ip, "Mythtv backend hostname or IP address", "IP_ADDRESS" },
    2.54          { "port", 'p', 0, G_OPTION_ARG_INT, &host_port, "Mythtv backend port", "PORT" },
    2.55 -        { "filename", 'f', 0, G_OPTION_ARG_STRING, filename, "Recorded file name available in the Mythtv backend", "FILE" },
    2.56 +        { "filename", 'f', 0, G_OPTION_ARG_STRING, &filename, "Recorded file name available in the Mythtv backend", "FILE" },
    2.57 +        { "channel", 'c', 0, G_OPTION_ARG_STRING, &channel, "Mythtv channel number", "CHANNEL" },
    2.58          { NULL }
    2.59      };
    2.60  
    2.61 +    g_return_val_if_fail (options != NULL, FALSE);
    2.62 +
    2.63      context = g_option_context_new ("- loads a mythtv backend recorded file and prints it on the standard output\n");
    2.64      g_option_context_add_main_entries (context, entries, NULL);
    2.65      g_option_context_parse (context, &argc, &argv, &error);
    2.66 @@ -35,44 +68,47 @@
    2.67  
    2.68      g_option_context_free (context);
    2.69      
    2.70 -    gmyth_backend_info_set_hostname (b_info, host_ip);
    2.71 -    gmyth_backend_info_set_port (b_info, host_port);
    2.72 +    if ((!host_ip) || (host_port == 0) ) {
    2.73 +        g_free (host_ip);
    2.74 +        g_free (filename);
    2.75 +        g_free (channel);
    2.76 +        return FALSE;
    2.77 +    }
    2.78 +
    2.79 +    gmyth_backend_info_set_hostname (options->b_info, host_ip);
    2.80 +    gmyth_backend_info_set_port (options->b_info, host_port);
    2.81 +    if (filename)
    2.82 +        options->filename = g_strdup (filename);
    2.83 +    if (channel)
    2.84 +        options->channel = g_strdup (channel);
    2.85  
    2.86      g_free (host_ip);
    2.87 +    g_free (filename);
    2.88 +    g_free (channel);
    2.89  
    2.90      return TRUE;
    2.91  }
    2.92  
    2.93 -int 
    2.94 -main (int argc, char *argv[])
    2.95 +static gboolean
    2.96 +_cat_recorded_file (cat_options_t *options)
    2.97  {
    2.98 -    gboolean res = FALSE;
    2.99 -    gchar *filename = NULL;
   2.100 -    GMythBackendInfo *b_info;
   2.101      GByteArray *array = NULL;
   2.102      GMythFileTransfer *transfer;
   2.103      guint64 size = 0, total = 0;
   2.104 + 
   2.105 +    g_return_val_if_fail (options != NULL, FALSE);
   2.106 +    g_return_val_if_fail (options->b_info != NULL, FALSE);
   2.107 +    g_return_val_if_fail (options->filename != NULL, FALSE);
   2.108  
   2.109 -    g_type_init ();
   2.110 -    g_thread_init (NULL);
   2.111 -    
   2.112 -    b_info = gmyth_backend_info_new ();
   2.113 -
   2.114 -    res = _parse_args (argc, argv, b_info, &filename);
   2.115 -    if (!res) {
   2.116 -        g_printerr ("Argument invalid. Type --help\n");
   2.117 -        return 1;
   2.118 +    if (!gmyth_util_file_exists (options->b_info, options->filename)) {
   2.119 +        g_printerr ("File %s was not found in the mythtv server\n", options->filename);
   2.120 +        return FALSE;
   2.121      }
   2.122  
   2.123 -    if (!gmyth_util_file_exists (b_info, filename)) {
   2.124 -        g_printerr ("File %s was not found in the mythtv server\n", filename);
   2.125 -        return 1;
   2.126 -    }
   2.127 -
   2.128 -    transfer = gmyth_file_transfer_new (b_info);
   2.129 -    if (!gmyth_file_transfer_open (transfer, filename)) {
   2.130 -        g_printerr ("File %s could not be opened\n", filename);
   2.131 -        return 1;
   2.132 +    transfer = gmyth_file_transfer_new (options->b_info);
   2.133 +    if (!gmyth_file_transfer_open (transfer, options->filename)) {
   2.134 +        g_printerr ("File %s could not be opened\n", options->filename);
   2.135 +        return FALSE;
   2.136      }
   2.137  
   2.138      size = gmyth_file_transfer_get_filesize (transfer);
   2.139 @@ -96,12 +132,109 @@
   2.140          g_byte_array_free (array, TRUE);
   2.141      }
   2.142      
   2.143 -    g_free (filename);
   2.144 -
   2.145      gmyth_file_transfer_close (transfer);
   2.146      g_object_unref (transfer);
   2.147  
   2.148 -    g_object_unref (b_info);
   2.149 +    return TRUE;
   2.150 +}
   2.151 +
   2.152 +static gboolean
   2.153 +_cat_channel (cat_options_t *options)
   2.154 +{
   2.155 +    GMythLiveTV * livetv = NULL;
   2.156 +    GMythFile *gmyth_file = NULL;
   2.157 +    GByteArray *array = NULL;
   2.158 +    gint tries = 0;
   2.159 +    GMythFileReadResult res;
   2.160 +
   2.161 +    g_return_val_if_fail (options != NULL, FALSE);
   2.162 +    g_return_val_if_fail (options->b_info != NULL, FALSE);
   2.163 +    g_return_val_if_fail (options->channel != NULL, FALSE);
   2.164 +    livetv = gmyth_livetv_new (options->b_info);
   2.165 +    if (gmyth_livetv_channel_name_setup (livetv, options->channel) == FALSE) {
   2.166 +        g_printerr ("coult not setup remote livetv");
   2.167 +        g_object_unref (livetv);
   2.168 +        return FALSE;
   2.169 +    }
   2.170 +
   2.171 +    gmyth_file = GMYTH_FILE( gmyth_livetv_create_file_transfer (livetv) );
   2.172 +    if (gmyth_file == NULL) {
   2.173 +        g_printerr ("Could not open livetv recording file for transfer");
   2.174 +        g_object_unref (livetv);
   2.175 +        return FALSE;
   2.176 +    }
   2.177 +
   2.178 +    if (!gmyth_file_transfer_open ( GMYTH_FILE_TRANSFER(gmyth_file),
   2.179 +                                    livetv->uri != NULL ? 
   2.180 +                                        gmyth_uri_get_path (livetv->uri) :
   2.181 +                                        livetv->proginfo->pathname->str)) {
   2.182 +
   2.183 +        g_printerr ("Couldn't open MythTV FileTransfer is NULL!\n");
   2.184 +        return FALSE;
   2.185 +    }
   2.186 +
   2.187 +    while (TRUE) {
   2.188 +        array = g_byte_array_new ();
   2.189 +
   2.190 +        res = gmyth_file_transfer_read ( GMYTH_FILE_TRANSFER(gmyth_file),
   2.191 +                                       array, 64000, TRUE);
   2.192 +        if (res == GMYTH_FILE_READ_OK) {
   2.193 +            tries = 0;
   2.194 +            fwrite (array->data, array->len, 1, stdout);
   2.195 +            fflush (stdout);
   2.196 +
   2.197 +            if (array->len == 64000) {
   2.198 +               tries = 0;
   2.199 +            } else {
   2.200 +                tries += 1;
   2.201 +                // after 20 tries without any data, we give up
   2.202 +                g_printerr ("-----> 20 read tries exceeded\n");
   2.203 +                if (tries > 20) {
   2.204 +                    g_byte_array_free (array, TRUE);
   2.205 +                    goto error;
   2.206 +                }
   2.207 +                g_usleep (10000);
   2.208 +            }    
   2.209 +        } else {
   2.210 +            g_printerr ("Error while file transfer read\n");
   2.211 +            goto error;
   2.212 +        }
   2.213 +        g_byte_array_free (array, TRUE);
   2.214 +    }
   2.215 +
   2.216 +error:
   2.217 +    g_object_unref (gmyth_file);
   2.218 +    g_object_unref (livetv);
   2.219 +
   2.220 +    return TRUE;
   2.221 +}
   2.222 +
   2.223 +int 
   2.224 +main (int argc, char *argv[])
   2.225 +{
   2.226 +    gboolean res = FALSE;
   2.227 +    cat_options_t *options;
   2.228 +
   2.229 +    g_type_init ();
   2.230 +    g_thread_init (NULL);
   2.231 +    
   2.232 +    options = _cat_options_new ();
   2.233 +    res = _parse_args (argc, argv, options);
   2.234 +    if (!res) {
   2.235 +        g_printerr ("Argument invalid. Type --help\n");
   2.236 +        return 1;
   2.237 +    }
   2.238 +
   2.239 +    if (options->filename) {
   2.240 +        res = _cat_recorded_file (options);
   2.241 +    } else if (options->channel) {
   2.242 +        res = _cat_channel (options);
   2.243 +    } else {
   2.244 +        g_printerr ("Argument invalid. You must specify --filename or --channel.\nType --help for more information.\n");
   2.245 +        res = FALSE;
   2.246 +    }
   2.247 +
   2.248 +    _cat_options_free (options);
   2.249  
   2.250      return 0;
   2.251  }