Hosting Components
We provide three base StreamHost
implementations that you can choose from depending on your case.
Using them is a bit different but they all share the same functionality from the StreamHost
interface.
### StreamHostController
A StreamHostController
is the low level implementation that, unlike the others, is detached from
the UI. You will typically hold the controller instance in a ViewModel. Optionally,
for perfect state restoration during configuration changes, we also recommend that you use Android's
SavedStateHandle
and pass it to the controller constructor.
class StreamingViewModel(state: SavedStateHandle) : ViewModel() {
val streamHost = StreamHostController(StreamProfile.LOW_LATENCY_HLS, state)
override fun onCleared() {
super.onCleared()
streamHost.release()
}
}
As you can see, the StreamHostController
must be released when you're done with it.
In order to show the UI and start using the host, you must also call one of the bind
methods
as soon as you have a view container. For example, with a fragment:
class StreamingFragment : Fragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
viewModel.streamHost.bind(this, view.findViewById(R.id.streaming_container))
}
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
viewModel.streamHost.onRequestPermissionsResult(requestCode)
}
}
By passing in the fragment instance, the controller will know how to ask for permissions. We also use the fragment lifecycle to avoid memory leaks, so there's no need to unbind.
StreamHostFragment
The StreamHostFragment
is a fragment implemented exactly as described above. It is
the recommended implementation as it is very easy to use - no need to release or bind UI,
because the fragment owns the views.
You can customize the fragment after it is attached or when creating it, thanks to
the StreamHostOptions
class:
val options = StreamHostOptions.build {
overlay(R.layout.custom_controls)
facing(CameraFacing.FRONT)
flash(CameraFlash.OFF)
zoom(0F)
profile(StreamProfile.LOW_LATENCY_HLS)
streamingMode(StreamingMode.CAMERA_AND_AUDIO)
}
val fragment = StreamHostFragment.newInstance(options)
StreamHostView
The StreamHostView
is a view that holds a controller, to be used for codebases that
do not use fragments at all. Just like the controller, you must pass a fragment / activity /
lifecycle with bind()
to use it. For example:
class StreamingActivity : AppCompatActivity() {
val streamHost by lazy { findViewById<StreamHostView>(R.id.stream_host) }
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.host_activity)
streamHost.bind(this) // necessary!
}
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
streamHost.onRequestPermissionsResult(requestCode)
}
}