Skip to content

Commit

Permalink
clang-format
Browse files Browse the repository at this point in the history
  • Loading branch information
pseregiet committed Aug 2, 2020
1 parent 03e6c81 commit 0cf51ec
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 33 deletions.
14 changes: 7 additions & 7 deletions src/audio_input/audio.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,17 @@
#include "audio.h"
#include "../util/log.h"
#include "../util/memory.h"
#define SAMPLES_PER_CALLBACK 4096 //has to be power of 2
#define SAMPLES_PER_CALLBACK 4096 // has to be power of 2

static void audioCallback(void *userdata, uint8_t *stream, int len) {
RSAudio* audio = (RSAudio*)userdata;
RSAudio *audio = (RSAudio *)userdata;
pthread_spin_lock(&audio->sampleGetLock);
rsCircleStaticAdd(&audio->data, stream, len);
pthread_spin_unlock(&audio->sampleGetLock);
}

static const char *getDeviceName(const RSConfig *config) {
const char* devname = config->audioDeviceName;
const char *devname = config->audioDeviceName;
if (!devname || !strcmp(config->audioDeviceName, "auto")) {
return NULL;
}
Expand Down Expand Up @@ -62,11 +62,11 @@ void rsAudioCreate(RSAudio *audio, const RSConfig *config) {
ispec.freq = (int)config->audioSamplerate;
ispec.format = AUDIO_S16LSB;
ispec.channels = (Uint8)config->audioChannels;
ispec.samples = SAMPLES_PER_CALLBACK; //must be power of 2
ispec.samples = SAMPLES_PER_CALLBACK; // must be power of 2
ispec.callback = audioCallback;
ispec.userdata = audio;

const char* devname = getDeviceName(config);
const char *devname = getDeviceName(config);
devId = SDL_OpenAudioDevice(devname, true, &ispec, &ospec, 0);
if (!devId) {
rsError("SDL2 couldn't open audio device %s", SDL_GetError());
Expand All @@ -75,7 +75,8 @@ void rsAudioCreate(RSAudio *audio, const RSConfig *config) {
audio->deviceId = devId;
int size1s = ospec.channels * ospec.freq * (int)sizeof(uint16_t);
int sizeTotal = size1s * config->duration;
int numOfCallbacks = (sizeTotal + (SAMPLES_PER_CALLBACK - 1)) / SAMPLES_PER_CALLBACK; //round up div
int numOfCallbacks =
(sizeTotal + (SAMPLES_PER_CALLBACK - 1)) / SAMPLES_PER_CALLBACK; // round up div
sizeTotal = numOfCallbacks * SAMPLES_PER_CALLBACK;
audio->sizeBatch = size1s / config->framerate;

Expand All @@ -99,4 +100,3 @@ void rsAudioDestroy(RSAudio *audio) {
rsCircleStaticDestroy(&audio->data);
pthread_spin_destroy(&audio->sampleGetLock);
}

5 changes: 2 additions & 3 deletions src/audio_input/audio.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
#ifndef RS_UTIL_AUDIO_H
#define RS_UTIL_AUDIO_H

#include <SDL2/SDL.h>
#include <pthread.h>
#include "../config.h"
#include "../util/circle_static.h"
#include <SDL2/SDL.h>
#include <pthread.h>

typedef struct RSAudio {
SDL_AudioDeviceID deviceId;
Expand All @@ -39,4 +39,3 @@ void rsAudioDestroy(RSAudio *audio);
void rsAudioGetSamples(RSAudio *audio, uint8_t *newbuff, int rewindFrames);

#endif

20 changes: 12 additions & 8 deletions src/audio_input/audio_encoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,29 @@
#include "../util/log.h"
#include "../util/memory.h"

void rsAudioEncoderCreate(RSAudioEncoder* audioenc, const RSConfig *config) {
void rsAudioEncoderCreate(RSAudioEncoder *audioenc, const RSConfig *config) {
if (AACENC_OK != aacEncOpen(&(audioenc->aac_enc), 0, 0)) {
rsError("aacEncOpen failed");
}
aacEncoder_SetParam(audioenc->aac_enc, AACENC_TRANSMUX, 0);
aacEncoder_SetParam(audioenc->aac_enc, AACENC_AFTERBURNER, 1);
aacEncoder_SetParam(audioenc->aac_enc, AACENC_BITRATE, (UINT)config->audioBitrate);
aacEncoder_SetParam(audioenc->aac_enc, AACENC_SAMPLERATE, (UINT)config->audioSamplerate);
aacEncoder_SetParam(audioenc->aac_enc, AACENC_CHANNELMODE, (UINT)config->audioChannels);
aacEncoder_SetParam(audioenc->aac_enc, AACENC_SAMPLERATE,
(UINT)config->audioSamplerate);
aacEncoder_SetParam(audioenc->aac_enc, AACENC_CHANNELMODE,
(UINT)config->audioChannels);
if (AACENC_OK != aacEncEncode(audioenc->aac_enc, NULL, NULL, NULL, NULL)) {
rsError("aacEncEncode failed. Probably bad bitrate. Tried %d", config->audioBitrate);
rsError("aacEncEncode failed. Probably bad bitrate. Tried %d",
config->audioBitrate);
}
aacEncInfo(audioenc->aac_enc, &(audioenc->aac_info));
audioenc->samplesPerFrame = (int)audioenc->aac_info.frameLength * config->audioChannels;
audioenc->samplesPerFrame =
(int)audioenc->aac_info.frameLength * config->audioChannels;
audioenc->frameSize = audioenc->samplesPerFrame * (int)sizeof(uint16_t);
}

void rsAudioEncoderEncode(RSAudioEncoder *audioenc, uint8_t *rawSamples, uint8_t *out, int *numBytes, int *numSamples) {
void rsAudioEncoderEncode(RSAudioEncoder *audioenc, uint8_t *rawSamples, uint8_t *out,
int *numBytes, int *numSamples) {
AACENC_BufDesc ibuf = {0};
AACENC_BufDesc obuf = {0};
AACENC_InArgs iargs = {0};
Expand Down Expand Up @@ -67,8 +72,7 @@ void rsAudioEncoderEncode(RSAudioEncoder *audioenc, uint8_t *rawSamples, uint8_t
obuf.bufSizes = &obufsizes;
obuf.bufElSizes = &oelsizes;

if (AACENC_OK !=
aacEncEncode(audioenc->aac_enc, &ibuf, &obuf, &iargs, &oargs)) {
if (AACENC_OK != aacEncEncode(audioenc->aac_enc, &ibuf, &obuf, &iargs, &oargs)) {
rsLog("AAC: aac encode failed");
}
*numBytes = oargs.numOutBytes;
Expand Down
8 changes: 4 additions & 4 deletions src/audio_input/audio_encoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
#ifndef RS_AUDIO_ENCODER_H
#define RS_AUDIO_ENCODER_H

#include <fdk-aac/aacenc_lib.h>
#include "../config.h"
#include "../util/circle_static.h"
#include <fdk-aac/aacenc_lib.h>

typedef struct RSAudioEncoder {
HANDLE_AACENCODER aac_enc;
Expand All @@ -31,8 +31,8 @@ typedef struct RSAudioEncoder {
int frameSize;
} RSAudioEncoder;

void rsAudioEncoderCreate(RSAudioEncoder* audioenc, const RSConfig *config);
void rsAudioEncoderEncode(RSAudioEncoder *audioenc, uint8_t *samples, uint8_t *out, int *numBytes, int *numSamples);
void rsAudioEncoderCreate(RSAudioEncoder *audioenc, const RSConfig *config);
void rsAudioEncoderEncode(RSAudioEncoder *audioenc, uint8_t *samples, uint8_t *out,
int *numBytes, int *numSamples);

#endif

2 changes: 1 addition & 1 deletion src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
* along with ReplaySorcery. If not, see <https://www.gnu.org/licenses/>.
*/

#include "audio_input/audio.h"
#include "compress.h"
#include "config.h"
#include "output.h"
#include "audio_input/audio.h"
#include "std.h"
#include "system/xlib.h"
#include "util/circle.h"
Expand Down
19 changes: 11 additions & 8 deletions src/output.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ static void *outputThread(void *data) {
mp4_h26x_write_init(&track, muxer, output->config->width, output->config->height,
false);

//setup for audio encoding
// setup for audio encoding
RSAudioEncoder audioenc;
rsAudioEncoderCreate(&audioenc, output->config);
MP4E_track_t tr;
Expand All @@ -105,11 +105,11 @@ static void *outputThread(void *data) {
tr.u.a.channelcount = (unsigned)output->config->audioChannels;
int audio_track_id = MP4E_add_track(muxer, &tr);
MP4E_set_dsi(muxer, audio_track_id, audioenc.aac_info.confBuf,
(int)audioenc.aac_info.confSize);
(int)audioenc.aac_info.confSize);
int64_t ts = 0;
int64_t ats = 0;
int samplesCount = 0;

RSFrame frame, yFrame, uFrame, vFrame;
rsFrameCreate(&frame, (size_t)output->config->width, (size_t)output->config->height,
3);
Expand Down Expand Up @@ -143,19 +143,22 @@ static void *outputThread(void *data) {
inPic.i_pts = (int64_t)(duration * i);
x264_encoder_encode(x264, &nals, &nalCount, &inPic, &outPic);
outputNals(&track, nals, nalCount, duration);

ts += (OUTPUT_TIMEBASE / output->config->framerate) * output->config->audioChannels;
while (ats < ts) {
uint8_t buf[2048];
int numBytes = 0;
int numSamples = 0;
rsAudioEncoderEncode(&audioenc, output->rawSamples, buf, &numBytes, &numSamples);
output->rawSamples += audioenc.frameSize;
output->rawSamples += audioenc.frameSize;
samplesCount += numSamples;
ats = (int64_t)samplesCount * OUTPUT_TIMEBASE / output->config->audioSamplerate;
if (MP4E_STATUS_OK != MP4E_put_sample(muxer, audio_track_id, buf, numBytes,
(numSamples / output->config->audioChannels) * OUTPUT_TIMEBASE / output->config->audioSamplerate, MP4E_SAMPLE_RANDOM_ACCESS)) {
rsError("MP4E_put_sample failed\n");
if (MP4E_STATUS_OK !=
MP4E_put_sample(muxer, audio_track_id, buf, numBytes,
(numSamples / output->config->audioChannels) *
OUTPUT_TIMEBASE / output->config->audioSamplerate,
MP4E_SAMPLE_RANDOM_ACCESS)) {
rsError("MP4E_put_sample failed\n");
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/output.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@

#ifndef RS_OUTPUT_H
#define RS_OUTPUT_H
#include "config.h"
#include "audio_input/audio.h"
#include "audio_input/audio_encoder.h"
#include "config.h"
#include "std.h"
#include "util/buffer.h"
#include "util/circle.h"
Expand Down
1 change: 0 additions & 1 deletion src/util/circle_static.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,3 @@ void rsCircleStaticMoveBackIndex(RSCircleStatic *circle, int bytes) {
}
circle->index = circle->size - (bytes - circle->index);
}

0 comments on commit 0cf51ec

Please sign in to comment.