1.1 --- a/gmyth-stream/client/src/gmyth-stream-client.c Thu May 17 20:06:15 2007 +0100
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,252 +0,0 @@
1.4 -#ifdef HAVE_CONFIG_H
1.5 -#include "config.h"
1.6 -#endif
1.7 -
1.8 -#include <sys/types.h>
1.9 -#include <sys/socket.h>
1.10 -#include <arpa/inet.h>
1.11 -#include <netdb.h>
1.12 -#include <netinet/in.h>
1.13 -#include <unistd.h>
1.14 -#include <glib.h>
1.15 -#include <glib/gprintf.h>
1.16 -#include <string.h>
1.17 -#include <stdlib.h>
1.18 -
1.19 -#include "gmyth-stream-client.h"
1.20 -
1.21 -#define BUFFER_SIZE 1024
1.22 -
1.23 -#define GMYTH_STREAM_CLIENT_GET_PRIVATE(obj) \
1.24 - (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GMYTH_TYPE_STREAM_CLIENT,\
1.25 - GMythStreamClientPrivate))
1.26 -
1.27 -
1.28 -typedef struct _sock _socket;
1.29 -struct _sock
1.30 -{
1.31 - gint fd;
1.32 - struct sockaddr_in addr;
1.33 -};
1.34 -
1.35 -
1.36 -typedef struct _GMythStreamClientPrivate GMythStreamClientPrivate;
1.37 -struct _GMythStreamClientPrivate
1.38 -{
1.39 - const gchar *host;
1.40 - _socket* sock;
1.41 - _socket* sock_stream;
1.42 - gboolean connected;
1.43 -};
1.44 -
1.45 -
1.46 -static void gmyth_stream_client_class_init (GMythStreamClientClass *klass);
1.47 -static void gmyth_stream_client_init (GMythStreamClient *object);
1.48 -static void gmyth_stream_client_dispose (GObject *object);
1.49 -static void gmyth_stream_client_finalize (GObject *object);
1.50 -
1.51 -
1.52 -G_DEFINE_TYPE(GMythStreamClient, gmyth_stream_client, G_TYPE_OBJECT)
1.53 -
1.54 -
1.55 -static _socket*
1.56 -create_socket (const gchar* hostname, gint port)
1.57 -{
1.58 - _socket* sock = (_socket*)g_malloc(sizeof(_socket));
1.59 -
1.60 - sock->fd = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
1.61 - if (sock->fd == -1) {
1.62 - g_debug ("Fail to create sock");
1.63 - g_free(sock);
1.64 - return NULL;
1.65 - }
1.66 -
1.67 - sock->addr.sin_family = AF_INET;
1.68 - sock->addr.sin_addr.s_addr = inet_addr(hostname);
1.69 - sock->addr.sin_port = htons(port);
1.70 -
1.71 - if (connect (sock->fd, (struct sockaddr *) &(sock->addr), \
1.72 - sizeof (sock->addr)) == -1) {
1.73 - g_debug ("Fail to connect with server");
1.74 - g_free(sock);
1.75 - return NULL;
1.76 - }
1.77 -
1.78 - return sock;
1.79 -}
1.80 -
1.81 -static gint
1.82 -read_message (int socket)
1.83 -{
1.84 - gint64 total_read = 0;
1.85 - gint result = -1;
1.86 - gchar buffer[BUFFER_SIZE];
1.87 - gchar** response;
1.88 -
1.89 - total_read = recv(socket, buffer, BUFFER_SIZE, 0);
1.90 -
1.91 - if (total_read > 0) {
1.92 - response = g_strsplit_set(buffer, " +\n", 8);
1.93 -
1.94 - if ( g_ascii_strcasecmp(response[0], "OK") == 0 ) {
1.95 -
1.96 - int payload = atoi(response[1]);
1.97 -
1.98 - if (payload == 0)
1.99 - result = 0;
1.100 -
1.101 - else if (payload == 1) {
1.102 - total_read = recv(socket, buffer, BUFFER_SIZE, 0);
1.103 -
1.104 - response = g_strsplit_set(buffer, "+\n", 8);
1.105 - result = atoi(response[1]);
1.106 - }
1.107 -
1.108 - }
1.109 -
1.110 - g_strfreev(response);
1.111 - }
1.112 -
1.113 - return result;
1.114 -}
1.115 -
1.116 -
1.117 -static void
1.118 -gmyth_stream_client_class_init (GMythStreamClientClass *klass)
1.119 -{
1.120 - GObjectClass *gobject_class;
1.121 - gobject_class = (GObjectClass *) klass;
1.122 -
1.123 - g_type_class_add_private (klass, sizeof (GMythStreamClientPrivate));
1.124 - gobject_class->dispose = gmyth_stream_client_dispose;
1.125 - gobject_class->finalize = gmyth_stream_client_finalize;
1.126 -}
1.127 -
1.128 -static void
1.129 -gmyth_stream_client_init (GMythStreamClient *self)
1.130 -{
1.131 - GMythStreamClientPrivate *priv = GMYTH_STREAM_CLIENT_GET_PRIVATE (self);
1.132 - priv->sock = NULL;
1.133 - priv->sock_stream = NULL;
1.134 -}
1.135 -
1.136 -static void
1.137 -gmyth_stream_client_dispose (GObject *object)
1.138 -{
1.139 - gmyth_stream_client_disconnect (GMYTH_STREAM_CLIENT (object));
1.140 -}
1.141 -
1.142 -
1.143 -static void
1.144 -gmyth_stream_client_finalize (GObject *object)
1.145 -{
1.146 -}
1.147 -
1.148 -GMythStreamClient*
1.149 -gmyth_stream_client_new ()
1.150 -{
1.151 - return GMYTH_STREAM_CLIENT (g_object_new (GMYTH_TYPE_STREAM_CLIENT, NULL));
1.152 -}
1.153 -
1.154 -gboolean
1.155 -gmyth_stream_client_connect (GMythStreamClient *self, const gchar *server, guint port)
1.156 -{
1.157 - GMythStreamClientPrivate *priv = GMYTH_STREAM_CLIENT_GET_PRIVATE (self);
1.158 - g_return_val_if_fail (priv->connected == FALSE, TRUE);
1.159 -
1.160 - priv->host = server;
1.161 - priv->sock = create_socket (server, port);
1.162 - if (priv->sock == NULL) return FALSE;
1.163 -
1.164 - priv->connected = TRUE;
1.165 - return TRUE;
1.166 -}
1.167 -
1.168 -void
1.169 -gmyth_stream_client_disconnect (GMythStreamClient *self)
1.170 -{
1.171 - GMythStreamClientPrivate *priv = GMYTH_STREAM_CLIENT_GET_PRIVATE (self);
1.172 -
1.173 - g_return_if_fail (priv->connected == TRUE);
1.174 -
1.175 - close (priv->sock->fd);
1.176 - //shutdown (priv->sock->fd, SHUT_RDWR);
1.177 - g_free(priv->sock);
1.178 - priv->sock = NULL;
1.179 - priv->connected = FALSE;
1.180 -}
1.181 -
1.182 -gint
1.183 -gmyth_stream_client_open_stream (GMythStreamClient *self,
1.184 - const gchar* file_name,
1.185 - const gchar* mux,
1.186 - const gchar* vcodec,
1.187 - guint vbitrate,
1.188 - gdouble fps,
1.189 - const gchar* acodec,
1.190 - guint abitrate,
1.191 - guint width, guint height,
1.192 - const gchar* opt)
1.193 -{
1.194 - gchar *cmd;
1.195 - GMythStreamClientPrivate *priv = GMYTH_STREAM_CLIENT_GET_PRIVATE (self);
1.196 -
1.197 - g_return_val_if_fail (priv->connected == TRUE, FALSE);
1.198 - g_return_val_if_fail (file_name != NULL, FALSE);
1.199 -
1.200 - cmd = g_strdup_printf ("SETUP %s %s %s %d %f %s %d %d %d %s\n",
1.201 - file_name,
1.202 - (mux == NULL ? "X" : mux),
1.203 - (vcodec == NULL ? "X" : vcodec),
1.204 - vbitrate,
1.205 - fps,
1.206 - (acodec == NULL ? "X" : acodec),
1.207 - abitrate,
1.208 - width, height,
1.209 - (opt == NULL ? "X" : opt) );
1.210 -
1.211 - if (send (priv->sock->fd, cmd, strlen (cmd), MSG_CONFIRM) == -1) {
1.212 - g_free (cmd);
1.213 - return -1;
1.214 - }
1.215 - g_free (cmd);
1.216 -
1.217 - read_message(priv->sock->fd);
1.218 -
1.219 - return 0;
1.220 -}
1.221 -
1.222 -
1.223 -gint
1.224 -gmyth_stream_client_play_stream (GMythStreamClient *self)
1.225 -{
1.226 - GMythStreamClientPrivate *priv = GMYTH_STREAM_CLIENT_GET_PRIVATE (self);
1.227 -
1.228 - g_return_val_if_fail (priv->connected == TRUE, FALSE);
1.229 -
1.230 - if (send (priv->sock->fd, "PLAY\n", 5, 0) == -1) {
1.231 - return -1;
1.232 - }
1.233 -
1.234 - gint port = read_message(priv->sock->fd);
1.235 - priv->sock_stream = create_socket(priv->host, port);
1.236 -
1.237 - return priv->sock_stream->fd;
1.238 -}
1.239 -
1.240 -void
1.241 -gmyth_stream_client_close_stream (GMythStreamClient *self)
1.242 -{
1.243 - GMythStreamClientPrivate *priv = GMYTH_STREAM_CLIENT_GET_PRIVATE (self);
1.244 - g_return_if_fail (priv->connected == TRUE);
1.245 -
1.246 - if (send (priv->sock->fd, "STOP\n", 5, 0) == -1) {
1.247 - return;
1.248 - }
1.249 -
1.250 - read_message(priv->sock->fd);
1.251 -
1.252 - close(priv->sock_stream->fd);
1.253 - g_free(priv->sock_stream);
1.254 - priv->sock_stream = NULL;
1.255 -}