Skip to content

Commit

Permalink
[android] Create MediaCodecBridgeBuilder.java (#3497)
Browse files Browse the repository at this point in the history
Copied `MediaCodecBridge.createAudioMediaCodecBridge()` to it as is,
without any modifications, except that it's commented out. This makes
further changes more straightforward to review.

Also remove an extra ';' in MediaFormatBuilder.java.

b/345542000

Change-Id: Iabc9af942f27ba126e2b23e2849b9d4103f69957
  • Loading branch information
xiaomings authored Jun 7, 2024
1 parent a216d2b commit 959d023
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// Copyright 2024 The Cobalt Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package dev.cobalt.media;

/*
class MediaCodecBridgeBuilder {
@SuppressWarnings("unused")
@UsedByNative
public static MediaCodecBridge createAudioMediaCodecBridge(
long nativeMediaCodecBridge,
String mime,
String decoderName,
int sampleRate,
int channelCount,
MediaCrypto crypto,
@Nullable byte[] configurationData) {
if (decoderName.equals("")) {
Log.e(TAG, "Invalid decoder name.");
return null;
}
MediaCodec mediaCodec = null;
try {
Log.i(TAG, "Creating \"%s\" decoder.", decoderName);
mediaCodec = MediaCodec.createByCodecName(decoderName);
} catch (Exception e) {
Log.e(TAG, "Failed to create MediaCodec: %s, DecoderName: %s", mime, decoderName, e);
return null;
}
if (mediaCodec == null) {
return null;
}
MediaCodecBridge bridge =
new MediaCodecBridge(
nativeMediaCodecBridge, mediaCodec, mime, BitrateAdjustmentTypes.NO_ADJUSTMENT, -1);
MediaFormat mediaFormat = createAudioFormat(mime, sampleRate, channelCount);
if (mime.contains("opus")) {
if (!setOpusConfigurationData(mediaFormat, sampleRate, configurationData)) {
bridge.release();
return null;
}
} else {
// TODO: Determine if we should explicitly check the mime for AAC audio before calling
// setFrameHasADTSHeader(), as more codecs may be supported here in the future.
setFrameHasADTSHeader(mediaFormat);
}
if (!bridge.configureAudio(mediaFormat, crypto, 0)) {
Log.e(TAG, "Failed to configure audio codec.");
bridge.release();
return null;
}
if (!bridge.start()) {
Log.e(TAG, "Failed to start audio codec.");
bridge.release();
return null;
}
return bridge;
}
}
*/
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,46 @@ public static void setCodecSpecificData(MediaFormat format, byte[][] csds) {
format.setByteBuffer(name, ByteBuffer.wrap(csds[i]));
}
}

/*
@SuppressWarnings("unused")
private static boolean setOpusConfigurationData(
MediaFormat format, int sampleRate, @Nullable byte[] configurationData) {
final int MIN_OPUS_INITIALIZATION_DATA_BUFFER_SIZE = 19;
final long NANOSECONDS_IN_ONE_SECOND = 1000000000L;
// 3840 is the default seek pre-roll samples used by ExoPlayer:
// https://github.com/google/ExoPlayer/blob/0ba317b1337eaa789f05dd6c5241246478a3d1e5/library/common/src/main/java/com/google/android/exoplayer2/audio/OpusUtil.java#L30.
final int DEFAULT_SEEK_PRE_ROLL_SAMPLES = 3840;
if (configurationData == null
|| configurationData.length < MIN_OPUS_INITIALIZATION_DATA_BUFFER_SIZE) {
Log.e(
TAG,
"Failed to configure Opus audio codec. "
+ (configurationData == null
? "|configurationData| is null."
: String.format(
Locale.US,
"Configuration data size (%d) is less than the required size (%d).",
configurationData.length,
MIN_OPUS_INITIALIZATION_DATA_BUFFER_SIZE)));
return false;
}
// Both the number of samples to skip from the beginning of the stream and the amount of time
// to pre-roll when seeking must be specified when configuring the Opus decoder. Logic adapted
// from ExoPlayer:
// https://github.com/google/ExoPlayer/blob/0ba317b1337eaa789f05dd6c5241246478a3d1e5/library/common/src/main/java/com/google/android/exoplayer2/audio/OpusUtil.java#L52.
int preSkipSamples = ((configurationData[11] & 0xFF) << 8) | (configurationData[10] & 0xFF);
long preSkipNanos = (preSkipSamples * NANOSECONDS_IN_ONE_SECOND) / sampleRate;
long seekPreRollNanos =
(DEFAULT_SEEK_PRE_ROLL_SAMPLES * NANOSECONDS_IN_ONE_SECOND) / sampleRate;
MediaFormatBuilder.setCodecSpecificData(
format,
new byte[][] {
configurationData,
ByteBuffer.allocate(8).order(ByteOrder.nativeOrder()).putLong(preSkipNanos).array(),
ByteBuffer.allocate(8).order(ByteOrder.nativeOrder()).putLong(seekPreRollNanos).array(),
});
return true;
}
*/
}
;

0 comments on commit 959d023

Please sign in to comment.