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)\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_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");

    }
}
时间: 2024-12-29 11:30:07

Android ffmpeg rtmp(source code)的相关文章

Android Branch and master source code merge(patch)

Environment : Android 4.4.2 merge with Android 4.4.3(with other vendors source code) 1.确定你要merge 到 其他分支的版本,并在服务器测获得具体lable 对应的commit 或者 从build 对应的Repo Manifest 中找到要patch 到目标代码的Commit ID <?xml version="1.0" encoding="UTF-8"?> <

Pro Android学习笔记(十二):了解Intent(下)

解析Intent,寻找匹配Activity 如果给出component名字(包名.类名)是explicit intent,否则是implicit intent.对于explicit intent,关键就是component 名字,在<intent-fliter>中声明的其他属性被忽略.对于implicit intent,则根据action,category和data来进行匹配.然而一个intent fliter中可以声明多个actions,多个categories,多个data属性,因此可以满

[Android] 图片JNI(C++\Java)高斯模糊 多线程

在我的博客中,曾经发布了一篇高斯模糊(堆栈模糊)的文章:在其中使用了国外的一个堆栈模糊来实现对图片的模糊处理:同时弄了一个JNI C++ 的版本. 这篇文章依然是堆栈模糊:可以说最原始的地方还是堆栈模糊部分:只不过是支持多线程的. 纳尼??感情是之前那个不支持多线程?Sorry,我说错了:两个都是支持多线程调用的.不过新讲的这个是能在内部采用多线程进行分段模糊. 原来的:[Android]-图片JNI(C++\Java)高斯模糊的实现与比较 开工吧 说明:其中代码大部分来源于网络,不过都是开源的

Android学习路线(十五)Activity生命周期——重新创建(Recreating)一个Activity

先占个位置,下次翻译~ :p There are a few scenarios in which your activity is destroyed due to normal app behavior, such as when the user presses the Back button or your activity signals its own destruction by calling finish(). The system may also destroy your

Android学习路线(十二)Activity生命周期——启动一个Activity

先占个位置,过会儿来翻译,:p Unlike other programming paradigms in which apps are launched with a main()method, the Android system initiates code in an Activity instance by invoking specific callback methods that correspond to specific stages of its lifecycle. Th

【转】 Pro Android学习笔记(七四):HTTP服务(8):使用后台线程AsyncTask

目录(?)[-] 5秒超时异常 AsyncTask 实现AsyncTask抽象类 对AsyncTask的调用 在哪里运行 其他重要method 文章转载只能用于非商业性质,且不能带有虚拟货币.积分.注册等附加条件,转载须注明出处:http://blog.csdn.net/flowingflying/ 之前,我们直接在activity中执行http通信,在通信过程中可能会出现连接超时.socket超时等情况,超时阈值一般是秒级,例如AndroidHttpClient中设置的20秒,如果出现超时,就

android产品研发(十五)--&gt;内存对象序列化

转载请标明出处:一片枫叶的专栏 上一篇文章中我们讲解了android app中的升级更新操作,app的升级更新操作算是App的标配了,升级操作就是获取App的升级信息,更新操作是下载,安装,更新app,其中我们既可以使用app store获取应用的升级信息,也可以在应用内通过请求本地服务器获取应用的升级信息,并通过与本地app的版本号对比判断应用是否需要升级. 升级信息是app更新的基础,只有我们的app的升级信息指明需要更新,我们才可以开始后续的更新操作–也就是下载安装更新app.这里强调一点

【转】 Pro Android学习笔记(七八):服务(3):远程服务:AIDL文件

目录(?)[-] 在AIDL中定义服务接口 根据AIDL文件自动生成接口代码 文章转载只能用于非商业性质,且不能带有虚拟货币.积分.注册等附加条件.转载须注明出处:http://blog.csdn.net/flowingflying/ Remote Service在之前的学习笔记 Android学习笔记(五三):服务Service(下)- Remote Service中介绍过.远程服允许行其他应用调用,及允许RPC(remote procedure call).在Android中remote需要

Android开发实战(十八):Android Studio 优秀插件:GsonFormat

原文:Android开发实战(十八):Android Studio 优秀插件:GsonFormat Android Studio 优秀插件系列: Android Studio 优秀插件(一):GsonFormat ------------------------------------------------------------------------------------------------------- 这几天没有活,于是乎整理了一些代码,顺便把一些一直在使用的东西也整理下,然后学