cspot_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_log.h"
  7. #include "esp_console.h"
  8. #include "esp_pthread.h"
  9. #include "esp_system.h"
  10. #include "platform_config.h"
  11. #include "audio_controls.h"
  12. #include "display.h"
  13. #include "accessors.h"
  14. #include "network_services.h"
  15. #include "tools.h"
  16. #include "cspot_private.h"
  17. #include "cspot_sink.h"
  18. char EXT_RAM_ATTR deviceId[16];
  19. static EXT_RAM_ATTR struct cspot_cb_s {
  20. cspot_cmd_vcb_t cmd;
  21. cspot_data_cb_t data;
  22. } cspot_cbs;
  23. static const char TAG[] = "cspot";
  24. static struct cspot_s *cspot;
  25. static cspot_cmd_vcb_t cmd_handler_chain;
  26. static void cspot_volume_up(bool pressed) {
  27. if (!pressed) return;
  28. cspot_cmd(cspot, CSPOT_VOLUME_UP, NULL);
  29. ESP_LOGI(TAG, "CSpot volume up");
  30. }
  31. static void cspot_volume_down(bool pressed) {
  32. if (!pressed) return;
  33. cspot_cmd(cspot, CSPOT_VOLUME_DOWN, NULL);
  34. ESP_LOGI(TAG, "CSpot volume down");
  35. }
  36. static void cspot_toggle(bool pressed) {
  37. if (!pressed) return;
  38. cspot_cmd(cspot, CSPOT_TOGGLE, NULL);
  39. ESP_LOGI(TAG, "CSpot play/pause");
  40. }
  41. static void cspot_pause(bool pressed) {
  42. if (!pressed) return;
  43. cspot_cmd(cspot, CSPOT_PAUSE, NULL);
  44. ESP_LOGI(TAG, "CSpot pause");
  45. }
  46. static void cspot_play(bool pressed) {
  47. if (!pressed) return;
  48. cspot_cmd(cspot, CSPOT_PLAY, NULL);
  49. ESP_LOGI(TAG, "CSpot play");
  50. }
  51. static void cspot_stop(bool pressed) {
  52. if (!pressed) return;
  53. cspot_cmd(cspot, CSPOT_STOP, NULL);
  54. ESP_LOGI(TAG, "CSpot stop");
  55. }
  56. static void cspot_prev(bool pressed) {
  57. if (!pressed) return;
  58. cspot_cmd(cspot, CSPOT_PREV, NULL);
  59. ESP_LOGI(TAG, "CSpot previous");
  60. }
  61. static void cspot_next(bool pressed) {
  62. if (!pressed) return;
  63. cspot_cmd(cspot, CSPOT_NEXT, NULL);
  64. ESP_LOGI(TAG, "CSpot next");
  65. }
  66. const static actrls_t controls = {
  67. NULL, // power
  68. cspot_volume_up, cspot_volume_down, // volume up, volume down
  69. cspot_toggle, cspot_play, // toggle, play
  70. cspot_pause, cspot_stop, // pause, stop
  71. NULL, NULL, // rew, fwd
  72. cspot_prev, cspot_next, // prev, next
  73. NULL, NULL, NULL, NULL, // left, right, up, down
  74. NULL, NULL, NULL, NULL, NULL, NULL, // pre1-6
  75. cspot_volume_down, cspot_volume_up, cspot_toggle// knob left, knob_right, knob push
  76. };
  77. /****************************************************************************************
  78. * Download callback
  79. */
  80. void got_artwork(uint8_t* data, size_t len, void *context) {
  81. if (data) {
  82. ESP_LOGI(TAG, "got artwork of %zu bytes", len);
  83. displayer_artwork(data);
  84. free(data);
  85. } else {
  86. ESP_LOGW(TAG, "artwork error or too large %zu", len);
  87. }
  88. }
  89. /****************************************************************************************
  90. * Command handler
  91. */
  92. static bool cmd_handler(cspot_event_t event, ...) {
  93. va_list args;
  94. static bool loaded = false;
  95. va_start(args, event);
  96. // handle audio event and stop if forbidden
  97. if (!cmd_handler_chain(event, args)) {
  98. va_end(args);
  99. return false;
  100. }
  101. // now handle events for display
  102. switch(event) {
  103. case CSPOT_SETUP:
  104. actrls_set(controls, false, NULL, actrls_ir_action);
  105. displayer_control(DISPLAYER_ACTIVATE, "SPOTIFY", true);
  106. break;
  107. case CSPOT_PLAY:
  108. displayer_control(DISPLAYER_TIMER_RUN);
  109. break;
  110. case CSPOT_PAUSE:
  111. displayer_control(DISPLAYER_TIMER_PAUSE);
  112. break;
  113. case CSPOT_DISC:
  114. actrls_unset();
  115. displayer_control(DISPLAYER_SUSPEND);
  116. break;
  117. case CSPOT_LOAD:
  118. // this message only appears if we load in the middle of a track
  119. loaded = true;
  120. __attribute__ ((fallthrough));
  121. case CSPOT_SEEK:
  122. displayer_timer(DISPLAYER_ELAPSED, va_arg(args, int), -1);
  123. break;
  124. case CSPOT_TRACK: {
  125. uint32_t sample_rate = va_arg(args, uint32_t);
  126. int duration = va_arg(args, int);
  127. char *artist = va_arg(args, char*), *album = va_arg(args, char*), *title = va_arg(args, char*);
  128. char *artwork = va_arg(args, char*);
  129. if (artwork && displayer_can_artwork()) {
  130. ESP_LOGI(TAG, "requesting artwork %s", artwork);
  131. http_download(artwork, 128*1024, got_artwork, NULL);
  132. }
  133. displayer_metadata(artist, album, title);
  134. displayer_timer(DISPLAYER_ELAPSED, loaded ? -1 : 0, duration);
  135. loaded = false;
  136. break;
  137. }
  138. // nothing to do on CSPOT_FLUSH
  139. default:
  140. break;
  141. }
  142. va_end(args);
  143. return true;
  144. }
  145. /****************************************************************************************
  146. * CSpot sink startup
  147. */
  148. static void cspot_sink_start(nm_state_t state_id, int sub_state) {
  149. const char *hostname;
  150. uint8_t mac[6];
  151. cmd_handler_chain = cspot_cbs.cmd;
  152. network_get_hostname(&hostname);
  153. esp_netif_get_mac(network_get_active_interface(), mac);
  154. for (int i = 0; i < 6; i++) sprintf(deviceId + 2*i, "%02x", mac[i]);
  155. ESP_LOGI(TAG, "Starting Spotify (CSpot) servicename %s with id %s", hostname, deviceId);
  156. cspot = cspot_create(hostname, cmd_handler, cspot_cbs.data);
  157. }
  158. /****************************************************************************************
  159. * CSpot sink initialization
  160. */
  161. void cspot_sink_init(cspot_cmd_vcb_t cmd_cb, cspot_data_cb_t data_cb) {
  162. cspot_cbs.cmd = cmd_cb;
  163. cspot_cbs.data = data_cb;
  164. network_register_state_callback(NETWORK_WIFI_ACTIVE_STATE, WIFI_CONNECTED_STATE, "cspot_sink_start", cspot_sink_start);
  165. network_register_state_callback(NETWORK_ETH_ACTIVE_STATE, ETH_ACTIVE_CONNECTED_STATE, "cspot_sink_start", cspot_sink_start);
  166. }
  167. /****************************************************************************************
  168. * CSpot forced disconnection
  169. */
  170. void cspot_disconnect(void) {
  171. ESP_LOGI(TAG, "forced disconnection");
  172. displayer_control(DISPLAYER_SHUTDOWN);
  173. cspot_cmd(cspot, CSPOT_DISC, NULL);
  174. actrls_unset();
  175. }