Fixed and documented example.c
authorSebastian Krysmanski <noreply@manski.net>
Fri, 27 Dec 2013 15:45:49 +0000 (16:45 +0100)
committerSebastian Krysmanski <noreply@manski.net>
Fri, 27 Dec 2013 15:45:49 +0000 (16:45 +0100)
* Most code from shairplay.c
* Removed password (didn't work)
* Renamed device to "FakePort" ("AppleTV" may already be in use)
* Example no longer quits after 100 seconds

.gitignore
src/test/example.c

index dbb3f51b8afaa66f81435f427eedddd967a24dfb..95089f257903562d13abd38e3938c35dc559400c 100644 (file)
@@ -26,4 +26,5 @@ autom4te.cache
 aclocal.m4
 stamp-h1
 src/shairplay
+src/test/example
 
index 393b7820efef6ea0dc7f5d1ee9a893dffe7a4817..e93f252cd9b70dd9ea2195d5cea0f081a8690ec3 100644 (file)
@@ -1,3 +1,15 @@
+/*
+ * Starts the AirPlay server (name "FakePort") and dumps the received contents
+ * to files:
+ * - audio.pcm : decoded audio content
+ * - metadata.bin : meta data
+ * - covertart.jpg : cover art
+ *
+ * Requires avahi-daemon to run. Also requires file "airplay.key" in the same directory.
+ *
+ * Compile with: gcc -o example -g -I../../include/shairplay example.c -lshairplay 
+ */
+
 #include <stdlib.h>
 #include <stdio.h>
 #include <unistd.h>
@@ -9,6 +21,35 @@
 #include "dnssd.h"
 #include "raop.h"
 
+static int running;
+
+#ifndef WIN32
+
+#include <signal.h>
+static void
+signal_handler(int sig)
+{
+       switch (sig) {
+       case SIGINT:
+       case SIGTERM:
+               running = 0;
+               break;
+       }
+}
+static void
+init_signals(void)
+{
+       struct sigaction sigact;
+
+       sigact.sa_handler = signal_handler;
+       sigemptyset(&sigact.sa_mask);
+       sigact.sa_flags = 0;
+       sigaction(SIGINT, &sigact, NULL);
+       sigaction(SIGTERM, &sigact, NULL);
+}
+
+#endif
+
 static void *
 audio_init(void *cls, int bits, int channels, int samplerate)
 {
@@ -75,14 +116,16 @@ raop_log_callback(void *cls, int level, const char *msg)
 int
 main(int argc, char *argv[])
 {
-        const char *name = "AppleTV";
-        unsigned short raop_port = 5000;
-        const char hwaddr[] = { 0x48, 0x5d, 0x60, 0x7c, 0xee, 0x22 };
+       const char *name = "FakePort";
+       unsigned short raop_port = 5000;
+       const char hwaddr[] = { 0x48, 0x5d, 0x60, 0x7c, 0xee, 0x22 };
 
        dnssd_t *dnssd;
        raop_t *raop;
        raop_callbacks_t raop_cbs;
 
+       int error;
+
        raop_cbs.cls = NULL;
        raop_cbs.audio_init = audio_init;
        raop_cbs.audio_set_volume = audio_set_volume;
@@ -93,18 +136,39 @@ main(int argc, char *argv[])
        raop_cbs.audio_destroy = audio_destroy;
 
        raop = raop_init_from_keyfile(10, &raop_cbs, "airport.key", NULL);
+       if (raop == NULL) {
+               fprintf(stderr, "Could not initialize the RAOP service (airport.key missing?)\n");
+               return -1;
+       }
+
        raop_set_log_level(raop, RAOP_LOG_DEBUG);
        raop_set_log_callback(raop, &raop_log_callback, NULL);
-       raop_start(raop, &raop_port, hwaddr, sizeof(hwaddr), "test");
+       raop_start(raop, &raop_port, hwaddr, sizeof(hwaddr), NULL);
+
+       error = 0;
+       dnssd = dnssd_init(&error);
+       if (error) {
+               fprintf(stderr, "ERROR: Could not initialize dnssd library!\n");
+               fprintf(stderr, "------------------------------------------\n");
+               fprintf(stderr, "You could try the following resolutions based on your OS:\n");
+               fprintf(stderr, "Windows: Try installing http://support.apple.com/kb/DL999\n");
+               fprintf(stderr, "Debian/Ubuntu: Try installing libavahi-compat-libdnssd-dev package\n");
+               raop_destroy(raop);
+               return -1;
+       }
 
-       dnssd = dnssd_init(NULL);
        dnssd_register_raop(dnssd, name, raop_port, hwaddr, sizeof(hwaddr), 1);
 
+       printf("Startup complete... Kill with Ctrl+C\n");
+
+       running = 1;
+       while (running != 0) {
 #ifndef WIN32
-       sleep(100);
+               sleep(1);
 #else
-       Sleep(100*1000);
+               Sleep(1000);
 #endif
+       }
 
        dnssd_unregister_raop(dnssd);
        dnssd_destroy(dnssd);