Ubuntu compiles the ijkplayer so library and plays the local raw/assets file

1, Preliminary basic knowledge reserve

1. Official project address of ijkplayer: https://github.com/Bilibili/ijkplayer

Ijkplayer is a lightweight Android/iOS video player based on ffmpeg. The cross platform function is realized, and the API is easy to integrate; The compilation configuration can be tailored to facilitate the control of the installation package size; Support hardware accelerated decoding to save more power. More formats can be supported through compilation. It can be said that ijkplayer supports as long as it is a format supported by ffmpeg.

2. Ubuntu download address: https://cn.ubuntu.com/

The official Chinese name "youbangtuo" also has unofficial translations of Bantu, wubantu, wubantu, wubangtu, stupid rabbit and so on.

Ubuntu is a Linux operating system based on desktop applications. It is one of the most popular Linux systems in the world.

1) Experience under virtual machine:

Run the virtual machine and get a more complete experience in the virtual machine. Download and install the virtual machine. After successful installation, run the virtual machine, select the "management" → "import virtual computer" option in the menu bar, import the downloaded experience image into the virtual machine, and finally run the Ubuntu virtual machine, so as to obtain a more real experience. Almost all operations can be completed in the virtual machine without restrictions.

Cabbage windows 10 system installation Linux (ubuntu) virtual machine super detailed tutorial

In this paper, we choose this method to install ubuttu in Windows 10 system.

2) Make Live USB
There is another way to get a real experience, that is, to make LiveUSB, which goes further than virtual machines and runs directly in the real hardware environment, faster and more resource-saving.

Ubuntu18.04/20.04 complete novice installation tutorial

2, Start compiling

The specific compilation steps are as follows:

1. Configuration environment (windows)

1) Install VMware virtual machine and Ubuntu system;

2) Download the NDK SDK and upload it to Ubuntu;

3) Configure NDK SDK environment variables;

4) Install git, yasm and make;

2. Formal compilation - refer to the steps of compiling Android in ijkplayer Build Android

3. Use the compiled so Library in the project

1. Configuration environment

1) Install VMware virtual machine and Ubuntu system

Cabbage windows 10 system installation Linux (ubuntu) virtual machine super detailed tutorial

2) Download the NDK SDK and upload it to Ubuntu

Download the Android SDK and NDK for Linux. Android SDK is selected here_ r24. 4.1-linux. Tgz and android-ndk-r10e-linux-x86_64.zip. After downloading, you can use the following command to extract the file:

tar -xvf android-sdk_r24.4.1-linux.tar

unzip android-ndk-r10e-linux-x86_64.zip

Remember not to put the NDK directory under the shared directory of the virtual machine. To ensure smooth compilation, put the NDK directory in the system directory of Ubuntu, that is, the directory under the / home / user name.

3) Configure the NDK SDK environment variables

Under Ubuntu, / home / username /, press Ctrl+h to view bashrc file and configure SDK and NDK environment variables. Refer to the following:

NDK=/home/chinstyle/android/android-ndk-r10e
export NDK
ADB=/home/chinstyle/android/android-sdk-linux/platform-tools
export ADB
# ANDROID_NDK and ANDROID_SDK path
ANDROID_NDK=/home/chinstyle/android/android-ndk-r10e
export ANDROID_NDK
ANDROID_SDK=/home/chinstyle/android/android-sdk-linux
export ANDROID_SDK 
# Add to PATH path
PATH=${PATH}:${NDK}:${ADB}:${ANDROID_NDK}:${ANDROID_SDK}

Save and close after configuration bashrc, open Terminal and enter ndk build - V to check whether the ndk configuration is successful. The operation log is as follows:

Ctrl + Alt + T - open terminal.

If you are prompted that the ndk -build permission is insufficient, you need to give permission. It is recommended to give permission to the entire ndk folder.

chmod -R 777 folder
Parameter - R means recursion
777 means that all permissions are open

4) Install git, yasm, and make

sudo apt-get update
sudo apt install git
sudo apt install yasm
sudo apt install make

Use git --version and make -v to check whether GIT and make tools are successfully installed. If successful, the corresponding version number will be displayed. Refer to the following:

chinstyle@chinstyle-virtual-machine:~$ git --version
git version 2.25.1
chinstyle@chinstyle-virtual-machine:~$ make -v
GNU Make 4.2.1
 by x86_64-pc-linux-gnu compile
Copyright (C) 1988-2016 Free Software Foundation, Inc.
licence: GPLv3+: GNU General public license version 3 or later<http://gnu.org/licenses/gpl.html>. 
This software is free software: you are free to modify and redistribute it.
There is no other guarantee to the extent permitted by law.

2. Formal compilation

Refer to the steps of compiling Android in ijkplayer Build Android

//clone ijkplayer source code
git clone https://github.com/Bilibili/ijkplayer.git ijkplayer
cd ijkplayer
git checkout -B latest k0.8.8
//Use a lighter module Lite sh
cd ijkplayer/config
rm module.sh
ln -s module-lite module.sh
//Download ffmpeg source code - time consuming
cd ijkplayer
./init-android.sh
//Compile arm64 ffmpeg
./compile-ffmpeg.sh clean
./compile-ffmpeg.sh arm64 / / if all is used, the so of all schemas will be compiled
//Compile ijkplayer to generate arm64 so file
cd ijkplayer/android
./compile-ijk.sh arm64 / / if all is used, the so of all schemas will be compiled

Some precautions:

1)git checkout -B latest k0.8.8 - switch to this branch, or the subsequent C + + methods will be missing;

