raop_sink.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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(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. };
  67. /****************************************************************************************
  68. * Command handler
  69. */
  70. static bool cmd_handler(raop_event_t event, ...) {
  71. bool chain = true, res = true;
  72. va_list args;
  73. va_start(args, event);
  74. switch(event) {
  75. case RAOP_SETUP:
  76. displayer_control(DISPLAYER_ACTIVATE, "AIRPLAY");
  77. break;
  78. case RAOP_PLAY:
  79. displayer_control(DISPLAYER_TIMER_RESUME);
  80. break;
  81. case RAOP_FLUSH:
  82. displayer_control(DISPLAYER_TIMER_PAUSE);
  83. break;
  84. case RAOP_STOP:
  85. displayer_control(DISPLAYER_DISABLE);
  86. break;
  87. case RAOP_METADATA: {
  88. char *artist = va_arg(args, char*), *album = va_arg(args, char*), *title = va_arg(args, char*);
  89. displayer_metadata(artist, album, title);
  90. chain = false;
  91. break;
  92. }
  93. case RAOP_PROGRESS: {
  94. u32_t elapsed = va_arg(args, u32_t), duration = va_arg(args, u32_t);
  95. displayer_timer(DISPLAYER_ELAPSED, elapsed, duration);
  96. chain = false;
  97. break;
  98. }
  99. default:
  100. break;
  101. }
  102. if (chain) res = cmd_handler_chain(event, args);
  103. va_end(args);
  104. return res;
  105. }
  106. /****************************************************************************************
  107. * Airplay taking/giving audio system's control
  108. */
  109. void raop_master(bool on) {
  110. if (on) actrls_set(controls, NULL);
  111. else actrls_unset();
  112. }
  113. /****************************************************************************************
  114. * Airplay sink de-initialization
  115. */
  116. void raop_sink_deinit(void) {
  117. raop_delete(raop);
  118. mdns_free();
  119. }
  120. /****************************************************************************************
  121. * Airplay sink initialization
  122. */
  123. void raop_sink_init(raop_cmd_vcb_t cmd_cb, raop_data_cb_t data_cb) {
  124. const char *hostname;
  125. char sink_name[64-6] = CONFIG_AIRPLAY_NAME;
  126. tcpip_adapter_ip_info_t ipInfo;
  127. struct in_addr host;
  128. // get various IP info
  129. tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_STA, &ipInfo);
  130. tcpip_adapter_get_hostname(TCPIP_ADAPTER_IF_STA, &hostname);
  131. host.s_addr = ipInfo.ip.addr;
  132. // initialize mDNS
  133. ESP_ERROR_CHECK( mdns_init() );
  134. ESP_ERROR_CHECK( mdns_hostname_set(hostname) );
  135. char * sink_name_buffer= (char *)config_alloc_get(NVS_TYPE_STR,"airplay_name");
  136. if(sink_name_buffer != NULL){
  137. memset(sink_name, 0x00, sizeof(sink_name));
  138. strncpy(sink_name,sink_name_buffer,sizeof(sink_name)-1 );
  139. free(sink_name_buffer);
  140. }
  141. LOG_INFO( "mdns hostname set to: [%s] with servicename %s", hostname, sink_name);
  142. // create RAOP instance, latency is set by controller
  143. uint8_t mac[6];
  144. esp_read_mac(mac, ESP_MAC_WIFI_STA);
  145. cmd_handler_chain = cmd_cb;
  146. raop = raop_create(host, sink_name, mac, 0, cmd_handler, data_cb);
  147. }
  148. /****************************************************************************************
  149. * Airplay forced disconnection
  150. */
  151. void raop_disconnect(void) {
  152. LOG_INFO("forced disconnection");
  153. displayer_control(DISPLAYER_SHUTDOWN);
  154. raop_cmd(raop, RAOP_STOP, NULL);
  155. actrls_unset();
  156. }