Skip to content

Commit

Permalink
Create .clang-format and reformat sources (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
vertexodessa authored Dec 7, 2024
1 parent 9da0e39 commit 0572ae3
Show file tree
Hide file tree
Showing 30 changed files with 4,946 additions and 4,152 deletions.
45 changes: 45 additions & 0 deletions app/videonative/src/main/cpp/.clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
BasedOnStyle: Google
IndentWidth: 4
TabWidth: 4
UseTab: Never
AlignAfterOpenBracket: AlwaysBreak
BreakBeforeBraces: Custom
BraceWrapping:
BeforeElse: true
AfterFunction: true
AfterEnum: true
AfterControlStatement: Always
AfterClass: true
AfterCaseLabel: true
AfterNamespace: true
AfterObjCDeclaration: true
AfterStruct: true
AfterUnion: true
AfterExternBlock: true
BeforeCatch: true
BeforeLambdaBody: true
BeforeWhile: true
IndentBraces: false
SplitEmptyFunction: true
SplitEmptyRecord: false
SplitEmptyNamespace: true
AllowShortFunctionsOnASingleLine: Inline
ColumnLimit: 120
NamespaceIndentation: None
AccessModifierOffset: -2
AlignConsecutiveAssignments: true
AlignConsecutiveDeclarations: true
AlignOperands: true
AlignTrailingComments: true
BinPackArguments: false
BinPackParameters: false
PointerAlignment: Left
DerivePointerAlignment: false
SpaceBeforeParens: ControlStatements
SpaceAfterCStyleCast: true
SpacesInAngles: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
SortIncludes: true
IncludeBlocks: Preserve
IndentCaseLabels: true
35 changes: 20 additions & 15 deletions app/videonative/src/main/cpp/AndroidThreadPrioValues.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,18 @@
// The values for android thread priorities and
// the values used by my FPV_VR app (2 different namespaces though)

namespace AndroidThreadPriorityValues {
//This one was taken from https://android.googlesource.com/platform/system/core/+/jb-dev/include/system/graphics.h
constexpr auto HAL_PRIORITY_URGENT_DISPLAY = -8;
// values taken from https://android.googlesource.com/platform/frameworks/native/+/android-4.2.2_r1/include/utils/ThreadDefs.h
namespace AndroidThreadPriorityValues
{
// This one was taken from https://android.googlesource.com/platform/system/core/+/jb-dev/include/system/graphics.h
constexpr auto HAL_PRIORITY_URGENT_DISPLAY = -8;
// values taken from
// https://android.googlesource.com/platform/frameworks/native/+/android-4.2.2_r1/include/utils/ThreadDefs.h
#ifdef __cplusplus
extern "C" {
extern "C"
{
#endif
enum {
enum
{
/*
* ***********************************************
* ** Keep in sync with android.os.Process.java **
Expand Down Expand Up @@ -52,27 +56,28 @@ namespace AndroidThreadPriorityValues {
ANDROID_PRIORITY_URGENT_AUDIO = -19,
/* should never be used in practice. regular process might not
* be allowed to use this level */
ANDROID_PRIORITY_HIGHEST = -20,
ANDROID_PRIORITY_DEFAULT = ANDROID_PRIORITY_NORMAL,
ANDROID_PRIORITY_HIGHEST = -20,
ANDROID_PRIORITY_DEFAULT = ANDROID_PRIORITY_NORMAL,
ANDROID_PRIORITY_MORE_FAVORABLE = -1,
ANDROID_PRIORITY_LESS_FAVORABLE = +1,
};
#ifdef __cplusplus
} // extern "C"
} // extern "C"
#endif
}
} // namespace AndroidThreadPriorityValues

// All these values are for FPVue_Android
//namespace FPV_VR_PRIORITY{
// namespace FPV_VR_PRIORITY{
// constexpr int CPU_PRIORITY_GLRENDERER_STEREO_FB=-19; //This one needs a whole CPU core all the time anyways
// constexpr int CPU_PRIORITY_GLRENDERER_STEREO=-16; //The GL thread also should get 1 whole cpu core
// constexpr int CPU_PRIORITY_UDPRECEIVER_VIDEO=-16; //needs low latency and does not use the cpu that much
// constexpr int CPU_PRIORITY_DECODER_OUTPUT=-16; //needs low latency and does not use the cpu that much
// constexpr int CPU_PRIORITY_UVC_FRAME_CALLBACK=-17; //needs low latency but uses CPU a lot (decoding). More prio than GLRenderer
// constexpr int CPU_PRIORITY_UVC_FRAME_CALLBACK=-17; //needs low latency but uses CPU a lot (decoding). More prio
// than GLRenderer
// // These are much lower
// constexpr int CPU_PRIORITY_GLRENDERER_MONO=-4; //only shows the OSD not video
// constexpr int CPU_PRIORITY_UDPRECEIVER_TELEMETRY=-4; //not as important as video but also needs almost no CPU processing time
// constexpr int CPU_PRIORITY_UDPSENDER_HEADTRACKING=-4;
// constexpr int CPU_PRIORITY_UDPRECEIVER_TELEMETRY=-4; //not as important as video but also needs almost no CPU
// processing time constexpr int CPU_PRIORITY_UDPSENDER_HEADTRACKING=-4;
//}

#endif //FPVUE_ANDROIDTHREADPRIOVALUES_HPP
#endif // FPVUE_ANDROIDTHREADPRIOVALUES_HPP
48 changes: 28 additions & 20 deletions app/videonative/src/main/cpp/AudioDecoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,20 @@
#define SAMPLE_RATE 8000
#define CHANNELS 1

AudioDecoder::AudioDecoder() {
AudioDecoder::AudioDecoder()
{
initAudio();
}

AudioDecoder::~AudioDecoder() {

AudioDecoder::~AudioDecoder()
{
stopAudioProcessing();
delete pOpusDecoder;
AAudioStream_requestStop(m_stream);
AAudioStream_close(m_stream);
}

void AudioDecoder::enqueueAudio(const uint8_t *data, const std::size_t data_length)
void AudioDecoder::enqueueAudio(const uint8_t* data, const std::size_t data_length)
{
{
std::lock_guard<std::mutex> lock(m_mtxQueue);
Expand All @@ -31,15 +32,19 @@ void AudioDecoder::enqueueAudio(const uint8_t *data, const std::size_t data_leng
m_cvQueue.notify_one();
}

void AudioDecoder::processAudioQueue() {
while (true) {
void AudioDecoder::processAudioQueue()
{
while (true)
{
std::unique_lock<std::mutex> lock(m_mtxQueue);
m_cvQueue.wait(lock, [this] { return !m_audioQueue.empty() || stopAudioFlag; });

if (stopAudioFlag) {
if (stopAudioFlag)
{
break;
}
if (!m_audioQueue.empty()) {
if (!m_audioQueue.empty())
{
AudioUDPPacket audioPkt = m_audioQueue.front();
onNewAudioData(audioPkt.data, audioPkt.len);
m_audioQueue.pop();
Expand All @@ -48,7 +53,8 @@ void AudioDecoder::processAudioQueue() {
}
}

void AudioDecoder::initAudio() {
void AudioDecoder::initAudio()
{
__android_log_print(ANDROID_LOG_DEBUG, TAG, "initAudio");
int error;
pOpusDecoder = opus_decoder_create(SAMPLE_RATE, CHANNELS, &error);
Expand All @@ -57,8 +63,8 @@ void AudioDecoder::initAudio() {

// Set the stream format
AAudioStreamBuilder_setFormat(m_builder, AAUDIO_FORMAT_PCM_I16);
AAudioStreamBuilder_setChannelCount(m_builder, CHANNELS); // Mono
AAudioStreamBuilder_setSampleRate(m_builder, SAMPLE_RATE); // 8000 Hz
AAudioStreamBuilder_setChannelCount(m_builder, CHANNELS); // Mono
AAudioStreamBuilder_setSampleRate(m_builder, SAMPLE_RATE); // 8000 Hz

AAudioStreamBuilder_setBufferCapacityInFrames(m_builder, BUFFER_CAPACITY_IN_FRAMES);

Expand All @@ -80,22 +86,24 @@ void AudioDecoder::stopAudio()
isInit = false;
}

void AudioDecoder::onNewAudioData(const uint8_t *data, const std::size_t data_length) {
const int rtp_header_size = 12;
const uint8_t* opus_payload = data + rtp_header_size;
int opus_payload_size = data_length - rtp_header_size;
void AudioDecoder::onNewAudioData(const uint8_t* data, const std::size_t data_length)
{
const int rtp_header_size = 12;
const uint8_t* opus_payload = data + rtp_header_size;
int opus_payload_size = data_length - rtp_header_size;

int frame_size = opus_packet_get_samples_per_frame(opus_payload, SAMPLE_RATE);
int nb_frames = opus_packet_get_nb_frames(opus_payload, opus_payload_size);
int nb_frames = opus_packet_get_nb_frames(opus_payload, opus_payload_size);

// Decode the frame
int pcm_size = frame_size * nb_frames * CHANNELS;
if(pOpusDecoder && m_stream) {
if (pOpusDecoder && m_stream)
{
opus_int16 pcm[pcm_size];
int decoded_samples = opus_decode(pOpusDecoder, opus_payload, opus_payload_size, pcm,
pcm_size, 0);
int decoded_samples = opus_decode(pOpusDecoder, opus_payload, opus_payload_size, pcm, pcm_size, 0);

if (decoded_samples < 0) {
if (decoded_samples < 0)
{
return;
}
// Process the decoded PCM data
Expand Down
51 changes: 28 additions & 23 deletions app/videonative/src/main/cpp/AudioDecoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,63 +4,68 @@

#ifndef PIXELPILOT_AUDIODECODER_H
#define PIXELPILOT_AUDIODECODER_H
#include "libs/include/opus.h"
#include <aaudio/AAudio.h>
#include <condition_variable>
#include <memory>
#include <queue>
#include <mutex>
#include <condition_variable>
#include <queue>
#include <thread>
#include "libs/include/opus.h"

typedef struct _AudioUDPPacket{
typedef struct _AudioUDPPacket
{
_AudioUDPPacket(const uint8_t* _data, size_t _len)
{
memcpy(data, _data, _len);
len = _len;
};
uint8_t data[250];
size_t len;
size_t len;
} AudioUDPPacket;

class AudioDecoder {
public:
class AudioDecoder
{
public:
AudioDecoder();
~AudioDecoder();

// Audio buffer
void initAudio();
void enqueueAudio(const uint8_t *data, const std::size_t data_length);
void startAudioProcessing() {
void enqueueAudio(const uint8_t* data, const std::size_t data_length);
void startAudioProcessing()
{
stopAudioFlag = false;
m_audioThread = std::thread(&AudioDecoder::processAudioQueue, this);
}

void stopAudioProcessing() {
void stopAudioProcessing()
{
{
std::lock_guard<std::mutex> lock(m_mtxQueue);
stopAudioFlag = true;
}
m_cvQueue.notify_all();
if (m_audioThread.joinable()) {
if (m_audioThread.joinable())
{
m_audioThread.join();
}
}
void processAudioQueue();
void stopAudio();
bool isInit = false;

private:
void onNewAudioData(const uint8_t *data, const std::size_t data_length);
private:
void onNewAudioData(const uint8_t* data, const std::size_t data_length);

private:
const int BUFFER_CAPACITY_IN_FRAMES = (1024 + 256);
private:
const int BUFFER_CAPACITY_IN_FRAMES = (1024 + 256);
std::queue<AudioUDPPacket> m_audioQueue;
std::mutex m_mtxQueue;
std::condition_variable m_cvQueue;
bool stopAudioFlag = false;
std::thread m_audioThread;
AAudioStreamBuilder* m_builder = nullptr;
AAudioStream* m_stream = nullptr;
OpusDecoder *pOpusDecoder = nullptr;
std::mutex m_mtxQueue;
std::condition_variable m_cvQueue;
bool stopAudioFlag = false;
std::thread m_audioThread;
AAudioStreamBuilder* m_builder = nullptr;
AAudioStream* m_stream = nullptr;
OpusDecoder* pOpusDecoder = nullptr;
};
#endif //PIXELPILOT_AUDIODECODER_H
#endif // PIXELPILOT_AUDIODECODER_H
Loading

0 comments on commit 0572ae3

Please sign in to comment.