esp32_main.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. * Squeezelite for esp32
  3. *
  4. * (c) Sebastien 2024
  5. * Philippe G. 2024, philippe_44@outlook.com
  6. *
  7. * This software is released under the MIT License.
  8. * https://opensource.org/licenses/MIT
  9. *
  10. */
  11. #include "Config.h"
  12. #include "squeezelite.h"
  13. #include <signal.h>
  14. extern bool user_rates;
  15. static unsigned int rates[MAX_SUPPORTED_SAMPLERATES] = {0};
  16. sys_squeezelite_config* config;
  17. log_level loglevel = lDEBUG;
  18. static void sighandler(int signum) {
  19. slimproto_stop();
  20. // remove ourselves in case above does not work, second SIGINT will cause non gracefull shutdown
  21. signal(signum, SIG_DFL);
  22. }
  23. unsigned int* get_rates() {
  24. unsigned int ref[] TEST_RATES;
  25. sys_squeezelite_rates_opt* ratescfg = &config->rates;
  26. if (!config->has_rates || ((ratescfg->list_count == 0 || ratescfg->list[0] == 0) &&
  27. ratescfg->min == 0 && ratescfg->max == 0)) {
  28. user_rates = false;
  29. return rates;
  30. }
  31. if (ratescfg->list_count > 0 && ratescfg->list[0] != 0) {
  32. // Sort the rates from the list
  33. for (int i = 0; i < ratescfg->list_count && i < MAX_SUPPORTED_SAMPLERATES; ++i) {
  34. rates[i] = ratescfg->list[i];
  35. }
  36. // Sort logic here if needed
  37. } else {
  38. // Use min and max to determine rates
  39. unsigned int min = ratescfg->min;
  40. unsigned int max = ratescfg->max;
  41. if (max < min) {
  42. unsigned int tmp = max;
  43. max = min;
  44. min = tmp;
  45. }
  46. for (int i = 0, j = 0; i < MAX_SUPPORTED_SAMPLERATES; ++i) {
  47. if (ref[i] <= max && ref[i] >= min) {
  48. rates[j++] = ref[i];
  49. }
  50. }
  51. }
  52. user_rates = true;
  53. return rates;
  54. }
  55. log_level log_level_from_sys_level(sys_squeezelite_debug_levels level) {
  56. switch (level) {
  57. case sys_squeezelite_debug_levels_DEFAULT:
  58. return lWARN;
  59. break;
  60. case sys_squeezelite_debug_levels_INFO:
  61. return lINFO;
  62. break;
  63. case sys_squeezelite_debug_levels_ERROR:
  64. return lERROR;
  65. break;
  66. case sys_squeezelite_debug_levels_WARN:
  67. return lWARN;
  68. break;
  69. case sys_squeezelite_debug_levels_DEBUG:
  70. return lDEBUG;
  71. break;
  72. case sys_squeezelite_debug_levels_SDEBUG:
  73. return lSDEBUG;
  74. break;
  75. default:
  76. return lWARN;
  77. }
  78. }
  79. void build_codec_string(sys_squeezelite_codecs* list, size_t count, char* buffer, size_t buf_size) {
  80. const char* prefix = STR(sys_squeezelite_codecs) "_c_";
  81. const char* name = NULL;
  82. for (int i = 0; i < count; i++) {
  83. if (i > 0) {
  84. strncat(buffer, ", ", buf_size);
  85. }
  86. name = sys_squeezelite_codecs_name(list[i]) + strlen(prefix);
  87. LOG_INFO("Found codec: %s ", name);
  88. strncat(buffer, name, buf_size);
  89. }
  90. LOG_INFO("Codec list: %s ", buffer);
  91. }
  92. sys_squeezelite_config* get_profile(const char* name) {
  93. const char* resolved_name = name && strlen(name) > 0 ? name : platform->services.current_profile;
  94. LOG_DEBUG("get_profile called with name: %s, resolved to: %s",
  95. name ? name : "NULL",
  96. resolved_name ? resolved_name : "NULL or EMPTY");
  97. if (!platform->has_services || platform->services.squeezelite_profiles_count <= 0) {
  98. LOG_ERROR("No squeezelite profiles available");
  99. return NULL;
  100. }
  101. if (!resolved_name || strlen(resolved_name) == 0) {
  102. LOG_WARN("No specific profile requested, using the first available profile");
  103. return &platform->services.squeezelite_profiles[0].profile;
  104. }
  105. for (int i = 0; i < platform->services.squeezelite_profiles_count; i++) {
  106. LOG_DEBUG("Checking profile: %s", platform->services.squeezelite_profiles[i].name);
  107. if (strcmp(platform->services.squeezelite_profiles[i].name, resolved_name) == 0) {
  108. LOG_DEBUG("Profile matched: %s", platform->services.squeezelite_profiles[i].name);
  109. return &platform->services.squeezelite_profiles[i].profile;
  110. }
  111. }
  112. if(platform->services.squeezelite_profiles[0].name && strlen(platform->services.squeezelite_profiles[0].name)>0){
  113. LOG_WARN("Could not find profile %s. Falling back to %s", resolved_name,platform->services.squeezelite_profiles[0].name);
  114. return &platform->services.squeezelite_profiles[0].profile;
  115. }
  116. LOG_ERROR("Profile '%s' not found", resolved_name);
  117. return NULL;
  118. }
  119. int squeezelite_main_start() {
  120. u8_t mac[6];
  121. unsigned output_buf_size = 0;
  122. LOG_INFO("Starting squeezelite");
  123. char include_codecs[101] = {0};
  124. char exclude_codecs[101] = {0};
  125. config = get_profile(NULL); // get the active profile
  126. if (!config) {
  127. LOG_ERROR("Squeezelite not configured");
  128. return -2;
  129. }
  130. LOG_INFO("Running embedded init");
  131. int err = embedded_init();
  132. if (err) return err;
  133. get_mac(mac);
  134. unsigned int* rates = get_rates();
  135. signal(SIGINT, sighandler);
  136. signal(SIGTERM, sighandler);
  137. #if defined(SIGQUIT)
  138. signal(SIGQUIT, sighandler);
  139. #endif
  140. #if defined(SIGHUP)
  141. signal(SIGHUP, sighandler);
  142. #endif
  143. output_buf_size = config->buffers.output*1024;
  144. // set the output buffer size if not specified in the parameters, take account of resampling
  145. if (output_buf_size == 0) {
  146. output_buf_size = OUTPUTBUF_SIZE;
  147. if (strlen(config->resample) > 0) {
  148. unsigned scale = 8;
  149. if (rates[0]) {
  150. scale = rates[0] / 44100;
  151. if (scale > 8) scale = 8;
  152. if (scale < 1) scale = 1;
  153. }
  154. output_buf_size *= scale;
  155. }
  156. }
  157. build_codec_string(config->excluded_codex, config->excluded_codex_count, exclude_codecs,
  158. sizeof(exclude_codecs));
  159. build_codec_string(config->included_codex, config->included_codex_count, include_codecs,
  160. sizeof(include_codecs));
  161. unsigned int stream_buf_size =STREAMBUF_SIZE;
  162. if(config->buffers.stream > 0){
  163. stream_buf_size = config->buffers.stream *1024;
  164. LOG_DEBUG("Found stream buffer configuration: %d (%d)",config->buffers.stream,stream_buf_size);
  165. }
  166. else {
  167. LOG_DEBUG("Using default stream buffer: %d",stream_buf_size);
  168. }
  169. stream_init(log_level_from_sys_level(config->log.stream), stream_buf_size);
  170. output_init_embedded();
  171. decode_init(log_level_from_sys_level(config->log.decode),
  172. strlen(include_codecs) > 0 ? include_codecs : NULL, exclude_codecs);
  173. #if RESAMPLE || RESAMPLE16
  174. if (config->resample && strlen(config->resample) > 0) {
  175. process_init(config->resample);
  176. }
  177. #endif
  178. if (!config->enabled) {
  179. LOG_WARN("LMS is disabled");
  180. while (1)
  181. sleep(3600);
  182. }
  183. char* name = strlen(platform->names.squeezelite) > 0 ? platform->names.squeezelite
  184. : platform->names.device;
  185. slimproto(log_level_from_sys_level(config->log.slimproto), config->server_name_ip, mac, name,
  186. NULL, NULL, config->max_rate);
  187. decode_close();
  188. stream_close();
  189. output_close_embedded();
  190. return (0);
  191. }