Added getopt to select mode #1; Added udpserver #2

This commit is contained in:
2017-12-15 12:03:53 +01:00
parent 1afa4504ab
commit c298de57a8
10 changed files with 208 additions and 54 deletions

View File

@@ -51,3 +51,12 @@ const char *lookupSounds(char input) {
return "";
}
int cfgreadudpport(void) {
int port;
if (!config_lookup_int(&cfg, "udpport", &port)) {
printf("FEHLER beim lesen von: udpport, Standardwert(8888) wird bereitgestellt.\n");
port = 8888;
}
return port;
}

View File

@@ -8,5 +8,6 @@ void cfginit(void);
void cfgdestroy(void);
void cfgreinit(void);
const char *lookupSounds(char input);
int cfgreadudpport(void);

View File

@@ -1,18 +1,54 @@
#include <ao/ao.h>
#include <mpg123.h>
#include <stdlib.h>
#include <stdio.h>
#include "csoundbox.h"
#include "config.h"
#include <string.h>
#include <ncurses.h>
#define BITS 8
#include <getopt.h>
#include "csoundbox.h"
#include "config.h"
#include "sound.h"
#include "udpserver.h"
int main(int argc, char *argv[]) {
const char *short_options = "hsi:l";
struct option long_options[] = {
{"server", no_argument, NULL, 's'},
{"ip", required_argument, NULL, 'i'},
{"help", no_argument, NULL, 'h'},
{"local", no_argument, NULL, 'l'}
};
int c;
//initialize libconfig
cfginit();
inputLocal();
while ( (c = getopt_long(argc, argv, short_options, long_options, NULL)) != -1 ) {
switch (c) {
case 's':
//start server
printf("Server.\n");
udpserver();
break;
case 'h':
//show the help page
break;
case 'i':
//connect to a csoundbox server
break;
case 'l':
default:
//start local
inputLocal();
break;
}
}
if (argc < 2) {
inputLocal();
}
cfgdestroy();
return 0;
}
@@ -44,49 +80,4 @@ void printCursesWelcome(void) {
mvprintw(y, x - (strlen(msg) / 2), msg);
}
void playSound(const char *path) {
mpg123_handle *mh;
unsigned char *buffer;
size_t buffer_size;
size_t done;
int err;
int driver;
ao_device *dev;
ao_sample_format format;
int channels, encoding;
long rate;
/* initializations */
ao_initialize();
driver = ao_default_driver_id();
mpg123_init();
mh = mpg123_new(NULL, &err);
buffer_size = mpg123_outblock(mh);
buffer = (unsigned char*) malloc(buffer_size * sizeof(unsigned char));
/* open the file and get the decoding format */
mpg123_open(mh, path);
mpg123_getformat(mh, &rate, &channels, &encoding);
/* set the output format and open the output device */
format.bits = mpg123_encsize(encoding) * BITS;
format.rate = rate;
format.channels = channels;
format.byte_format = AO_FMT_NATIVE;
format.matrix = 0;
dev = ao_open_live(driver, &format, NULL);
/* decode and play */
while (mpg123_read(mh, buffer, buffer_size, &done) == MPG123_OK)
ao_play(dev, (char *)buffer, done);
/* clean up */
free(buffer);
ao_close(dev);
mpg123_close(mh);
mpg123_delete(mh);
mpg123_exit();
ao_shutdown();
}

View File

@@ -6,3 +6,5 @@ soundmap = {
);
}
udpport = 8888;

View File

@@ -1,4 +1,3 @@
void playSound(const char *path);
void inputLocal(void);
void printCursesWelcome(void);

View File

@@ -7,7 +7,7 @@ CFLAGS = -Wall -O3 -DVERSION=\"$(VERSION)\"
#LDFLAGS = -lm -lpthread `gtk-config --cflags` `gtk-config --libs` -lgthread
LDFLAGS = -lmpg123 -lao -lconfig -lncurses
OBJ = csoundbox.o config.o
OBJ = csoundbox.o config.o sound.o udpserver.o
all: $(OBJ)
$(CC) $(CFLAGS) -o csoundbox $(OBJ) $(LDFLAGS)

