Android720pFormatStrategy.java

 1package eu.siacs.conversations.utils;
 2
 3import android.media.MediaCodecInfo;
 4import android.media.MediaFormat;
 5import android.os.Build;
 6import android.support.annotation.RequiresApi;
 7import android.util.Log;
 8
 9import net.ypresto.androidtranscoder.format.MediaFormatExtraConstants;
10import net.ypresto.androidtranscoder.format.MediaFormatStrategy;
11import net.ypresto.androidtranscoder.format.OutputFormatUnavailableException;
12
13import eu.siacs.conversations.Config;
14
15public class Android720pFormatStrategy implements MediaFormatStrategy {
16
17    private static final int LONGER_LENGTH = 1280;
18    private static final int SHORTER_LENGTH = 720;
19    private static final int DEFAULT_VIDEO_BITRATE = 2000 * 1000;
20    private static final int DEFAULT_AUDIO_BITRATE = 192 * 1000;
21    private final int mVideoBitrate;
22    private final int mAudioBitrate;
23    private final int mAudioChannels;
24
25    public Android720pFormatStrategy() {
26        mVideoBitrate = DEFAULT_VIDEO_BITRATE;
27        mAudioBitrate = DEFAULT_AUDIO_BITRATE;
28        mAudioChannels = 2;
29    }
30
31    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
32    @Override
33    public MediaFormat createVideoOutputFormat(MediaFormat inputFormat) {
34        int width = inputFormat.getInteger(MediaFormat.KEY_WIDTH);
35        int height = inputFormat.getInteger(MediaFormat.KEY_HEIGHT);
36        int longer, shorter, outWidth, outHeight;
37        if (width >= height) {
38            longer = width;
39            shorter = height;
40            outWidth = LONGER_LENGTH;
41            outHeight = SHORTER_LENGTH;
42        } else {
43            shorter = width;
44            longer = height;
45            outWidth = SHORTER_LENGTH;
46            outHeight = LONGER_LENGTH;
47        }
48        if (longer * 9 != shorter * 16) {
49            throw new OutputFormatUnavailableException("This video is not 16:9, and is not able to transcode. (" + width + "x" + height + ")");
50        }
51        if (shorter <= SHORTER_LENGTH) {
52            Log.d(Config.LOGTAG, "This video is less or equal to 720p, pass-through. (" + width + "x" + height + ")");
53            return null;
54        }
55        MediaFormat format = MediaFormat.createVideoFormat("video/avc", outWidth, outHeight);
56        format.setInteger(MediaFormat.KEY_BIT_RATE, mVideoBitrate);
57        format.setInteger(MediaFormat.KEY_FRAME_RATE, 30);
58        format.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, 3);
59        format.setInteger(MediaFormat.KEY_COLOR_FORMAT, MediaCodecInfo.CodecCapabilities.COLOR_FormatSurface);
60        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
61            format.setInteger(MediaFormat.KEY_PROFILE ,MediaCodecInfo.CodecProfileLevel.AVCProfileBaseline);
62            format.setInteger(MediaFormat.KEY_LEVEL, MediaCodecInfo.CodecProfileLevel.AVCLevel13);
63        }
64        return format;
65    }
66
67    @Override
68    public MediaFormat createAudioOutputFormat(MediaFormat inputFormat) {
69        final MediaFormat format = MediaFormat.createAudioFormat(MediaFormatExtraConstants.MIMETYPE_AUDIO_AAC, inputFormat.getInteger(MediaFormat.KEY_SAMPLE_RATE), mAudioChannels);
70        format.setInteger(MediaFormat.KEY_AAC_PROFILE, MediaCodecInfo.CodecProfileLevel.AACObjectLC);
71        format.setInteger(MediaFormat.KEY_BIT_RATE, mAudioBitrate);
72        return format;
73    }
74
75}