XMMS2

src/xmms/visualization/udp.c

Go to the documentation of this file.
00001 /*  XMMS2 - X Music Multiplexer System
00002  *  Copyright (C) 2003-2009 XMMS2 Team
00003  *
00004  *  PLUGINS ARE NOT CONSIDERED TO BE DERIVED WORK !!!
00005  *
00006  *  This library is free software; you can redistribute it and/or
00007  *  modify it under the terms of the GNU Lesser General Public
00008  *  License as published by the Free Software Foundation; either
00009  *  version 2.1 of the License, or (at your option) any later version.
00010  *
00011  *  This library is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  *  Lesser General Public License for more details.
00015  */
00016 
00017 #include <stdlib.h>
00018 #include "common.h"
00019 
00020 static gboolean
00021 udpwatcher (GIOChannel *src, GIOCondition cond, xmms_visualization_t *vis)
00022 {
00023     struct sockaddr_storage from;
00024     socklen_t sl = sizeof (from);
00025     xmmsc_vis_udp_timing_t packet_d;
00026     char* packet = packet_init_timing (&packet_d);
00027     if ((recvfrom (vis->socket, packet, packet_d.size, 0, (struct sockaddr *)&from, &sl)) > 0) {
00028         if (*packet_d.__unaligned_type == 'H') {
00029             xmms_vis_client_t *c;
00030             int32_t id;
00031 
00032             XMMSC_VIS_UNALIGNED_READ (id, packet_d.__unaligned_id, int32_t);
00033             id = ntohl (id);
00034 
00035             /* debug code starts
00036             char adrb[INET6_ADDRSTRLEN];
00037             struct sockaddr_in6 *a = (struct sockaddr_in6 *)&from;
00038             printf ("Client address: %s:%d, %d\n", inet_ntop (AF_INET6, &a->sin6_addr,
00039                     adrb, INET6_ADDRSTRLEN), a->sin6_port, id);
00040              debug code ends */
00041             g_mutex_lock (vis->clientlock);
00042             c = get_client (id);
00043             if (!c || c->type != VIS_UDP) {
00044                 g_mutex_unlock (vis->clientlock);
00045                 return TRUE;
00046             }
00047             /* save client address according to id */
00048             memcpy (&c->transport.udp.addr, &from, sizeof (from));
00049             c->transport.udp.socket[0] = 1;
00050             c->transport.udp.grace = 2000;
00051             g_mutex_unlock (vis->clientlock);
00052         } else if (*packet_d.__unaligned_type == 'T') {
00053             struct timeval time;
00054             xmms_vis_client_t *c;
00055             int32_t id;
00056 
00057             XMMSC_VIS_UNALIGNED_READ (id, packet_d.__unaligned_id, int32_t);
00058             id = ntohl (id);
00059 
00060             g_mutex_lock (vis->clientlock);
00061             c = get_client (id);
00062             if (!c || c->type != VIS_UDP) {
00063                 g_mutex_unlock (vis->clientlock);
00064                 free (packet);
00065                 return TRUE;
00066             }
00067             c->transport.udp.grace = 2000;
00068             g_mutex_unlock (vis->clientlock);
00069 
00070             /* give pong */
00071             gettimeofday (&time, NULL);
00072 
00073             struct timeval cts, sts;
00074 
00075             XMMSC_VIS_UNALIGNED_READ (cts.tv_sec, &packet_d.__unaligned_clientstamp[0], int32_t);
00076             XMMSC_VIS_UNALIGNED_READ (cts.tv_usec, &packet_d.__unaligned_clientstamp[1], int32_t);
00077             cts.tv_sec = ntohl (cts.tv_sec);
00078             cts.tv_usec = ntohl (cts.tv_usec);
00079 
00080             sts.tv_sec = time.tv_sec - cts.tv_sec;
00081             sts.tv_usec = time.tv_usec - cts.tv_usec;
00082             if (sts.tv_usec < 0) {
00083                 sts.tv_sec--;
00084                 sts.tv_usec += 1000000;
00085             }
00086 
00087             XMMSC_VIS_UNALIGNED_WRITE (&packet_d.__unaligned_serverstamp[0],
00088                                        (int32_t)htonl (sts.tv_sec), int32_t);
00089             XMMSC_VIS_UNALIGNED_WRITE (&packet_d.__unaligned_serverstamp[1],
00090                                        (int32_t)htonl (sts.tv_usec), int32_t);
00091 
00092             sendto (vis->socket, packet, packet_d.size, 0, (struct sockaddr *)&from, sl);
00093 
00094             /* new debug:
00095             printf ("Timings: local %f, remote %f, diff %f\n", tv2ts (&time), net2ts (packet_d.clientstamp), net2ts (packet_d.clientstamp) - tv2ts (&time));
00096              ends */
00097         } else {
00098             xmms_log_error ("Received invalid UDP package!");
00099         }
00100     }
00101     free (packet);
00102     return TRUE;
00103 }
00104 
00105 int32_t
00106 init_udp (xmms_visualization_t *vis, int32_t id, xmms_error_t *err)
00107 {
00108     // TODO: we need the currently used port, not only the default one! */
00109     int32_t port = XMMS_DEFAULT_TCP_PORT;
00110     xmms_vis_client_t *c;
00111 
00112     // setup socket if needed
00113     if (!xmms_socket_valid (vis->socket)) {
00114         struct addrinfo hints;
00115         struct addrinfo *result, *rp;
00116         int s;
00117 
00118         memset (&hints, 0, sizeof (hints));
00119         hints.ai_family = AF_UNSPEC;
00120         hints.ai_socktype = SOCK_DGRAM;
00121         hints.ai_flags = AI_PASSIVE;
00122         hints.ai_protocol = 0;
00123 
00124         if ((s = getaddrinfo (NULL, G_STRINGIFY (XMMS_DEFAULT_TCP_PORT), &hints, &result)) != 0)
00125         {
00126             xmms_log_error ("Could not setup socket! getaddrinfo: %s", gai_strerror (s));
00127             xmms_error_set (err, XMMS_ERROR_NO_SAUSAGE, "Could not setup socket!");
00128             return -1;
00129         }
00130 
00131         for (rp = result; rp != NULL; rp = rp->ai_next) {
00132             vis->socket = socket (rp->ai_family, rp->ai_socktype, rp->ai_protocol);
00133             if (!xmms_socket_valid (vis->socket)) {
00134                 continue;
00135             }
00136             if (bind (vis->socket, rp->ai_addr, rp->ai_addrlen) != -1) {
00137                 break;
00138             } else {
00139                 close (vis->socket);
00140             }
00141         }
00142         if (rp == NULL) {
00143             xmms_log_error ("Could not bind socket!");
00144             xmms_error_set (err, XMMS_ERROR_NO_SAUSAGE, "Could not bind socket!");
00145             freeaddrinfo (result);
00146             return -1;
00147         }
00148         freeaddrinfo (result);
00149 
00150         /* register into mainloop: */
00151 /* perhaps needed, perhaps not .. #ifdef __WIN32__
00152         vis->socketio = g_io_channel_win32_new_socket (vis->socket);
00153 #else */
00154         vis->socketio = g_io_channel_unix_new (vis->socket);
00155 /*#endif */
00156         g_io_channel_set_encoding (vis->socketio, NULL, NULL);
00157         g_io_channel_set_buffered (vis->socketio, FALSE);
00158         g_io_add_watch (vis->socketio, G_IO_IN, (GIOFunc) udpwatcher, vis);
00159     }
00160 
00161     /* set up client structure */
00162     x_fetch_client (id);
00163     c->type = VIS_UDP;
00164     memset (&c->transport.udp.addr, 0, sizeof (c->transport.udp.addr));
00165     c->transport.udp.socket[0] = 0;
00166     x_release_client ();
00167 
00168     xmms_log_info ("Visualization client %d initialised using UDP", id);
00169     return port;
00170 }
00171 
00172 void
00173 cleanup_udp (xmmsc_vis_udp_t *t, xmms_socket_t socket)
00174 {
00175     socklen_t sl = sizeof (t->addr);
00176     char packet = 'K';
00177     sendto (socket, &packet, 1, 0, (struct sockaddr *)&t->addr, sl);
00178 }
00179 
00180 gboolean
00181 write_udp (xmmsc_vis_udp_t *t, xmms_vis_client_t *c, int32_t id, struct timeval *time, int channels, int size, short *buf, int socket)
00182 {
00183     xmmsc_vis_udp_data_t packet_d;
00184     xmmsc_vischunk_t *__unaligned_dest;
00185     short res;
00186     int offset;
00187     char* packet;
00188 
00189     /* first check if the client is still there */
00190     if (t->grace == 0) {
00191         delete_client (id);
00192         return FALSE;
00193     }
00194     if (t->socket == 0) {
00195         return FALSE;
00196     }
00197 
00198     packet = packet_init_data (&packet_d);
00199     t->grace--;
00200     XMMSC_VIS_UNALIGNED_WRITE (packet_d.__unaligned_grace, htons (t->grace), uint16_t);
00201     __unaligned_dest = packet_d.__unaligned_data;
00202 
00203     XMMSC_VIS_UNALIGNED_WRITE (&__unaligned_dest->timestamp[0],
00204                                (int32_t)htonl (time->tv_sec), int32_t);
00205     XMMSC_VIS_UNALIGNED_WRITE (&__unaligned_dest->timestamp[1],
00206                                (int32_t)htonl (time->tv_usec), int32_t);
00207 
00208 
00209     XMMSC_VIS_UNALIGNED_WRITE (&__unaligned_dest->format, (uint16_t)htons (c->format), uint16_t);
00210     res = fill_buffer (__unaligned_dest->data, &c->prop, channels, size, buf);
00211     XMMSC_VIS_UNALIGNED_WRITE (&__unaligned_dest->size, (uint16_t)htons (res), uint16_t);
00212 
00213     offset = ((char*)&__unaligned_dest->data - (char*)__unaligned_dest);
00214 
00215     sendto (socket, packet, XMMS_VISPACKET_UDP_OFFSET + offset + res * sizeof (int16_t), 0, (struct sockaddr *)&t->addr, sizeof (t->addr));
00216     free (packet);
00217 
00218 
00219     return TRUE;
00220 }