2)ln -s module-lite module.sh - ijkplayer provides three versions of compiled script configuration

module-default.sh: by default, you can use this if you like more types;
module-lite-hevc.sh: if you prefer a smaller binary CODEC / format (including hevc function)
module-lite.sh: if you prefer a smaller binary size CODEC / format (by default)

Difference: Lite SH is equivalent to default On the basis of SH, all operations such as all decoders are turned off, and then the corresponding decoders are turned on as required. You can open the compiled script to view. You can open module SH modify it yourself.

3)compile-ffmpeg. sh arm64 & compile-ijk.sh arm64: only the arm64 bit so library is compiled here, so the compilation time is shorter. If you need to compile so of all architectures, replace arm64 with all.

The compilation time is long and needs to wait patiently.

4) Compilation result - generate the corresponding so Library

The compiled ijkplayer project as a whole is as follows:

3. Use the compiled so Library in the project

1) Add ijkplayer dependency

    //ijkplayer player
    implementation 'tv.danmaku.ijk.media:ijkplayer-java:0.8.8'
    //ijkplayer so file
    implementation 'tv.danmaku.ijk.media:ijkplayer-arm64:0.8.8'

This is a dependency provided by ijkplayer and can be used directly.

Here we choose to use the so we just compiled. Copy "ijkplayer Java" and "ijkplayer arm64" to our project and use them directly in a project dependent manner.

setting.gradle
include ':app', 'ijkplayer-arm64','ijkplayer-java'


build.gradle
    api project(path: ':ijkplayer-arm64')
    api project(path: ':ijkplayer-java')

2) Play music files under raw/assets

    // Play music under raw
    public void onIjkPlayRaw(View view) {
        //Instantiate playback kernel
        tv.danmaku.ijk.media.player.IjkMediaPlayer ijkPlayer = new tv.danmaku.ijk.media.player.IjkMediaPlayer();
        //Get playback source access portal
        AssetFileDescriptor afd = getResources().openRawResourceFd(R.raw.ynew); // Notice the difference here
        //Build IMediaDataSource recognized by IjkPlayer. The following RawDataSourceProvider implements IMediaDataSource interface
        RawDataSourceProvider sourceProvider = new RawDataSourceProvider(afd);
        //Set playback source for IjkPlayer
        ijkPlayer.setDataSource(sourceProvider);
        //Set readiness listening
        ijkPlayer .setOnPreparedListener((IMediaPlayer.OnPreparedListener) mp -> {
            // Start playing
            ijkPlayer.start();
        });
        //Ready to play
        ijkPlayer.prepareAsync();
    }


    // Play music under assets
    public void onIjkPlayAsset(View view) {
        tv.danmaku.ijk.media.player.IjkMediaPlayer ijkPlayer = new tv.danmaku.ijk.media.player.IjkMediaPlayer();
        AssetManager am = getAssets();
        try {
            AssetFileDescriptor afd = am.openFd("intput.aac");
            RawDataSourceProvider sourceProvider = new RawDataSourceProvider(afd);
            ijkPlayer.setDataSource(sourceProvider);
        } catch (IOException e) {
            e.printStackTrace();
        }
        ijkPlayer .setOnPreparedListener((IMediaPlayer.OnPreparedListener) mp -> {
            ijkPlayer.start();
        });
        ijkPlayer.prepareAsync();
    }
// ijkplayer is the entrance to play local files
public class RawDataSourceProvider implements IMediaDataSource {
    private AssetFileDescriptor mDescriptor;

    private byte[]  mMediaBytes;

    public RawDataSourceProvider(AssetFileDescriptor descriptor) {
        this.mDescriptor = descriptor;
    }

    @Override
    public int readAt(long position, byte[] buffer, int offset, int size) throws IOException {
        if(position + 1 >= mMediaBytes.length){
            return -1;
        }

        int length;
        if(position + size < mMediaBytes.length){
            length = size;
        }else{
            length = (int) (mMediaBytes.length - position);
            if(length > buffer.length)
                length = buffer.length ;

            length--;
        }
        System.arraycopy(mMediaBytes, (int) position, buffer, offset, length);

        return length;
    }

    @Override
    public long getSize() throws IOException {
        long length  = mDescriptor.getLength();
        if(mMediaBytes == null){
            InputStream inputStream = mDescriptor.createInputStream();
            mMediaBytes = readBytes(inputStream);
        }


        return length;
    }

    @Override
    public void close() throws IOException {
        if(mDescriptor != null)
            mDescriptor.close();

        mDescriptor = null;
        mMediaBytes = null;
    }

    private byte[] readBytes(InputStream inputStream) throws IOException {
        ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();

        int bufferSize = 1024;
        byte[] buffer = new byte[bufferSize];

        int len = 0;
        while ((len = inputStream.read(buffer)) != -1) {
            byteBuffer.write(buffer, 0, len);
        }

        return byteBuffer.toByteArray();
    }

    public static RawDataSourceProvider create(Context context, Uri uri){
        try {
            AssetFileDescriptor fileDescriptor = context.getContentResolver().openAssetFileDescriptor(uri, "r");
            return new RawDataSourceProvider(fileDescriptor);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }
}

So far, we have compiled ijkplayer.

Reference articles

<How to compile ijkplayer correctly>

<It's not that difficult for ijkplayer to compile so library>

<Take you step by step to compile Bili Bili ijkPlayer>

<Summary of methods for playing Raw/Assets audio and video>

Keywords: Ubuntu ffmpeg ijkPlayer

Added by wwwapu on Tue, 25 Jan 2022 23:05:22 +0200