raop_sink.c 4.9 KB

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