Skip to content

Commit 0451473

Browse files
authored
Merge pull request #1 from ChillingVan/Master
Master
2 parents 9dd7013 + 4e0f4e4 commit 0451473

File tree

17 files changed

+856
-58
lines changed

17 files changed

+856
-58
lines changed

app/src/main/AndroidManifest.xml

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
2525
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
2626
<uses-permission android:name="android.permission.CAMERA"/>
27+
<uses-permission android:name="android.permission.INTERNET"/>
2728

2829
<application
2930
android:allowBackup="true"
@@ -56,8 +57,18 @@
5657
<category android:name="com.chillingvan.instantvideo.sample"/>
5758
</intent-filter>
5859
</activity>
59-
<activity android:name=".test.camera.TestCameraAndVideoActivity"
60-
android:label="TestCameraAndVideoActivity"
60+
<activity
61+
android:name=".test.camera.TestCameraAndVideoActivity"
62+
android:label="TestCameraAndVideoActivity"
63+
>
64+
<intent-filter>
65+
<action android:name="android.intent.action.MAIN"/>
66+
67+
<category android:name="com.chillingvan.instantvideo.sample"/>
68+
</intent-filter>
69+
</activity>
70+
<activity android:name=".test.publisher.TestCameraPublisherActivity"
71+
android:label="TestCameraPublisherActivity"
6172
>
6273
<intent-filter>
6374
<action android:name="android.intent.action.MAIN"/>

app/src/main/java/com/chillingvan/instantvideo/sample/test/audio/TestAudioEncoder.java

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
package com.chillingvan.instantvideo.sample.test.audio;
2222

2323
import android.content.Context;
24+
import android.media.MediaCodec;
2425

2526
import com.chillingvan.canvasgl.Loggers;
2627
import com.chillingvan.lib.encoder.MediaCodecInputStream;
@@ -62,12 +63,18 @@ public void onComing() {
6263
write();
6364
}
6465
});
65-
isStart = true;
6666
} catch (IOException e) {
6767
e.printStackTrace();
6868
}
6969
}
7070

71+
public void start() {
72+
if (!isStart) {
73+
aacEncoder.start();
74+
isStart = true;
75+
}
76+
}
77+
7178

7279
public void stop() {
7380
isStart = false;
@@ -87,40 +94,25 @@ public void write() {
8794
boolean shouldAddPacketHeader = true;
8895
byte[] header = new byte[7];
8996
@Override
90-
public void onReadOnce(byte[] buffer, int readSize, int mediaBufferSize) {
97+
public void onReadOnce(byte[] buffer, int readSize, MediaCodec.BufferInfo bufferInfo) {
9198
if (readSize <= 0) {
9299
return;
93100
}
94101
try {
95-
Loggers.d("TestAudioEncoder", String.format("onReadOnce: readSize:%d, mediaBufferSize:%d", readSize, mediaBufferSize));
102+
Loggers.d("TestAudioEncoder", String.format("onReadOnce: readSize:%d, bufferInfo:%d", readSize, bufferInfo.size));
96103
if (shouldAddPacketHeader) {
97104
Loggers.d("TestAudioEncoder", String.format("onReadOnce: add packet header"));
98-
addADTStoPacket(header, 7 + mediaBufferSize);
105+
AACEncoder.addADTStoPacket(header, 7 + bufferInfo.size);
99106
os.write(header);
100107
}
101108
os.write(buffer, 0, readSize);
102109
os.flush();
103110
} catch (IOException e) {
104111
e.printStackTrace();
105112
}
106-
shouldAddPacketHeader = readSize >= mediaBufferSize;
113+
shouldAddPacketHeader = readSize >= bufferInfo.size;
107114
}
108115
});
109116
}
110117

111-
private static void addADTStoPacket(byte[] packet, int packetLen) {
112-
int profile = 2; // AAC LC
113-
int freqIdx = 4; // 44.1KHz
114-
int chanCfg = 2; // CPE
115-
116-
117-
// fill in ADTS data
118-
packet[0] = (byte) 0xFF;
119-
packet[1] = (byte) 0xF9;
120-
packet[2] = (byte) (((profile - 1) << 6) + (freqIdx << 2) + (chanCfg >> 2));
121-
packet[3] = (byte) (((chanCfg & 3) << 6) + (packetLen >> 11));
122-
packet[4] = (byte) ((packetLen & 0x7FF) >> 3);
123-
packet[5] = (byte) (((packetLen & 7) << 5) + 0x1F);
124-
packet[6] = (byte) 0xFC;
125-
}
126118
}

