forked from pedroSG94/RootEncoder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCameraFragment.kt
More file actions
246 lines (224 loc) · 8.04 KB
/
CameraFragment.kt
File metadata and controls
246 lines (224 loc) · 8.04 KB
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/*
* Copyright (C) 2024 pedroSG94.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.pedro.streamer.rotation
import android.annotation.SuppressLint
import android.os.Build
import android.os.Bundle
import android.view.LayoutInflater
import android.view.SurfaceHolder
import android.view.SurfaceView
import android.view.View
import android.view.ViewGroup
import android.widget.EditText
import android.widget.ImageView
import android.widget.TextView
import androidx.annotation.RequiresApi
import androidx.fragment.app.Fragment
import com.pedro.common.ConnectChecker
import com.pedro.common.onMainThreadHandler
import com.pedro.encoder.input.sources.video.Camera1Source
import com.pedro.encoder.input.sources.video.Camera2Source
import com.pedro.extrasources.CameraXSource
import com.pedro.library.base.recording.RecordController
import com.pedro.library.generic.GenericStream
import com.pedro.library.util.BitrateAdapter
import com.pedro.streamer.R
import com.pedro.streamer.utils.PathUtils
import com.pedro.streamer.utils.toast
import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale
/**
* Example code to stream using StreamBase. This is the recommend way to use the library.
* Necessary API 21+
* This mode allow you stream using custom Video/Audio sources, attach a preview or not dynamically, support device rotation, etc.
*
* Check Menu to use filters, video and audio sources, and orientation
*
* Orientation horizontal (by default) means that you want stream with vertical resolution
* (with = 640, height = 480 and rotation = 0) The stream/record result will be 640x480 resolution
*
* Orientation vertical means that you want stream with vertical resolution
* (with = 640, height = 480 and rotation = 90) The stream/record result will be 480x640 resolution
*
* More documentation see:
* [com.pedro.library.base.StreamBase]
* Support RTMP, RTSP and SRT with commons features
* [com.pedro.library.generic.GenericStream]
* Support RTSP with all RTSP features
* [com.pedro.library.rtsp.RtspStream]
* Support RTMP with all RTMP features
* [com.pedro.library.rtmp.RtmpStream]
* Support SRT with all SRT features
* [com.pedro.library.srt.SrtStream]
*/
@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
class CameraFragment: Fragment(), ConnectChecker {
companion object {
fun getInstance(): CameraFragment = CameraFragment()
}
val genericStream: GenericStream by lazy {
GenericStream(requireContext(), this).apply {
getGlInterface().autoHandleOrientation = true
}
}
private lateinit var surfaceView: SurfaceView
private lateinit var bStartStop: ImageView
private lateinit var txtBitrate: TextView
val width = 640
val height = 480
val vBitrate = 1200 * 1000
private var rotation = 0
private val sampleRate = 32000
private val isStereo = true
private val aBitrate = 128 * 1000
private var recordPath = ""
//Bitrate adapter used to change the bitrate on fly depend of the bandwidth.
private val bitrateAdapter = BitrateAdapter {
genericStream.setVideoBitrateOnFly(it)
}.apply {
setMaxBitrate(vBitrate + aBitrate)
}
@SuppressLint("ClickableViewAccessibility")
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
): View? {
val view = inflater.inflate(R.layout.fragment_camera, container, false)
bStartStop = view.findViewById(R.id.b_start_stop)
val bRecord = view.findViewById<ImageView>(R.id.b_record)
val bSwitchCamera = view.findViewById<ImageView>(R.id.switch_camera)
val etUrl = view.findViewById<EditText>(R.id.et_rtp_url)
txtBitrate = view.findViewById(R.id.txt_bitrate)
surfaceView = view.findViewById(R.id.surfaceView)
(activity as? RotationActivity)?.let {
surfaceView.setOnTouchListener(it)
}
surfaceView.holder.addCallback(object: SurfaceHolder.Callback {
override fun surfaceCreated(holder: SurfaceHolder) {
if (!genericStream.isOnPreview) genericStream.startPreview(surfaceView)
}
override fun surfaceChanged(holder: SurfaceHolder, format: Int, width: Int, height: Int) {
genericStream.getGlInterface().setPreviewResolution(width, height)
}
override fun surfaceDestroyed(holder: SurfaceHolder) {
if (genericStream.isOnPreview) genericStream.stopPreview()
}
})
bStartStop.setOnClickListener {
if (!genericStream.isStreaming) {
genericStream.startStream(etUrl.text.toString())
bStartStop.setImageResource(R.drawable.stream_stop_icon)
} else {
genericStream.stopStream()
bStartStop.setImageResource(R.drawable.stream_icon)
}
}
bRecord.setOnClickListener {
if (!genericStream.isRecording) {
val folder = PathUtils.getRecordPath()
if (!folder.exists()) folder.mkdir()
val sdf = SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault())
recordPath = "${folder.absolutePath}/${sdf.format(Date())}.mp4"
bRecord.setImageResource(R.drawable.pause_icon)
genericStream.startRecord(recordPath) { status ->
if (status == RecordController.Status.RECORDING) {
onMainThreadHandler {
bRecord.setImageResource(R.drawable.stop_icon)
}
}
}
} else {
genericStream.stopRecord()
bRecord.setImageResource(R.drawable.record_icon)
PathUtils.updateGallery(requireContext(), recordPath)
}
}
bSwitchCamera.setOnClickListener {
when (val source = genericStream.videoSource) {
is Camera1Source -> source.switchCamera()
is Camera2Source -> source.switchCamera()
is CameraXSource -> source.switchCamera()
}
}
return view
}
fun setOrientationMode(isVertical: Boolean) {
val wasOnPreview = genericStream.isOnPreview
genericStream.release()
rotation = if (isVertical) 90 else 0
prepare()
if (wasOnPreview) genericStream.startPreview(surfaceView)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
prepare()
genericStream.getStreamClient().setReTries(10)
}
private fun prepare() {
val prepared = try {
genericStream.prepareVideo(width, height, vBitrate, rotation = rotation)
&& genericStream.prepareAudio(sampleRate, isStereo, aBitrate)
} catch (_: IllegalArgumentException) {
false
}
if (!prepared) {
toast("Audio or Video configuration failed")
activity?.finish()
}
}
override fun onDestroy() {
super.onDestroy()
genericStream.release()
}
override fun onConnectionStarted(url: String) {
}
override fun onConnectionSuccess() {
toast("Connected")
}
override fun onConnectionFailed(reason: String) {
if (genericStream.getStreamClient().reTry(5000, reason, null)) {
toast("Retry")
} else {
genericStream.stopStream()
onMainThreadHandler {
bStartStop.setImageResource(R.drawable.stream_icon)
}
toast("Failed: $reason")
}
}
override fun onNewBitrate(bitrate: Long) {
onMainThreadHandler {
bitrateAdapter.adaptBitrate(bitrate, genericStream.getStreamClient().hasCongestion())
txtBitrate.text = String.format(Locale.getDefault(), "%.1f mb/s", bitrate / 1000_000f)
}
}
override fun onDisconnect() {
onMainThreadHandler {
txtBitrate.text = String()
toast("Disconnected")
}
}
override fun onAuthError() {
genericStream.stopStream()
onMainThreadHandler {
bStartStop.setImageResource(R.drawable.stream_icon)
}
toast("Auth error")
}
override fun onAuthSuccess() {
toast("Auth success")
}
}