Skip to content

Commit a7fab45

Browse files
author
v_yholu
committed
体验、UI、代码优化
重写视频编码器处理 SPS 和 PPS 的方式; 设备端使屏幕常亮; 设备呼设备对端画面比例调整。
1 parent b024e7d commit a7fab45

File tree

3 files changed

+46
-41
lines changed

3 files changed

+46
-41
lines changed

app/src/main/java/com/example/ivdemo/ClientActivity.kt

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import android.content.Context
44
import android.graphics.SurfaceTexture
55
import android.os.Bundle
66
import android.util.Log
7+
import android.util.Size
78
import android.view.Surface
89
import android.view.TextureView
910
import android.view.View
@@ -27,6 +28,7 @@ import com.tencent.iot.video.device.model.IvStreamInfo
2728
import com.tencent.iotvideo.link.CameraRecorder
2829
import com.tencent.iotvideo.link.SimplePlayer
2930
import com.tencent.iotvideo.link.listener.OnEncodeListener
31+
import com.tencent.iotvideo.link.util.adjustAspectRatio
3032
import kotlinx.coroutines.launch
3133
import org.json.JSONObject
3234
import java.nio.ByteBuffer
@@ -364,9 +366,9 @@ class ClientActivity : AppCompatActivity(), IvClientCallback, OnEncodeListener {
364366
this.width = width
365367

366368
lifecycleScope.launch {
367-
// adjustAspectRatio(width, height, binding.tvRemoteVideo, binding.tvRemoteVideo.height, binding.tvRemoteVideo.width)
369+
// adjustAspectRatio(width, height, binding.tvRemoteVideo, resources.displayMetrics.widthPixels, resources.displayMetrics.heightPixels)
368370
val params = binding.tvRemoteVideo.layoutParams as ConstraintLayout.LayoutParams
369-
params.dimensionRatio = "H,$width:$height"
371+
params.dimensionRatio = "H,$height:$width"
370372
binding.tvRemoteVideo.layoutParams = params
371373
}
372374

@@ -557,6 +559,31 @@ class ClientActivity : AppCompatActivity(), IvClientCallback, OnEncodeListener {
557559
return params.joinToString("&")
558560
}
559561

562+
private fun getNewSize(width: Int, height: Int): Size? {
563+
if (width < 0 || height < 0) {
564+
Log.w(TAG, "getNewSize: width or height must >= 0!")
565+
return null
566+
}
567+
568+
val screenWidth = resources.displayMetrics.widthPixels
569+
val screenHeight = resources.displayMetrics.heightPixels
570+
val ratio = height.toFloat() / width
571+
var newWidth = width
572+
var newHeight = height
573+
574+
if (width < screenHeight) {
575+
newWidth = screenHeight
576+
newHeight = (screenWidth * ratio).toInt()
577+
}
578+
579+
Log.d(
580+
TAG,
581+
"getNewSize: width: $width, height: $height, newWidth: $newWidth, newHeight: $newHeight"
582+
)
583+
584+
return Size(newWidth, newHeight)
585+
}
586+
560587
private fun sendCommand(cmd: String?, timeout: Long = 5000L): Int {
561588
if (!cmd.isNullOrEmpty()) {
562589
val responseResult = mVideoNativeInterface.sendClientCommand(clientId, cmd.toByteArray(), timeout)

app/src/main/java/com/tencent/iotvideo/link/encoder/VideoEncoder.java

Lines changed: 15 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,6 @@ public class VideoEncoder {
4343
private long seq = 0L;
4444
private int MAX_FRAMERATE_LENGTH = 20;
4545
private int MIN_FRAMERATE_LENGTH = 5;
46-
// 缓存 SPS PPS 信息
47-
ByteBuffer mSpsPpsBuffer = null;
4846

4947
public VideoEncoder(VideoEncodeParam param) {
5048
this.videoEncodeParam = param;
@@ -183,40 +181,24 @@ public void encoderH264(byte[] data, boolean mirror) {
183181
outputBuffer.get(outData);
184182
boolean isKeyFrame = false;
185183

186-
if ((bufferInfo.flags & MediaCodec.BUFFER_FLAG_CODEC_CONFIG) != 0) {
187-
// Log.w(TAG, "encoderH264: sps pps info, outData: " + UtilsKt.toHexString(outData));
188-
189-
if (mSpsPpsBuffer != null) {
190-
mSpsPpsBuffer.clear();
191-
mSpsPpsBuffer = null;
192-
}
193-
194-
// 缓存 SPS/PPS
195-
mSpsPpsBuffer = ByteBuffer.allocate(outData.length);
196-
mSpsPpsBuffer.put(outData);
197-
mSpsPpsBuffer.flip();
198-
} else if ((bufferInfo.flags & MediaCodec.BUFFER_FLAG_KEY_FRAME) != 0) {
199-
// Log.w(TAG, "encoderH264: I frame, outData: " + UtilsKt.toHexString(outData));
184+
if ((bufferInfo.flags & MediaCodec.BUFFER_FLAG_KEY_FRAME) != 0) {
200185
isKeyFrame = true;
201-
202-
if (mSpsPpsBuffer != null) {
203-
byte[] spsPpsData = new byte[mSpsPpsBuffer.remaining()];
204-
mSpsPpsBuffer.get(spsPpsData);
205-
mSpsPpsBuffer.rewind();
206-
Log.w(TAG, "encoderH264: I frame, spsPpsData: " + UtilsKt.toHexString(spsPpsData));
207-
208-
if (spsPpsData.length > 0) {
209-
notifyEncoded(spsPpsData, isKeyFrame);
210-
} else {
211-
Log.w(TAG, "encoderH264: no sps pps data");
212-
}
213-
} else {
214-
Log.w(TAG, "encoderH264: I frame spsPpsBuffer = null");
215-
}
186+
ByteBuffer spsb = mediaCodec.getOutputFormat().getByteBuffer("csd-0");
187+
byte[] sps = new byte[spsb.remaining()];
188+
spsb.get(sps, 0, sps.length);
189+
ByteBuffer ppsb = mediaCodec.getOutputFormat().getByteBuffer("csd-1");
190+
byte[] pps = new byte[ppsb.remaining()];
191+
ppsb.get(pps, 0, pps.length);
192+
byte[] dataBytes = new byte[sps.length + pps.length + outData.length];
193+
System.arraycopy(sps, 0, dataBytes, 0, sps.length);
194+
System.arraycopy(pps, 0, dataBytes, sps.length, pps.length);
195+
System.arraycopy(outData, 0, dataBytes, pps.length + sps.length, outData.length);
196+
notifyEncoded(dataBytes, isKeyFrame);
197+
} else {
198+
// 打印编码后的数据大小
199+
notifyEncoded(outData, isKeyFrame);
216200
}
217201

218-
// 打印编码后的数据大小
219-
notifyEncoded(outData, isKeyFrame);
220202
// 释放输出缓冲区
221203
mediaCodec.releaseOutputBuffer(outputBufferIndex, false);
222204
outputBufferIndex = mediaCodec.dequeueOutputBuffer(bufferInfo, 0);
@@ -255,11 +237,6 @@ public void stop() {
255237
mediaCodec = null;
256238
}
257239

258-
if (mSpsPpsBuffer != null) {
259-
mSpsPpsBuffer.clear();
260-
mSpsPpsBuffer = null;
261-
}
262-
263240
executor.shutdown();
264241
} catch (Exception e) {
265242
e.printStackTrace();

app/src/main/res/layout/activity_custom_duplex_video.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
33
xmlns:app="http://schemas.android.com/apk/res-auto"
44
android:layout_width="match_parent"
5-
android:layout_height="match_parent">
5+
android:layout_height="match_parent"
6+
android:keepScreenOn="true">
67

78
<androidx.constraintlayout.widget.ConstraintLayout
89
android:layout_width="match_parent"

0 commit comments

Comments
 (0)