|
13 | 13 | // limitations under the License. |
14 | 14 |
|
15 | 15 | #include "connection.h" |
| 16 | +#include "inline_text.h" |
16 | 17 | #include "platform/json_arena.h" |
17 | 18 | #include "platform/logging.h" |
18 | 19 | #include "protocol_messages.h" |
|
22 | 23 | #include "time_filter.h" |
23 | 24 |
|
24 | 25 | #include <algorithm> |
| 26 | +#include <array> |
25 | 27 | #include <cinttypes> |
| 28 | +#include <cstring> |
26 | 29 | #include <iterator> |
27 | 30 |
|
28 | 31 | static const char* const TAG = "sendspin.source"; |
@@ -125,7 +128,17 @@ SourceRole::Impl::Impl(SourceRoleConfig config, SendspinClient* client) |
125 | 128 | : config(config), |
126 | 129 | client(client), |
127 | 130 | task(std::make_unique<SourceTask>(this)), |
128 | | - config_valid(validate_config(config)) {} |
| 131 | + config_valid(validate_config(config)) { |
| 132 | + if (!config.metadata) { |
| 133 | + return; |
| 134 | + } |
| 135 | + auto block = std::make_unique<SourceMetadataBlock>(); |
| 136 | + if (!block->text.allocate(SourceMetadataBlock::OFFSETS.back(), config.buffer_location)) { |
| 137 | + SS_LOGE(TAG, "Failed to reserve the source metadata block; set_metadata() is refused"); |
| 138 | + return; |
| 139 | + } |
| 140 | + this->metadata = std::move(block); |
| 141 | +} |
129 | 142 |
|
130 | 143 | SourceRole::Impl::~Impl() { |
131 | 144 | // Joined first, while the role it reads its config and gate through is still whole |
@@ -206,6 +219,14 @@ void SourceRole::set_signal(SourceSignal signal) { |
206 | 219 | this->impl_->set_signal(signal); |
207 | 220 | } |
208 | 221 |
|
| 222 | +bool SourceRole::set_metadata(const SourceMetadata& metadata) { |
| 223 | + return this->impl_->set_metadata(metadata); |
| 224 | +} |
| 225 | + |
| 226 | +void SourceRole::clear_metadata() { |
| 227 | + this->impl_->clear_metadata(); |
| 228 | +} |
| 229 | + |
209 | 230 | bool SourceRole::is_streaming() const { |
210 | 231 | return this->impl_->streaming_active; |
211 | 232 | } |
@@ -400,6 +421,99 @@ bool SourceRole::Impl::write_audio(const uint8_t* data, size_t len, int64_t capt |
400 | 421 | return this->task->write_audio(data, len, capture_time_us); |
401 | 422 | } |
402 | 423 |
|
| 424 | +bool SourceRole::Impl::set_metadata(const SourceMetadata& report) { |
| 425 | + if (!this->config.metadata) { |
| 426 | + SS_LOGW(TAG, "set_metadata() ignored: metadata not configured"); |
| 427 | + return false; |
| 428 | + } |
| 429 | + if (!this->metadata) { |
| 430 | + return false; |
| 431 | + } |
| 432 | + const std::array<const std::optional<std::string_view>*, SOURCE_METADATA_TEXT_FIELDS> fields{ |
| 433 | + &report.title, &report.artist, &report.album_artist, &report.album, &report.artwork_url}; |
| 434 | + for (const std::optional<std::string_view>* field : fields) { |
| 435 | + if (field->has_value() && !is_valid_utf8(field->value())) { |
| 436 | + SS_LOGW(TAG, "set_metadata() refused: a text field is not valid UTF-8"); |
| 437 | + return false; |
| 438 | + } |
| 439 | + } |
| 440 | + if (report.track == 0) { |
| 441 | + SS_LOGW(TAG, "set_metadata() refused: track must be at least 1"); |
| 442 | + return false; |
| 443 | + } |
| 444 | + { |
| 445 | + SourceMetadataBlock& block = *this->metadata; |
| 446 | + const std::lock_guard<std::mutex> lock(block.mutex); |
| 447 | + for (size_t i = 0; i < SOURCE_METADATA_TEXT_FIELDS; ++i) { |
| 448 | + const auto field = static_cast<SourceMetadataText>(i); |
| 449 | + block.present[i] = fields[i]->has_value(); |
| 450 | + const std::string_view kept = |
| 451 | + block.present[i] |
| 452 | + ? utf8_truncate(fields[i]->value(), SourceMetadataBlock::capacity(field)) |
| 453 | + : std::string_view{}; |
| 454 | + std::memcpy(block.text.data() + SourceMetadataBlock::OFFSETS[i], kept.data(), |
| 455 | + kept.size()); |
| 456 | + block.sizes[i] = static_cast<uint16_t>(kept.size()); |
| 457 | + } |
| 458 | + block.timestamp_us = |
| 459 | + report.timestamp_us != 0 ? report.timestamp_us : SendspinClient::now_us(); |
| 460 | + block.progress = report.progress; |
| 461 | + block.year = report.year; |
| 462 | + block.track = report.track; |
| 463 | + block.set = true; |
| 464 | + } |
| 465 | + this->client->publish_state(); |
| 466 | + return true; |
| 467 | +} |
| 468 | + |
| 469 | +void SourceRole::Impl::clear_metadata() { |
| 470 | + if (!this->metadata) { |
| 471 | + return; |
| 472 | + } |
| 473 | + { |
| 474 | + const std::lock_guard<std::mutex> lock(this->metadata->mutex); |
| 475 | + if (!this->metadata->set) { |
| 476 | + return; |
| 477 | + } |
| 478 | + this->metadata->set = false; |
| 479 | + } |
| 480 | + this->client->publish_state(); |
| 481 | +} |
| 482 | + |
| 483 | +void SourceRole::Impl::append_state_metadata(JsonObject source, |
| 484 | + const SendspinTimeFilter& filter) const { |
| 485 | + if (!this->metadata) { |
| 486 | + return; |
| 487 | + } |
| 488 | + static constexpr std::array<const char*, SOURCE_METADATA_TEXT_FIELDS> KEYS{ |
| 489 | + "title", "artist", "album_artist", "album", "artwork_url"}; |
| 490 | + const SourceMetadataBlock& block = *this->metadata; |
| 491 | + // ArduinoJson copies each string_view into the document, so nothing outlives the lock. |
| 492 | + const std::lock_guard<std::mutex> lock(block.mutex); |
| 493 | + if (!block.set) { |
| 494 | + return; |
| 495 | + } |
| 496 | + JsonObject out = source["metadata"].to<JsonObject>(); |
| 497 | + out["timestamp"] = filter.compute_server_time(block.timestamp_us); |
| 498 | + for (size_t i = 0; i < SOURCE_METADATA_TEXT_FIELDS; ++i) { |
| 499 | + if (block.present[i]) { |
| 500 | + out[KEYS[i]] = block.view(static_cast<SourceMetadataText>(i)); |
| 501 | + } |
| 502 | + } |
| 503 | + if (block.year.has_value()) { |
| 504 | + out["year"] = block.year.value(); |
| 505 | + } |
| 506 | + if (block.track.has_value()) { |
| 507 | + out["track"] = block.track.value(); |
| 508 | + } |
| 509 | + if (block.progress.has_value()) { |
| 510 | + JsonObject progress = out["progress"].to<JsonObject>(); |
| 511 | + progress["track_progress"] = block.progress->track_progress_ms; |
| 512 | + progress["track_duration"] = block.progress->track_duration_ms; |
| 513 | + progress["playback_speed"] = block.progress->playback_speed; |
| 514 | + } |
| 515 | +} |
| 516 | + |
403 | 517 | void SourceRole::Impl::set_signal(SourceSignal new_signal) { |
404 | 518 | if (!this->config.line_sense) { |
405 | 519 | SS_LOGW(TAG, "set_signal() ignored: line_sense not configured"); |
|
0 commit comments