Skip to content

StreamBase (in progress)

Pedro Sánchez edited this page Apr 14, 2024 · 2 revisions

Introduction

This is the recommended way to use the library. This classes allow you:

  • Attach or remove a preview in real time using a normal SurfaceView or TextureView as preview
  • Support background stream
  • Support Activity rotation and auto handle rotation if you want.
  • Support filters
  • You can use differents video and audio sources and change it on fly (Camera1, Camera2, Screen, Files, Microphone, Audio from the device) or even add your custom source!

All code and features in this section can be used with the following classes:

RtmpStream, RtspStream, SrtStream, UdpStream and GenericStream

Quick start

First of all we need create an instance of the class. Using Camera2 and Microphone by default:

val genericStream = GenericStream(context, connectChecker)

Or Add other video or audio source:

val genericStream = GenericStream(context, , connectChecker, Camera2Source(context), MicrophoneSource())

At this point we can configure the video or audio source depend of the requirements:

    val prepared = try {
      genericStream.prepareVideo(width, height, vBitrate) &&
          genericStream.prepareAudio(sampleRate, isStereo, aBitrate)
    } catch (e: IllegalArgumentException) {
      false
    }

You only need call this methods one time on all the instance lifecycle. You only need call it again if you want change any parameter but remember that you need stop all (preview, stream and record) before call this methods.

If all is success we can start with the preview, stream or recording a file or all at the same time

//start/stop preview
if (!genericStream.isOnPreview) genericStream.startPreview(surfaceView)
else genericStream.stopPreview()
//we can update the preview size using this method:
genericStream.getGlInterface().setPreviewResolution(width, height)

//start/stop stream
if (!genericStream.isStreaming) genericStream.startStream(endpoint)
else genericStream.stopStream()

//start/stop record
if (!genericStream.isStreaming) {
  try {
    genericStream.startRecord(path) { status ->
     //get record status
    }
  } catch (e: IOException) {
    //fail to access to the path
  }
} else genericStream.stopRecord()

After all the work we need call to release to avoid problems:

genericStream.release()

Change video or audio sources on fly

We can change video or audio sources of the stream on fly using this methods:

try {
  genericStream.changeVideoSource(Camera1Source(applicationContext))
} catch (e: IllegalArgumentException) {
  //fail to change the source because the new source can't support the current video or audio configuration
}

try {
  genericStream.changeAudioSource(MicrophoneSource())
} catch (e: IllegalArgumentException) {
  //fail to change the source because the new source can't support the current video or audio configuration
}

Audio and Audio sources usage

You can use video and audio source methods anytime this way:

(genericStream.videoSource as Camera2Source).switchCamera()
(genericStream.audioSource as MicrophoneSource).mute()

The idea is get videoSource or audioSource from the stream class and cast it to the current source used. This way you can use all methods of this class. This allow you use dedicated methods in a source