Introduction to Android EasyRTMP

What is EasyRTMP?

EasyRTMP is an RTMP push library that can be used with EasyDarwin or can be used alone. Through EasyRTMP, we can avoid touching the slightly complicated RTMP push process. We only need to call several API interfaces of EasyRTMP to easily and stably transfer the stream Push media audio and video data to RTMP servers such as Red5, Ngnix, crtmpserver, etc.

We have a dedicated Android version of EasyRTMP implemented by the EasyRTMP library, which can directly push the mobile phone camera or desktop to the relevant server with RTMP protocol, which is easy to use and easy to integrate. The APP interface of EasyRTMP is shown in the figure:

Android EasyRTMP running interface

As you can see, there are the following functional elements on the home page
- switch resolution
- record
- switch camera
- frame rate bit rate display
- turn on, turn off camera push
- turn on, turn off screen push
- settings

Here are one by one:

switch resolution

Switch resolution, that is, switch the preview resolution of the camera. First, you need to obtain the resolution supported by the camera. Obtain the resolution supported by the camera as follows:

List<Camera.Size> supportedPreviewSizes = mCamera.getParameters().getSupportedPreviewSizes();

Set the preview resolution as follows:

Camera.Parameters parameters = mCamera.getParameters();
parameters.setPreviewSize(width, height);
...
mCamera.setParameters(parameters);

When switching resolutions, first close the preview, reset the resolution, and then open the preview:

mCamera.stopPreview();
// 设置新分辨率
...
mCamera.startPreview();

video

Video recording is performed through EasyMuxer. EasyMuxer is a package for MediaMuxer, which can perform convenient recording, change files, close recording and other functions through simple interfaces. At the same time, EasyMuxer can record PCM audio format, so that any All types of audio encoded data can be decoded first, and then encoded into AAC to record in .MP4 format. For the introduction of EasyMuxer, see the blog post: http://blog.csdn.net/jyt0551/article/details/72787095

switch camera

Switching cameras is similar to switching resolutions, in that you must first close the current preview and then reopen it. The difference is that when switching cameras, you need to release the camera and reopen it, and to switch resolutions, you only need to stopPreview and then startPreview.

stopPreview();
destroyCamera();
if (mCameraId == Camera.CameraInfo.CAMERA_FACING_FRONT) {
    //现在是后置,变更为前置
    if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {//代表摄像头的方位,CAMERA_FACING_FRONT前置      CAMERA_FACING_BACK后置
        mCameraId = Camera.CameraInfo.CAMERA_FACING_BACK;
        createCamera();
        startPreview();
        break;
    }
} else {
    //现在是前置, 变更为后置
    if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_BACK) {//代表摄像头的方位,CAMERA_FACING_FRONT前置      CAMERA_FACING_BACK后置
        mCameraId = Camera.CameraInfo.CAMERA_FACING_FRONT;
        createCamera();
        startPreview();
        break;
    }
}

Display frame rate bit rate

In order to facilitate processing, the statistics of frame rate and bit rate are placed on the push side. That is, the number of video frames and bytes sent in a certain period of time is counted, and then divided by the period of time to obtain the result. Here, use EventBus POST to go out. After the upper layer captures the Event, it will be displayed.

mTotal += length;
if (type == 1){
    mTotalFrms++;
}
long interval = System.currentTimeMillis() - pPreviewTS;
if (interval >= 3000){
    long bps = mTotal * 1000 / (interval);
    long fps = mTotalFrms * 1000 / (interval);
    Log.i(TAG, String.format("bps:%d, fps:%d", fps, bps));
    pPreviewTS = System.currentTimeMillis();
    mTotal = 0;
    mTotalFrms = 0;

    BUS.post(new StreamStat((int)fps, (int)bps));
}

The upper layer captures and displays:

@Subscribe
public void onStreamStat(final StreamStat stat) {
    streamStat.post(new Runnable() {
        @Override
        public void run() {
            streamStat.setText(getString(R.string.stream_stat, stat.fps, stat.bps / 1024));
        }
    });
}

Enable and disable camera push

When it is not pushed, click the button to initiate the push. At this time, the Pusher library is initialized. After the initialization is successful, the Pusher handle is obtained. The subsequent Push interface will judge whether to perform the real push action according to this value.
When the push starts , click the button to close the push. Deinitialize accordingly, so that the push will not be performed on the Push interface.

Enable and disable screen push

The push screen is mainly carried out in the RecordService service. When clicking the push button, start the service first:

Intent intent = new Intent(getApplicationContext(), RecordService.class);
startService(intent);

Specifically, the MediaProjection class can capture the current screen image into a surface, and MediaCodec can obtain the video data source from a surface. We let MediaProjection project to the Surface created by MediaCodec, and MediaCodec can get the video projected by MediaProjection.

When the screen push is closed, the server is stopped, and the related resources are deinitialized when the onDestory is inside the service.
For a more detailed description of the screen push, see:
http://blog.csdn.net/jyt0551/article/details/72787095

set up

The functions in the settings are as follows:

write picture description here

  • The URL can set the stream address to be pushed, and the client can also use the changed address to play;
  • Whether to enable camera background capture. If enabled, the push will continue when the APP goes to the background.
  • Whether to use soft encoding? If enabled, the app will use x264 soft encoding to push
  • If superimposed watermark is enabled, some watermark information will be superimposed on the video. At present, the text watermark can be realized, and the text content can be set in the APP
  • Only push audio If enabled, it will not push video, only sound.
  • Open the video folder and click to open the video clips generated in the APP, which can be easily viewed
  • version number display
  • Save the settings and exit the settings interface

EasyRTMP project introduction

EasyRTMP is a set of RTMP live push function components developed by the EasyDarwin team. The internal integration includes: basic RTMP protocol, disconnection reconnection, asynchronous push, ring buffer, push network congestion automatic frame loss, buffer key frame retrieval, event callback (disconnection, audio and video data callback), through EasyRTMP, we can avoid touching the slightly complicated RTMP push or client process, only need to call a few API interfaces of EasyRTMP, we can easily and stably stream media audio and video Data push supports most RTMP streaming media servers on the market, including mainstream RTMP servers such as Red5, Ngnix_rtmp, crtmpserver, etc. All platforms support: Windows, Linux, ARM (various cross-compilation toolchains), Android, iOS;

EasyRTMP project address: https://github.com/EasyDarwin/EasyRTMP

Get more information

Email: [email protected]

WEB:www.EasyDarwin.org

Copyright © EasyDarwin.org 2012-2017

EasyDarwin

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325444126&siteId=291194637