Skip to content

Commit

Permalink
Better setting low-latency using moonlight-stream
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeyvasilyev committed Nov 16, 2024
1 parent c8b16c2 commit edbc8d6
Show file tree
Hide file tree
Showing 5 changed files with 1,033 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import android.util.Log
import com.alexvas.utils.MediaCodecUtils
import com.alexvas.utils.capabilitiesToString
import androidx.media3.common.util.Util
import com.limelight.binding.video.MediaCodecHelper
import java.nio.ByteBuffer
import java.util.concurrent.TimeUnit
import java.util.concurrent.atomic.AtomicBoolean
Expand Down Expand Up @@ -145,11 +146,15 @@ abstract class VideoDecodeThread (
else
Log.i(TAG, "Configuring surface ${safeWidthHeight.first}x${safeWidthHeight.second} w/ '$mimeType'")
format.setInteger(MediaFormat.KEY_ROTATION, rotation)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
// format.setFeatureEnabled(android.media.MediaCodecInfo.CodecCapabilities.FEATURE_LowLatency, true)
// Request low-latency for the decoder. Not all of the decoders support that.
format.setInteger(MediaFormat.KEY_LOW_LATENCY, 1)
}
// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
// // format.setFeatureEnabled(android.media.MediaCodecInfo.CodecCapabilities.FEATURE_LowLatency, true)
// // Request low-latency for the decoder. Not all of the decoders support that.
// format.setInteger(MediaFormat.KEY_LOW_LATENCY, 1)
// }

val succeeded = MediaCodecHelper.setDecoderLowLatencyOptions(format, decoder.codecInfo, 1)
Log.i(TAG, "Low-latency: $succeeded")

return format
}

Expand Down Expand Up @@ -391,7 +396,7 @@ abstract class VideoDecodeThread (
if (DEBUG) Log.d(TAG, "OutputBuffer BUFFER_FLAG_END_OF_STREAM")
break
}
} catch (ignored: InterruptedException) {
} catch (_: InterruptedException) {
} catch (e: IllegalStateException) {
// Restarting decoder in software mode
Log.e(TAG, "${e.message}", e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import android.widget.ImageView
import com.alexvas.rtsp.codec.VideoDecodeThread
import com.alexvas.rtsp.codec.VideoDecoderBitmapThread
import com.alexvas.rtsp.widget.RtspProcessor.Statistics
import com.limelight.binding.video.MediaCodecHelper

/**
* Low latency RTSP stream playback on image view (bitmap).
Expand Down Expand Up @@ -72,6 +73,7 @@ class RtspImageView : ImageView {

private fun initView(context: Context, attrs: AttributeSet?, defStyleAttr: Int) {
if (DEBUG) Log.v(TAG, "initView()")
MediaCodecHelper.initialize(context, /*glRenderer*/ "")
}

fun init(uri: Uri, username: String?, password: String?, userAgent: String?) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import org.jcodec.codecs.h264.io.model.VUIParameters
import java.net.Socket
import java.nio.ByteBuffer
import java.util.concurrent.atomic.AtomicBoolean
import kotlin.compareTo
import kotlin.math.min

class RtspProcessor(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import androidx.media3.container.NalUnitUtil
import com.alexvas.rtsp.codec.VideoDecodeThread.DecoderType
import com.alexvas.rtsp.codec.VideoDecoderSurfaceThread
import com.alexvas.rtsp.widget.RtspProcessor.Statistics
import com.limelight.binding.video.MediaCodecHelper

/**
* Low latency RTSP stream playback on surface view.
Expand Down Expand Up @@ -88,6 +89,7 @@ open class RtspSurfaceView: SurfaceView {

private fun initView(context: Context, attrs: AttributeSet?, defStyleAttr: Int) {
if (DEBUG) Log.v(TAG, "initView()")
MediaCodecHelper.initialize(context, /*glRenderer*/ "")
holder.addCallback(surfaceCallback)
}

Expand Down
Loading

0 comments on commit edbc8d6

Please sign in to comment.