-
-
Notifications
You must be signed in to change notification settings - Fork 74
/
Copy pathRtspSurfaceView.kt
166 lines (140 loc) · 5.71 KB
/
RtspSurfaceView.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package com.alexvas.rtsp.widget
import android.content.Context
import android.net.Uri
import android.util.AttributeSet
import android.util.Log
import android.view.SurfaceHolder
import android.view.SurfaceView
import androidx.annotation.OptIn
import androidx.media3.common.util.UnstableApi
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.
*/
open class RtspSurfaceView: SurfaceView {
private var surfaceWidth = 1920
private var surfaceHeight = 1080
private var rtspProcessor = RtspProcessor(
onVideoDecoderCreateRequested = {
videoMimeType, videoRotation, videoFrameQueue, videoDecoderListener, videoDecoderType ->
VideoDecoderSurfaceThread(
holder.surface,
videoMimeType,
surfaceWidth,
surfaceHeight,
videoRotation,
videoFrameQueue,
videoDecoderListener,
videoDecoderType,
)
}
)
var statistics = Statistics()
get() = rtspProcessor.statistics
private set
var videoRotation: Int
get() = rtspProcessor.videoRotation
set(value) { rtspProcessor.videoRotation = value }
var videoDecoderType: DecoderType
get() = rtspProcessor.videoDecoderType
set(value) { rtspProcessor.videoDecoderType = value }
var experimentalUpdateSpsFrameWithLowLatencyParams: Boolean
get() = rtspProcessor.experimentalUpdateSpsFrameWithLowLatencyParams
set(value) { rtspProcessor.experimentalUpdateSpsFrameWithLowLatencyParams = value }
var debug: Boolean
get() = rtspProcessor.debug
set(value) { rtspProcessor.debug = value }
private val surfaceCallback = object: SurfaceHolder.Callback {
override fun surfaceCreated(holder: SurfaceHolder) {
if (DEBUG) Log.v(TAG, "surfaceCreated()")
}
override fun surfaceChanged(holder: SurfaceHolder, format: Int, width: Int, height: Int) {
if (DEBUG) Log.v(TAG, "surfaceChanged(format=$format, width=$width, height=$height)")
surfaceWidth = width
surfaceHeight = height
}
override fun surfaceDestroyed(holder: SurfaceHolder) {
if (DEBUG) Log.v(TAG, "surfaceDestroyed()")
rtspProcessor.stopDecoders()
}
}
constructor(context: Context) : super(context) {
initView(context, null, 0)
}
constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
initView(context, attrs, 0)
}
constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
initView(context, attrs, defStyleAttr)
}
private fun initView(context: Context, attrs: AttributeSet?, defStyleAttr: Int) {
if (DEBUG) Log.v(TAG, "initView()")
MediaCodecHelper.initialize(context, /*glRenderer*/ "")
holder.addCallback(surfaceCallback)
}
fun init(uri: Uri, username: String?, password: String?, userAgent: String?) {
if (DEBUG) Log.v(TAG, "init(uri='$uri', username='$username', password='$password', userAgent='$userAgent')")
rtspProcessor.init(uri, username, password, userAgent)
}
/**
* Start RTSP client.
*
* @param requestVideo request video track
* @param requestAudio request audio track
* @param requestApplication request application track
* @see https://datatracker.ietf.org/doc/html/rfc4566#section-5.14
*/
fun start(requestVideo: Boolean, requestAudio: Boolean, requestApplication: Boolean = false) {
if (DEBUG) Log.v(TAG, "start(requestVideo=$requestVideo, requestAudio=$requestAudio, requestApplication=$requestApplication)")
rtspProcessor.start(requestVideo, requestAudio, requestApplication)
}
/**
* Stop RTSP client.
*/
fun stop() {
if (DEBUG) Log.v(TAG, "stop()")
rtspProcessor.stop()
}
fun isStarted(): Boolean {
return rtspProcessor.isStarted()
}
fun setStatusListener(listener: RtspStatusListener?) {
if (DEBUG) Log.v(TAG, "setStatusListener()")
rtspProcessor.statusListener = listener
}
fun setDataListener(listener: RtspDataListener?) {
if (DEBUG) Log.v(TAG, "setDataListener()")
rtspProcessor.dataListener = listener
}
companion object {
private val TAG: String = RtspSurfaceView::class.java.simpleName
private const val DEBUG = false
}
}
@OptIn(UnstableApi::class)
fun NalUnitUtil.SpsData.spsDataToString(): String {
return "" +
"width=${this.width}, " +
"height=${this.height}, " +
"profile_idc=${this.profileIdc}, " +
"constraint_set_flags=${this.constraintsFlagsAndReservedZero2Bits}, " +
"level_idc=${this.levelIdc}, " +
"max_num_ref_frames=${this.maxNumRefFrames}, " +
"frame_mbs_only_flag=${this.frameMbsOnlyFlag}, " +
"log2_max_frame_num=${this.frameNumLength}, " +
"pic_order_cnt_type=${this.picOrderCountType}, " +
"log2_max_pic_order_cnt_lsb=${this.picOrderCntLsbLength}, " +
"delta_pic_order_always_zero_flag=${this.deltaPicOrderAlwaysZeroFlag}, " +
"max_reorder_frames=${this.maxNumReorderFrames}"
}
fun ByteArray.toHexString(offset: Int, maxLength: Int): String {
val length = minOf(maxLength, size - offset)
return sliceArray(offset until (offset + length))
.joinToString(separator = "") { byte ->
"%02x ".format(byte).uppercase()
}
}