gmyth/samples/gmyth_cat.c
author renatofilho
Thu Jun 14 18:21:08 2007 +0100 (2007-06-14)
branchtrunk
changeset 751 3cf3c6019e3b
parent 686 b29ea6deb6f8
child 754 cb885ee44618
permissions -rw-r--r--
[svn r757] fixed indent using GNU Style
     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 {
    17   GMythBackendInfo *b_info;
    18   char *filename;
    19   char *channel;
    20 } cat_options_t;
    21 
    22 static cat_options_t *
    23 _cat_options_new()
    24 {
    25   cat_options_t *options = g_new0(cat_options_t, 1);
    26   options->b_info = gmyth_backend_info_new();
    27 
    28   return options;
    29 }
    30 
    31 static void
    32 _cat_options_free(cat_options_t * options)
    33 {
    34   g_return_if_fail(options != NULL);
    35 
    36   if (options->b_info)
    37 	g_object_unref(options->b_info);
    38   g_free(options->filename);
    39   g_free(options->channel);
    40 }
    41 
    42 static gboolean
    43 _parse_args(int argc, char *argv[], cat_options_t * options)
    44 {
    45   GError *error = NULL;
    46   GOptionContext *context;
    47 
    48   gchar *host_ip = NULL;
    49   gint host_port = 0;
    50   gchar *filename = NULL;
    51   gchar *channel = NULL;
    52 
    53   GOptionEntry entries[] = {
    54 	{"hostname", 'h', 0, G_OPTION_ARG_STRING, &host_ip,
    55 	 "Mythtv backend hostname or " "IP address", "IP_ADDRESS"},
    56 
    57 	{"port", 'p', 0, G_OPTION_ARG_INT, &host_port, "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("- loads a mythtv backend recorded file and prints "
    73 						 "it on the standard output\n");
    74   g_option_context_add_main_entries(context, entries, NULL);
    75   g_option_context_parse(context, &argc, &argv, &error);
    76   g_option_context_set_help_enabled(context, TRUE);
    77 
    78   g_option_context_free(context);
    79 
    80   if ((!host_ip) || (host_port == 0))
    81 	{
    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, total = 0;
   108 
   109   g_return_val_if_fail(options != NULL, FALSE);
   110   g_return_val_if_fail(options->b_info != NULL, FALSE);
   111   g_return_val_if_fail(options->filename != NULL, FALSE);
   112 
   113   if (!gmyth_util_file_exists(options->b_info, options->filename))
   114 	{
   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 	{
   123 	  g_printerr("File %s could not be opened\n", options->filename);
   124 	  return FALSE;
   125 	}
   126 
   127   size = gmyth_file_transfer_get_filesize(transfer);
   128   fprintf(stderr, "Size:%d\n", size);
   129 
   130   array = g_array_new(FALSE, TRUE, sizeof(gchar));
   131 
   132   while (total != size)
   133 	{
   134 	  GMythFileReadResult res;
   135 
   136 	  res = gmyth_file_transfer_read(transfer, (GByteArray *) array,
   137 									 (size - total) >
   138 									 64000 ? 64000 : (size - total), FALSE);
   139 	  if ((res != GMYTH_FILE_READ_OK) && (res != GMYTH_FILE_READ_EOF))
   140 		{
   141 		  g_array_free(array, TRUE);
   142 		  g_printerr("Error while reading the file: aborting!!\n");
   143 		  break;
   144 		}
   145 
   146 	  fwrite(array->data, array->len, 1, stdout);
   147 	  fflush(stdout);
   148 
   149 	  total += array->len;
   150 	  fprintf(stderr, "%d\n", total);
   151 	  g_array_remove_range(array, 0, array->len);
   152 	  //usleep(300000);
   153 	}
   154 
   155   gmyth_file_transfer_close(transfer);
   156   g_array_free(array, TRUE);
   157   g_object_unref(transfer);
   158 
   159   return TRUE;
   160 }
   161 
   162 static gboolean
   163 _cat_channel(cat_options_t * options)
   164 {
   165   GMythLiveTV *livetv = NULL;
   166   GMythFile *gmyth_file = NULL;
   167   GArray *array = NULL;
   168 
   169   gint file_transf_ret;
   170 
   171   g_return_val_if_fail(options != NULL, FALSE);
   172   g_return_val_if_fail(options->b_info != NULL, FALSE);
   173   g_return_val_if_fail(options->channel != NULL, FALSE);
   174   livetv = gmyth_livetv_new(options->b_info);
   175 
   176   if (gmyth_livetv_channel_name_setup(livetv, options->channel) == FALSE)
   177 	{
   178 	  g_printerr("Could not setup remote livetv");
   179 	  g_object_unref(livetv);
   180 	  return FALSE;
   181 	}
   182 
   183   gmyth_file = GMYTH_FILE(gmyth_livetv_create_file_transfer(livetv));
   184   if (gmyth_file == NULL)
   185 	{
   186 	  g_printerr("Could not open livetv recording file for transfer");
   187 	  g_object_unref(livetv);
   188 	  return FALSE;
   189 	}
   190 
   191   if (!gmyth_file_transfer_open(GMYTH_FILE_TRANSFER(gmyth_file),
   192 								livetv->uri != NULL ?
   193 								gmyth_uri_get_path(livetv->uri) :
   194 								livetv->proginfo->pathname->str))
   195 	{
   196 
   197 	  g_printerr("Couldn't open MythTV. FileTransfer is NULL!\n");
   198 	  return FALSE;
   199 	}
   200 
   201   array = g_array_new(FALSE, TRUE, sizeof(gchar));
   202 
   203   while (((file_transf_ret = gmyth_file_transfer_read
   204 		   (GMYTH_FILE_TRANSFER(gmyth_file),
   205 			(GByteArray *) array, 64000, TRUE)) == GMYTH_FILE_READ_OK) ||
   206 		 file_transf_ret == GMYTH_FILE_READ_NEXT_PROG_CHAIN)
   207 	{
   208 
   209 	  fwrite(array->data, array->len, 1, stdout);
   210 	  fflush(stdout);
   211 	  g_array_remove_range(array, 0, array->len);
   212 
   213 	  g_main_context_iteration(g_main_context_default(), FALSE);
   214 	}
   215 
   216   g_array_free(array, TRUE);
   217   g_object_unref(gmyth_file);
   218   g_object_unref(livetv);
   219 
   220   return TRUE;
   221 }
   222 
   223 int
   224 main(int argc, char *argv[])
   225 {
   226   gboolean res = FALSE;
   227   cat_options_t *options;
   228 
   229   g_type_init();
   230   if (!g_thread_supported())
   231 	g_thread_init(NULL);
   232 
   233   options = _cat_options_new();
   234   res = _parse_args(argc, argv, options);
   235   if (!res)
   236 	{
   237 	  g_printerr("Argument invalid. Type --help\n");
   238 	  return 1;
   239 	}
   240 
   241   if (options->filename)
   242 	res = _cat_recorded_file(options);
   243   else if (options->channel)
   244 	res = _cat_channel(options);
   245   else
   246 	g_printerr("Argument invalid. You must specify --filename or --channel.\n"
   247 			   "Type --help for more information.\n");
   248 
   249   _cat_options_free(options);
   250 
   251   return 0;
   252 }