Java Code Examples for android.media.MediaCodec#CodecException

The following examples show how to use android.media.MediaCodec#CodecException . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: UnexpectedErrorMapper.java    From no-player with Apache License 2.0 6 votes vote down vote up
static NoPlayer.PlayerError map(RuntimeException unexpectedException, String message) {
    if (unexpectedException instanceof EGLSurfaceTexture.GlException) {
        return new NoPlayerError(PlayerErrorType.UNEXPECTED, DetailErrorType.EGL_OPERATION_ERROR, message);
    }

    if (unexpectedException instanceof DefaultAudioSink.InvalidAudioTrackTimestampException) {
        return new NoPlayerError(PlayerErrorType.UNEXPECTED, DetailErrorType.SPURIOUS_AUDIO_TRACK_TIMESTAMP_ERROR, message);
    }

    if (unexpectedException instanceof IllegalStateException && message.contains("Multiple renderer media clocks")) {
        return new NoPlayerError(PlayerErrorType.UNEXPECTED, DetailErrorType.MULTIPLE_RENDERER_MEDIA_CLOCK_ENABLED_ERROR, message);
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && unexpectedException instanceof MediaCodec.CodecException) {
        String errorMessage = ErrorFormatter.formatCodecException((MediaCodec.CodecException) unexpectedException);
        return new NoPlayerError(PlayerErrorType.UNEXPECTED, DetailErrorType.UNEXPECTED_CODEC_ERROR, errorMessage);
    }

    return new NoPlayerError(PlayerErrorType.UNKNOWN, DetailErrorType.UNKNOWN, message);
}
 
Example 2
Source File: BaseEncoder.java    From SoloPi with Apache License 2.0 5 votes vote down vote up
/**
 * Must call in a worker handler thread!
 */
