Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
lingxiaoplus committed Apr 15, 2019
2 parents c74af96 + 8ab77ab commit 1424803
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 31 deletions.
12 changes: 12 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,18 @@
<activity android:name=".audio.AudioActivity" />
<activity android:name=".BaseActivity" />
<activity android:name=".video.VideoListActivity"></activity>
<activity android:name=".SystemCameraActivity"></activity>

<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.fileProvider"
android:grantUriPermissions="true"
android:exported="false">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>

</application>

</manifest>
2 changes: 1 addition & 1 deletion app/src/main/java/com/camera/lingxiao/camerademo/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)); //设置拍照后图片保存的位置
Expand All @@ -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());
Expand Down
20 changes: 18 additions & 2 deletions app/src/main/res/layout/activity_splash.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -70,4 +70,20 @@
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button_extractor"
app:layout_constraintVertical_bias="0.0" />

<Button
android:id="@+id/button_system_camera"
android:layout_width="0dp"
android:layout_height="wrap_content"
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"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button_player"
app:layout_constraintVertical_bias="0.0" />
</androidx.constraintlayout.widget.ConstraintLayout>
2 changes: 1 addition & 1 deletion app/src/main/res/layout/activity_system_camera.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SystemCameraActivity">
>

<ImageView
android:id="@+id/imageView"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ public class Server{

public Server() {
acceptor = new NioSocketAcceptor();
//消息的处理交给handler
acceptor.setHandler(new StreamHandler());
//获取所有的拦截器之后添加一个新的拦截器 ProtocolCodecFilter 用于二进制和对象之间的转化
acceptor.getFilterChain().addLast("tlv",
new ProtocolCodecFilter(new TLVCodecFactory()));
}
Expand All @@ -47,9 +49,11 @@ public boolean start(int port, ServerConfig _config) {
this.config = _config;
if(config.heartBeatInterval > 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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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),
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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++) {
Expand All @@ -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);
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down

0 comments on commit 1424803

Please sign in to comment.