1.1 --- a/gmyth-stream/libgnomevfs2/modules/gmythstream-method.c Wed Aug 29 10:00:25 2007 +0100
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,363 +0,0 @@
1.4 -/*
1.5 - * @author Artur Duque de Souza <souza.artur@indt.org.br>
1.6 - *
1.7 - * This program is free software; you can redistribute it and/or modify
1.8 - * it under the terms of the GNU Lesser General Public License as published by
1.9 - * the Free Software Foundation; either version 2 of the License, or
1.10 - * (at your option) any later version.
1.11 - *
1.12 - * This program is distributed in the hope that it will be useful,
1.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.15 - * GNU General Public License for more details.
1.16 - *
1.17 - m * You should have received a copy of the GNU Lesser General Public License
1.18 - * along with this program; if not, write to the Free Software
1.19 - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
1.20 - */
1.21 -
1.22 -#ifdef HAVE_CONFIG_H
1.23 -#include <config.h>
1.24 -#endif
1.25 -
1.26 -#include <sys/socket.h> /* for socket(), connect(), send(), and
1.27 - * recv() */
1.28 -#include <arpa/inet.h> /* for sockaddr_in and inet_addr() */
1.29 -#include <stdlib.h> /* for atoi() and exit() */
1.30 -#include <string.h> /* for memset() */
1.31 -#include <unistd.h> /* for close() */
1.32 -#include <errno.h>
1.33 -
1.34 -#include <glib.h>
1.35 -#include <glib/gprintf.h>
1.36 -#include <glib/gstdio.h>
1.37 -#include <gmyth-stream-client.h>
1.38 -
1.39 -#include <libgnomevfs/gnome-vfs-module.h>
1.40 -#include <libgnomevfs/gnome-vfs-utils.h>
1.41 -
1.42 -#define BUFFER_SIZE 4096
1.43 -
1.44 -typedef struct {
1.45 - gint port;
1.46 - gchar *hostname;
1.47 -
1.48 - GMythStreamClient *stream;
1.49 - gint fd;
1.50 -} gmsHandle;
1.51 -
1.52 -typedef struct {
1.53 - gchar *file_name;
1.54 - gchar *mux;
1.55 - gchar *vcodec;
1.56 - guint vbitrate;
1.57 - gdouble fps;
1.58 - gchar *acodec;
1.59 - guint abitrate;
1.60 - guint width;
1.61 - guint height;
1.62 - guint port;
1.63 - gchar *opt;
1.64 -} UriArgs;
1.65 -
1.66 -
1.67 -static gmsHandle *gmsHandle_new(GnomeVFSURI * uri);
1.68 -
1.69 -static GnomeVFSResult
1.70 -do_open(GnomeVFSMethod * method,
1.71 - GnomeVFSMethodHandle ** method_handle,
1.72 - GnomeVFSURI * uri,
1.73 - GnomeVFSOpenMode mode, GnomeVFSContext * context);
1.74 -
1.75 -static GnomeVFSResult
1.76 -do_read(GnomeVFSMethod * method,
1.77 - GnomeVFSMethodHandle * method_handle,
1.78 - gpointer buffer,
1.79 - GnomeVFSFileSize bytes,
1.80 - GnomeVFSFileSize * bytes_read, GnomeVFSContext * context);
1.81 -
1.82 -static GnomeVFSResult
1.83 -do_close(GnomeVFSMethod * method,
1.84 - GnomeVFSMethodHandle * method_handle, GnomeVFSContext * context);
1.85 -
1.86 -static GnomeVFSResult
1.87 -do_get_file_info(GnomeVFSMethod * method,
1.88 - GnomeVFSURI * uri,
1.89 - GnomeVFSFileInfo * file_info,
1.90 - GnomeVFSFileInfoOptions options,
1.91 - GnomeVFSContext * context);
1.92 -
1.93 -
1.94 -static GnomeVFSResult
1.95 -do_get_file_info_from_handle(GnomeVFSMethod * method,
1.96 - GnomeVFSMethodHandle * method_handle,
1.97 - GnomeVFSFileInfo * file_info,
1.98 - GnomeVFSFileInfoOptions options,
1.99 - GnomeVFSContext * context);
1.100 -
1.101 -
1.102 -static gboolean
1.103 -do_is_local(GnomeVFSMethod * method, const GnomeVFSURI * uri);
1.104 -
1.105 -
1.106 -static gmsHandle *
1.107 -gmsHandle_new(GnomeVFSURI * uri)
1.108 -{
1.109 - gmsHandle *handler = (gmsHandle *) g_malloc0(sizeof(gmsHandle));
1.110 -
1.111 - handler->hostname = (gchar *) gnome_vfs_uri_get_host_name(uri);
1.112 - handler->port = gnome_vfs_uri_get_host_port(uri);
1.113 - handler->stream = gmyth_stream_client_new();
1.114 -
1.115 - return handler;
1.116 -}
1.117 -
1.118 -static GnomeVFSMethod method = {
1.119 - sizeof(GnomeVFSMethod),
1.120 - do_open, /* open */
1.121 - NULL, /* create */
1.122 - do_close, /* close */
1.123 - do_read, /* read */
1.124 - NULL, /* write */
1.125 - NULL, /* seek */
1.126 - NULL, /* tell */
1.127 - NULL, /* truncate_handle */
1.128 - NULL, /* open_directory */
1.129 - NULL, /* close_directory */
1.130 - NULL, /* read_directory */
1.131 - do_get_file_info, /* get_file_info */
1.132 - do_get_file_info_from_handle, /* get_file_info_from_handle */
1.133 - do_is_local, /* is_local */
1.134 - NULL, /* make_directory */
1.135 - NULL, /* remove_directory */
1.136 - NULL, /* move */
1.137 - NULL, /* unlink */
1.138 - NULL, /* check_same_fs */
1.139 - NULL, /* set_file_info */
1.140 - NULL, /* truncate */
1.141 - NULL, /* find_directory */
1.142 - NULL, /* create_symbolic_link */
1.143 - NULL, /* monitor_add */
1.144 - NULL, /* monitor_cancel */
1.145 - NULL /* file_control */
1.146 -};
1.147 -
1.148 -GnomeVFSMethod *
1.149 -vfs_module_init(const char *method_name, const char *args)
1.150 -{
1.151 - return &method;
1.152 -}
1.153 -
1.154 -void
1.155 -vfs_module_shutdown(GnomeVFSMethod * method)
1.156 -{
1.157 - return;
1.158 -}
1.159 -
1.160 -char *
1.161 -_parse_opt(char *opt)
1.162 -{
1.163 - char **list = g_strsplit(opt, "opt=", 2);
1.164 - char **opts = g_strsplit(list[1], "+", 32);
1.165 - char *value = "";
1.166 - char *aux;
1.167 - gint i = 0;
1.168 -
1.169 - for (aux = opts[0]; aux != NULL; aux = opts[++i])
1.170 - value = g_strdup_printf("%s %s", value, aux);
1.171 -
1.172 - g_free(aux);
1.173 - g_strfreev(list);
1.174 - g_strfreev(opts);
1.175 - return value;
1.176 -}
1.177 -
1.178 -static UriArgs *
1.179 -_uri_parse_args(const GnomeVFSURI * uri)
1.180 -{
1.181 - gchar *file = gnome_vfs_unescape_string(uri->text, NULL);
1.182 -
1.183 - gchar **list = g_strsplit(file, "\'", 3);
1.184 - gchar **prefix = g_strsplit_set(list[0], "/=", 3);
1.185 - gchar **lst = g_strsplit(list[2], "?", 0);
1.186 -
1.187 - UriArgs *info = g_new0(UriArgs, 1);
1.188 - gint i = 1;
1.189 -
1.190 - gchar **prop = NULL;
1.191 - prop = g_strsplit_set(lst[0], "/=", 3);
1.192 -
1.193 - info->file_name = g_strdup_printf("%s://%s", prefix[1], list[1]);
1.194 -
1.195 - // g_debug("FILENAME: [%s]", info->file_name);
1.196 -
1.197 - g_strfreev(prop);
1.198 -
1.199 - gchar *walk;
1.200 - for (walk = lst[1]; walk != NULL; walk = lst[++i]) {
1.201 - prop = g_strsplit(walk, "=", 2);
1.202 -
1.203 - if (g_strv_length(prop) == 2) {
1.204 - if (strcmp(prop[0], "mux") == 0) {
1.205 - info->mux = g_strdup(prop[1]);
1.206 - } else if (strcmp(prop[0], "vcodec") == 0) {
1.207 - info->vcodec = g_strdup(prop[1]);
1.208 - } else if (strcmp(prop[0], "vbitrate") == 0) {
1.209 - info->vbitrate = atoi(prop[1]);
1.210 - } else if (strcmp(prop[0], "fps") == 0) {
1.211 - info->fps = g_strtod(prop[1], NULL);
1.212 - } else if (strcmp(prop[0], "acodec") == 0) {
1.213 - info->acodec = g_strdup(prop[1]);
1.214 - } else if (strcmp(prop[0], "abitrate") == 0) {
1.215 - info->abitrate = atoi(prop[1]);
1.216 - } else if (strcmp(prop[0], "width") == 0) {
1.217 - info->width = atoi(prop[1]);
1.218 - } else if (strcmp(prop[0], "height") == 0) {
1.219 - info->height = atoi(prop[1]);
1.220 - } else if (strcmp(prop[0], "opt") == 0) {
1.221 - info->opt = g_strdup(_parse_opt(walk));
1.222 - }
1.223 - }
1.224 - g_strfreev(prop);
1.225 - }
1.226 -
1.227 - g_free(file);
1.228 - g_strfreev(list);
1.229 - g_strfreev(prefix);
1.230 - g_strfreev(lst);
1.231 - return info;
1.232 -}
1.233 -
1.234 -static GnomeVFSResult
1.235 -do_open(GnomeVFSMethod * method,
1.236 - GnomeVFSMethodHandle ** method_handle,
1.237 - GnomeVFSURI * uri,
1.238 - GnomeVFSOpenMode mode, GnomeVFSContext * context)
1.239 -{
1.240 - gmsHandle *handle = gmsHandle_new(uri);
1.241 - UriArgs *args;
1.242 -
1.243 - if (!gmyth_stream_client_connect(handle->stream,
1.244 - gnome_vfs_uri_get_host_name(uri),
1.245 - gnome_vfs_uri_get_host_port(uri))) {
1.246 -
1.247 - return GNOME_VFS_ERROR_HOST_NOT_FOUND;
1.248 - }
1.249 -
1.250 - args = _uri_parse_args(uri);
1.251 -
1.252 - gint ret = gmyth_stream_client_open_stream(handle->stream,
1.253 - args->file_name,
1.254 - args->mux,
1.255 - args->vcodec,
1.256 - args->vbitrate,
1.257 - args->fps,
1.258 - args->acodec,
1.259 - args->abitrate,
1.260 - args->width,
1.261 - args->height,
1.262 - args->opt);
1.263 -
1.264 - g_free(args);
1.265 -
1.266 - if (ret == -1) {
1.267 - gmyth_stream_client_disconnect(handle->stream);
1.268 - return GNOME_VFS_ERROR_HOST_NOT_FOUND;
1.269 - }
1.270 -
1.271 - handle->fd = gmyth_stream_client_play_stream(handle->stream);
1.272 -
1.273 - if (handle->fd == -1) {
1.274 - gmyth_stream_client_disconnect(handle->stream);
1.275 - return GNOME_VFS_ERROR_HOST_NOT_FOUND;
1.276 - }
1.277 -
1.278 - *method_handle = (GnomeVFSMethodHandle *) handle;
1.279 - return GNOME_VFS_OK;
1.280 -}
1.281 -
1.282 -static GnomeVFSResult
1.283 -do_read(GnomeVFSMethod * method,
1.284 - GnomeVFSMethodHandle * method_handle,
1.285 - gpointer buffer,
1.286 - GnomeVFSFileSize bytes,
1.287 - GnomeVFSFileSize * bytes_read, GnomeVFSContext * context)
1.288 -{
1.289 -
1.290 - gint64 total_read = 0;
1.291 - gmsHandle *handle = (gmsHandle *) method_handle;
1.292 -
1.293 - // g_debug("waiting something");
1.294 -
1.295 - total_read = recv(handle->fd, buffer, BUFFER_SIZE, 0);
1.296 - // g_debug("COULD READ: %d bytes", total_read);
1.297 - *bytes_read = (GnomeVFSFileSize) total_read;
1.298 -
1.299 - // if (total_read < 0) g_debug("ERROR!!!!!!!!!!!!!!!!");
1.300 -
1.301 - if (total_read < 0)
1.302 - return GNOME_VFS_ERROR_INTERNAL;
1.303 - else
1.304 - return GNOME_VFS_OK;
1.305 -
1.306 -}
1.307 -
1.308 -static GnomeVFSResult
1.309 -do_close(GnomeVFSMethod * method,
1.310 - GnomeVFSMethodHandle * method_handle, GnomeVFSContext * context)
1.311 -{
1.312 - gmsHandle *handle = (gmsHandle *) method_handle;
1.313 -
1.314 - g_debug("close close close");
1.315 -
1.316 - gmyth_stream_client_close_stream(handle->stream);
1.317 - gmyth_stream_client_disconnect(handle->stream);
1.318 -
1.319 - g_free(handle);
1.320 - return GNOME_VFS_OK;
1.321 -}
1.322 -
1.323 -
1.324 -static GnomeVFSResult
1.325 -do_get_file_info(GnomeVFSMethod * method,
1.326 - GnomeVFSURI * uri,
1.327 - GnomeVFSFileInfo * file_info,
1.328 - GnomeVFSFileInfoOptions options,
1.329 - GnomeVFSContext * context)
1.330 -{
1.331 - file_info->valid_fields = GNOME_VFS_FILE_INFO_FIELDS_TYPE |
1.332 - GNOME_VFS_FILE_INFO_FIELDS_PERMISSIONS;
1.333 -
1.334 - file_info->type = GNOME_VFS_FILE_TYPE_SOCKET;
1.335 -
1.336 - file_info->permissions = GNOME_VFS_PERM_USER_READ |
1.337 - GNOME_VFS_PERM_OTHER_READ | GNOME_VFS_PERM_GROUP_READ;
1.338 -
1.339 - return GNOME_VFS_OK;
1.340 -}
1.341 -
1.342 -
1.343 -static GnomeVFSResult
1.344 -do_get_file_info_from_handle(GnomeVFSMethod * method,
1.345 - GnomeVFSMethodHandle * method_handle,
1.346 - GnomeVFSFileInfo * file_info,
1.347 - GnomeVFSFileInfoOptions options,
1.348 - GnomeVFSContext * context)
1.349 -{
1.350 - file_info->valid_fields = GNOME_VFS_FILE_INFO_FIELDS_TYPE |
1.351 - GNOME_VFS_FILE_INFO_FIELDS_PERMISSIONS;
1.352 -
1.353 - file_info->type = GNOME_VFS_FILE_TYPE_SOCKET;
1.354 -
1.355 - file_info->permissions = GNOME_VFS_PERM_USER_READ |
1.356 - GNOME_VFS_PERM_OTHER_READ | GNOME_VFS_PERM_GROUP_READ;
1.357 -
1.358 - return GNOME_VFS_OK;
1.359 -}
1.360 -
1.361 -
1.362 -static gboolean
1.363 -do_is_local(GnomeVFSMethod * method, const GnomeVFSURI * uri)
1.364 -{
1.365 - return FALSE;
1.366 -}