raop_sink.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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 "esp_netif.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 "platform_config.h"
  14. #include "raop.h"
  15. #include "audio_controls.h"
  16. #include "display.h"
  17. #include "accessors.h"
  18. #include "log_util.h"
  19. #ifndef CONFIG_AIRPLAY_NAME
  20. #define CONFIG_AIRPLAY_NAME "ESP32-AirPlay"
  21. #endif
  22. static EXT_RAM_ATTR struct raop_cb_s {
  23. raop_cmd_vcb_t cmd;
  24. raop_data_cb_t data;
  25. } raop_cbs;
  26. log_level raop_loglevel = lINFO;
  27. log_level util_loglevel;
  28. static log_level *loglevel = &raop_loglevel;
  29. static struct raop_ctx_s *raop;
  30. static raop_cmd_vcb_t cmd_handler_chain;
  31. static void raop_volume_up(bool pressed) {
  32. if (!pressed) return;
  33. raop_cmd(raop, RAOP_VOLUME_UP, NULL);
  34. LOG_INFO("AirPlay volume up");
  35. }
  36. static void raop_volume_down(bool pressed) {
  37. if (!pressed) return;
  38. raop_cmd(raop, RAOP_VOLUME_DOWN, NULL);
  39. LOG_INFO("AirPlay volume down");
  40. }
  41. static void raop_toggle(bool pressed) {
  42. if (!pressed) return;
  43. raop_cmd(raop, RAOP_TOGGLE, NULL);
  44. LOG_INFO("AirPlay play/pause");
  45. }
  46. static void raop_pause(bool pressed) {
  47. if (!pressed) return;
  48. raop_cmd(raop, RAOP_PAUSE, NULL);
  49. LOG_INFO("AirPlay pause");
  50. }
  51. static void raop_play(bool pressed) {
  52. if (!pressed) return;
  53. raop_cmd(raop, RAOP_PLAY, NULL);
  54. LOG_INFO("AirPlay play");
  55. }
  56. static void raop_stop(bool pressed) {
  57. if (!pressed) return;
  58. raop_cmd(raop, RAOP_STOP, NULL);
  59. LOG_INFO("AirPlay stop");
  60. }
  61. static void raop_prev(bool pressed) {
  62. if (!pressed) return;
  63. raop_cmd(raop, RAOP_PREV, NULL);
  64. LOG_INFO("AirPlay previous");
  65. }
  66. static void raop_next(bool pressed) {
  67. if (!pressed) return;
  68. raop_cmd(raop, RAOP_NEXT, NULL);
  69. LOG_INFO("AirPlay next");
  70. }
  71. const static actrls_t controls = {
  72. NULL, // power
  73. raop_volume_up, raop_volume_down, // volume up, volume down
  74. raop_toggle, raop_play, // toggle, play
  75. raop_pause, raop_stop, // pause, stop
  76. NULL, NULL, // rew, fwd
  77. raop_prev, raop_next, // prev, next
  78. NULL, NULL, NULL, NULL, // left, right, up, down
  79. NULL, NULL, NULL, NULL, NULL, NULL, // pre1-6
  80. raop_volume_down, raop_volume_up, raop_toggle// knob left, knob_right, knob push
  81. };
  82. /****************************************************************************************
  83. * Command handler
  84. */
  85. static bool cmd_handler(raop_event_t event, ...) {
  86. va_list args;
  87. va_start(args, event);
  88. // handle audio event and stop if forbidden
  89. if (!cmd_handler_chain(event, args)) {
  90. va_end(args);
  91. return false;
  92. }
  93. // now handle events for display
  94. switch(event) {
  95. case RAOP_SETUP:
  96. actrls_set(controls, false, NULL, actrls_ir_action);
  97. displayer_control(DISPLAYER_ACTIVATE, "AIRPLAY");
  98. break;
  99. case RAOP_PLAY:
  100. displayer_control(DISPLAYER_TIMER_RUN);
  101. break;
  102. case RAOP_FLUSH:
  103. displayer_control(DISPLAYER_TIMER_PAUSE);
  104. break;
  105. case RAOP_STOP:
  106. actrls_unset();
  107. displayer_control(DISPLAYER_SUSPEND);
  108. break;
  109. case RAOP_METADATA: {
  110. char *artist = va_arg(args, char*), *album = va_arg(args, char*), *title = va_arg(args, char*);
  111. displayer_metadata(artist, album, title);
  112. break;
  113. }
  114. case RAOP_PROGRESS: {
  115. int elapsed = va_arg(args, int), duration = va_arg(args, int);
  116. displayer_timer(DISPLAYER_ELAPSED, elapsed, duration);
  117. break;
  118. }
  119. default:
  120. break;
  121. }
  122. va_end(args);
  123. return true;
  124. }
  125. /****************************************************************************************
  126. * Airplay sink de-initialization
  127. */
  128. void raop_sink_deinit(void) {
  129. raop_delete(raop);
  130. mdns_free();
  131. }
  132. /****************************************************************************************
  133. * Airplay sink startup
  134. */
  135. static bool raop_sink_start(raop_cmd_vcb_t cmd_cb, raop_data_cb_t data_cb) {
  136. const char *hostname = NULL;
  137. char sink_name[64-6] = CONFIG_AIRPLAY_NAME;
  138. tcpip_adapter_ip_info_t ipInfo = { };
  139. struct in_addr host;
  140. tcpip_adapter_if_t ifs[] = { TCPIP_ADAPTER_IF_ETH, TCPIP_ADAPTER_IF_STA, TCPIP_ADAPTER_IF_AP };
  141. // get various IP info
  142. // it is possible to get the currently active interface with the following call:
  143. // network_get_active_interface()
  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. }