raop_sink.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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", true);
  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. displayer_artwork(NULL);
  115. break;
  116. }
  117. case RAOP_ARTWORK: {
  118. uint8_t *data = va_arg(args, uint8_t*);
  119. displayer_artwork(data);
  120. break;
  121. }
  122. case RAOP_PROGRESS: {
  123. int elapsed = va_arg(args, int), duration = va_arg(args, int);
  124. displayer_timer(DISPLAYER_ELAPSED, elapsed, duration);
  125. break;
  126. }
  127. default:
  128. break;
  129. }
  130. va_end(args);
  131. return true;
  132. }
  133. /****************************************************************************************
  134. * Airplay sink de-initialization
  135. */
  136. void raop_sink_deinit(void) {
  137. raop_delete(raop);
  138. mdns_free();
  139. }
  140. /****************************************************************************************
  141. * Airplay sink startup
  142. */
  143. static bool raop_sink_start(raop_cmd_vcb_t cmd_cb, raop_data_cb_t data_cb) {
  144. const char *hostname = NULL;
  145. char sink_name[64-6] = CONFIG_AIRPLAY_NAME;
  146. tcpip_adapter_ip_info_t ipInfo = { };
  147. tcpip_adapter_if_t ifs[] = { TCPIP_ADAPTER_IF_ETH, TCPIP_ADAPTER_IF_STA, TCPIP_ADAPTER_IF_AP };
  148. // get various IP info
  149. for (int i = 0; i < sizeof(ifs) / sizeof(tcpip_adapter_if_t); i++)
  150. if (tcpip_adapter_get_ip_info(ifs[i], &ipInfo) == ESP_OK && ipInfo.ip.addr != IPADDR_ANY) {
  151. tcpip_adapter_get_hostname(ifs[i], &hostname);
  152. break;
  153. }
  154. if (!hostname) {
  155. LOG_INFO( "no hostname/IP found, can't start AirPlay");
  156. return false;
  157. }
  158. // initialize mDNS
  159. ESP_ERROR_CHECK( mdns_init() );
  160. ESP_ERROR_CHECK( mdns_hostname_set(hostname) );
  161. char * sink_name_buffer= (char *)config_alloc_get(NVS_TYPE_STR,"airplay_name");
  162. if (sink_name_buffer != NULL){
  163. memset(sink_name, 0x00, sizeof(sink_name));
  164. strncpy(sink_name,sink_name_buffer,sizeof(sink_name)-1 );
  165. free(sink_name_buffer);
  166. }
  167. LOG_INFO( "mdns hostname for ip %s set to: [%s] with servicename %s", inet_ntoa(ipInfo.ip.addr), hostname, sink_name);
  168. // create RAOP instance, latency is set by controller
  169. uint8_t mac[6];
  170. esp_read_mac(mac, ESP_MAC_WIFI_STA);
  171. cmd_handler_chain = cmd_cb;
  172. raop = raop_create(ipInfo.ip.addr, sink_name, mac, 0, cmd_handler, data_cb);
  173. return true;
  174. }
  175. /****************************************************************************************
  176. * Airplay sink timer handler
  177. */
  178. static void raop_start_handler( TimerHandle_t xTimer ) {
  179. if (raop_sink_start(raop_cbs.cmd, raop_cbs.data)) {
  180. xTimerDelete(xTimer, portMAX_DELAY);
  181. }
  182. }
  183. /****************************************************************************************
  184. * Airplay sink initialization
  185. */
  186. void raop_sink_init(raop_cmd_vcb_t cmd_cb, raop_data_cb_t data_cb) {
  187. if (!raop_sink_start(cmd_cb, data_cb)) {
  188. raop_cbs.cmd = cmd_cb;
  189. raop_cbs.data = data_cb;
  190. TimerHandle_t timer = xTimerCreate("raopStart", 5000 / portTICK_RATE_MS, pdTRUE, NULL, raop_start_handler);
  191. xTimerStart(timer, portMAX_DELAY);
  192. LOG_INFO( "Delaying AirPlay start");
  193. }
  194. }
  195. /****************************************************************************************
  196. * Airplay forced disconnection
  197. */
  198. void raop_disconnect(void) {
  199. LOG_INFO("forced disconnection");
  200. displayer_control(DISPLAYER_SHUTDOWN);
  201. // in case we can't communicate with AirPlay controller, abort session
  202. if (!raop_cmd(raop, RAOP_STOP, NULL)) raop_abort(raop);
  203. actrls_unset();
  204. }