Skip to content

Commit 50d3e50

Browse files
committed
Provide refactoring
1 parent ca2890b commit 50d3e50

28 files changed

+592
-566
lines changed

compose/build.gradle.kts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,6 @@ dependencies {
2323
testRuntimeOnly("org.junit.platform:junit-platform-launcher:1.10.0")
2424
}
2525

26-
tasks.withType<JavaCompile> {
27-
options.release.set(JavaVersion.VERSION_15.ordinal)
28-
}
29-
3026
tasks.withType<KotlinCompile> {
3127
kotlinOptions.jvmTarget = "${JavaVersion.VERSION_15}"
3228
}

core/build.gradle.kts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,8 @@ dependencies {
2020
testRuntimeOnly("org.junit.platform:junit-platform-launcher:1.10.0")
2121
}
2222

23-
tasks.withType<JavaCompile> {
24-
options.release.set(JavaVersion.VERSION_11.ordinal)
25-
}
26-
2723
tasks.withType<KotlinCompile> {
28-
kotlinOptions.jvmTarget = "${JavaVersion.VERSION_11}"
24+
kotlinOptions.jvmTarget = "${JavaVersion.VERSION_15}"
2925
}
3026

3127
tasks.test {

core/src/main/cpp/.gitignore

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Compiled Object files, Static and Dynamic libs (Shared Objects)
1+
# Compiled Object media, Static and Dynamic libs (Shared Objects)
22
*.o
33
*.a
44
*.so
@@ -13,14 +13,14 @@
1313
cmake-build-debug/
1414
cmake-build-release/
1515

16-
# CLion project files
16+
# CLion project media
1717
.idea/
1818

1919
# Compiled output
2020
bin/
2121
lib/
2222

23-
# Ignore any local user-specific files
23+
# Ignore any local user-specific media
2424
.vscode/
2525
*.suo
2626
*.user

core/src/main/cpp/CMakeLists.txt

Lines changed: 53 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
cmake_minimum_required(VERSION 3.24)
22
project(klarity VERSION 1.0.0)
33

4-
set(CMAKE_CXX_STANDARD 23)
4+
set(CMAKE_CXX_STANDARD 20)
5+
6+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
57

68
set(CMAKE_SHARED_LIBRARY_PREFIX "")
79

810
add_library(klarity SHARED
911
src/common.cpp
1012
src/decoder/decoder.cpp
11-
src/decoder/com_github_numq_klarity_core_decoder_NativeDecoder.cpp
1213
src/sampler/sampler.cpp
14+
src/decoder/com_github_numq_klarity_core_decoder_NativeDecoder.cpp
1315
src/sampler/com_github_numq_klarity_core_sampler_NativeSampler.cpp
1416
)
1517

@@ -33,13 +35,57 @@ target_include_directories(klarity PRIVATE
3335
include/sampler/stretch
3436
)
3537

36-
target_link_directories(klarity PRIVATE bin/ffmpeg bin/portaudio)
38+
target_link_directories(klarity PRIVATE
39+
bin/ffmpeg
40+
bin/portaudio
41+
)
3742

3843
target_link_libraries(klarity PRIVATE
39-
avutil-59
40-
swscale-8
41-
swresample-5
4244
avcodec-61
4345
avformat-61
46+
avutil-59
47+
swresample-5
48+
swscale-8
4449
libportaudio
45-
)
50+
)
51+
52+
#find_package(JNI)
53+
#
54+
#add_library(klarity-decoder SHARED
55+
# src/decoder/decoder.cpp
56+
# src/decoder/com_github_numq_klarity_core_decoder_NativeDecoder.cpp
57+
#)
58+
#
59+
#add_library(klarity-sampler SHARED
60+
# src/sampler/sampler.cpp
61+
# src/sampler/com_github_numq_klarity_core_sampler_NativeSampler.cpp
62+
#)
63+
#
64+
#if (JNI_FOUND)
65+
# message(STATUS "JNI_INCLUDE_DIRS=${JNI_INCLUDE_DIRS}")
66+
# message(STATUS "JNI_LIBRARIES=${JNI_LIBRARIES}")
67+
# target_include_directories(klarity-decoder PRIVATE ${JNI_INCLUDE_DIRS})
68+
# target_include_directories(klarity-sampler PRIVATE ${JNI_INCLUDE_DIRS})
69+
#else ()
70+
# message(FATAL_ERROR "JNI not found.")
71+
#endif ()
72+
#
73+
#target_include_directories(klarity-decoder PRIVATE
74+
# include/decoder
75+
# include/decoder/ffmpeg
76+
#)
77+
#
78+
#target_include_directories(klarity-sampler PRIVATE
79+
# include/sampler
80+
# include/sampler/dsp
81+
# include/sampler/portaudio
82+
# include/sampler/stretch
83+
#)
84+
#
85+
#target_link_directories(klarity-decoder PRIVATE bin/ffmpeg)
86+
#
87+
#target_link_directories(klarity-sampler PRIVATE bin/portaudio)
88+
#
89+
#target_link_libraries(klarity-decoder PRIVATE avcodec-61 avformat-61 avutil-59 swresample-5 swscale-8)
90+
#
91+
#target_link_libraries(klarity-sampler PRIVATE libportaudio)

core/src/main/cpp/include/decoder/com_github_numq_klarity_core_decoder_NativeDecoder.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
#include <jni.h>
2-
#include <memory>
32
#include <shared_mutex>
4-
#include <unordered_map>
53
#include "common.h"
64
#include "decoder.h"
7-
#include "decoder-deleter.h"
85

