Publishing and Subscribing with the IVS Linux Broadcast SDK | Real-Time Streaming
This document takes you through the steps involved in publishing and subscribing to a stage using the IVS real-time streaming Linux broadcast SDK.
Concepts
Three core concepts underlie real-time functionality: stage, strategy, and hooks. The design goal is minimizing the amount of client-side logic necessary to build a working product.
Stage
The stage is the main point of interaction between your application and
the SDK. It represents the stage itself, and you use it to join and leave the
stage. Creating and joining a stage requires a valid, unexpired token string
from the control plane (represented as token). Joining and leaving
a stage are simple.
join returns when the connection attempt starts, not when it
succeeds. The SDK reports the progress through the connection state hook. After
leave, the same stage can join again.
You give the stage its strategy and its hooks when you create it. The stage
keeps both until you destroy it. To receive no events, pass null
for the callback table in C, or leave Hooks empty in Go.
Strategy
The strategy provides a way for the host application to communicate the
desired state of the stage to the SDK. Three functions need to be implemented:
subscription_types, should_publish, and
streams_to_publish (in Go: SubscriptionTypes,
ShouldPublish, and StreamsToPublish). All are
discussed below.
In C, fill in an aws_ivs_realtime_stage_strategy_callbacks table
and create a strategy handle from it. Set size to the size of the
table and supply all three callbacks. The SDK copies the table, so later changes
to it have no effect. In Go, implement the StageStrategy
interface.
Subscribing to participants
When a remote participant joins the stage, the SDK queries the host
application about the desired subscription state for that participant. Return
AUDIO to subscribe to the audio of the participant, or
NONE to not subscribe. When returning a value for this function,
you do not need to worry about the publish state, current
subscription state, or stage connection state. If AUDIO is
returned, the SDK waits until the remote participant is publishing before
subscribing, and it updates the host application through the hooks throughout
the process.
The SDK does not support subscribing to or publishing video yet.
Here is a sample implementation:
Use this implementation when your host application needs to hear every participant, such as a recording service.
For more information about subscribing based on participant attributes, see Get Participant Attributes.
Publishing
Once connected to the stage, the SDK queries the host application to see if the local participant should publish. This is invoked only on local participants that have permission to publish based on the provided token.
Here is a sample implementation:
This is for an application that always publishes, such as a bot that plays
audio into the stage. To stop publishing, return false and
refresh the strategy.
Choosing streams to publish
When publishing, streams_to_publish determines what audio
should be published. In C, name the audio source on the streams collector
with aws_ivs_realtime_publish_streams_set_audio. Do not store
the collector. In Go, return the source in a PublishStreams
value.
The SDK compares the streams you name with the streams it already publishes. A stream it already publishes stays as is, and a stream you leave out stops. For more information about publishing audio, see Publish Audio.
Updating the strategy
The strategy is intended to be dynamic: the values returned from any of the
above functions can be changed at any time. For example, if the host
application does not want to publish until it receives a command from your
backend, you could return a variable from should_publish
(something like publish_requested). When that variable changes,
call aws_ivs_realtime_stage_refresh_strategy (Go:
Stage.RefreshStrategy) to signal to the SDK that it should query
the strategy for the latest values, applying only things that have
changed.
If the SDK observes that the should_publish value has changed,
it starts the publish process. If the SDK queries and all functions
return the same value as before, the refresh call does not modify the
stage.
The SDK calls the strategy on its own thread, the same thread that calls the hooks. The command arrives on another thread, so the samples use an atomic flag.
If the return value of subscription_types changes from
AUDIO to NONE, the SDK unsubscribes from that
participant.
Generally, the stage uses the strategy to most efficiently apply the difference between the previous and current strategies, without the host application needing to worry about all the state required to manage it properly. Because of this, think of refreshing the strategy as a cheap operation, because it does nothing unless the strategy changes. You can refresh the strategy from inside a hook or strategy callback.
Hooks
The hooks communicate the state of the stage to the host application. In C,
they are the function pointers in an
aws_ivs_realtime_stage_event_callbacks table. In Go, they are the
fields of StageHooks. Every hook is optional: leave it
null in C, or nil in Go, to skip that event. The hooks
are:
It is not expected that the information provided by the hooks impacts the
return values of the strategy. For example, the return value of
subscription_types is not expected to change when the publish state
of a participant changes. If the host application wants to subscribe to a
particular participant, it should return the desired subscription type
regardless of that participant's publish state. The SDK is responsible for
ensuring that the desired state of the strategy is acted on at the correct time
based on the state of the stage.
The hooks are attached to the stage when you create it:
Only publishing participants trigger the participant joined hook. When a participant stops publishing or leaves the stage session, the SDK triggers the participant left hook. The local participant triggers the participant joined hook when the stage connects.
Publish Audio
The host application supplies the audio it publishes. Create an audio source that
describes the audio you will push: the sample rate, the channel count, and the
Pulse Code Modulation (PCM) format. Name the source in
streams_to_publish, then push PCM to it as
you produce it.
Push audio in real time, as you produce it, in buffers of 10 ms to 100 ms. The SDK does not pace audio, and it drops audio that you push faster than real time. For the supported formats and the other rules of the audio source, see the API documentation.
To stop publishing audio, leave the source out of
streams_to_publish and refresh the strategy.
Receive Participant Audio
The SDK delivers subscribed audio as PCM, in one of two ways:
-
Mixed audio: One callback receives the audio of all subscribed participants, mixed together.
-
Audio for each participant: A callback on each audio stream receives the audio of that participant only, before mixing.
You can use both at the same time.
Mixed audio
Register the mixed audio callback before you join the stage, or after you leave it. The registration stays across leave and join.
For the format and lifetime of the audio data, see the API documentation of the audio callback.
Audio for each participant
Register a callback on each audio stream in the streams added hook. The registration lasts as long as the stream. When a stream ends, the SDK reports it through the streams removed hook. A replacement stream has a new stream ID and needs its own registration.
The registration stays active after the hook returns, so you do not need to keep the stream to keep receiving its audio.
To tell streams apart, compare their stream IDs (C:
aws_ivs_realtime_stage_stream_get_stream_id, Go:
StreamID). A stream ID is unique within the stage and does not
change. In C, do not compare handle pointers, because a clone is a different
pointer to the same stream.
When a participant stops publishing or you unsubscribe from them, the SDK calls the streams removed hook with the removed streams. You must use this as a signal to release the state you keep for those streams.
The SDK invokes the streams removed hook for all scenarios in which it might remove a stream, including:
-
The remote participant stops publishing.
-
The local participant unsubscribes from the remote participant.
-
The remote participant leaves the stage.
-
The local participant leaves the stage.
Because the SDK invokes the streams removed hook for all scenarios, you do not need custom business logic to release participant state during remote or local leave operations.
Get Participant Attributes
If you specify attributes in the CreateParticipantToken operation
request, you can see the attributes in the participant info. For example, this
strategy subscribes only to participants whose role attribute is guest:
You can use this to create a host application that listens only to guests and ignores moderators.
When the user ID or attributes of a participant change, the participant metadata updated hook delivers the new participant info. It has the same participant ID as before.
Handling Network Issues
If the network connection drops, the SDK tries to reconnect without action
from the host application. If it cannot reconnect, the connection state hook reports
DISCONNECTED with an error. Call join again after the network
connection recovers. If join fails with a fatal error, create a new stage.