react-native-audio-api 0.10.0-nightly-e16d7ff-20251030 → 0.11.0-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/RNAudioAPI.podspec +11 -16
- package/android/build.gradle +3 -33
- package/android/src/main/cpp/audioapi/CMakeLists.txt +0 -6
- package/android/src/main/cpp/audioapi/android/AudioAPIModule.cpp +0 -5
- package/android/src/main/cpp/audioapi/android/AudioAPIModule.h +0 -1
- package/android/src/main/cpp/audioapi/android/core/AndroidAudioRecorder.cpp +142 -28
- package/android/src/main/cpp/audioapi/android/core/AndroidAudioRecorder.h +40 -13
- package/android/src/main/cpp/audioapi/android/core/AudioPlayer.cpp +2 -4
- package/android/src/main/cpp/audioapi/android/core/NativeAudioRecorder.hpp +9 -9
- package/android/src/main/cpp/audioapi/android/core/utils/AndroidFileWriterBackend.h +37 -0
- package/android/src/main/cpp/audioapi/android/core/utils/AndroidRecorderCallback.cpp +187 -0
- package/android/src/main/cpp/audioapi/android/core/utils/AndroidRecorderCallback.h +57 -0
- package/android/src/main/cpp/audioapi/android/core/utils/AudioDecoder.cpp +0 -6
- package/android/src/main/cpp/audioapi/android/core/utils/FileUtils.h +34 -0
- package/android/src/main/cpp/audioapi/android/core/utils/FileUtilts.cpp +133 -0
- package/android/src/main/cpp/audioapi/android/core/utils/MiniaudioImplementation.cpp +3 -0
- package/android/src/main/cpp/audioapi/android/core/utils/ffmpegBackend/FFmpegAudioFileOptions.cpp +154 -0
- package/android/src/main/cpp/audioapi/android/core/utils/ffmpegBackend/FFmpegAudioFileOptions.h +41 -0
- package/android/src/main/cpp/audioapi/android/core/utils/ffmpegBackend/FFmpegFileWriter.cpp +429 -0
- package/android/src/main/cpp/audioapi/android/core/utils/ffmpegBackend/FFmpegFileWriter.h +113 -0
- package/android/src/main/cpp/audioapi/android/core/utils/miniaudioBackend/MiniAudioFileOptions.cpp +47 -0
- package/android/src/main/cpp/audioapi/android/core/utils/miniaudioBackend/MiniAudioFileOptions.h +28 -0
- package/android/src/main/cpp/audioapi/android/core/utils/miniaudioBackend/MiniAudioFileWriter.cpp +269 -0
- package/android/src/main/cpp/audioapi/android/core/utils/miniaudioBackend/MiniAudioFileWriter.h +47 -0
- package/android/src/main/cpp/audioapi/android/system/NativeFileInfo.hpp +31 -0
- package/android/src/main/java/com/swmansion/audioapi/AudioAPIModule.kt +3 -29
- package/android/src/main/java/com/swmansion/audioapi/system/AudioFocusListener.kt +9 -21
- package/android/src/main/java/com/swmansion/audioapi/system/LockScreenManager.kt +6 -27
- package/android/src/main/java/com/swmansion/audioapi/system/MediaNotificationManager.kt +0 -11
- package/android/src/main/java/com/swmansion/audioapi/system/NativeFileInfo.kt +18 -0
- package/android/src/main/jniLibs/arm64-v8a/libavcodec.so +0 -0
- package/android/src/main/jniLibs/arm64-v8a/libavformat.so +0 -0
- package/android/src/main/jniLibs/arm64-v8a/libavutil.so +0 -0
- package/android/src/main/jniLibs/arm64-v8a/libswresample.so +0 -0
- package/android/src/main/jniLibs/armeabi-v7a/libavcodec.so +0 -0
- package/android/src/main/jniLibs/armeabi-v7a/libavformat.so +0 -0
- package/android/src/main/jniLibs/armeabi-v7a/libavutil.so +0 -0
- package/android/src/main/jniLibs/armeabi-v7a/libswresample.so +0 -0
- package/android/src/main/jniLibs/x86/libavcodec.so +0 -0
- package/android/src/main/jniLibs/x86/libavformat.so +0 -0
- package/android/src/main/jniLibs/x86/libavutil.so +0 -0
- package/android/src/main/jniLibs/x86/libswresample.so +0 -0
- package/android/src/main/jniLibs/x86_64/libavcodec.so +0 -0
- package/android/src/main/jniLibs/x86_64/libavformat.so +0 -0
- package/android/src/main/jniLibs/x86_64/libavutil.so +0 -0
- package/android/src/main/jniLibs/x86_64/libswresample.so +0 -0
- package/common/cpp/audioapi/AudioAPIModuleInstaller.h +2 -30
- package/common/cpp/audioapi/HostObjects/BaseAudioContextHostObject.cpp +0 -18
- package/common/cpp/audioapi/HostObjects/BaseAudioContextHostObject.h +0 -1
- package/common/cpp/audioapi/HostObjects/effects/BiquadFilterNodeHostObject.cpp +1 -1
- package/common/cpp/audioapi/HostObjects/inputs/AudioRecorderHostObject.cpp +97 -18
- package/common/cpp/audioapi/HostObjects/inputs/AudioRecorderHostObject.h +18 -6
- package/common/cpp/audioapi/core/AudioNode.h +2 -3
- package/common/cpp/audioapi/core/AudioParam.cpp +2 -2
- package/common/cpp/audioapi/core/BaseAudioContext.cpp +0 -10
- package/common/cpp/audioapi/core/BaseAudioContext.h +0 -2
- package/common/cpp/audioapi/core/effects/BiquadFilterNode.cpp +32 -69
- package/common/cpp/audioapi/core/effects/BiquadFilterNode.h +1 -37
- package/common/cpp/audioapi/core/inputs/AudioRecorder.cpp +36 -82
- package/common/cpp/audioapi/core/inputs/AudioRecorder.h +52 -31
- package/common/cpp/audioapi/core/sources/StreamerNode.cpp +58 -59
- package/common/cpp/audioapi/core/sources/StreamerNode.h +8 -37
- package/common/cpp/audioapi/core/utils/AudioNodeManager.cpp +0 -5
- package/common/cpp/audioapi/core/utils/Constants.h +1 -2
- package/common/cpp/audioapi/core/utils/worklets/SafeIncludes.h +10 -0
- package/common/cpp/audioapi/dsp/AudioUtils.cpp +1 -1
- package/common/cpp/audioapi/dsp/FFT.cpp +26 -0
- package/common/cpp/audioapi/dsp/FFT.h +2 -26
- package/common/cpp/audioapi/external/arm64-v8a/libcrypto.a +0 -0
- package/common/cpp/audioapi/external/arm64-v8a/libogg.a +0 -0
- package/common/cpp/audioapi/external/arm64-v8a/libopus.a +0 -0
- package/common/cpp/audioapi/external/arm64-v8a/libopusfile.a +0 -0
- package/common/cpp/audioapi/external/arm64-v8a/libssl.a +0 -0
- package/common/cpp/audioapi/external/arm64-v8a/libvorbis.a +0 -0
- package/common/cpp/audioapi/external/arm64-v8a/libvorbisenc.a +0 -0
- package/common/cpp/audioapi/external/arm64-v8a/libvorbisfile.a +0 -0
- package/common/cpp/audioapi/external/armeabi-v7a/libcrypto.a +0 -0
- package/common/cpp/audioapi/external/armeabi-v7a/libogg.a +0 -0
- package/common/cpp/audioapi/external/armeabi-v7a/libopus.a +0 -0
- package/common/cpp/audioapi/external/armeabi-v7a/libopusfile.a +0 -0
- package/common/cpp/audioapi/external/armeabi-v7a/libssl.a +0 -0
- package/common/cpp/audioapi/external/armeabi-v7a/libvorbis.a +0 -0
- package/common/cpp/audioapi/external/armeabi-v7a/libvorbisenc.a +0 -0
- package/common/cpp/audioapi/external/armeabi-v7a/libvorbisfile.a +0 -0
- package/common/cpp/audioapi/external/ffmpeg_include/libavcodec/avcodec.h +2 -2
- package/common/cpp/audioapi/external/ffmpeg_include/libavcodec/codec.h +1 -1
- package/common/cpp/audioapi/external/ffmpeg_include/libavcodec/codec_desc.h +1 -1
- package/common/cpp/audioapi/external/ffmpeg_include/libavcodec/codec_id.h +1 -1
- package/common/cpp/audioapi/external/ffmpeg_include/libavcodec/codec_par.h +1 -1
- package/common/cpp/audioapi/external/ffmpeg_include/libavformat/avformat.h +2 -2
- package/common/cpp/audioapi/external/ffmpeg_include/libavutil/avutil.h +2 -2
- package/common/cpp/audioapi/external/iphoneos/libcrypto.a +0 -0
- package/common/cpp/audioapi/external/iphoneos/libogg.a +0 -0
- package/common/cpp/audioapi/external/iphoneos/libopus.a +0 -0
- package/common/cpp/audioapi/external/iphoneos/libopusfile.a +0 -0
- package/common/cpp/audioapi/external/iphoneos/libssl.a +0 -0
- package/common/cpp/audioapi/external/iphoneos/libvorbis.a +0 -0
- package/common/cpp/audioapi/external/iphoneos/libvorbisenc.a +0 -0
- package/common/cpp/audioapi/external/iphoneos/libvorbisfile.a +0 -0
- package/common/cpp/audioapi/external/iphonesimulator/libcrypto.a +0 -0
- package/common/cpp/audioapi/external/iphonesimulator/libogg.a +0 -0
- package/common/cpp/audioapi/external/iphonesimulator/libopus.a +0 -0
- package/common/cpp/audioapi/external/iphonesimulator/libopusfile.a +0 -0
- package/common/cpp/audioapi/external/iphonesimulator/libssl.a +0 -0
- package/common/cpp/audioapi/external/iphonesimulator/libvorbis.a +0 -0
- package/common/cpp/audioapi/external/iphonesimulator/libvorbisenc.a +0 -0
- package/common/cpp/audioapi/external/iphonesimulator/libvorbisfile.a +0 -0
- package/common/cpp/audioapi/external/libavcodec.xcframework/Info.plist +44 -0
- package/common/cpp/audioapi/external/libavcodec.xcframework/ios-arm64/libavcodec.framework/Info.plist +1 -0
- package/common/cpp/audioapi/external/libavcodec.xcframework/ios-arm64/libavcodec.framework/libavcodec +0 -0
- package/common/cpp/audioapi/external/libavcodec.xcframework/ios-arm64_x86_64-simulator/libavcodec.framework/Info.plist +1 -0
- package/common/cpp/audioapi/external/libavcodec.xcframework/ios-arm64_x86_64-simulator/libavcodec.framework/libavcodec +0 -0
- package/common/cpp/audioapi/external/libavformat.xcframework/Info.plist +44 -0
- package/common/cpp/audioapi/external/libavformat.xcframework/ios-arm64/libavformat.framework/Info.plist +1 -0
- package/common/cpp/audioapi/external/libavformat.xcframework/ios-arm64/libavformat.framework/libavformat +0 -0
- package/common/cpp/audioapi/external/libavformat.xcframework/ios-arm64_x86_64-simulator/libavformat.framework/Info.plist +1 -0
- package/common/cpp/audioapi/external/libavformat.xcframework/ios-arm64_x86_64-simulator/libavformat.framework/libavformat +0 -0
- package/common/cpp/audioapi/external/libavutil.xcframework/Info.plist +44 -0
- package/common/cpp/audioapi/external/libavutil.xcframework/ios-arm64/libavutil.framework/Info.plist +1 -0
- package/common/cpp/audioapi/external/libavutil.xcframework/ios-arm64/libavutil.framework/libavutil +0 -0
- package/common/cpp/audioapi/external/libavutil.xcframework/ios-arm64_x86_64-simulator/libavutil.framework/Info.plist +1 -0
- package/common/cpp/audioapi/external/libavutil.xcframework/ios-arm64_x86_64-simulator/libavutil.framework/libavutil +0 -0
- package/common/cpp/audioapi/external/libswresample.xcframework/Info.plist +44 -0
- package/common/cpp/audioapi/external/libswresample.xcframework/ios-arm64/libswresample.framework/Info.plist +1 -0
- package/common/cpp/audioapi/external/libswresample.xcframework/ios-arm64/libswresample.framework/libswresample +0 -0
- package/common/cpp/audioapi/external/libswresample.xcframework/ios-arm64_x86_64-simulator/libswresample.framework/Info.plist +1 -0
- package/common/cpp/audioapi/external/libswresample.xcframework/ios-arm64_x86_64-simulator/libswresample.framework/libswresample +0 -0
- package/common/cpp/audioapi/external/x86/libcrypto.a +0 -0
- package/common/cpp/audioapi/external/x86/libogg.a +0 -0
- package/common/cpp/audioapi/external/x86/libopus.a +0 -0
- package/common/cpp/audioapi/external/x86/libopusfile.a +0 -0
- package/common/cpp/audioapi/external/x86/libssl.a +0 -0
- package/common/cpp/audioapi/external/x86/libvorbis.a +0 -0
- package/common/cpp/audioapi/external/x86/libvorbisenc.a +0 -0
- package/common/cpp/audioapi/external/x86/libvorbisfile.a +0 -0
- package/common/cpp/audioapi/external/x86_64/libcrypto.a +0 -0
- package/common/cpp/audioapi/external/x86_64/libogg.a +0 -0
- package/common/cpp/audioapi/external/x86_64/libopus.a +0 -0
- package/common/cpp/audioapi/external/x86_64/libopusfile.a +0 -0
- package/common/cpp/audioapi/external/x86_64/libssl.a +0 -0
- package/common/cpp/audioapi/external/x86_64/libvorbis.a +0 -0
- package/common/cpp/audioapi/external/x86_64/libvorbisenc.a +0 -0
- package/common/cpp/audioapi/external/x86_64/libvorbisfile.a +0 -0
- package/common/cpp/audioapi/libs/ffmpeg/FFmpegDecoding.cpp +3 -2
- package/common/cpp/audioapi/libs/ffmpeg/INSTRUCTIONS.md +32 -0
- package/common/cpp/audioapi/libs/ffmpeg/create_xcframework.sh +111 -0
- package/common/cpp/audioapi/libs/ffmpeg/ffmpeg_setup.sh +391 -0
- package/common/cpp/audioapi/utils/AudioBus.cpp +0 -28
- package/common/cpp/audioapi/utils/AudioBus.h +0 -3
- package/common/cpp/audioapi/utils/ThreadPool.hpp +1 -59
- package/common/cpp/test/CMakeLists.txt +14 -19
- package/ios/audioapi/ios/AudioAPIModule.h +1 -2
- package/ios/audioapi/ios/AudioAPIModule.mm +0 -2
- package/ios/audioapi/ios/core/IOSAudioFileOptions.h +36 -0
- package/ios/audioapi/ios/core/IOSAudioFileOptions.mm +140 -0
- package/ios/audioapi/ios/core/IOSAudioFileWriter.h +51 -0
- package/ios/audioapi/ios/core/IOSAudioFileWriter.mm +223 -0
- package/ios/audioapi/ios/core/IOSAudioRecorder.h +28 -10
- package/ios/audioapi/ios/core/IOSAudioRecorder.mm +117 -30
- package/ios/audioapi/ios/core/IOSRecorderCallback.h +57 -0
- package/ios/audioapi/ios/core/IOSRecorderCallback.mm +189 -0
- package/ios/audioapi/ios/core/NativeAudioRecorder.h +4 -9
- package/ios/audioapi/ios/core/NativeAudioRecorder.m +29 -71
- package/ios/audioapi/ios/core/utils/AudioDecoder.mm +1 -0
- package/ios/audioapi/ios/system/AudioEngine.mm +3 -3
- package/ios/audioapi/ios/system/AudioSessionManager.mm +0 -9
- package/ios/audioapi/ios/system/LockScreenManager.h +1 -0
- package/ios/audioapi/ios/system/LockScreenManager.mm +19 -6
- package/lib/commonjs/api.js +76 -141
- package/lib/commonjs/api.js.map +1 -1
- package/lib/commonjs/api.web.js +0 -8
- package/lib/commonjs/api.web.js.map +1 -1
- package/lib/commonjs/core/AudioContext.js +1 -1
- package/lib/commonjs/core/AudioContext.js.map +1 -1
- package/lib/commonjs/core/AudioRecorder.js +171 -13
- package/lib/commonjs/core/AudioRecorder.js.map +1 -1
- package/lib/commonjs/core/BaseAudioContext.js +25 -28
- package/lib/commonjs/core/BaseAudioContext.js.map +1 -1
- package/lib/commonjs/core/OfflineAudioContext.js +1 -1
- package/lib/commonjs/core/OfflineAudioContext.js.map +1 -1
- package/lib/commonjs/types.js +46 -0
- package/lib/commonjs/types.js.map +1 -1
- package/lib/commonjs/utils/bitEnums.js +33 -0
- package/lib/commonjs/utils/bitEnums.js.map +1 -0
- package/lib/commonjs/utils/index.js +21 -19
- package/lib/commonjs/utils/index.js.map +1 -1
- package/lib/commonjs/web-core/AudioContext.js +0 -12
- package/lib/commonjs/web-core/AudioContext.js.map +1 -1
- package/lib/commonjs/web-core/OfflineAudioContext.js +0 -12
- package/lib/commonjs/web-core/OfflineAudioContext.js.map +1 -1
- package/lib/module/api.js +15 -16
- package/lib/module/api.js.map +1 -1
- package/lib/module/api.web.js +0 -1
- package/lib/module/api.web.js.map +1 -1
- package/lib/module/core/AudioContext.js +2 -2
- package/lib/module/core/AudioContext.js.map +1 -1
- package/lib/module/core/AudioRecorder.js +171 -13
- package/lib/module/core/AudioRecorder.js.map +1 -1
- package/lib/module/core/BaseAudioContext.js +26 -29
- package/lib/module/core/BaseAudioContext.js.map +1 -1
- package/lib/module/core/OfflineAudioContext.js +2 -2
- package/lib/module/core/OfflineAudioContext.js.map +1 -1
- package/lib/module/types.js +45 -1
- package/lib/module/types.js.map +1 -1
- package/lib/module/utils/bitEnums.js +27 -0
- package/lib/module/utils/bitEnums.js.map +1 -0
- package/lib/module/utils/index.js +2 -15
- package/lib/module/utils/index.js.map +1 -1
- package/lib/module/web-core/AudioContext.js +0 -12
- package/lib/module/web-core/AudioContext.js.map +1 -1
- package/lib/module/web-core/OfflineAudioContext.js +0 -12
- package/lib/module/web-core/OfflineAudioContext.js.map +1 -1
- package/lib/typescript/api.d.ts +17 -19
- package/lib/typescript/api.d.ts.map +1 -1
- package/lib/typescript/api.web.d.ts +0 -1
- package/lib/typescript/api.web.d.ts.map +1 -1
- package/lib/typescript/core/AudioContext.d.ts.map +1 -1
- package/lib/typescript/core/AudioRecorder.d.ts +62 -7
- package/lib/typescript/core/AudioRecorder.d.ts.map +1 -1
- package/lib/typescript/core/BaseAudioContext.d.ts +1 -3
- package/lib/typescript/core/BaseAudioContext.d.ts.map +1 -1
- package/lib/typescript/events/types.d.ts +16 -0
- package/lib/typescript/events/types.d.ts.map +1 -1
- package/lib/typescript/interfaces.d.ts +42 -9
- package/lib/typescript/interfaces.d.ts.map +1 -1
- package/lib/typescript/types.d.ts +79 -8
- package/lib/typescript/types.d.ts.map +1 -1
- package/lib/typescript/utils/bitEnums.d.ts +4 -0
- package/lib/typescript/utils/bitEnums.d.ts.map +1 -0
- package/lib/typescript/utils/index.d.ts +2 -5
- package/lib/typescript/utils/index.d.ts.map +1 -1
- package/lib/typescript/web-core/AudioContext.d.ts +0 -3
- package/lib/typescript/web-core/AudioContext.d.ts.map +1 -1
- package/lib/typescript/web-core/BaseAudioContext.d.ts +0 -2
- package/lib/typescript/web-core/BaseAudioContext.d.ts.map +1 -1
- package/lib/typescript/web-core/OfflineAudioContext.d.ts +0 -3
- package/lib/typescript/web-core/OfflineAudioContext.d.ts.map +1 -1
- package/package.json +4 -7
- package/src/api.ts +18 -45
- package/src/api.web.ts +0 -1
- package/src/core/AudioContext.ts +2 -3
- package/src/core/AudioRecorder.ts +211 -24
- package/src/core/BaseAudioContext.ts +60 -67
- package/src/core/OfflineAudioContext.ts +2 -2
- package/src/events/types.ts +18 -0
- package/src/interfaces.ts +52 -16
- package/src/types.ts +90 -9
- package/src/utils/bitEnums.ts +51 -0
- package/src/utils/index.ts +2 -22
- package/src/web-core/AudioContext.tsx +0 -25
- package/src/web-core/BaseAudioContext.tsx +0 -2
- package/src/web-core/OfflineAudioContext.tsx +0 -25
- package/common/cpp/audioapi/HostObjects/effects/ConvolverNodeHostObject.cpp +0 -47
- package/common/cpp/audioapi/HostObjects/effects/ConvolverNodeHostObject.h +0 -20
- package/common/cpp/audioapi/core/effects/ConvolverNode.cpp +0 -210
- package/common/cpp/audioapi/core/effects/ConvolverNode.h +0 -55
- package/common/cpp/audioapi/dsp/Convolver.cpp +0 -213
- package/common/cpp/audioapi/dsp/Convolver.h +0 -45
- package/common/cpp/audioapi/libs/ffmpeg/relinking.md +0 -24
- package/common/cpp/audioapi/utils/AlignedAllocator.hpp +0 -50
- package/common/cpp/test/src/biquad/BiquadFilterChromium.cpp +0 -389
- package/common/cpp/test/src/biquad/BiquadFilterChromium.h +0 -64
- package/common/cpp/test/src/biquad/BiquadFilterTest.cpp +0 -284
- package/common/cpp/test/src/biquad/BiquadFilterTest.h +0 -40
- package/lib/commonjs/core/ConvolverNode.js +0 -37
- package/lib/commonjs/core/ConvolverNode.js.map +0 -1
- package/lib/commonjs/web-core/ConvolverNode.js +0 -40
- package/lib/commonjs/web-core/ConvolverNode.js.map +0 -1
- package/lib/commonjs/web-core/ConvolverNodeOptions.js +0 -6
- package/lib/commonjs/web-core/ConvolverNodeOptions.js.map +0 -1
- package/lib/module/core/ConvolverNode.js +0 -31
- package/lib/module/core/ConvolverNode.js.map +0 -1
- package/lib/module/web-core/ConvolverNode.js +0 -34
- package/lib/module/web-core/ConvolverNode.js.map +0 -1
- package/lib/module/web-core/ConvolverNodeOptions.js +0 -4
- package/lib/module/web-core/ConvolverNodeOptions.js.map +0 -1
- package/lib/typescript/core/ConvolverNode.d.ts +0 -12
- package/lib/typescript/core/ConvolverNode.d.ts.map +0 -1
- package/lib/typescript/web-core/ConvolverNode.d.ts +0 -11
- package/lib/typescript/web-core/ConvolverNode.d.ts.map +0 -1
- package/lib/typescript/web-core/ConvolverNodeOptions.d.ts +0 -6
- package/lib/typescript/web-core/ConvolverNodeOptions.d.ts.map +0 -1
- package/scripts/download-prebuilt-binaries.sh +0 -52
- package/scripts/rnaa_utils.rb +0 -8
- package/scripts/validate-worklets-version.js +0 -28
- package/src/core/ConvolverNode.ts +0 -35
- package/src/web-core/ConvolverNode.tsx +0 -43
- package/src/web-core/ConvolverNodeOptions.tsx +0 -6
|
@@ -0,0 +1,429 @@
|
|
|
1
|
+
#include <android/log.h>
|
|
2
|
+
#include <cmath>
|
|
3
|
+
|
|
4
|
+
extern "C" {
|
|
5
|
+
#include <libavformat/avio.h>
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
#include <audioapi/android/core/utils/AndroidFileWriterBackend.h>
|
|
9
|
+
#include <audioapi/android/core/utils/ffmpegBackend/FFmpegAudioFileOptions.h>
|
|
10
|
+
#include <audioapi/android/core/utils/ffmpegBackend/FFmpegFileWriter.h>
|
|
11
|
+
|
|
12
|
+
constexpr double BYTES_TO_MB = 1024.0 * 1024.0;
|
|
13
|
+
|
|
14
|
+
namespace audioapi {
|
|
15
|
+
|
|
16
|
+
FFmpegAudioFileWriter::FFmpegAudioFileWriter(
|
|
17
|
+
float sampleRate,
|
|
18
|
+
size_t channelCount,
|
|
19
|
+
size_t bitRate,
|
|
20
|
+
size_t androidFlags)
|
|
21
|
+
: AndroidFileWriterBackend(
|
|
22
|
+
sampleRate,
|
|
23
|
+
channelCount,
|
|
24
|
+
bitRate,
|
|
25
|
+
androidFlags) {
|
|
26
|
+
fileOptions_ = std::make_shared<FFmpegAudioFileOptions>(
|
|
27
|
+
sampleRate, channelCount, bitRate, androidFlags);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
FFmpegAudioFileWriter::~FFmpegAudioFileWriter() {
|
|
31
|
+
isFileOpen_.store(false);
|
|
32
|
+
fileOptions_.reset();
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
std::string FFmpegAudioFileWriter::openFile(
|
|
36
|
+
int32_t streamSampleRate,
|
|
37
|
+
int32_t streamChannelCount,
|
|
38
|
+
int32_t streamMaxBufferSize) {
|
|
39
|
+
filePath_ = fileOptions_->getFilePath("audio");
|
|
40
|
+
|
|
41
|
+
streamSampleRate_ = streamSampleRate;
|
|
42
|
+
streamChannelCount_ = streamChannelCount;
|
|
43
|
+
streamMaxBufferSize_ = streamMaxBufferSize;
|
|
44
|
+
framesWritten_.store(0);
|
|
45
|
+
nextPts_ = 0;
|
|
46
|
+
|
|
47
|
+
const AVCodec *codec = fileOptions_->getCodec();
|
|
48
|
+
AVFormatContext *rawFormatCtx = nullptr;
|
|
49
|
+
|
|
50
|
+
if (avformat_alloc_output_context2(
|
|
51
|
+
&rawFormatCtx,
|
|
52
|
+
nullptr,
|
|
53
|
+
fileOptions_->getMuxerName().c_str(),
|
|
54
|
+
filePath_.c_str()) < 0 ||
|
|
55
|
+
!rawFormatCtx) {
|
|
56
|
+
__android_log_print(
|
|
57
|
+
ANDROID_LOG_ERROR,
|
|
58
|
+
"FFmpegFileWriter",
|
|
59
|
+
"Failed to allocate FFmpeg format context for file: %s",
|
|
60
|
+
filePath_.c_str());
|
|
61
|
+
return "";
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
formatCtx_ = AVFormatContextPtr(rawFormatCtx);
|
|
65
|
+
stream_ = avformat_new_stream(formatCtx_.get(), codec);
|
|
66
|
+
|
|
67
|
+
encoderCtx_ = AVCodecContextPtr(avcodec_alloc_context3(codec));
|
|
68
|
+
|
|
69
|
+
if (!encoderCtx_) {
|
|
70
|
+
__android_log_print(
|
|
71
|
+
ANDROID_LOG_ERROR,
|
|
72
|
+
"FFmpegFileWriter",
|
|
73
|
+
"Failed to allocate FFmpeg codec context for file");
|
|
74
|
+
return "";
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
AVDictionary *codecOptions = nullptr;
|
|
78
|
+
size_t bitRate = fileOptions_->getBitRate();
|
|
79
|
+
int flacCompressionLevel = fileOptions_->getFlacCompressionLevel();
|
|
80
|
+
av_channel_layout_default(
|
|
81
|
+
&encoderCtx_->ch_layout, fileOptions_->getChannelCount());
|
|
82
|
+
|
|
83
|
+
encoderCtx_->sample_rate = fileOptions_->getSampleRate();
|
|
84
|
+
encoderCtx_->sample_fmt = fileOptions_->getSampleFormat();
|
|
85
|
+
|
|
86
|
+
if (bitRate > 0) {
|
|
87
|
+
encoderCtx_->bit_rate = bitRate;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
if (flacCompressionLevel >= 0) {
|
|
91
|
+
av_dict_set_int(
|
|
92
|
+
&codecOptions, "compression_level", flacCompressionLevel, 0);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
if (avcodec_open2(encoderCtx_.get(), codec, &codecOptions) < 0) {
|
|
96
|
+
__android_log_print(
|
|
97
|
+
ANDROID_LOG_ERROR,
|
|
98
|
+
"FFmpegFileWriter",
|
|
99
|
+
"Failed to open FFmpeg codec for file");
|
|
100
|
+
av_dict_free(&codecOptions);
|
|
101
|
+
return "";
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
av_dict_free(&codecOptions);
|
|
105
|
+
|
|
106
|
+
if (avcodec_parameters_from_context(stream_->codecpar, encoderCtx_.get()) <
|
|
107
|
+
0) {
|
|
108
|
+
__android_log_print(
|
|
109
|
+
ANDROID_LOG_ERROR,
|
|
110
|
+
"FFmpegFileWriter",
|
|
111
|
+
"Failed to copy codec parameters to stream for file");
|
|
112
|
+
return "";
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
if (!(formatCtx_->oformat->flags & AVFMT_NOFILE)) {
|
|
116
|
+
if (avio_open(&formatCtx_->pb, filePath_.c_str(), AVIO_FLAG_WRITE) < 0) {
|
|
117
|
+
__android_log_print(
|
|
118
|
+
ANDROID_LOG_ERROR,
|
|
119
|
+
"FFmpegFileWriter",
|
|
120
|
+
"Failed to open output file: %s",
|
|
121
|
+
filePath_.c_str());
|
|
122
|
+
return "";
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
stream_->time_base =
|
|
127
|
+
AVRational{1, static_cast<int>(encoderCtx_->sample_rate)};
|
|
128
|
+
|
|
129
|
+
if (avformat_write_header(formatCtx_.get(), nullptr) < 0) {
|
|
130
|
+
__android_log_print(
|
|
131
|
+
ANDROID_LOG_ERROR,
|
|
132
|
+
"FFmpegFileWriter",
|
|
133
|
+
"Failed to write header to file: %s",
|
|
134
|
+
filePath_.c_str());
|
|
135
|
+
return "";
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
frame_ = AVFramePtr(av_frame_alloc());
|
|
139
|
+
packet_ = AVPacketPtr(av_packet_alloc());
|
|
140
|
+
|
|
141
|
+
resampleCtx_ = SwrContextPtr(swr_alloc());
|
|
142
|
+
|
|
143
|
+
AVChannelLayout inChannelLayout;
|
|
144
|
+
av_channel_layout_default(&inChannelLayout, streamChannelCount_);
|
|
145
|
+
|
|
146
|
+
av_opt_set_chlayout(resampleCtx_.get(), "in_chlayout", &inChannelLayout, 0);
|
|
147
|
+
|
|
148
|
+
av_opt_set_chlayout(
|
|
149
|
+
resampleCtx_.get(), "out_chlayout", &encoderCtx_->ch_layout, 0);
|
|
150
|
+
|
|
151
|
+
av_opt_set_int(resampleCtx_.get(), "in_sample_rate", streamSampleRate, 0);
|
|
152
|
+
|
|
153
|
+
av_opt_set_int(
|
|
154
|
+
resampleCtx_.get(), "out_sample_rate", encoderCtx_->sample_rate, 0);
|
|
155
|
+
|
|
156
|
+
av_opt_set_sample_fmt(
|
|
157
|
+
resampleCtx_.get(), "in_sample_fmt", AV_SAMPLE_FMT_FLT, 0);
|
|
158
|
+
|
|
159
|
+
av_opt_set_sample_fmt(
|
|
160
|
+
resampleCtx_.get(), "out_sample_fmt", encoderCtx_->sample_fmt, 0);
|
|
161
|
+
|
|
162
|
+
if (swr_init(resampleCtx_.get()) < 0) {
|
|
163
|
+
__android_log_print(
|
|
164
|
+
ANDROID_LOG_ERROR,
|
|
165
|
+
"FFmpegFileWriter",
|
|
166
|
+
"Failed to initialize resampler for file: %s",
|
|
167
|
+
filePath_.c_str());
|
|
168
|
+
return "";
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
int contextFrameRatio = 2;
|
|
172
|
+
|
|
173
|
+
if (encoderCtx_->frame_size > 0) {
|
|
174
|
+
contextFrameRatio = static_cast<int>(std::ceil(
|
|
175
|
+
static_cast<double>(streamMaxBufferSize_) /
|
|
176
|
+
static_cast<double>(encoderCtx_->frame_size)));
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
int fifoSize = std::max(
|
|
180
|
+
encoderCtx_->frame_size > 0
|
|
181
|
+
? encoderCtx_->frame_size * contextFrameRatio * 2
|
|
182
|
+
: streamMaxBufferSize_ * 2,
|
|
183
|
+
4096);
|
|
184
|
+
|
|
185
|
+
audioFifo_ = AVAudioFifoPtr(av_audio_fifo_alloc(
|
|
186
|
+
encoderCtx_->sample_fmt, encoderCtx_->ch_layout.nb_channels, fifoSize));
|
|
187
|
+
|
|
188
|
+
__android_log_print(
|
|
189
|
+
ANDROID_LOG_INFO,
|
|
190
|
+
"FFmpegFileWriter",
|
|
191
|
+
"Using audio FIFO size of %d frames",
|
|
192
|
+
fifoSize);
|
|
193
|
+
|
|
194
|
+
isFileOpen_.store(true);
|
|
195
|
+
|
|
196
|
+
return filePath_;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
std::tuple<double, double> FFmpegAudioFileWriter::closeFile() {
|
|
200
|
+
if (!isFileOpen()) {
|
|
201
|
+
return {0.0, 0.0};
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
isFileOpen_.store(false);
|
|
205
|
+
|
|
206
|
+
const int frameSize =
|
|
207
|
+
encoderCtx_->frame_size > 0 ? encoderCtx_->frame_size : 512;
|
|
208
|
+
|
|
209
|
+
while (av_audio_fifo_size(audioFifo_.get()) > 0) {
|
|
210
|
+
const int chunkSize =
|
|
211
|
+
std::min(av_audio_fifo_size(audioFifo_.get()), frameSize);
|
|
212
|
+
|
|
213
|
+
frame_->nb_samples = chunkSize;
|
|
214
|
+
av_channel_layout_copy(&frame_->ch_layout, &encoderCtx_->ch_layout);
|
|
215
|
+
frame_->format = encoderCtx_->sample_fmt;
|
|
216
|
+
frame_->sample_rate = encoderCtx_->sample_rate;
|
|
217
|
+
|
|
218
|
+
if (av_frame_get_buffer(frame_.get(), 0) < 0) {
|
|
219
|
+
__android_log_print(
|
|
220
|
+
ANDROID_LOG_ERROR,
|
|
221
|
+
"FFmpegFileWriter",
|
|
222
|
+
"Failed to allocate audio frame buffer during flushing");
|
|
223
|
+
break;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
int fifoReadFrameCount =
|
|
227
|
+
av_audio_fifo_read(audioFifo_.get(), (void **)frame_->data, chunkSize);
|
|
228
|
+
|
|
229
|
+
if (fifoReadFrameCount != chunkSize) {
|
|
230
|
+
__android_log_print(
|
|
231
|
+
ANDROID_LOG_ERROR,
|
|
232
|
+
"FFmpegFileWriter",
|
|
233
|
+
"Failed to read audio samples from FIFO during flushing");
|
|
234
|
+
break;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
frame_->pts = nextPts_;
|
|
238
|
+
nextPts_ += chunkSize;
|
|
239
|
+
|
|
240
|
+
if (avcodec_send_frame(encoderCtx_.get(), frame_.get()) < 0) {
|
|
241
|
+
__android_log_print(
|
|
242
|
+
ANDROID_LOG_ERROR,
|
|
243
|
+
"FFmpegFileWriter",
|
|
244
|
+
"Failed to send audio frame to encoder during flushing");
|
|
245
|
+
av_frame_unref(frame_.get());
|
|
246
|
+
break;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
av_frame_unref(frame_.get());
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
avcodec_send_frame(encoderCtx_.get(), nullptr);
|
|
253
|
+
|
|
254
|
+
while (avcodec_receive_packet(encoderCtx_.get(), packet_.get()) == 0) {
|
|
255
|
+
av_packet_rescale_ts(
|
|
256
|
+
packet_.get(),
|
|
257
|
+
AVRational{1, encoderCtx_->sample_rate},
|
|
258
|
+
stream_->time_base);
|
|
259
|
+
packet_->stream_index = stream_->index;
|
|
260
|
+
av_interleaved_write_frame(formatCtx_.get(), packet_.get());
|
|
261
|
+
av_packet_unref(packet_.get());
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
if (av_write_trailer(formatCtx_.get()) < 0) {
|
|
265
|
+
__android_log_print(
|
|
266
|
+
ANDROID_LOG_ERROR,
|
|
267
|
+
"FFmpegFileWriter",
|
|
268
|
+
"Failed to write trailer to file: %s",
|
|
269
|
+
filePath_.c_str());
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
double fileSizeInMB = avio_size(formatCtx_->pb) / BYTES_TO_MB;
|
|
273
|
+
double durationInSeconds = getCurrentDuration();
|
|
274
|
+
|
|
275
|
+
if (formatCtx_ && formatCtx_->pb) {
|
|
276
|
+
avio_closep(&formatCtx_->pb);
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
resampleCtx_.reset();
|
|
280
|
+
frame_.reset();
|
|
281
|
+
packet_.reset();
|
|
282
|
+
encoderCtx_.reset();
|
|
283
|
+
formatCtx_.reset();
|
|
284
|
+
audioFifo_.reset();
|
|
285
|
+
|
|
286
|
+
filePath_ = "";
|
|
287
|
+
return {fileSizeInMB, durationInSeconds};
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
bool FFmpegAudioFileWriter::writeAudioData(void *data, int numFrames) {
|
|
291
|
+
if (!isFileOpen()) {
|
|
292
|
+
return false;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
int outputLength = av_rescale_rnd(
|
|
296
|
+
numFrames, encoderCtx_->sample_rate, streamSampleRate_, AV_ROUND_UP);
|
|
297
|
+
|
|
298
|
+
frame_->nb_samples = outputLength;
|
|
299
|
+
av_channel_layout_copy(&frame_->ch_layout, &encoderCtx_->ch_layout);
|
|
300
|
+
frame_->format = encoderCtx_->sample_fmt;
|
|
301
|
+
frame_->sample_rate = encoderCtx_->sample_rate;
|
|
302
|
+
|
|
303
|
+
if (av_frame_get_buffer(frame_.get(), 0) < 0) {
|
|
304
|
+
__android_log_print(
|
|
305
|
+
ANDROID_LOG_ERROR,
|
|
306
|
+
"FFmpegFileWriter",
|
|
307
|
+
"Failed to allocate audio frame buffer, outputLength: %d, format: %d, sample_rate: %d, channels: %d",
|
|
308
|
+
outputLength,
|
|
309
|
+
encoderCtx_->sample_fmt,
|
|
310
|
+
encoderCtx_->sample_rate,
|
|
311
|
+
encoderCtx_->ch_layout.nb_channels);
|
|
312
|
+
|
|
313
|
+
return false;
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
const uint8_t *inputData[1] = {reinterpret_cast<const uint8_t *>(data)};
|
|
317
|
+
|
|
318
|
+
int convertedSamples = swr_convert(
|
|
319
|
+
resampleCtx_.get(), frame_->data, outputLength, inputData, numFrames);
|
|
320
|
+
|
|
321
|
+
if (convertedSamples < 0) {
|
|
322
|
+
__android_log_print(
|
|
323
|
+
ANDROID_LOG_ERROR,
|
|
324
|
+
"FFmpegFileWriter",
|
|
325
|
+
"Failed to convert audio samples for file: %s",
|
|
326
|
+
filePath_.c_str());
|
|
327
|
+
av_frame_unref(frame_.get());
|
|
328
|
+
return false;
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
int fifoWrittenFrameCount = av_audio_fifo_write(
|
|
332
|
+
audioFifo_.get(), (void **)frame_->data, convertedSamples);
|
|
333
|
+
|
|
334
|
+
if (fifoWrittenFrameCount < convertedSamples) {
|
|
335
|
+
__android_log_print(
|
|
336
|
+
ANDROID_LOG_ERROR,
|
|
337
|
+
"FFmpegFileWriter",
|
|
338
|
+
"Failed to write audio samples to FIFO");
|
|
339
|
+
av_frame_unref(frame_.get());
|
|
340
|
+
return false;
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
av_frame_unref(frame_.get());
|
|
344
|
+
const int frameSize =
|
|
345
|
+
encoderCtx_->frame_size > 0 ? encoderCtx_->frame_size : 512;
|
|
346
|
+
|
|
347
|
+
while (av_audio_fifo_size(audioFifo_.get()) >= frameSize) {
|
|
348
|
+
frame_->nb_samples = frameSize;
|
|
349
|
+
av_channel_layout_copy(&frame_->ch_layout, &encoderCtx_->ch_layout);
|
|
350
|
+
frame_->format = encoderCtx_->sample_fmt;
|
|
351
|
+
frame_->sample_rate = encoderCtx_->sample_rate;
|
|
352
|
+
|
|
353
|
+
if (av_frame_get_buffer(frame_.get(), 0) < 0) {
|
|
354
|
+
__android_log_print(
|
|
355
|
+
ANDROID_LOG_ERROR,
|
|
356
|
+
"FFmpegFileWriter",
|
|
357
|
+
"Failed to allocate audio frame buffer");
|
|
358
|
+
return false;
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
int fifoReadFrameCount =
|
|
362
|
+
av_audio_fifo_read(audioFifo_.get(), (void **)frame_->data, frameSize);
|
|
363
|
+
|
|
364
|
+
if (fifoReadFrameCount != frameSize) {
|
|
365
|
+
__android_log_print(
|
|
366
|
+
ANDROID_LOG_ERROR,
|
|
367
|
+
"FFmpegFileWriter",
|
|
368
|
+
"Failed to read audio samples from FIFO");
|
|
369
|
+
return false;
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
frame_->pts = nextPts_;
|
|
373
|
+
nextPts_ += frameSize;
|
|
374
|
+
|
|
375
|
+
if (avcodec_send_frame(encoderCtx_.get(), frame_.get()) < 0) {
|
|
376
|
+
__android_log_print(
|
|
377
|
+
ANDROID_LOG_ERROR,
|
|
378
|
+
"FFmpegFileWriter",
|
|
379
|
+
"Failed to send audio frame to encoder");
|
|
380
|
+
av_frame_unref(frame_.get());
|
|
381
|
+
return false;
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
while (avcodec_receive_packet(encoderCtx_.get(), packet_.get()) == 0) {
|
|
385
|
+
av_packet_rescale_ts(
|
|
386
|
+
packet_.get(),
|
|
387
|
+
AVRational{1, encoderCtx_->sample_rate},
|
|
388
|
+
stream_->time_base);
|
|
389
|
+
|
|
390
|
+
packet_->stream_index = stream_->index;
|
|
391
|
+
|
|
392
|
+
if (av_interleaved_write_frame(formatCtx_.get(), packet_.get()) < 0) {
|
|
393
|
+
__android_log_print(
|
|
394
|
+
ANDROID_LOG_ERROR,
|
|
395
|
+
"FFmpegFileWriter",
|
|
396
|
+
"Failed to write audio packet to file");
|
|
397
|
+
|
|
398
|
+
av_packet_unref(packet_.get());
|
|
399
|
+
av_frame_unref(frame_.get());
|
|
400
|
+
return false;
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
av_packet_unref(packet_.get());
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
av_frame_unref(frame_.get());
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
framesWritten_.fetch_add(numFrames);
|
|
410
|
+
return true;
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
bool FFmpegAudioFileWriter::initializeConverterIfNeeded() {
|
|
414
|
+
return false;
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
bool FFmpegAudioFileWriter::initializeEncoder() {
|
|
418
|
+
return false;
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
bool FFmpegAudioFileWriter::isFileOpen() {
|
|
422
|
+
return isFileOpen_.load();
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
bool FFmpegAudioFileWriter::isConverterRequired() {
|
|
426
|
+
return isConverterRequired_.load();
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
} // namespace audioapi
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
extern "C" {
|
|
4
|
+
#include <libavutil/opt.h>
|
|
5
|
+
#include <libavcodec/avcodec.h>
|
|
6
|
+
#include <libavutil/audio_fifo.h>
|
|
7
|
+
#include <libavformat/avformat.h>
|
|
8
|
+
#include <libswresample/swresample.h>
|
|
9
|
+
#include <libavutil/channel_layout.h>
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
#include <audioapi/android/core/utils/AndroidFileWriterBackend.h>
|
|
13
|
+
|
|
14
|
+
#include <string>
|
|
15
|
+
#include <memory>
|
|
16
|
+
#include <tuple>
|
|
17
|
+
|
|
18
|
+
namespace audioapi {
|
|
19
|
+
|
|
20
|
+
class FFmpegAudioFileOptions;
|
|
21
|
+
|
|
22
|
+
struct AVCodecContextDTOR {
|
|
23
|
+
void operator()(AVCodecContext* ctx) const {
|
|
24
|
+
if (ctx) {
|
|
25
|
+
avcodec_free_context(&ctx);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
struct AVFormatContextDTOR {
|
|
31
|
+
void operator()(AVFormatContext* ctx) const {
|
|
32
|
+
if (ctx) {
|
|
33
|
+
avformat_free_context(ctx);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
struct AVFrameDTOR {
|
|
39
|
+
void operator()(AVFrame* frame) const {
|
|
40
|
+
if (frame) {
|
|
41
|
+
av_frame_free(&frame);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
struct AVPacketDTOR {
|
|
47
|
+
void operator()(AVPacket* packet) const {
|
|
48
|
+
if (packet) {
|
|
49
|
+
av_packet_free(&packet);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
struct SwrContextDTOR {
|
|
55
|
+
void operator()(SwrContext* ctx) const {
|
|
56
|
+
if (ctx) {
|
|
57
|
+
swr_free(&ctx);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
struct AVAudioFifoDTOR {
|
|
63
|
+
void operator()(AVAudioFifo* fifo) const {
|
|
64
|
+
if (fifo) {
|
|
65
|
+
av_audio_fifo_free(fifo);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
using AVCodecContextPtr = std::unique_ptr<AVCodecContext, AVCodecContextDTOR>;
|
|
71
|
+
using AVFormatContextPtr = std::unique_ptr<AVFormatContext, AVFormatContextDTOR>;
|
|
72
|
+
using AVFramePtr = std::unique_ptr<AVFrame, AVFrameDTOR>;
|
|
73
|
+
using AVPacketPtr = std::unique_ptr<AVPacket, AVPacketDTOR>;
|
|
74
|
+
using SwrContextPtr = std::unique_ptr<SwrContext, SwrContextDTOR>;
|
|
75
|
+
using AVAudioFifoPtr = std::unique_ptr<AVAudioFifo, AVAudioFifoDTOR>;
|
|
76
|
+
|
|
77
|
+
class FFmpegAudioFileWriter : public AndroidFileWriterBackend {
|
|
78
|
+
public:
|
|
79
|
+
FFmpegAudioFileWriter(
|
|
80
|
+
float sampleRate,
|
|
81
|
+
size_t channelCount,
|
|
82
|
+
size_t bitRate,
|
|
83
|
+
size_t androidFlags);
|
|
84
|
+
~FFmpegAudioFileWriter() override;
|
|
85
|
+
|
|
86
|
+
std::string openFile(int32_t streamSampleRate, int32_t streamChannelCount, int32_t streamMaxBufferSize) override;
|
|
87
|
+
std::tuple<double, double> closeFile() override;
|
|
88
|
+
|
|
89
|
+
bool writeAudioData(void *data, int numFrames) override;
|
|
90
|
+
|
|
91
|
+
private:
|
|
92
|
+
std::shared_ptr<FFmpegAudioFileOptions> fileOptions_;
|
|
93
|
+
|
|
94
|
+
std::atomic<bool> isFileOpen_{false};
|
|
95
|
+
std::atomic<bool> isConverterRequired_{false};
|
|
96
|
+
|
|
97
|
+
AVCodecContextPtr encoderCtx_{nullptr};
|
|
98
|
+
AVFormatContextPtr formatCtx_{nullptr};
|
|
99
|
+
SwrContextPtr resampleCtx_{nullptr};
|
|
100
|
+
AVPacketPtr packet_{nullptr};
|
|
101
|
+
AVFramePtr frame_{nullptr};
|
|
102
|
+
AVStream* stream_{nullptr};
|
|
103
|
+
AVAudioFifoPtr audioFifo_{nullptr};
|
|
104
|
+
int64_t nextPts_;
|
|
105
|
+
|
|
106
|
+
bool initializeConverterIfNeeded();
|
|
107
|
+
bool initializeEncoder();
|
|
108
|
+
|
|
109
|
+
bool isFileOpen();
|
|
110
|
+
bool isConverterRequired();
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
} // namespace audioapi
|
package/android/src/main/cpp/audioapi/android/core/utils/miniaudioBackend/MiniAudioFileOptions.cpp
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
|
|
2
|
+
#include <audioapi/android/core/utils/FileUtils.h>
|
|
3
|
+
#include <audioapi/android/core/utils/miniaudioBackend/MiniAudioFileOptions.h>
|
|
4
|
+
|
|
5
|
+
namespace audioapi {
|
|
6
|
+
|
|
7
|
+
MiniAudioFileOptions::MiniAudioFileOptions(
|
|
8
|
+
float sampleRate,
|
|
9
|
+
size_t channelCount,
|
|
10
|
+
size_t bitRate,
|
|
11
|
+
size_t flags)
|
|
12
|
+
: sampleRate_(sampleRate), channelCount_(channelCount), flags_(flags) {};
|
|
13
|
+
|
|
14
|
+
ma_encoding_format MiniAudioFileOptions::getFormat() const {
|
|
15
|
+
return ma_encoding_format_wav;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
std::string MiniAudioFileOptions::getFilePath(
|
|
19
|
+
const std::string &baseFileName) const {
|
|
20
|
+
return android::fileutils::getFilePath(
|
|
21
|
+
android::fileutils::directoryFromFlags(flags_), baseFileName, "wav");
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
float MiniAudioFileOptions::getSampleRate() const {
|
|
25
|
+
return sampleRate_;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
size_t MiniAudioFileOptions::getChannelCount() const {
|
|
29
|
+
return channelCount_;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
ma_format MiniAudioFileOptions::getDataFormat() const {
|
|
33
|
+
android::fileutils::BitDepth bitDepth =
|
|
34
|
+
android::fileutils::bitDepthFromFlags(flags_);
|
|
35
|
+
switch (bitDepth) {
|
|
36
|
+
case android::fileutils::BitDepth::BIT_16:
|
|
37
|
+
return ma_format_s16;
|
|
38
|
+
case android::fileutils::BitDepth::BIT_24:
|
|
39
|
+
return ma_format_s24;
|
|
40
|
+
case android::fileutils::BitDepth::BIT_32:
|
|
41
|
+
return ma_format_f32;
|
|
42
|
+
default:
|
|
43
|
+
return ma_format_f32;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
} // namespace audioapi
|
package/android/src/main/cpp/audioapi/android/core/utils/miniaudioBackend/MiniAudioFileOptions.h
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#include <audioapi/libs/miniaudio/miniaudio.h>
|
|
4
|
+
#include <cstddef>
|
|
5
|
+
#include <cstdint>
|
|
6
|
+
#include <string>
|
|
7
|
+
|
|
8
|
+
namespace audioapi {
|
|
9
|
+
|
|
10
|
+
class MiniAudioFileOptions {
|
|
11
|
+
public:
|
|
12
|
+
MiniAudioFileOptions(float sampleRate, size_t channelCount, size_t bitRate, size_t flags);
|
|
13
|
+
~MiniAudioFileOptions() = default;
|
|
14
|
+
|
|
15
|
+
ma_encoding_format getFormat() const;
|
|
16
|
+
std::string getFilePath(const std::string &baseFileName) const;
|
|
17
|
+
|
|
18
|
+
float getSampleRate() const;
|
|
19
|
+
size_t getChannelCount() const;
|
|
20
|
+
ma_format getDataFormat() const;
|
|
21
|
+
|
|
22
|
+
private:
|
|
23
|
+
float sampleRate_;
|
|
24
|
+
size_t channelCount_;
|
|
25
|
+
size_t flags_;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
} // namespace audioapi
|