From 1f06f73dc8cce4df10803e0c88b54d9ea22ce1f4 Mon Sep 17 00:00:00 2001
From: lingxiaopua <1083514716@qq.com>
Date: Sat, 30 Mar 2019 22:41:15 +0800
Subject: [PATCH 1/2] update use system camera example with androidx
---
app/src/main/AndroidManifest.xml | 12 +++++++++++
.../com/camera/lingxiao/camerademo/App.java | 2 +-
.../lingxiao/camerademo/SplashActivity.java | 5 ++++-
app/src/main/res/layout/activity_splash.xml | 20 +++++++++++++++++--
.../res/layout/activity_system_camera.xml | 2 +-
.../media/lingxiao/harddecoder/Server.java | 8 ++++++--
.../harddecoder/decoder/AudioDecoder.java | 11 +---------
.../harddecoder/decoder/H264Decoder.java | 14 +++----------
.../lingxiao/harddecoder/tlv/TLVDecoder.java | 2 +-
9 files changed, 47 insertions(+), 29 deletions(-)
diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml
index db3e080..fb154c1 100644
--- a/app/src/main/AndroidManifest.xml
+++ b/app/src/main/AndroidManifest.xml
@@ -37,6 +37,18 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/app/src/main/java/com/camera/lingxiao/camerademo/App.java b/app/src/main/java/com/camera/lingxiao/camerademo/App.java
index 837c20d..62a1ed0 100644
--- a/app/src/main/java/com/camera/lingxiao/camerademo/App.java
+++ b/app/src/main/java/com/camera/lingxiao/camerademo/App.java
@@ -12,6 +12,6 @@ public class App extends Application{
@Override
public void onCreate() {
super.onCreate();
- //CrashHandler.getInstance().init(this,true);
+ CrashHandler.getInstance().init(this,true);
}
}
diff --git a/app/src/main/java/com/camera/lingxiao/camerademo/SplashActivity.java b/app/src/main/java/com/camera/lingxiao/camerademo/SplashActivity.java
index 6aec329..dcf594a 100644
--- a/app/src/main/java/com/camera/lingxiao/camerademo/SplashActivity.java
+++ b/app/src/main/java/com/camera/lingxiao/camerademo/SplashActivity.java
@@ -48,7 +48,7 @@ protected void initWidget() {
methodRequiresTwoPermission();
}
- @OnClick({R.id.button_extractor,R.id.button_camera,R.id.button_audio})
+ @OnClick({R.id.button_extractor,R.id.button_camera,R.id.button_audio,R.id.button_system_camera})
public void onClick(View v) {
switch (v.getId()) {
default:
@@ -62,6 +62,9 @@ public void onClick(View v) {
case R.id.button_audio:
startActivity(new Intent(getApplicationContext(), AudioActivity.class));
break;
+ case R.id.button_system_camera:
+ startActivity(new Intent(getApplicationContext(), SystemCameraActivity.class));
+ break;
}
}
diff --git a/app/src/main/res/layout/activity_splash.xml b/app/src/main/res/layout/activity_splash.xml
index 58d5b3e..659be54 100644
--- a/app/src/main/res/layout/activity_splash.xml
+++ b/app/src/main/res/layout/activity_splash.xml
@@ -44,10 +44,10 @@
android:id="@+id/button_audio"
android:layout_width="0dp"
android:layout_height="wrap_content"
- android:layout_marginBottom="8dp"
- android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
+ android:layout_marginEnd="8dp"
+ android:layout_marginBottom="8dp"
android:text="音频采集"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
@@ -70,4 +70,20 @@
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button_extractor"
app:layout_constraintVertical_bias="0.0" />
+
+
\ No newline at end of file
diff --git a/app/src/main/res/layout/activity_system_camera.xml b/app/src/main/res/layout/activity_system_camera.xml
index 9899a3d..6ea1679 100644
--- a/app/src/main/res/layout/activity_system_camera.xml
+++ b/app/src/main/res/layout/activity_system_camera.xml
@@ -4,7 +4,7 @@
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
- tools:context=".SystemCameraActivity">
+ >
0) {
// 发送空闲需要加心跳
- acceptor.getSessionConfig().setIdleTime(IdleStatus.WRITER_IDLE, config.heartBeatInterval);
+ //acceptor.getSessionConfig().setIdleTime(IdleStatus.WRITER_IDLE, config.heartBeatInterval);
// 接收空闲判断连接是否已断开了
- acceptor.getSessionConfig().setIdleTime(IdleStatus.READER_IDLE, config.heartBeatInterval);
+ //acceptor.getSessionConfig().setIdleTime(IdleStatus.READER_IDLE, config.heartBeatInterval);
+ //等同于上面两个,等5秒进入空闲状态 handler的sessionIdle会收到消息
+ acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE,config.heartBeatInterval);
}
try {
diff --git a/harddecoder/src/main/java/com/media/lingxiao/harddecoder/decoder/AudioDecoder.java b/harddecoder/src/main/java/com/media/lingxiao/harddecoder/decoder/AudioDecoder.java
index 786575c..518eb25 100644
--- a/harddecoder/src/main/java/com/media/lingxiao/harddecoder/decoder/AudioDecoder.java
+++ b/harddecoder/src/main/java/com/media/lingxiao/harddecoder/decoder/AudioDecoder.java
@@ -208,7 +208,6 @@ public void run() {
audioExtractor.setDataSource(MPEG_4_Path);
AudioTrack audioTrack = null;
int audioExtractorTrackIndex = -1; //提供音频的音频轨
- int audioMaxInputSize = 0; //能获取的音频的最大值
//多媒体流中video轨和audio轨的总个数
for (int i = 0; i < audioExtractor.getTrackCount(); i++) {
MediaFormat format = audioExtractor.getTrackFormat(i);
@@ -221,10 +220,6 @@ public void run() {
int minBufferSize = AudioTrack.getMinBufferSize(audioSampleRate,
(audioChannels == 1 ? AudioFormat.CHANNEL_OUT_MONO : AudioFormat.CHANNEL_OUT_STEREO),
AudioFormat.ENCODING_PCM_16BIT);
- //int maxInputSize = format.getInteger(MediaFormat.KEY_MAX_INPUT_SIZE);
- //audioMaxInputSize = minBufferSize > 0 ? minBufferSize * 4 : maxInputSize;
- //int frameSizeInBytes = audioChannels * 2;
- //audioMaxInputSize = (audioMaxInputSize / frameSizeInBytes) * frameSizeInBytes;
audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
audioSampleRate,
(audioChannels == 1 ? AudioFormat.CHANNEL_OUT_MONO : AudioFormat.CHANNEL_OUT_STEREO),
@@ -235,17 +230,13 @@ public void run() {
try {
mCodec = MediaCodec.createDecoderByType(mime);
mCodec.configure(format, null, null, 0);
+ mCodec.start();
} catch (IOException e) {
e.printStackTrace();
}
break;
}
}
- if (mCodec == null) {
- Log.d(TAG, "audio decoder is unexpectedly null");
- return;
- }
- mCodec.start();
MediaCodec.BufferInfo audioBufferInfo = new MediaCodec.BufferInfo();
ByteBuffer[] inputBuffers = mCodec.getInputBuffers();
diff --git a/harddecoder/src/main/java/com/media/lingxiao/harddecoder/decoder/H264Decoder.java b/harddecoder/src/main/java/com/media/lingxiao/harddecoder/decoder/H264Decoder.java
index 3f9da8c..a3f9121 100644
--- a/harddecoder/src/main/java/com/media/lingxiao/harddecoder/decoder/H264Decoder.java
+++ b/harddecoder/src/main/java/com/media/lingxiao/harddecoder/decoder/H264Decoder.java
@@ -228,7 +228,6 @@ public void run() {
try {
MediaExtractor videoExtractor = new MediaExtractor(); //MediaExtractor作用是将音频和视频的数据进行分离
videoExtractor.setDataSource(MPEG_4_Path);
-
int videoTrackIndex = -1; //提供音频的音频轨
//多媒体流中video轨和audio轨的总个数
for (int i = 0; i < videoExtractor.getTrackCount(); i++) {
@@ -243,6 +242,7 @@ public void run() {
try {
mCodec = MediaCodec.createDecoderByType(mime);
mCodec.configure(format, mSurface, null, 0);
+ mCodec.start();
if (mVideoCallBack != null){
mVideoCallBack.onGetVideoInfo(width,height,time);
}
@@ -252,16 +252,10 @@ public void run() {
break;
}
}
- if (mCodec == null) {
- Log.d(TAG, "video decoder is unexpectedly null");
- return;
- }
- mCodec.start();
MediaCodec.BufferInfo videoBufferInfo = new MediaCodec.BufferInfo();
long startTimeStamp = System.currentTimeMillis(); //记录开始解码的时间
while (isDecoding){
- // 暂停
if (isPause) {
continue;
}
@@ -311,14 +305,12 @@ public void run() {
* @param startMillis
*/
private void decodeDelay(MediaCodec.BufferInfo bufferInfo, long startMillis) {
- while (bufferInfo.presentationTimeUs / 1000 > System.currentTimeMillis() - startMillis) {
+ long current = bufferInfo.presentationTimeUs / 1000 - (System.currentTimeMillis() - startMillis);
+ if (current > 0) {
try {
- long current = bufferInfo.presentationTimeUs / 1000 - (System.currentTimeMillis() - startMillis);
- //Log.d(TAG, "decodeDelay: " + current + "ms");
Thread.sleep(current);
} catch (InterruptedException e) {
e.printStackTrace();
- break;
}
}
}
diff --git a/harddecoder/src/main/java/com/media/lingxiao/harddecoder/tlv/TLVDecoder.java b/harddecoder/src/main/java/com/media/lingxiao/harddecoder/tlv/TLVDecoder.java
index a3751ba..3ff23e2 100644
--- a/harddecoder/src/main/java/com/media/lingxiao/harddecoder/tlv/TLVDecoder.java
+++ b/harddecoder/src/main/java/com/media/lingxiao/harddecoder/tlv/TLVDecoder.java
@@ -14,7 +14,7 @@
public final class TLVDecoder extends CumulativeProtocolDecoder {
@Override
protected boolean doDecode(IoSession session, IoBuffer in, ProtocolDecoderOutput out) throws Exception {
- if (in.remaining() >= 8) { // 前8字节是包头+长度
+ if (in.remaining() >= 8) { // 前8字节是包头+长度 两个都是int 所以是8
// 标记当前position的快照标记mark,以便后继的reset操作能恢复position位置
in.mark();
From 8ab77abb6d63dd84f2ba27df0819672fc01b502f Mon Sep 17 00:00:00 2001
From: lingxiaopua <1083514716@qq.com>
Date: Sat, 30 Mar 2019 22:47:15 +0800
Subject: [PATCH 2/2] update use system camera example with androidx
---
.../camera/lingxiao/camerademo/SystemCameraActivity.java | 7 +++++--
1 file changed, 5 insertions(+), 2 deletions(-)
diff --git a/app/src/main/java/com/camera/lingxiao/camerademo/SystemCameraActivity.java b/app/src/main/java/com/camera/lingxiao/camerademo/SystemCameraActivity.java
index ca86b42..5d4009a 100644
--- a/app/src/main/java/com/camera/lingxiao/camerademo/SystemCameraActivity.java
+++ b/app/src/main/java/com/camera/lingxiao/camerademo/SystemCameraActivity.java
@@ -2,6 +2,7 @@
import android.app.Activity;
import android.content.Intent;
+import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
@@ -59,7 +60,8 @@ public void onClick(View v) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { //如果是7.0以上,使用FileProvider,否则会报错
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
- Uri imgUri = FileProvider.getUriForFile(getApplicationContext(), "com.camera.lingxiao.camerademo.fileProvider", it);
+ String authority = getApplicationInfo().packageName + ".fileProvider";
+ Uri imgUri = FileProvider.getUriForFile(getApplicationContext(), authority, it);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imgUri); //设置拍照后图片保存的位置
} else {
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(it)); //设置拍照后图片保存的位置
@@ -77,8 +79,9 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK) {
if (requestCode == 200) {
Toast.makeText(this, "拍照成功", Toast.LENGTH_SHORT).show();
+ String authority = getApplicationInfo().packageName + ".fileProvider";
Uri imgUri = FileProvider.getUriForFile(getApplicationContext(),
- "com.camera.lingxiao.camerademo.fileProvider", it);
+ authority, it);
gotoCrop(imgUri);
} else if (requestCode == 201) {
Bitmap bitmap = BitmapFactory.decodeFile(it.getAbsolutePath());