@Override
public void prepare() throws IOException {
    if (Looper.myLooper() == null
            || Looper.myLooper() == Looper.getMainLooper()) {
        throw new IllegalStateException("should run in a HandlerThread");
    }
    if (mEncoder != null) {
        throw new IllegalStateException("prepared!");
    }
    MediaFormat format = createMediaFormat();
    LogUtil.i("Encoder", "Create media format: " + format);

    String mimeType = format.getString(MediaFormat.KEY_MIME);
    final MediaCodec encoder = createEncoder(mimeType);
    try {
        if (this.mCallback != null) {
            // NOTE: MediaCodec maybe crash on some devices due to null callback
            encoder.setCallback( mCodecCallback);
        }
        encoder.configure(format, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
        onEncoderConfigured(encoder);
        encoder.start();
    } catch (MediaCodec.CodecException e) {
        LogUtil.e("Encoder", "Configure codec failure!\n  with format" + format, e);
        throw e;
    }
    mEncoder = encoder;
}
 
Example 3
Source File: MediaCodecVideoDecoder.java    From webrtc_android with MIT License 5 votes vote down vote up
@CalledByNativeUnchecked
private void returnDecodedOutputBuffer(int index)
    throws IllegalStateException, MediaCodec.CodecException {
  checkOnMediaCodecThread();
  if (useSurface()) {
    throw new IllegalStateException("returnDecodedOutputBuffer() called for surface decoding.");
  }
  mediaCodec.releaseOutputBuffer(index, false /* render */);
}
 
Example 4
Source File: TrackTranscoderException.java    From LiTr with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
@Nullable
private String getExceptionDiagnosticInfo(@Nullable Throwable cause) {
    if (!(cause instanceof MediaCodec.CodecException)) {
        return null;
    }

    return ((MediaCodec.CodecException) cause).getDiagnosticInfo();
}
 
Example 5
Source File: StreamingAudioEncoder.java    From live-transcribe-speech-engine with Apache License 2.0 5 votes vote down vote up
@Override
public byte[] flushAndStop() {
  ByteArrayOutputStream outputBytes = new ByteArrayOutputStream();
  try {
    processAudioBytesInternal(null, 0, 0, true, outputBytes);  // Flush!
    codec.stop();
  } catch (MediaCodec.CodecException e) {
    logger.atSevere().log("Something went wrong in the underlying codec!");
  }
  codec.release();
  return outputBytes.toByteArray();
}
 
Example 6
Source File: BaseEncoder.java    From ScreenCapture with MIT License 5 votes vote down vote up
/**
 * Must call in a worker handler thread!
 */
@Override
public void prepare() throws IOException {
    if (Looper.myLooper() == null
            || Looper.myLooper() == Looper.getMainLooper()) {
        throw new IllegalStateException("should run in a HandlerThread");
    }
    if (mEncoder != null) {
        throw new IllegalStateException("prepared!");
    }
    MediaFormat format = createMediaFormat();
    Log.d("Encoder", "Create media format: " + format);

    String mimeType = format.getString(MediaFormat.KEY_MIME);
    final MediaCodec encoder = createEncoder(mimeType);
    try {
        if (this.mCallback != null) {
            // NOTE: MediaCodec maybe crash on some devices due to null callback
            encoder.setCallback(mCodecCallback);
        }
        encoder.configure(format, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
        onEncoderConfigured(encoder);
        encoder.start();
    } catch (MediaCodec.CodecException e) {
        Log.e("Encoder", "Configure codec failure!\n  with format" + format, e);
        throw e;
    }
    mEncoder = encoder;
}
 
Example 7
Source File: HyperionScreenEncoder.java    From hyperion-android-grabber with MIT License 5 votes vote down vote up
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
HyperionScreenEncoder(final HyperionThread.HyperionThreadListener listener,
                       final MediaProjection projection, final int width, final int height,
                       final int density, HyperionGrabberOptions options) {
    super(listener, projection, width, height, density, options);

    try {
        prepare();
    } catch (MediaCodec.CodecException e) {
        e.printStackTrace();
    }
}
 
Example 8
Source File: HyperionScreenEncoder.java    From hyperion-android-grabber with MIT License 5 votes vote down vote up
@TargetApi(Build.VERSION_CODES.M)
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private void prepare() throws MediaCodec.CodecException {
    if (DEBUG) Log.d(TAG, "Preparing encoder");

    mVirtualDisplay = mMediaProjection.createVirtualDisplay(
            TAG,
            getGrabberWidth(), getGrabberHeight(), mDensity,
            DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR,
            null, mDisplayCallback, null);

    setImageReader();
}
 
Example 9
Source File: VideoEncoder.java    From rtmp-rtsp-stream-client-java with Apache License 2.0 5 votes vote down vote up
@RequiresApi(api = Build.VERSION_CODES.M)
private void createAsyncCallback() {
  callback = new MediaCodec.Callback() {
    @Override
    public void onInputBufferAvailable(@NonNull MediaCodec mediaCodec, int inBufferIndex) {
      try {
        inputAvailable(mediaCodec, inBufferIndex, null);
      } catch (IllegalStateException e) {
        Log.i(TAG, "Encoding error", e);
      }
    }

    @Override
    public void onOutputBufferAvailable(@NonNull MediaCodec mediaCodec, int outBufferIndex,
        @NonNull MediaCodec.BufferInfo bufferInfo) {
      try {
        outputAvailable(mediaCodec, outBufferIndex, bufferInfo);
      } catch (IllegalStateException e) {
        Log.i(TAG, "Encoding error", e);
      }
    }

    @Override
    public void onError(@NonNull MediaCodec mediaCodec, @NonNull MediaCodec.CodecException e) {
      Log.e(TAG, "Error", e);
    }

    @Override
    public void onOutputFormatChanged(@NonNull MediaCodec mediaCodec,
        @NonNull MediaFormat mediaFormat) {
      formatChanged(mediaCodec, mediaFormat);
    }
  };
}
 
Example 10
Source File: ErrorFormatter.java    From no-player with Apache License 2.0 5 votes vote down vote up
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
static String formatCodecException(MediaCodec.CodecException exception) {
    String diagnosticInformation = "diagnosticInformation=" + exception.getDiagnosticInfo();
    String isTransient = " : isTransient=" + exception.isTransient();
    String isRecoverable = " : isRecoverable=" + exception.isRecoverable();

    return diagnosticInformation + isTransient + isRecoverable;
}
 
Example 11
Source File: ErrorFormatterTest.java    From no-player with Apache License 2.0 5 votes vote down vote up
@Test
public void givenMediaCodecException_whenFormattingMessage_thenReturnsExpectedMessageFormat() {
    MediaCodec.CodecException codecException = mock(MediaCodec.CodecException.class);
    given(codecException.getDiagnosticInfo()).willReturn("android.media.MediaCodec.error_+1234");
    given(codecException.isTransient()).willReturn(true);
    given(codecException.isRecoverable()).willReturn(false);

    String expectedFormat = "diagnosticInformation=android.media.MediaCodec.error_+1234 : isTransient=true : isRecoverable=false";
    String actualFormat = ErrorFormatter.formatCodecException(codecException);

    assertThat(actualFormat).isEqualTo(expectedFormat);
}
 
Example 12
Source File: BaseAudioDecoder.java    From sdl_java_suite with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
protected void onMediaCodecError(@NonNull MediaCodec.CodecException e) {
    Log.e(TAG, "MediaCodec.onError: " + e.getLocalizedMessage());
    if (listener != null) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            listener.onDecoderError(e);
        } else {
            listener.onDecoderError(new Exception("Error decoding audio file"));
        }
    }
}
 
