java jni librtmp_AndroidInstantVideo

AndroidInstantVideo

展现Android硬编码下的视频数据流动,可以对视频做处理,例如加滤镜,加水印等。

本项目主要是为了展现Android使用硬编码下的视频数据流动,目前完成了H264和AAC编码以及对视频帧的图像处理,以及RTMP直播推流。欢迎Fork和Pull Request。

使用要求

Android API 18 以上

用法

功能:

硬编码H264格式视频 + 对视频帧的图像处理

具体看例子里的 TestVideoEncoder

其中实现图片处理的部分:

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等方法实现对视频帧的处理.

//

//

}

});

//...

}

//...

}

例子中生成的h264文件在/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();

}

}

}

对于生成的文件,可以在PC上使用 PotPlayer 播放

使用 camera 录像并处理

具体可以查看例子,目前例子在我的4.4手机上录下来的文件有一卡一卡的现象,在一部6.0的手机上没有。所以说其实MediaCodec的坑不少,看过不少sdk似乎都是使用ffmpeg作录制。

编码产生aac格式音频

直接使用手机进行录音。

具体看例子里的 TestAudioEncoder

例子中生成的aac文件在/storage/sdcard/Android/data/com.chillingvan.instantvideo.sample/files/test_aac_encode.aac",可以在代码里修改输出路径

对于生成的文件,可以在PC上使用 PotPlayer 播放

使用 LibRtmp 将h264和aac变成RTMP流 发送到服务器

需要测试的话,请自行搭建RTMP服务器。我用的是自己搭建的Nginx服务器,用的Module是nginx-rtmp-module。搭建服务器不需要写代码,根据教程敲几行命令就行。

可以用开源直播软件OBS对比播放效果。播放器用各种都行,VLC,PotPlayer,ffplay都可以。我用的是ffplay,注意,因为只是简单的服务器,所以要先开播放器连接后再开始启动推流。

例如我使用的命令是:.\ffplay.exe "rtmp://localhost:19305/live/room live=1"

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);

}

...

}

最近更新

添加使用Android MediaMuxer的 Mp4Muxer,输出Mp4文件。例子 TestMp4MuxerActivity

修改 IMuxer 接口,使之更通用。给StreamPublisherParam添加更多参数。

TODO

RTSP流

关于 Pull Request

欢迎Fork!

添加了功能发出 Pull Request 的话,希望能在sample的module里添加相应的测试代码,最好在文件开头加上自己的license注释。

相关博文

License

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在安卓JNI中,__FUNCTION__是一个预定义的宏,它可以返回当前函数的名称。它通常用于在调试或日志输出中显示当前函数的名称。在引用中,__FUNCTION__被用于显示正在执行的函数名称为AndroidRuntime::startVm。这个函数是用来启动虚拟机的,并且它的执行流程与sigchain_dummy不同,因此不会调用到sigchain_dummy这里。在引用中,__FUNCTION__没有被直接使用,但是在初始化操作的过程中,可能会使用其它函数来回调Java函数,并且使用__FUNCTION__来记录相关的日志信息。因此,__FUNCTION__主要是用来在日志或调试中标识当前函数的名称。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [安卓高版本c/c++调用java,通过JNI_CreateJavaVM/JNI_GetCreatedJavaVMs创建虚拟机实例](https://blog.csdn.net/zs787055144/article/details/131322208)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [JNI_OnLoad和JNI_OnUnload](https://blog.csdn.net/zhao007z5/article/details/80064818)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值