cspot_sink.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 "http_server_handlers.h"
  16. #include "tools.h"
  17. #include "cspot_private.h"
  18. #include "cspot_sink.h"
  19. char EXT_RAM_ATTR deviceId[16];
  20. static EXT_RAM_ATTR struct cspot_cb_s {
  21. cspot_cmd_vcb_t cmd;
  22. cspot_data_cb_t data;
  23. } cspot_cbs;
  24. static const char TAG[] = "cspot";
  25. static struct cspot_s *cspot;
  26. static cspot_cmd_vcb_t cmd_handler_chain;
  27. static void cspot_volume_up(bool pressed) {
  28. if (!pressed) return;
  29. cspot_cmd(cspot, CSPOT_VOLUME_UP, NULL);
  30. ESP_LOGI(TAG, "CSpot volume up");
  31. }
  32. static void cspot_volume_down(bool pressed) {
  33. if (!pressed) return;
  34. cspot_cmd(cspot, CSPOT_VOLUME_DOWN, NULL);
  35. ESP_LOGI(TAG, "CSpot volume down");
  36. }
  37. static void cspot_toggle(bool pressed) {
  38. if (!pressed) return;
  39. cspot_cmd(cspot, CSPOT_TOGGLE, NULL);
  40. ESP_LOGI(TAG, "CSpot play/pause");
  41. }
  42. static void cspot_pause(bool pressed) {
  43. if (!pressed) return;
  44. cspot_cmd(cspot, CSPOT_PAUSE, NULL);
  45. ESP_LOGI(TAG, "CSpot pause");
  46. }
  47. static void cspot_play(bool pressed) {
  48. if (!pressed) return;
  49. cspot_cmd(cspot, CSPOT_PLAY, NULL);
  50. ESP_LOGI(TAG, "CSpot play");
  51. }
  52. static void cspot_stop(bool pressed) {
  53. if (!pressed) return;
  54. cspot_cmd(cspot, CSPOT_STOP, NULL);
  55. ESP_LOGI(TAG, "CSpot stop");
  56. }
  57. static void cspot_prev(bool pressed) {
  58. if (!pressed) return;
  59. cspot_cmd(cspot, CSPOT_PREV, NULL);
  60. ESP_LOGI(TAG, "CSpot previous");
  61. }
  62. static void cspot_next(bool pressed) {
  63. if (!pressed) return;
  64. cspot_cmd(cspot, CSPOT_NEXT, NULL);
  65. ESP_LOGI(TAG, "CSpot next");
  66. }
  67. const static actrls_t controls = {
  68. NULL, // power
  69. cspot_volume_up, cspot_volume_down, // volume up, volume down
  70. cspot_toggle, cspot_play, // toggle, play
  71. cspot_pause, cspot_stop, // pause, stop
  72. NULL, NULL, // rew, fwd
  73. cspot_prev, cspot_next, // prev, next
  74. NULL, NULL, NULL, NULL, // left, right, up, down
  75. NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, // pre1-10
  76. cspot_volume_down, cspot_volume_up, cspot_toggle// knob left, knob_right, knob push
  77. };
  78. /****************************************************************************************
  79. * Download callback
  80. */
  81. void got_artwork(uint8_t* data, size_t len, void *context) {
  82. if (data) {
  83. ESP_LOGI(TAG, "got artwork of %zu bytes", len);
  84. displayer_artwork(data);
  85. free(data);
  86. } else {
  87. ESP_LOGW(TAG, "artwork error or too large %zu", len);
  88. }
  89. }
  90. /****************************************************************************************
  91. * Command handler
  92. */
  93. static bool cmd_handler(cspot_event_t event, ...) {
  94. va_list args;
  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_START:
  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_SEEK:
  118. displayer_timer(DISPLAYER_ELAPSED, va_arg(args, int), -1);
  119. break;
  120. case CSPOT_TRACK_INFO: {
  121. uint32_t duration = va_arg(args, int);
  122. uint32_t offset = va_arg(args, int);
  123. char *artist = va_arg(args, char*), *album = va_arg(args, char*), *title = va_arg(args, char*);
  124. char *artwork = va_arg(args, char*);
  125. if (artwork && displayer_can_artwork()) {
  126. ESP_LOGI(TAG, "requesting artwork %s", artwork);
  127. http_download(artwork, 128*1024, got_artwork, NULL);
  128. }
  129. displayer_metadata(artist, album, title);
  130. displayer_timer(DISPLAYER_ELAPSED, offset, duration);
  131. break;
  132. }
  133. // nothing to do on CSPOT_FLUSH
  134. default:
  135. break;
  136. }
  137. va_end(args);
  138. return true;
  139. }
  140. /****************************************************************************************
  141. * CSpot sink startup
  142. */
  143. static void cspot_sink_start(nm_state_t state_id, int sub_state) {
  144. const char *hostname;
  145. uint8_t mac[6];
  146. cmd_handler_chain = cspot_cbs.cmd;
  147. network_get_hostname(&hostname);
  148. esp_netif_get_mac(network_get_active_interface(), mac);
  149. for (int i = 0; i < 6; i++) sprintf(deviceId + 2*i, "%02x", mac[i]);
  150. ESP_LOGI(TAG, "Starting Spotify (CSpot) servicename %s with id %s", hostname, deviceId);
  151. int port;
  152. httpd_handle_t server = http_get_server(&port);
  153. cspot = cspot_create(hostname, server, port, cmd_handler, cspot_cbs.data);
  154. }
  155. /****************************************************************************************
  156. * CSpot sink initialization
  157. */
  158. void cspot_sink_init(cspot_cmd_vcb_t cmd_cb, cspot_data_cb_t data_cb) {
  159. cspot_cbs.cmd = cmd_cb;
  160. cspot_cbs.data = data_cb;
  161. network_register_state_callback(NETWORK_WIFI_ACTIVE_STATE, WIFI_CONNECTED_STATE, "cspot_sink_start", cspot_sink_start);
  162. network_register_state_callback(NETWORK_ETH_ACTIVE_STATE, ETH_ACTIVE_CONNECTED_STATE, "cspot_sink_start", cspot_sink_start);
  163. }
  164. /****************************************************************************************
  165. * CSpot forced disconnection
  166. */
  167. void cspot_disconnect(void) {
  168. ESP_LOGI(TAG, "forced disconnection");
  169. displayer_control(DISPLAYER_SHUTDOWN);
  170. cspot_cmd(cspot, CSPOT_DISC, NULL);
  171. actrls_unset();
  172. }