[svn r342] Put back the do_get_file_info to the 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;
68 do_open (GnomeVFSMethod *method,
69 GnomeVFSMethodHandle **method_handle,
71 GnomeVFSOpenMode mode,
72 GnomeVFSContext *context)
74 MythtvHandle *myth_handle;
75 GMythBackendInfo *backend_info;
76 GMythURI *gmyth_uri = NULL;
79 _GNOME_VFS_METHOD_PARAM_CHECK (method_handle != NULL);
80 _GNOME_VFS_METHOD_PARAM_CHECK (uri != NULL);
82 myth_handle = g_new0 (MythtvHandle, 1);
84 myth_handle->is_livetv = FALSE;
86 if (mode & GNOME_VFS_OPEN_WRITE) {
87 return GNOME_VFS_ERROR_NOT_PERMITTED;
90 if (gnome_vfs_uri_get_host_name (uri) == NULL) {
91 return GNOME_VFS_ERROR_INVALID_HOST_NAME;
94 /* Initialize mythtv handler*/
95 myth_handle->file_transfer = NULL;
96 myth_handle->livetv = NULL;
97 myth_handle->mythtv_version = MYTHTV_VERSION_DEFAULT;
98 myth_handle->bytes_read = 0;
99 myth_handle->content_size = (GnomeVFSFileSize) - 1;
101 /* Creates and fills out the backend info structure */
102 backend_info = gmyth_backend_info_new_with_uri (
103 gnome_vfs_unescape_string( gnome_vfs_uri_to_string( uri, GNOME_VFS_URI_HIDE_NONE ), "" ) );
105 /* creates an instance of */
106 gmyth_uri = gmyth_uri_new_with_value(
107 gnome_vfs_unescape_string( gnome_vfs_uri_to_string( uri, GNOME_VFS_URI_HIDE_NONE ), "" ) );
109 myth_handle->is_livetv = gmyth_uri_is_livetv( gmyth_uri );
111 /* Connect to the backend */
112 if ( gmyth_uri != NULL && myth_handle->is_livetv == TRUE ) {
114 if ( NULL == myth_handle->livetv )
116 myth_handle->livetv = gmyth_livetv_new ();
118 myth_handle->channel_name = gmyth_uri_get_channel_name( gmyth_uri );
120 g_debug( "[%s] Channel name = %s\n", __FUNCTION__, myth_handle->channel_name );
122 if ( myth_handle->channel_name != NULL ) {
123 if (gmyth_livetv_channel_name_setup (myth_handle->livetv, myth_handle->channel_name,
124 backend_info) == FALSE) {
125 g_object_unref( gmyth_uri );
129 if ( gmyth_livetv_setup (myth_handle->livetv, backend_info) == FALSE ) {
130 g_object_unref( gmyth_uri );
136 if ( NULL == myth_handle->file_transfer ) {
137 myth_handle->file_transfer = gmyth_livetv_create_file_transfer (myth_handle->livetv);
139 if (NULL == myth_handle->file_transfer) {
141 g_debug ("MythTV FileTransfer is NULL!\n");
142 return GNOME_VFS_ERROR_NOT_OPEN;
145 if ( !gmyth_file_transfer_open( myth_handle->file_transfer, myth_handle->livetv->uri != NULL ?
146 gmyth_uri_get_path(myth_handle->livetv->uri) :
147 myth_handle->livetv->proginfo->pathname->str ) )
149 g_debug ("Couldn't open MythTV FileTransfer is NULL!\n");
150 g_object_unref( myth_handle->file_transfer );
151 myth_handle->file_transfer = NULL;
154 } /* if - FileTransfer is NULL, or not */
158 if (NULL == myth_handle->file_transfer ) {
160 myth_handle->file_transfer = gmyth_file_transfer_new (backend_info);
162 /* Verifies if the file exists */
163 if (!gmyth_util_file_exists (backend_info, gmyth_uri_get_path (gmyth_uri))) {
164 g_object_unref (backend_info);
168 /* sets the Playback monitor connection */
169 ret = gmyth_file_transfer_open ( myth_handle->file_transfer,
170 gmyth_uri_get_path (gmyth_uri) );
174 } /* if - LiveTV or not? */
177 g_debug ("MythTV FileTransfer open error.\n");
178 return GNOME_VFS_ERROR_NOT_OPEN;
181 //g_object_unref (backend_info);
183 //if ( gmyth_uri != NULL )
184 // g_object_unref( gmyth_uri );
186 g_return_val_if_fail (myth_handle->file_transfer != NULL, GNOME_VFS_ERROR_NOT_OPEN);
188 if ( /*myth_handle->file_transfer->filesize <= 0 && */myth_handle->is_livetv ) {
189 myth_handle->content_size = (GnomeVFSFileSize) - 1;
190 //myth_handle->content_size = gmyth_recorder_get_file_position( myth_handle->livetv->recorder );
192 myth_handle->content_size = myth_handle->file_transfer->filesize;
194 myth_handle->buffer = g_byte_array_sized_new (MYTHTV_BUFFER_SIZE);
195 myth_handle->buffer_remain = 0;
197 *method_handle = (GnomeVFSMethodHandle *) myth_handle;
202 static GnomeVFSResult
203 do_read (GnomeVFSMethod *method,
204 GnomeVFSMethodHandle *method_handle,
206 GnomeVFSFileSize num_bytes,
207 GnomeVFSFileSize *bytes_read,
208 GnomeVFSContext *context)
210 MythtvHandle *myth_handle = (MythtvHandle *) method_handle;
211 GnomeVFSFileSize bytes_to_read;
215 if ( !myth_handle->is_livetv && ( myth_handle->bytes_read >= myth_handle->content_size ) )
216 return GNOME_VFS_ERROR_EOF;
218 // fixme: change this to min math function
219 if (myth_handle->content_size > 0 && num_bytes > ( myth_handle->content_size - myth_handle->bytes_read ))
220 bytes_to_read = myth_handle->content_size - myth_handle->bytes_read;
222 bytes_to_read = num_bytes;
224 /* Loop sending the Myth File Transfer request:
225 * Retry whilst authentication fails and we supply it. */
226 //if (myth_handle->buffer_remain < MYTHTV_BUFFER_SIZE) {
227 if ( ( myth_handle->buffer_remain = myth_handle->buffer->len ) < MYTHTV_BUFFER_SIZE ) {
228 GByteArray *tmp_buffer = g_byte_array_new();
230 printf ("XXXXXXXXXXXXXX Pedindo %d %d\n", MYTHTV_BUFFER_SIZE, myth_handle->buffer_remain);
232 gint len = gmyth_file_transfer_read (myth_handle->file_transfer,
233 tmp_buffer, MYTHTV_BUFFER_SIZE - myth_handle->buffer_remain, TRUE);
236 g_byte_array_free (tmp_buffer, TRUE);
237 g_print ("Fail to read bytes");
238 return GNOME_VFS_ERROR_IO;
239 } /*else if (len == 0) {
240 g_byte_array_free (tmp_buffer, TRUE);
241 g_warning ("End of file probably achieved");
242 return GNOME_VFS_ERROR_EOF;
245 myth_handle->buffer = g_byte_array_append (myth_handle->buffer,
246 tmp_buffer->data, len);
248 myth_handle->buffer_remain += len;
250 g_byte_array_free (tmp_buffer, TRUE);
254 bytes_to_read = (bytes_to_read > myth_handle->buffer_remain) ? myth_handle->buffer_remain : bytes_to_read;
255 /* gets the first buffer_size bytes from the byte array buffer variable */
257 g_memmove (buffer, myth_handle->buffer->data, bytes_to_read);
259 myth_handle->bytes_read += bytes_to_read;
260 myth_handle->buffer_remain -= bytes_to_read;
262 /* flushs the newly buffer got from byte array */
263 myth_handle->buffer = g_byte_array_remove_range (myth_handle->buffer, 0, bytes_to_read);
264 *bytes_read = bytes_to_read;
269 static GnomeVFSResult
270 do_close (GnomeVFSMethod *method,
271 GnomeVFSMethodHandle *method_handle,
272 GnomeVFSContext *context)
275 MythtvHandle *myth_handle = (MythtvHandle *) method_handle;
277 if (myth_handle->file_transfer) {
278 //gmyth_file_transfer_close (myth_handle->file_transfer);
279 g_object_unref (myth_handle->file_transfer);
280 myth_handle->file_transfer = NULL;
283 if (myth_handle->is_livetv && myth_handle->livetv != NULL) {
284 g_object_unref (myth_handle->livetv);
285 myth_handle->livetv = NULL;
288 if (myth_handle->buffer) {
289 g_byte_array_free (myth_handle->buffer, TRUE);
290 myth_handle->buffer = NULL;
293 g_free (myth_handle);
298 static GnomeVFSResult
299 do_get_file_info (GnomeVFSMethod *method,
301 GnomeVFSFileInfo *file_info,
302 GnomeVFSFileInfoOptions options,
303 GnomeVFSContext *context)
305 GMythFileTransfer *file_transfer = NULL;
306 GMythRecorder *recorder = NULL;
307 GMythBackendInfo *backend_info = NULL;
308 GMythURI *gmyth_uri = NULL;
309 GMythSocket *socket = NULL;
310 gboolean is_livetv = FALSE;
313 /* Creates and fills out the backend info structure */
314 backend_info = gmyth_backend_info_new_with_uri (
315 gnome_vfs_unescape_string( gnome_vfs_uri_to_string( uri, GNOME_VFS_URI_HIDE_NONE ), "" ) );
317 /* creates an instance of */
318 gmyth_uri = gmyth_uri_new_with_value(
319 gnome_vfs_unescape_string( gnome_vfs_uri_to_string( uri, GNOME_VFS_URI_HIDE_NONE ), "" ) );
321 is_livetv = gmyth_uri_is_livetv( gmyth_uri );
323 file_info->valid_fields = file_info->valid_fields
324 | GNOME_VFS_FILE_INFO_FIELDS_TYPE
325 | GNOME_VFS_FILE_INFO_FIELDS_MIME_TYPE
326 | GNOME_VFS_FILE_INFO_FIELDS_PERMISSIONS;
327 file_info->type = GNOME_VFS_FILE_TYPE_REGULAR;
328 /* fixme: get from file extension? */
329 file_info->mime_type = g_strdup ("video/x-nuv");
330 file_info->permissions =
331 GNOME_VFS_PERM_USER_READ |
332 GNOME_VFS_PERM_OTHER_READ |
333 GNOME_VFS_PERM_GROUP_READ;
335 g_print( "gnome_vfs_uri == %s | gmyth_uri == %s.\n",
336 gnome_vfs_uri_get_path (uri),
337 gmyth_uri_get_path (gmyth_uri) );
339 /* Connect to the backend */
340 if ( gmyth_uri != NULL && is_livetv == TRUE ) {
343 /* start to get file info from LiveTV remote encoder */
344 socket = gmyth_socket_new ();
346 /* FIME: Implement this at gmyth_socket */
347 res = gmyth_socket_connect_to_backend (socket, backend_info->hostname,
348 backend_info->port, TRUE);
350 g_print ("[%s] LiveTV can not connect to backend", __FUNCTION__);
355 if ( gmyth_remote_util_get_free_recorder_count (socket) <= 0 ) {
356 g_print ("No free remote encoder available.");
361 /* Gets the recorder num */
362 recorder = remote_request_next_free_recorder (socket, -1);
364 if ( socket != NULL )
365 g_object_unref (socket);
367 if ( recorder == NULL ) {
368 g_print ("[%s] None remote encoder available", __FUNCTION__);
373 /* Init remote encoder. Opens its control socket. */
374 res = gmyth_recorder_setup(recorder);
376 g_print ("[%s] Fail while setting remote encoder\n", __FUNCTION__);
381 //gchar* channel_name = gmyth_uri_get_channel_name( gmyth_uri );
384 GMythProgramInfo* prog_info = gmyth_recorder_get_current_program_info( recorder );
386 if ( prog_info != NULL )
388 gmyth_program_info_print( prog_info );
390 g_print( "path = %s", prog_info->pathname->str );
392 file_info->name = g_strdup ( g_strrstr( prog_info->pathname->str, "/" ) );
395 size = gmyth_recorder_get_file_position( recorder );
398 file_info->size = prog_info->filesize;
401 file_info->size = size;
404 file_info->size = gmyth_recorder_get_file_position( recorder );
407 if ( recorder != NULL )
408 g_object_unref (recorder);
410 if ( prog_info != NULL )
411 g_object_unref( prog_info );
414 file_info->size = (GnomeVFSFileSize) - 1;
415 file_info->name = g_strdup ( "LiveTV.nuv" );
420 /* start to get file info from remote file encoder */
421 file_transfer = gmyth_file_transfer_new (backend_info);
423 /* Verifies if the file exists */
424 if (!gmyth_util_file_exists (backend_info, gmyth_uri_get_path (gmyth_uri))) {
425 g_object_unref (backend_info);
426 return GNOME_VFS_ERROR_NOT_FOUND;
429 /* sets the Playback monitor connection */
430 ret = gmyth_file_transfer_open ( file_transfer, gmyth_uri_get_path (gmyth_uri) );
432 file_info->name = g_strdup ( gnome_vfs_uri_get_path (uri) );
434 } /* if - LiveTV or not? */
437 g_debug ("MythTV FileTransfer open error\n");
438 return GNOME_VFS_ERROR_NOT_OPEN;
441 if ( ret == TRUE && file_transfer != NULL ) {
442 file_info->size = gmyth_file_transfer_get_filesize (file_transfer);
444 g_object_unref (file_transfer);
447 file_info->block_count = GNOME_VFS_FILE_INFO_FIELDS_BLOCK_COUNT;
448 file_info->io_block_size = GNOME_VFS_FILE_INFO_FIELDS_IO_BLOCK_SIZE;
452 g_object_unref (backend_info);
458 do_is_local (GnomeVFSMethod *method,
459 const GnomeVFSURI *uri)
464 static GnomeVFSMethod method = {
465 sizeof (GnomeVFSMethod),
496 vfs_module_init (const char *method_name, const char *args)
502 vfs_module_shutdown (GnomeVFSMethod *method)