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"
17 GMythBackendInfo *b_info;
22 static cat_options_t *
25 cat_options_t *options = g_new0(cat_options_t, 1);
26 options->b_info = gmyth_backend_info_new();
32 _cat_options_free(cat_options_t * options)
34 g_return_if_fail(options != NULL);
37 g_object_unref(options->b_info);
38 g_free(options->filename);
39 g_free(options->channel);
43 _parse_args(int argc, char *argv[], cat_options_t * options)
46 GOptionContext *context;
48 gchar *host_ip = NULL;
50 gchar *filename = NULL;
51 gchar *channel = NULL;
53 GOptionEntry entries[] = {
54 {"hostname", 'h', 0, G_OPTION_ARG_STRING, &host_ip,
55 "Mythtv backend hostname or " "IP address", "IP_ADDRESS"},
57 {"port", 'p', 0, G_OPTION_ARG_INT, &host_port, "Mythtv backend port",
60 {"filename", 'f', 0, G_OPTION_ARG_STRING, &filename,
61 "Recorded file name available " "in the Mythtv backend", "FILE"},
63 {"channel", 'c', 0, G_OPTION_ARG_STRING, &channel,
64 "Mythtv channel number", "CHANNEL"},
69 g_return_val_if_fail(options != NULL, FALSE);
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);
78 g_option_context_free(context);
80 if ((!host_ip) || (host_port == 0))
88 gmyth_backend_info_set_hostname(options->b_info, host_ip);
89 gmyth_backend_info_set_port(options->b_info, host_port);
91 options->filename = g_strdup(filename);
93 options->channel = g_strdup(channel);
103 _cat_recorded_file(cat_options_t * options)
105 GArray *array = NULL;
106 GMythFileTransfer *transfer;
107 guint64 size = 0, total = 0;
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);
113 if (!gmyth_util_file_exists(options->b_info, options->filename))
115 g_printerr("File %s was not found in the mythtv server\n",
120 transfer = gmyth_file_transfer_new(options->b_info);
121 if (!gmyth_file_transfer_open(transfer, options->filename))
123 g_printerr("File %s could not be opened\n", options->filename);
127 size = gmyth_file_transfer_get_filesize(transfer);
128 fprintf(stderr, "Size:%d\n", size);
130 array = g_array_new(FALSE, TRUE, sizeof(gchar));
132 while (total != size)
134 GMythFileReadResult res;
136 res = gmyth_file_transfer_read(transfer, (GByteArray *) array,
138 64000 ? 64000 : (size - total), FALSE);
139 if ((res != GMYTH_FILE_READ_OK) && (res != GMYTH_FILE_READ_EOF))
141 g_array_free(array, TRUE);
142 g_printerr("Error while reading the file: aborting!!\n");
146 fwrite(array->data, array->len, 1, stdout);
150 fprintf(stderr, "%d\n", total);
151 g_array_remove_range(array, 0, array->len);
155 gmyth_file_transfer_close(transfer);
156 g_array_free(array, TRUE);
157 g_object_unref(transfer);
163 _cat_channel(cat_options_t * options)
165 GMythLiveTV *livetv = NULL;
166 GMythFile *gmyth_file = NULL;
167 GArray *array = NULL;
169 gint file_transf_ret;
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);
176 if (gmyth_livetv_channel_name_setup(livetv, options->channel) == FALSE)
178 g_printerr("Could not setup remote livetv");
179 g_object_unref(livetv);
183 gmyth_file = GMYTH_FILE(gmyth_livetv_create_file_transfer(livetv));
184 if (gmyth_file == NULL)
186 g_printerr("Could not open livetv recording file for transfer");
187 g_object_unref(livetv);
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))
197 g_printerr("Couldn't open MythTV. FileTransfer is NULL!\n");
201 array = g_array_new(FALSE, TRUE, sizeof(gchar));
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)
209 fwrite(array->data, array->len, 1, stdout);
211 g_array_remove_range(array, 0, array->len);
213 g_main_context_iteration(g_main_context_default(), FALSE);
216 g_array_free(array, TRUE);
217 g_object_unref(gmyth_file);
218 g_object_unref(livetv);
224 main(int argc, char *argv[])
226 gboolean res = FALSE;
227 cat_options_t *options;
230 if (!g_thread_supported())
233 options = _cat_options_new();
234 res = _parse_args(argc, argv, options);
237 g_printerr("Argument invalid. Type --help\n");
241 if (options->filename)
242 res = _cat_recorded_file(options);
243 else if (options->channel)
244 res = _cat_channel(options);
246 g_printerr("Argument invalid. You must specify --filename or --channel.\n"
247 "Type --help for more information.\n");
249 _cat_options_free(options);