Hacktoberfest 2026: the issues maintainers tagged for October, open and beginner-friendly. Browse Hacktoberfest issues

Excessive number of chunks when streaming to MediaMTX

Open
#2,099 3 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 2 days

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
15/100
Issue type
Bug
Clarity
Needs clarification
Activity status
Quiet
Tech stack
android, kotlin

Research direction

The report gives no reproduction steps beyond streaming to MediaMTX, so start by reading the code it quotes: GenericStream setup, getStreamClient().setBitrateExponentialFactor(0.5f), the BitrateAdapter callback calling setVideoBitrateOnFly, and startStreaming calling genericStream.startStream. Done would mean identifying which of these changes bitrate or chunk sizing under poor LTE conditions, backed by a reproduction or test. The thread has no maintainer decision yet, so confirm the approach in the comments before writing code.

Written by the indexing model from the issue text.

Description

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: https://github.com/bluenviron/mediamtx/issues/5810

Dominant language
Kotlin
Stars
3k
Forks
946
Avg merge
4d 6h
Merged PRs (30d)
9

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from pedroSG94/RootEncoder

All issues in pedroSG94/RootEncoder

Similar issues

More Kotlin issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.