Browse files

Airplay WIP

  • Loading branch information...
1 parent 8d147a4 commit 0576ad4bdd6c7e699456780efb907a67f1d9db99 @andoma committed Mar 24, 2011
Showing with 209 additions and 1 deletion.
  1. +2 −1 Makefile
  2. +1 −0 configure.linux
  3. +4 −0 src/api/api.c
  4. +201 −0 src/sd/avahi.c
  5. +1 −0 support/configure.inc
View
3 Makefile
@@ -188,7 +188,8 @@ SRCS += src/api/api.c \
src/api/tmdb.c \
src/api/tvdb.c \
-SRCS-$(CONFIG_HTTPSERVER) += src/api/httpcontrol.c \
+SRCS-$(CONFIG_HTTPSERVER) += src/api/httpcontrol.c
+SRCS-$(CONFIG_AIRPLAY) += src/api/airplay.c
##############################################################
# Networking
View
1 configure.linux
@@ -107,6 +107,7 @@ enable httpserver
enable timegm
enable inotify
enable realpath
+#enable airplay -- not functional yet
#enable libxrandr -- code does not really work yet
for opt do
View
4 src/api/api.c
@@ -6,13 +6,17 @@
#include "tmdb.h"
#include "tvdb.h"
#include "lastfm.h"
+#include "airplay.h"
void
api_init(void)
{
opensub_init();
#if ENABLE_HTTPSERVER
httpcontrol_init();
+#if ENABLE_AIRPLAY
+ airplay_init();
+#endif
#endif
tmdb_init();
lastfm_init();
View
201 src/sd/avahi.c
@@ -20,6 +20,10 @@
#include <string.h>
#include <avahi-client/client.h>
+#include <avahi-client/publish.h>
+
+#include <avahi-common/alternative.h>
+
#include <avahi-client/lookup.h>
#include <avahi-common/simple-watch.h>
#include <avahi-common/error.h>
@@ -185,6 +189,198 @@ service_type_add(const char *name, service_class_t class, AvahiClient *c)
}
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+#if ENABLE_AIRPLAY
+
+static AvahiEntryGroup *group = NULL;
+static char *name = NULL;
+
+static void create_services(AvahiClient *c);
+
+static void
+entry_group_callback(AvahiEntryGroup *g, AvahiEntryGroupState state,
+ void *userdata)
+{
+ char *n;
+
+ assert(g == group || group == NULL);
+ group = g;
+
+ /* Called whenever the entry group state changes */
+
+ switch (state) {
+ case AVAHI_ENTRY_GROUP_ESTABLISHED :
+ /* The entry group has been established successfully */
+ TRACE(TRACE_INFO, "AVAHI",
+ "Service '%s' successfully established.", name);
+ break;
+
+ case AVAHI_ENTRY_GROUP_COLLISION:
+
+ /* A service name collision with a remote service
+ * happened. Let's pick a new name */
+ n = avahi_alternative_service_name(name);
+ avahi_free(name);
+ name = n;
+
+ TRACE(TRACE_ERROR, "AVAHI",
+ "Service name collision, renaming service to '%s'", name);
+
+ /* And recreate the services */
+ create_services(avahi_entry_group_get_client(g));
+ break;
+
+ case AVAHI_ENTRY_GROUP_FAILURE:
+ TRACE(TRACE_ERROR, "AVAHI",
+ "Entry group failure: %s",
+ avahi_strerror(avahi_client_errno(avahi_entry_group_get_client(g))));
+ break;
+
+ case AVAHI_ENTRY_GROUP_UNCOMMITED:
+ case AVAHI_ENTRY_GROUP_REGISTERING:
+ ;
+ }
+}
+
+
+/**
+ *
+ */
+static void
+create_services(AvahiClient *c)
+{
+ char *n;
+ int ret;
+ assert(c);
+
+ /* If this is the first time we're called, let's create a new
+ * entry group if necessary */
+
+ if (!group)
+ if (!(group = avahi_entry_group_new(c, entry_group_callback, NULL))) {
+ TRACE(TRACE_ERROR, "AVAHI",
+ "avahi_enty_group_new() failed: %s",
+ avahi_strerror(avahi_client_errno(c)));
+ goto fail;
+ }
+
+ /* If the group is empty (either because it was just created, or
+ * because it was reset previously, add our entries. */
+
+ if (avahi_entry_group_is_empty(group)) {
+ TRACE(TRACE_DEBUG, "AVAHI", "Adding service '%s'", name);
+
+ /* Add the service for HTSP */
+ if ((ret = avahi_entry_group_add_service(group, AVAHI_IF_UNSPEC,
+ AVAHI_PROTO_UNSPEC, 0, name,
+ "_airplay._tcp", NULL, NULL, 42000,
+ NULL)) < 0) {
+
+ if (ret == AVAHI_ERR_COLLISION)
+ goto collision;
+
+ TRACE(TRACE_ERROR, "AVAHI",
+ "Failed to add _airplay._tcp service: %s",
+ avahi_strerror(ret));
+ goto fail;
+ }
+
+
+ /* Tell the server to register the service */
+ if ((ret = avahi_entry_group_commit(group)) < 0) {
+ TRACE(TRACE_ERROR, "AVAHI",
+ "Failed to commit entry group: %s",
+ avahi_strerror(ret));
+ goto fail;
+ }
+ }
+
+ return;
+
+ collision:
+
+ /* A service name collision with a local service happened. Let's
+ * pick a new name */
+ n = avahi_alternative_service_name(name);
+ avahi_free(name);
+ name = n;
+
+ TRACE(TRACE_ERROR, "AVAHI",
+ "Service name collision, renaming service to '%s'", name);
+
+ avahi_entry_group_reset(group);
+
+ create_services(c);
+ return;
+
+ fail:
+ return;
+}
+
+
+/**
+ *
+ */
+static void
+client_callback(AvahiClient *c, AvahiClientState state, void *userdata)
+{
+ assert(c);
+
+ /* Called whenever the client or server state changes */
+
+ switch (state) {
+ case AVAHI_CLIENT_S_RUNNING:
+
+ /* The server has startup successfully and registered its host
+ * name on the network, so it's time to create our services */
+ create_services(c);
+ break;
+
+ case AVAHI_CLIENT_FAILURE:
+ TRACE(TRACE_ERROR, "AVAHI", "Client failure: %s",
+ avahi_strerror(avahi_client_errno(c)));
+ break;
+
+ case AVAHI_CLIENT_S_COLLISION:
+
+ /* Let's drop our registered services. When the server is back
+ * in AVAHI_SERVER_RUNNING state we will register them
+ * again with the new host name. */
+
+ case AVAHI_CLIENT_S_REGISTERING:
+
+ /* The server records are now being established. This
+ * might be caused by a host name change. We need to wait
+ * for our own records to register until the host name is
+ * properly esatblished. */
+
+ if(group)
+ avahi_entry_group_reset(group);
+
+ break;
+
+ case AVAHI_CLIENT_CONNECTING:
+ ;
+ }
+}
+#endif
+
/**
*
*/
@@ -202,6 +398,11 @@ avahi_thread(void *aux)
service_type_add("_webdav._tcp", SERVICE_WEBDAV, c);
service_type_add("_htsp._tcp", SERVICE_HTSP, c);
+#if ENABLE_AIRPLAY
+ name = strdup("Showtime");
+ avahi_client_new(ap, AVAHI_CLIENT_NO_FAIL, client_callback, NULL, NULL);
+#endif
+
while((avahi_simple_poll_iterate(asp, -1)) != -1) {}
return NULL;
View
1 support/configure.inc
@@ -82,6 +82,7 @@ CONFIG_LIST="
libav
vda
tlsf
+ airplay
"
cleanup() {

0 comments on commit 0576ad4

Please sign in to comment.