[Alsaplayer-devel]socket control!

Andy Lo A Foe andy@alsaplayer.org
Mon, 11 Feb 2002 01:59:34 +0100


--nFreZHaLTZJo0R7j
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline

Howdy,

We have socket control! Yes, still in the very early stage, some stuff
is hardcoded, only a single alsaplayer process supported right now, but
it is working!!!

Every alsaplayer process will  create a socket where it will listen
for commands from external sources. The initial commands will be send
only (i.e. no replies). You will be able to do freaky things like set 
20 x playback speed (tested with 100 x, no audible sound, but no crash
either :-) I just committed the initial code. I have a simple client
that is only able to set the speed.. attached, please try with latest
CVS, usage 'client speed_in_float' i.e. 'client 1.25' or 'client -2.0'

compile: gcc -o client client.c

Have fun!
Andy

--nFreZHaLTZJo0R7j
Content-Type: text/x-chdr; charset=us-ascii
Content-Disposition: attachment; filename="ControlSocket.h"

/*  ControlSocket.h
 *  Copyright (C) 2002 Andy Lo A Foe <andy@alsaplayer.org>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/ 

#define SOCKET_CONTROL_BASE_VERSION		0x1000
#define SOCKET_CONTROL_VERSION				(SOCKET_CONTROL_BASE_VERSION + 1)

typedef enum {
	TYPE_INT8 = 0x1,
	TYPE_INT16,
	TYPE_INT32,
	TYPE_FLOAT,
	TYPE_STRING,
	TYPE_NONE
} PayloadType;

typedef enum  {
	CMD_PLAY = 0x1,
	CMD_STOP,
	CMD_NEXT,
	CMD_PREV,
	CMD_PAUSE,
	CMD_PING,
	CMD_GET_NAME,
	CMD_GET_SONGNAME,
	CMD_GET_SONGLENGTH,
	CMD_GET_CURRENTTIME,
	CMD_SET_VOLUME,
	CMD_SET_POS,
	CMD_SET_SPEED,
	CMD_GET_VOLUME,
	CMD_GET_POS,
	CMD_GET_SPEED
} CommandType;	

struct _ap_msg {
	int version;			
	CommandType cmd_type;
	PayloadType pld_type;
	int length;
};	
	
typedef struct _ap_msg ap_msg_t;

int  AP_Connect(int session);
int  AP_SendMessage(int session_id, ap_msg_t *msg, ap_msg_t *reply);


--nFreZHaLTZJo0R7j
Content-Type: text/x-csrc; charset=us-ascii
Content-Disposition: attachment; filename="client.c"

#include "ControlSocket.h"
#include <sys/un.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

static float val = 0.0;

void send_message()
{
	int socket_fd;
	struct sockaddr_un saddr;
	static int i = 0;
	float float_val;
	ap_msg_t msg;
				
	if ((socket_fd = socket(AF_UNIX, SOCK_STREAM, 0)) != -1) {
		saddr.sun_family = AF_UNIX;
		sprintf(saddr.sun_path, "/tmp/alsaplayer_%d", 0);
		if (connect(socket_fd, (struct sockaddr *) &saddr, sizeof (saddr)) != -1) {
				msg.version = SOCKET_CONTROL_VERSION;
				msg.cmd_type = CMD_SET_SPEED;
				msg.pld_type = TYPE_FLOAT;
				msg.length = sizeof(float);
				float_val = val;
				printf("writing client %d (%.2f)\n", ++i, float_val);
				write(socket_fd, &msg, sizeof(ap_msg_t));
				write(socket_fd, &float_val, sizeof(float)); 
				close(socket_fd);
		}
	}
}

int main(int argc, char *argv[])
{
	int i;
	if (argc == 2) {
					if (sscanf(argv[1], "%f", &val) == 1) 
									printf("Setting speed to %.2f\n", val);
	}				
	send_message();
	return 0;
}

--nFreZHaLTZJo0R7j--