app/src/main/java/com/chillingvan/instantvideo/sample/test/audio/TestAudioEncoderActivity.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,12 @@ protected void onCreate(Bundle savedInstanceState) {
3737
setContentView(R.layout.activity_test_audio_encoder);
3838

3939
testAudioEncoder = new TestAudioEncoder(getApplicationContext());
40+
testAudioEncoder.prepareEncoder();
4041
}
4142

4243

44+
45+
4346
@Override
4447
protected void onPause() {
4548
super.onPause();
@@ -52,7 +55,7 @@ public void clickStartTest(View view) {
5255
testAudioEncoder.stop();
5356
textView.setText("RECORD");
5457
} else {
55-
testAudioEncoder.prepareEncoder();
58+
testAudioEncoder.start();
5659
textView.setText("stop");
5760
}
5861
}

app/src/main/java/com/chillingvan/instantvideo/sample/test/camera/TestCameraAndVideoActivity.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ protected void onCreate(Bundle savedInstanceState) {
3636
super.onCreate(savedInstanceState);
3737
setContentView(R.layout.activity_camera_and_video);
3838
cameraPreviewTextureView = (CameraPreviewTextureView) findViewById(R.id.camera_produce_view);
39-
instantVideoCamera = new InstantVideoCamera();
39+
instantVideoCamera = new InstantVideoCamera(Camera.CameraInfo.CAMERA_FACING_FRONT, 1280, 720);
4040

4141
}
4242

