RtmpPublisher

repository·master·Indexed 20 days ago

https://github.com/takusemba/rtmppublisher

An Android library for RTMP streaming that simplifies camera management, video/audio encoding, and streaming lifecycle. It features a Builder-based API for configuring resolution, bitrates, and camera modes, and provides a PublisherListener interface to handle connection and disconnection events.

Tokens
1.4K
Snippets
6
Records
8
Agent score
21%

What's inside RtmpPublisher

  1. Add RtmpPublisher to your Gradle project

    master

    To use RtmpPublisher in your Android project, add the following dependency to your build.gradle file. Replace x.x.x with the desired version number.

    dependencies {
        compile 'com.github.takusemba:rtmppublisher:x.x.x'
    }
  2. Control RTMP streaming and camera

    master

    Once a Publisher is created, you can control the streaming lifecycle and camera settings using the following methods:

    • startPublishing(): Begins the RTMP stream.
    • stopPublishing(): Stops the current stream.
    • switchCamera(): Toggles between the front and back cameras.
    // start publishing!
    publisher.startPublishing()
    
    // switch camera between front and back
    publisher.switchCamera()
    
    // stop publishing!
    publisher.stopPublishing()
  3. Create a Publisher instance

    master

    Use the Publisher.Builder to configure and instantiate a Publisher. You must provide a context (e.g., this in an Activity), a GLView for rendering, and the RTMP url. You can also customize the resolution, audio/video bitrates, camera mode, and attach a listener.

    val publisher: Publisher = Publisher.Builder(this)
      .setGlView(glView)
      .setUrl(rtmpUrl)
      .setSize(Publisher.Builder.DEFAULT_WIDTH, Publisher.Builder.DEFAULT_HEIGHT)
      .setAudioBitrate(Publisher.Builder.DEFAULT_AUDIO_BITRATE)
      .setVideoBitrate(Publisher.Builder.DEFAULT_VIDEO_BITRATE)
      .setCameraMode(Publisher.Builder.DEFAULT_MODE)
      .setListener(this)
      .build()
  4. Handle publisher lifecycle events with PublisherListener

    master

    Implement the PublisherListener interface and register it using setOnPublisherListener to respond to streaming events such as connection status and disconnections.

    publisher.setOnPublisherListener(object: PublisherListener {
      override fun onStarted() {
        // do something
      }
      override fun onStopped() {
        // do something
      }
      override fun onFailedToConnect() {
        // do something
      }
      override fun onDisconnected() {
        // do something
      }
    })
  5. Create a Publisher using the Builder pattern

    master

    To initialize an RTMP streaming session, use the Publisher.Builder class. You must provide an AppCompatActivity, a GLSurfaceView for the video preview, and the target RTMP url. You can optionally configure the video/audio bitrates, stream resolution, camera mode, and a PublisherListener for lifecycle events.

    Publisher publisher = new Publisher.Builder(activity)
        .setGlView(glSurfaceView)
        .setUrl("rtmp://your-server-url/live/stream")
        .setSize(1280, 720)
        .setVideoBitrate(100000)
        .setAudioBitrate(6400)
        .setCameraMode(CameraMode.BACK)
        .setListener(new PublisherListener() {
            // Implement listener methods
        })
        .build();
  6. Control RTMP streaming with the Publisher interface

    master

    The Publisher interface provides the primary methods for managing the lifecycle of an RTMP stream and controlling the camera hardware.

    // Start the stream
    publisher.startPublishing();
    
    // Switch between front and back cameras
    publisher.switchCamera();
    
    // Check if currently streaming
    if (publisher.isPublishing()) {
        // ...
    }
    
    // Stop the stream
    publisher.stopPublishing();
  7. Configure Publisher.Builder parameters

    master

    When using Publisher.Builder, you can set the following parameters:

    MethodTypeRequirementDescription
    setGlView(GLSurfaceView)GLSurfaceViewRequiredThe view used for the camera preview.
    setUrl(String)StringRequiredThe target RTMP URL.
    setSize(int width, int height)int, intOptionalSets the video stream resolution.
    setAudioBitrate(int)intOptionalSets the audio bitrate. Default: 6400
    setVideoBitrate(int)intOptionalSets the video bitrate. Default: 100000
    setCameraMode(CameraMode)CameraModeOptionalSets initial camera mode (FRONT or BACK). Default: BACK
    setListener(PublisherListener)PublisherListenerOptionalSets the callback listener for stream events.