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