# HG changeset patch # User rosfran # Date 1176128437 -3600 # Node ID 12a61040588d80fced85124528be250644d424fc # Parent 5f15d749c024f58011d6a5d842a628ac8ab73466 [svn r518] Newly added GMythFileLocal, to deal with local stream files. diff -r 5f15d749c024 -r 12a61040588d gmyth/src/gmyth_file_local.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/gmyth/src/gmyth_file_local.c Mon Apr 09 15:20:37 2007 +0100 @@ -0,0 +1,429 @@ +/** + * GMyth Library + * + * @file_local gmyth/gmyth_file_local.c + * + * @brief

GMythFileLocal deals with the file_local streaming media remote/local + * transfering to the MythTV frontend. + * + * Copyright (C) 2006 INdT - Instituto Nokia de Tecnologia. + * @author Rosfran Lins Borges + * + *//* + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "gmyth_file_local.h" +#include "gmyth_recorder.h" +#include "gmyth_util.h" +#include "gmyth_socket.h" +#include "gmyth_stringlist.h" +#include "gmyth_debug.h" +#include "gmyth_uri.h" +#include "gmyth_marshal.h" + +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +#define GMYTH_FILE_LOCAL_GET_PRIVATE(obj) \ + (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GMYTH_FILE_LOCAL_TYPE, GMythFileLocalPrivate)) + +struct _GMythFileLocalPrivate { + + gboolean disposed; + + GMutex *mutex; + + gint fd; + + GIOChannel *file_io; + +}; + +static void gmyth_file_local_class_init (GMythFileLocalClass *klass); +static void gmyth_file_local_init (GMythFileLocal *object); +static void gmyth_file_local_dispose (GObject *object); +static void gmyth_file_local_finalize (GObject *object); + +static gboolean _control_acquire_context( GMythFileTransfer *transfer, gboolean do_wait ); + +static gboolean _control_release_context( GMythFileTransfer *transfer ); + +G_DEFINE_TYPE(GMythFileLocal, gmyth_file_local, GMYTH_FILE_TYPE) + +static void +gmyth_file_local_class_init (GMythFileLocalClass *klass) +{ + GObjectClass *gobject_class; + GMythFileLocalClass *gtransfer_class; + + gobject_class = (GObjectClass *) klass; + gtransfer_class = (GMythFileLocalClass *) gobject_class; + + gobject_class->dispose = gmyth_file_local_dispose; + gobject_class->finalize = gmyth_file_local_finalize; + + g_type_class_add_private (gobject_class, sizeof (GMythFileLocalPrivate)); + +} + +static void +gmyth_file_local_init (GMythFileLocal *file_local) +{ + GMythFileLocalPrivate *priv; + g_return_if_fail( file_local != NULL ); + + priv = GMYTH_FILE_LOCAL_GET_PRIVATE(file_local); + + priv->mutex = g_mutex_new(); +} + +static void +gmyth_file_local_dispose (GObject *object) +{ + GMythFileLocalPrivate *priv; + GMythFileLocal *file_local = GMYTH_FILE_LOCAL(object); + + g_return_if_fail( file_local != NULL ); + + priv = GMYTH_FILE_LOCAL_GET_PRIVATE(file_local); + + if (priv->disposed) { + /* If dispose did already run, return. */ + return; + } + + /* Make sure dispose does not run twice. */ + priv->disposed = TRUE; + + if (priv->mutex != NULL ) { + g_mutex_free (priv->mutex ); + priv->mutex = NULL; + } + + if (priv->file_io != NULL ) { + g_io_channel_unref (priv->file_io ); + priv->file_io = NULL; + } + + G_OBJECT_CLASS (gmyth_file_local_parent_class)->dispose (object); +} + +static void +gmyth_file_local_finalize (GObject *object) +{ + g_signal_handlers_destroy (object); + + G_OBJECT_CLASS (gmyth_file_local_parent_class)->finalize (object); +} + +/** + * Creates a new instance of GMythFileLocal. + * + * @param backend_info The BackendInfo instance, with all the MythTV network + * configuration data. + * + * @return a new instance of the File Transfer. + */ +GMythFileLocal* +gmyth_file_local_new (GMythBackendInfo *backend_info) +{ + GMythFileLocal *file_local = GMYTH_FILE_LOCAL (g_object_new (GMYTH_FILE_TYPE, backend_info, NULL)); + + return file_local; +} + +/** + * Creates a new instance of GMythFileLocal. + * + * @param uri_str The URI poiting to the MythTV backend server. + * + * @return a new instance of the File Transfer. + */ +GMythFileLocal* +gmyth_file_local_new_with_uri (const gchar* uri_str) +{ + GMythFileLocal *file_local = GMYTH_FILE_LOCAL (g_object_new (GMYTH_FILE_TYPE, uri_str, NULL)); + + return file_local; +} + +gchar* +gmyth_file_local_get_file_name (GMythFileLocal *file_local) +{ + return gmyth_file_get_filename( g_type_peek_parent( file_local ) ); +} + +/** + * Open a File in order to get a local file. + * + * @param file_local The actual File Transfer instance. + * + * @return true, if the connection opening had been done successfully. + */ +gboolean +gmyth_file_local_open (GMythFileLocal *file_local) +{ + gboolean ret = TRUE; + GMythFileLocalPrivate *priv; + gchar *file_name_uri = NULL; + + g_return_val_if_fail (file_local != NULL, FALSE); + + priv = GMYTH_FILE_LOCAL_GET_PRIVATE (file_local); + file_name_uri = gmyth_file_local_get_file_name(file_local); + + if ( file_name_uri != NULL ) + { + GMythURI *uri = gmyth_uri_new_with_value(file_name_uri); + gmyth_debug( "GMythURI path segment = %s", gmyth_uri_get_path(uri) ); + priv->file_io = g_io_channel_new_file( g_strdup( gmyth_uri_get_path(uri) ), "r+", NULL ); + g_object_unref( uri ); + g_free( file_name_uri ); + } + + if ( priv->file_io < 0 ) + ret = FALSE; + + return ret; +} + +/** + * Closes a remote File Transfer connection. + * + * @param file_local The actual File Transfer instance. + */ +void +gmyth_file_local_close (GMythFileLocal *file_local ) +{ + GMythFileLocalPrivate *priv; + + g_return_if_fail (file_local != NULL); + +} + +/** + * Acquire access to a local file read/write pointer. + * + * @param transfer The actual File Local instance. + * @param do_wait Waits or not on a GCond, when trying to read from the remote socket. + * + * @return true, if the acquire had been got. + */ +static gboolean +_control_acquire_context( GMythFileLocal *file_local, gboolean do_wait ) +{ + gboolean ret = TRUE; + GMythFileLocalPrivate *priv; + + g_return_val_if_fail (file_local != NULL, FALSE); + priv = GMYTH_FILE_LOCAL_GET_PRIVATE (transfer); + + g_mutex_lock (priv->mutex); + return ret; +} + +/** + * Release access to a local file read/write pointer. + * + * @param transfer The actual File Transfer instance. + * + * @return true, if the local file read/write permissions had been releaseds. + */ +static gboolean +_control_release_context( GMythFileLocal *file_local ) +{ + gboolean ret = TRUE; + GMythFileLocalPrivate *priv; + + g_return_val_if_fail (file_local != NULL, FALSE); + priv = GMYTH_FILE_LOCAL_GET_PRIVATE (file_local); + + g_mutex_unlock (priv->mutex ); + + return ret; +} + +/** + * Reads a block from a remote file. + * + * @param transfer The actual File Transfer instance. + * @param data A GByteArray instance, where all the binary data representing + * the remote file will be stored. + * @param size The block size, in bytes, to be requested from a remote file. + * @param read_unlimited Tells the backend to read indefinitely (LiveTV), or only + * gets the actual size + * + * @return The actual block size (in bytes) returned by REQUEST_BLOCK message, + * or the error code. + */ +GMythFileReadResult +gmyth_file_local_read(GMythFileLocal *file_local, GByteArray *data, gint size, gboolean read_unlimited) +{ + gsize bytes_read = 0; + gint64 total_read = 0; + GMythFileReadResult retval = GMYTH_FILE_READ_OK; + GMythFileLocalPrivate *priv; + + GError *error = NULL; + + GIOCondition io_cond; + GIOStatus io_status = G_IO_STATUS_NORMAL; + + gboolean ret = TRUE; + + g_return_val_if_fail (file_local != NULL, FALSE); + g_return_val_if_fail (data != NULL, GMYTH_FILE_READ_ERROR); + + priv = GMYTH_FILE_LOCAL_GET_PRIVATE (file_local); + + io_status = g_io_channel_set_encoding( priv->file_io, NULL, &error ); + if ( io_status == G_IO_STATUS_NORMAL ) + gmyth_debug ( "Setting encoding to binary file data stream.\n" ); + + io_cond = g_io_channel_get_buffer_condition( priv->file_io ); + + _control_acquire_context (file_local, TRUE ); + + if (size > 0) { + gchar *data_buffer = g_new0 (gchar, size); + io_status = g_io_channel_read_chars (priv->file_io, + data_buffer, + (gsize) size, + &bytes_read, + &error); + + if (io_status != G_IO_STATUS_NORMAL) { + gmyth_debug ("Error on io_channel"); + g_free (data_buffer); + retval = GMYTH_FILE_READ_ERROR; + goto error; + } + + /* append new data to the increasing byte array */ + data = g_byte_array_append (data, (const guint8*)data_buffer, bytes_read); + total_read += bytes_read; + + if (!read_unlimited && ( gmyth_file_local_get_file_size(file_local) > 0) && + (gmyth_file_local_get_offset(file_local) == gmyth_file_local_get_file_size(file_local))) { + retval = GMYTH_FILE_TRANSFER_READ_EOF; + goto error; + } + + g_free (data_buffer); + } else { + retval = GMYTH_FILE_TRANSFER_READ_ERROR; + } + +error: + _control_release_context (file_local); + + if ( error != NULL ) { + gmyth_debug ("Cleaning-up ERROR: [msg = %s, code = %d]\n", error->message, + error->code); + g_error_free (error); + } + + if ( total_read > 0 ) + gmyth_file_local_set_offset(file_local, ( gmyth_file_local_get_offset(file_local) + total_read ) ); + + return retval; +} + +/** + * Gets the actual file_local size of the binary content. + * + * @param file_local The actual File Transfer instance. + * + * @return The actual file_local size in bytes. + */ +guint64 +gmyth_file_local_get_filesize (GMythFileLocal *file_local) +{ + GMythFilePrivate *priv; + + g_return_val_if_fail (file_local != NULL, 0); + + priv = GMYTH_FILE_GET_PRIVATE ( GMYTH_FILE(g_object_peek_parent(file_local))); + return priv->filesize; +} + +/** + * Sets the actual file_local size. + * + * @param file_local The actual File Transfer instance. + * @param filesize The actual File Transfer size, in bytes. + */ +void +gmyth_file_local_set_filesize (GMythFileLocal *file_local, guint64 filesize) +{ + GMythFilePrivate *priv; + + g_return_val_if_fail (file_local != NULL, 0); + + priv = GMYTH_FILE_GET_PRIVATE (GMYTH_FILE(g_object_peek_parent(file_local))); + + priv->filesize = filesize; +} + +/** + * Gets the actual file offset of the binary content. + * + * @param file_local The actual File Transfer instance. + * + * @return The actual file offset in bytes. + */ +gint64 +gmyth_file_local_get_offset (GMythFileLocal *file_local) +{ + GMythFilePrivate *priv; + + g_return_val_if_fail (file_local != NULL, 0); + + priv = GMYTH_FILE_GET_PRIVATE ( GMYTH_FILE(g_object_peek_parent(file_local))); + return priv->offset; +} + +/** + * Sets the actual file offset. + * + * @param file_local The actual File Local instance. + * @param offset The actual File Local offset, in bytes. + */ +void +gmyth_file_local_set_offset (GMythFileLocal *file_local, gint64 offset) +{ + GMythFilePrivate *priv; + + g_return_val_if_fail (file_local != NULL, 0); + + priv = GMYTH_FILE_GET_PRIVATE (GMYTH_FILE(g_object_peek_parent(file_local))); + + priv->offset = offset; +} + diff -r 5f15d749c024 -r 12a61040588d gmyth/src/gmyth_file_local.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/gmyth/src/gmyth_file_local.h Mon Apr 09 15:20:37 2007 +0100 @@ -0,0 +1,90 @@ +/** + * GMyth Library + * + * @file gmyth/gmyth_file_local_local.h + * + * @brief

