gmyth-stream/client/src/gmyth-stream-client.c
author morphbr
Wed Apr 04 21:18:43 2007 +0100 (2007-04-04)
branchtrunk
changeset 499 9900800a8dd6
child 520 92acf23b87f0
permissions -rw-r--r--
[svn r504] Gmyth-Streamer: mencoder stdout bug fix (Chenca's bug)
     1 #ifdef HAVE_CONFIG_H
     2 #include "config.h"
     3 #endif
     4 
     5 #include <sys/types.h>
     6 #include <sys/socket.h>
     7 #include <netdb.h>
     8 #include <netinet/in.h>
     9 #include <unistd.h>
    10 #include <glib.h>
    11 #include <glib/gprintf.h>
    12 #include <string.h>
    13 
    14 #include "gmyth-stream-client.h"
    15 
    16 
    17 #define GMYTH_STREAM_CLIENT_GET_PRIVATE(obj) \
    18     (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GMYTH_TYPE_STREAM_CLIENT, GMythStreamClientPrivate))
    19 
    20 
    21 typedef struct _GMythStreamClientPrivate GMythStreamClientPrivate;
    22 
    23 struct _GMythStreamClientPrivate
    24 {
    25 	GList *streams;
    26 	gint sock;
    27 	gboolean connected;
    28 };
    29 
    30 typedef struct _StreamData StreamData;
    31 
    32 struct _StreamData
    33 {
    34 	guint id;
    35 	guint port;
    36 };
    37 
    38 static void gmyth_stream_client_class_init  (GMythStreamClientClass *klass);
    39 static void gmyth_stream_client_init        (GMythStreamClient *object);
    40 static void gmyth_stream_client_dispose  	(GObject *object);
    41 static void gmyth_stream_client_finalize 	(GObject *object);
    42 
    43 G_DEFINE_TYPE(GMythStreamClient, gmyth_stream_client, G_TYPE_OBJECT)
    44 
    45 static void
    46 gmyth_stream_client_class_init (GMythStreamClientClass *klass)
    47 {   
    48 	GObjectClass *gobject_class;
    49 
    50 	gobject_class = (GObjectClass *) klass;
    51 
    52 	g_type_class_add_private (klass, sizeof (GMythStreamClientPrivate));
    53         
    54 	gobject_class->dispose  = gmyth_stream_client_dispose;
    55 	gobject_class->finalize = gmyth_stream_client_finalize;
    56 }
    57 
    58 static void
    59 gmyth_stream_client_init (GMythStreamClient *self)
    60 {
    61 }
    62 
    63 static void 
    64 gmyth_stream_client_dispose (GObject *object)
    65 {
    66     gmyth_stream_client_disconnect (GMYTH_STREAM_CLIENT (object));
    67 }
    68 
    69 
    70 static void 
    71 gmyth_stream_client_finalize (GObject *object)
    72 {
    73 }
    74 
    75 GMythStreamClient*    	
    76 gmyth_stream_client_new ()                         
    77 {
    78 	return GMYTH_STREAM_CLIENT (g_object_new (GMYTH_TYPE_STREAM_CLIENT, NULL));
    79 }
    80 
    81 gboolean
    82 gmyth_stream_client_connect (GMythStreamClient *self, const gchar *server, guint port)
    83 {
    84     	struct sockaddr_in addr;
    85      	struct hostent *hp;
    86 
    87 	GMythStreamClientPrivate *priv = GMYTH_STREAM_CLIENT_GET_PRIVATE (self);
    88 	
    89 	g_return_val_if_fail (priv->connected == FALSE, TRUE);
    90 
    91 	priv->sock = socket (PF_INET, SOCK_STREAM, 0);
    92 	if (priv->sock == -1) {	    
    93 	    g_debug ("Fail to create sock");
    94 	    return FALSE;
    95 	}
    96 
    97 	memset(&addr, 0, sizeof(addr));
    98 	hp = gethostbyname(server);
    99 	memcpy(&addr, *(hp->h_addr_list),sizeof(struct in_addr));
   100 	addr.sin_family = AF_INET;
   101 	addr.sin_port = htons (port);
   102 
   103 	if (connect (priv->sock, (struct sockaddr *) &addr, sizeof (addr)) == -1) {
   104             g_debug ("Fail to connect with server");
   105 	    shutdown (priv->sock,  SHUT_RDWR);
   106 	    priv->sock = -1;
   107 	    return FALSE;
   108 	}
   109 
   110     priv->connected = TRUE;
   111     return TRUE;
   112 }
   113 
   114 void
   115 gmyth_stream_client_disconnect (GMythStreamClient *self)
   116 {
   117     GMythStreamClientPrivate *priv = GMYTH_STREAM_CLIENT_GET_PRIVATE (self);
   118     
   119 	g_return_if_fail (priv->connected == FALSE);
   120 
   121 	GList *lst = priv->streams;
   122 
   123 	for (; lst != NULL; lst = lst->next) {
   124 		StreamData *data = (StreamData *) lst->data;
   125 		gmyth_stream_client_close_stream (self, data->id);
   126 	}
   127 
   128 	close (priv->sock);	
   129 	shutdown (priv->sock, SHUT_RDWR);
   130     priv->sock = -1;
   131     priv->connected = FALSE;	
   132 }
   133 
   134 guint
   135 gmyth_stream_client_open_stream (GMythStreamClient *self,
   136                 const gchar* file_name,
   137                 const gchar* mux,
   138                 const gchar* vcodec,
   139                 guint vbitrate,
   140                 gdouble fps,
   141                 const gchar* acodec,
   142                 guint abitrate,
   143 				guint width, guint height,
   144 				guint port,
   145 				const gchar* opt)
   146 {
   147 	gint stream_id = 0;
   148 	gchar *cmd;
   149 	StreamData *data = NULL;
   150 	GMythStreamClientPrivate *priv = GMYTH_STREAM_CLIENT_GET_PRIVATE (self);
   151 	
   152 	g_return_val_if_fail (priv->connected == TRUE, FALSE);
   153 	
   154 	if (send (priv->sock, "SETUP", 5, MSG_CONFIRM) == -1)
   155  	    return -1;
   156 
   157 	cmd = g_strdup_printf ("%s %s %s %d %f %s %d %d %d %d %s",
   158 	                file_name, mux, vcodec, vbitrate, fps,
   159 	                acodec, abitrate, width, height, port, opt);
   160 	
   161 	
   162 	if (send (priv->sock, cmd, strlen (cmd), MSG_CONFIRM) == -1) {
   163 	    g_free (cmd);
   164 	    return -1;
   165 	}
   166 	g_free (cmd);
   167 
   168 	
   169 	data = g_new0 (StreamData, 1);
   170 	data->id = stream_id;
   171     	data->port = port;
   172 
   173 	priv->streams = g_list_append (priv->streams, data);
   174 	return stream_id;
   175 }
   176 
   177 gboolean
   178 gmyth_stream_client_play_stream (GMythStreamClient *self,
   179 				                 guint stream_id)
   180 {
   181 	GMythStreamClientPrivate *priv = GMYTH_STREAM_CLIENT_GET_PRIVATE (self);
   182 	
   183 	g_return_val_if_fail (priv->connected == TRUE, FALSE);
   184     
   185     	if (send (priv->sock, "PLAY", 4, MSG_MORE) == -1) {
   186 	        return FALSE;
   187     	}	
   188 	return TRUE;
   189 }	
   190 
   191 void
   192 gmyth_stream_client_pause_stream (GMythStreamClient *self,
   193 				                  guint stream_id)
   194 {
   195 	GMythStreamClientPrivate *priv = GMYTH_STREAM_CLIENT_GET_PRIVATE (self);
   196 	
   197 	g_return_if_fail (priv->connected == TRUE);
   198     
   199     if (send (priv->sock, "PAUSE", 5, MSG_MORE) == -1) {
   200         return;
   201     }	
   202 }
   203 
   204 void
   205 gmyth_stream_client_close_stream (GMythStreamClient *self,
   206                     			  guint stream_id)
   207 {
   208 	GMythStreamClientPrivate *priv = GMYTH_STREAM_CLIENT_GET_PRIVATE (self);
   209 	
   210 	g_return_if_fail (priv->connected == TRUE);
   211     
   212     if (send (priv->sock, "STOP", 4, MSG_MORE) == -1) {
   213         return;
   214     }
   215     
   216     //TODO: remove from streams list
   217         	
   218 }