Souce Code:
Android.mk
Compile to generate the so file that the APK needs to call
Local_path:= $ (call My-dir) include $ (clear_vars) local_module:= libavcodeclocal_src_files:= lib/ Libavcodec-57.solocal_export_c_includes: = $ (Local_path)/includeinclude $ (prebuilt_shared_library) include $ (CLEAR_ VARS) local_module:= libavformatlocal_src_files:= lib/libavformat-57.solocal_export_c_includes: = $ (LOCAL_PATH)/ Includeinclude $ (prebuilt_shared_library) include $ (clear_vars) local_module:= libswscalelocal_src_files:= lib/ Libswscale-4.solocal_export_c_includes: = $ (Local_path)/includeinclude $ (prebuilt_shared_library) include $ (CLEAR_ VARS) local_module:= libavutillocal_src_files:= lib/libavutil-55.solocal_export_c_includes: = $ (LOCAL_PATH)/ Includeinclude $ (prebuilt_shared_library) include $ (clear_vars) local_module:= libavfilterlocal_src_files:= lib/ Libavfilter-6.solocal_export_c_includes: = $ (Local_path)/includeinclude $ (prebuilt_shared_library) include $ (CLEAR_ VARS) local_module:= libswresamplelocal_src_files:= lib/libswresample-2.solocal_export_c_includes: = $ (LOCAL_PATH)/ IncludeincludE $ (prebuilt_shared_library) #Programinclude $ (clear_vars) #LOCAL_ALLOW_UNDEFINED_SYMBOLS: = Truelocal_module: = Hellolocal_src_files: = Main.clocal_c_includes + = $ (local_path)/includelocal_ldlibs: =-llog-lzlocal_shared_ LIBRARIES: =avcodec avdevice avfilter avformat avutil postproc swresample swscaleinclude $ (build_shared_library)
C Language Implementation file
Write C, C + + files to implement the underlying logic functions, eventually compiled to so file by Java call
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_stringfromjni8(JNIENV *env, Jobject jobj)9 {Ten Charinfo[ +]= {0 }; Oneavformatcontext* Pformatctx =NULL; A Av_register_all (); - avformat_network_init (); -Pformatctx =Avformat_alloc_context (); the //char* url= "/storage/emulated/0/1.mp4"; - Char* Url="rtmp://live.hkstv.hk.lxdns.com/live/hks"; - - //if (avformat_open_input (&pformatctx,url,null,null)! = 0) + // { - //return (*env)->newstringutf (env, "Open URL failed!"); + // } A intret = Avformat_open_input (&pformatctx,url,null,null); at //char buf[1024] = {0}; - //Av_strerror (ret,buf,1024); - //sprintf (info, "couldn ' t Open file%s:%d (%s) \ n", url,ret,buf); - //return (*env)->newstringutf (env,info); - - if(!pformatctx) in return(*env)->newstringutf (env,"Pformat is null!"); - to if(Avformat_find_stream_info (Pformatctx,null) <0) + { - return(*env)->newstringutf (env,"did not find info"); the } * intVideoindex =-1; $ inti;Panax Notoginseng for(i =0; I <pformatctx->nb_streams; ++i) - { the if(Pformatctx->streams[i]->codec->codec_type = =Avmedia_type_video) + { AVideoindex =i; the Break; + } - } $ if(Videoindex = =-1) $ { - return(*env)->newstringutf (env,"Did not find video steam!"); - } the -avcodeccontext* Pcodecctx = pformatctx->streams[videoindex]->codec;Wuyiavcodec* Pcodec = Avcodec_find_decoder (pcodecctx->codec_id); the - if(Pcodec = =NULL) Wu return(*env)->newstringutf (env,"Did not find video decoder"); - About if(Avcodec_open2 (Pcodecctx,pcodec,null) <0) $ { - return(*env)->newstringutf (env,"avcodec_open2 failed!"); - } - A + //sprintf (info, "%s\n", Avcodec_configuration ()); thesprintf (Info,"[Input:]%s\n", URL); -sprintf (Info,"%s[format:]%s\n",info,pformatctx->iformat->name); $sprintf (Info,"%s[codec:]%s\n",info,pcodecctx->codec->name); thesprintf (Info,"%s[resolution:]%dx%d\n",info,pcodecctx->width,pcodecctx->height); the return(*env)Newstringutf (env,info); the } the - /* in * class:com_example_zhaohu_test_mainactivity the * Method:unimplementedstringfromjni the * Signature: () ljava/lang/string; About */ the jstring Jnicall Java_com_example_zhaohu_test_mainactivity_unimplementedstringfromjni the(JNIENV *env, Jobject jobj) the { + return(*env)->newstringutf (env,"Java_com_example_zhaohu_test_mainactivity_unimplementedstringfromjni"); -}
Implementing Android Code
Invoke the C,c++ implementation file
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 String stringfromjni (); Public native String unimplementedstringfromjni (); 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)