AdsPollingTask runs RunAdsPolling as a persistent worker thread. After an optional ads handshake, it polls the active ads endpoint every ADS_POLL_MS milliseconds. The task can be interrupted by setting control = "stop" or by toggling resetChannel when the viewer switches channels.
Unlike
AuthTask and PlaylistTask, this task does not stop automatically. It loops until control is set to "stop" or the channel is closed.XML component definition
AdsPollingTask.xml
Input fields
External identifier of the current stream (channel). Used as the
stream_id query parameter when streamPosition is not available. If both are empty the poll is skipped.Channel number of the currently playing stream (e.g.
"5"). Used as the stream_position query parameter and takes priority over streamId when building poll params. If not set the task falls back to reading m.global.currentChannelIndex from the channel list.Toggle to
true to reset the since_version snapshot and trigger an immediate poll for the new channel. The task resets the field to false after processing.Output fields
The normalised payload for the most recently received ad. Contains at minimum
media_url and format. Additional fields (ad_id, duration_ms, layout hints) depend on the server response. Observe this field to render a new ad.Full snapshot object for the current poll cycle, including
version, ads (array), server_time, next_check_at, and stream context.Toggled
false → true to signal that the current ad should be dismissed. Check clearedStreamPosition and clearedStreamId to confirm the cleared ad matches the current context.The
stream_position value from the request that triggered the clear event.The
stream_id value from the request that triggered the clear event.Toggled
false → true when the server signals that the subscriber’s account has been deactivated mid-session.Human-readable reason accompanying
userInactive, sourced from the server payload (user_inactive_reason, subscriberDisabledReason, reason, or message).Toggled
false → true when the server returns a credential or password-change error during polling, indicating the session must be re-established.Localised message accompanying
sessionInvalid (e.g. "Tu contraseña cambió. Inicia sesión nuevamente.").Function flow
Ads handshake (optional)
If
ADS_HANDSHAKE_ENABLED = true and ADS_USE_DEDICATED = true, calls TryAdsHandshake() which POSTs device info and credentials to ADS_API_BASE_URL + ADS_PATH_HANDSHAKE. Failures are logged as warnings but do not stop the task.Initial poll
Calls
DoPollResult() immediately before entering the wait loop, so ads are available as soon as the task starts.Wait loop
Waits up to
nextIntervalMs (default ADS_POLL_MS = 10 000 ms) on a message port. The port observes both control and resetChannel.Handle control messages
If
control = "stop", the task returns. If resetChannel = true, resets since_version to "" and immediately polls for the new channel’s ads.DoPollResult
Builds query params via
BuildAdsPollParams() using stream_position (preferred) and/or stream_id plus the current since_version. Calls ExecutePollWithPathFallback() which tries ADS_PATH_ACTIVE and falls back to ADS_PATH_ACTIVE_FALLBACK on 404.ProcessPollResult
Interprets the HTTP response:
- 204 — no changes; keep current
since_version - 422 with
since_versionerror — resetssince_versionto force a full fetch - Non-200 with auth error — emits
sessionInvalidoruserInactive - 200 with valid JSON — unwraps the response, normalises the ad list via
NormalizeAdsList(), builds a snapshot, and writesadDataandadsSnapshot - 200 with
ads = []— emitsadsCleared
AppConstants values used
| Constant | Value | Used for |
|---|---|---|
ADS_POLL_MS | 10000 ms | Default polling interval |
TIMEOUT_HTTP | 12000 ms | HTTP timeout for each poll request |
ADS_USE_DEDICATED | true | Routes ad requests to ADS_API_BASE_URL instead of activeServer |
ADS_API_BASE_URL | "https://ads.globaltv.lat/api/v1" | Base URL for the dedicated ads server |
ADS_PATH_ACTIVE | "/app/ads/active" | Primary endpoint path for active ads |
ADS_PATH_ACTIVE_FALLBACK | "" | Fallback path tried on 404 |
ADS_PATH_HANDSHAKE | "/app/devices/handshake" | Handshake endpoint on the ads server |
ADS_HANDSHAKE_ENABLED | true | Enables the initial ads handshake |
ADS_FLOW_DIAG | false | Enables verbose flow diagnostic logging |
Usage example
Ad normalisation
NormalizeAdCandidate() coerces vendor-specific field names into a consistent shape:
media_url resolution order
media_url resolution order
media_url → image_url → url → mediaUrl → media → image → creative.media_url → creative.url → creative.image_urlformat resolution order
format resolution order
format (object or string) → ad_format → assembled from type, position, width_percent, height_percent, fit_modead_id resolution order
ad_id resolution order
ad_id → idmedia_url is silently dropped.