Skip to content

Commit

Permalink
Clean up code for SDK_INT < 24 (#1157)
Browse files Browse the repository at this point in the history
b/293497406
  • Loading branch information
gbournou authored Aug 7, 2023
1 parent f5c1af1 commit d1714cd
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 189 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import android.net.ConnectivityManager.NetworkCallback;
import android.net.Network;
import android.net.NetworkCapabilities;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import dev.cobalt.util.Log;
Expand Down Expand Up @@ -74,32 +73,26 @@ public NetworkStatus(Context appContext) {
(ConnectivityManager) appContext.getSystemService(Context.CONNECTIVITY_SERVICE);
Log.i(TAG, "Opening NetworkStatus");
networkCallback = new CobaltNetworkCallback(this, mainHandler);
if (Build.VERSION.SDK_INT >= 24) {
connectivityManager.registerDefaultNetworkCallback(networkCallback);
callbackAdded = true;
}
connectivityManager.registerDefaultNetworkCallback(networkCallback);
callbackAdded = true;
}

public void beforeStartOrResume() {
if (connectivityManager != null && !callbackAdded) {
if (Build.VERSION.SDK_INT >= 24) {
connectivityManager.registerDefaultNetworkCallback(networkCallback);
callbackAdded = true;
}
connectivityManager.registerDefaultNetworkCallback(networkCallback);
callbackAdded = true;
}
}

public void beforeSuspend() {
if (connectivityManager != null && callbackAdded) {
if (Build.VERSION.SDK_INT >= 24) {
connectivityManager.unregisterNetworkCallback(networkCallback);
callbackAdded = false;
}
connectivityManager.unregisterNetworkCallback(networkCallback);
callbackAdded = false;
}
}

public boolean isConnected() {
if (connectivityManager != null && Build.VERSION.SDK_INT >= 24) {
if (connectivityManager != null) {
// Return the current network bandwidth when client pings the NetworkStatus service.
NetworkCapabilities cap =
connectivityManager.getNetworkCapabilities(connectivityManager.getActiveNetwork());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
import android.view.accessibility.AccessibilityManager;
import android.view.accessibility.CaptioningManager;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import dev.cobalt.account.UserAuthorizer;
import dev.cobalt.media.AudioOutputManager;
import dev.cobalt.media.CaptionSettings;
Expand Down Expand Up @@ -529,23 +528,12 @@ boolean isNetworkConnected() {
@SuppressWarnings("unused")
@UsedByNative
public boolean isMicrophoneDisconnected() {
if (Build.VERSION.SDK_INT >= 23) {
return !isMicrophoneConnectedV23();
} else {
// There is no way of checking for a connected microphone/device before API 23, so cannot
// guarantee that no microphone is connected.
return false;
}
}

@RequiresApi(23)
private boolean isMicrophoneConnectedV23() {
// A check specifically for microphones is not available before API 28, so it is assumed that a
// connected input audio device is a microphone.
AudioManager audioManager = (AudioManager) appContext.getSystemService(AUDIO_SERVICE);
AudioDeviceInfo[] devices = audioManager.getDevices(GET_DEVICES_INPUTS);
if (devices.length > 0) {
return true;
return false;
}

// fallback to check for BT voice capable RCU
Expand All @@ -555,10 +543,10 @@ private boolean isMicrophoneConnectedV23() {
final InputDevice inputDevice = inputManager.getInputDevice(inputDeviceId);
final boolean hasMicrophone = inputDevice.hasMicrophone();
if (hasMicrophone) {
return true;
return false;
}
}
return false;
return true;
}

/**
Expand All @@ -579,34 +567,6 @@ public boolean isMicrophoneMute() {
@SuppressWarnings("unused")
@UsedByNative
boolean isCurrentNetworkWireless() {
if (Build.VERSION.SDK_INT >= 23) {
return isCurrentNetworkWirelessV23();
} else {
return isCurrentNetworkWirelessDeprecated();
}
}

@SuppressWarnings("deprecation")
private boolean isCurrentNetworkWirelessDeprecated() {
ConnectivityManager connMgr =
(ConnectivityManager) appContext.getSystemService(Context.CONNECTIVITY_SERVICE);
android.net.NetworkInfo activeInfo = connMgr.getActiveNetworkInfo();
if (activeInfo == null) {
return false;
}
switch (activeInfo.getType()) {
case ConnectivityManager.TYPE_ETHERNET:
return false;
default:
// Consider anything that's not definitely wired to be wireless.
// For example, TYPE_VPN is ambiguous, but it's highly likely to be
// over wifi.
return true;
}
}

@RequiresApi(23)
private boolean isCurrentNetworkWirelessV23() {
ConnectivityManager connMgr =
(ConnectivityManager) appContext.getSystemService(Context.CONNECTIVITY_SERVICE);
Network activeNetwork = connMgr.getActiveNetwork();
Expand Down Expand Up @@ -744,7 +704,6 @@ public void setVideoSurfaceBounds(final int x, final int y, final int width, fin
}

/** Return supported hdr types. */
@RequiresApi(24)
@SuppressWarnings("unused")
@UsedByNative
public int[] getSupportedHdrTypes() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,7 @@ AudioTrackBridge createAudioTrackBridge(
audioTrackBridgeList.add(audioTrackBridge);
hasAudioDeviceChanged.set(false);

if (Build.VERSION.SDK_INT < 23
|| hasRegisteredAudioDeviceCallback
|| !enableAudioDeviceCallback) {
if (hasRegisteredAudioDeviceCallback || !enableAudioDeviceCallback) {
return audioTrackBridge;
}

Expand All @@ -106,7 +104,7 @@ private void handleConnectedDeviceChange(AudioDeviceInfo[] devices) {
TAG,
"Setting |hasAudioDeviceChanged| to true for audio device %s, %s.",
info.getProductName(),
getDeviceTypeNameV23(info.getType()));
getDeviceTypeName(info.getType()));
hasAudioDeviceChanged.set(true);
break;
}
Expand Down Expand Up @@ -158,15 +156,6 @@ int getMaxChannels() {
return 2;
}

if (Build.VERSION.SDK_INT >= 23) {
return getMaxChannelsV23();
}
return 2;
}

/** Returns the maximum number of HDMI channels for API 23 and above. */
@RequiresApi(23)
private int getMaxChannelsV23() {
AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
AudioDeviceInfo[] deviceInfos = audioManager.getDevices(AudioManager.GET_DEVICES_OUTPUTS);
int maxChannels = 2;
Expand Down Expand Up @@ -212,22 +201,6 @@ boolean getOutputDeviceInfo(int index, OutputDeviceInfo outDeviceInfo) {
return false;
}

if (Build.VERSION.SDK_INT >= 23) {
return getOutputDeviceInfoV23(index, outDeviceInfo);
}

if (index == 0) {
outDeviceInfo.type = AudioDeviceInfo.TYPE_HDMI;
outDeviceInfo.channels = getMaxChannels();
return true;
}

return false;
}

/** Returns output device info for API 23 and above. */
@RequiresApi(23)
private boolean getOutputDeviceInfoV23(int index, OutputDeviceInfo outDeviceInfo) {
AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
AudioDeviceInfo[] deviceInfos = audioManager.getDevices(AudioManager.GET_DEVICES_OUTPUTS);

Expand All @@ -250,8 +223,7 @@ private boolean getOutputDeviceInfoV23(int index, OutputDeviceInfo outDeviceInfo
}

/** Convert AudioDeviceInfo.TYPE_* to name in String */
@RequiresApi(23)
private static String getDeviceTypeNameV23(int deviceType) {
private static String getDeviceTypeName(int deviceType) {
switch (deviceType) {
case AudioDeviceInfo.TYPE_AUX_LINE:
return "TYPE_AUX_LINE";
Expand Down Expand Up @@ -304,7 +276,6 @@ private static String getDeviceTypeNameV23(int deviceType) {
}

/** Convert audio encodings in int[] to common separated values in String */
@RequiresApi(23)
private static String getEncodingNames(final int[] encodings) {
StringBuffer encodingsInString = new StringBuffer("[");
for (int i = 0; i < encodings.length; ++i) {
Expand Down Expand Up @@ -354,11 +325,6 @@ private static String getEncodingNames(final int[] encodings) {

/** Dump all audio output devices. */
public void dumpAllOutputDevices() {
if (Build.VERSION.SDK_INT < 23) {
Log.i(TAG, "dumpAllOutputDevices() is only supported in API level 23 or above.");
return;
}

Log.i(TAG, "Dumping all audio output devices:");

AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
Expand All @@ -368,7 +334,7 @@ public void dumpAllOutputDevices() {
Log.i(
TAG,
" Audio Device: %s, channels: %s, sample rates: %s, encodings: %s",
getDeviceTypeNameV23(info.getType()),
getDeviceTypeName(info.getType()),
Arrays.toString(info.getChannelCounts()),
Arrays.toString(info.getSampleRates()),
getEncodingNames(info.getEncodings()));
Expand Down Expand Up @@ -426,16 +392,6 @@ int generateTunnelModeAudioSessionId(int numberOfChannels) {
@SuppressWarnings("unused")
@UsedByNative
boolean hasPassthroughSupportFor(int encoding) {
if (Build.VERSION.SDK_INT < 23) {
Log.i(
TAG,
"Passthrough on encoding %d is rejected on api %d, as passthrough is only"
+ " supported on api 23 or later.",
encoding,
Build.VERSION.SDK_INT);
return false;
}

AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
AudioDeviceInfo[] deviceInfos = audioManager.getDevices(AudioManager.GET_DEVICES_OUTPUTS);

Expand Down Expand Up @@ -517,7 +473,6 @@ boolean hasPassthroughSupportFor(int encoding) {
}

/** Returns whether passthrough on `encoding` is supported for API 23 and above. */
@RequiresApi(23)
private boolean hasPassthroughSupportForV23(final AudioDeviceInfo[] deviceInfos, int encoding) {
for (AudioDeviceInfo info : deviceInfos) {
final int type = info.getType();
Expand All @@ -536,7 +491,7 @@ private boolean hasPassthroughSupportForV23(final AudioDeviceInfo[] deviceInfos,
"Passthrough on encoding %d is supported on %s, because getEncodings() returns"
+ " an empty array.",
encoding,
getDeviceTypeNameV23(type));
getDeviceTypeName(type));
return true;
}
for (int i = 0; i < encodings.length; ++i) {
Expand All @@ -545,15 +500,15 @@ private boolean hasPassthroughSupportForV23(final AudioDeviceInfo[] deviceInfos,
TAG,
"Passthrough on encoding %d is supported on %s.",
encoding,
getDeviceTypeNameV23(type));
getDeviceTypeName(type));
return true;
}
}
Log.i(
TAG,
"Passthrough on encoding %d is not supported on %s.",
encoding,
getDeviceTypeNameV23(type));
getDeviceTypeName(type));
}
Log.i(TAG, "Passthrough on encoding %d is not supported on any devices.", encoding);
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,12 +271,7 @@ private int write(byte[] audioData, int sizeInBytes, long presentationTimeInMicr
return writeWithAvSync(audioData, sizeInBytes, presentationTimeInMicroseconds);
}

if (Build.VERSION.SDK_INT >= 23) {
return audioTrack.write(audioData, 0, sizeInBytes, AudioTrack.WRITE_NON_BLOCKING);
} else {
ByteBuffer byteBuffer = ByteBuffer.wrap(audioData);
return audioTrack.write(byteBuffer, sizeInBytes, AudioTrack.WRITE_NON_BLOCKING);
}
return audioTrack.write(audioData, 0, sizeInBytes, AudioTrack.WRITE_NON_BLOCKING);
}

private int writeWithAvSync(
Expand All @@ -297,7 +292,7 @@ private int writeWithAvSync(
// Set the following constant to |false| to test manual sync header writing in API level 23 or
// later. Note that the code to write sync header manually only supports v1 sync header.
final boolean useAutoSyncHeaderWrite = true;
if (useAutoSyncHeaderWrite && Build.VERSION.SDK_INT >= 23) {
if (useAutoSyncHeaderWrite) {
ByteBuffer byteBuffer = ByteBuffer.wrap(audioData);
return audioTrack.write(
byteBuffer, sizeInBytes, AudioTrack.WRITE_NON_BLOCKING, presentationTimeInNanoseconds);
Expand Down Expand Up @@ -392,15 +387,6 @@ private AudioTimestamp getAudioTimestamp() {
@SuppressWarnings("unused")
@UsedByNative
private int getUnderrunCount() {
if (Build.VERSION.SDK_INT >= 24) {
return getUnderrunCountV24();
}
// The function getUnderrunCount() is added in API level 24.
return 0;
}

@RequiresApi(24)
private int getUnderrunCountV24() {
if (audioTrack == null) {
Log.e(TAG, "Unable to call getUnderrunCount() with NULL audio track.");
return 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,7 @@ public void onOutputFormatChanged(MediaCodec codec, MediaFormat format) {
mMediaCodec.setCallback(mCallback);

// TODO: support OnFrameRenderedListener for non tunnel mode
if (tunnelModeAudioSessionId != -1 && Build.VERSION.SDK_INT >= 23) {
if (tunnelModeAudioSessionId != -1) {
mTunnelModeFrameRendererListener =
new MediaCodec.OnFrameRenderedListener() {
@Override
Expand Down Expand Up @@ -995,7 +995,7 @@ private int queueSecureInputBuffer(
cryptoInfo.set(
numSubSamples, numBytesOfClearData, numBytesOfEncryptedData, keyId, iv, cipherMode);

if (Build.VERSION.SDK_INT >= 24 && cipherMode == MediaCodec.CRYPTO_MODE_AES_CBC) {
if (cipherMode == MediaCodec.CRYPTO_MODE_AES_CBC) {
cryptoInfo.setPattern(new Pattern(blocksToEncrypt, blocksToSkip));
} else if (blocksToEncrypt != 0 || blocksToSkip != 0) {
Log.e(TAG, "Pattern encryption only supported for 'cbcs' scheme (CBC mode).");
Expand All @@ -1013,13 +1013,7 @@ private int queueSecureInputBuffer(
TAG,
"Failed to queue secure input buffer: "
+ "CryptoException.ERROR_INSUFFICIENT_OUTPUT_PROTECTION");
// Note that in Android OS version before 23, the MediaDrm class doesn't expose the current
// key ids it holds. In such case the Starboard media stack is unable to notify Cobalt of
// the error via key statuses so MEDIA_CODEC_ERROR is returned instead to signal a general
// media codec error.
return Build.VERSION.SDK_INT >= 23
? MEDIA_CODEC_INSUFFICIENT_OUTPUT_PROTECTION
: MEDIA_CODEC_ERROR;
return MEDIA_CODEC_INSUFFICIENT_OUTPUT_PROTECTION;
}
Log.e(
TAG,
Expand Down Expand Up @@ -1304,11 +1298,7 @@ private int getAudioFormat(int channelCount) {
case 6:
return AudioFormat.CHANNEL_OUT_5POINT1;
case 8:
if (Build.VERSION.SDK_INT >= 23) {
return AudioFormat.CHANNEL_OUT_7POINT1_SURROUND;
} else {
return AudioFormat.CHANNEL_OUT_7POINT1;
}
return AudioFormat.CHANNEL_OUT_7POINT1_SURROUND;
default:
return AudioFormat.CHANNEL_OUT_DEFAULT;
}
Expand Down
Loading

0 comments on commit d1714cd

Please sign in to comment.