[svn r334] Put all the gmyth recorder and tvchain inseide get_file_info; removed reference to LiveTV.
2 * @author Hallyson Melo <hallyson.melo@indt.org.br>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #include <libgnomevfs/gnome-vfs-module.h>
28 #include <libgnomevfs/gnome-vfs-utils.h>
30 #include <gmyth/gmyth_file_transfer.h>
31 #include <gmyth/gmyth_livetv.h>
32 #include <gmyth/gmyth_uri.h>
33 #include <gmyth/gmyth_recorder.h>
34 #include <gmyth/gmyth_backendinfo.h>
35 #include <gmyth/gmyth_util.h>
36 #include <gmyth/gmyth_remote_util.h>
37 #include <gmyth/gmyth_tvchain.h>
39 #define GST_MYTHTV_ID_NUM 1
40 #define MYTHTV_VERSION_DEFAULT 30
41 #define MYTHTV_TRANSFER_MAX_WAITS 100
43 #define MYTHTV_BUFFER_SIZE 1024*64
45 static GnomeVFSResult do_read (GnomeVFSMethod *method,
46 GnomeVFSMethodHandle *method_handle,
48 GnomeVFSFileSize num_bytes,
49 GnomeVFSFileSize *bytes_read,
50 GnomeVFSContext *context);
53 GMythFileTransfer *file_transfer;
66 do_open (GnomeVFSMethod *method,
67 GnomeVFSMethodHandle **method_handle,
69 GnomeVFSOpenMode mode,
70 GnomeVFSContext *context)
72 MythtvHandle *myth_handle;
73 GMythBackendInfo *backend_info;
74 GMythURI *gmyth_uri = NULL;
76 gboolean is_livetv = FALSE;
78 _GNOME_VFS_METHOD_PARAM_CHECK (method_handle != NULL);
79 _GNOME_VFS_METHOD_PARAM_CHECK (uri != NULL);
81 myth_handle = g_new0 (MythtvHandle, 1);
83 if (mode & GNOME_VFS_OPEN_WRITE) {
84 return GNOME_VFS_ERROR_NOT_PERMITTED;
87 if (gnome_vfs_uri_get_host_name (uri) == NULL) {
88 return GNOME_VFS_ERROR_INVALID_HOST_NAME;
91 /* Initialize mythtv handler*/
92 myth_handle->file_transfer = NULL;
93 myth_handle->livetv = NULL;
94 myth_handle->mythtv_version = MYTHTV_VERSION_DEFAULT;
95 myth_handle->bytes_read = 0;
96 myth_handle->content_size = (GnomeVFSFileSize) - 1;
98 /* Creates and fills out the backend info structure */
99 backend_info = gmyth_backend_info_new_with_uri (
100 gnome_vfs_unescape_string( gnome_vfs_uri_to_string( uri, GNOME_VFS_URI_HIDE_NONE ), "" ) );
102 /* creates an instance of */
103 gmyth_uri = gmyth_uri_new_with_value(
104 gnome_vfs_unescape_string( gnome_vfs_uri_to_string( uri, GNOME_VFS_URI_HIDE_NONE ), "" ) );
106 is_livetv = gmyth_uri_is_livetv( gmyth_uri );
108 /* Connect to the backend */
109 if ( gmyth_uri != NULL && is_livetv == TRUE ) {
110 myth_handle->livetv = gmyth_livetv_new ();
112 myth_handle->channel_name = gmyth_uri_get_channel_name( gmyth_uri );
114 g_print( "[%s] Channel name = %s", __FUNCTION__, myth_handle->channel_name );
116 if ( myth_handle->channel_name != NULL ) {
117 if (gmyth_livetv_channel_name_setup (myth_handle->livetv, myth_handle->channel_name,
118 backend_info) == FALSE) {
119 g_object_unref( gmyth_uri );
123 if ( gmyth_livetv_setup (myth_handle->livetv, backend_info) == FALSE ) {
124 g_object_unref( gmyth_uri );
129 myth_handle->file_transfer = gmyth_livetv_create_file_transfer (myth_handle->livetv);
131 if (NULL == myth_handle->file_transfer) {
135 if ( !gmyth_file_transfer_open( myth_handle->file_transfer, myth_handle->livetv->uri != NULL ?
136 gmyth_uri_get_path(myth_handle->livetv->uri) :
137 myth_handle->livetv->proginfo->pathname->str ) )
139 g_object_unref( myth_handle->file_transfer );
140 myth_handle->file_transfer = NULL;
146 myth_handle->file_transfer = gmyth_file_transfer_new (backend_info);
148 /* Verifies if the file exists */
149 if (!gmyth_util_file_exists (backend_info, gmyth_uri_get_path (gmyth_uri))) {
150 g_object_unref (backend_info);
154 /* sets the Playback monitor connection */
155 ret = gmyth_file_transfer_open ( myth_handle->file_transfer,
156 gmyth_uri_get_path (gmyth_uri) );
158 } /* if - LiveTV or not? */
161 g_warning ("MythTV FileTransfer open error.\n");
162 return GNOME_VFS_ERROR_NOT_OPEN;
165 g_object_unref (backend_info);
167 //if ( gmyth_uri != NULL )
168 // g_object_unref( gmyth_uri );
170 g_return_val_if_fail (myth_handle->file_transfer != NULL, GNOME_VFS_ERROR_NOT_OPEN);
172 if ( myth_handle->file_transfer->filesize < 0 && is_livetv ) {
173 myth_handle->content_size = (GnomeVFSFileSize) - 1;
174 myth_handle->content_size = gmyth_recorder_get_file_position( myth_handle->livetv->recorder );
176 myth_handle->content_size = myth_handle->file_transfer->filesize;
178 myth_handle->buffer = g_byte_array_sized_new (MYTHTV_BUFFER_SIZE);
179 myth_handle->buffer_remain = 0;
181 *method_handle = (GnomeVFSMethodHandle *) myth_handle;
186 static GnomeVFSResult
187 do_read (GnomeVFSMethod *method,
188 GnomeVFSMethodHandle *method_handle,
190 GnomeVFSFileSize num_bytes,
191 GnomeVFSFileSize *bytes_read,
192 GnomeVFSContext *context)
194 MythtvHandle *myth_handle = (MythtvHandle *) method_handle;
195 GnomeVFSFileSize bytes_to_read;
199 if (myth_handle->bytes_read >= myth_handle->content_size)
200 return GNOME_VFS_ERROR_EOF;
202 // fixme: change this to min math function
203 if (myth_handle->content_size > 0 && num_bytes > ( myth_handle->content_size - myth_handle->bytes_read ))
204 bytes_to_read = myth_handle->content_size - myth_handle->bytes_read;
206 bytes_to_read = num_bytes;
208 /* Loop sending the Myth File Transfer request:
209 * Retry whilst authentication fails and we supply it. */
210 //if (myth_handle->buffer_remain < MYTHTV_BUFFER_SIZE) {
211 if ( ( myth_handle->buffer_remain = myth_handle->buffer->len ) < MYTHTV_BUFFER_SIZE ) {
212 GByteArray *tmp_buffer = g_byte_array_new();
214 printf ("XXXXXXXXXXXXXX Pedindo %d %d\n", MYTHTV_BUFFER_SIZE, myth_handle->buffer_remain);
216 gint len = gmyth_file_transfer_read (myth_handle->file_transfer,
217 tmp_buffer, MYTHTV_BUFFER_SIZE - myth_handle->buffer_remain, TRUE);
220 g_byte_array_free (tmp_buffer, TRUE);
221 g_warning ("Fail to read bytes");
222 return GNOME_VFS_ERROR_IO;
223 } /*else if (len == 0) {
224 g_byte_array_free (tmp_buffer, TRUE);
225 g_warning ("End of file probably achieved");
226 return GNOME_VFS_ERROR_EOF;
229 myth_handle->buffer = g_byte_array_append (myth_handle->buffer,
230 tmp_buffer->data, len);
232 myth_handle->buffer_remain += len;
234 g_byte_array_free (tmp_buffer, TRUE);
238 bytes_to_read = (bytes_to_read > myth_handle->buffer_remain) ? myth_handle->buffer_remain : bytes_to_read;
239 /* gets the first buffer_size bytes from the byte array buffer variable */
241 g_memmove (buffer, myth_handle->buffer->data, bytes_to_read);
243 myth_handle->bytes_read += bytes_to_read;
244 myth_handle->buffer_remain -= bytes_to_read;
246 /* flushs the newly buffer got from byte array */
247 myth_handle->buffer = g_byte_array_remove_range (myth_handle->buffer, 0, bytes_to_read);
248 *bytes_read = bytes_to_read;
253 static GnomeVFSResult
254 do_close (GnomeVFSMethod *method,
255 GnomeVFSMethodHandle *method_handle,
256 GnomeVFSContext *context)
259 MythtvHandle *myth_handle = (MythtvHandle *) method_handle;
261 if (myth_handle->file_transfer) {
262 gmyth_file_transfer_close (myth_handle->file_transfer);
263 g_object_unref (myth_handle->file_transfer);
264 myth_handle->file_transfer = NULL;
267 if (myth_handle->livetv) {
268 g_object_unref (myth_handle->livetv);
269 myth_handle->livetv = NULL;
272 if (myth_handle->buffer) {
273 g_byte_array_free (myth_handle->buffer, TRUE);
274 myth_handle->buffer = NULL;
277 g_free (myth_handle);
282 static GnomeVFSResult
283 do_get_file_info (GnomeVFSMethod *method,
285 GnomeVFSFileInfo *file_info,
286 GnomeVFSFileInfoOptions options,
287 GnomeVFSContext *context)
289 GMythFileTransfer *file_transfer = NULL;
290 GMythRecorder *recorder = NULL;
291 GMythTVChain *tvchain = NULL;
292 GMythBackendInfo *backend_info = NULL;
293 GMythURI *gmyth_uri = NULL;
294 GMythSocket *socket = NULL;
295 gboolean is_livetv = FALSE;
298 /* Creates and fills out the backend info structure */
299 backend_info = gmyth_backend_info_new_with_uri (
300 gnome_vfs_unescape_string( gnome_vfs_uri_to_string( uri, GNOME_VFS_URI_HIDE_NONE ), "" ) );
302 /* creates an instance of */
303 gmyth_uri = gmyth_uri_new_with_value(
304 gnome_vfs_unescape_string( gnome_vfs_uri_to_string( uri, GNOME_VFS_URI_HIDE_NONE ), "" ) );
306 is_livetv = gmyth_uri_is_livetv( gmyth_uri );
308 file_info->valid_fields = file_info->valid_fields
309 | GNOME_VFS_FILE_INFO_FIELDS_TYPE
310 | GNOME_VFS_FILE_INFO_FIELDS_MIME_TYPE
311 | GNOME_VFS_FILE_INFO_FIELDS_PERMISSIONS;
312 file_info->type = GNOME_VFS_FILE_TYPE_REGULAR;
313 /* fixme: get from file extension? */
314 file_info->mime_type = g_strdup ("video/x-nuv");
315 file_info->permissions =
316 GNOME_VFS_PERM_USER_READ |
317 GNOME_VFS_PERM_OTHER_READ |
318 GNOME_VFS_PERM_GROUP_READ;
320 g_print( "gnome_vfs_uri == %s | gmyth_uri == %s.\n",
321 gnome_vfs_uri_get_path (uri),
322 gmyth_uri_get_path (gmyth_uri) );
324 /* Connect to the backend */
325 if ( gmyth_uri != NULL && is_livetv == TRUE ) {
329 /* start to get file info from LiveTV remote encoder */
331 socket = gmyth_socket_new ();
333 /* FIME: Implement this at gmyth_socket */
334 res = gmyth_socket_connect_to_backend (socket, backend_info->hostname,
335 backend_info->port, TRUE);
337 g_warning ("[%s] LiveTV can not connect to backend", __FUNCTION__);
342 if ( gmyth_remote_util_get_free_recorder_count (socket) <= 0 ) {
343 g_warning ("No free remote encoder available.");
348 /* Gets the recorder num */
349 recorder = remote_request_next_free_recorder (socket, -1);
350 gmyth_socket_close_connection (socket);
352 if ( recorder == NULL ) {
353 g_warning ("[%s] None remote encoder available", __FUNCTION__);
358 /* Init remote encoder. Opens its control socket. */
359 res = gmyth_recorder_setup(recorder);
361 g_warning ("[%s] Fail while setting remote encoder\n", __FUNCTION__);
366 /* Creates livetv chain handler */
367 tvchain = gmyth_tvchain_new();
368 gmyth_tvchain_initialize ( tvchain, backend_info );
370 if ( tvchain == NULL || tvchain->tvchain_id == NULL ) {
375 // Spawn live tv. Uses the socket to send mythprotocol data to start livetv in the backend (remotelly)
376 res = gmyth_recorder_spawntv ( recorder,
377 gmyth_tvchain_get_id(tvchain) );
379 g_warning ("[%s] Fail while spawn tv\n", __FUNCTION__);
384 gchar* channel_name = gmyth_uri_get_channel_name( gmyth_uri );
387 /* loop finished, set the max tries variable to zero again... */
388 gint wait_to_transfer = 0;
390 while (wait_to_transfer++ < MYTHTV_TRANSFER_MAX_WAITS &&
391 (gmyth_recorder_is_recording (recorder) == FALSE))
394 /* IS_RECORDING again, just like the MythTV backend does... */
395 gmyth_recorder_is_recording (recorder);
397 if ( channel_name != NULL )
399 /* Pauses remote encoder. */
400 res = gmyth_recorder_pause_recording(recorder);
402 g_warning ("[%s] Fail while pausing remote encoder\n", __FUNCTION__);
407 if ( gmyth_recorder_check_channel_name( recorder, channel_name ) )
409 if ( gmyth_recorder_set_channel_name( recorder, channel_name ) )
411 g_warning( "Channel changed!!! [%s].\n", channel_name );
415 } /* if - changes the channel number */
422 GMythProgramInfo* prog_info = gmyth_recorder_get_current_program_info( recorder );
424 if ( prog_info != NULL )
427 g_print( "path = %s", prog_info->pathname->str );
429 file_info->name = g_strdup ( g_strrstr( prog_info->pathname->str, "/" ) );
432 size = gmyth_recorder_get_file_position( recorder );
435 //GMythProgramInfo *prog_info = gmyth_recorder_get_current_program_info ( recorder );
436 file_info->size = prog_info->filesize;
439 file_info->size = size;
442 file_info->size = gmyth_recorder_get_file_position( recorder );
445 if ( tvchain != NULL)
446 g_object_unref (tvchain);
448 if ( recorder != NULL )
449 g_object_unref (recorder);
451 g_object_unref( prog_info );
455 /* start to get file info from remote file encoder */
456 file_transfer = gmyth_file_transfer_new (backend_info);
458 /* Verifies if the file exists */
459 if (!gmyth_util_file_exists (backend_info, gmyth_uri_get_path (gmyth_uri))) {
460 g_object_unref (backend_info);
461 return GNOME_VFS_ERROR_NOT_FOUND;
464 /* sets the Playback monitor connection */
465 ret = gmyth_file_transfer_open ( file_transfer, gmyth_uri_get_path (gmyth_uri) );
467 file_info->name = g_strdup ( gnome_vfs_uri_get_path (uri) );
470 } /* if - LiveTV or not? */
474 g_warning ("MythTV FileTransfer open error\n");
475 return GNOME_VFS_ERROR_NOT_OPEN;
478 if ( ret == TRUE && file_transfer != NULL ) {
479 file_info->size = gmyth_file_transfer_get_filesize (file_transfer);
481 g_object_unref (file_transfer);
484 file_info->block_count = GNOME_VFS_FILE_INFO_FIELDS_BLOCK_COUNT;
485 file_info->io_block_size = GNOME_VFS_FILE_INFO_FIELDS_IO_BLOCK_SIZE;
489 g_object_unref (backend_info);
495 do_is_local (GnomeVFSMethod *method,
496 const GnomeVFSURI *uri)
501 static GnomeVFSMethod method = {
502 sizeof (GnomeVFSMethod),
533 vfs_module_init (const char *method_name, const char *args)
539 vfs_module_shutdown (GnomeVFSMethod *method)