[svn r289] Added pause recording calls to gmyth_livetv; removed sleep calls from the MythTV plug-in, put it back to livetv_setup function call.
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;
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;
77 gint wait_to_transfer = 0;
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 if (mode & GNOME_VFS_OPEN_WRITE) {
85 return GNOME_VFS_ERROR_NOT_PERMITTED;
88 if (gnome_vfs_uri_get_host_name (uri) == NULL) {
89 return GNOME_VFS_ERROR_INVALID_HOST_NAME;
92 /* Initialize mythtv handler*/
93 myth_handle->file_transfer = NULL;
94 myth_handle->livetv = NULL;
95 myth_handle->mythtv_version = MYTHTV_VERSION_DEFAULT;
96 myth_handle->bytes_read = 0;
97 myth_handle->content_size = -1;
99 /* Creates and fills out the backend info structure */
100 backend_info = gmyth_backend_info_new_with_uri (
101 gnome_vfs_unescape_string( gnome_vfs_uri_to_string( uri, GNOME_VFS_URI_HIDE_NONE ), "" ) );
103 /* creates an instance of */
104 gmyth_uri = gmyth_uri_new_with_value(
105 gnome_vfs_unescape_string( gnome_vfs_uri_to_string( uri, GNOME_VFS_URI_HIDE_NONE ), "" ) );
107 /* Connect to the backend */
108 if ( gmyth_uri != NULL && ( is_livetv = gmyth_uri_is_livetv( gmyth_uri ) ) == TRUE ) {
109 myth_handle->livetv = gmyth_livetv_new ();
111 myth_handle->channel_num = gmyth_uri_get_channel_num( gmyth_uri );
113 if ( myth_handle->channel_num != -1 ) {
114 if (gmyth_livetv_channel_setup (myth_handle->livetv, myth_handle->channel_num,
115 backend_info) == FALSE) {
116 g_object_unref( gmyth_uri );
120 if ( gmyth_livetv_setup (myth_handle->livetv, backend_info) == FALSE ) {
121 g_object_unref( gmyth_uri );
126 myth_handle->file_transfer = gmyth_livetv_create_file_transfer (myth_handle->livetv);
128 if (NULL == myth_handle->file_transfer) {
132 if ( gmyth_uri != NULL )
133 g_object_unref( gmyth_uri );
137 myth_handle->file_transfer = gmyth_file_transfer_new (backend_info);
139 /* Verifies if the file exists */
140 if (!gmyth_util_file_exists (backend_info, gnome_vfs_uri_get_path (uri))) {
141 g_object_unref (backend_info);
142 return GNOME_VFS_ERROR_NOT_FOUND;
145 /* sets the Playback monitor connection */
146 ret = gmyth_file_transfer_open ( myth_handle->file_transfer, gnome_vfs_uri_get_path (uri) );
148 } /* if - LiveTV or not? */
151 g_warning ("MythTV FileTransfer open error\n");
152 return GNOME_VFS_ERROR_NOT_OPEN;
155 if ( is_livetv == TRUE && ret == TRUE ) {
156 /* loop finished, set the max tries variable to zero again... */
157 wait_to_transfer = 0;
159 while ( wait_to_transfer++ < MYTHTV_TRANSFER_MAX_WAITS &&
160 (gmyth_livetv_is_recording (myth_handle->livetv) == FALSE))
163 /* IS_RECORDING again, just like the MythTV backend does... */
164 gmyth_livetv_is_recording (myth_handle->livetv);
166 sleep (4); /* FIXME: this is evil (tpm) */
169 g_object_unref (backend_info);
171 g_return_val_if_fail (myth_handle->file_transfer != NULL, GNOME_VFS_ERROR_NOT_OPEN);
173 myth_handle->content_size = myth_handle->file_transfer->filesize;
175 myth_handle->buffer = g_byte_array_sized_new (MYTHTV_BUFFER_SIZE);
176 myth_handle->buffer_remain = 0;
178 *method_handle = (GnomeVFSMethodHandle *) myth_handle;
183 static GnomeVFSResult
184 do_read (GnomeVFSMethod *method,
185 GnomeVFSMethodHandle *method_handle,
187 GnomeVFSFileSize num_bytes,
188 GnomeVFSFileSize *bytes_read,
189 GnomeVFSContext *context)
191 MythtvHandle *myth_handle = (MythtvHandle *) method_handle;
192 GnomeVFSFileSize bytes_to_read;
196 if (myth_handle->bytes_read >= myth_handle->content_size)
197 return GNOME_VFS_ERROR_EOF;
199 // fixme: change this to min math function
200 if (num_bytes > myth_handle->content_size - myth_handle->bytes_read)
201 bytes_to_read = myth_handle->content_size - myth_handle->bytes_read;
203 bytes_to_read = num_bytes;
205 /* Loop sending the Myth File Transfer request:
206 * Retry whilst authentication fails and we supply it. */
208 if ( bytes_to_read > myth_handle->buffer_remain ) {
209 GByteArray *tmp_buffer = g_byte_array_new();
211 gint len = gmyth_file_transfer_read (myth_handle->file_transfer,
212 tmp_buffer, MYTHTV_BUFFER_SIZE - myth_handle->buffer_remain, TRUE);
215 g_byte_array_free (tmp_buffer, TRUE);
216 g_warning ("Fail to read bytes");
217 return GNOME_VFS_ERROR_IO;
218 } else if (len == 0) {
219 g_byte_array_free (tmp_buffer, TRUE);
220 g_warning ("End of file probably achieved");
221 return GNOME_VFS_ERROR_EOF;
224 myth_handle->buffer = g_byte_array_append (myth_handle->buffer,
225 tmp_buffer->data, len);
227 myth_handle->buffer_remain += len;
229 g_byte_array_free (tmp_buffer, TRUE);
233 bytes_to_read = (bytes_to_read > myth_handle->buffer_remain) ? myth_handle->buffer_remain : bytes_to_read;
234 /* gets the first buffer_size bytes from the byte array buffer variable */
236 g_memmove (buffer, myth_handle->buffer->data, bytes_to_read);
238 myth_handle->bytes_read += bytes_to_read;
239 myth_handle->buffer_remain -= bytes_to_read;
241 /* flushs the newly buffer got from byte array */
242 myth_handle->buffer = g_byte_array_remove_range (myth_handle->buffer, 0, bytes_to_read);
243 *bytes_read = bytes_to_read;
248 static GnomeVFSResult
249 do_close (GnomeVFSMethod *method,
250 GnomeVFSMethodHandle *method_handle,
251 GnomeVFSContext *context)
254 MythtvHandle *myth_handle = (MythtvHandle *) method_handle;
256 if (myth_handle->file_transfer) {
257 gmyth_file_transfer_close (myth_handle->file_transfer);
258 g_object_unref (myth_handle->file_transfer);
259 myth_handle->file_transfer = NULL;
262 if (myth_handle->livetv) {
263 g_object_unref (myth_handle->livetv);
264 myth_handle->livetv = NULL;
267 if (myth_handle->buffer) {
268 g_byte_array_free (myth_handle->buffer, TRUE);
269 myth_handle->buffer = NULL;
272 g_free (myth_handle);
277 static GnomeVFSResult
278 do_get_file_info (GnomeVFSMethod *method,
280 GnomeVFSFileInfo *file_info,
281 GnomeVFSFileInfoOptions options,
282 GnomeVFSContext *context)
284 GMythFileTransfer *file_transfer = NULL;
285 GMythBackendInfo *backend_info = NULL;
287 file_info->name = g_strdup (gnome_vfs_uri_get_path (uri));
288 file_info->valid_fields = file_info->valid_fields
289 | GNOME_VFS_FILE_INFO_FIELDS_TYPE
290 | GNOME_VFS_FILE_INFO_FIELDS_MIME_TYPE
291 | GNOME_VFS_FILE_INFO_FIELDS_PERMISSIONS;
292 file_info->type = GNOME_VFS_FILE_TYPE_REGULAR;
293 // fixme: get from file extension?
294 file_info->mime_type = g_strdup ("video/x-nuv");
295 file_info->permissions =
296 GNOME_VFS_PERM_USER_READ |
297 GNOME_VFS_PERM_OTHER_READ |
298 GNOME_VFS_PERM_GROUP_READ;
300 backend_info = gmyth_backend_info_new_full (gnome_vfs_uri_get_host_name (uri),
301 gnome_vfs_uri_get_user_name (uri),
302 gnome_vfs_uri_get_password (uri),
304 gnome_vfs_uri_get_host_port (uri));
306 file_transfer = gmyth_file_transfer_new (backend_info);
307 if (gmyth_file_transfer_open (file_transfer, gnome_vfs_uri_get_path (uri)) == TRUE) {
308 file_info->size = gmyth_file_transfer_get_filesize (file_transfer);
309 file_info->block_count = GNOME_VFS_FILE_INFO_FIELDS_BLOCK_COUNT;
310 file_info->io_block_size = GNOME_VFS_FILE_INFO_FIELDS_IO_BLOCK_SIZE;
313 g_object_unref (file_transfer);
314 g_object_unref (backend_info);
319 do_is_local (GnomeVFSMethod *method,
320 const GnomeVFSURI *uri)
325 static GnomeVFSMethod method = {
326 sizeof (GnomeVFSMethod),
357 vfs_module_init (const char *method_name, const char *args)
363 vfs_module_shutdown (GnomeVFSMethod *method)