#include <errno.h>
#include <netinet/in.h>
#include <sys/time.h>
+#include <sys/un.h>
#include "log.h"
#include "mem.h"
#include "timeval.h"
#include "client.h"
#include "playrtp.h"
+#include "inputline.h"
#define readahead linux_headers_are_borked
HEAP_DEFINE(pheap, struct packet *, lt_packet);
+/** @brief Control socket or NULL */
+const char *control_socket;
+
static const struct option options[] = {
{ "help", no_argument, 0, 'h' },
{ "version", no_argument, 0, 'V' },
#if HAVE_COREAUDIO_AUDIOHARDWARE_H
{ "core-audio", no_argument, 0, 'c' },
#endif
+ { "socket", required_argument, 0, 's' },
{ "config", required_argument, 0, 'C' },
{ 0, 0, 0, 0 }
};
+/** @brief Control thread
+ *
+ * This thread is responsible for accepting control commands from Disobedience
+ * (or other controllers) over an AF_UNIX stream socket with a path specified
+ * by the @c --socket option. The protocol uses simple string commands and
+ * replies:
+ *
+ * - @c stop will shut the player down
+ * - @c query will send back the reply @c running
+ * - anything else is ignored
+ *
+ * Commands and response strings terminated by shutting down the connection or
+ * by a newline. No attempt is made to multiplex multiple clients so it is
+ * important that the command be sent as soon as the connection is made - it is
+ * assumed that both parties to the protocol are entirely cooperating with one
+ * another.
+ */
+static void *control_thread(void attribute((unused)) *arg) {
+ struct sockaddr_un sa;
+ int sfd, cfd;
+ char *line;
+ socklen_t salen;
+ FILE *fp;
+
+ assert(control_socket);
+ unlink(control_socket);
+ memset(&sa, 0, sizeof sa);
+ sa.sun_family = AF_UNIX;
+ strcpy(sa.sun_path, control_socket);
+ sfd = xsocket(PF_UNIX, SOCK_STREAM, 0);
+ if(bind(sfd, (const struct sockaddr *)&sa, sizeof sa) < 0)
+ fatal(errno, "error binding to %s", control_socket);
+ if(listen(sfd, 128) < 0)
+ fatal(errno, "error calling listen on %s", control_socket);
+ info("listening on %s", control_socket);
+ for(;;) {
+ salen = sizeof sa;
+ cfd = accept(sfd, (struct sockaddr *)&sa, &salen);
+ if(cfd < 0) {
+ switch(errno) {
+ case EINTR:
+ case EAGAIN:
+ break;
+ default:
+ fatal(errno, "error calling accept on %s", control_socket);
+ }
+ }
+ if(!(fp = fdopen(cfd, "r+"))) {
+ error(errno, "error calling fdopen for %s connection", control_socket);
+ close(cfd);
+ continue;
+ }
+ if(!inputline(control_socket, fp, &line, '\n')) {
+ if(!strcmp(line, "stop")) {
+ info("stopped via %s", control_socket);
+ exit(0); /* terminate immediately */
+ }
+ if(!strcmp(line, "query"))
+ fprintf(fp, "running");
+ xfree(line);
+ }
+ if(fclose(fp) < 0)
+ error(errno, "error closing %s connection", control_socket);
+ }
+}
+
/** @brief Drop the first packet
*
* Assumes that @ref lock is held.
*/
static void play_rtp(void) {
pthread_t ltid;
+ int err;
/* We receive and convert audio data in a background thread */
- pthread_create(<id, 0, listen_thread, 0);
+ if((err = pthread_create(<id, 0, listen_thread, 0)))
+ fatal(err, "pthread_create listen_thread");
/* We have a second thread to add received packets to the queue */
- pthread_create(<id, 0, queue_thread, 0);
+ if((err = pthread_create(<id, 0, queue_thread, 0)))
+ fatal(err, "pthread_create queue_thread");
/* The rest of the work is backend-specific */
backend();
}
}
int main(int argc, char **argv) {
- int n;
+ int n, err;
struct addrinfo *res;
struct stringlist sl;
char *sockname;
case 'c': backend = playrtp_coreaudio; break;
#endif
case 'C': configfile = optarg; break;
+ case 's': control_socket = optarg; break;
default: fatal(0, "invalid option");
}
}
info("default socket receive buffer %d", rcvbuf);
if(logfp)
info("WARNING: -L option can impact performance");
+ if(control_socket) {
+ pthread_t tid;
+
+ if((err = pthread_create(&tid, 0, control_thread, 0)))
+ fatal(err, "pthread_create control_thread");
+ }
play_rtp();
return 0;
}