54
sound.c Normal file
View File

@@ -0,0 +1,54 @@
#include <ao/ao.h>
#include <mpg123.h>
#include "sound.h"
#define BITS 8
void playSound(const char *path) {
mpg123_handle *mh;
unsigned char *buffer;
size_t buffer_size;
size_t done;
int err;
int driver;
ao_device *dev;
ao_sample_format format;
int channels, encoding;
long rate;
/* initializations */
ao_initialize();
driver = ao_default_driver_id();
mpg123_init();
mh = mpg123_new(NULL, &err);
buffer_size = mpg123_outblock(mh);
buffer = (unsigned char*) malloc(buffer_size * sizeof(unsigned char));
/* open the file and get the decoding format */
mpg123_open(mh, path);
mpg123_getformat(mh, &rate, &channels, &encoding);
/* set the output format and open the output device */
format.bits = mpg123_encsize(encoding) * BITS;
format.rate = rate;
format.channels = channels;
format.byte_format = AO_FMT_NATIVE;
format.matrix = 0;
dev = ao_open_live(driver, &format, NULL);
/* decode and play */
while (mpg123_read(mh, buffer, buffer_size, &done) == MPG123_OK)
ao_play(dev, (char *)buffer, done);
/* clean up */
free(buffer);
ao_close(dev);
mpg123_close(mh);
mpg123_delete(mh);
mpg123_exit();
ao_shutdown();
}

1
sound.h Normal file
View File

@@ -0,0 +1 @@
void playSound(const char *path);

94
udpserver.c Normal file
View File

@@ -0,0 +1,94 @@
#include<stdio.h> //printf
#include<string.h> //memset, strtok
#include<stdlib.h> //exit(0);
#include<arpa/inet.h>
#include<sys/socket.h>
#include <unistd.h> //close()
#include "udpserver.h"
#include "sound.h"
#include "config.h"
#define BUFLEN 512 //Max length of buffer
//#define PORT 8888
#include "udpserver.h"
void die(char *s)
{
perror(s);
//exit(1);
}
void udpserver(void)
{
struct sockaddr_in si_me, si_other;
int PORT = cfgreadudpport();
int s, slen = sizeof(si_other) , recv_len;
char buf[BUFLEN];
//create a UDP socket
if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
{
die("socket");
}
// zero out the structure
memset((char *) &si_me, 0, sizeof(si_me));
si_me.sin_family = AF_INET;
si_me.sin_port = htons(PORT);
si_me.sin_addr.s_addr = htonl(INADDR_ANY);
//bind socket to port
if( bind(s , (struct sockaddr*)&si_me, sizeof(si_me) ) == -1)
{
die("bind");
}
//keep listening for data
while(1)
{
//printf("Waiting for data...");
//fflush(stdout);
memset(buf,'\0', BUFLEN);
//try to receive some data, this is a blocking call
if ((recv_len = recvfrom(s, buf, BUFLEN, 0, (struct sockaddr *) &si_other, (unsigned int *)&slen)) == -1)
{
die("recvfrom()");
}
//print details of the client/peer and the data received
printf("Received packet from %s:%d\n", inet_ntoa(si_other.sin_addr), ntohs(si_other.sin_port));
printf("Data: %s\n" , buf);
if (!processUDPInput(buf)) {
break;
}
//now reply the client with the same data
if (sendto(s, buf, recv_len, 0, (struct sockaddr*) &si_other, slen) == -1)
{
die("sendto()");
}
}
close(s);
}
int processUDPInput(char *input) {
/**
* Returns 0 on exit
* Return 1 on success
*/
if (strcmp(input, "exit") == 0) {
return 0;
}
playSound(lookupSounds(input[0]));
return 1;
}

3
udpserver.h Normal file
View File

@@ -0,0 +1,3 @@
void die(char *s); //For debugging
void udpserver(void); //start the server by calling this function
int processUDPInput(char *input);