Skip to content

Commit

Permalink
完成摄像头RTMP推流
Browse files Browse the repository at this point in the history
  • Loading branch information
eric committed Nov 24, 2017
1 parent 73916e0 commit ce99897
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
18 changes: 16 additions & 2 deletions rtmpfile/src/main/cpp/ffmpeg_handle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ int y_length;
int uv_length;
int width = 480;
int height = 320;
int fps = 20;
int fps = 15;

/**
* 初始化
Expand Down Expand Up @@ -490,7 +490,6 @@ Java_com_wangheart_rtmpfile_ffmpeg_FFmpegHandle_onFrameCallback(JNIEnv *env, job
avError(ret);
return -2;
}
count++;
enc_pkt.stream_index = video_st->index;
AVRational time_base = ofmt_ctx->streams[0]->time_base;//{ 1, 1000 };
enc_pkt.pts = count * (video_st->time_base.den) / ((video_st->time_base.num) * fps);
Expand All @@ -504,10 +503,25 @@ Java_com_wangheart_rtmpfile_ffmpeg_FFmpegHandle_onFrameCallback(JNIEnv *env, job
(long long) enc_pkt.duration,
time_base.num, time_base.den);
enc_pkt.pos = -1;
// AVRational time_base_q = {1, AV_TIME_BASE};
// //计算视频播放时间
// int64_t pts_time = av_rescale_q(enc_pkt.dts, time_base, time_base_q);
// //计算实际视频的播放时间
// if (count == 0) {
// startTime = av_gettime();
// }
// int64_t now_time = av_gettime() - startTime;
// __android_log_print(ANDROID_LOG_WARN, "eric", "delt time :%lld", (pts_time - now_time));
// if (pts_time > now_time) {
// //睡眠一段时间(目的是让当前视频记录的播放时间与实际时间同步)
// av_usleep((unsigned int) (pts_time - now_time));
// }

ret = av_interleaved_write_frame(ofmt_ctx, &enc_pkt);
if (ret != 0) {
loge("av_interleaved_write_frame failed");
}
count++;
env->ReleaseByteArrayElements(buffer_, in, 0);
return 0;

Expand Down
15 changes: 11 additions & 4 deletions rtmpfile/src/main/java/com/wangheart/rtmpfile/CameraActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ private void init() {
mHolder = sv.getHolder();
mHolder.addCallback(this);
String path = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "CameraDemo1" + File.separator + "test.flv";
FFmpegHandle.getInstance().initVideo(path);
FFmpegHandle.getInstance().initVideo(url);
}

@Override
Expand Down Expand Up @@ -103,8 +103,15 @@ private Camera getCamera() {
try {
Camera.Parameters parameters = camera.getParameters();
//对拍照参数进行设置
for (Camera.Size size : parameters.getSupportedPictureSizes()) {
LogUtils.d(size.width + " " + size.height);
}
LogUtils.d("============");
for (Camera.Size size : parameters.getSupportedPreviewSizes()) {
LogUtils.d(size.width + " " + size.height);
}
parameters.setPreviewSize(screenWidth, screenHeight); // 设置预览照片的大小
parameters.setPreviewFpsRange(20000, 30000);
parameters.setPreviewFpsRange(30000, 30000);
parameters.setPictureFormat(ImageFormat.NV21); // 设置图片格式
parameters.setPictureSize(screenWidth, screenHeight); // 设置照片的大小
camera.setParameters(parameters);
Expand Down Expand Up @@ -178,7 +185,7 @@ protected Void doInBackground(Void... params) {
if (mData != null) {
encodeTime = System.currentTimeMillis();
FFmpegHandle.getInstance().onFrameCallback(mData);
LogUtils.d(count++ + "消耗时间:" + (System.currentTimeMillis() - encodeTime));
LogUtils.d((++count) + "消耗时间:" + (System.currentTimeMillis() - encodeTime));
}
return null;
}
Expand Down Expand Up @@ -215,7 +222,7 @@ public void run() {
LogUtils.w("编码第:" + (encodeCount++) + "帧,耗时:" + (System.currentTimeMillis() - encodeTime));
}
});
LogUtils.d("采集第:" + (count++) + "帧,距上一帧间隔时间:"
LogUtils.d("采集第:" + (++count) + "帧,距上一帧间隔时间:"
+ (endTime - previewTime) + " " + Thread.currentThread().getName());
previewTime = endTime;
}
Expand Down

0 comments on commit ce99897

Please sign in to comment.