diff --git a/README.md b/README.md
index 34b7d79..76de46e 100644
--- a/README.md
+++ b/README.md
@@ -17,9 +17,12 @@ android端基于FFmpeg库在中的使用。
- #### 视频添加水印
- #### 图片合成视频
- #### 视频解码播放
-
+- #### 本地直播推流
+
+左边是ffplay客户端拉流播放,中间是web网页播放:
+
***
-后续会加上音视频解码同步播放、直播推流。
+后续会加上音视频解码同步播放。
diff --git a/app/CMakeLists.txt b/app/CMakeLists.txt
index 3690aac..c192646 100644
--- a/app/CMakeLists.txt
+++ b/app/CMakeLists.txt
@@ -24,7 +24,8 @@ add_library( # Sets the name of the library.
src/main/cpp/ffmpeg_opt.c
src/main/cpp/audio_player.c
src/main/cpp/openSL_audio_player.c
- src/main/cpp/video_player.c)
+ src/main/cpp/video_player.c
+ src/main/cpp/ffmpeg_pusher.cpp)
add_library( ffmpeg
SHARED
diff --git a/app/build.gradle b/app/build.gradle
index 58ef199..249b53b 100644
--- a/app/build.gradle
+++ b/app/build.gradle
@@ -42,6 +42,7 @@ android {
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
+ compile 'com.android.support.constraint:constraint-layout:1.0.2'
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml
index d34302a..352a7bb 100644
--- a/app/src/main/AndroidManifest.xml
+++ b/app/src/main/AndroidManifest.xml
@@ -20,23 +20,25 @@
android:screenOrientation="landscape"
android:supportsRtl="true"
android:theme="@style/AppTheme">
-
+
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
diff --git a/app/src/main/cpp/ffmpeg.c b/app/src/main/cpp/ffmpeg.c
index e303ab2..4dfb092 100644
--- a/app/src/main/cpp/ffmpeg.c
+++ b/app/src/main/cpp/ffmpeg.c
@@ -31,7 +31,6 @@
#include
#include
#include
-#include "logjam.h"
#if HAVE_IO_H
#include
diff --git a/app/src/main/cpp/ffmpeg_pusher.cpp b/app/src/main/cpp/ffmpeg_pusher.cpp
new file mode 100644
index 0000000..c36430c
--- /dev/null
+++ b/app/src/main/cpp/ffmpeg_pusher.cpp
@@ -0,0 +1,178 @@
+//
+// Created by frank on 2018/2/2.
+//
+
+#include
+#include
+#include
+
+#define LOG_TAG "FFmpegLive"
+#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)
+#define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+#include
+#include
+#include
+#include
+
+ JNIEXPORT jint JNICALL
+ Java_com_frank_ffmpeg_Pusher_pushStream(JNIEnv *env, jobject, jstring filePath, jstring liveUrl) {
+
+ AVOutputFormat *output_format = NULL;
+ AVFormatContext *in_format = NULL, *out_format = NULL;
+ AVPacket packet;
+ const char *file_path, *live_url;
+ int video_index = -1;
+ int ret = 0, i;
+ int frame_index = 0;
+ int64_t start_time = 0;
+
+ file_path = env->GetStringUTFChars(filePath, NULL);
+ live_url = env->GetStringUTFChars(liveUrl, NULL);
+
+ LOGI("file_path=%s", file_path);
+ LOGI("live_url=%s", live_url);
+
+ //注册所有组件
+ av_register_all();
+ //初始化网络
+ avformat_network_init();
+ //打开输入文件
+ if((ret = avformat_open_input(&in_format, file_path, 0, 0)) < 0){
+ LOGE("could not open input file...");
+ goto end;
+ }
+ //寻找流信息
+ if((ret = avformat_find_stream_info(in_format, 0)) < 0){
+ LOGE("could not find stream info...");
+ goto end;
+ }
+ //
+ for(i=0; inb_streams; i++){
+ //找到视频流
+ if(in_format->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO){
+ video_index = i;
+ break;
+ }
+ }
+ av_dump_format(in_format, 0, file_path, 0);
+ //分配输出封装格式上下文, rtmp协议支持格式为flv
+ avformat_alloc_output_context2(&out_format, NULL, "flv", live_url);
+ if(!out_format){
+ LOGE("could not alloc output context...");
+ ret = AVERROR_UNKNOWN;
+ goto end;
+ }
+ //根据输入流来创建输出流
+ for(i=0; inb_streams; i++){
+ AVStream *in_stream = in_format->streams[i];
+ AVStream *out_stream = avformat_new_stream(out_format, in_stream->codec->codec);
+ if(!out_stream){
+ LOGE("could not alloc output stream...");
+ ret = AVERROR_UNKNOWN;
+ goto end;
+ }
+ //复制封装格式上下文
+ ret = avcodec_copy_context(out_stream->codec, in_stream->codec);
+ if(ret < 0){
+ LOGE("could not copy context...");
+ goto end;
+ }
+ out_stream->codec->codec_tag = 0;
+ if(out_format->oformat->flags & AVFMT_GLOBALHEADER){
+ out_stream->codec->flags |= CODEC_FLAG_GLOBAL_HEADER;
+ }
+ }
+ //封装格式
+ output_format = out_format->oformat;
+ //打开输出文件/URL
+ if(!(output_format->flags & AVFMT_NOFILE)){
+ ret = avio_open(&out_format->pb, live_url, AVIO_FLAG_WRITE);
+ if(ret < 0){
+ LOGE("could not open output url '%s'", live_url);
+ goto end;
+ }
+ }
+ //写文件头
+ ret = avformat_write_header(out_format, NULL);
+ if(ret < 0){
+ LOGE("could not write header...");
+ goto end;
+ }
+ //获取开始时间
+ start_time = av_gettime();
+ //开始循环读一帧数据
+ while (1){
+ AVStream *in_stream, *out_stream;
+ ret = av_read_frame(in_format, &packet);
+ if(ret < 0){
+ break;
+ }
+ //计算帧间隔,参考时钟/采样率
+ if(packet.pts == AV_NOPTS_VALUE){
+ AVRational time_base = in_format->streams[video_index]->time_base;
+ int64_t cal_duration = (int64_t)(AV_TIME_BASE/av_q2d(in_format->streams[video_index]->r_frame_rate));
+ packet.pts = (int64_t)((frame_index * cal_duration)/(av_q2d(time_base) * AV_TIME_BASE));
+ packet.dts = packet.pts;
+ packet.duration = (int64_t)(cal_duration/(av_q2d(time_base) * AV_TIME_BASE));
+ }
+ //视频帧之间延时
+ if(packet.stream_index == video_index){
+ AVRational time_base = in_format->streams[video_index]->time_base;
+ AVRational time_base_q = {1, AV_TIME_BASE};
+ int64_t pts_time = av_rescale_q(packet.dts, time_base, time_base_q);
+ int64_t now_time = av_gettime() - start_time;
+ //延时以保持同步
+ if(pts_time > now_time){
+ av_usleep((unsigned int)(pts_time - now_time));
+ }
+ }
+ in_stream = in_format->streams[packet.stream_index];
+ out_stream = out_format->streams[packet.stream_index];
+
+ //pts/dts转换
+ packet.pts = av_rescale_q_rnd(packet.pts, in_stream->time_base, out_stream->time_base,
+ (AVRounding)(AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX));
+ packet.dts = av_rescale_q_rnd(packet.dts, in_stream->time_base, out_stream->time_base,
+ (AVRounding)(AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX));
+ packet.duration = av_rescale_q(packet.duration, in_stream->time_base, out_stream->time_base);
+ packet.pos = -1;
+
+ //视频帧计数
+ if(packet.stream_index == video_index){
+ frame_index ++;
+ LOGI("write frame = %d", frame_index);
+ }
+ //写一帧数据
+ ret = av_interleaved_write_frame(out_format, &packet);
+ if(ret < 0){
+ LOGE("could not write frame...");
+ break;
+ }
+ //释放包数据内存
+ av_packet_unref(&packet);
+ }
+ //写文件尾
+ av_write_trailer(out_format);
+
+ end:
+ avformat_close_input(&in_format);
+ if(out_format && !(out_format->flags & AVFMT_NOFILE)){
+ avio_close(out_format->pb);
+ }
+ avformat_free_context(in_format);
+ avformat_free_context(out_format);
+ env->ReleaseStringUTFChars(filePath, file_path);
+ env->ReleaseStringUTFChars(liveUrl, live_url);
+ if(ret < 0 && ret != AVERROR_EOF){
+ return -1;
+ }
+ return 0;
+ }
+
+#ifdef __cplusplus
+}
+#endif
\ No newline at end of file
diff --git a/app/src/main/java/com/frank/ffmpeg/Pusher.java b/app/src/main/java/com/frank/ffmpeg/Pusher.java
new file mode 100644
index 0000000..3ce1a7a
--- /dev/null
+++ b/app/src/main/java/com/frank/ffmpeg/Pusher.java
@@ -0,0 +1,16 @@
+package com.frank.ffmpeg;
+
+/**
+ * 使用FFmepg进行推流直播
+ * Created by frank on 2018/2/2.
+ */
+
+public class Pusher {
+ static {
+ System.loadLibrary("media-handle");
+ }
+
+ //选择本地文件推流到指定平台直播
+ public native int pushStream(String filePath, String liveUrl);
+
+}
diff --git a/app/src/main/java/com/frank/ffmpeg/activity/MainActivity.java b/app/src/main/java/com/frank/ffmpeg/activity/MainActivity.java
index 27173ec..05811c2 100644
--- a/app/src/main/java/com/frank/ffmpeg/activity/MainActivity.java
+++ b/app/src/main/java/com/frank/ffmpeg/activity/MainActivity.java
@@ -24,6 +24,7 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
findViewById(R.id.btn_audio).setOnClickListener(this);
findViewById(R.id.btn_media).setOnClickListener(this);
findViewById(R.id.btn_video).setOnClickListener(this);
+ findViewById(R.id.btn_push).setOnClickListener(this);
}
@Override
@@ -38,6 +39,9 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
case R.id.btn_video://视频处理
startActivity(new Intent(MainActivity.this, VideoHandleActivity.class));
break;
+ case R.id.btn_push://FFmpeg推流
+ startActivity(new Intent(MainActivity.this, PushActivity.class));
+ break;
default:
break;
}
diff --git a/app/src/main/java/com/frank/ffmpeg/activity/PushActivity.java b/app/src/main/java/com/frank/ffmpeg/activity/PushActivity.java
new file mode 100644
index 0000000..c7b02b3
--- /dev/null
+++ b/app/src/main/java/com/frank/ffmpeg/activity/PushActivity.java
@@ -0,0 +1,72 @@
+package com.frank.ffmpeg.activity;
+
+import android.support.v7.app.AppCompatActivity;
+import android.os.Bundle;
+import android.text.TextUtils;
+import android.util.Log;
+import android.view.View;
+import android.widget.Button;
+import android.widget.EditText;
+import android.widget.Toast;
+
+import com.frank.ffmpeg.Pusher;
+import com.frank.ffmpeg.R;
+
+import java.io.File;
+
+/**
+ * 使用ffmpeg推流直播
+ * Created by frank on 2018/2/2.
+ */
+public class PushActivity extends AppCompatActivity {
+
+ private static final String TAG = PushActivity.class.getSimpleName();
+ private static final String FILE_PATH = "storage/emulated/0/hello.flv";
+ private static final String LIVE_URL = "rtmp://192.168.1.104/live/stream";
+
+ @Override
+ protected void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ setContentView(R.layout.activity_push);
+
+ initView();
+ }
+
+ private void initView() {
+ final EditText edit_file_path = (EditText) findViewById(R.id.edit_file_path);
+ final EditText edit_live_url = (EditText) findViewById(R.id.edit_live_url);
+ edit_file_path.setText(FILE_PATH);
+ edit_live_url.setText(LIVE_URL);
+
+ Button btn_push_stream = (Button)findViewById(R.id.btn_push_stream);
+
+ btn_push_stream.setOnClickListener(new View.OnClickListener() {
+ @Override
+ public void onClick(View v) {
+ //TODO 视频文件格式为flv
+ final String filePath = edit_file_path.getText().toString();
+ final String liveUrl = edit_live_url.getText().toString();
+ Log.i(TAG, "filePath=" + filePath);
+ Log.i(TAG, "liveUrl=" + liveUrl);
+
+ if(!TextUtils.isEmpty(filePath) && !TextUtils.isEmpty(filePath)){
+ File file = new File(filePath);
+ //判断文件是否存在
+ if(file.exists()){
+ //开启子线程
+ new Thread(new Runnable() {
+ @Override
+ public void run() {
+ //开始推流
+ new Pusher().pushStream(filePath, liveUrl);
+ }
+ }).start();
+ }else {
+ Toast.makeText(PushActivity.this, "文件不存在", Toast.LENGTH_SHORT).show();
+ }
+ }
+ }
+ });
+ }
+
+}
diff --git a/app/src/main/java/com/frank/ffmpeg/activity/VideoHandleActivity.java b/app/src/main/java/com/frank/ffmpeg/activity/VideoHandleActivity.java
index ac79c86..39428f0 100644
--- a/app/src/main/java/com/frank/ffmpeg/activity/VideoHandleActivity.java
+++ b/app/src/main/java/com/frank/ffmpeg/activity/VideoHandleActivity.java
@@ -201,7 +201,7 @@ public class VideoHandleActivity extends AppCompatActivity implements View.OnCli
String combineVideo = PATH + File.separator + "combineVideo.mp4";
commandLine = FFmpegUtil.pictureToVideo(picturePath, combineVideo);
break;
- case 8:
+ case 8://视频解码播放
startActivity(new Intent(VideoHandleActivity.this, VideoPlayerActivity.class));
return;
default:
diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml
index a1749bc..3ab3ac2 100644
--- a/app/src/main/res/layout/activity_main.xml
+++ b/app/src/main/res/layout/activity_main.xml
@@ -29,4 +29,13 @@
android:layout_centerHorizontal="true"
android:layout_below="@+id/btn_media"/>
+
+
diff --git a/app/src/main/res/layout/activity_push.xml b/app/src/main/res/layout/activity_push.xml
new file mode 100644
index 0000000..76718d3
--- /dev/null
+++ b/app/src/main/res/layout/activity_push.xml
@@ -0,0 +1,35 @@
+
+
+
+
+
+
+
+
+
+
+
diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml
index 1960755..feca6d5 100644
--- a/app/src/main/res/values/strings.xml
+++ b/app/src/main/res/values/strings.xml
@@ -10,6 +10,7 @@
音频处理
音视频处理
视频处理
+ 推流直播
音视频合成
提取音频
diff --git a/gif/live.gif b/gif/live.gif
new file mode 100644
index 0000000..4fcb747
Binary files /dev/null and b/gif/live.gif differ
diff --git a/mp4/live.mp4 b/mp4/live.mp4
new file mode 100644
index 0000000..0ef2a1f
Binary files /dev/null and b/mp4/live.mp4 differ
diff --git a/picture/live.png b/picture/live.png
new file mode 100644
index 0000000..edfaf51
Binary files /dev/null and b/picture/live.png differ