Starting from version 3.0.0, you can perform live streaming and recording at the same time using a DualEndpoint. This approach uses a single encoder (one for video and one for audio) and pushes the encoded frames to two different endpoints. This is the most resource-efficient method as it minimizes hardware usage.
To implement this, use a DualEndpointFactory to define a mainEndpointFactory (typically for recording) and a secondEndpointFactory (typically for live streaming). Note that if you use SrtEndpointFactory, the SRT package must be included in your project.
Workflow:
- Initialize the
DualEndpointFactory. - Create a
SingleStreamer with that factory. - Open the main endpoint via
streamer.open(url). - Cast the streamer's endpoint to
DualEndpoint and call openSecond(url). - Call
streamer.startStream() to start the main endpoint. - Call
endpoint.startStreamSecond() to start the second endpoint.
val dualEndpointFactory = DualEndpointFactory(
mainEndpointFactory = MediaMuxerEndpointFactory(),
secondEndpointFactory = SrtEndpointFactory() // SRT package is required for SrtEndpointFactory
)
val streamer = SingleStreamer(
context,
endpointFactory = dualEndpointFactory
)
// Implementation steps:
streamer.open("rtmp/serverip:1935/s/streamKey") // open the main endpoint
val endpoint = streamer.endpoint as DualEndpoint
endpoint.openSecond("file:///path.to.file.mp4") // open the second endpoint
streamer.startStream() // start the streamer and the first endpoint
endpoint.startStreamSecond() // start the second endpoint