Skip to content

Commit

Permalink
Cherry pick PR #2458: Revert "[android] Handle mediacodec callback wi…
Browse files Browse the repository at this point in the history
…th a handler thread (#2044)" (#2465)

Refer to the original PR: #2458

Reverts #2044

b/326091431

Co-authored-by: Bo-Rong Chen <[email protected]>
  • Loading branch information
cobalt-github-releaser-bot and borongc authored Feb 22, 2024
1 parent d37e761 commit 9c94ba5
Show file tree
Hide file tree
Showing 13 changed files with 34 additions and 117 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
7 changes: 3 additions & 4 deletions starboard/android/shared/audio_renderer_passthrough.cc
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,15 @@ int ParseAc3SyncframeAudioSampleCount(const uint8_t* buffer, int size) {
AudioRendererPassthrough::AudioRendererPassthrough(
const AudioStreamInfo& audio_stream_info,
SbDrmSystem drm_system,
bool enable_audio_device_callback,
bool use_mediacodec_callback_thread)
bool enable_audio_device_callback)
: audio_stream_info_(audio_stream_info),
enable_audio_device_callback_(enable_audio_device_callback) {
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
3 changes: 1 addition & 2 deletions starboard/android/shared/audio_renderer_passthrough.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ class AudioRendererPassthrough

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

bool is_valid() const { return decoder_ != nullptr; }
Expand Down
13 changes: 5 additions & 8 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 @@ -229,7 +228,6 @@ scoped_ptr<MediaCodecBridge> MediaCodecBridge::CreateVideoMediaCodecBridge(
int tunnel_mode_audio_session_id,
bool force_big_endian_hdr_metadata,
bool force_improved_support_check,
bool use_callback_thread,
int max_video_input_size,
std::string* error_message) {
SB_DCHECK(error_message);
Expand Down Expand Up @@ -320,15 +318,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
4 changes: 1 addition & 3 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 @@ -175,7 +174,6 @@ class MediaCodecBridge {
int tunnel_mode_audio_session_id,
bool force_big_endian_hdr_metadata,
bool force_improved_support_check,
bool use_callback_thread,
int max_video_input_size,
std::string* error_message);

Expand Down
8 changes: 3 additions & 5 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 @@ -120,7 +119,6 @@ MediaDecoder::MediaDecoder(Host* host,
int tunnel_mode_audio_session_id,
bool force_big_endian_hdr_metadata,
bool force_improved_support_check,
bool use_mediacodec_callback_thread,
int max_video_input_size,
std::string* error_message)
: media_type_(kSbMediaTypeVideo),
Expand All @@ -140,7 +138,7 @@ MediaDecoder::MediaDecoder(Host* host,
j_output_surface, j_media_crypto, color_metadata, require_secured_decoder,
require_software_codec, tunnel_mode_audio_session_id,
force_big_endian_hdr_metadata, force_improved_support_check,
use_mediacodec_callback_thread, max_video_input_size, error_message);
max_video_input_size, error_message);
if (!media_codec_bridge_) {
SB_LOG(ERROR) << "Failed to create video media codec bridge with error: "
<< *error_message;
Expand Down
4 changes: 1 addition & 3 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 @@ -99,7 +98,6 @@ class MediaDecoder
int tunnel_mode_audio_session_id,
bool force_big_endian_hdr_metadata,
bool force_improved_support_check,
bool use_mediacodec_callback_thread,
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 9c94ba5

Please sign in to comment.