Skip to content

Commit

Permalink
Revert "[android] Handle mediacodec callback with a handler thread (#…
Browse files Browse the repository at this point in the history
…2044)" (#2458)

Reverts #2044

b/326091431

(cherry picked from commit c11007e)
  • Loading branch information
borongc authored and anonymous1-me committed Feb 22, 2024
1 parent d37e761 commit 8824d8f
Show file tree
Hide file tree
Showing 13 changed files with 101 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@
import android.media.MediaFormat;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.view.Surface;
import androidx.annotation.Nullable;
import dev.cobalt.util.Log;
Expand Down Expand Up @@ -95,9 +93,6 @@ class MediaCodecBridge {
// which would cause GC cycles long enough to impact playback.
private final MediaCodec.BufferInfo info = new MediaCodec.BufferInfo();

private Handler mHandler = null;
private HandlerThread mCallbackThread = null;

// Type of bitrate adjustment for video encoder.
public enum BitrateAdjustmentTypes {
// No adjustment - video encoder has no known bitrate problem.
Expand Down Expand Up @@ -456,8 +451,7 @@ private MediaCodecBridge(
MediaCodec mediaCodec,
String mime,
BitrateAdjustmentTypes bitrateAdjustmentType,
int tunnelModeAudioSessionId,
boolean useCallbackThread) {
int tunnelModeAudioSessionId) {
if (mediaCodec == null) {
throw new IllegalArgumentException();
}
Expand All @@ -467,11 +461,6 @@ private MediaCodecBridge(
mLastPresentationTimeUs = 0;
mFlushed = true;
mBitrateAdjustmentType = bitrateAdjustmentType;
if (useCallbackThread) {
mCallbackThread = new HandlerThread("MediaCodec:Callback:Handler");
mCallbackThread.start();
mHandler = new Handler(mCallbackThread.getLooper());
}
mCallback =
new MediaCodec.Callback() {
@Override
Expand Down Expand Up @@ -536,7 +525,7 @@ public void onOutputFormatChanged(MediaCodec codec, MediaFormat format) {
}
}
};
mMediaCodec.setCallback(mCallback, mHandler);
mMediaCodec.setCallback(mCallback);

if (isFrameRenderedCallbackEnabled() || tunnelModeAudioSessionId != -1) {
mFrameRendererListener =
Expand Down Expand Up @@ -572,7 +561,6 @@ public static MediaCodecBridge createAudioMediaCodecBridge(
int sampleRate,
int channelCount,
MediaCrypto crypto,
boolean useCallbackThread,
@Nullable byte[] configurationData) {
if (decoderName.equals("")) {
Log.e(TAG, "Invalid decoder name.");
Expand All @@ -591,12 +579,7 @@ public static MediaCodecBridge createAudioMediaCodecBridge(
}
MediaCodecBridge bridge =
new MediaCodecBridge(
nativeMediaCodecBridge,
mediaCodec,
mime,
BitrateAdjustmentTypes.NO_ADJUSTMENT,
-1,
useCallbackThread);
nativeMediaCodecBridge, mediaCodec, mime, BitrateAdjustmentTypes.NO_ADJUSTMENT, -1);

MediaFormat mediaFormat = createAudioFormat(mime, sampleRate, channelCount);

Expand Down Expand Up @@ -641,7 +624,6 @@ public static void createVideoMediaCodecBridge(
MediaCrypto crypto,
ColorInfo colorInfo,
int tunnelModeAudioSessionId,
boolean useCallbackThread,
int maxVideoInputSize,
CreateMediaCodecBridgeResult outCreateMediaCodecBridgeResult) {
MediaCodec mediaCodec = null;
Expand Down Expand Up @@ -696,8 +678,7 @@ public static void createVideoMediaCodecBridge(
mediaCodec,
mime,
BitrateAdjustmentTypes.NO_ADJUSTMENT,
tunnelModeAudioSessionId,
useCallbackThread);
tunnelModeAudioSessionId);
MediaFormat mediaFormat =
createVideoDecoderFormat(mime, widthHint, heightHint, videoCapabilities);

Expand Down Expand Up @@ -846,11 +827,6 @@ public void release() {
Log.e(TAG, "Cannot release media codec", e);
}
mMediaCodec = null;
if (mCallbackThread != null) {
mCallbackThread.quitSafely();
mCallbackThread = null;
mHandler = null;
}
}

@SuppressWarnings("unused")
Expand Down
9 changes: 3 additions & 6 deletions starboard/android/shared/audio_decoder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,12 @@ void* IncrementPointerByBytes(void* pointer, int offset) {
} // namespace

AudioDecoder::AudioDecoder(const AudioStreamInfo& audio_stream_info,
SbDrmSystem drm_system,
bool use_mediacodec_callback_thread)
SbDrmSystem drm_system)
: audio_stream_info_(audio_stream_info),
sample_type_(GetSupportedSampleType()),
output_sample_rate_(audio_stream_info.samples_per_second),
output_channel_count_(audio_stream_info.number_of_channels),
drm_system_(static_cast<DrmSystem*>(drm_system)),
use_mediacodec_callback_thread_(use_mediacodec_callback_thread) {
drm_system_(static_cast<DrmSystem*>(drm_system)) {
if (!InitializeCodec()) {
SB_LOG(ERROR) << "Failed to initialize audio decoder.";
}
Expand Down Expand Up @@ -188,8 +186,7 @@ void AudioDecoder::Reset() {

bool AudioDecoder::InitializeCodec() {
SB_DCHECK(!media_decoder_);
media_decoder_.reset(new MediaDecoder(this, audio_stream_info_, drm_system_,
use_mediacodec_callback_thread_));
media_decoder_.reset(new MediaDecoder(this, audio_stream_info_, drm_system_));
if (media_decoder_->is_valid()) {
if (error_cb_) {
media_decoder_->Initialize(
Expand Down
7 changes: 1 addition & 6 deletions starboard/android/shared/audio_decoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ class AudioDecoder
AudioStreamInfo;

AudioDecoder(const AudioStreamInfo& audio_stream_info,
SbDrmSystem drm_system,
bool use_mediacodec_callback_thread);
SbDrmSystem drm_system);
~AudioDecoder() override;

void Initialize(const OutputCB& output_cb, const ErrorCB& error_cb) override;
Expand Down Expand Up @@ -84,10 +83,6 @@ class AudioDecoder

DrmSystem* drm_system_;

// Set mediacodec callback with a handler on another thread to avoid running
// callbacks on the main thread and being blocked by other main thread tasks.
const bool use_mediacodec_callback_thread_;

OutputCB output_cb_;
ErrorCB error_cb_;
ConsumedCB consumed_cb_;
Expand Down
9 changes: 7 additions & 2 deletions starboard/android/shared/audio_renderer_passthrough.cc
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,22 @@ int ParseAc3SyncframeAudioSampleCount(const uint8_t* buffer, int size) {

AudioRendererPassthrough::AudioRendererPassthrough(
const AudioStreamInfo& audio_stream_info,
<<<<<<< HEAD
SbDrmSystem drm_system,
bool enable_audio_device_callback,
bool use_mediacodec_callback_thread)
: audio_stream_info_(audio_stream_info),
enable_audio_device_callback_(enable_audio_device_callback) {
=======
SbDrmSystem drm_system)
: audio_stream_info_(audio_stream_info) {
>>>>>>> c11007e9f58 (Revert "[android] Handle mediacodec callback with a handler thread (#2044)" (#2458))
SB_DCHECK(audio_stream_info_.codec == kSbMediaAudioCodecAc3 ||
audio_stream_info_.codec == kSbMediaAudioCodecEac3);
if (SbDrmSystemIsValid(drm_system)) {
SB_LOG(INFO) << "Creating AudioDecoder as decryptor.";
scoped_ptr<AudioDecoder> audio_decoder(new AudioDecoder(
audio_stream_info, drm_system, use_mediacodec_callback_thread));
scoped_ptr<AudioDecoder> audio_decoder(
new AudioDecoder(audio_stream_info, drm_system));
if (audio_decoder->is_valid()) {
decoder_.reset(audio_decoder.release());
}
Expand Down
4 changes: 4 additions & 0 deletions starboard/android/shared/audio_renderer_passthrough.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,13 @@ class AudioRendererPassthrough
AudioStreamInfo;

AudioRendererPassthrough(const AudioStreamInfo& audio_stream_info,
<<<<<<< HEAD
SbDrmSystem drm_system,
bool enable_audio_device_callback,
bool use_mediacodec_callback_thread);
=======
SbDrmSystem drm_system);
>>>>>>> c11007e9f58 (Revert "[android] Handle mediacodec callback with a handler thread (#2044)" (#2458))
~AudioRendererPassthrough() override;

bool is_valid() const { return decoder_ != nullptr; }
Expand Down
15 changes: 8 additions & 7 deletions starboard/android/shared/media_codec_bridge.cc
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,7 @@ Java_dev_cobalt_media_MediaCodecBridge_nativeOnMediaCodecOutputFormatChanged(
scoped_ptr<MediaCodecBridge> MediaCodecBridge::CreateAudioMediaCodecBridge(
const AudioStreamInfo& audio_stream_info,
Handler* handler,
jobject j_media_crypto,
bool use_callback_thread) {
jobject j_media_crypto) {
bool is_passthrough = false;
const char* mime =
SupportedAudioCodecToMimeType(audio_stream_info.codec, &is_passthrough);
Expand Down Expand Up @@ -194,11 +193,11 @@ scoped_ptr<MediaCodecBridge> MediaCodecBridge::CreateAudioMediaCodecBridge(
new MediaCodecBridge(handler));
jobject j_media_codec_bridge = env->CallStaticObjectMethodOrAbort(
"dev/cobalt/media/MediaCodecBridge", "createAudioMediaCodecBridge",
"(JLjava/lang/String;Ljava/lang/String;IILandroid/media/MediaCrypto;Z"
"(JLjava/lang/String;Ljava/lang/String;IILandroid/media/MediaCrypto;"
"[B)Ldev/cobalt/media/MediaCodecBridge;",
reinterpret_cast<jlong>(native_media_codec_bridge.get()), j_mime.Get(),
j_decoder_name.Get(), audio_stream_info.samples_per_second,
audio_stream_info.number_of_channels, j_media_crypto, use_callback_thread,
audio_stream_info.number_of_channels, j_media_crypto,
configuration_data.Get());

if (!j_media_codec_bridge) {
Expand Down Expand Up @@ -228,8 +227,11 @@ scoped_ptr<MediaCodecBridge> MediaCodecBridge::CreateVideoMediaCodecBridge(
bool require_software_codec,
int tunnel_mode_audio_session_id,
bool force_big_endian_hdr_metadata,
<<<<<<< HEAD
bool force_improved_support_check,
bool use_callback_thread,
=======
>>>>>>> c11007e9f58 (Revert "[android] Handle mediacodec callback with a handler thread (#2044)" (#2458))
int max_video_input_size,
std::string* error_message) {
SB_DCHECK(error_message);
Expand Down Expand Up @@ -320,15 +322,14 @@ scoped_ptr<MediaCodecBridge> MediaCodecBridge::CreateVideoMediaCodecBridge(
"(JLjava/lang/String;Ljava/lang/String;IIIIILandroid/view/Surface;"
"Landroid/media/MediaCrypto;"
"Ldev/cobalt/media/MediaCodecBridge$ColorInfo;"
"IZI"
"II"
"Ldev/cobalt/media/MediaCodecBridge$CreateMediaCodecBridgeResult;)"
"V",
reinterpret_cast<jlong>(native_media_codec_bridge.get()), j_mime.Get(),
j_decoder_name.Get(), width_hint, height_hint, fps,
max_width.value_or(-1), max_height.value_or(-1), j_surface,
j_media_crypto, j_color_info.Get(), tunnel_mode_audio_session_id,
use_callback_thread, max_video_input_size,
j_create_media_codec_bridge_result.Get());
max_video_input_size, j_create_media_codec_bridge_result.Get());

jobject j_media_codec_bridge = env->CallObjectMethodOrAbort(
j_create_media_codec_bridge_result.Get(), "mediaCodecBridge",
Expand Down
6 changes: 4 additions & 2 deletions starboard/android/shared/media_codec_bridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,7 @@ class MediaCodecBridge {
static scoped_ptr<MediaCodecBridge> CreateAudioMediaCodecBridge(
const AudioStreamInfo& audio_stream_info,
Handler* handler,
jobject j_media_crypto,
bool use_callback_thread);
jobject j_media_crypto);

// `max_width` and `max_height` can be set to positive values to specify the
// maximum resolutions the video can be adapted to.
Expand All @@ -174,8 +173,11 @@ class MediaCodecBridge {
bool require_software_codec,
int tunnel_mode_audio_session_id,
bool force_big_endian_hdr_metadata,
<<<<<<< HEAD
bool force_improved_support_check,
bool use_callback_thread,
=======
>>>>>>> c11007e9f58 (Revert "[android] Handle mediacodec callback with a handler thread (#2044)" (#2458))
int max_video_input_size,
std::string* error_message);

Expand Down
12 changes: 9 additions & 3 deletions starboard/android/shared/media_decoder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,7 @@ const char* GetDecoderName(SbMediaType media_type) {

MediaDecoder::MediaDecoder(Host* host,
const AudioStreamInfo& audio_stream_info,
SbDrmSystem drm_system,
bool use_mediacodec_callback_thread)
SbDrmSystem drm_system)
: media_type_(kSbMediaTypeAudio),
host_(host),
drm_system_(static_cast<DrmSystem*>(drm_system)),
Expand All @@ -88,7 +87,7 @@ MediaDecoder::MediaDecoder(Host* host,
jobject j_media_crypto = drm_system_ ? drm_system_->GetMediaCrypto() : NULL;
SB_DCHECK(!drm_system_ || j_media_crypto);
media_codec_bridge_ = MediaCodecBridge::CreateAudioMediaCodecBridge(
audio_stream_info, this, j_media_crypto, use_mediacodec_callback_thread);
audio_stream_info, this, j_media_crypto);
if (!media_codec_bridge_) {
SB_LOG(ERROR) << "Failed to create audio media codec bridge.";
return;
Expand Down Expand Up @@ -119,8 +118,11 @@ MediaDecoder::MediaDecoder(Host* host,
const FrameRenderedCB& frame_rendered_cb,
int tunnel_mode_audio_session_id,
bool force_big_endian_hdr_metadata,
<<<<<<< HEAD
bool force_improved_support_check,
bool use_mediacodec_callback_thread,
=======
>>>>>>> c11007e9f58 (Revert "[android] Handle mediacodec callback with a handler thread (#2044)" (#2458))
int max_video_input_size,
std::string* error_message)
: media_type_(kSbMediaTypeVideo),
Expand All @@ -139,8 +141,12 @@ MediaDecoder::MediaDecoder(Host* host,
video_codec, width_hint, height_hint, fps, max_width, max_height, this,
j_output_surface, j_media_crypto, color_metadata, require_secured_decoder,
require_software_codec, tunnel_mode_audio_session_id,
<<<<<<< HEAD
force_big_endian_hdr_metadata, force_improved_support_check,
use_mediacodec_callback_thread, max_video_input_size, error_message);
=======
force_big_endian_hdr_metadata, max_video_input_size, error_message);
>>>>>>> c11007e9f58 (Revert "[android] Handle mediacodec callback with a handler thread (#2044)" (#2458))
if (!media_codec_bridge_) {
SB_LOG(ERROR) << "Failed to create video media codec bridge with error: "
<< *error_message;
Expand Down
6 changes: 4 additions & 2 deletions starboard/android/shared/media_decoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,7 @@ class MediaDecoder

MediaDecoder(Host* host,
const AudioStreamInfo& audio_stream_info,
SbDrmSystem drm_system,
bool use_mediacodec_callback_thread);
SbDrmSystem drm_system);
MediaDecoder(Host* host,
SbMediaVideoCodec video_codec,
// `width_hint` and `height_hint` are used to create the Android
Expand All @@ -98,8 +97,11 @@ class MediaDecoder
const FrameRenderedCB& frame_rendered_cb,
int tunnel_mode_audio_session_id,
bool force_big_endian_hdr_metadata,
<<<<<<< HEAD
bool force_improved_support_check,
bool use_mediacodec_callback_thread,
=======
>>>>>>> c11007e9f58 (Revert "[android] Handle mediacodec callback with a handler thread (#2044)" (#2458))
int max_video_input_size,
std::string* error_message);
~MediaDecoder();
Expand Down
4 changes: 0 additions & 4 deletions starboard/android/shared/media_is_video_supported.cc
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,6 @@ bool SbMediaIsVideoSupported(SbMediaVideoCodec video_codec,
false)) {
MaxMediaCodecOutputBuffersLookupTable::GetInstance()->SetEnabled(false);
}

if (!mime_type->ValidateBoolParameter("mediacodeccallbackthread")) {
return false;
}
}

if (must_support_tunnel_mode && decode_to_texture_required) {
Expand Down
Loading

0 comments on commit 8824d8f

Please sign in to comment.