Skip to content

Commit

Permalink
FFmpeg jni cpp (WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shabinder committed Sep 2, 2021
1 parent 40e9b0a commit df6e969
Show file tree
Hide file tree
Showing 19 changed files with 1,079 additions and 84 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ Gemfile
Gemfile.lock
/maintenance-tasks/build/
/android/.cxx/Debug/5k2s1t1p/x86/
/ffmpeg/ffmpeg-kit-android-lib/.cxx/Debug/
4 changes: 2 additions & 2 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[submodule "spotiflyer-ios"]
path = spotiflyer-ios
url = https://github.com/Shabinder/spotiflyer-ios
[submodule "ffmpeg-android-maker"]
path = ffmpeg-android-maker
[submodule "ffmpeg/ffmpeg-android-maker"]
path = ffmpeg/ffmpeg-android-maker
url = https://github.com/Shabinder/ffmpeg-android-maker
24 changes: 0 additions & 24 deletions android/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -57,20 +57,6 @@ android {
targetSdk = Versions.targetSdkVersion
versionCode = Versions.versionCode
versionName = Versions.versionName

ndk {
abiFilters.addAll(setOf("x86", "x86_64", "armeabi-v7a", "arm64-v8a"))
}
}
sourceSets {
named("main") {
jniLibs.srcDir("../ffmpeg-android-maker/output/lib")
}
}
externalNativeBuild {
cmake {
path("CMakeLists.txt")
}
}
buildTypes {
getByName("release") {
Expand Down Expand Up @@ -103,16 +89,6 @@ android {
exclude(group = "androidx.compose.ui")
}
}
packagingOptions {
resources {
excludes.apply {
add("META-INF/*")
}
jniLibs.pickFirsts.apply {
add("**/*.so")
}
}
}
}
dependencies {
implementation(compose.material)
Expand Down
18 changes: 0 additions & 18 deletions android/src/main/cpp/main.cpp

This file was deleted.

26 changes: 20 additions & 6 deletions android/src/main/java/com/shabinder/spotiflyer/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,19 @@ import com.google.accompanist.insets.statusBarsPadding
import com.shabinder.common.core_components.ConnectionLiveData
import com.shabinder.common.core_components.analytics.AnalyticsManager
import com.shabinder.common.core_components.file_manager.FileManager
import com.shabinder.common.core_components.media_converter.AndroidMediaConverter
import com.shabinder.common.core_components.preference_manager.PreferenceManager
import com.shabinder.common.di.observeAsState
import com.shabinder.common.models.*
import com.shabinder.common.models.PlatformActions.Companion.SharedPreferencesKey
import com.shabinder.common.models.event.coroutines.success
import com.shabinder.common.providers.FetchPlatformQueryResult
import com.shabinder.common.root.SpotiFlyerRoot
import com.shabinder.common.root.callbacks.SpotiFlyerRootCallBacks
import com.shabinder.common.translations.Strings
import com.shabinder.common.uikit.configurations.SpotiFlyerTheme
import com.shabinder.common.uikit.configurations.colorOffWhite
import com.shabinder.common.uikit.screens.SpotiFlyerRootContent
import com.shabinder.spotiflyer.ffmpeg.FFmpeg
import com.shabinder.spotiflyer.service.ForegroundService
import com.shabinder.spotiflyer.ui.AnalyticsDialog
import com.shabinder.spotiflyer.ui.NetworkDialog
Expand Down Expand Up @@ -106,8 +107,15 @@ class MainActivity : ComponentActivity() {
// This app draws behind the system bars, so we want to handle fitting system windows
WindowCompat.setDecorFitsSystemWindows(window, false)
rootComponent = spotiFlyerRoot(defaultComponentContext())
Log.d("FFmpeg","init")
FFmpeg.testInit()
lifecycleScope.launch {
Log.d("FFmpeg", "init")
AndroidMediaConverter().convertAudioFile("/storage/emulated/0/Music/SpotiFlyer/Playlists/Sing-along_Punjabi/Kya_Baat_Ay.mp3","/storage/emulated/0/Music/SpotiFlyer/Playlists/Sing-along_Punjabi/Kya_Baat_Ay.temp.mp3").fold({
Log.d("FFmpeg Success",it)
}){
it.printStackTrace()
}
}
/*FFmpeg.testInit()*/
setContent {
SpotiFlyerTheme {
Surface(contentColor = colorOffWhite) {
Expand Down Expand Up @@ -246,7 +254,11 @@ class MainActivity : ComponentActivity() {
}

@Suppress("DEPRECATION")
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<out String>,
grantResults: IntArray
) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
permissionGranted.value = checkPermissions()
}
Expand All @@ -261,11 +273,13 @@ class MainActivity : ComponentActivity() {
override val fileManager: FileManager = this@MainActivity.fileManager
override val preferenceManager = this@MainActivity.preferenceManager
override val analyticsManager: AnalyticsManager = this@MainActivity.analyticsManager
override val downloadProgressFlow: MutableSharedFlow<HashMap<String, DownloadStatus>> = trackStatusFlow
override val downloadProgressFlow: MutableSharedFlow<HashMap<String, DownloadStatus>> =
trackStatusFlow
override val actions = object : Actions {

override val platformActions = object : PlatformActions {
override val imageCacheDir: String = applicationContext.cacheDir.absolutePath + File.separator
override val imageCacheDir: String =
applicationContext.cacheDir.absolutePath + File.separator
override val sharedPreferences = applicationContext.getSharedPreferences(
SharedPreferencesKey,
MODE_PRIVATE
Expand Down

This file was deleted.

3 changes: 2 additions & 1 deletion common/core-components/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ kotlin {
dependencies {
implementation(Extras.mp3agic)
implementation(Extras.Android.countly)
implementation("com.arthenica:ffmpeg-kit-audio:4.4.LTS")
implementation(project(":ffmpeg:ffmpeg-kit-android-lib"))
// implementation("com.arthenica:ffmpeg-kit-audio:4.4.LTS")
//api(files("$rootDir/libs/mobile-ffmpeg.aar"))
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ internal class AndroidAnalyticsManager(private val mainActivity: Activity) : Ana
setIdMode(DeviceId.Type.OPEN_UDID)
setViewTracking(true)
enableCrashReporting()
setLoggingEnabled(true)
setLoggingEnabled(false)
setRecordAllThreadsWithCrash()
setRequiresConsent(true)
setShouldIgnoreAppCrawlers(true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ class AndroidFileManager(
SuspendableEvent.success(trackDetails.outputFilePath)
} catch (e: Throwable) {
e.printStackTrace()
if (songFile.exists()) songFile.delete()
//if (songFile.exists()) songFile.delete()
logger.e { "${songFile.absolutePath} could not be created" }
SuspendableEvent.error(e)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
package com.shabinder.common.core_components.media_converter

import android.util.Log
import com.arthenica.ffmpegkit.FFmpegKit
import com.arthenica.ffmpegkit.ReturnCode
import com.shabinder.spotiflyer.ffmpeg.AndroidFFmpeg.runTranscode
import com.shabinder.common.models.AudioQuality
import com.shabinder.common.models.SpotiFlyerException
import org.koin.dsl.bind
import org.koin.dsl.module
import com.arthenica.ffmpegkit.FFprobeKit

import com.arthenica.ffmpegkit.MediaInformationSession
import kotlin.math.ceil
import kotlin.math.roundToInt


class AndroidMediaConverter : MediaConverter() {
Expand All @@ -21,7 +13,10 @@ class AndroidMediaConverter : MediaConverter() {
audioQuality: AudioQuality,
progressCallbacks: (Long) -> Unit,
) = executeSafelyInPool {
val kbpsArg = if (audioQuality == AudioQuality.UNKNOWN) {
// 192 is Default
val audioBitrate = if (audioQuality == AudioQuality.UNKNOWN) 192 else audioQuality.kbps.toIntOrNull() ?: 192
runTranscode(inputFilePath,outputFilePath,audioBitrate).toString()
/*val kbpsArg = if (audioQuality == AudioQuality.UNKNOWN) {
val mediaInformation = FFprobeKit.getMediaInformation(inputFilePath)
val bitrate = ((mediaInformation.mediaInformation.bitrate).toFloat()/1000).roundToInt()
Log.d("MEDIA-INPUT Bit", bitrate.toString())
Expand All @@ -41,7 +36,7 @@ class AndroidMediaConverter : MediaConverter() {
throw SpotiFlyerException.MP3ConversionFailed("FFmpeg Conversion Canceled for $inputFilePath")
}
else -> throw SpotiFlyerException.MP3ConversionFailed("FFmpeg Conversion Failed for $inputFilePath")
}
}*/
}
}

Expand Down
1 change: 0 additions & 1 deletion ffmpeg-android-maker
Submodule ffmpeg-android-maker deleted from b1dc4b
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ set(
# List variable name
ffmpeg_libs_names
# Values in the list
avutil avformat avcodec avresample swresample
avutil avformat avcodec swresample avdevice avfilter swscale
)

foreach (ffmpeg_lib_name ${ffmpeg_libs_names})
Expand All @@ -28,22 +28,29 @@ endforeach ()

add_library(
# Name for a library to build
spotiflyer-converter
spotiflyer-ffmpeg
# Type of a library
SHARED
# All cpp files to compile
src/main/cpp/main.cpp
# src/main/cpp/media_file_builder.cpp
# src/main/cpp/media_file_builder_jni.cpp
# src/main/cpp/frame_loader_context.cpp
# src/main/cpp/frame_loader_context_jni.cpp
# src/main/cpp/frame_extractor.cpp
# src/main/cpp/utils.cpp
# mobile-ffmpeg
src/main/cpp/doc_examples_transcode_aac.c

# ffmpeg-kit
# src/main/cpp/ffmpegkit.c
# src/main/cpp/ffprobekit.c
# src/main/cpp/ffmpegkit_exception.c
# src/main/cpp/fftools_cmdutils.c
# src/main/cpp/fftools_ffmpeg.c
# src/main/cpp/fftools_ffprobe.c
# src/main/cpp/fftools_ffmpeg_opt.c
# src/main/cpp/fftools_ffmpeg_hw.c
# src/main/cpp/fftools_ffmpeg_filter.c
# src/main/cpp/saf_wrapper.c
)

target_link_libraries(
# Library to link
spotiflyer-converter
spotiflyer-ffmpeg
# List of libraries to link against:
# Library for writing messages in LogCat
log
Expand Down
63 changes: 63 additions & 0 deletions ffmpeg/ffmpeg-kit-android-lib/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
plugins {
id("com.android.library")
id("kotlin-android")
}

android {
//ndkVersion "22.0.7026061"
compileSdk = Versions.compileSdkVersion
buildToolsVersion = "30.0.3"

defaultConfig {
consumerProguardFile("proguard-rules.pro")

minSdk = Versions.minSdkVersion
targetSdk = Versions.targetSdkVersion

/*versionCode = Versions.versionCode
versionName = Versions.versionName*/

ndk {
abiFilters.addAll(setOf("x86", "x86_64", "armeabi-v7a", "arm64-v8a"))
}
}

sourceSets {
named("main") {
jniLibs.srcDir("../ffmpeg-android-maker/output/lib")
}
}
externalNativeBuild {
cmake {
path("CMakeLists.txt")
}
}

buildTypes {
getByName("release") {
isMinifyEnabled = false
proguardFiles(
getDefaultProguardFile("proguard-android.txt"),
"proguard-rules.pro"
)
}
}

compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}

packagingOptions {
resources {
excludes.apply {
add("META-INF/*")
}
jniLibs.pickFirsts.apply {
add("**/*.so")
}
}
}
}

dependencies { /**/ }
17 changes: 17 additions & 0 deletions ffmpeg/ffmpeg-kit-android-lib/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.kts.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

-keep class com.arthenica.ffmpegkit.FFmpegKitConfig {
native <methods>;
void log(long, int, byte[]);
void statistics(long, int, float, float, long , int, double, double);
void closeParcelFileDescriptor(int);
}

-keep class com.arthenica.ffmpegkit.AbiDetect {
native <methods>;
}
4 changes: 4 additions & 0 deletions ffmpeg/ffmpeg-kit-android-lib/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.shabinder.spotiflyer.ffmpeg">

</manifest>
2 changes: 2 additions & 0 deletions ffmpeg/ffmpeg-kit-android-lib/src/main/cpp/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/android_lts_support.o
/libandroidltssupport.a
Loading

0 comments on commit df6e969

Please sign in to comment.