Android ffmpeg rtmp(source code)
Posted 长风破浪会有时,直挂云帆济沧海
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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"); } }
以上是关于Android ffmpeg rtmp(source code)的主要内容,如果未能解决你的问题,请参考以下文章
Android 音视频深入 十五 FFmpeg 实现基于Rtmp协议的推流(附源码下载)
Android 音视频深入 十七 FFmpeg 获取RTMP流保存为flv (附源码下载)