raop_sink.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <ctype.h>
  4. #include <stdlib.h>
  5. #include "nvs.h"
  6. #include "esp_netif.h"
  7. #include "esp_log.h"
  8. #include "esp_console.h"
  9. #include "esp_pthread.h"
  10. #include "esp_system.h"
  11. #include "freertos/timers.h"
  12. #include "platform_config.h"
  13. #include "raop.h"
  14. #include "audio_controls.h"
  15. #include "display.h"
  16. #include "accessors.h"
  17. #include "log_util.h"
  18. #include "network_services.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, NULL, NULL, NULL, NULL, // pre1-10
  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", true);
  98. displayer_artwork(NULL);
  99. break;
  100. case RAOP_PLAY:
  101. displayer_control(DISPLAYER_TIMER_RUN);
  102. break;
  103. case RAOP_FLUSH:
  104. displayer_control(DISPLAYER_TIMER_PAUSE);
  105. break;
  106. case RAOP_STALLED:
  107. raop_abort(raop);
  108. actrls_unset();
  109. displayer_control(DISPLAYER_SHUTDOWN);
  110. break;
  111. case RAOP_STOP:
  112. actrls_unset();
  113. displayer_control(DISPLAYER_SUSPEND);
  114. break;
  115. case RAOP_METADATA: {
  116. char *artist = va_arg(args, char*), *album = va_arg(args, char*), *title = va_arg(args, char*);
  117. displayer_metadata(artist, album, title);
  118. break;
  119. }
  120. case RAOP_ARTWORK: {
  121. uint8_t *data = va_arg(args, uint8_t*);
  122. displayer_artwork(data);
  123. break;
  124. }
  125. case RAOP_PROGRESS: {
  126. int elapsed = va_arg(args, int), duration = va_arg(args, int);
  127. displayer_timer(DISPLAYER_ELAPSED, elapsed, duration);
  128. break;
  129. }
  130. default:
  131. break;
  132. }
  133. va_end(args);
  134. return true;
  135. }
  136. /****************************************************************************************
  137. * Airplay sink de-initialization
  138. */
  139. void raop_sink_deinit(void) {
  140. raop_delete(raop);
  141. }
  142. /****************************************************************************************
  143. * Airplay sink startup
  144. */
  145. static void raop_sink_start(nm_state_t state_id, int sub_state) {
  146. esp_netif_t* netif;
  147. esp_netif_ip_info_t ipInfo = { };
  148. uint8_t mac[6];
  149. char* sink_name = (char*) config_alloc_get_default(NVS_TYPE_STR, "airplay_name", CONFIG_AIRPLAY_NAME, 0);
  150. netif = network_get_active_interface();
  151. esp_netif_get_ip_info(netif, &ipInfo);
  152. esp_netif_get_mac(netif, mac);
  153. cmd_handler_chain = raop_cbs.cmd;
  154. LOG_INFO( "starting Airplay for ip %s with servicename %s", inet_ntoa(ipInfo.ip.addr), sink_name);
  155. raop = raop_create(ipInfo.ip.addr, sink_name, mac, 0, cmd_handler, raop_cbs.data);
  156. free(sink_name);
  157. }
  158. /****************************************************************************************
  159. * Airplay sink initialization
  160. */
  161. void raop_sink_init(raop_cmd_vcb_t cmd_cb, raop_data_cb_t data_cb) {
  162. raop_cbs.cmd = cmd_cb;
  163. raop_cbs.data = data_cb;
  164. network_register_state_callback(NETWORK_WIFI_ACTIVE_STATE, WIFI_CONNECTED_STATE, "raop_sink_start", raop_sink_start);
  165. network_register_state_callback(NETWORK_ETH_ACTIVE_STATE, ETH_ACTIVE_CONNECTED_STATE, "raop_sink_start", raop_sink_start);
  166. }
  167. /****************************************************************************************
  168. * Airplay forced disconnection
  169. */
  170. void raop_disconnect(void) {
  171. LOG_INFO("forced disconnection");
  172. // in case we can't communicate with AirPlay controller, abort session
  173. if (!raop_cmd(raop, RAOP_STOP, NULL)) cmd_handler(RAOP_STALLED);
  174. else displayer_control(DISPLAYER_SHUTDOWN);
  175. }