Skip to content

Commit

Permalink
1. Upgraded gradle
Browse files Browse the repository at this point in the history
2. Switched from obsolete com.google.android.exoplayer to androidx.media3:media3-exoplayer
3. Try to use low-latency hardware decoder by default if Android device supports it.
4. Set special priorities for video and audio decoder threads.
  • Loading branch information
alexeyvasilyev committed Jul 25, 2024
1 parent a09e929 commit e1a62f5
Show file tree
Hide file tree
Showing 13 changed files with 120 additions and 56 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ Unlike [ExoPlayer](https://github.com/google/ExoPlayer) which also supports RTSP
- RTSP/RTSPS over TCP.
- Video H.264 only.
- Audio AAC LC only.
- Basic/Digest authentification.
- Basic/Digest authentication.
- Supports majority of RTSP IP cameras.
- Uses Android's Low-Latency MediaCodec by default if available.


## Permissions:
Expand Down
20 changes: 10 additions & 10 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'

android {
compileSdkVersion 34
compileSdk 34
buildToolsVersion "34.0.0"

defaultConfig {
applicationId "com.alexvas.rtsp.demo"
minSdkVersion 24
targetSdkVersion 34
minSdk 24
targetSdk 34
versionCode 1
versionName "1.0"

Expand All @@ -32,7 +32,7 @@ android {
targetCompatibility JavaVersion.VERSION_17
}
kotlinOptions {
jvmTarget = "17"
jvmTarget = JavaVersion.VERSION_17.toString()
}
buildFeatures {
viewBinding true
Expand All @@ -43,17 +43,17 @@ android {
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'androidx.core:core-ktx:1.10.1'
implementation 'com.google.android.material:material:1.9.0'
implementation 'androidx.appcompat:appcompat:1.7.0'
implementation 'androidx.core:core-ktx:1.13.1'
implementation 'com.google.android.material:material:1.12.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
def androidXNavigationVersion = '2.6.0'
def androidXNavigationVersion = '2.7.7'
implementation "androidx.navigation:navigation-fragment-ktx:$androidXNavigationVersion"
implementation "androidx.navigation:navigation-ui-ktx:$androidXNavigationVersion"
implementation "androidx.navigation:navigation-fragment-ktx:$androidXNavigationVersion"
implementation "androidx.navigation:navigation-ui-ktx:$androidXNavigationVersion"
api "com.github.AppDevNext.Logcat:LogcatCoreLib:2.12"
api "com.github.AppDevNext.Logcat:LogcatCoreUI:2.12"
api "com.github.AppDevNext.Logcat:LogcatCoreLib:3.2"
api "com.github.AppDevNext.Logcat:LogcatCoreUI:3.2"
implementation project(':library-client-rtsp')
}
12 changes: 6 additions & 6 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
buildscript {

ext.kotlin_version = '1.8.21'
ext.compile_sdk_version = 34
ext.kotlin_version = '2.0.0'
ext.compile_sdk_version = 35
ext.min_sdk_version = 24
ext.target_sdk_version = 34
ext.project_version_code = 211
ext.project_version_name = '2.0.11'
ext.project_version_code = 220
ext.project_version_name = '2.2.0'

repositories {
google()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:8.0.2'
classpath 'com.android.tools.build:gradle:8.5.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
Expand All @@ -25,6 +25,6 @@ allprojects {
}
}

task clean(type: Delete) {
tasks.register('clean', Delete) {
delete rootProject.buildDir
}
4 changes: 2 additions & 2 deletions constants.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
project.ext {
androidXAnnotationVersion = '1.6.0'
exoplayerVersion = '2.19.0'
androidXAnnotationVersion = '1.8.0'
exoplayerVersion = '1.3.1'
}
Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
3 changes: 2 additions & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.2.1-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.9-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
34 changes: 21 additions & 13 deletions gradlew
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0
#

##############################################################################
#
Expand Down Expand Up @@ -55,7 +57,7 @@
# Darwin, MinGW, and NonStop.
#
# (3) This script is generated from the Groovy template
# https://github.com/gradle/gradle/blob/HEAD/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
# https://github.com/gradle/gradle/blob/HEAD/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
# within the Gradle project.
#
# You can find Gradle at https://github.com/gradle/gradle/.
Expand Down Expand Up @@ -83,10 +85,9 @@ done
# This is normally unused
# shellcheck disable=SC2034
APP_BASE_NAME=${0##*/}
APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit

# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036)
APP_HOME=$( cd -P "${APP_HOME:-./}" > /dev/null && printf '%s
' "$PWD" ) || exit

# Use the maximum available, or set MAX_FD != -1 to use that value.
MAX_FD=maximum
Expand Down Expand Up @@ -133,26 +134,29 @@ location of your Java installation."
fi
else
JAVACMD=java
which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
if ! command -v java >/dev/null 2>&1
then
die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
Please set the JAVA_HOME variable in your environment to match the
location of your Java installation."
fi
fi

# Increase the maximum file descriptors if we can.
if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then
case $MAX_FD in #(
max*)
# In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked.
# shellcheck disable=SC3045
# shellcheck disable=SC2039,SC3045
MAX_FD=$( ulimit -H -n ) ||
warn "Could not query maximum file descriptor limit"
esac
case $MAX_FD in #(
'' | soft) :;; #(
*)
# In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked.
# shellcheck disable=SC3045
# shellcheck disable=SC2039,SC3045
ulimit -n "$MAX_FD" ||
warn "Could not set maximum file descriptor limit to $MAX_FD"
esac
Expand Down Expand Up @@ -197,11 +201,15 @@ if "$cygwin" || "$msys" ; then
done
fi

# Collect all arguments for the java command;
# * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of
# shell script including quotes and variable substitutions, so put them in
# double quotes to make sure that they get re-expanded; and
# * put everything else in single quotes, so that it's not re-expanded.

# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'

# Collect all arguments for the java command:
# * DEFAULT_JVM_OPTS, JAVA_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments,
# and any embedded shellness will be escaped.
# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be
# treated as '${Hostname}' itself on the command line.

set -- \
"-Dorg.gradle.appname=$APP_BASE_NAME" \
Expand Down
22 changes: 12 additions & 10 deletions gradlew.bat
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
@rem See the License for the specific language governing permissions and
@rem limitations under the License.
@rem
@rem SPDX-License-Identifier: Apache-2.0
@rem

@if "%DEBUG%"=="" @echo off
@rem ##########################################################################
Expand Down Expand Up @@ -43,11 +45,11 @@ set JAVA_EXE=java.exe
%JAVA_EXE% -version >NUL 2>&1
if %ERRORLEVEL% equ 0 goto execute

echo.
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.
echo. 1>&2
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2
echo. 1>&2
echo Please set the JAVA_HOME variable in your environment to match the 1>&2
echo location of your Java installation. 1>&2

goto fail

Expand All @@ -57,11 +59,11 @@ set JAVA_EXE=%JAVA_HOME%/bin/java.exe

if exist "%JAVA_EXE%" goto execute

echo.
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.
echo. 1>&2
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2
echo. 1>&2
echo Please set the JAVA_HOME variable in your environment to match the 1>&2
echo location of your Java installation. 1>&2

goto fail

Expand Down
11 changes: 6 additions & 5 deletions library-client-rtsp/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ project.afterEvaluate {
}

android {
compileSdkVersion compile_sdk_version

compileSdk compile_sdk_version

defaultConfig {
minSdkVersion min_sdk_version
targetSdkVersion target_sdk_version
minSdk min_sdk_version
targetSdk target_sdk_version
}

compileOptions {
Expand All @@ -31,13 +32,13 @@ android {
}

kotlinOptions {
jvmTarget = "17"
jvmTarget = JavaVersion.VERSION_17.toString()
}

namespace 'com.alexvas.rtsp'
}

dependencies {
implementation "androidx.annotation:annotation:$androidXAnnotationVersion"
implementation "com.google.android.exoplayer:exoplayer-core:$exoplayerVersion"
implementation "androidx.media3:media3-exoplayer:$exoplayerVersion"
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.alexvas.rtsp.codec

import android.media.*
import android.os.Process
import android.util.Log
import java.nio.ByteBuffer

Expand All @@ -24,6 +25,8 @@ class AudioDecodeThread (
override fun run() {
if (DEBUG) Log.d(TAG, "$name started")

Process.setThreadPriority(Process.THREAD_PRIORITY_AUDIO)

// Creating audio decoder
val decoder = MediaCodec.createDecoderByType(mimeType)
val format = MediaFormat.createAudioFormat(mimeType, sampleRate, channelCount)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ import android.media.MediaFormat
import android.os.Build
import android.os.Handler
import android.os.Looper
import android.os.Process
import android.util.Log
import android.view.Surface
import com.alexvas.utils.MediaCodecUtils
import com.alexvas.utils.capabilitiesToString
import com.google.android.exoplayer2.util.Util
import androidx.media3.common.util.Util
import java.nio.ByteBuffer
import java.util.concurrent.TimeUnit
import java.util.concurrent.atomic.AtomicBoolean
Expand Down Expand Up @@ -100,6 +101,11 @@ class VideoDecodeThread (
else
Log.i(TAG, "Configuring surface ${safeWidthHeight.first}x${safeWidthHeight.second} w/ '$mimeType'")
format.setInteger(MediaFormat.KEY_ROTATION, rotation)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
// format.setFeatureEnabled(android.media.MediaCodecInfo.CodecCapabilities.FEATURE_LowLatency, true)
// Request low-latency for the decoder. Not all of the decoders support that.
format.setInteger(MediaFormat.KEY_LOW_LATENCY, 1)
}
return format
}

Expand All @@ -118,7 +124,11 @@ class VideoDecodeThread (
Log.w(TAG, "Cannot get hardware video decoders for mime type '$mimeType'. Using default one.")
MediaCodec.createDecoderByType(mimeType)
} else {
val name = hwDecoders[0].name
val lowLatencyDecoder = MediaCodecUtils.getLowLatencyDecoder(hwDecoders)
val name = lowLatencyDecoder?.let {
Log.i(TAG, "[$name] Dedicated low-latency decoder found '${lowLatencyDecoder.name}'")
lowLatencyDecoder.name
} ?: hwDecoders[0].name
MediaCodec.createByCodecName(name)
}
}
Expand Down Expand Up @@ -146,6 +156,18 @@ class VideoDecodeThread (
val format = getDecoderMediaFormat(decoder)
decoder.configure(format, surface, null, 0)
decoder.start()

val capabilities = decoder.codecInfo.getCapabilitiesForType(mimeType)
val lowLatencySupport = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
capabilities.isFeatureSupported(android.media.MediaCodecInfo.CodecCapabilities.FEATURE_LowLatency)
} else {
false
}
Log.i(TAG, "[$name] Video decoder '${decoder.name}' started " +
"(${if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { if (decoder.codecInfo.isHardwareAccelerated) "hardware" else "software" } else ""}, " +
"${capabilities.capabilitiesToString()}, " +
"${if (lowLatencySupport) "w/" else "w/o"} low-latency support)")

return decoder
}

Expand All @@ -172,6 +194,10 @@ class VideoDecodeThread (
override fun run() {
if (DEBUG) Log.d(TAG, "$name started")

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
Process.setThreadPriority(Process.THREAD_PRIORITY_VIDEO)
}

videoDecoderListener.onVideoDecoderStarted()

try {
Expand All @@ -191,8 +217,6 @@ class VideoDecodeThread (
}
}

Log.i(TAG, "Video decoder '${decoder.name}' started (${decoder.codecInfo.getCapabilitiesForType(mimeType).capabilitiesToString()})")

val bufferInfo = MediaCodec.BufferInfo()

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.google.android.exoplayer2.util.ParsableBitArray;
import com.google.android.exoplayer2.util.ParsableByteArray;
import androidx.media3.common.util.ParsableBitArray;
import androidx.media3.common.util.ParsableByteArray;

// https://tools.ietf.org/html/rfc3640
// +---------+-----------+-----------+---------------+
Expand Down
Loading

0 comments on commit e1a62f5

Please sign in to comment.