[svn r365] Optimizations to the N800.
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>
38 #include <gmyth/gmyth_programinfo.h>
40 #define GST_MYTHTV_ID_NUM 1
41 #define MYTHTV_VERSION_DEFAULT 30
42 #define MYTHTV_TRANSFER_MAX_WAITS 100
44 /* internal GnomeVFS plug-in buffer size ( 120 Kbytes ) */
45 #define MYTHTV_BUFFER_SIZE 1024*80
46 /* internally sized GnomeVFS plug-in buffer ( 4 Kbytes ) */
47 #define MYTHTV_MAX_VFS_BUFFER_SIZE 4096
48 /* maximum number of bytes to be requested to the MythTV backend ( 64 Kbytes ) */
49 #define MYTHTV_MAX_REQUEST_SIZE 1024*64
51 static GnomeVFSResult do_read (GnomeVFSMethod *method,
52 GnomeVFSMethodHandle *method_handle,
54 GnomeVFSFileSize num_bytes,
55 GnomeVFSFileSize *bytes_read,
56 GnomeVFSContext *context);
59 GMythFileTransfer *file_transfer;
75 //static MythtvHandle *myth_handle = NULL;
78 do_open (GnomeVFSMethod *method,
79 GnomeVFSMethodHandle **method_handle,
81 GnomeVFSOpenMode mode,
82 GnomeVFSContext *context)
84 MythtvHandle *myth_handle = NULL;
85 GMythBackendInfo *backend_info = NULL;
86 GMythURI *gmyth_uri = NULL;
89 _GNOME_VFS_METHOD_PARAM_CHECK (method_handle != NULL);
90 _GNOME_VFS_METHOD_PARAM_CHECK (uri != NULL);
92 if (mode & GNOME_VFS_OPEN_WRITE) {
93 return GNOME_VFS_ERROR_NOT_PERMITTED;
96 if (gnome_vfs_uri_get_host_name (uri) == NULL) {
97 return GNOME_VFS_ERROR_INVALID_HOST_NAME;
101 if ( method_handle != NULL && *method_handle != NULL )
103 myth_handle = (MythtvHandle*)*method_handle;
104 //if ( !myth_handle->configured )
108 if ( ( NULL == myth_handle ) || !myth_handle->configured ) {
109 myth_handle = g_new0 (MythtvHandle, 1);
111 myth_handle->configured = FALSE;
113 myth_handle->is_livetv = FALSE;
115 /* Initialize mythtv handler*/
116 myth_handle->file_transfer = NULL;
117 myth_handle->livetv = NULL;
118 myth_handle->mythtv_version = MYTHTV_VERSION_DEFAULT;
119 myth_handle->bytes_read = 0;
120 myth_handle->content_size = (GnomeVFSFileSize) - 1;
122 /* Creates and fills out the backend info structure */
123 backend_info = gmyth_backend_info_new_with_uri (
124 gnome_vfs_unescape_string( gnome_vfs_uri_to_string( uri, GNOME_VFS_URI_HIDE_NONE ), "" ) );
126 /* creates an instance of */
127 gmyth_uri = gmyth_uri_new_with_value(
128 gnome_vfs_unescape_string( gnome_vfs_uri_to_string( uri, GNOME_VFS_URI_HIDE_NONE ), "" ) );
130 myth_handle->is_livetv = gmyth_uri_is_livetv( gmyth_uri );
132 /* Connect to the backend */
133 if ( gmyth_uri != NULL && myth_handle->is_livetv == TRUE ) {
135 if ( NULL == myth_handle->livetv )
137 myth_handle->livetv = gmyth_livetv_new ();
139 myth_handle->channel_name = gmyth_uri_get_channel_name( gmyth_uri );
141 g_debug( "[%s] Channel name = %s\n", __FUNCTION__, myth_handle->channel_name );
143 if ( myth_handle->channel_name != NULL ) {
144 if (gmyth_livetv_channel_name_setup (myth_handle->livetv, myth_handle->channel_name,
145 backend_info) == FALSE) {
146 g_object_unref( gmyth_uri );
150 if ( gmyth_livetv_setup (myth_handle->livetv, backend_info) == FALSE ) {
151 g_object_unref( gmyth_uri );
157 if ( NULL == myth_handle->file_transfer ) {
158 myth_handle->file_transfer = gmyth_livetv_create_file_transfer (myth_handle->livetv);
160 if (NULL == myth_handle->file_transfer) {
162 g_debug ("MythTV FileTransfer is NULL!\n");
163 return GNOME_VFS_ERROR_NOT_OPEN;
166 if ( !gmyth_file_transfer_open( myth_handle->file_transfer, myth_handle->livetv->uri != NULL ?
167 gmyth_uri_get_path(myth_handle->livetv->uri) :
168 myth_handle->livetv->proginfo->pathname->str ) )
170 g_debug ("Couldn't open MythTV FileTransfer is NULL!\n");
171 g_object_unref( myth_handle->file_transfer );
172 myth_handle->file_transfer = NULL;
175 } /* if - FileTransfer is NULL, or not */
179 if (NULL == myth_handle->file_transfer ) {
181 myth_handle->file_transfer = gmyth_file_transfer_new (backend_info);
183 /* Verifies if the file exists */
184 if (!gmyth_util_file_exists (backend_info, gmyth_uri_get_path (gmyth_uri))) {
185 g_object_unref (backend_info);
189 /* sets the Playback monitor connection */
190 ret = gmyth_file_transfer_open ( myth_handle->file_transfer,
191 gmyth_uri_get_path (gmyth_uri) );
195 } /* if - LiveTV or not? */
198 g_debug ("MythTV FileTransfer open error.\n");
199 return GNOME_VFS_ERROR_NOT_OPEN;
202 myth_handle->configured = TRUE;
204 g_object_unref (backend_info);
206 if ( gmyth_uri != NULL )
207 g_object_unref( gmyth_uri );
209 myth_handle->buffer = g_byte_array_sized_new (MYTHTV_BUFFER_SIZE);
210 myth_handle->buffer_remain = 0;
212 g_return_val_if_fail (myth_handle->file_transfer != NULL, GNOME_VFS_ERROR_NOT_OPEN);
214 if ( /*myth_handle->file_transfer->filesize <= 0 && */myth_handle->is_livetv ) {
215 myth_handle->content_size = gmyth_recorder_get_file_position(myth_handle->livetv->recorder);
217 myth_handle->content_size = myth_handle->file_transfer->filesize;
220 } /* if - configured or not? */
222 *method_handle = (GnomeVFSMethodHandle *) myth_handle;
227 static GnomeVFSResult
228 do_read (GnomeVFSMethod *method,
229 GnomeVFSMethodHandle *method_handle,
231 GnomeVFSFileSize num_bytes,
232 GnomeVFSFileSize *bytes_read,
233 GnomeVFSContext *context)
235 MythtvHandle *myth_handle = (MythtvHandle *) method_handle;
236 GnomeVFSFileSize bytes_to_read = num_bytes;
238 if ( !myth_handle->is_livetv && ( myth_handle->bytes_read >= myth_handle->content_size ) )
239 return GNOME_VFS_ERROR_EOF;
241 /* fixme: change this to min math function */
242 if (( myth_handle->content_size > 0 ) )
244 if ( !myth_handle->is_livetv &&
245 ( num_bytes > ( myth_handle->content_size - myth_handle->bytes_read ) ) )
247 bytes_to_read = myth_handle->content_size - myth_handle->bytes_read;
251 /* Loop sending the Myth File Transfer request:
252 * Retry whilst authentication fails and we supply it. */
253 if ( ( myth_handle->buffer_remain = myth_handle->buffer->len ) < bytes_to_read )
257 while ( MYTHTV_BUFFER_SIZE != myth_handle->buffer_remain ) {
259 /* resize buffer length request to no more than MYTHTV_MAX_REQUEST_SIZE */
260 if ( ( MYTHTV_BUFFER_SIZE - myth_handle->buffer_remain ) <= MYTHTV_MAX_REQUEST_SIZE )
261 buffer_size = MYTHTV_BUFFER_SIZE - myth_handle->buffer_remain;
263 buffer_size = MYTHTV_MAX_REQUEST_SIZE;
265 GByteArray *tmp_buffer = g_byte_array_new();
267 g_debug ("XXXXXXXXXX Asking %d bytes (there is %d bytes in the buffer)\n",
268 buffer_size, myth_handle->buffer_remain);
270 gint len = gmyth_file_transfer_read (myth_handle->file_transfer,
271 tmp_buffer, buffer_size, TRUE);
273 if (!myth_handle->is_livetv && len < 0) {
274 g_byte_array_free (tmp_buffer, TRUE);
275 g_debug ("Fail to read bytes");
276 return GNOME_VFS_ERROR_IO;
277 } /*else if (len == 0) {
278 g_byte_array_free (tmp_buffer, TRUE);
279 g_warning ("End of file probably achieved");
280 return GNOME_VFS_ERROR_EOF;
283 myth_handle->buffer = g_byte_array_append ( myth_handle->buffer,
284 tmp_buffer->data, len );
286 myth_handle->buffer_remain += len;
288 if ( tmp_buffer != NULL )
290 g_byte_array_free ( tmp_buffer, TRUE );
293 } /* while - iterates until fills the internal buffer */
295 } /* if - got from the network, or not */
297 bytes_to_read = ( bytes_to_read > myth_handle->buffer_remain ) ? myth_handle->buffer_remain : bytes_to_read;
298 /* gets the first buffer_size bytes from the byte array buffer variable */
300 g_memmove (buffer, myth_handle->buffer->data, bytes_to_read);
302 myth_handle->bytes_read += bytes_to_read;
303 myth_handle->buffer_remain -= bytes_to_read;
305 /* flushs the newly buffer got from byte array */
306 myth_handle->buffer = g_byte_array_remove_range (myth_handle->buffer, 0, bytes_to_read);
307 g_debug ("Got from %llu bytes from internal buffer. (there are %d bytes in the buffer, from a total of %llu dispatched.)\n",
308 bytes_to_read, myth_handle->buffer_remain, myth_handle->bytes_read );
310 *bytes_read = bytes_to_read;
315 static GnomeVFSResult
316 do_close (GnomeVFSMethod *method,
317 GnomeVFSMethodHandle *method_handle,
318 GnomeVFSContext *context)
321 MythtvHandle *myth_handle = (MythtvHandle *) method_handle;
323 //if ( NULL == myth_handle || myth_handle->configured ) {
325 if (myth_handle->file_transfer != NULL) {
326 g_object_unref (myth_handle->file_transfer);
327 myth_handle->file_transfer = NULL;
330 if (myth_handle->is_livetv && myth_handle->livetv != NULL) {
331 g_object_unref (myth_handle->livetv);
332 myth_handle->livetv = NULL;
335 if (myth_handle->buffer) {
336 g_byte_array_free (myth_handle->buffer, TRUE);
337 myth_handle->buffer = NULL;
340 myth_handle->configured = FALSE;
342 g_free (myth_handle);
351 static GnomeVFSResult
352 do_get_file_info (GnomeVFSMethod *method,
354 GnomeVFSFileInfo *file_info,
355 GnomeVFSFileInfoOptions options,
356 GnomeVFSContext *context)
358 GMythFileTransfer *file_transfer = NULL;
359 GMythRecorder *recorder = NULL;
360 GMythTVChain *tvchain = NULL;
361 GMythBackendInfo *backend_info = NULL;
362 GMythURI *gmyth_uri = NULL;
363 GMythSocket *socket = NULL;
364 gboolean is_livetv = FALSE;
368 /* Creates and fills out the backend info structure */
369 backend_info = gmyth_backend_info_new_with_uri (
370 gnome_vfs_unescape_string( gnome_vfs_uri_to_string( uri, GNOME_VFS_URI_HIDE_NONE ), "" ) );
372 /* creates an instance of */
373 gmyth_uri = gmyth_uri_new_with_value(
374 gnome_vfs_unescape_string( gnome_vfs_uri_to_string( uri, GNOME_VFS_URI_HIDE_NONE ), "" ) );
376 is_livetv = gmyth_uri_is_livetv( gmyth_uri );
378 file_info->valid_fields = file_info->valid_fields
379 | GNOME_VFS_FILE_INFO_FIELDS_TYPE
380 | GNOME_VFS_FILE_INFO_FIELDS_MIME_TYPE
381 | GNOME_VFS_FILE_INFO_FIELDS_PERMISSIONS;
382 file_info->type = GNOME_VFS_FILE_TYPE_REGULAR;
383 /* fixme: get from file extension? */
384 file_info->mime_type = g_strdup ("video/x-nuv");
385 file_info->permissions =
386 GNOME_VFS_PERM_USER_READ |
387 GNOME_VFS_PERM_OTHER_READ |
388 GNOME_VFS_PERM_GROUP_READ;
390 g_print( "gnome_vfs_uri == %s | gmyth_uri == %s.\n",
391 gnome_vfs_uri_get_path (uri),
392 gmyth_uri_get_path (gmyth_uri) );
394 /* Connect to the backend */
395 if ( gmyth_uri != NULL && is_livetv == TRUE ) {
397 /* start to get file info from LiveTV remote encoder */
398 socket = gmyth_socket_new ();
400 /* FIME: Implement this at gmyth_socket */
401 res = gmyth_socket_connect_to_backend (socket, backend_info->hostname,
402 backend_info->port, TRUE);
404 g_print ("[%s] LiveTV can not connect to backend", __FUNCTION__);
409 if ( gmyth_remote_util_get_free_recorder_count (socket) <= 0 ) {
410 g_print ("No free remote encoder available.");
415 /* Gets the recorder num */
416 recorder = remote_request_next_free_recorder (socket, -1);
418 //if ( socket != NULL )
419 // g_object_unref (socket);
421 if ( recorder == NULL ) {
422 g_print ("[%s] None remote encoder available", __FUNCTION__);
427 /* Init remote encoder. Opens its control socket. */
428 res = gmyth_recorder_setup(recorder);
430 g_print ("[%s] Fail while setting remote encoder\n", __FUNCTION__);
435 /* Creates livetv chain handler */
436 tvchain = gmyth_tvchain_new();
437 gmyth_tvchain_initialize ( tvchain, backend_info );
439 if ( tvchain == NULL || tvchain->tvchain_id == NULL ) {
444 // Spawn live tv. Uses the socket to send mythprotocol data to start livetv in the backend (remotelly)
445 res = gmyth_recorder_spawntv ( recorder,
446 gmyth_tvchain_get_id(tvchain) );
448 g_warning ("[%s] Fail while spawn tv\n", __FUNCTION__);
453 //gchar* channel_name = gmyth_uri_get_channel_name( gmyth_uri );
456 GMythProgramInfo* prog_info = gmyth_recorder_get_current_program_info( recorder );
458 if ( prog_info != NULL )
460 //gmyth_program_info_print( prog_info );
461 g_debug( "Program Info: %s\n", gmyth_program_info_to_string( prog_info ) );
463 g_print( "path = %s", prog_info->pathname->str );
465 file_info->name = g_strdup ( g_strrstr( prog_info->pathname->str, "/" ) );
468 file_info->name = g_strdup ( "LiveTV.nuv" );
469 file_info->size = gmyth_recorder_get_file_position( recorder );
472 if ( recorder != NULL )
473 g_object_unref (recorder);
475 if ( prog_info != NULL )
476 g_object_unref( prog_info );
478 if ( tvchain != NULL )
479 g_object_unref (tvchain);
481 file_info->size = (GnomeVFSFileSize) - 1;
485 /* start to get file info from remote file encoder */
486 file_transfer = gmyth_file_transfer_new (backend_info);
488 /* Verifies if the file exists */
489 if (!gmyth_util_file_exists (backend_info, gmyth_uri_get_path (gmyth_uri))) {
490 g_object_unref (backend_info);
491 return GNOME_VFS_ERROR_NOT_FOUND;
494 /* sets the Playback monitor connection */
495 ret = gmyth_file_transfer_open ( file_transfer, gmyth_uri_get_path (gmyth_uri) );
497 file_info->name = g_strdup ( gnome_vfs_uri_get_path (uri) );
499 } /* if - LiveTV or not? */
502 g_debug ("MythTV FileTransfer open error\n");
503 return GNOME_VFS_ERROR_NOT_OPEN;
506 if ( ret == TRUE && file_transfer != NULL ) {
507 file_info->size = gmyth_file_transfer_get_filesize (file_transfer);
509 g_object_unref (file_transfer);
512 file_info->block_count = GNOME_VFS_FILE_INFO_FIELDS_BLOCK_COUNT;
513 file_info->io_block_size = GNOME_VFS_FILE_INFO_FIELDS_IO_BLOCK_SIZE;
517 g_object_unref (backend_info);
520 return GNOME_VFS_ERROR_IO;
526 do_is_local (GnomeVFSMethod *method,
527 const GnomeVFSURI *uri)
532 static GnomeVFSMethod method = {
533 sizeof (GnomeVFSMethod),
564 vfs_module_init (const char *method_name, const char *args)
570 vfs_module_shutdown (GnomeVFSMethod *method)