Show the stream of Android video hardware encode, including video processing and video publishing.
- Use MediaCodec for H264 encoding.
- Use MediaCodec for AAC encoding.
- Use RTMP for video publishing.
Thanks for these projects
- Android API >= 18
-
MediaCodec Hardware encoding for H264 + Video Processing
Sample: TestVideoEncoder The Video Processing Part:
public class TestVideoEncoder {
//...
public void prepareEncoder() {
//...
h264Encoder.setOnDrawListener(new H264Encoder.OnDrawListener() {
@Override
public void onGLDraw(ICanvasGL canvasGL, SurfaceTexture producedSurfaceTexture, RawTexture rawTexture, @Nullable SurfaceTexture outsideSurfaceTexture, @Nullable BasicTexture outsideTexture) {
// 此处可以使用canvasGL的drawTexture, drawBitmap等方法实现对视频帧的处理.
//
//
}
});
//...
}
//...
}
The key for video frame processing : android-opengl-canvas
H264 test file: /storage/sdcard/Android/data/com.chillingvan.instantvideo.sample/files/test_h264_encode.h264,可以在代码里修改输出路径
public class TestVideoEncoder {
public TestVideoEncoder(Context ctx, final EglContextWrapper eglCtx) {
try {
os = new FileOutputStream(ctx.getExternalFilesDir(null) + File.separator + "test_h264_encode.h264");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
You can use PotPlayer to play the test files.
-
Camera
I wrap a class for simple usage InstantVideoCamera Not work well at my 4.4 phone but fine on my 6.0 phone.
-
AAC Audio
Sample: TestAudioEncoder
Test file directory:/storage/sdcard/Android/data/com.chillingvan.instantvideo.sample/files/test_aac_encode.aac",可以在代码里修改输出路径 You can use PotPlayer to play the test files.
-
LibRtmp for RTMP Stream
Use nginx-rtmp-module for your Nginx test local server. Use OBS for comparing effect. And player can be VLC,PotPlayer,ffplay. Make sure you use player open stream first because of the simple test server.
Sample: TestCameraPublisherActivity
public class TestCameraPublisherActivity extends AppCompatActivity {
...
@Override
protected void onCreate(Bundle savedInstanceState) {
...
handler = new Handler(handlerThread.getLooper()) {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
StreamPublisher.StreamPublisherParam streamPublisherParam = new StreamPublisher.StreamPublisherParam();
streamPublisher.prepareEncoder(streamPublisherParam, new H264Encoder.OnDrawListener() {
@Override
public void onGLDraw(ICanvasGL canvasGL, SurfaceTexture surfaceTexture, RawTexture rawTexture, @Nullable SurfaceTexture outsideSurfaceTexture, @Nullable BasicTexture outsideTexture) {
// Here you can do video process
// 此处可以视频处理,例如加水印等等
canvasGL.drawSurfaceTexture(outsideTexture, outsideSurfaceTexture, 0, 0, outsideTexture.getWidth(), outsideTexture.getHeight());
Loggers.i("DEBUG", "gl draw");
}
});
try {
streamPublisher.startPublish(addrEditText.getText().toString(), streamPublisherParam.width, streamPublisherParam.height);
} catch (IOException e) {
e.printStackTrace();
}
}
};
// streamPublisher = new CameraStreamPublisher(new RTMPStreamMuxer(), cameraPreviewTextureView, instantVideoCamera);
String filename = getExternalFilesDir(null) + "/test_flv_encode.flv";
streamPublisher = new CameraStreamPublisher(new RTMPStreamMuxer(filename), cameraPreviewTextureView, instantVideoCamera);
}
...
}
- Add Mp4Muxer(Use Android MediaMuxer). Sample: TestMp4MuxerActivity
- Update Interface Imuxer. Add more parameters to StreamPublisherParam.
- RTSP Stream.
Welcome. Please add sample code to sample module. Add your license comment.
Copyright 2017 ChillingVan.
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.