cspot_sink.c 4.7 KB

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