gmyth/samples/gmyth_cat.c
author melunko
Fri Aug 10 14:45:36 2007 +0100 (2007-08-10)
branchtrunk
changeset 798 19626dc62c16
parent 754 cb885ee44618
child 827 30368d31696e
permissions -rw-r--r--
[svn r804] Added function to retrieve backend details (total space, free space, etc)
     1 
     2 #ifdef HAVE_CONFIG_H
     3 #include "config.h"
     4 #endif
     5 
     6 #include <stdio.h>
     7 #include <glib.h>
     8 
     9 #include "gmyth_backendinfo.h"
    10 #include "gmyth_file_transfer.h"
    11 #include "gmyth_livetv.h"
    12 #include "gmyth_util.h"
    13 #include "gmyth_common.h"
    14 
    15 typedef struct {
    16     GMythBackendInfo *b_info;
    17     char           *filename;
    18     char           *channel;
    19 } cat_options_t;
    20 
    21 static cat_options_t *
    22 _cat_options_new()
    23 {
    24     cat_options_t  *options = g_new0(cat_options_t, 1);
    25     options->b_info = gmyth_backend_info_new();
    26 
    27     return options;
    28 }
    29 
    30 static void
    31 _cat_options_free(cat_options_t * options)
    32 {
    33     g_return_if_fail(options != NULL);
    34 
    35     if (options->b_info)
    36         g_object_unref(options->b_info);
    37     g_free(options->filename);
    38     g_free(options->channel);
    39 }
    40 
    41 static          gboolean
    42 _parse_args(int argc, char *argv[], cat_options_t * options)
    43 {
    44     GError         *error = NULL;
    45     GOptionContext *context;
    46 
    47     gchar          *host_ip = NULL;
    48     gint            host_port = 0;
    49     gchar          *filename = NULL;
    50     gchar          *channel = NULL;
    51 
    52     GOptionEntry    entries[] = {
    53         {"hostname", 'h', 0, G_OPTION_ARG_STRING, &host_ip,
    54          "Mythtv backend hostname or " "IP address", "IP_ADDRESS"},
    55 
    56         {"port", 'p', 0, G_OPTION_ARG_INT, &host_port,
    57          "Mythtv backend port",
    58          "PORT"},
    59 
    60         {"filename", 'f', 0, G_OPTION_ARG_STRING, &filename,
    61          "Recorded file name available " "in the Mythtv backend", "FILE"},
    62 
    63         {"channel", 'c', 0, G_OPTION_ARG_STRING, &channel,
    64          "Mythtv channel number", "CHANNEL"},
    65 
    66         {NULL}
    67     };
    68 
    69     g_return_val_if_fail(options != NULL, FALSE);
    70 
    71     context =
    72         g_option_context_new
    73         ("- loads a mythtv backend recorded file and prints "
    74          "it on the standard output\n");
    75     g_option_context_add_main_entries(context, entries, NULL);
    76     g_option_context_parse(context, &argc, &argv, &error);
    77     g_option_context_set_help_enabled(context, TRUE);
    78 
    79     g_option_context_free(context);
    80 
    81     if ((!host_ip) || (host_port == 0)) {
    82         g_free(host_ip);
    83         g_free(filename);
    84         g_free(channel);
    85         return FALSE;
    86     }
    87 
    88     gmyth_backend_info_set_hostname(options->b_info, host_ip);
    89     gmyth_backend_info_set_port(options->b_info, host_port);
    90     if (filename)
    91         options->filename = g_strdup(filename);
    92     if (channel)
    93         options->channel = g_strdup(channel);
    94 
    95     g_free(host_ip);
    96     g_free(filename);
    97     g_free(channel);
    98 
    99     return TRUE;
   100 }
   101 
   102 static          gboolean
   103 _cat_recorded_file(cat_options_t * options)
   104 {
   105     GArray         *array = NULL;
   106     GMythFileTransfer *transfer;
   107     guint64         size = 0,
   108         total = 0;
   109 
   110     g_return_val_if_fail(options != NULL, FALSE);
   111     g_return_val_if_fail(options->b_info != NULL, FALSE);
   112     g_return_val_if_fail(options->filename != NULL, FALSE);
   113 
   114     if (!gmyth_util_file_exists(options->b_info, options->filename)) {
   115         g_printerr("File %s was not found in the mythtv server\n",
   116                    options->filename);
   117         return FALSE;
   118     }
   119 
   120     transfer = gmyth_file_transfer_new(options->b_info);
   121     if (!gmyth_file_transfer_open(transfer, options->filename)) {
   122         g_printerr("File %s could not be opened\n", options->filename);
   123         return FALSE;
   124     }
   125 
   126     size = gmyth_file_transfer_get_filesize(transfer);
   127     fprintf(stderr, "Size:%" G_GUINT64_FORMAT "\n", size);
   128 
   129     array = g_array_new(FALSE, TRUE, sizeof(gchar));
   130 
   131     while (total != size) {
   132         GMythFileReadResult res;
   133 
   134         res = gmyth_file_transfer_read(transfer, (GByteArray *) array,
   135                                        (size - total) >
   136                                        64000 ? 64000 : (size - total),
   137                                        FALSE);
   138         if ((res != GMYTH_FILE_READ_OK) && (res != GMYTH_FILE_READ_EOF)) {
   139             g_array_free(array, TRUE);
   140             g_printerr("Error while reading the file: aborting!!\n");
   141             break;
   142         }
   143 
   144         fwrite(array->data, array->len, 1, stdout);
   145         fflush(stdout);
   146 
   147         total += array->len;
   148         fprintf(stderr, "%" G_GUINT64_FORMAT "\n", total);
   149         g_array_remove_range(array, 0, array->len);
   150         // usleep(300000);
   151     }
   152 
   153     gmyth_file_transfer_close(transfer);
   154     g_array_free(array, TRUE);
   155     g_object_unref(transfer);
   156 
   157     return TRUE;
   158 }
   159 
   160 static          gboolean
   161 _cat_channel(cat_options_t * options)
   162 {
   163     GMythLiveTV    *livetv = NULL;
   164     GMythFile      *gmyth_file = NULL;
   165     GArray         *array = NULL;
   166 
   167     gint            file_transf_ret;
   168 
   169     g_return_val_if_fail(options != NULL, FALSE);
   170     g_return_val_if_fail(options->b_info != NULL, FALSE);
   171     g_return_val_if_fail(options->channel != NULL, FALSE);
   172     livetv = gmyth_livetv_new(options->b_info);
   173 
   174     if (gmyth_livetv_channel_name_setup(livetv, options->channel) == FALSE) {
   175         g_printerr("Could not setup remote livetv");
   176         g_object_unref(livetv);
   177         return FALSE;
   178     }
   179 
   180     gmyth_file = GMYTH_FILE(gmyth_livetv_create_file_transfer(livetv));
   181     if (gmyth_file == NULL) {
   182         g_printerr("Could not open livetv recording file for transfer");
   183         g_object_unref(livetv);
   184         return FALSE;
   185     }
   186 
   187     if (!gmyth_file_transfer_open(GMYTH_FILE_TRANSFER(gmyth_file),
   188                                   livetv->uri != NULL ?
   189                                   gmyth_uri_get_path(livetv->uri) :
   190                                   livetv->proginfo->pathname->str)) {
   191 
   192         g_printerr("Couldn't open MythTV. FileTransfer is NULL!\n");
   193         return FALSE;
   194     }
   195 
   196     array = g_array_new(FALSE, TRUE, sizeof(gchar));
   197 
   198     while (((file_transf_ret = gmyth_file_transfer_read
   199              (GMYTH_FILE_TRANSFER(gmyth_file),
   200               (GByteArray *) array, 64000, TRUE)) == GMYTH_FILE_READ_OK) ||
   201            file_transf_ret == GMYTH_FILE_READ_NEXT_PROG_CHAIN) {
   202 
   203         fwrite(array->data, array->len, 1, stdout);
   204         fflush(stdout);
   205         g_array_remove_range(array, 0, array->len);
   206 
   207         g_main_context_iteration(g_main_context_default(), FALSE);
   208     }
   209 
   210     g_array_free(array, TRUE);
   211     g_object_unref(gmyth_file);
   212     g_object_unref(livetv);
   213 
   214     return TRUE;
   215 }
   216 
   217 int
   218 main(int argc, char *argv[])
   219 {
   220     gboolean        res = FALSE;
   221     cat_options_t  *options;
   222 
   223     g_type_init();
   224     if (!g_thread_supported())
   225         g_thread_init(NULL);
   226 
   227     options = _cat_options_new();
   228     res = _parse_args(argc, argv, options);
   229     if (!res) {
   230         g_printerr("Argument invalid. Type --help\n");
   231         return 1;
   232     }
   233 
   234     if (options->filename)
   235         res = _cat_recorded_file(options);
   236     else if (options->channel)
   237         res = _cat_channel(options);
   238     else
   239         g_printerr
   240             ("Argument invalid. You must specify --filename or --channel.\n"
   241              "Type --help for more information.\n");
   242 
   243     _cat_options_free(options);
   244 
   245     return 0;
   246 }