bravobit / FFmpeg-Android

FFMpeg/FFprobe compiled for Android
https://bravobit.nl/
MIT License
741 stars 176 forks source link

Can support multi thread? #9

Closed Blankeer closed 6 years ago

Blankeer commented 6 years ago

I see the code,only allowed to run single command at a time. so,How to use multi command when multi thread? thanks.

adriandleon commented 6 years ago

I am also interested on multi commands.

function1983 commented 6 years ago

This works for me. It takes 3 steps:

  1. Create FFmpegList.java:
    private static List<FFmpeg> instanceList;
    private final static int INSTANCE_COUNT = 2;
    public static List<FFmpeg> getInstanceList(final Context context) {
        if (instanceList == null) {
            //instanceList = new ArrayList<Pair<FFmpeg,FFcommandExecuteAsyncTask>>();
            instanceList = new ArrayList<>();
            for (int i = 0; i<INSTANCE_COUNT; i ++) {
                FFmpeg mFFmpeg = new FFmpeg(new FFbinaryContextProvider() {
                    @Override
                    public Context provide() {
                        return context;
                    }
                });
                instanceList.add(mFFmpeg);
            }
        }
        return instanceList;
    }
  1. In FFmpeg.java you have to change:

ffmpegExecuteAsyncTask.execute()

to

ffmpegExecuteAsyncTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR)

This will make sure the asynctasks run in parallel (which I think what you want)

  1. Create MyService.java:
    FFmpeg ffmpeg = FFmpegList.getInstanceList(getApplicationContext()).get(0);
    try {
    ffmpeg.execute(cmd, new ExecuteBinaryResponseHandler() {}...
    }
    FFmpeg ffmpeg1 = FFmpegList.getInstanceList(getApplicationContext()).get(1);
    try {
    ffmpeg1.execute(cmd1, new ExecuteBinaryResponseHandler() {}...
    }

    Just to give you an idea.

Brianvdb commented 6 years ago

@function1983 Thanks for that code.

Multithreading has now been added for FFmpeg & FFprobe commands.

dependencies {
    implementation 'nl.bravobit:android-ffmpeg:1.1.2'
}