# 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 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 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