Add namespace for encoder and decoder

This commit is contained in:
badaix 2019-11-04 14:53:02 +01:00
parent c1a2fedd8d
commit 07e8290ee4
25 changed files with 124 additions and 38 deletions

View file

@ -26,6 +26,8 @@
#include "message/codec_header.hpp"
#include "message/pcm_chunk.hpp"
namespace encoder
{
class Encoder;
@ -96,5 +98,6 @@ protected:
std::string codecOptions_;
};
} // namespace encoder
#endif

View file

@ -34,6 +34,8 @@
using namespace std;
namespace encoder
{
Encoder* EncoderFactory::createEncoder(const std::string& codecSettings) const
{
@ -76,3 +78,5 @@ Encoder* EncoderFactory::createEncoder(const std::string& codecSettings) const
}
*/
}
} // namespace encoder

View file

@ -4,6 +4,9 @@
#include "encoder.hpp"
#include <string>
namespace encoder
{
class EncoderFactory
{
public:
@ -11,5 +14,6 @@ public:
Encoder* createEncoder(const std::string& codecSettings) const;
};
} // namespace encoder
#endif

View file

@ -25,6 +25,8 @@
using namespace std;
namespace encoder
{
FlacEncoder::FlacEncoder(const std::string& codecOptions) : Encoder(codecOptions), encoder_(nullptr), pcmBufferSize_(0), encodedSamples_(0)
{
@ -133,14 +135,15 @@ FLAC__StreamEncoderWriteStatus FlacEncoder::write_callback(const FLAC__StreamEnc
return FLAC__STREAM_ENCODER_WRITE_STATUS_OK;
}
namespace callback
{
FLAC__StreamEncoderWriteStatus write_callback(const FLAC__StreamEncoder* encoder, const FLAC__byte buffer[], size_t bytes, unsigned samples,
unsigned current_frame, void* client_data)
{
FlacEncoder* flacEncoder = (FlacEncoder*)client_data;
return flacEncoder->write_callback(encoder, buffer, bytes, samples, current_frame);
}
} // namespace callback
void FlacEncoder::initEncoder()
{
@ -196,7 +199,9 @@ void FlacEncoder::initEncoder()
throw SnapException("error setting meta data");
// initialize encoder
init_status = FLAC__stream_encoder_init_stream(encoder_, ::write_callback, nullptr, nullptr, nullptr, this);
init_status = FLAC__stream_encoder_init_stream(encoder_, callback::write_callback, nullptr, nullptr, nullptr, this);
if (init_status != FLAC__STREAM_ENCODER_INIT_STATUS_OK)
throw SnapException("ERROR: initializing encoder: " + string(FLAC__StreamEncoderInitStatusString[init_status]));
}
} // namespace encoder

View file

@ -26,6 +26,8 @@
#include "FLAC/metadata.h"
#include "FLAC/stream_encoder.h"
namespace encoder
{
class FlacEncoder : public Encoder
{
@ -53,5 +55,6 @@ protected:
size_t encodedSamples_;
};
} // namespace encoder
#endif

View file

@ -28,6 +28,8 @@
using namespace std;
namespace encoder
{
OggEncoder::OggEncoder(const std::string& codecOptions) : Encoder(codecOptions), lastGranulepos_(0)
{
@ -257,3 +259,5 @@ void OggEncoder::initEncoder()
pos += og_.body_len;
}
}
} // namespace encoder

View file

@ -22,6 +22,9 @@
#include <ogg/ogg.h>
#include <vorbis/vorbisenc.h>
namespace encoder
{
class OggEncoder : public Encoder
{
public:
@ -48,5 +51,6 @@ private:
ogg_int64_t lastGranulepos_;
};
} // namespace encoder
#endif

View file

@ -22,6 +22,9 @@
#include "common/str_compat.hpp"
#include "common/utils/string_utils.hpp"
namespace encoder
{
#define ID_OPUS 0x4F505553
static constexpr opus_int32 const_min_bitrate = 6000;
static constexpr opus_int32 const_max_bitrate = 512000;
@ -36,8 +39,6 @@ void assign(void* pointer, T val)
}
} // namespace
// TODO:
// - handle variable chunk durations (now it's fixed to 10ms)
OpusEncoder::OpusEncoder(const std::string& codecOptions) : Encoder(codecOptions), enc_(nullptr)
{
@ -152,6 +153,9 @@ void OpusEncoder::initEncoder()
}
// TODO:
// handle variable chunk durations, now it's fixed to a "stream_buffer" value of
// 5, 10, 20, 40, 60
void OpusEncoder::encode(const msg::PcmChunk* chunk)
{
int samples_per_channel = chunk->getFrameCount();
@ -179,3 +183,5 @@ void OpusEncoder::encode(const msg::PcmChunk* chunk)
<< '\n';
}
}
} // namespace encoder

View file

@ -22,6 +22,9 @@
#include <opus/opus.h>
namespace encoder
{
class OpusEncoder : public Encoder
{
public:
@ -35,6 +38,8 @@ public:
protected:
void initEncoder() override;
OpusEncoder* enc_;
::OpusEncoder* enc_;
std::vector<u_char> encoded_;
};
} // namespace encoder

View file

@ -21,6 +21,9 @@
#include <memory>
namespace encoder
{
#define ID_RIFF 0x46464952
#define ID_WAVE 0x45564157
#define ID_FMT 0x20746d66
@ -76,3 +79,5 @@ std::string PcmEncoder::name() const
{
return "pcm";
}
} // namespace encoder

View file

@ -20,6 +20,8 @@
#define PCM_ENCODER_H
#include "encoder.hpp"
namespace encoder
{
class PcmEncoder : public Encoder
{
@ -32,5 +34,6 @@ protected:
void initEncoder() override;
};
} // namespace encoder
#endif