[Alsaplayer-devel]new client.c

Andy Lo A Foe andy@alsaplayer.org
Wed, 13 Feb 2002 19:29:40 +0100


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

Attached is a client.c that will work with the current CVS version.
Again, only speed control is implemented. This code is just used as a
testbed. I will be creating a libalsaplayer.so shared library with the
necessary functions to communicate with an alsaplayer session (also
scheduled for tonight).

Andy

--bp/iNruPH9dso1Pn
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;

int
ap_connect_session (int session)
{
  int socket_fd;
  struct sockaddr_un saddr;

  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) {
      return socket_fd;
    }
    else {
      fprintf (stderr, "Could not connect to socket\n");
    }
  }
  else {
    fprintf (stderr, "Could not open socket\n");
  }
  return -1;
}


void
ap_read_packet (int fd, ap_pkt_t * pkt)
{
  read (fd, pkt, sizeof (ap_pkt_t));
  if (pkt->pld_length) {
    pkt->payload = malloc (pkt->pld_length);
    read (fd, pkt->payload, pkt->pld_length);
  }
}


void
ap_write_packet (int fd, ap_pkt_t pkt)
{
  pkt.version = AP_CONTROL_VERSION;
  write (fd, &pkt, sizeof (ap_pkt_t));
  if (pkt.pld_length) {
    write (fd, pkt.payload, pkt.pld_length);
  }
}


int
ap_do (int session, ap_cmd_t cmd)
{
  ap_pkt_t pkt;
  int fd;

  if ((fd = ap_connect_session (session)) == -1)
    return -1;
  pkt.cmd = cmd;
  pkt.pld_length = 0;
  ap_write_packet (fd, pkt);
  close (fd);
}


int
ap_get_int (int session, ap_cmd_t cmd)
{
  ap_pkt_t pkt;
  int fd;
  int state = -1;

  if ((fd = ap_connect_session (session)) == -1)
    return -1;
  ap_read_packet (fd, &pkt);
  if (pkt.pld_length == sizeof (int)) {
    state = *(int *) pkt.payload;
  }
  close (fd);
  return state;
}


int
ap_set_float (int session, ap_cmd_t cmd, float val)
{
  ap_pkt_t pkt;
  int fd;
  if ((fd = ap_connect_session (session)) == -1)
    return -1;
  pkt.cmd = cmd;
  pkt.pld_length = sizeof (float);
  pkt.payload = &val;
  ap_write_packet (fd, pkt);
  close (fd);
}


float
ap_get_float (int session, ap_cmd_t cmd)
{
  float val = 0.0;
  ap_pkt_t pkt;
  int fd;
  if ((fd = ap_connect_session (session)) == -1)
    return val;

  pkt.cmd = cmd;
  pkt.pld_length = 0;
  ap_write_packet (fd, pkt);
  ap_read_packet (fd, &pkt);
  if (pkt.pld_length) {
    val = *(float *) pkt.payload;
    free (pkt.payload);
  }
  close (fd);
  return val;
}


int
main (int argc, char *argv[])
{
  int i;
  if (argc == 2) {
    if (sscanf (argv[1], "%f", &val) == 1) {
      if (val == 0.0) {
	printf ("Pausing player\n");
	ap_do (0, AP_DO_PAUSE);
      }
      else {
	printf ("Setting speed to %.2f\n", val);
	ap_set_float (0, AP_SET_FLOAT_SPEED, val);
      }
      sleep (1);
    }
  }
  printf ("Current speed = %.2f\n", ap_get_float (0, AP_GET_FLOAT_SPEED));
  return 0;
}

--bp/iNruPH9dso1Pn--