raop_sink.c 4.6 KB

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