2018-09-28 16:38:54 +00:00
|
|
|
package eu.siacs.conversations.utils;
|
|
|
|
|
|
|
|
import android.media.MediaCodecInfo;
|
|
|
|
import android.media.MediaFormat;
|
|
|
|
import android.util.Log;
|
|
|
|
|
|
|
|
import net.ypresto.androidtranscoder.format.MediaFormatExtraConstants;
|
|
|
|
import net.ypresto.androidtranscoder.format.MediaFormatStrategy;
|
|
|
|
import net.ypresto.androidtranscoder.format.OutputFormatUnavailableException;
|
|
|
|
|
|
|
|
import eu.siacs.conversations.Config;
|
|
|
|
|
2018-10-06 14:15:32 +00:00
|
|
|
public class Android720pFormatStrategy implements MediaFormatStrategy {
|
2018-09-28 16:38:54 +00:00
|
|
|
|
2018-10-06 14:15:32 +00:00
|
|
|
private static final int LONGER_LENGTH = 1280;
|
|
|
|
private static final int SHORTER_LENGTH = 720;
|
2018-10-07 01:25:16 +00:00
|
|
|
private static final int DEFAULT_VIDEO_BITRATE = 2000 * 1000;
|
|
|
|
private static final int DEFAULT_AUDIO_BITRATE = 192 * 1000;
|
2018-09-28 16:38:54 +00:00
|
|
|
private final int mVideoBitrate;
|
|
|
|
private final int mAudioBitrate;
|
|
|
|
private final int mAudioChannels;
|
|
|
|
|
2018-10-06 14:15:32 +00:00
|
|
|
public Android720pFormatStrategy() {
|
2018-09-28 16:38:54 +00:00
|
|
|
mVideoBitrate = DEFAULT_VIDEO_BITRATE;
|
|
|
|
mAudioBitrate = DEFAULT_AUDIO_BITRATE;
|
|
|
|
mAudioChannels = 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public MediaFormat createVideoOutputFormat(MediaFormat inputFormat) {
|
|
|
|
int width = inputFormat.getInteger(MediaFormat.KEY_WIDTH);
|
|
|
|
int height = inputFormat.getInteger(MediaFormat.KEY_HEIGHT);
|
|
|
|
int longer, shorter, outWidth, outHeight;
|
|
|
|
if (width >= height) {
|
|
|
|
longer = width;
|
|
|
|
shorter = height;
|
|
|
|
outWidth = LONGER_LENGTH;
|
|
|
|
outHeight = SHORTER_LENGTH;
|
|
|
|
} else {
|
|
|
|
shorter = width;
|
|
|
|
longer = height;
|
|
|
|
outWidth = SHORTER_LENGTH;
|
|
|
|
outHeight = LONGER_LENGTH;
|
|
|
|
}
|
|
|
|
if (longer * 9 != shorter * 16) {
|
|
|
|
throw new OutputFormatUnavailableException("This video is not 16:9, and is not able to transcode. (" + width + "x" + height + ")");
|
|
|
|
}
|
|
|
|
if (shorter <= SHORTER_LENGTH) {
|
2018-10-06 14:15:32 +00:00
|
|
|
Log.d(Config.LOGTAG, "This video is less or equal to 720p, pass-through. (" + width + "x" + height + ")");
|
2018-09-28 16:38:54 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
MediaFormat format = MediaFormat.createVideoFormat("video/avc", outWidth, outHeight);
|
|
|
|
// From Nexus 4 Camera in 720p
|
|
|
|
format.setInteger(MediaFormat.KEY_BIT_RATE, mVideoBitrate);
|
|
|
|
format.setInteger(MediaFormat.KEY_FRAME_RATE, 30);
|
|
|
|
format.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, 3);
|
|
|
|
format.setInteger(MediaFormat.KEY_COLOR_FORMAT, MediaCodecInfo.CodecCapabilities.COLOR_FormatSurface);
|
|
|
|
return format;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public MediaFormat createAudioOutputFormat(MediaFormat inputFormat) {
|
|
|
|
final MediaFormat format = MediaFormat.createAudioFormat(MediaFormatExtraConstants.MIMETYPE_AUDIO_AAC, inputFormat.getInteger(MediaFormat.KEY_SAMPLE_RATE), mAudioChannels);
|
|
|
|
format.setInteger(MediaFormat.KEY_AAC_PROFILE, MediaCodecInfo.CodecProfileLevel.AACObjectLC);
|
|
|
|
format.setInteger(MediaFormat.KEY_BIT_RATE, mAudioBitrate);
|
|
|
|
return format;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|