AndroidInstantVideo

Introduction: 展现 Android 硬编码下的视频数据流动,可以对视频做处理,例如加滤镜,加水印等,做直播推流(用 RTMP)。 Show the stream of Android video hardware encode, including video processing and video publishing by RTMP.
More: Author   ReportBugs   
Tags:

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

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

English README

感谢以下项目 LibRtmp-Client-for-Android

使用要求

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

    实现图像处理的关键是用了android-opengl-canvas

    例子中生成的 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 录像并处理

    关于 Camera,我已经封装好了 InstantVideoCamera

    具体可以查看例子,目前例子在我的 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"

    可以看例子TestCameraPublisherActivity ```java

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); } ... } ```

最近更新

  1. 添加使用 Android MediaMuxer 的 Mp4Muxer,输出 Mp4 文件。例子 TestMp4MuxerActivity
  2. 修改 IMuxer 接口,使之更通用。给 StreamPublisherParam 添加更多参数。

TODO

  1. RTSP 流

关于 Pull Request

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

相关博文

使用 MediaCodec 和 RTMP 做直播推流

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.
Apps
About Me
GitHub: Trinea
Facebook: Dev Tools