raop_sink.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. // IDF-V4++ #include "esp_netif.h"
  9. #include "esp_log.h"
  10. #include "esp_console.h"
  11. #include "esp_pthread.h"
  12. #include "esp_system.h"
  13. #include "freertos/timers.h"
  14. #include "platform_config.h"
  15. #include "raop.h"
  16. #include "audio_controls.h"
  17. #include "display.h"
  18. #include "accessors.h"
  19. #include "log_util.h"
  20. #include "trace.h"
  21. #ifndef CONFIG_AIRPLAY_NAME
  22. #define CONFIG_AIRPLAY_NAME "ESP32-AirPlay"
  23. #endif
  24. log_level raop_loglevel = lINFO;
  25. log_level util_loglevel;
  26. static log_level *loglevel = &raop_loglevel;
  27. static struct raop_ctx_s *raop;
  28. static raop_cmd_vcb_t cmd_handler_chain;
  29. static void raop_volume_up(void) {
  30. raop_cmd(raop, RAOP_VOLUME_UP, NULL);
  31. LOG_INFO("AirPlay volume up");
  32. }
  33. static void raop_volume_down(void) {
  34. raop_cmd(raop, RAOP_VOLUME_DOWN, NULL);
  35. LOG_INFO("AirPlay volume down");
  36. }
  37. static void raop_toggle(void) {
  38. raop_cmd(raop, RAOP_TOGGLE, NULL);
  39. LOG_INFO("AirPlay play/pause");
  40. }
  41. static void raop_pause(void) {
  42. raop_cmd(raop, RAOP_PAUSE, NULL);
  43. LOG_INFO("AirPlay pause");
  44. }
  45. static void raop_play(void) {
  46. raop_cmd(raop, RAOP_PLAY, NULL);
  47. LOG_INFO("AirPlay play");
  48. }
  49. static void raop_stop(void) {
  50. raop_cmd(raop, RAOP_STOP, NULL);
  51. LOG_INFO("AirPlay stop");
  52. }
  53. static void raop_prev(void) {
  54. raop_cmd(raop, RAOP_PREV, NULL);
  55. LOG_INFO("AirPlay previous");
  56. }
  57. static void raop_next(void) {
  58. raop_cmd(raop, RAOP_NEXT, NULL);
  59. LOG_INFO("AirPlay next");
  60. }
  61. const static actrls_t controls = {
  62. raop_volume_up, raop_volume_down, // volume up, volume down
  63. raop_toggle, raop_play, // toggle, play
  64. raop_pause, raop_stop, // pause, stop
  65. NULL, NULL, // rew, fwd
  66. raop_prev, raop_next, // prev, next
  67. NULL, NULL, NULL, NULL, // left, right, up, down
  68. raop_volume_down, raop_volume_up, raop_toggle// knob left, knob_right, knob push
  69. };
  70. /****************************************************************************************
  71. * Command handler
  72. */
  73. static bool cmd_handler(raop_event_t event, ...) {
  74. va_list args;
  75. va_start(args, event);
  76. // handle audio event and stop if forbidden
  77. if (!cmd_handler_chain(event, args)) {
  78. va_end(args);
  79. return false;
  80. }
  81. // now handle events for display
  82. switch(event) {
  83. case RAOP_SETUP:
  84. actrls_set(controls, NULL);
  85. displayer_control(DISPLAYER_ACTIVATE, "AIRPLAY");
  86. break;
  87. case RAOP_PLAY:
  88. displayer_control(DISPLAYER_TIMER_RUN);
  89. break;
  90. case RAOP_FLUSH:
  91. displayer_control(DISPLAYER_TIMER_PAUSE);
  92. break;
  93. case RAOP_STOP:
  94. actrls_unset();
  95. displayer_control(DISPLAYER_SUSPEND);
  96. break;
  97. case RAOP_METADATA: {
  98. char *artist = va_arg(args, char*), *album = va_arg(args, char*), *title = va_arg(args, char*);
  99. displayer_metadata(artist, album, title);
  100. break;
  101. }
  102. case RAOP_PROGRESS: {
  103. int elapsed = va_arg(args, int), duration = va_arg(args, int);
  104. displayer_timer(DISPLAYER_ELAPSED, elapsed, duration);
  105. break;
  106. }
  107. default:
  108. break;
  109. }
  110. va_end(args);
  111. return true;
  112. }
  113. /****************************************************************************************
  114. * Airplay sink de-initialization
  115. */
  116. void raop_sink_deinit(void) {
  117. raop_delete(raop);
  118. mdns_free();
  119. }
  120. /****************************************************************************************
  121. * Airplay sink initialization
  122. */
  123. void raop_sink_init(raop_cmd_vcb_t cmd_cb, raop_data_cb_t data_cb) {
  124. const char *hostname;
  125. char sink_name[64-6] = CONFIG_AIRPLAY_NAME;
  126. tcpip_adapter_ip_info_t ipInfo;
  127. struct in_addr host;
  128. // get various IP info
  129. tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_STA, &ipInfo);
  130. tcpip_adapter_get_hostname(TCPIP_ADAPTER_IF_STA, &hostname);
  131. host.s_addr = ipInfo.ip.addr;
  132. // initialize mDNS
  133. ESP_ERROR_CHECK( mdns_init() );
  134. ESP_ERROR_CHECK( mdns_hostname_set(hostname) );
  135. char * sink_name_buffer= (char *)config_alloc_get(NVS_TYPE_STR,"airplay_name");
  136. if(sink_name_buffer != NULL){
  137. memset(sink_name, 0x00, sizeof(sink_name));
  138. strncpy(sink_name,sink_name_buffer,sizeof(sink_name)-1 );
  139. free(sink_name_buffer);
  140. }
  141. LOG_INFO( "mdns hostname for ip %s set to: [%s] with servicename %s", inet_ntoa(host), hostname, sink_name);
  142. // create RAOP instance, latency is set by controller
  143. uint8_t mac[6];
  144. esp_read_mac(mac, ESP_MAC_WIFI_STA);
  145. cmd_handler_chain = cmd_cb;
  146. raop = raop_create(host, sink_name, mac, 0, cmd_handler, data_cb);
  147. }
  148. /****************************************************************************************
  149. * Airplay forced disconnection
  150. */
  151. void raop_disconnect(void) {
  152. LOG_INFO("forced disconnection");
  153. displayer_control(DISPLAYER_SHUTDOWN);
  154. // in case we can't communicate with AirPlay controller, abort session
  155. if (!raop_cmd(raop, RAOP_STOP, NULL)) raop_abort(raop);
  156. actrls_unset();
  157. }