Android ffmpeg rtmp(source code)

卧疾丰暇豫,翰墨时间作。这篇文章主要讲述Android ffmpeg rtmp(source code)相关的知识,希望能为你提供帮助。
souce code:
 
android.mk
编译生成APK需要调用的so文件
 

LOCAL_PATH:= $(call my-dir)include $(CLEAR_VARS) LOCAL_MODULE:= libavcodec LOCAL_SRC_FILES:= lib/libavcodec-57.so LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include include $(PREBUILT_SHARED_LIBRARY)include $(CLEAR_VARS) LOCAL_MODULE:= libavformat LOCAL_SRC_FILES:= lib/libavformat-57.so LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include include $(PREBUILT_SHARED_LIBRARY)include $(CLEAR_VARS) LOCAL_MODULE:= libswscale LOCAL_SRC_FILES:= lib/libswscale-4.so LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include include $(PREBUILT_SHARED_LIBRARY)include $(CLEAR_VARS) LOCAL_MODULE:= libavutil LOCAL_SRC_FILES:= lib/libavutil-55.so LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include include $(PREBUILT_SHARED_LIBRARY)include $(CLEAR_VARS) LOCAL_MODULE:= libavfilter LOCAL_SRC_FILES:= lib/libavfilter-6.so LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include include $(PREBUILT_SHARED_LIBRARY)include $(CLEAR_VARS) LOCAL_MODULE:= libswresample LOCAL_SRC_FILES:= lib/libswresample-2.so LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include include $(PREBUILT_SHARED_LIBRARY)#Program include $(CLEAR_VARS) #LOCAL_ALLOW_UNDEFINED_SYMBOLS := true LOCAL_MODULE := hello LOCAL_SRC_FILES := main.c LOCAL_C_INCLUDES += $(LOCAL_PATH)/include LOCAL_LDLIBS := -llog -lz LOCAL_SHARED_LIBRARIES :=avcodec avdevice avfilter avformat avutil postproc swresample swscaleinclude $(BUILD_SHARED_LIBRARY)

 


 
C语言实现文件
编写C、C++文件实现底层的逻辑功能,最终编译为so文件被java调用

1 #include < jni.h> 2 #include < stdio.h> 3 #include "include/libavcodec/avcodec.h" 4 #include "include/libavformat/avformat.h" 5 #include "include/libavfilter/avfilter.h" 6 7 jstring JNICALL Java_com_example_zhaohu_test_MainActivity_stringFromJNI 8(JNIEnv *env, jobject jObj) 9{ 10char info[1000]= { 0 }; 11AVFormatContext* pFormatCtx = NULL; 12av_register_all(); 13avformat_network_init(); 14pFormatCtx = avformat_alloc_context(); 15//char* url="/storage/emulated/0/1.mp4"; 16char* url="rtmp://live.hkstv.hk.lxdns.com/live/hks"; 17 18 //if(avformat_open_input(& pFormatCtx,url,NULL,NULL) != 0) 19 //{ 20 //return (*env)-> NewStringUTF(env,"open url failed!"); 21 //} 22int ret = avformat_open_input(& pFormatCtx,url,NULL,NULL); 23//char buf[1024] = { 0 }; 24//av_strerror(ret,buf,1024); 25//sprintf(info,"Couldn‘t open file %s: %d(%s)\n",url,ret,buf); 26//return (*env)-> NewStringUTF(env,info); 27 28if(!pFormatCtx) 29return (*env)-> NewStringUTF(env,"pFormat is NULL!"); 30 31if(avformat_find_stream_info(pFormatCtx,NULL) < 0) 32{ 33return (*env)-> NewStringUTF(env,"Did not find info"); 34} 35int videoIndex = -1; 36int i; 37for (i = 0; i < pFormatCtx-> nb_streams ; ++i) 38{ 39if(pFormatCtx-> streams[i]-> codec-> codec_type == AVMEDIA_TYPE_VIDEO) 40{ 41videoIndex = i; 42break; 43} 44} 45if(videoIndex == -1) 46{ 47return (*env)-> NewStringUTF(env,"Did not find video steam!"); 48} 49 50AVCodecContext* pCodecCtx = pFormatCtx-> streams[videoIndex]-> codec; 51AVCodec* pCodec = avcodec_find_decoder(pCodecCtx-> codec_id); 52 53if(pCodec == NULL) 54return (*env)-> NewStringUTF(env,"Did not find video decoder"); 55 56if(avcodec_open2(pCodecCtx,pCodec,NULL) < 0) 57{ 58return (*env)-> NewStringUTF(env,"avcodec_open2 failed!"); 59} 60 61 62//sprintf(info,"%s\n",avcodec_configuration()); 63sprintf(info,"[Input:] %s\n",url); 64sprintf(info,"%s[Format:] %s\n",info,pFormatCtx-> iformat-> name); 65sprintf(info,"%s[codec:] %s\n",info,pCodecCtx-> codec-> name); 66sprintf(info,"%s[Resolution:] %dx%d\n",info,pCodecCtx-> width,pCodecCtx-> height); 67return (*env)-> NewStringUTF(env,info); 68} 69 70 /* 71* Class:com_example_zhaohu_test_MainActivity 72* Method:unimplementedStringFromJNI 73* Signature: ()Ljava/lang/String; 74*/ 75 jstring JNICALL Java_com_example_zhaohu_test_MainActivity_unimplementedStringFromJNI 76(JNIEnv *env, jobject jObj) 77{ 78return (*env)-> NewStringUTF(env,"Java_com_example_zhaohu_test_MainActivity_unimplementedStringFromJNI"); 79}

 

实现Android代码
调用C,C++的实现文件
package com.example.zhaohu.test; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.TextView; public class MainActivity extends AppCompatActivity {@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final TextView infoText = (TextView)findViewById(R.id.info); //infoText.setText("This is My Test"); infoText.setText(stringFromJNI()); }public native StringstringFromJNI(); public native StringunimplementedStringFromJNI(); static { System.loadLibrary("avcodec-57"); System.loadLibrary("avfilter-6"); System.loadLibrary("avformat-57"); System.loadLibrary("avutil-55"); System.loadLibrary("swresample-2"); System.loadLibrary("swscale-4"); System.loadLibrary("hello"); } }

【Android ffmpeg rtmp(source code)】 




    推荐阅读