|
| 1 | +// Copyright 2026 Sendspin Contributors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +/// @file announcement_role.h |
| 16 | +/// @brief Announcement role that decodes short per-client audio clips (TTS, chimes, alerts) |
| 17 | +/// concurrently with the media stream, with ducking hints for the embedder |
| 18 | + |
| 19 | +#pragma once |
| 20 | + |
| 21 | +#include "sendspin/config.h" |
| 22 | +#include "sendspin/player_role.h" // ServerPlayerStreamObject (shared codec-parameter shape) |
| 23 | +#include "sendspin/types.h" |
| 24 | + |
| 25 | +#include <cstddef> |
| 26 | +#include <cstdint> |
| 27 | +#include <memory> |
| 28 | +#include <optional> |
| 29 | + |
| 30 | +namespace sendspin { |
| 31 | + |
| 32 | +class SendspinClient; |
| 33 | + |
| 34 | +// ============================================================================ |
| 35 | +// Announcement types |
| 36 | +// ============================================================================ |
| 37 | + |
| 38 | +/// @brief Announcement stream parameters sent by the server in stream/start messages |
| 39 | +/// |
| 40 | +/// The codec parameters share the player stream-object shape; the additional fields carry the |
| 41 | +/// per-announcement ducking policy and optional output level. |
| 42 | +struct ServerAnnouncementStreamObject { |
| 43 | + /// @brief Default ducking ramp duration in milliseconds, used when the server omits the field |
| 44 | + static constexpr uint16_t DEFAULT_DUCK_RAMP_MS = 100U; |
| 45 | + |
| 46 | + ServerPlayerStreamObject format{}; |
| 47 | + |
| 48 | + /// Server clock time in microseconds when announcement output should begin. The client |
| 49 | + /// translates it via clock synchronization and starts at that time, or as soon as possible |
| 50 | + /// if it has already passed. The same value on several clients gives a coordinated |
| 51 | + /// multi-speaker start. Required. |
| 52 | + int64_t start_timestamp{0}; |
| 53 | + |
| 54 | + /// Reduction in decibel (0-50) to apply to this client's own media output while the |
| 55 | + /// announcement stream is active. 0 means no ducking. |
| 56 | + uint8_t media_duck_db{0}; |
| 57 | + |
| 58 | + /// Ramp duration in milliseconds (0-2000) for both applying and releasing the ducking. |
| 59 | + uint16_t duck_ramp_ms{DEFAULT_DUCK_RAMP_MS}; |
| 60 | + |
| 61 | + /// When set, the announcement should be rendered at the loudness that master volume |
| 62 | + /// `volume` (0-100) would produce, regardless of the current master volume. When absent, |
| 63 | + /// the announcement follows the current master volume. |
| 64 | + std::optional<uint8_t> volume{}; |
| 65 | + |
| 66 | + /// When true, a muted client still renders the announcement while its media stays muted. |
| 67 | + bool override_mute{false}; |
| 68 | + |
| 69 | + bool is_complete() const { |
| 70 | + return this->format.is_complete(); |
| 71 | + } |
| 72 | +}; |
| 73 | + |
| 74 | +/// @brief Listener for announcement role events |
| 75 | +/// |
| 76 | +/// THREAD SAFETY: on_announcement_write() fires on the announcement task's background thread. |
| 77 | +/// Implementations must be thread-safe for this method. on_announcement_start() and |
| 78 | +/// on_announcement_end() fire on the main loop thread via drain_events(). The listener must |
| 79 | +/// outlive the role. |
| 80 | +class AnnouncementRoleListener { |
| 81 | +public: |
| 82 | + virtual ~AnnouncementRoleListener() = default; |
| 83 | + |
| 84 | + /// @brief Writes decoded announcement PCM audio to the platform's announcement output |
| 85 | + /// |
| 86 | + /// Fires on the announcement task's background thread. May block up to timeout_ms; the |
| 87 | + /// blocking write is what paces the decode loop against the sink. |
| 88 | + /// @param data Pointer to the decoded PCM audio data |
| 89 | + /// @param length Number of bytes to write; always a whole number of PCM frames |
| 90 | + /// @param timeout_ms Maximum time to wait for the write to complete |
| 91 | + /// @return Number of bytes actually written; partial writes must be whole PCM frames |
| 92 | + virtual size_t on_announcement_write(uint8_t* data, size_t length, uint32_t timeout_ms) = 0; |
| 93 | + |
| 94 | + /// @brief Called when an announcement stream starts. Fires on the main loop thread |
| 95 | + /// |
| 96 | + /// The embedder applies the ducking policy here (e.g. duck the media pipeline by |
| 97 | + /// `params.media_duck_db` over `params.duck_ramp_ms`) and honors `params.override_mute` and |
| 98 | + /// `params.volume`. Also fires when the server re-sends `stream/start` to update the active |
| 99 | + /// announcement's config (duck level, volume, or override_mute): the embedder re-applies the |
| 100 | + /// updated policy, ramping a new `media_duck_db` from the current gain, without any |
| 101 | + /// intervening on_announcement_end(). |
| 102 | + virtual void on_announcement_start(const ServerAnnouncementStreamObject& /*params*/) {} |
| 103 | + |
| 104 | + /// @brief Called when the announcement stream ends (normally, aborted, or on transport |
| 105 | + /// loss). Fires on the main loop thread. The embedder releases the ducking here |
| 106 | + virtual void on_announcement_end() {} |
| 107 | +}; |
| 108 | + |
| 109 | +/** |
| 110 | + * @brief Announcement role that decodes short per-client audio clips next to the media stream |
| 111 | + * |
| 112 | + * Owns an AnnouncementTask that runs on a background thread. Encoded announcement chunks |
| 113 | + * arrive from the WebSocket network thread, are written into a dedicated ring buffer, decoded, |
| 114 | + * and delivered to the platform through AnnouncementRoleListener::on_announcement_write(). |
| 115 | + * Unlike the player role there is no sample-accurate sync machinery: announcements are |
| 116 | + * per-client, start at (or as soon as possible after) the stream's single `start_timestamp` - |
| 117 | + * which is also what aligns a coordinated multi-speaker announcement scheduled by the server - |
| 118 | + * and are then paced by the sink. |
| 119 | + * |
| 120 | + * Usage: |
| 121 | + * 1. Implement AnnouncementRoleListener with at minimum on_announcement_write() |
| 122 | + * 2. Add the role to the client via SendspinClient::add_announcement() |
| 123 | + * 3. Call set_listener() with your listener implementation |
| 124 | + * |
| 125 | + * @code |
| 126 | + * struct MyAnnouncementListener : AnnouncementRoleListener { |
| 127 | + * size_t on_announcement_write(uint8_t* data, size_t len, uint32_t timeout_ms) override { |
| 128 | + * return announcement_output.write(data, len, timeout_ms); |
| 129 | + * } |
| 130 | + * void on_announcement_start(const ServerAnnouncementStreamObject& params) override { |
| 131 | + * media_mixer.apply_ducking(params.media_duck_db, params.duck_ramp_ms); |
| 132 | + * } |
| 133 | + * void on_announcement_end() override { media_mixer.apply_ducking(0, ramp_ms); } |
| 134 | + * }; |
| 135 | + * |
| 136 | + * MyAnnouncementListener listener; |
| 137 | + * AnnouncementRoleConfig config; |
| 138 | + * config.audio_formats = {{SendspinCodecFormat::PCM, 1, 48000, 16}}; |
| 139 | + * auto& announcement = client.add_announcement(config); |
| 140 | + * announcement.set_listener(&listener); |
| 141 | + * @endcode |
| 142 | + */ |
| 143 | +class AnnouncementRole { |
| 144 | + friend class SendspinClient; |
| 145 | + |
| 146 | +public: |
| 147 | + struct Impl; |
| 148 | + |
| 149 | + AnnouncementRole(AnnouncementRoleConfig config, SendspinClient* client); |
| 150 | + ~AnnouncementRole(); |
| 151 | + |
| 152 | + /// @brief Sets the listener for announcement events |
| 153 | + /// @param listener Pointer to the listener implementation; must outlive this role |
| 154 | + void set_listener(AnnouncementRoleListener* listener); |
| 155 | + |
| 156 | + // ======================================== |
| 157 | + // Queries |
| 158 | + // ======================================== |
| 159 | + |
| 160 | + /// @brief Returns a reference to the current announcement stream parameters |
| 161 | + /// @return Const reference to the active announcement stream parameters. |
| 162 | + const ServerAnnouncementStreamObject& get_current_stream_params() const; |
| 163 | + |
| 164 | + /// @brief Returns true if announcement audio is currently being output |
| 165 | + /// @return true while the announcement pipeline is playing, false otherwise. |
| 166 | + bool is_playing() const; |
| 167 | + |
| 168 | +private: |
| 169 | + std::unique_ptr<Impl> impl_; |
| 170 | +}; |
| 171 | + |
| 172 | +} // namespace sendspin |
0 commit comments