zoukankan      html  css  js  c++  java
  • 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   {
    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)
    ",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
    ",avcodec_configuration());
    63       sprintf(info,"[Input:] %s
    ",url);
    64       sprintf(info,"%s[Format:] %s
    ",info,pFormatCtx->iformat->name);
    65       sprintf(info,"%s[codec:] %s
    ",info,pCodecCtx->codec->name);
    66       sprintf(info,"%s[Resolution:] %dx%d
    ",info,pCodecCtx->width,pCodecCtx->height);
    67      return (*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   {
    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");
    
        }
    }
    
    作者:长风 Email:844064492@qq.com QQ群:607717453 Git:https://github.com/zhaohu19910409Dz 开源项目:https://github.com/OriginMEK/MEK 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利. 感谢您的阅读。如果觉得有用的就请各位大神高抬贵手“推荐一下”吧!你的精神支持是博主强大的写作动力。 如果觉得我的博客有意思,欢迎点击首页左上角的“+加关注”按钮关注我!
  • 相关阅读:
    php 高级 提高PHP代码的性能10条建议
    CSRF预防手段
    如何在PHP中防止SQL注入
    网络安全 如何防范 XSS 攻击
    php 算法知识 冒泡排序
    php 基础知识 常见面试题
    MySQL高级 InnoDB 和 MyISAM 的区别
    php 算法知识 猴子选大王
    MySQL高级-索引1
    [POI2007]POW-The Flood(并查集)
  • 原文地址:https://www.cnblogs.com/zhaohu/p/7148203.html
Copyright © 2011-2022 走看看