[svn r350] Changed criteria to move to next program chain.
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->is_livetv && ( myth_handle->content_size > 0 ) &&
220 ( num_bytes > ( myth_handle->content_size - myth_handle->bytes_read ) ) )
221 bytes_to_read = myth_handle->content_size - myth_handle->bytes_read;
223 bytes_to_read = num_bytes;
225 /* Loop sending the Myth File Transfer request:
226 * Retry whilst authentication fails and we supply it. */
227 //if (myth_handle->buffer_remain < MYTHTV_BUFFER_SIZE) {
228 if ( ( myth_handle->buffer_remain = myth_handle->buffer->len ) < MYTHTV_BUFFER_SIZE ) {
229 GByteArray *tmp_buffer = g_byte_array_new();
231 printf ("XXXXXXXXXXXXXX Pedindo %d %d\n", MYTHTV_BUFFER_SIZE, myth_handle->buffer_remain);
233 gint len = gmyth_file_transfer_read (myth_handle->file_transfer,
234 tmp_buffer, MYTHTV_BUFFER_SIZE - myth_handle->buffer_remain, TRUE);
236 if (!myth_handle->is_livetv && len < 0) {
237 g_byte_array_free (tmp_buffer, TRUE);
238 g_print ("Fail to read bytes");
239 return GNOME_VFS_ERROR_IO;
240 } /*else if (len == 0) {
241 g_byte_array_free (tmp_buffer, TRUE);
242 g_warning ("End of file probably achieved");
243 return GNOME_VFS_ERROR_EOF;
246 myth_handle->buffer = g_byte_array_append (myth_handle->buffer,
247 tmp_buffer->data, len);
249 myth_handle->buffer_remain += len;
251 g_byte_array_free (tmp_buffer, TRUE);
255 bytes_to_read = (bytes_to_read > myth_handle->buffer_remain) ? myth_handle->buffer_remain : bytes_to_read;
256 /* gets the first buffer_size bytes from the byte array buffer variable */
258 g_memmove (buffer, myth_handle->buffer->data, bytes_to_read);
260 myth_handle->bytes_read += bytes_to_read;
261 myth_handle->buffer_remain -= bytes_to_read;
263 /* flushs the newly buffer got from byte array */
264 myth_handle->buffer = g_byte_array_remove_range (myth_handle->buffer, 0, bytes_to_read);
265 *bytes_read = bytes_to_read;
270 static GnomeVFSResult
271 do_close (GnomeVFSMethod *method,
272 GnomeVFSMethodHandle *method_handle,
273 GnomeVFSContext *context)
276 MythtvHandle *myth_handle = (MythtvHandle *) method_handle;
278 if (myth_handle->file_transfer != NULL) {
279 //gmyth_file_transfer_close (myth_handle->file_transfer);
280 g_object_unref (myth_handle->file_transfer);
281 myth_handle->file_transfer = NULL;
284 if (myth_handle->is_livetv && myth_handle->livetv != NULL) {
285 g_object_unref (myth_handle->livetv);
286 myth_handle->livetv = NULL;
289 if (myth_handle->buffer) {
290 g_byte_array_free (myth_handle->buffer, TRUE);
291 myth_handle->buffer = NULL;
294 g_free (myth_handle);
299 static GnomeVFSResult
300 do_get_file_info (GnomeVFSMethod *method,
302 GnomeVFSFileInfo *file_info,
303 GnomeVFSFileInfoOptions options,
304 GnomeVFSContext *context)
306 GMythFileTransfer *file_transfer = NULL;
307 GMythRecorder *recorder = NULL;
308 GMythTVChain *tvchain = NULL;
309 GMythBackendInfo *backend_info = NULL;
310 GMythURI *gmyth_uri = NULL;
311 GMythSocket *socket = NULL;
312 gboolean is_livetv = FALSE;
316 /* Creates and fills out the backend info structure */
317 backend_info = gmyth_backend_info_new_with_uri (
318 gnome_vfs_unescape_string( gnome_vfs_uri_to_string( uri, GNOME_VFS_URI_HIDE_NONE ), "" ) );
320 /* creates an instance of */
321 gmyth_uri = gmyth_uri_new_with_value(
322 gnome_vfs_unescape_string( gnome_vfs_uri_to_string( uri, GNOME_VFS_URI_HIDE_NONE ), "" ) );
324 is_livetv = gmyth_uri_is_livetv( gmyth_uri );
326 file_info->valid_fields = file_info->valid_fields
327 | GNOME_VFS_FILE_INFO_FIELDS_TYPE
328 | GNOME_VFS_FILE_INFO_FIELDS_MIME_TYPE
329 | GNOME_VFS_FILE_INFO_FIELDS_PERMISSIONS;
330 file_info->type = GNOME_VFS_FILE_TYPE_REGULAR;
331 /* fixme: get from file extension? */
332 file_info->mime_type = g_strdup ("video/x-nuv");
333 file_info->permissions =
334 GNOME_VFS_PERM_USER_READ |
335 GNOME_VFS_PERM_OTHER_READ |
336 GNOME_VFS_PERM_GROUP_READ;
338 g_print( "gnome_vfs_uri == %s | gmyth_uri == %s.\n",
339 gnome_vfs_uri_get_path (uri),
340 gmyth_uri_get_path (gmyth_uri) );
342 /* Connect to the backend */
343 if ( gmyth_uri != NULL && is_livetv == TRUE ) {
345 /* start to get file info from LiveTV remote encoder */
346 socket = gmyth_socket_new ();
348 /* FIME: Implement this at gmyth_socket */
349 res = gmyth_socket_connect_to_backend (socket, backend_info->hostname,
350 backend_info->port, TRUE);
352 g_print ("[%s] LiveTV can not connect to backend", __FUNCTION__);
357 if ( gmyth_remote_util_get_free_recorder_count (socket) <= 0 ) {
358 g_print ("No free remote encoder available.");
363 /* Gets the recorder num */
364 recorder = remote_request_next_free_recorder (socket, -1);
366 //if ( socket != NULL )
367 // g_object_unref (socket);
369 if ( recorder == NULL ) {
370 g_print ("[%s] None remote encoder available", __FUNCTION__);
375 /* Init remote encoder. Opens its control socket. */
376 res = gmyth_recorder_setup(recorder);
378 g_print ("[%s] Fail while setting remote encoder\n", __FUNCTION__);
383 /* Creates livetv chain handler */
384 tvchain = gmyth_tvchain_new();
385 gmyth_tvchain_initialize ( tvchain, backend_info );
387 if ( tvchain == NULL || tvchain->tvchain_id == NULL ) {
392 // Spawn live tv. Uses the socket to send mythprotocol data to start livetv in the backend (remotelly)
393 res = gmyth_recorder_spawntv ( recorder,
394 gmyth_tvchain_get_id(tvchain) );
396 g_warning ("[%s] Fail while spawn tv\n", __FUNCTION__);
401 //gchar* channel_name = gmyth_uri_get_channel_name( gmyth_uri );
404 GMythProgramInfo* prog_info = gmyth_recorder_get_current_program_info( recorder );
406 if ( prog_info != NULL )
408 gmyth_program_info_print( prog_info );
410 g_print( "path = %s", prog_info->pathname->str );
412 file_info->name = g_strdup ( g_strrstr( prog_info->pathname->str, "/" ) );
415 file_info->name = g_strdup ( "LiveTV.nuv" );
416 file_info->size = gmyth_recorder_get_file_position( recorder );
419 if ( recorder != NULL )
420 g_object_unref (recorder);
422 if ( prog_info != NULL )
423 g_object_unref( prog_info );
425 if ( tvchain != NULL )
426 g_object_unref (tvchain);
428 file_info->size = (GnomeVFSFileSize) - 1;
432 /* start to get file info from remote file encoder */
433 file_transfer = gmyth_file_transfer_new (backend_info);
435 /* Verifies if the file exists */
436 if (!gmyth_util_file_exists (backend_info, gmyth_uri_get_path (gmyth_uri))) {
437 g_object_unref (backend_info);
438 return GNOME_VFS_ERROR_NOT_FOUND;
441 /* sets the Playback monitor connection */
442 ret = gmyth_file_transfer_open ( file_transfer, gmyth_uri_get_path (gmyth_uri) );
444 file_info->name = g_strdup ( gnome_vfs_uri_get_path (uri) );
446 } /* if - LiveTV or not? */
449 g_debug ("MythTV FileTransfer open error\n");
450 return GNOME_VFS_ERROR_NOT_OPEN;
453 if ( ret == TRUE && file_transfer != NULL ) {
454 file_info->size = gmyth_file_transfer_get_filesize (file_transfer);
456 g_object_unref (file_transfer);
459 file_info->block_count = GNOME_VFS_FILE_INFO_FIELDS_BLOCK_COUNT;
460 file_info->io_block_size = GNOME_VFS_FILE_INFO_FIELDS_IO_BLOCK_SIZE;
464 g_object_unref (backend_info);
467 return GNOME_VFS_ERROR_IO;
473 do_is_local (GnomeVFSMethod *method,
474 const GnomeVFSURI *uri)
479 static GnomeVFSMethod method = {
480 sizeof (GnomeVFSMethod),
511 vfs_module_init (const char *method_name, const char *args)
517 vfs_module_shutdown (GnomeVFSMethod *method)