Android ffmpeg rtmp(source code)

來源:互聯網
上載者:User

標籤:產生   ldl   source   this   break   zha   java   void   register   

souce code:

 

Android.mk

  編譯產生APK需要調用的so檔案

 

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語言實現檔案
  編寫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   {10       char info[1000]= { 0 };11       AVFormatContext* pFormatCtx = NULL;12       av_register_all();13       avformat_network_init();14       pFormatCtx = avformat_alloc_context();15       //char* url="/storage/emulated/0/1.mp4";16       char* 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 //      }22       int 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 28       if(!pFormatCtx)29           return (*env)->NewStringUTF(env,"pFormat is NULL!");30 31       if(avformat_find_stream_info(pFormatCtx,NULL) < 0)32       {33           return (*env)->NewStringUTF(env,"Did not find info");34       }35       int videoIndex = -1;36       int i;37       for (i = 0; i <pFormatCtx->nb_streams ; ++i)38       {39         if(pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO)40         {41             videoIndex = i;42             break;43         }44       }45       if(videoIndex == -1)46       {47           return (*env)->NewStringUTF(env,"Did not find video steam!");48       }49 50       AVCodecContext* pCodecCtx = pFormatCtx->streams[videoIndex]->codec;51       AVCodec* pCodec = avcodec_find_decoder(pCodecCtx->codec_id);52 53       if(pCodec == NULL)54           return (*env)->NewStringUTF(env,"Did not find video decoder");55 56       if(avcodec_open2(pCodecCtx,pCodec,NULL) < 0)57       {58           return (*env)->NewStringUTF(env,"avcodec_open2 failed!");59       }60 61 62       //sprintf(info,"%s\n",avcodec_configuration());63       sprintf(info,"[Input:] %s\n",url);64       sprintf(info,"%s[Format:] %s\n",info,pFormatCtx->iformat->name);65       sprintf(info,"%s[codec:] %s\n",info,pCodecCtx->codec->name);66       sprintf(info,"%s[Resolution:] %dx%d\n",info,pCodecCtx->width,pCodecCtx->height);67      return (*env)->NewStringUTF(env,info);68   }69 70 /*71  * Class:     com_example_zhaohu_test_MainActivity72  * Method:    unimplementedStringFromJNI73  * Signature: ()Ljava/lang/String;74  */75 jstring JNICALL Java_com_example_zhaohu_test_MainActivity_unimplementedStringFromJNI76   (JNIEnv *env, jobject jObj)77   {78     return (*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 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)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.