[svn r314] A lot of bug fixed. Test recordings program added.
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>
37 #define GST_MYTHTV_ID_NUM 1
38 #define MYTHTV_VERSION_DEFAULT 30
39 #define MYTHTV_TRANSFER_MAX_WAITS 100
41 #define MYTHTV_BUFFER_SIZE 1024*64
43 static GnomeVFSResult do_read (GnomeVFSMethod *method,
44 GnomeVFSMethodHandle *method_handle,
46 GnomeVFSFileSize num_bytes,
47 GnomeVFSFileSize *bytes_read,
48 GnomeVFSContext *context);
51 GMythFileTransfer *file_transfer;
64 do_open (GnomeVFSMethod *method,
65 GnomeVFSMethodHandle **method_handle,
67 GnomeVFSOpenMode mode,
68 GnomeVFSContext *context)
70 MythtvHandle *myth_handle;
71 GMythBackendInfo *backend_info;
72 GMythURI *gmyth_uri = NULL;
74 gboolean is_livetv = FALSE;
76 _GNOME_VFS_METHOD_PARAM_CHECK (method_handle != NULL);
77 _GNOME_VFS_METHOD_PARAM_CHECK (uri != NULL);
79 myth_handle = g_new0 (MythtvHandle, 1);
81 if (mode & GNOME_VFS_OPEN_WRITE) {
82 return GNOME_VFS_ERROR_NOT_PERMITTED;
85 if (gnome_vfs_uri_get_host_name (uri) == NULL) {
86 return GNOME_VFS_ERROR_INVALID_HOST_NAME;
89 /* Initialize mythtv handler*/
90 myth_handle->file_transfer = NULL;
91 myth_handle->livetv = NULL;
92 myth_handle->mythtv_version = MYTHTV_VERSION_DEFAULT;
93 myth_handle->bytes_read = 0;
94 myth_handle->content_size = 0;
96 /* Creates and fills out the backend info structure */
97 backend_info = gmyth_backend_info_new_with_uri (
98 gnome_vfs_unescape_string( gnome_vfs_uri_to_string( uri, GNOME_VFS_URI_HIDE_NONE ), "" ) );
100 /* creates an instance of */
101 gmyth_uri = gmyth_uri_new_with_value(
102 gnome_vfs_unescape_string( gnome_vfs_uri_to_string( uri, GNOME_VFS_URI_HIDE_NONE ), "" ) );
104 is_livetv = gmyth_uri_is_livetv( gmyth_uri );
106 /* Connect to the backend */
107 if ( gmyth_uri != NULL && is_livetv == TRUE ) {
108 myth_handle->livetv = gmyth_livetv_new ();
110 myth_handle->channel_num = gmyth_uri_get_channel_num( gmyth_uri );
112 if ( myth_handle->channel_num != -1 ) {
113 if (gmyth_livetv_channel_setup (myth_handle->livetv, myth_handle->channel_num,
114 backend_info) == FALSE) {
115 g_object_unref( gmyth_uri );
119 if ( gmyth_livetv_setup (myth_handle->livetv, backend_info) == FALSE ) {
120 g_object_unref( gmyth_uri );
125 myth_handle->file_transfer = gmyth_livetv_create_file_transfer (myth_handle->livetv);
127 if (NULL == myth_handle->file_transfer) {
133 myth_handle->file_transfer = gmyth_file_transfer_new (backend_info);
135 /* Verifies if the file exists */
136 if (!gmyth_util_file_exists (backend_info, gmyth_uri_get_path (gmyth_uri))) {
137 g_object_unref (backend_info);
138 return GNOME_VFS_ERROR_NOT_FOUND;
141 /* sets the Playback monitor connection */
142 ret = gmyth_file_transfer_open ( myth_handle->file_transfer, gmyth_uri_get_path (gmyth_uri) );
144 } /* if - LiveTV or not? */
147 g_warning ("MythTV FileTransfer open error\n");
148 return GNOME_VFS_ERROR_NOT_OPEN;
151 g_object_unref (backend_info);
153 //if ( gmyth_uri != NULL )
154 // g_object_unref( gmyth_uri );
156 g_return_val_if_fail (myth_handle->file_transfer != NULL, GNOME_VFS_ERROR_NOT_OPEN);
158 myth_handle->content_size = myth_handle->file_transfer->filesize;
160 myth_handle->buffer = g_byte_array_sized_new (MYTHTV_BUFFER_SIZE);
161 myth_handle->buffer_remain = 0;
163 *method_handle = (GnomeVFSMethodHandle *) myth_handle;
168 static GnomeVFSResult
169 do_read (GnomeVFSMethod *method,
170 GnomeVFSMethodHandle *method_handle,
172 GnomeVFSFileSize num_bytes,
173 GnomeVFSFileSize *bytes_read,
174 GnomeVFSContext *context)
176 MythtvHandle *myth_handle = (MythtvHandle *) method_handle;
177 GnomeVFSFileSize bytes_to_read;
181 if (myth_handle->bytes_read >= myth_handle->content_size)
182 return GNOME_VFS_ERROR_EOF;
184 // fixme: change this to min math function
185 if (num_bytes > myth_handle->content_size - myth_handle->bytes_read)
186 bytes_to_read = myth_handle->content_size - myth_handle->bytes_read;
188 bytes_to_read = num_bytes;
190 /* Loop sending the Myth File Transfer request:
191 * Retry whilst authentication fails and we supply it. */
192 if ((myth_handle->buffer_remain = myth_handle->buffer->len) < MYTHTV_BUFFER_SIZE) {
193 //if ( bytes_to_read > myth_handle->buffer_remain ) {
194 GByteArray *tmp_buffer = g_byte_array_new();
196 gint len = gmyth_file_transfer_read (myth_handle->file_transfer,
197 tmp_buffer, MYTHTV_BUFFER_SIZE - myth_handle->buffer_remain, TRUE);
200 g_byte_array_free (tmp_buffer, TRUE);
201 g_warning ("Fail to read bytes");
202 return GNOME_VFS_ERROR_IO;
203 } /*else if (len == 0) {
204 g_byte_array_free (tmp_buffer, TRUE);
205 g_warning ("End of file probably achieved");
206 return GNOME_VFS_ERROR_EOF;
209 myth_handle->buffer = g_byte_array_append (myth_handle->buffer,
210 tmp_buffer->data, len);
212 myth_handle->buffer_remain += len;
214 g_byte_array_free (tmp_buffer, TRUE);
218 bytes_to_read = (bytes_to_read > myth_handle->buffer_remain) ? myth_handle->buffer_remain : bytes_to_read;
219 /* gets the first buffer_size bytes from the byte array buffer variable */
221 g_memmove (buffer, myth_handle->buffer->data, bytes_to_read);
223 myth_handle->bytes_read += bytes_to_read;
224 myth_handle->buffer_remain -= bytes_to_read;
226 /* flushs the newly buffer got from byte array */
227 myth_handle->buffer = g_byte_array_remove_range (myth_handle->buffer, 0, bytes_to_read);
228 *bytes_read = bytes_to_read;
233 static GnomeVFSResult
234 do_close (GnomeVFSMethod *method,
235 GnomeVFSMethodHandle *method_handle,
236 GnomeVFSContext *context)
239 MythtvHandle *myth_handle = (MythtvHandle *) method_handle;
241 if (myth_handle->file_transfer) {
242 gmyth_file_transfer_close (myth_handle->file_transfer);
243 g_object_unref (myth_handle->file_transfer);
244 myth_handle->file_transfer = NULL;
247 if (myth_handle->livetv) {
248 g_object_unref (myth_handle->livetv);
249 myth_handle->livetv = NULL;
252 if (myth_handle->buffer) {
253 g_byte_array_free (myth_handle->buffer, TRUE);
254 myth_handle->buffer = NULL;
257 g_free (myth_handle);
262 static GnomeVFSResult
263 do_get_file_info (GnomeVFSMethod *method,
265 GnomeVFSFileInfo *file_info,
266 GnomeVFSFileInfoOptions options,
267 GnomeVFSContext *context)
269 GMythFileTransfer *file_transfer = NULL;
270 GMythLiveTV *livetv = NULL;
271 GMythBackendInfo *backend_info = NULL;
272 GMythURI *gmyth_uri = NULL;
273 gboolean is_livetv = FALSE;
276 /* Creates and fills out the backend info structure */
277 backend_info = gmyth_backend_info_new_with_uri (
278 gnome_vfs_unescape_string( gnome_vfs_uri_to_string( uri, GNOME_VFS_URI_HIDE_NONE ), "" ) );
280 /* creates an instance of */
281 gmyth_uri = gmyth_uri_new_with_value(
282 gnome_vfs_unescape_string( gnome_vfs_uri_to_string( uri, GNOME_VFS_URI_HIDE_NONE ), "" ) );
284 is_livetv = gmyth_uri_is_livetv( gmyth_uri );
286 file_info->name = g_strdup (gmyth_uri_get_path (gmyth_uri)+1);
287 file_info->valid_fields = file_info->valid_fields
288 | GNOME_VFS_FILE_INFO_FIELDS_TYPE
289 | GNOME_VFS_FILE_INFO_FIELDS_MIME_TYPE
290 | GNOME_VFS_FILE_INFO_FIELDS_PERMISSIONS;
291 file_info->type = GNOME_VFS_FILE_TYPE_REGULAR;
292 /* fixme: get from file extension? */
293 file_info->mime_type = g_strdup ("video/x-nuv");
294 file_info->permissions =
295 GNOME_VFS_PERM_USER_READ |
296 GNOME_VFS_PERM_OTHER_READ |
297 GNOME_VFS_PERM_GROUP_READ;
299 /* Connect to the backend */
300 if ( gmyth_uri != NULL && is_livetv == TRUE ) {
301 livetv = gmyth_livetv_new ();
303 gint channel_num = gmyth_uri_get_channel_num( gmyth_uri );
305 if ( channel_num != -1 ) {
306 if (gmyth_livetv_channel_setup (livetv, channel_num,
307 backend_info) == FALSE) {
308 g_object_unref( gmyth_uri );
312 if ( gmyth_livetv_setup (livetv, backend_info) == FALSE ) {
313 g_object_unref( gmyth_uri );
318 file_transfer = gmyth_livetv_create_file_transfer (livetv);
320 if (NULL == file_transfer) {
326 file_transfer = gmyth_file_transfer_new (backend_info);
328 /* Verifies if the file exists */
329 if (!gmyth_util_file_exists (backend_info, gmyth_uri_get_path (gmyth_uri))) {
330 g_object_unref (backend_info);
331 return GNOME_VFS_ERROR_NOT_FOUND;
334 /* sets the Playback monitor connection */
335 ret = gmyth_file_transfer_open ( file_transfer, gmyth_uri_get_path (gmyth_uri) );
337 } /* if - LiveTV or not? */
341 g_warning ("MythTV FileTransfer open error\n");
342 return GNOME_VFS_ERROR_NOT_OPEN;
346 file_info->size = gmyth_file_transfer_get_filesize (file_transfer);
347 file_info->block_count = GNOME_VFS_FILE_INFO_FIELDS_BLOCK_COUNT;
348 file_info->io_block_size = GNOME_VFS_FILE_INFO_FIELDS_IO_BLOCK_SIZE;
350 g_object_unref (file_transfer);
351 g_object_unref (livetv);
352 g_object_unref (backend_info);
358 do_is_local (GnomeVFSMethod *method,
359 const GnomeVFSURI *uri)
364 static GnomeVFSMethod method = {
365 sizeof (GnomeVFSMethod),
396 vfs_module_init (const char *method_name, const char *args)
402 vfs_module_shutdown (GnomeVFSMethod *method)