您当前的位置: 首页 > 学无止境 > 心得笔记 网站首页心得笔记
2. testopengles_shader项目配置和创建_完成java部分代码~1
发布时间:2021-06-05 15:02:35编辑:雪饮阅读()
這第一步我們要準備一個yuv數據。
C:\Users\Administrator>cd D:\software\ffmpegwin\ffmpeg\ffmpeg-20180605-b748772-win64-static\bin
C:\Users\Administrator>d:
D:\software\ffmpegwin\ffmpeg\ffmpeg-20180605-b748772-win64-static\bin>ffmpeg.exe -i 1080.mp4 -pix_fmt yuv420p -s 424x240 out.yuv
ffmpeg version N-91230-gb74877206e Copyright (c) 2000-2018 the FFmpeg developers
built with gcc 7.3.0 (GCC)
configuration: --enable-gpl --enable-version3 --enable-sdl2 --enable-bzlib --enable-fontconfig --enable-gnutls --enable-iconv --enable-libass --enable-libbluray --enable-libfreetype --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libopus --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libtheora --enable-libtwolame --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libzimg --enable-lzma --enable-zlib --enable-gmp --enable-libvidstab --enable-libvorbis --enable-libvo-amrwbenc --enable-libmysofa --enable-libspeex --enable-libxvid --enable-libaom --enable-libmfx --enable-amf --enable-ffnvcodec --enable-cuvid --enable-d3d11va --enable-nvenc --enable-nvdec --enable-dxva2 --enable-avisynth
libavutil 56. 18.102 / 56. 18.102
libavcodec 58. 19.105 / 58. 19.105
libavformat 58. 17.100 / 58. 17.100
libavdevice 58. 4.100 / 58. 4.100
libavfilter 7. 25.100 / 7. 25.100
libswscale 5. 2.100 / 5. 2.100
libswresample 3. 2.100 / 3. 2.100
libpostproc 55. 2.100 / 55. 2.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '1080.mp4':
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2avc1mp41
encoder : Lavf57.71.100
description : Codec by Bilibili XCode Worker v4.6.24(fixed_gap:False)
Duration: 00:04:52.08, start: 0.000000, bitrate: 230 kb/s
Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 426x240, 187 kb/s, 29.97 fps, 29.97 tbr, 16k tbn, 59.94 tbc (default)
Metadata:
handler_name : VideoHandler
Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, mono, fltp, 32 kb/s (default)
Metadata:
handler_name : SoundHandler
Stream mapping:
Stream #0:0 -> #0:0 (h264 (native) -> rawvideo (native))
Press [q] to stop, [?] for help
Output #0, rawvideo, to 'out.yuv':
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2avc1mp41
description : Codec by Bilibili XCode Worker v4.6.24(fixed_gap:False)
encoder : Lavf58.17.100
Stream #0:0(und): Video: rawvideo (I420 / 0x30323449), yuv420p, 424x240, q=2-31, 36597 kb/s, 29.97 fps, 29.97 tbn, 29.97 tbc (default)
Metadata:
handler_name : VideoHandler
encoder : Lavc58.19.105 rawvideo
frame= 8751 fps=876 q=-0.0 Lsize= 1304446kB time=00:04:51.99 bitrate=36597.0kbits/s speed=29.2x
video:1304446kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.000000%
然後我們再次新建一個項目,專門用於讀取yuv的安卓項目(同樣支持c++)
該項目中清單文件中同樣需要文件讀取權限:AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.yuv">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Yuv">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.yuv">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Yuv">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
接下來我們要創建具體的yuv播放的實現的java代碼:XPlay.java于MainActivity.java同級目錄:
package com.example.yuv;
import android.content.Context;
import android.opengl.GLSurfaceView;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
public class XPlay extends GLSurfaceView implements Runnable, SurfaceHolder.Callback {
public XPlay(Context context, AttributeSet attrs){
super(context,attrs);
}
@Override
public void run(){
Open("/sdcard/out.yuv",getHolder().getSurface());
}
@Override
public void surfaceCreated(SurfaceHolder var1){
new Thread(this).start();
}
@Override
public void surfaceChanged(SurfaceHolder var1,int var2,int var3,int var4){
}
@Override
public void surfaceDestroyed(SurfaceHolder var1){
}
public native void Open(String url,Object surface);
}
然後cpp/CMakeLists.txt中要添加鏈接3個庫:
# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html
# Sets the minimum version of CMake required to build the native library.
cmake_minimum_required(VERSION 3.10.2)
# Declares and names the project.
project("yuv")
# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.
add_library( # Sets the name of the library.
native-lib
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
native-lib.cpp )
# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.
find_library( # Sets the name of the path variable.
log-lib
# Specifies the name of the NDK library that
# you want CMake to locate.
log )
# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.
target_link_libraries( # Specifies the target library.
native-lib
GLESv2
EGL
android
# Links the target library to the log library
# included in the NDK.
${log-lib} )
然後佈局界面layout/activity_main.xml中要添加對於yuv的區域:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/sample_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.example.yuv.XPlay
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
那麽cpp/native-lib.cpp:中就暫時初始化一個沒有完全實現的JNI空方法。
#include <jni.h>
#include <string>
extern "C" JNIEXPORT jstring JNICALL
Java_com_example_yuv_MainActivity_stringFromJNI(
JNIEnv* env,
jobject /* this */) {
std::string hello = "Hello from C++";
return env->NewStringUTF(hello.c_str());
}extern "C"
JNIEXPORT void JNICALL
Java_com_example_yuv_XPlay_Open(JNIEnv *env, jobject thiz, jstring url, jobject surface) {
// TODO: implement Open()
}
由於現在只是把結構搭建處理,具體的邏輯還沒有處理,所以最後部署並運行的結果就是一個空白的黑屏而已。
关键字词:shader,java