@@ -59,7 +59,7 @@ public void handleMessage(Message msg) {
5959
protected void onResume() {
6060
super.onResume();
6161
initWriteFileHandler();
62-
instantVideoCamera.openCamera(Camera.CameraInfo.CAMERA_FACING_FRONT, 1280, 720);
62+
instantVideoCamera.openCamera();
6363
initCameraTexture();
6464
cameraPreviewTextureView.onResume();
6565
}
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/*
2+
*
3+
* *
4+
* * * Copyright (C) 2017 ChillingVan
5+
* * *
6+
* * * Licensed under the Apache License, Version 2.0 (the "License");
7+
* * * you may not use this file except in compliance with the License.
8+
* * * You may obtain a copy of the License at
9+
* * *
10+
* * * http://www.apache.org/licenses/LICENSE-2.0
11+
* * *
12+
* * * Unless required by applicable law or agreed to in writing, software
13+
* * * distributed under the License is distributed on an "AS IS" BASIS,
14+
* * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* * * See the License for the specific language governing permissions and
16+
* * * limitations under the License.
17+
* *
18+
*
19+
*/
20+
21+
package com.chillingvan.instantvideo.sample.test.publisher;
22+
23+
import android.graphics.SurfaceTexture;
24+
import android.hardware.Camera;
25+
import android.os.Bundle;
26+
import android.os.Handler;
27+
import android.os.HandlerThread;
28+
import android.os.Message;
29+
import android.support.annotation.Nullable;
30+
import android.support.v7.app.AppCompatActivity;
31+
import android.view.View;
32+
import android.widget.EditText;
33+
import android.widget.TextView;
34+
35+
import com.chillingvan.canvasgl.ICanvasGL;
36+
import com.chillingvan.canvasgl.Loggers;
37+
import com.chillingvan.canvasgl.glcanvas.BasicTexture;
38+
import com.chillingvan.canvasgl.glcanvas.RawTexture;
39+
import com.chillingvan.instantvideo.sample.R;
40+
import com.chillingvan.instantvideo.sample.test.camera.CameraPreviewTextureView;
41+
import com.chillingvan.lib.camera.InstantVideoCamera;
42+
import com.chillingvan.lib.encoder.video.H264Encoder;
43+
import com.chillingvan.lib.muxer.RTMPStreamMuxer;
44+
import com.chillingvan.lib.publisher.CameraStreamPublisher;
45+
import com.chillingvan.lib.publisher.StreamPublisher;
46+
47+
import java.io.IOException;
48+
49+
public class TestCameraPublisherActivity extends AppCompatActivity {
50+
51+
private CameraStreamPublisher streamPublisher;
52+
private CameraPreviewTextureView cameraPreviewTextureView;
53+
private InstantVideoCamera instantVideoCamera;
54+
private Handler handler;
55+
private EditText addrEditText;
56+
57+
@Override
58+
protected void onCreate(Bundle savedInstanceState) {
59+
super.onCreate(savedInstanceState);
60+
setContentView(R.layout.activity_test_camera_publisher);
61+
cameraPreviewTextureView = (CameraPreviewTextureView) findViewById(R.id.camera_produce_view);
62+
addrEditText = (EditText) findViewById(R.id.ip_input_test);
63+
instantVideoCamera = new InstantVideoCamera(Camera.CameraInfo.CAMERA_FACING_FRONT, 1280, 720);
64+
65+
HandlerThread handlerThread = new HandlerThread("StreamPublisherOpen");
66+
handlerThread.start();
67+
handler = new Handler(handlerThread.getLooper()) {
68+
@Override
69+
public void handleMessage(Message msg) {
70+
super.handleMessage(msg);
71+
StreamPublisher.StreamPublisherParam streamPublisherParam = new StreamPublisher.StreamPublisherParam();
72+
streamPublisher.prepareEncoder(streamPublisherParam, new H264Encoder.OnDrawListener() {
73+
@Override
74+
public void onGLDraw(ICanvasGL canvasGL, SurfaceTexture surfaceTexture, RawTexture rawTexture, @Nullable SurfaceTexture outsideSurfaceTexture, @Nullable BasicTexture outsideTexture) {
75+
76+
canvasGL.drawSurfaceTexture(outsideTexture, outsideSurfaceTexture, 0, 0, outsideTexture.getWidth(), outsideTexture.getHeight());
77+
Loggers.i("DEBUG", "gl draw");
78+
}
79+
});
80+
try {
81+
streamPublisher.startPublish(addrEditText.getText().toString(), streamPublisherParam.width, streamPublisherParam.height);
82+
} catch (IOException e) {
83+
e.printStackTrace();
84+
}
85+
}
86+
};
87+
88+
// streamPublisher = new CameraStreamPublisher(new RTMPStreamMuxer(), cameraPreviewTextureView, instantVideoCamera);
89+
String filename = getExternalFilesDir(null) + "/test_flv_encode.flv";
90+
streamPublisher = new CameraStreamPublisher(new RTMPStreamMuxer(filename), cameraPreviewTextureView, instantVideoCamera);
91+
}
92+
93+
@Override
94+
protected void onResume() {
95+
super.onResume();
96+
streamPublisher.resumeCamera();
97+
}
98+
99+
@Override
100+
protected void onPause() {
101+
super.onPause();
102+
streamPublisher.pauseCamera();
103+
}
104+
105+
@Override
106+
protected void onDestroy() {
107+
super.onDestroy();
108+
streamPublisher.closeAll();
109+
}
110+
111+
public void clickStartTest(View view) {
112+
TextView textView = (TextView) view;
113+
if (streamPublisher.isStart()) {
114+
streamPublisher.stopPublish();
115+
textView.setText("START");
116+
} else {
117+
handler.sendEmptyMessage(1);
118+
textView.setText("STOP");
119+
}
120+
}
121+
}

app/src/main/java/com/chillingvan/instantvideo/sample/test/video/TestVideoEncoder.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import android.graphics.Color;
2525
import android.graphics.Rect;
2626
import android.graphics.SurfaceTexture;
27+
import android.media.MediaCodec;
2728
import android.util.Log;
2829

2930
import com.chillingvan.canvasgl.ICanvasGL;
@@ -129,7 +130,7 @@ public void write() {
129130
MediaCodecInputStream mediaCodecInputStream = h264Encoder.getMediaCodecInputStream();
130131
MediaCodecInputStream.readAll(mediaCodecInputStream, writeBuffer, 0, new MediaCodecInputStream.OnReadAllCallback() {
131132
@Override
132-
public void onReadOnce(byte[] buffer, int readSize, int mediaBufferSize) {
133+
public void onReadOnce(byte[] buffer, int readSize, MediaCodec.BufferInfo bufferInfo) {
133134
Loggers.d("TestVideoEncoder", String.format("onReadOnce: readSize:%d", readSize));
134135
if (readSize <= 0) {
135136
return;
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
~ /*
4+
~ *
5+
~ * * Copyright (C) 2017 ChillingVan
6+
~ * *
7+
~ * * Licensed under the Apache License, Version 2.0 (the "License");
8+
~ * * you may not use this file except in compliance with the License.
9+
~ * * You may obtain a copy of the License at
10+
~ * *
11+
~ * * http://www.apache.org/licenses/LICENSE-2.0
12+
~ * *
13+
~ * * Unless required by applicable law or agreed to in writing, software
14+
~ * * distributed under the License is distributed on an "AS IS" BASIS,
15+
~ * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
~ * * See the License for the specific language governing permissions and
17+
~ * * limitations under the License.
18+
~ *
19+
~ */
20+
-->
21+
22+
<android.support.constraint.ConstraintLayout
23+
xmlns:android="http://schemas.android.com/apk/res/android"
24+
xmlns:app="http://schemas.android.com/apk/res-auto"
25+
xmlns:tools="http://schemas.android.com/tools"
26+
android:layout_width="match_parent"
27+
android:layout_height="match_parent"
28+
tools:context="com.chillingvan.instantvideo.sample.test.publisher.TestCameraPublisherActivity">
29+
30+
<Button
31+
android:id="@+id/test_camera_button"
32+
android:layout_width="wrap_content"
33+
android:layout_height="wrap_content"
34+
android:text="record"
35+
android:layout_marginRight="8dp"
36+
app:layout_constraintRight_toRightOf="parent"
37+
android:layout_marginLeft="8dp"
38+
app:layout_constraintLeft_toLeftOf="parent"
39+
app:layout_constraintTop_toTopOf="parent"
40+
android:layout_marginTop="8dp"
41+
app:layout_constraintBottom_toBottomOf="parent"
42+
android:layout_marginBottom="8dp"
43+
android:layout_marginStart="8dp"
44+
android:layout_marginEnd="8dp"
45+
android:onClick="clickStartTest"
46+
app:layout_constraintHorizontal_bias="0.971"
47+
app:layout_constraintVertical_bias="0.498"/>
48+
49+
50+
<com.chillingvan.instantvideo.sample.test.camera.CameraPreviewTextureView
51+
android:id="@+id/camera_produce_view"
52+
android:layout_width="wrap_content"
53+
android:layout_height="200dp"
54+
app:layout_constraintTop_toTopOf="parent"
55+
android:layout_marginTop="16dp"
56+
android:layout_marginLeft="8dp"
57+
app:layout_constraintLeft_toLeftOf="parent"
58+
android:layout_marginRight="8dp"
59+
app:layout_constraintRight_toRightOf="parent"
60+
android:layout_marginBottom="8dp"
61+
app:layout_constraintBottom_toTopOf="@+id/test_camera_button"/>
62+
63+
<EditText
64+
android:id="@+id/ip_input_test"
65+
android:layout_width="262dp"
66+
android:layout_height="wrap_content"
67+
android:text="rtmp://192.168.0.140:19305/live/room"
68+
android:layout_marginRight="8dp"
69+
android:layout_marginTop="3dp"
70+
android:hint="rtmp://192.168.1.139:19305/live/room"
71+
app:layout_constraintTop_toTopOf="@+id/test_camera_button"
72+
app:layout_constraintRight_toLeftOf="@+id/test_camera_button"/>
73+
74+
</android.support.constraint.ConstraintLayout>

app/src/main/res/values-zh-rCN/strings.xml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
<resources>
2323

24+
<string name="app_name">InstantVideo</string>
2425
<string name="permission_tips">需要权限,否则崩溃╮(╯▽╰)╭</string>
25-
<string name="first_image">选照片一</string>
26-
<string name="second_image">选照片二</string>
2726
</resources>

applibs/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,6 @@ dependencies {
4949
})
5050
compile 'com.android.support:appcompat-v7:25.2.0'
5151
testCompile 'junit:junit:4.12'
52+
compile 'net.butterflytv.utils:rtmp-client:0.2.6'
53+
5254
}

0 commit comments

Comments
 (0)