raop_sink.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. static EXT_RAM_ATTR struct raop_cb_s {
  25. raop_cmd_vcb_t cmd;
  26. raop_data_cb_t data;
  27. } raop_cbs;
  28. log_level raop_loglevel = lINFO;
  29. log_level util_loglevel;
  30. static log_level *loglevel = &raop_loglevel;
  31. static struct raop_ctx_s *raop;
  32. static raop_cmd_vcb_t cmd_handler_chain;
  33. static void raop_volume_up(bool pressed) {
  34. if (!pressed) return;
  35. raop_cmd(raop, RAOP_VOLUME_UP, NULL);
  36. LOG_INFO("AirPlay volume up");
  37. }
  38. static void raop_volume_down(bool pressed) {
  39. if (!pressed) return;
  40. raop_cmd(raop, RAOP_VOLUME_DOWN, NULL);
  41. LOG_INFO("AirPlay volume down");
  42. }
  43. static void raop_toggle(bool pressed) {
  44. if (!pressed) return;
  45. raop_cmd(raop, RAOP_TOGGLE, NULL);
  46. LOG_INFO("AirPlay play/pause");
  47. }
  48. static void raop_pause(bool pressed) {
  49. if (!pressed) return;
  50. raop_cmd(raop, RAOP_PAUSE, NULL);
  51. LOG_INFO("AirPlay pause");
  52. }
  53. static void raop_play(bool pressed) {
  54. if (!pressed) return;
  55. raop_cmd(raop, RAOP_PLAY, NULL);
  56. LOG_INFO("AirPlay play");
  57. }
  58. static void raop_stop(bool pressed) {
  59. if (!pressed) return;
  60. raop_cmd(raop, RAOP_STOP, NULL);
  61. LOG_INFO("AirPlay stop");
  62. }
  63. static void raop_prev(bool pressed) {
  64. if (!pressed) return;
  65. raop_cmd(raop, RAOP_PREV, NULL);
  66. LOG_INFO("AirPlay previous");
  67. }
  68. static void raop_next(bool pressed) {
  69. if (!pressed) return;
  70. raop_cmd(raop, RAOP_NEXT, NULL);
  71. LOG_INFO("AirPlay next");
  72. }
  73. const static actrls_t controls = {
  74. NULL, // power
  75. raop_volume_up, raop_volume_down, // volume up, volume down
  76. raop_toggle, raop_play, // toggle, play
  77. raop_pause, raop_stop, // pause, stop
  78. NULL, NULL, // rew, fwd
  79. raop_prev, raop_next, // prev, next
  80. NULL, NULL, NULL, NULL, // left, right, up, down
  81. NULL, NULL, NULL, NULL, NULL, NULL, // pre1-6
  82. raop_volume_down, raop_volume_up, raop_toggle// knob left, knob_right, knob push
  83. };
  84. /****************************************************************************************
  85. * Command handler
  86. */
  87. static bool cmd_handler(raop_event_t event, ...) {
  88. va_list args;
  89. va_start(args, event);
  90. // handle audio event and stop if forbidden
  91. if (!cmd_handler_chain(event, args)) {
  92. va_end(args);
  93. return false;
  94. }
  95. // now handle events for display
  96. switch(event) {
  97. case RAOP_SETUP:
  98. actrls_set(controls, false, NULL, actrls_ir_action);
  99. displayer_control(DISPLAYER_ACTIVATE, "AIRPLAY");
  100. break;
  101. case RAOP_PLAY:
  102. displayer_control(DISPLAYER_TIMER_RUN);
  103. break;
  104. case RAOP_FLUSH:
  105. displayer_control(DISPLAYER_TIMER_PAUSE);
  106. break;
  107. case RAOP_STOP:
  108. actrls_unset();
  109. displayer_control(DISPLAYER_SUSPEND);
  110. break;
  111. case RAOP_METADATA: {
  112. char *artist = va_arg(args, char*), *album = va_arg(args, char*), *title = va_arg(args, char*);
  113. displayer_metadata(artist, album, title);
  114. break;
  115. }
  116. case RAOP_PROGRESS: {
  117. int elapsed = va_arg(args, int), duration = va_arg(args, int);
  118. displayer_timer(DISPLAYER_ELAPSED, elapsed, duration);
  119. break;
  120. }
  121. default:
  122. break;
  123. }
  124. va_end(args);
  125. return true;
  126. }
  127. /****************************************************************************************
  128. * Airplay sink de-initialization
  129. */
  130. void raop_sink_deinit(void) {
  131. raop_delete(raop);
  132. mdns_free();
  133. }
  134. /****************************************************************************************
  135. * Airplay sink startup
  136. */
  137. static bool raop_sink_start(raop_cmd_vcb_t cmd_cb, raop_data_cb_t data_cb) {
  138. const char *hostname = NULL;
  139. char sink_name[64-6] = CONFIG_AIRPLAY_NAME;
  140. tcpip_adapter_ip_info_t ipInfo = { };
  141. struct in_addr host;
  142. tcpip_adapter_if_t ifs[] = { TCPIP_ADAPTER_IF_ETH, TCPIP_ADAPTER_IF_STA, TCPIP_ADAPTER_IF_AP };
  143. // get various IP info
  144. for (int i = 0; i < sizeof(ifs) / sizeof(tcpip_adapter_if_t); i++)
  145. if (tcpip_adapter_get_ip_info(ifs[i], &ipInfo) == ESP_OK && ipInfo.ip.addr != IPADDR_ANY) {
  146. tcpip_adapter_get_hostname(ifs[i], &hostname);
  147. break;
  148. }
  149. if (!hostname) {
  150. LOG_INFO( "no hostname/IP found, can't start AirPlay");
  151. return false;
  152. }
  153. host.s_addr = ipInfo.ip.addr;
  154. // initialize mDNS
  155. ESP_ERROR_CHECK( mdns_init() );
  156. ESP_ERROR_CHECK( mdns_hostname_set(hostname) );
  157. char * sink_name_buffer= (char *)config_alloc_get(NVS_TYPE_STR,"airplay_name");
  158. if (sink_name_buffer != NULL){
  159. memset(sink_name, 0x00, sizeof(sink_name));
  160. strncpy(sink_name,sink_name_buffer,sizeof(sink_name)-1 );
  161. free(sink_name_buffer);
  162. }
  163. LOG_INFO( "mdns hostname for ip %s set to: [%s] with servicename %s", inet_ntoa(host), hostname, sink_name);
  164. // create RAOP instance, latency is set by controller
  165. uint8_t mac[6];
  166. esp_read_mac(mac, ESP_MAC_WIFI_STA);
  167. cmd_handler_chain = cmd_cb;
  168. raop = raop_create(host, sink_name, mac, 0, cmd_handler, data_cb);
  169. return true;
  170. }
  171. /****************************************************************************************
  172. * Airplay sink timer handler
  173. */
  174. static void raop_start_handler( TimerHandle_t xTimer ) {
  175. if (raop_sink_start(raop_cbs.cmd, raop_cbs.data)) {
  176. xTimerDelete(xTimer, portMAX_DELAY);
  177. }
  178. }
  179. /****************************************************************************************
  180. * Airplay sink initialization
  181. */
  182. void raop_sink_init(raop_cmd_vcb_t cmd_cb, raop_data_cb_t data_cb) {
  183. if (!raop_sink_start(cmd_cb, data_cb)) {
  184. raop_cbs.cmd = cmd_cb;
  185. raop_cbs.data = data_cb;
  186. TimerHandle_t timer = xTimerCreate("raopStart", 5000 / portTICK_RATE_MS, pdTRUE, NULL, raop_start_handler);
  187. xTimerStart(timer, portMAX_DELAY);
  188. LOG_INFO( "Delaying AirPlay start");
  189. }
  190. }
  191. /****************************************************************************************
  192. * Airplay forced disconnection
  193. */
  194. void raop_disconnect(void) {
  195. LOG_INFO("forced disconnection");
  196. displayer_control(DISPLAYER_SHUTDOWN);
  197. // in case we can't communicate with AirPlay controller, abort session
  198. if (!raop_cmd(raop, RAOP_STOP, NULL)) raop_abort(raop);
  199. actrls_unset();
  200. }