Example 13
Source File: BaseEncoder.java    From SoloPi with Apache License 2.0 4 votes vote down vote up
@Override
public void onError(MediaCodec codec, MediaCodec.CodecException e) {
    mCallback.onError(BaseEncoder.this, e);
}
 
Example 14
Source File: BaseEncoder.java    From ScreenCapture with MIT License 4 votes vote down vote up
@Override
public void onError(MediaCodec codec, MediaCodec.CodecException e) {
    mCallback.onError(BaseEncoder.this, e);
}
 
Example 15
Source File: VideoProcessor.java    From VideoProcessor with Apache License 2.0 4 votes vote down vote up
/**
 * 对视频先检查,如果不是全关键帧,先处理成所有帧都是关键帧,再逆序
 */
public static void reverseVideo(Context context, MediaSource input, String output, boolean reverseAudio, @Nullable VideoProgressListener listener) throws Exception {
    File tempFile = new File(context.getCacheDir(), System.currentTimeMillis() + ".temp");
    File temp2File = new File(context.getCacheDir(), System.currentTimeMillis() + ".temp2");
    try {
        MediaExtractor extractor = new MediaExtractor();
        input.setDataSource(extractor);
        int trackIndex = VideoUtil.selectTrack(extractor, false);
        extractor.selectTrack(trackIndex);
        int keyFrameCount = 0;
        int frameCount = 0;
        List<Long> frameTimeStamps = new ArrayList<>();
        while (true) {
            int flags = extractor.getSampleFlags();
            if (flags > 0 && (flags & MediaExtractor.SAMPLE_FLAG_SYNC) != 0) {
                keyFrameCount++;
            }
            long sampleTime = extractor.getSampleTime();
            if (sampleTime < 0) {
                break;
            }
            frameTimeStamps.add(sampleTime);
            frameCount++;
            extractor.advance();
        }
        extractor.release();

        if (frameCount == keyFrameCount || frameCount == keyFrameCount + 1) {
            reverseVideoNoDecode(input, output, reverseAudio, frameTimeStamps, listener);
        } else {
            VideoMultiStepProgress stepProgress = new VideoMultiStepProgress(new float[]{0.45f, 0.1f, 0.45f}, listener);
            stepProgress.setCurrentStep(0);
            float bitrateMultiple = (frameCount - keyFrameCount) / (float) keyFrameCount + 1;
            MediaMetadataRetriever retriever = new MediaMetadataRetriever();
            input.setDataSource(retriever);
            int oriBitrate = Integer.parseInt(retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_BITRATE));
            int duration = Integer.parseInt(retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION));
            try {
                processor(context)
                        .input(input)
                        .output(tempFile.getAbsolutePath())
                        .bitrate((int) (oriBitrate * bitrateMultiple))
                        .iFrameInterval(0)
                        .progressListener(stepProgress)
                        .process();
            } catch (MediaCodec.CodecException e) {
                CL.e(e);
                /** Nexus5上-1代表全关键帧*/
                processor(context)
                        .input(input)
                        .output(tempFile.getAbsolutePath())
                        .bitrate((int) (oriBitrate * bitrateMultiple))
                        .iFrameInterval(-1)
                        .progressListener(stepProgress)
                        .process();
            }
            stepProgress.setCurrentStep(1);
            reverseVideoNoDecode(new MediaSource(tempFile.getAbsolutePath()), temp2File.getAbsolutePath(), reverseAudio, null, stepProgress);
            int oriIFrameInterval = (int) (keyFrameCount / (duration / 1000f));
            oriIFrameInterval = oriIFrameInterval == 0 ? 1 : oriIFrameInterval;
            stepProgress.setCurrentStep(2);
            processor(context)
                    .input(temp2File.getAbsolutePath())
                    .output(output)
                    .bitrate(oriBitrate)
                    .iFrameInterval(oriIFrameInterval)
                    .progressListener(stepProgress)
                    .process();
        }
    } finally {
        tempFile.delete();
        temp2File.delete();
    }
}
 
Example 16
Source File: MediaEncoder.java    From DeviceConnect-Android with MIT License 4 votes vote down vote up
@Override
public void onError(@NonNull MediaCodec codec, @NonNull MediaCodec.CodecException e) {
    postOnError(new MediaEncoderException(e));
}