6 #include <sys/socket.h>
8 #include <netinet/in.h>
11 #include <glib/gprintf.h>
14 #include "gmyth-stream-client.h"
17 #define GMYTH_STREAM_CLIENT_GET_PRIVATE(obj) \
18 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GMYTH_TYPE_STREAM_CLIENT, GMythStreamClientPrivate))
21 typedef struct _GMythStreamClientPrivate GMythStreamClientPrivate;
23 struct _GMythStreamClientPrivate
30 typedef struct _StreamData StreamData;
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);
43 G_DEFINE_TYPE(GMythStreamClient, gmyth_stream_client, G_TYPE_OBJECT)
46 gmyth_stream_client_class_init (GMythStreamClientClass *klass)
48 GObjectClass *gobject_class;
50 gobject_class = (GObjectClass *) klass;
52 g_type_class_add_private (klass, sizeof (GMythStreamClientPrivate));
54 gobject_class->dispose = gmyth_stream_client_dispose;
55 gobject_class->finalize = gmyth_stream_client_finalize;
59 gmyth_stream_client_init (GMythStreamClient *self)
64 gmyth_stream_client_dispose (GObject *object)
66 gmyth_stream_client_disconnect (GMYTH_STREAM_CLIENT (object));
71 gmyth_stream_client_finalize (GObject *object)
76 gmyth_stream_client_new ()
78 return GMYTH_STREAM_CLIENT (g_object_new (GMYTH_TYPE_STREAM_CLIENT, NULL));
82 gmyth_stream_client_connect (GMythStreamClient *self, const gchar *server, guint port)
84 struct sockaddr_in addr;
87 GMythStreamClientPrivate *priv = GMYTH_STREAM_CLIENT_GET_PRIVATE (self);
89 g_return_val_if_fail (priv->connected == FALSE, TRUE);
91 priv->sock = socket (PF_INET, SOCK_STREAM, 0);
92 if (priv->sock == -1) {
93 g_debug ("Fail to create sock");
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);
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);
110 priv->connected = TRUE;
115 gmyth_stream_client_disconnect (GMythStreamClient *self)
117 GMythStreamClientPrivate *priv = GMYTH_STREAM_CLIENT_GET_PRIVATE (self);
119 g_return_if_fail (priv->connected == FALSE);
121 GList *lst = priv->streams;
123 for (; lst != NULL; lst = lst->next) {
124 StreamData *data = (StreamData *) lst->data;
125 gmyth_stream_client_close_stream (self, data->id);
129 shutdown (priv->sock, SHUT_RDWR);
131 priv->connected = FALSE;
135 gmyth_stream_client_open_stream (GMythStreamClient *self,
136 const gchar* file_name,
143 guint width, guint height,
149 StreamData *data = NULL;
150 GMythStreamClientPrivate *priv = GMYTH_STREAM_CLIENT_GET_PRIVATE (self);
152 g_return_val_if_fail (priv->connected == TRUE, FALSE);
154 if (send (priv->sock, "SETUP", 5, MSG_CONFIRM) == -1)
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);
162 if (send (priv->sock, cmd, strlen (cmd), MSG_CONFIRM) == -1) {
169 data = g_new0 (StreamData, 1);
170 data->id = stream_id;
173 priv->streams = g_list_append (priv->streams, data);
178 gmyth_stream_client_play_stream (GMythStreamClient *self,
181 GMythStreamClientPrivate *priv = GMYTH_STREAM_CLIENT_GET_PRIVATE (self);
183 g_return_val_if_fail (priv->connected == TRUE, FALSE);
185 if (send (priv->sock, "PLAY", 4, MSG_MORE) == -1) {
192 gmyth_stream_client_pause_stream (GMythStreamClient *self,
195 GMythStreamClientPrivate *priv = GMYTH_STREAM_CLIENT_GET_PRIVATE (self);
197 g_return_if_fail (priv->connected == TRUE);
199 if (send (priv->sock, "PAUSE", 5, MSG_MORE) == -1) {
205 gmyth_stream_client_close_stream (GMythStreamClient *self,
208 GMythStreamClientPrivate *priv = GMYTH_STREAM_CLIENT_GET_PRIVATE (self);
210 g_return_if_fail (priv->connected == TRUE);
212 if (send (priv->sock, "STOP", 4, MSG_MORE) == -1) {
216 //TODO: remove from streams list