raop_sink.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <ctype.h>
  4. #include <stdlib.h>
  5. #include "mdns.h"
  6. #include "nvs.h"
  7. #include "tcpip_adapter.h"
  8. #include "esp_log.h"
  9. #include "esp_console.h"
  10. #include "esp_pthread.h"
  11. #include "esp_system.h"
  12. #include "freertos/timers.h"
  13. #include "config.h"
  14. #include "raop.h"
  15. #include "audio_controls.h"
  16. #include "log_util.h"
  17. #include "trace.h"
  18. #ifndef CONFIG_AIRPLAY_NAME
  19. #define CONFIG_AIRPLAY_NAME "ESP32-AirPlay"
  20. #endif
  21. log_level raop_loglevel = lINFO;
  22. log_level util_loglevel;
  23. static log_level *loglevel = &raop_loglevel;
  24. static struct raop_ctx_s *raop;
  25. static void raop_volume_up(void) {
  26. raop_cmd(raop, RAOP_VOLUME_UP, NULL);
  27. LOG_INFO("AirPlay volume up");
  28. }
  29. static void raop_volume_down(void) {
  30. raop_cmd(raop, RAOP_VOLUME_DOWN, NULL);
  31. LOG_INFO("AirPlay volume down");
  32. }
  33. static void raop_toggle(void) {
  34. raop_cmd(raop, RAOP_TOGGLE, NULL);
  35. LOG_INFO("AirPlay play/pause");
  36. }
  37. static void raop_pause(void) {
  38. raop_cmd(raop, RAOP_PAUSE, NULL);
  39. LOG_INFO("AirPlay pause");
  40. }
  41. static void raop_play(void) {
  42. raop_cmd(raop, RAOP_PLAY, NULL);
  43. LOG_INFO("AirPlay play");
  44. }
  45. static void raop_stop(void) {
  46. raop_cmd(raop, RAOP_STOP, NULL);
  47. LOG_INFO("AirPlay stop");
  48. }
  49. static void raop_prev(void) {
  50. raop_cmd(raop, RAOP_PREV, NULL);
  51. LOG_INFO("AirPlay previous");
  52. }
  53. static void raop_next(void) {
  54. raop_cmd(raop, RAOP_NEXT, NULL);
  55. LOG_INFO("AirPlay next");
  56. }
  57. const static actrls_t controls = {
  58. raop_volume_up, raop_volume_down, // volume up, volume down
  59. raop_toggle, raop_play, // toggle, play
  60. raop_pause, raop_stop, // pause, stop
  61. NULL, NULL, // rew, fwd
  62. raop_prev, raop_next, // prev, next
  63. };
  64. /****************************************************************************************
  65. * Airplay taking/giving audio system's control
  66. */
  67. void raop_master(bool on) {
  68. if (on) actrls_set(controls, NULL);
  69. else actrls_unset();
  70. }
  71. /****************************************************************************************
  72. * Airplay sink de-initialization
  73. */
  74. void raop_sink_deinit(void) {
  75. raop_delete(raop);
  76. mdns_free();
  77. }
  78. /****************************************************************************************
  79. * Airplay sink initialization
  80. */
  81. void raop_sink_init(raop_cmd_cb_t cmd_cb, raop_data_cb_t data_cb) {
  82. const char *hostname;
  83. char sink_name[64-6] = CONFIG_AIRPLAY_NAME;
  84. tcpip_adapter_ip_info_t ipInfo;
  85. struct in_addr host;
  86. // get various IP info
  87. tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_STA, &ipInfo);
  88. tcpip_adapter_get_hostname(TCPIP_ADAPTER_IF_STA, &hostname);
  89. host.s_addr = ipInfo.ip.addr;
  90. // initialize mDNS
  91. ESP_ERROR_CHECK( mdns_init() );
  92. ESP_ERROR_CHECK( mdns_hostname_set(hostname) );
  93. char * sink_name_buffer= (char *)config_alloc_get(NVS_TYPE_STR,"airplay_name");
  94. if(sink_name_buffer != NULL){
  95. memset(sink_name, 0x00, sizeof(sink_name));
  96. strncpy(sink_name,sink_name_buffer,sizeof(sink_name)-1 );
  97. free(sink_name_buffer);
  98. }
  99. LOG_INFO( "mdns hostname set to: [%s] with servicename %s", hostname, sink_name);
  100. // create RAOP instance, latency is set by controller
  101. uint8_t mac[6];
  102. esp_read_mac(mac, ESP_MAC_WIFI_STA);
  103. raop = raop_create(host, sink_name, mac, 0, cmd_cb, data_cb);
  104. }
  105. /****************************************************************************************
  106. * Airplay forced disconnection
  107. */
  108. void raop_disconnect(void) {
  109. LOG_INFO("forced disconnection");
  110. raop_cmd(raop, RAOP_STOP, NULL);
  111. actrls_unset();
  112. }