Skip to content

Excessive number of chunks when streaming to MediaMTX #2099

@zeridon

Description

@zeridon

Describe the bug
When streaming to MediaMTX we see randomly excessive number of chunks recorded.

When streaming from an application created with RootEncoder we are randomly seeing excessive number of chunks (about 7000 in one instance) when recording the streaming. The same is observed on HLS streamed from the server.

The client (device) is an android phone with RootEncoder app. It is streaming over LTE(4g) mobile network with h264/720p/1.6 Mbps CBR.

The behaviour is observed on different quality of network connectivity (not only poor). We do not have control over location, quality of connectivity of the device doing the streaming.

We have 60 ingest servers (mediamtx handling incomming rtmp traffic) and the clinets are more or less equally distributed on them. Top planned load per server is 200 streams, and we are doing extremely minimal processing (e.g. appending a bit of metadata with ffmpeg, calling few curls to external services). Servers in and of themselves are not overloaded. Usuall observed load is in the range of 90% - 95%

code is mostly:

/**
 * StreamActivity is responsible for handling the camera preview and video streaming functionality.
 * It utilizes the RootEncoder library for streaming video and audio data.
 * This activity implements the ConnectChecker interface to handle streaming connection events.
 */
class StreamActivity : ComponentActivity(), ConnectChecker, Loggable {

    //region PROPERTIES ⤵

    private lateinit var viewModel: StreamActivityViewModel

    /**
     * Lazy-loaded instance of GenericStream, used for handling video and audio streaming.
     * Sets automatic orientation handling and adjusts bitrate based on network conditions.
     */
    val genericStream: GenericStream by lazy {
        GenericStream(this, this).apply {
            getGlInterface().autoHandleOrientation = true
            getStreamClient().setBitrateExponentialFactor(0.5f)
        }
    }

    private var _binding: ActivityStreamBinding? = null
    private val binding get() = _binding!!
    private val width = 1280 // Video resolution width
    private val height = 720 // Video resolution height
    private val vBitrate = 1500 * 1000 // Video bitrate in bps
    private var rotation = 0 // Video rotation angle
    private val sampleRate = 44100 // Audio sample rate
    private val isStereo = false // Stereo audio configuration(Mono)
    private val aBitrate = 48 * 1000 // Audio bitrate in kbps
    private var recordPath = "" // Path for saving recorded video
    private val fps = 20 // Frames per second
    private var timerJob: Job? = null
    private var broadcastStartTime: Long = 0L
    private var checkBatteryLevelTimer: Timer? = null
    private val handler = Handler(Looper.getMainLooper())
    private val orientationHelper = OrientationHelper()
    private val section: Section = getCurrentSection()
    private val backPressHandler = Handler(Looper.getMainLooper())
    private var currentToast: Toast? = null
    private var backPressedOnce = false
    private var soundLevelJob: Job? = null
    private var microphoneSourceWrapper: MicrophoneSourceWrapper? = null
    private var startingTime: Date = Date()

    // Bitrate adapter to dynamically adjust bitrate based on bandwidth conditions
    private val bitrateAdapter = BitrateAdapter {
        genericStream.setVideoBitrateOnFly(it)
    }.apply {
        setMaxBitrate(vBitrate + aBitrate)
    }

    /**
     * Initiates streaming if not already active.
     *
     * @param rtmpUrl The RTMP URL to stream to.
     *
     */
    private fun startStreaming(rtmpUrl: String) {
        logInfo("Start streaming to -> $rtmpUrl")

        if (!genericStream.isStreaming) {
            binding.apply {
                genericStream.startStream(rtmpUrl)
            }
        }
    }

To Reproduce
Start streaming against mediamtx

Expected behavior
Normal streaming with no chunking except in extremely lossy conditions (e.g. very bad 4/5G connectivity)

Smartphone (please complete the following information):

  • Library version: 2.6.1
  • Device: motorola moto e13
  • OS: android 13
  • Media server: MediaMTX 1.7.0
  • Class used [e.g. RtmpCamera1]

Additional context
Cross posted to Mediamtx: bluenviron/mediamtx#5810

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions