1.1 --- a/gmyth-stream/client/src/gmyth-stream-client.c Tue Apr 03 19:42:17 2007 +0100
1.2 +++ b/gmyth-stream/client/src/gmyth-stream-client.c Wed Apr 11 19:31:15 2007 +0100
1.3 @@ -11,6 +11,7 @@
1.4 #include <glib/gprintf.h>
1.5 #include <string.h>
1.6
1.7 +
1.8 #include "gmyth-stream-client.h"
1.9
1.10
1.11 @@ -23,8 +24,10 @@
1.12 struct _GMythStreamClientPrivate
1.13 {
1.14 GList *streams;
1.15 + struct hostent *host;
1.16 gint sock;
1.17 gboolean connected;
1.18 + struct sockaddr_in addr;
1.19 };
1.20
1.21 typedef struct _StreamData StreamData;
1.22 @@ -33,24 +36,26 @@
1.23 {
1.24 guint id;
1.25 guint port;
1.26 + gint s;
1.27 };
1.28
1.29 -static void gmyth_stream_client_class_init (GMythStreamClientClass *klass);
1.30 -static void gmyth_stream_client_init (GMythStreamClient *object);
1.31 -static void gmyth_stream_client_dispose (GObject *object);
1.32 -static void gmyth_stream_client_finalize (GObject *object);
1.33 +static void gmyth_stream_client_class_init (GMythStreamClientClass *klass);
1.34 +static void gmyth_stream_client_init (GMythStreamClient *object);
1.35 +static void gmyth_stream_client_dispose (GObject *object);
1.36 +static void gmyth_stream_client_finalize (GObject *object);
1.37 +static StreamData* gmtyh_stream_client_get_streamdata (GMythStreamClient *self,
1.38 + guint stream_id);
1.39 +
1.40
1.41 G_DEFINE_TYPE(GMythStreamClient, gmyth_stream_client, G_TYPE_OBJECT)
1.42
1.43 static void
1.44 gmyth_stream_client_class_init (GMythStreamClientClass *klass)
1.45 -{
1.46 +{
1.47 GObjectClass *gobject_class;
1.48 -
1.49 gobject_class = (GObjectClass *) klass;
1.50
1.51 g_type_class_add_private (klass, sizeof (GMythStreamClientPrivate));
1.52 -
1.53 gobject_class->dispose = gmyth_stream_client_dispose;
1.54 gobject_class->finalize = gmyth_stream_client_finalize;
1.55 }
1.56 @@ -60,20 +65,20 @@
1.57 {
1.58 }
1.59
1.60 -static void
1.61 +static void
1.62 gmyth_stream_client_dispose (GObject *object)
1.63 {
1.64 gmyth_stream_client_disconnect (GMYTH_STREAM_CLIENT (object));
1.65 }
1.66
1.67
1.68 -static void
1.69 +static void
1.70 gmyth_stream_client_finalize (GObject *object)
1.71 {
1.72 }
1.73
1.74 -GMythStreamClient*
1.75 -gmyth_stream_client_new ()
1.76 +GMythStreamClient*
1.77 +gmyth_stream_client_new ()
1.78 {
1.79 return GMYTH_STREAM_CLIENT (g_object_new (GMYTH_TYPE_STREAM_CLIENT, NULL));
1.80 }
1.81 @@ -81,26 +86,22 @@
1.82 gboolean
1.83 gmyth_stream_client_connect (GMythStreamClient *self, const gchar *server, guint port)
1.84 {
1.85 - struct sockaddr_in addr;
1.86 - struct hostent *hp;
1.87 -
1.88 GMythStreamClientPrivate *priv = GMYTH_STREAM_CLIENT_GET_PRIVATE (self);
1.89 -
1.90 g_return_val_if_fail (priv->connected == FALSE, TRUE);
1.91
1.92 priv->sock = socket (PF_INET, SOCK_STREAM, 0);
1.93 - if (priv->sock == -1) {
1.94 + if (priv->sock == -1) {
1.95 g_debug ("Fail to create sock");
1.96 return FALSE;
1.97 }
1.98
1.99 - memset(&addr, 0, sizeof(addr));
1.100 - hp = gethostbyname(server);
1.101 - memcpy(&addr, *(hp->h_addr_list),sizeof(struct in_addr));
1.102 - addr.sin_family = AF_INET;
1.103 - addr.sin_port = htons (port);
1.104 + memset(&(priv->addr), 0, sizeof(priv->addr));
1.105 + priv->host = gethostbyname(server);
1.106 + memcpy(&(priv->addr), *(priv->host->h_addr_list), sizeof(struct in_addr));
1.107 + priv->addr.sin_family = AF_INET;
1.108 + priv->addr.sin_port = htons (port);
1.109
1.110 - if (connect (priv->sock, (struct sockaddr *) &addr, sizeof (addr)) == -1) {
1.111 + if (connect (priv->sock, (struct sockaddr *) &(priv->addr), sizeof (priv->addr)) == -1) {
1.112 g_debug ("Fail to connect with server");
1.113 shutdown (priv->sock, SHUT_RDWR);
1.114 priv->sock = -1;
1.115 @@ -115,7 +116,7 @@
1.116 gmyth_stream_client_disconnect (GMythStreamClient *self)
1.117 {
1.118 GMythStreamClientPrivate *priv = GMYTH_STREAM_CLIENT_GET_PRIVATE (self);
1.119 -
1.120 +
1.121 g_return_if_fail (priv->connected == FALSE);
1.122
1.123 GList *lst = priv->streams;
1.124 @@ -125,10 +126,10 @@
1.125 gmyth_stream_client_close_stream (self, data->id);
1.126 }
1.127
1.128 - close (priv->sock);
1.129 + close (priv->sock);
1.130 shutdown (priv->sock, SHUT_RDWR);
1.131 priv->sock = -1;
1.132 - priv->connected = FALSE;
1.133 + priv->connected = FALSE;
1.134 }
1.135
1.136 guint
1.137 @@ -144,61 +145,90 @@
1.138 guint port,
1.139 const gchar* opt)
1.140 {
1.141 - gint stream_id = 0;
1.142 gchar *cmd;
1.143 StreamData *data = NULL;
1.144 GMythStreamClientPrivate *priv = GMYTH_STREAM_CLIENT_GET_PRIVATE (self);
1.145 -
1.146 +
1.147 g_return_val_if_fail (priv->connected == TRUE, FALSE);
1.148 -
1.149 - if (send (priv->sock, "SETUP", 5, MSG_CONFIRM) == -1)
1.150 - return -1;
1.151 + g_return_val_if_fail (file_name != NULL, FALSE);
1.152
1.153 - cmd = g_strdup_printf ("%s %s %s %d %f %s %d %d %d %d %s",
1.154 - file_name, mux, vcodec, vbitrate, fps,
1.155 - acodec, abitrate, width, height, port, opt);
1.156 -
1.157 -
1.158 + cmd = g_strdup_printf ("SETUP %s %s %s %d %f %s %d %d %d %d %s\n",
1.159 + file_name,
1.160 + (mux == NULL? "X" : mux),
1.161 + (vcodec == NULL ? "X" : vcodec),
1.162 + vbitrate,
1.163 + fps,
1.164 + (acodec == NULL ? "X" : acodec),
1.165 + abitrate,
1.166 + width, height, port,
1.167 + (opt == NULL ? "X" : opt));
1.168 +
1.169 if (send (priv->sock, cmd, strlen (cmd), MSG_CONFIRM) == -1) {
1.170 - g_free (cmd);
1.171 - return -1;
1.172 + g_free (cmd);
1.173 + return -1;
1.174 }
1.175 g_free (cmd);
1.176
1.177 -
1.178 +
1.179 data = g_new0 (StreamData, 1);
1.180 - data->id = stream_id;
1.181 - data->port = port;
1.182 + data->port = port;
1.183 + data->id = data->s = socket (PF_INET, SOCK_STREAM, 0);
1.184 +
1.185
1.186 priv->streams = g_list_append (priv->streams, data);
1.187 - return stream_id;
1.188 + return data->id;
1.189 +}
1.190 +
1.191 +static gchar**
1.192 +_parse_return (int fd)
1.193 +{
1.194 +
1.195 }
1.196
1.197 gboolean
1.198 gmyth_stream_client_play_stream (GMythStreamClient *self,
1.199 - guint stream_id)
1.200 + guint stream_id)
1.201 {
1.202 GMythStreamClientPrivate *priv = GMYTH_STREAM_CLIENT_GET_PRIVATE (self);
1.203 -
1.204 + StreamData *data;
1.205 + struct sockaddr_in addr;
1.206 + gchar **retval;
1.207 +
1.208 g_return_val_if_fail (priv->connected == TRUE, FALSE);
1.209 -
1.210 - if (send (priv->sock, "PLAY", 4, MSG_MORE) == -1) {
1.211 - return FALSE;
1.212 - }
1.213 +
1.214 + data = gmtyh_stream_client_get_streamdata (self, stream_id);
1.215 + g_return_val_if_fail (data != NULL, FALSE);
1.216 +
1.217 + if (send (priv->sock, "PLAY\n", 5, MSG_MORE) == -1) {
1.218 + return FALSE;
1.219 + }
1.220 +
1.221 + retval = _read_message (priv->sock);
1.222 +
1.223 + g_usleep (10 * G_USEC_PER_SEC);
1.224 +
1.225 + memset(&addr, 0, sizeof(addr));
1.226 + memcpy(&addr, *(priv->host->h_addr_list), sizeof(struct in_addr));
1.227 + addr.sin_family = AF_INET;
1.228 + addr.sin_port = htons (data->port);
1.229 + g_debug ("request connection on port %d", data->port);
1.230 + if (connect(data->s,(struct sockaddr*) &addr, sizeof(addr)) == -1)
1.231 + g_warning ("Fail to connect to server");
1.232 +
1.233 return TRUE;
1.234 -}
1.235 +}
1.236
1.237 void
1.238 gmyth_stream_client_pause_stream (GMythStreamClient *self,
1.239 - guint stream_id)
1.240 + guint stream_id)
1.241 {
1.242 GMythStreamClientPrivate *priv = GMYTH_STREAM_CLIENT_GET_PRIVATE (self);
1.243 -
1.244 +
1.245 g_return_if_fail (priv->connected == TRUE);
1.246 -
1.247 - if (send (priv->sock, "PAUSE", 5, MSG_MORE) == -1) {
1.248 - return;
1.249 - }
1.250 +
1.251 + if (send (priv->sock, "PAUSE\n", 6, MSG_MORE) == -1) {
1.252 + return;
1.253 + }
1.254 }
1.255
1.256 void
1.257 @@ -206,13 +236,27 @@
1.258 guint stream_id)
1.259 {
1.260 GMythStreamClientPrivate *priv = GMYTH_STREAM_CLIENT_GET_PRIVATE (self);
1.261 -
1.262 +
1.263 g_return_if_fail (priv->connected == TRUE);
1.264 -
1.265 - if (send (priv->sock, "STOP", 4, MSG_MORE) == -1) {
1.266 - return;
1.267 - }
1.268 -
1.269 - //TODO: remove from streams list
1.270 -
1.271 + if (send (priv->sock, "STOP\n", 5, MSG_MORE) == -1) {
1.272 + return;
1.273 + }
1.274 + //TODO: remove from streams list
1.275 }
1.276 +
1.277 +static StreamData*
1.278 +gmtyh_stream_client_get_streamdata (GMythStreamClient *self,
1.279 + guint stream_id)
1.280 +{
1.281 + GMythStreamClientPrivate *priv = GMYTH_STREAM_CLIENT_GET_PRIVATE (self);
1.282 + GList *lst;
1.283 +
1.284 + lst = priv->streams;
1.285 + for (; lst != NULL; lst = lst->next) {
1.286 + StreamData *data = (StreamData *) lst->data;
1.287 + if (data->id == stream_id) {
1.288 + return data;
1.289 + }
1.290 + }
1.291 + return NULL;
1.292 +}