2
0

airplay_sink.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <ctype.h>
  4. #include <stdlib.h>
  5. #include "mdns.h"
  6. #include "nvs.h"
  7. #include "tcpip_adapter.h"
  8. #include "esp_log.h"
  9. #include "esp_console.h"
  10. #include "esp_pthread.h"
  11. #include "esp_system.h"
  12. #include "freertos/timers.h"
  13. #include "airplay_sink.h"
  14. #include "trace.h"
  15. static const char * TAG = "platform";
  16. extern char current_namespace[];
  17. void airplay_sink_init(void) {
  18. const char *hostname;
  19. char *airplay_name, sink_name[32] = CONFIG_AIRPLAY_NAME;
  20. nvs_handle nvs;
  21. tcpip_adapter_get_hostname(TCPIP_ADAPTER_IF_STA, &hostname);
  22. //initialize mDNS
  23. ESP_ERROR_CHECK( mdns_init() );
  24. ESP_ERROR_CHECK( mdns_hostname_set(hostname) );
  25. //structure with TXT records
  26. mdns_txt_item_t serviceTxtData[] = {
  27. {"am", "esp32"},
  28. {"tp", "UDP"},
  29. {"sm","false"},
  30. {"sv","false"},
  31. {"ek","1"},
  32. {"et","0,1"},
  33. {"md","0,1,2"},
  34. {"cn","0,1"},
  35. {"ch","2"},
  36. {"ss","16"},
  37. {"sr","44100"},
  38. {"vn","3"},
  39. {"txtvers","1"},
  40. };
  41. if (nvs_open(current_namespace, NVS_READONLY, &nvs) == ESP_OK) {
  42. size_t len = 31;
  43. nvs_get_str(nvs, "airplay_sink_name", sink_name, &len);
  44. nvs_close(nvs);
  45. }
  46. // AirPlay wants mDNS name to be MAC@name
  47. uint8_t mac[6];
  48. esp_read_mac(mac, ESP_MAC_WIFI_STA);
  49. asprintf(&airplay_name, "%02X%02X%02X%02X%02X%02X@%s", mac[3], mac[4], mac[5], mac[3], mac[4], mac[5], sink_name);
  50. ESP_LOGI(TAG, "mdns hostname set to: [%s] with servicename %s", hostname, sink_name);
  51. //initialize service
  52. ESP_ERROR_CHECK( mdns_service_add(airplay_name, "_raop", "_tcp", 6000, serviceTxtData, sizeof(serviceTxtData) / sizeof(mdns_txt_item_t)) );
  53. free(airplay_name);
  54. }