2
0

cspot_sink.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. va_start(args, event);
  81. // handle audio event and stop if forbidden
  82. if (!cmd_handler_chain(event, args)) {
  83. va_end(args);
  84. return false;
  85. }
  86. // now handle events for display
  87. switch(event) {
  88. case CSPOT_SETUP:
  89. actrls_set(controls, false, NULL, actrls_ir_action);
  90. displayer_control(DISPLAYER_ACTIVATE, "SPOTIFY");
  91. break;
  92. case CSPOT_PLAY:
  93. displayer_control(DISPLAYER_TIMER_RUN);
  94. break;
  95. case CSPOT_PAUSE:
  96. displayer_control(DISPLAYER_TIMER_PAUSE);
  97. break;
  98. case CSPOT_DISC:
  99. actrls_unset();
  100. displayer_control(DISPLAYER_SUSPEND);
  101. break;
  102. case CSPOT_SEEK:
  103. displayer_timer(DISPLAYER_ELAPSED, va_arg(args, int), -1);
  104. break;
  105. case CSPOT_TRACK: {
  106. uint32_t sample_rate = va_arg(args, uint32_t);
  107. char *artist = va_arg(args, char*), *album = va_arg(args, char*), *title = va_arg(args, char*);
  108. displayer_metadata(artist, album, title);
  109. displayer_timer(DISPLAYER_ELAPSED, 0, -1);
  110. break;
  111. }
  112. // nothing to do on CSPOT_FLUSH
  113. default:
  114. break;
  115. }
  116. va_end(args);
  117. return true;
  118. }
  119. /****************************************************************************************
  120. * CSpot sink startup
  121. */
  122. static void cspot_sink_start(nm_state_t state_id, int sub_state) {
  123. const char *hostname;
  124. cmd_handler_chain = cspot_cbs.cmd;
  125. network_get_hostname(&hostname);
  126. ESP_LOGI(TAG, "Starting Spotify (CSpot) servicename %s", hostname);
  127. cspot = cspot_create(hostname, cmd_handler, cspot_cbs.data);
  128. }
  129. /****************************************************************************************
  130. * CSpot sink initialization
  131. */
  132. void cspot_sink_init(cspot_cmd_vcb_t cmd_cb, cspot_data_cb_t data_cb) {
  133. cspot_cbs.cmd = cmd_cb;
  134. cspot_cbs.data = data_cb;
  135. network_register_state_callback(NETWORK_WIFI_ACTIVE_STATE, WIFI_CONNECTED_STATE, "cspot_sink_start", cspot_sink_start);
  136. network_register_state_callback(NETWORK_ETH_ACTIVE_STATE, ETH_ACTIVE_CONNECTED_STATE, "cspot_sink_start", cspot_sink_start);
  137. }
  138. /****************************************************************************************
  139. * CSpot forced disconnection
  140. */
  141. void cspot_disconnect(void) {
  142. ESP_LOGI(TAG, "forced disconnection");
  143. displayer_control(DISPLAYER_SHUTDOWN);
  144. cspot_cmd(cspot, CSPOT_DISC, NULL);
  145. actrls_unset();
  146. }