GMythFileLocal is the parent GMythFile that deals with the file streaming + * media local transfering from the MythTV backend. + * + * Copyright (C) 2006 INdT - Instituto Nokia de Tecnologia. + * @author Rosfran Lins Borges + * + *//* + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef __GMYTH_FILE_LOCAL_H__ +#define __GMYTH_FILE_LOCAL_H__ + +#include +#include + +#include "gmyth_file.h" +#include "gmyth_uri.h" +#include "gmyth_backendinfo.h" + +#include +#include +#include +#include +#include +#include + +G_BEGIN_DECLS + +#define GMYTH_FILE_LOCAL_TYPE (gmyth_file_local_get_type ()) +#define GMYTH_FILE_LOCAL_(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GMYTH_FILE_LOCAL_TYPE, GMythFileLocal)) +#define GMYTH_FILE_LOCAL_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GMYTH_FILE_LOCAL_TYPE, GMythFileLocalClass)) +#define IS_GMYTH_FILE_LOCAL_(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GMYTH_FILE_LOCAL_TYPE)) +#define IS_GMYTH_FILE_LOCAL_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GMYTH_FILE_LOCAL_TYPE)) +#define GMYTH_FILE_LOCAL_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GMYTH_FILE_LOCAL_TYPE, GMythFileLocalClass)) + +typedef struct _GMythFileLocal GMythFileLocal; +typedef struct _GMythFileLocalClass GMythFileLocalClass; +typedef struct _GMythFileLocalPrivate GMythFileLocalPrivate; + +struct _GMythFileLocal +{ + GMythFile parent; +}; + +struct _GMythFileLocalClass +{ + GMythFileClass parent_class; +}; + + +GType gmyth_file_local_get_type (void); +GMythFileLocal* gmyth_file_local_new (GMythBackendInfo *backend_info); +gchar* gmyth_file_local_get_file_name (GMythFileLocal *file_local); +gboolean gmyth_file_local_open (GMythFileLocal *file_local, + const gchar* filename); +void gmyth_file_local_close (GMythFileLocal *file_local); +gboolean gmyth_file_local_is_open (GMythFileLocal *file_local); +GMythFileLocalReadResult + gmyth_file_local_read (GMythFileLocal *file_local, + GByteArray *data, + gint size, + gboolean read_unlimited); +guint64 gmyth_file_local_get_filesize (GMythFileLocal *file_local); +void gmyth_file_local_set_filesize (GMythFileLocal *file, guint64 filesize); + +gint64 gmyth_file_local_get_offset (GMythFileLocal *file_local); +void gmyth_file_local_set_offset (GMythFileLocal *file_local, gint64 offset); + +G_END_DECLS + +#endif /* __GMYTH_FILE_LOCAL_H__ */