Skip to content

Commit

Permalink
更新libEasyRTMP,优化Android视频流推送
Browse files Browse the repository at this point in the history
  • Loading branch information
jinlong0813 committed Jan 22, 2017
1 parent 2de7091 commit c1efbd4
Show file tree
Hide file tree
Showing 13 changed files with 69 additions and 32 deletions.
4 changes: 2 additions & 2 deletions Android/EasyRTMP/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ android {
applicationId "org.easydarwin.easyrtmp"
minSdkVersion 17
targetSdkVersion 22
versionCode 3
versionName "1.0.16.1107"
versionCode 4
versionName "1.1.17.0122"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import android.media.projection.MediaProjection;
import android.media.projection.MediaProjectionManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.IBinder;
import android.support.annotation.Nullable;
Expand Down Expand Up @@ -52,6 +53,8 @@ public class RecordService extends Service {
private WindowManager wm;

private MediaCodec.BufferInfo mBufferInfo = new MediaCodec.BufferInfo();
private byte[] mH264Buffer;
private long timeStamp = System.currentTimeMillis();

static EasyRTMP mEasyRTMP;
private Thread mPushThread;
Expand Down Expand Up @@ -137,6 +140,8 @@ public void run() {

mEasyRTMP.initPush(url, getApplicationContext(), null);
mAudioStream.startRecord();
mH264Buffer = new byte[(int) (windowWidth*windowHeight*1.5)];
timeStamp = System.currentTimeMillis();
while (mPushThread != null) {
int index = mMediaCodec.dequeueOutputBuffer(mBufferInfo, 10000);
Log.i(TAG, "dequeue output buffer index=" + index);
Expand All @@ -149,28 +154,36 @@ public void run() {
}
} else if (index >= 0) {//有效输出
ByteBuffer outputBuffer = mMediaCodec.getOutputBuffer(index);

byte[] outData = new byte[mBufferInfo.size];
outputBuffer.get(outData);
int type = outputBuffer.get(4) & 0x1F;

// String data0 = String.format("%x %x %x %x %x %x %x %x %x %x ", outData[0], outData[1], outData[2], outData[3], outData[4], outData[5], outData[6], outData[7], outData[8], outData[9]);
// Log.e("out_data", data0);

//记录pps和sps
int type = outData[4] & 0x07;
if (type == 7 || type == 8) {
byte[] outData = new byte[mBufferInfo.size];
outputBuffer.get(outData);
mPpsSps = outData;
} else if (type == 5) {
//在关键帧前面加上pps和sps数据
if (mPpsSps != null) {
byte[] iframeData = new byte[mPpsSps.length + outData.length];
System.arraycopy(mPpsSps, 0, iframeData, 0, mPpsSps.length);
System.arraycopy(outData, 0, iframeData, mPpsSps.length, outData.length);
outData = iframeData;
System.arraycopy(mPpsSps, 0, mH264Buffer, 0, mPpsSps.length);
outputBuffer.get(mH264Buffer, mPpsSps.length, mBufferInfo.size);
mEasyRTMP.push(mH264Buffer, 0,mPpsSps.length+mBufferInfo.size,System.currentTimeMillis(), 1);
}
} else {
outputBuffer.get(mH264Buffer, 0, mBufferInfo.size);
if (System.currentTimeMillis() - timeStamp >= 3000) {
timeStamp = System.currentTimeMillis();
if (Build.VERSION.SDK_INT >= 23) {
Bundle params = new Bundle();
params.putInt(MediaCodec.PARAMETER_KEY_REQUEST_SYNC_FRAME, 0);
mMediaCodec.setParameters(params);
}
}
mEasyRTMP.push(mH264Buffer, 0, mBufferInfo.size, System.currentTimeMillis(), 1);
}

mEasyRTMP.push(outData, mBufferInfo.presentationTimeUs/1000, 1);
mMediaCodec.releaseOutputBuffer(index, false);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ static class CODE {
* @param data H264数据
* @param timestamp 时间戳,毫秒
*/
private native void push(long pusherObj, byte[] data, long timestamp, int type);
private native void push(long pusherObj, byte[] data, int offset, int length, long timestamp, int type);

/**
* 停止推送
Expand All @@ -67,12 +67,16 @@ public void stop() {
}

public void initPush(final String url, final Context context, final OnInitPusherCallback callback) {
String key = "79397037795A36526D3432412F517459702F51434A656876636D63755A57467A65575268636E64706269356C59584E35636E52746346634D5671442F7065424859585A7062695A4359574A76633246414D6A41784E6B566863336C4559584A33615735555A5746745A57467A65513D3D";
String key = "79397037795A36526D343041396F4E597033774A70656876636D63755A57467A65575268636E64706269356C59584E35636E52746346634D5671442F7065424859585A7062695A4359574A76633246414D6A41784E6B566863336C4559584A33615735555A5746745A57467A65513D3D";
mPusherObj = init(url, key, context, callback);
}

public void push(byte[] data, long timestamp, int type){
push(mPusherObj, data, timestamp,type);
push(data, 0, data.length, timestamp,type);
}

public void push(byte[] data, int offset, int length, long timestamp, int type){
push(mPusherObj, data, offset, length, timestamp,type);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import android.hardware.Camera;
import android.media.MediaCodec;
import android.media.MediaFormat;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;

Expand Down Expand Up @@ -165,7 +167,9 @@ public synchronized void startPreview() {
ByteBuffer[] inputBuffers;
byte[] dst;
ByteBuffer[] outputBuffers;
private long timeStamp = System.currentTimeMillis();
byte[] mPpsSps = new byte[0];
byte []h264 = new byte[(int) (width * height*3/2)];

@Override
public void onPreviewFrame(byte[] data, Camera camera) {
Expand All @@ -179,6 +183,12 @@ public void onPreviewFrame(byte[] data, Camera camera) {
return;
}

if(previewSize.width != width || previewSize.height != height){
Log.e(TAG, String.format("previewSize=%dx%d, not the setted value!", previewSize.width, previewSize.height));
mCamera.addCallbackBuffer(data);
return;
}

inputBuffers = mMediaCodec.getInputBuffers();
outputBuffers = mMediaCodec.getOutputBuffers();
dst = new byte[data.length];
Expand Down Expand Up @@ -208,24 +218,34 @@ public void onPreviewFrame(byte[] data, Camera camera) {
int outputBufferIndex = mMediaCodec.dequeueOutputBuffer(bufferInfo, 0);
while (outputBufferIndex >= 0) {
ByteBuffer outputBuffer = outputBuffers[outputBufferIndex];
byte[] outData = new byte[bufferInfo.size];
outputBuffer.get(outData);
int type = outputBuffer.get(4) & 0x1F;

// String data0 = String.format("%x %x %x %x %x %x %x %x %x %x ", outData[0], outData[1], outData[2], outData[3], outData[4], outData[5], outData[6], outData[7], outData[8], outData[9]);
// Log.e("out_data", data0);

//记录pps和sps
int type = outData[4] & 0x07;
if (type == 7 || type == 8) {
byte[] outData = new byte[bufferInfo.size];
outputBuffer.get(outData);
mPpsSps = outData;
} else if (type == 5) {
//在关键帧前面加上pps和sps数据
byte[] iframeData = new byte[mPpsSps.length + outData.length];
System.arraycopy(mPpsSps, 0, iframeData, 0, mPpsSps.length);
System.arraycopy(outData, 0, iframeData, mPpsSps.length, outData.length);
outData = iframeData;
System.arraycopy(mPpsSps, 0, h264, 0, mPpsSps.length);
outputBuffer.get(h264, mPpsSps.length, bufferInfo.size);
mEasyRTMP.push(h264, 0,mPpsSps.length+bufferInfo.size,System.currentTimeMillis(), 1);
} else {
outputBuffer.get(h264, 0, bufferInfo.size);
if (System.currentTimeMillis() - timeStamp >= 3000) {
timeStamp = System.currentTimeMillis();
if (Build.VERSION.SDK_INT >= 23) {
Bundle params = new Bundle();
params.putInt(MediaCodec.PARAMETER_KEY_REQUEST_SYNC_FRAME, 0);
mMediaCodec.setParameters(params);
}
}
mEasyRTMP.push(h264, 0, bufferInfo.size, System.currentTimeMillis(), 1);
}
mEasyRTMP.push(outData, System.currentTimeMillis(), 1);

mMediaCodec.releaseOutputBuffer(outputBufferIndex, false);
outputBufferIndex = mMediaCodec.dequeueOutputBuffer(bufferInfo, 0);
}
Expand Down
Binary file not shown.
Binary file modified Android/EasyRTMP/app/src/main/jniLibs/armeabi/libeasyrtmp.so
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@
android:layout_centerHorizontal="true"
android:layout_marginBottom="10dp"
android:gravity="center"
android:text="Copyright 2012-2016 www.EasyDarwin.org" />
android:text="Copyright 2012-2017 www.EasyDarwin.org" />

</RelativeLayout>
2 changes: 1 addition & 1 deletion Android/EasyRTMP/app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<resources>
<string name="app_name">EasyRTMP</string>
<string name="version">1.0</string>
<string name="version">1.1</string>
</resources>
4 changes: 2 additions & 2 deletions EasyRTMP_Demo/EasyRTMP_FILE/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ using namespace std;
#ifdef _WIN32
#include <windows.h>
#include "getopt.h"
#define KEY "6A34714D6C3469576B5A75414A553558714C48574F4F784659584E355548567A6147567958305A4A544555755A58686C567778576F50753434456468646D6C754A6B4A68596D397A595541794D4445325257467A65555268636E6470626C526C5957316C59584E35"
#define KEY "79397037795969576B5A7341396F4E597033774A7065354659584E35556C524E55463947535578464C6D56345A56634D5671442F7065424859585A7062695A4359574A76633246414D6A41784E6B566863336C4559584A33615735555A5746745A57467A65513D3D"
#else
#include "unistd.h"
#include <signal.h>
#define KEY "6A34714D6C354F576B5971414A553558714C485A4576426C59584E356348567A6147567958325A7062475658444661672F704C67523246326157346D516D466962334E68514449774D545A4659584E355247467964326C75564756686257566863336B3D"
#define KEY "79397037795A4F576B596F41396F4E597033774A7065354659584E35556C524E55463947535578464C6D56345A56634D5671442F7065424859585A7062695A4359574A76633246414D6A41784E6B566863336C4559584A33615735555A5746745A57467A65513D3D"
#endif

char* SRTMP= "rtmp://127.0.0.1/live/stream"; //Default RTMP Push StreamName
Expand Down
8 changes: 4 additions & 4 deletions EasyRTMP_Demo/EasyRTMP_RTSP/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
#include "EasyRTMPAPI.h"
#pragma comment(lib,"libeasyrtmp.lib")

#define RTSPURL "rtsp://admin:admin@192.168.31.100/11"
#define RTSPURL "rtsp://admin:admin@192.168.2.100/11"
//#define RTSPURL "rtsp://192.168.1.93:8554/524155"

//#define SRTMP "rtmp://124.193.154.4/live/stream"
//#define SRTMP "rtmp://w.gslb.lecloud.com/live/201610053000001k699?sign=35b318b30eb40642ba86374780e3e0e7&tm=20161005174352"
#define SRTMP "rtmp://127.0.0.1/live/kimpc"
#define SRTMP "rtmp://121.40.50.44/live/kimpckim"

#define BUFFER_SIZE 1024*1024

Expand Down Expand Up @@ -197,9 +197,9 @@ int Easy_APICALL __RTSPSourceCallBack( int _chid, void *_chPtr, int _mediatype,

int main()
{
EasyRTMP_Activate("79397037795969576B5A75412F517459702F51434A65354659584E35556C524E5546395356464E514C6D56345A56634D5671442F7065424859585A7062695A4359574A76633246414D6A41784E6B566863336C4559584A33615735555A5746745A57467A65513D3D");
EasyRTMP_Activate("79397037795969576B5A7341396F4E597033774A7065354659584E35556C524E5546395356464E514C6D56345A56634D5671442F7065424859585A7062695A4359574A76633246414D6A41784E6B566863336C4559584A33615735555A5746745A57467A65513D3D");
//创建EasyNVSource
EasyRTSP_Activate("6A59754D6A3469576B5A75412F517459702F51424575354659584E35556C524E5546395356464E514C6D56345A56634D5671442B6B75424859585A7062695A4359574A76633246414D6A41784E6B566863336C4559584A33615735555A5746745A57467A65513D3D");
EasyRTSP_Activate("6A59754D6A3469576B5A7341396F4E597033774A7065354659584E35556C524E5546395356464E514C6D56345A56634D5671442F7065424859585A7062695A4359574A76633246414D6A41784E6B566863336C4559584A33615735555A5746745A57467A65513D3D");
EasyRTSP_Init(&fNVSHandle);
if (NULL == fNVSHandle) return 0;

Expand Down
4 changes: 2 additions & 2 deletions EasyRTMP_Demo/EasyRTMP_SDK/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
#include "EasyRTMPAPI.h"
#include "EasyAACEncoderAPI.h"
#ifdef _WIN32
#define KEY "6A34714D6C3469576B5A75414A553558714C48574F4F314659584E355548567A6147567958314E455379356C65475868567778576F50753434456468646D6C754A6B4A68596D397A595541794D4445325257467A65555268636E6470626C526C5957316C59584E35"
#define KEY "79397037795969576B5A7341396F4E597033774A7065394659584E35556C524E55463954524573755A58686C6246634D5671442F7065424859585A7062695A4359574A76633246414D6A41784E6B566863336C4559584A33615735555A5746745A57467A65513D3D"
#include "getopt.h"
#else
#define KEY "6A34714D6C354F576B5971414A553558714C485A4576466C59584E356348567A6147567958334E6B61395A58444661672F704C67523246326157346D516D466962334E68514449774D545A4659584E355247467964326C75564756686257566863336B3D"
#define KEY "79397037795A4F576B596F41396F4E597033774A7065394659584E35556C524E55463954524573755A58686C316C634D5671442F7065424859585A7062695A4359574A76633246414D6A41784E6B566863336C4559584A33615735555A5746745A57467A65513D3D"
#include "unistd.h"
#include <signal.h>
#endif
Expand Down
Binary file modified EasyRTMP_Demo/Lib/libeasyrtmp.dll
Binary file not shown.
Binary file modified EasyRTMP_Demo/Lib/libeasyrtmp.lib
Binary file not shown.

0 comments on commit c1efbd4

Please sign in to comment.