96
#ifndef _Included_com_github_numq_klarity_core_decoder_NativeDecoder
107
#define _Included_com_github_numq_klarity_core_decoder_NativeDecoder

core/src/main/cpp/include/decoder/decoder-deleter.h

Lines changed: 0 additions & 60 deletions
This file was deleted.

core/src/main/cpp/include/decoder/decoder.h

Lines changed: 63 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
#include <iostream>
66
#include <memory>
77
#include <mutex>
8+
#include <optional>
89
#include "frame.h"
910
#include "format.h"
1011
#include "exception.h"
11-
#include "decoder-deleter.h"
1212

1313
extern "C" {
1414
#include "libavutil/imgutils.h"
@@ -20,36 +20,86 @@ extern "C" {
2020

2121
struct Decoder {
2222
private:
23+
class AVPacketGuard {
24+
public:
25+
AVPacketGuard() {
26+
packet = av_packet_alloc();
27+
}
28+
29+
~AVPacketGuard() {
30+
if (packet) {
31+
av_packet_free(&packet);
32+
}
33+
}
34+
35+
AVPacket *get() { return packet; }
36+
37+
private:
38+
AVPacket *packet = nullptr;
39+
};
40+
41+
class AVFrameGuard {
42+
public:
43+
AVFrameGuard() {
44+
frame = av_frame_alloc();
45+
}
46+
47+
~AVFrameGuard() {
48+
if (frame) {
49+
av_frame_free(&frame);
50+
}
51+
}
52+
53+
AVFrame *get() { return frame; }
54+
55+
private:
56+
AVFrame *frame = nullptr;
57+
};
58+
59+
const AVSampleFormat sampleFormat = AV_SAMPLE_FMT_FLT;
60+
61+
const AVPixelFormat pixelFormat = AV_PIX_FMT_RGBA;
62+
2363
std::mutex mutex;
2464

25-
std::unique_ptr<AVFormatContext, av_format_context_deleter> formatContext;
65+
std::vector<uint8_t> audioBuffer;
66+
67+
std::vector<uint8_t> videoBuffer;
68+
69+
AVFormatContext *formatContext = nullptr;
2670

27-
std::unique_ptr<AVCodecContext, av_codec_context_deleter> audioCodecContext;
71+
AVCodecContext *audioCodecContext = nullptr;
2872

29-
std::unique_ptr<AVCodecContext, av_codec_context_deleter> videoCodecContext;
73+
AVCodecContext *videoCodecContext = nullptr;
3074

31-
std::unique_ptr<AVStream> audioStream;
75+
SwrContext *swrContext = nullptr;
3276

33-
std::unique_ptr<AVStream> videoStream;
77+
SwsContext *swsContext = nullptr;
3478

35-
std::unique_ptr<SwsContext, sws_context_deleter> swsContext;
79+
int audioStreamIndex = -1;
3680

37-
std::unique_ptr<SwrContext, swr_context_deleter> swrContext;
81+
int videoStreamIndex = -1;
3882

39-
std::vector<uint8_t> _processAudioFrame(const AVFrame &src);
83+
AVCodecContext *_initCodecContext(unsigned int streamIndex);
4084

41-
std::vector<uint8_t> _processVideoFrame(const AVFrame &src, int64_t width, int64_t height);
85+
std::vector<uint8_t> &_processAudioFrame(const AVFrame &src);
86+
87+
std::vector<uint8_t> &_processVideoFrame(const AVFrame &src, int64_t width, int64_t height);
88+
89+
void _cleanUp();
4290

4391
public:
44-
std::unique_ptr<Format> format{nullptr};
92+
Format format;
4593

4694
explicit Decoder(const std::string &location, bool findAudioStream, bool findVideoStream);
4795

48-
std::unique_ptr<Frame> nextFrame(int64_t width, int64_t height);
96+
~Decoder();
97+
98+
std::optional<Frame> nextFrame(int64_t width, int64_t height);
4999

50100
void seekTo(long timestampMicros, bool keyframesOnly);
51101

52102
void reset();
53103
};
54104

55-
#endif //KLARITY_DECODER_DECODER_H
105+
#endif // KLARITY_DECODER_DECODER_H

core/src/main/cpp/include/sampler/com_github_numq_klarity_core_sampler_NativeSampler.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
#include <jni.h>
2-
#include <iostream>
32
#include <shared_mutex>
4-
#include <mutex>
53
#include "common.h"
64
#include "sampler.h"
75

core/src/main/cpp/include/sampler/sampler-deleter.h

Lines changed: 0 additions & 23 deletions
This file was deleted.

core/src/main/cpp/include/sampler/sampler.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
#include "exception.h"
99
#include "portaudio.h"
1010
#include "stretch/stretch.h"
11-
#include "sampler-deleter.h"
1211

1312
struct Sampler {
1413
private:
@@ -18,17 +17,21 @@ struct Sampler {
1817

1918
uint32_t channels;
2019

21-
std::unique_ptr<PaStream, pa_stream_deleter> stream;
20+
PaStream *stream = nullptr;
2221

23-
std::unique_ptr<signalsmith::stretch::SignalsmithStretch<float>, signalsmith_stretch_deleter> stretch;
22+
signalsmith::stretch::SignalsmithStretch<float> *stretch = nullptr;
2423

2524
float playbackSpeedFactor = 1.0f;
2625

2726
float volume = 1.0f;
2827

28+
void _cleanUp();
29+
2930
public:
3031
explicit Sampler(uint32_t sampleRate, uint32_t channels);
3132

33+
~Sampler();
34+
3235
void setPlaybackSpeed(float factor);
3336

3437
void setVolume(float value);

0 commit comments

Comments
 (0)