This repository has been archived by the owner on Oct 7, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 796
/
SafTabFragment.java
420 lines (343 loc) · 14.6 KB
/
SafTabFragment.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
/*
* Copyright (c) 2018 Taner Sener
*
* This file is part of MobileFFmpeg.
*
* MobileFFmpeg is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* MobileFFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with MobileFFmpeg. If not, see <http://www.gnu.org/licenses/>.
*/
package com.arthenica.mobileffmpeg.test;
import android.app.AlertDialog;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.util.AndroidRuntimeException;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.MediaController;
import android.widget.TextView;
import android.widget.VideoView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import com.arthenica.mobileffmpeg.Config;
import com.arthenica.mobileffmpeg.ExecuteCallback;
import com.arthenica.mobileffmpeg.FFmpeg;
import com.arthenica.mobileffmpeg.FFprobe;
import com.arthenica.mobileffmpeg.LogCallback;
import com.arthenica.mobileffmpeg.LogMessage;
import com.arthenica.mobileffmpeg.Statistics;
import com.arthenica.mobileffmpeg.StatisticsCallback;
import com.arthenica.mobileffmpeg.util.DialogUtil;
import com.arthenica.mobileffmpeg.util.ResourcesUtil;
import com.arthenica.smartexception.java.Exceptions;
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.concurrent.Callable;
import static android.app.Activity.RESULT_OK;
import static com.arthenica.mobileffmpeg.Config.RETURN_CODE_SUCCESS;
import static com.arthenica.mobileffmpeg.test.MainActivity.TAG;
public class SafTabFragment extends Fragment {
private TextView outputText;
private Uri inUri;
private Uri outUri;
private static final int REQUEST_SAF_FFPROBE = 11;
private static final int REQUEST_SAF_FFMPEG = 12;
private boolean backFromIntent = false;
private VideoView videoView;
private AlertDialog progressDialog;
private Statistics statistics;
private Button runFFmpegButton;
private Button runFFprobeButton;
public SafTabFragment() {
super(R.layout.fragment_saf_tab);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
runFFmpegButton = view.findViewById(R.id.runFFmpegButton);
runFFmpegButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT)
.setType("video/*")
.putExtra(Intent.EXTRA_TITLE, "video.mp4")
.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(intent, REQUEST_SAF_FFMPEG);
}
});
runFFprobeButton = view.findViewById(R.id.runFFprobeButton);
runFFprobeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT)
.setType("*/*")
.putExtra(Intent.EXTRA_MIME_TYPES, new String[]{"image/*", "video/*", "audio/*"})
.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(intent, REQUEST_SAF_FFPROBE);
}
});
outputText = view.findViewById(R.id.outputText);
outputText.setMovementMethod(new ScrollingMovementMethod());
videoView = view.findViewById(R.id.videoPlayerFrame);
progressDialog = DialogUtil.createProgressDialog(requireContext(), "Encoding video");
}
@Override
public void onResume() {
super.onResume();
setActive();
}
static SafTabFragment newInstance() {
return new SafTabFragment();
}
private void enableLogCallback() {
Config.enableLogCallback(new LogCallback() {
@Override
public void apply(final LogMessage message) {
MainActivity.addUIAction(new Callable() {
@Override
public Object call() {
appendLog(message.getText());
return null;
}
});
}
});
}
private void runFFprobe() {
videoView.setVisibility(View.GONE);
outputText.setVisibility(View.VISIBLE);
clearLog();
final String ffprobeCommand = "-hide_banner -print_format json -show_format -show_streams " + Config.getSafParameterForRead(getContext(), inUri);
Log.d(TAG, "Testing FFprobe COMMAND synchronously.");
Log.d(TAG, String.format("FFprobe process started with arguments\n\'%s\'", ffprobeCommand));
int result = FFprobe.execute(ffprobeCommand);
Log.d(TAG, String.format("FFprobe process exited with rc %d", result));
if (result != 0) {
Popup.show(requireContext(), "Command failed. Please check output for the details.");
}
inUri = null;
}
private void setActive() {
if (backFromIntent) {
backFromIntent = false;
return;
}
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
Popup.show(requireContext(), "SAF is only available for Android 4.4 and above.");
runFFprobeButton.setEnabled(false);
runFFmpegButton.setEnabled(false);
outputText.setEnabled(false);
Log.i(TAG, "SAF Tab Dectivated");
return;
}
Log.i(TAG, "SAF Tab Activated");
enableLogCallback();
enableStatisticsCallback();
Popup.show(requireContext(), getString(R.string.saf_test_tooltip_text));
}
private void appendLog(final String logMessage) {
outputText.append(logMessage);
}
private void clearLog() {
outputText.setText("");
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
backFromIntent = true;
if (requestCode == REQUEST_SAF_FFPROBE && resultCode == RESULT_OK && data != null) {
inUri = data.getData();
MainActivity.handler.post(new Runnable() {
@Override
public void run() {
runFFprobe();
}
});
} else if (requestCode == REQUEST_SAF_FFMPEG && resultCode == MainActivity.RESULT_OK && data != null) {
outUri = data.getData();
MainActivity.handler.post(new Runnable() {
@Override
public void run() {
encodeVideo();
}
});
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}
private String getCodec(String videoPath) {
String extension = "mp4";
int pos = videoPath.lastIndexOf('.');
if (pos >= 0)
extension = videoPath.substring(pos+1);
switch (extension) {
case "webm":
return "vp8";
case "mkv":
return "aom";
case "ogv":
return "theora";
case "mov":
return "hap";
case "mp4":
default:
return "mpeg4";
}
}
private String getCustomOptions(String videoCodec) {
switch (videoCodec) {
case "x265":
return "-crf 28 -preset fast ";
case "vp8":
return "-b:v 1M -crf 10 ";
case "vp9":
return "-b:v 2M ";
case "aom":
return "-crf 30 -strict experimental ";
case "theora":
return "-qscale:v 7 ";
case "hap":
return "-format hap_q ";
default:
// kvazaar, mpeg4, x264, xvid
return "";
}
}
private void enableStatisticsCallback() {
Config.enableStatisticsCallback(new StatisticsCallback() {
@Override
public void apply(final Statistics newStatistics) {
MainActivity.addUIAction(new Callable<Object>() {
@Override
public Object call() {
statistics = newStatistics;
updateProgressDialog();
return null;
}
});
throw new AndroidRuntimeException("I am test exception thrown by test application");
}
});
}
private void encodeVideo() {
final File image1File = new File(requireContext().getCacheDir(), "colosseum.jpg");
final File image2File = new File(requireContext().getCacheDir(), "pyramid.jpg");
final File image3File = new File(requireContext().getCacheDir(), "tajmahal.jpg");
final String videoPath = Config.getSafParameterForWrite(requireContext(), outUri);
try {
// IF VIDEO IS PLAYING STOP PLAYBACK
videoView.stopPlayback();
videoView.setVisibility(View.GONE);
outputText.setVisibility(View.VISIBLE);
String selectedCodec = getCodec(videoPath);
Log.d(TAG, String.format("Testing VIDEO encoding with '%s' codec", selectedCodec));
showProgressDialog();
ResourcesUtil.resourceToFile(getResources(), R.drawable.colosseum, image1File);
ResourcesUtil.resourceToFile(getResources(), R.drawable.pyramid, image2File);
ResourcesUtil.resourceToFile(getResources(), R.drawable.tajmahal, image3File);
final String ffmpegCommand = Video.generateEncodeVideoScript(image1File.getAbsolutePath(), image2File.getAbsolutePath(), image3File.getAbsolutePath(), videoPath, selectedCodec, getCustomOptions(selectedCodec));
Log.d(TAG, String.format("FFmpeg process started with arguments\n'%s'.", ffmpegCommand));
long executionId = FFmpeg.executeAsync(ffmpegCommand, new ExecuteCallback() {
@Override
public void apply(final long executionId, final int returnCode) {
Log.d(TAG, String.format("FFmpeg process exited with rc %d.", returnCode));
Log.d(TAG, "FFmpeg process output:");
Config.printLastCommandOutput(Log.INFO);
hideProgressDialog();
MainActivity.addUIAction(new Callable<Object>() {
@Override
public Object call() {
if (returnCode == RETURN_CODE_SUCCESS) {
Log.d(TAG, "Encode completed successfully; playing video.");
playVideo(outUri, new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
videoView.setVisibility(View.GONE);
outputText.setVisibility(View.VISIBLE);
}
});
} else {
Popup.show(requireContext(), "Encode failed. Please check log for the details.");
Log.d(TAG, String.format("Encode failed with rc=%d.", returnCode));
}
return null;
}
});
}
});
Log.d(TAG, String.format("Async FFmpeg process started with executionId %d.", executionId));
} catch (IOException e) {
Log.e(TAG, String.format("Encode video failed %s.", Exceptions.getStackTraceString(e)));
Popup.show(requireContext(), "Encode video failed");
}
}
private void playVideo(Uri videoUri, MediaPlayer.OnCompletionListener onCompletionListener) {
videoView.setVisibility(View.VISIBLE);
outputText.setVisibility(View.GONE);
MediaController mediaController = new MediaController(requireContext());
mediaController.setAnchorView(videoView);
videoView.setVideoURI(videoUri);
videoView.setMediaController(mediaController);
videoView.requestFocus();
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
videoView.setBackgroundColor(0x00000000);
}
});
videoView.setOnErrorListener(new MediaPlayer.OnErrorListener() {
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
videoView.stopPlayback();
return false;
}
});
videoView.setOnCompletionListener(onCompletionListener);
videoView.start();
}
private void showProgressDialog() {
// CLEAN STATISTICS
statistics = null;
Config.resetStatistics();
progressDialog.show();
}
private void updateProgressDialog() {
if (statistics == null) {
return;
}
int timeInMilliseconds = this.statistics.getTime();
if (timeInMilliseconds > 0) {
int totalVideoDuration = 9000;
String completePercentage = new BigDecimal(timeInMilliseconds).multiply(new BigDecimal(100)).divide(new BigDecimal(totalVideoDuration), 0, BigDecimal.ROUND_HALF_UP).toString();
TextView textView = progressDialog.findViewById(R.id.progressDialogText);
if (textView != null) {
textView.setText(String.format("Encoding video: %% %s.", completePercentage));
}
}
}
private void hideProgressDialog() {
progressDialog.dismiss();
MainActivity.addUIAction(new Callable<Object>() {
@Override
public Object call() {
progressDialog = DialogUtil.createProgressDialog(requireContext(), "Encoding video");
return null;
}
});
}
}