4

I am trying to record screen using https://github.com/saki4510t/ScreenRecordingSample/tree/master/app/src/main/java/com/serenegiant/media and provided few related code snippet:

@Override
void prepare() throws IOException {
    if (DEBUG) Log.i(TAG, "prepare: ");
    mSurface = prepare_surface_encoder(MIME_TYPE, fps, bitrate);
    mMediaCodec.start();
    mIsRecording = true;
    new Thread(mScreenCaptureTask, "ScreenCaptureThread").start();
    if (DEBUG) Log.i(TAG, "prepare finishing");
    if (mListener != null) {
        try {
            mListener.onPrepared(this);
        } catch (final Exception e) {
            Log.e(TAG, "prepare:", e);
        }
    }
}   
protected MediaFormat create_encoder_format(final String mime, final int frame_rate, final int bitrate) {
    if (DEBUG) Log.v(TAG, String.format("create_encoder_format:(%d,%d),mime=%s,frame_rate=%d,bitrate=%d", mWidth, mHeight, mime, frame_rate, bitrate));
    final MediaFormat format = MediaFormat.createVideoFormat(mime, mWidth, mHeight);
    format.setInteger(MediaFormat.KEY_COLOR_FORMAT, MediaCodecInfo.CodecCapabilities.COLOR_FormatSurface);  // API >= 18
    format.setInteger(MediaFormat.KEY_BIT_RATE, bitrate > 0 ? bitrate : calcBitRate(frame_rate)); //800000
    format.setInteger(MediaFormat.KEY_FRAME_RATE,30);
    format.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, 10);
    return format;
}

protected Surface prepare_surface_encoder(final String mime, final int frame_rate, final int bitrate)
    throws IOException, IllegalArgumentException {

    if (DEBUG) Log.v(TAG, String.format("prepare_surface_encoder:(%d,%d),mime=%s,frame_rate=%d,bitrate=%d", mWidth, mHeight, mime, frame_rate, bitrate));

    mTrackIndex = -1;
    mMuxerStarted = mIsEOS = false;

    final MediaCodecInfo videoCodecInfo = selectVideoCodec(mime);
    if (videoCodecInfo == null) {
        throw new IllegalArgumentException("Unable to find an appropriate codec for " + mime);
    }
    if (DEBUG) Log.i(TAG, "selected codec: " + videoCodecInfo.getName());

    final MediaFormat format = create_encoder_format(mime, frame_rate, bitrate);
    if (DEBUG) Log.i(TAG, "format: " + format);

    mMediaCodec = MediaCodec.createEncoderByType(mime);
    mMediaCodec.configure(format, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
    // get Surface for encoder input
    // this method only can call between #configure and #start
    return mMediaCodec.createInputSurface();    // API >= 18
}

It works for most of the devices but not for Samsung Galaxy note 4 running android 6.0.1. Where i am getting following error.

`Non-fatal Exception: android.media.MediaCodec$CodecException: Error 0xffffec77
   at android.media.MediaCodec.native_configure(MediaCodec.java)
   at android.media.MediaCodec.configure(MediaCodec.java:1778)
   at com.urcaddy.utilities.utils.media.MediaVideoEncoderBase.prepare_surface_encoder(MediaVideoEncoderBase.java:90)
   at com.urcaddy.utilities.utils.media.MediaScreenEncoder.prepare(MediaScreenEncoder.java:87)
   at com.urcaddy.utilities.utils.media.MediaMuxerWrapper.prepare(MediaMuxerWrapper.java:74)
   at com.urcaddy.services.backgroundservices.ScreenRecorderService.startScreenRecord(ScreenRecorderService.java:229)
   at com.urcaddy.services.backgroundservices.ScreenRecorderService.onStartCommand(ScreenRecorderService.java:139)
   at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:4062)
   at android.app.ActivityThread.access$2400(ActivityThread.java:221)
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1897)
   at android.os.Handler.dispatchMessage(Handler.java:102)
   at android.os.Looper.loop(Looper.java:158)
   at android.app.ActivityThread.main(ActivityThread.java:7225)
   at java.lang.reflect.Method.invoke(Method.java)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)`
Ajit
  • 339
  • 5
  • 15
  • Please post LogCat output as well. You can obtain it using Samsung online test lab: http://developer.samsung.com/remotetestlab/rtlDeviceList.action?os=101 – user1643723 Jun 27 '17 at 02:18
  • There is a solution. You can try it: https://stackoverflow.com/a/52950367/5267209 – Özgür Oct 31 '18 at 08:04
  • There is a solution. Take a look: https://stackoverflow.com/a/52950367/5267209 – Özgür Nov 01 '18 at 08:39

1 Answers1

0

After a lots of brainstroming, we found that the issue was with the height and width.Below is the code which worked for us.

protected MediaFormat create_encoder_format(final String mime, final int frame_rate, final int bitrate) {
if (DEBUG) Log.v(TAG, String.format("create_encoder_format:(%d,%d),mime=%s,frame_rate=%d,bitrate=%d", 720, 1280, mime, frame_rate, bitrate));
final MediaFormat format = MediaFormat.createVideoFormat(mime, 720, 1280);
format.setInteger(MediaFormat.KEY_COLOR_FORMAT, MediaCodecInfo.CodecCapabilities.COLOR_FormatSurface);  // API >= 18
format.setInteger(MediaFormat.KEY_BIT_RATE, bitrate > 0 ? bitrate : calcBitRate(frame_rate)); //800000
format.setInteger(MediaFormat.KEY_FRAME_RATE,30);
format.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, 10);
return format;
}
Ajit
  • 339
  • 5
  • 15
  • 3
    I'm getting error with the exact same error code when trying to configure for avc encoding. 92% of affected devices are samsung devices: galaxy j7 and A series mainly. First I thought that the issue was 1080p width, I changed it to 640 but it didn't fix anythig. Any idea? – WPMed Jun 10 '18 at 19:18
  • 3
    Yes! We nicknamed this issue "blackscreen bug" at our company. The problem was we didn't properly release some exoplayers which caused our encoder wasn't available. Releasing exoplayers solved the issue, but if it's not the case for you also make sure you write a codec selector. You should query the available codecs on the device and select one that doesn't fail when you prepare it. There are multiple codecs on a device so you will probably find one. If nothing works we fall back to an alternative encoding code path involving ffmpeg. Hope that helps. – WPMed Sep 20 '19 at 17:30
  • @WPMed You're right about making sure to release `MediaCodec` before creating another. But, when other applications didn't handle the release of `MediaCodec`, it can also cause this issue. I asked a question about this, see the link below. I going to open an issue ticket on developers.google since there is no way for me to release another application's instance of `MediaCodec`. https://stackoverflow.com/q/58324341/5550161 – HB. Oct 12 '19 at 14:04