2
0

cspot_sink.c 4.9 KB

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