use of android.media.MediaFormat in project grafika by google.
the class GeneratedMovie method prepareEncoder.
/**
* Prepares the video encoder, muxer, and an EGL input surface.
*/
protected void prepareEncoder(String mimeType, int width, int height, int bitRate, int framesPerSecond, File outputFile) throws IOException {
mBufferInfo = new MediaCodec.BufferInfo();
MediaFormat format = MediaFormat.createVideoFormat(mimeType, width, height);
// Set some properties. Failing to specify some of these can cause the MediaCodec
// configure() call to throw an unhelpful exception.
format.setInteger(MediaFormat.KEY_COLOR_FORMAT, MediaCodecInfo.CodecCapabilities.COLOR_FormatSurface);
format.setInteger(MediaFormat.KEY_BIT_RATE, bitRate);
format.setInteger(MediaFormat.KEY_FRAME_RATE, framesPerSecond);
format.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, IFRAME_INTERVAL);
if (VERBOSE)
Log.d(TAG, "format: " + format);
// Create a MediaCodec encoder, and configure it with our format. Get a Surface
// we can use for input and wrap it with a class that handles the EGL work.
mEncoder = MediaCodec.createEncoderByType(mimeType);
mEncoder.configure(format, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
Log.v(TAG, "encoder is " + mEncoder.getCodecInfo().getName());
Surface surface;
try {
surface = mEncoder.createInputSurface();
} catch (IllegalStateException ise) {
// TODO: failure message should come out of strings.xml for i18n
if (isSoftwareCodec(mEncoder)) {
throw new RuntimeException("Can't use input surface with software codec: " + mEncoder.getCodecInfo().getName(), ise);
} else {
throw new RuntimeException("Failed to create input surface", ise);
}
}
mEglCore = new EglCore(null, EglCore.FLAG_RECORDABLE);
mInputSurface = new WindowSurface(mEglCore, surface, true);
mInputSurface.makeCurrent();
mEncoder.start();
// the raw H.264 elementary stream we get from MediaCodec into a .mp4 file.
if (VERBOSE)
Log.d(TAG, "output will go to " + outputFile);
mMuxer = new MediaMuxer(outputFile.toString(), MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4);
mTrackIndex = -1;
mMuxerStarted = false;
}
use of android.media.MediaFormat in project grafika by google.
the class CodecOpenActivity method clickStart.
/**
* onClick handler for "start" button.
*
* We create as many codecs as we can and return without releasing them. The codecs
* will remain in use until the next GC.
*/
public void clickStart(@SuppressWarnings("unused") View unused) {
final String MIME_TYPE = "video/avc";
final int WIDTH = 320;
final int HEIGHT = 240;
final int BIT_RATE = 1000000;
final int FRAME_RATE = 15;
final int IFRAME_INTERVAL = 1;
final boolean START_CODEC = true;
MediaFormat format = MediaFormat.createVideoFormat(MIME_TYPE, WIDTH, HEIGHT);
format.setInteger(MediaFormat.KEY_COLOR_FORMAT, MediaCodecInfo.CodecCapabilities.COLOR_FormatSurface);
format.setInteger(MediaFormat.KEY_BIT_RATE, BIT_RATE);
format.setInteger(MediaFormat.KEY_FRAME_RATE, FRAME_RATE);
format.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, IFRAME_INTERVAL);
Log.d(TAG, "format: " + format);
MediaCodec[] codecs = new MediaCodec[MAX_OPEN];
int i;
for (i = 0; i < MAX_OPEN; i++) {
try {
codecs[i] = MediaCodec.createEncoderByType(MIME_TYPE);
codecs[i].configure(format, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
if (START_CODEC) {
codecs[i].createInputSurface();
codecs[i].start();
}
} catch (Exception ex) {
Log.i(TAG, "Failed on creation of codec #" + i, ex);
break;
}
}
showCountDialog(i);
}
use of android.media.MediaFormat in project grafika by google.
the class ScreenRecordActivity method onCreate.
@TargetApi(Build.VERSION_CODES.M)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
new AlertDialog.Builder(this).setTitle("Error").setMessage("This activity only works on Marshmallow or later.").setNeutralButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ScreenRecordActivity.this.finish();
}
}).show();
return;
}
Button toggleRecording = findViewById(R.id.screen_record_button);
toggleRecording.setOnClickListener(new View.OnClickListener() {
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
public void onClick(View v) {
if (v.getId() == R.id.screen_record_button) {
if (muxerStarted) {
stopRecording();
((Button) findViewById(R.id.screen_record_button)).setText(R.string.toggleRecordingOn);
} else {
Intent permissionIntent = mediaProjectionManager.createScreenCaptureIntent();
startActivityForResult(permissionIntent, REQUEST_CODE_CAPTURE_PERM);
findViewById(R.id.screen_record_button).setEnabled(false);
}
}
}
});
mediaProjectionManager = (MediaProjectionManager) getSystemService(android.content.Context.MEDIA_PROJECTION_SERVICE);
encoderCallback = new MediaCodec.Callback() {
@Override
public void onInputBufferAvailable(@NonNull MediaCodec codec, int index) {
Log.d(TAG, "Input Buffer Avail");
}
@Override
public void onOutputBufferAvailable(@NonNull MediaCodec codec, int index, @NonNull MediaCodec.BufferInfo info) {
ByteBuffer encodedData = videoEncoder.getOutputBuffer(index);
if (encodedData == null) {
throw new RuntimeException("couldn't fetch buffer at index " + index);
}
if ((info.flags & MediaCodec.BUFFER_FLAG_CODEC_CONFIG) != 0) {
info.size = 0;
}
if (info.size != 0) {
if (muxerStarted) {
encodedData.position(info.offset);
encodedData.limit(info.offset + info.size);
muxer.writeSampleData(trackIndex, encodedData, info);
}
}
videoEncoder.releaseOutputBuffer(index, false);
}
@Override
public void onError(@NonNull MediaCodec codec, @NonNull MediaCodec.CodecException e) {
Log.e(TAG, "MediaCodec " + codec.getName() + " onError:", e);
}
@Override
public void onOutputFormatChanged(@NonNull MediaCodec codec, @NonNull MediaFormat format) {
Log.d(TAG, "Output Format changed");
if (trackIndex >= 0) {
throw new RuntimeException("format changed twice");
}
trackIndex = muxer.addTrack(videoEncoder.getOutputFormat());
if (!muxerStarted && trackIndex >= 0) {
muxer.start();
muxerStarted = true;
}
}
};
}
use of android.media.MediaFormat in project grafika by google.
the class ScreenRecordActivity method prepareVideoEncoder.
@RequiresApi(api = Build.VERSION_CODES.M)
private void prepareVideoEncoder(int width, int height) {
MediaFormat format = MediaFormat.createVideoFormat(VIDEO_MIME_TYPE, width, height);
// 30 fps
int frameRate = 30;
// Set some required properties. The media codec may fail if these aren't defined.
format.setInteger(MediaFormat.KEY_COLOR_FORMAT, MediaCodecInfo.CodecCapabilities.COLOR_FormatSurface);
// 6Mbps
format.setInteger(MediaFormat.KEY_BIT_RATE, 6000000);
format.setInteger(MediaFormat.KEY_FRAME_RATE, frameRate);
format.setInteger(MediaFormat.KEY_CAPTURE_RATE, frameRate);
format.setInteger(MediaFormat.KEY_REPEAT_PREVIOUS_FRAME_AFTER, 1000000 / frameRate);
format.setInteger(MediaFormat.KEY_CHANNEL_COUNT, 1);
// 1 seconds between I-frames
format.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, 1);
// Create a MediaCodec encoder and configure it. Get a Surface we can use for recording into.
try {
videoEncoder = MediaCodec.createEncoderByType(VIDEO_MIME_TYPE);
videoEncoder.configure(format, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
inputSurface = videoEncoder.createInputSurface();
videoEncoder.setCallback(encoderCallback);
videoEncoder.start();
} catch (IOException e) {
releaseEncoders();
}
}
use of android.media.MediaFormat in project grafika by google.
the class SoftInputSurfaceActivity method prepareEncoder.
/**
* Prepares the video encoder, muxer, and an input surface.
*/
private void prepareEncoder(File outputFile) throws IOException {
mBufferInfo = new MediaCodec.BufferInfo();
MediaFormat format = MediaFormat.createVideoFormat(MIME_TYPE, WIDTH, HEIGHT);
// Set some properties. Failing to specify some of these can cause the MediaCodec
// configure() call to throw an unhelpful exception.
format.setInteger(MediaFormat.KEY_COLOR_FORMAT, MediaCodecInfo.CodecCapabilities.COLOR_FormatSurface);
format.setInteger(MediaFormat.KEY_BIT_RATE, BIT_RATE);
format.setInteger(MediaFormat.KEY_FRAME_RATE, FRAMES_PER_SECOND);
format.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, IFRAME_INTERVAL);
if (VERBOSE)
Log.d(TAG, "format: " + format);
// Create a MediaCodec encoder, and configure it with our format. Get a Surface
// we can use for input and wrap it with a class that handles the EGL work.
mEncoder = MediaCodec.createEncoderByType(MIME_TYPE);
mEncoder.configure(format, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
mInputSurface = mEncoder.createInputSurface();
mEncoder.start();
// the raw H.264 elementary stream we get from MediaCodec into a .mp4 file.
if (VERBOSE)
Log.d(TAG, "output will go to " + outputFile);
mMuxer = new MediaMuxer(outputFile.toString(), MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4);
mTrackIndex = -1;
mMuxerStarted = false;
}
Aggregations