mirror of
https://github.com/badaix/snapcast.git
synced 2025-06-04 20:01:44 +02:00
add clang-format file
reformat code
This commit is contained in:
parent
b733f646ea
commit
b20add3815
105 changed files with 7773 additions and 7723 deletions
|
@ -1,6 +1,6 @@
|
|||
/***
|
||||
This file is part of snapcast
|
||||
Copyright (C) 2014-2018 Johannes Pohl
|
||||
Copyright (C) 2014-2019 Johannes Pohl
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
|
@ -19,12 +19,12 @@
|
|||
#ifndef ENCODER_H
|
||||
#define ENCODER_H
|
||||
|
||||
#include <string>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
#include "message/pcmChunk.h"
|
||||
#include "message/codecHeader.h"
|
||||
#include "common/sampleFormat.h"
|
||||
#include "message/codecHeader.h"
|
||||
#include "message/pcmChunk.h"
|
||||
|
||||
|
||||
class Encoder;
|
||||
|
@ -36,7 +36,7 @@ class Encoder;
|
|||
class EncoderListener
|
||||
{
|
||||
public:
|
||||
virtual void onChunkEncoded(const Encoder* encoder, msg::PcmChunk* chunk, double duration) = 0;
|
||||
virtual void onChunkEncoded(const Encoder* encoder, msg::PcmChunk* chunk, double duration) = 0;
|
||||
};
|
||||
|
||||
|
||||
|
@ -49,56 +49,54 @@ public:
|
|||
class Encoder
|
||||
{
|
||||
public:
|
||||
/// ctor. Codec options (E.g. compression level) are passed as string and are codec dependend
|
||||
Encoder(const std::string& codecOptions = "") : headerChunk_(NULL), codecOptions_(codecOptions)
|
||||
{
|
||||
}
|
||||
/// ctor. Codec options (E.g. compression level) are passed as string and are codec dependend
|
||||
Encoder(const std::string& codecOptions = "") : headerChunk_(NULL), codecOptions_(codecOptions)
|
||||
{
|
||||
}
|
||||
|
||||
virtual ~Encoder()
|
||||
{
|
||||
}
|
||||
virtual ~Encoder()
|
||||
{
|
||||
}
|
||||
|
||||
/// The listener will receive the encoded stream
|
||||
virtual void init(EncoderListener* listener, const SampleFormat& format)
|
||||
{
|
||||
if (codecOptions_ == "")
|
||||
codecOptions_ = getDefaultOptions();
|
||||
listener_ = listener;
|
||||
sampleFormat_ = format;
|
||||
initEncoder();
|
||||
}
|
||||
/// The listener will receive the encoded stream
|
||||
virtual void init(EncoderListener* listener, const SampleFormat& format)
|
||||
{
|
||||
if (codecOptions_ == "")
|
||||
codecOptions_ = getDefaultOptions();
|
||||
listener_ = listener;
|
||||
sampleFormat_ = format;
|
||||
initEncoder();
|
||||
}
|
||||
|
||||
/// Here the work is done. Encoded data is passed to the EncoderListener.
|
||||
virtual void encode(const msg::PcmChunk* chunk) = 0;
|
||||
/// Here the work is done. Encoded data is passed to the EncoderListener.
|
||||
virtual void encode(const msg::PcmChunk* chunk) = 0;
|
||||
|
||||
virtual std::string name() const = 0;
|
||||
virtual std::string name() const = 0;
|
||||
|
||||
virtual std::string getAvailableOptions() const
|
||||
{
|
||||
return "No codec options supported";
|
||||
}
|
||||
virtual std::string getAvailableOptions() const
|
||||
{
|
||||
return "No codec options supported";
|
||||
}
|
||||
|
||||
virtual std::string getDefaultOptions() const
|
||||
{
|
||||
return "";
|
||||
}
|
||||
virtual std::string getDefaultOptions() const
|
||||
{
|
||||
return "";
|
||||
}
|
||||
|
||||
/// Header information needed to decode the data
|
||||
virtual std::shared_ptr<msg::CodecHeader> getHeader() const
|
||||
{
|
||||
return headerChunk_;
|
||||
}
|
||||
/// Header information needed to decode the data
|
||||
virtual std::shared_ptr<msg::CodecHeader> getHeader() const
|
||||
{
|
||||
return headerChunk_;
|
||||
}
|
||||
|
||||
protected:
|
||||
virtual void initEncoder() = 0;
|
||||
virtual void initEncoder() = 0;
|
||||
|
||||
SampleFormat sampleFormat_;
|
||||
std::shared_ptr<msg::CodecHeader> headerChunk_;
|
||||
EncoderListener* listener_;
|
||||
std::string codecOptions_;
|
||||
SampleFormat sampleFormat_;
|
||||
std::shared_ptr<msg::CodecHeader> headerChunk_;
|
||||
EncoderListener* listener_;
|
||||
std::string codecOptions_;
|
||||
};
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
/***
|
||||
This file is part of snapcast
|
||||
Copyright (C) 2014-2018 Johannes Pohl
|
||||
Copyright (C) 2014-2019 Johannes Pohl
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
|
@ -24,9 +24,9 @@
|
|||
#if defined(HAS_FLAC)
|
||||
#include "flacEncoder.h"
|
||||
#endif
|
||||
#include "common/utils/string_utils.h"
|
||||
#include "common/snapException.h"
|
||||
#include "aixlog.hpp"
|
||||
#include "common/snapException.h"
|
||||
#include "common/utils/string_utils.h"
|
||||
|
||||
|
||||
using namespace std;
|
||||
|
@ -34,41 +34,38 @@ using namespace std;
|
|||
|
||||
Encoder* EncoderFactory::createEncoder(const std::string& codecSettings) const
|
||||
{
|
||||
Encoder* encoder;
|
||||
std::string codec(codecSettings);
|
||||
std::string codecOptions;
|
||||
if (codec.find(":") != std::string::npos)
|
||||
{
|
||||
codecOptions = utils::string::trim_copy(codec.substr(codec.find(":") + 1));
|
||||
codec = utils::string::trim_copy(codec.substr(0, codec.find(":")));
|
||||
}
|
||||
Encoder* encoder;
|
||||
std::string codec(codecSettings);
|
||||
std::string codecOptions;
|
||||
if (codec.find(":") != std::string::npos)
|
||||
{
|
||||
codecOptions = utils::string::trim_copy(codec.substr(codec.find(":") + 1));
|
||||
codec = utils::string::trim_copy(codec.substr(0, codec.find(":")));
|
||||
}
|
||||
if (codec == "pcm")
|
||||
encoder = new PcmEncoder(codecOptions);
|
||||
#if defined(HAS_OGG) && defined(HAS_VORBIS) && defined(HAS_VORBISENC)
|
||||
else if (codec == "ogg")
|
||||
encoder = new OggEncoder(codecOptions);
|
||||
encoder = new OggEncoder(codecOptions);
|
||||
#endif
|
||||
#if defined(HAS_FLAC)
|
||||
else if (codec == "flac")
|
||||
encoder = new FlacEncoder(codecOptions);
|
||||
else if (codec == "flac")
|
||||
encoder = new FlacEncoder(codecOptions);
|
||||
#endif
|
||||
else
|
||||
{
|
||||
throw SnapException("unknown codec: " + codec);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw SnapException("unknown codec: " + codec);
|
||||
}
|
||||
|
||||
return encoder;
|
||||
/* try
|
||||
{
|
||||
encoder->init(NULL, format, codecOptions);
|
||||
}
|
||||
catch (const std::exception& e)
|
||||
{
|
||||
cout << "Error: " << e.what() << "\n";
|
||||
return 1;
|
||||
}
|
||||
*/
|
||||
return encoder;
|
||||
/* try
|
||||
{
|
||||
encoder->init(NULL, format, codecOptions);
|
||||
}
|
||||
catch (const std::exception& e)
|
||||
{
|
||||
cout << "Error: " << e.what() << "\n";
|
||||
return 1;
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -1,14 +1,14 @@
|
|||
#ifndef ENCODER_FACTORY_H
|
||||
#define ENCODER_FACTORY_H
|
||||
|
||||
#include <string>
|
||||
#include "encoder.h"
|
||||
#include <string>
|
||||
|
||||
class EncoderFactory
|
||||
{
|
||||
public:
|
||||
// EncoderFactory(const std::string& codecSettings);
|
||||
Encoder* createEncoder(const std::string& codecSettings) const;
|
||||
// EncoderFactory(const std::string& codecSettings);
|
||||
Encoder* createEncoder(const std::string& codecSettings) const;
|
||||
};
|
||||
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
/***
|
||||
This file is part of snapcast
|
||||
Copyright (C) 2014-2018 Johannes Pohl
|
||||
Copyright (C) 2014-2019 Johannes Pohl
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
|
@ -18,194 +18,185 @@
|
|||
|
||||
#include <iostream>
|
||||
|
||||
#include "flacEncoder.h"
|
||||
#include "common/strCompat.h"
|
||||
#include "common/snapException.h"
|
||||
#include "aixlog.hpp"
|
||||
#include "common/snapException.h"
|
||||
#include "common/strCompat.h"
|
||||
#include "flacEncoder.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
||||
FlacEncoder::FlacEncoder(const std::string& codecOptions) : Encoder(codecOptions), encoder_(NULL), pcmBufferSize_(0), encodedSamples_(0)
|
||||
{
|
||||
flacChunk_ = new msg::PcmChunk();
|
||||
headerChunk_.reset(new msg::CodecHeader("flac"));
|
||||
pcmBuffer_ = (FLAC__int32*)malloc(pcmBufferSize_ * sizeof(FLAC__int32));
|
||||
flacChunk_ = new msg::PcmChunk();
|
||||
headerChunk_.reset(new msg::CodecHeader("flac"));
|
||||
pcmBuffer_ = (FLAC__int32*)malloc(pcmBufferSize_ * sizeof(FLAC__int32));
|
||||
}
|
||||
|
||||
|
||||
FlacEncoder::~FlacEncoder()
|
||||
{
|
||||
if (encoder_ != NULL)
|
||||
{
|
||||
FLAC__stream_encoder_finish(encoder_);
|
||||
FLAC__metadata_object_delete(metadata_[0]);
|
||||
FLAC__metadata_object_delete(metadata_[1]);
|
||||
FLAC__stream_encoder_delete(encoder_);
|
||||
}
|
||||
if (encoder_ != NULL)
|
||||
{
|
||||
FLAC__stream_encoder_finish(encoder_);
|
||||
FLAC__metadata_object_delete(metadata_[0]);
|
||||
FLAC__metadata_object_delete(metadata_[1]);
|
||||
FLAC__stream_encoder_delete(encoder_);
|
||||
}
|
||||
|
||||
delete flacChunk_;
|
||||
free(pcmBuffer_);
|
||||
delete flacChunk_;
|
||||
free(pcmBuffer_);
|
||||
}
|
||||
|
||||
|
||||
std::string FlacEncoder::getAvailableOptions() const
|
||||
{
|
||||
return "compression level: [0..8]";
|
||||
return "compression level: [0..8]";
|
||||
}
|
||||
|
||||
|
||||
std::string FlacEncoder::getDefaultOptions() const
|
||||
{
|
||||
return "2";
|
||||
return "2";
|
||||
}
|
||||
|
||||
|
||||
std::string FlacEncoder::name() const
|
||||
{
|
||||
return "flac";
|
||||
return "flac";
|
||||
}
|
||||
|
||||
|
||||
void FlacEncoder::encode(const msg::PcmChunk* chunk)
|
||||
{
|
||||
int samples = chunk->getSampleCount();
|
||||
int frames = chunk->getFrameCount();
|
||||
// LOG(INFO) << "payload: " << chunk->payloadSize << "\tframes: " << frames << "\tsamples: " << samples << "\tduration: " << chunk->duration<chronos::msec>().count() << "\n";
|
||||
int samples = chunk->getSampleCount();
|
||||
int frames = chunk->getFrameCount();
|
||||
// LOG(INFO) << "payload: " << chunk->payloadSize << "\tframes: " << frames << "\tsamples: " << samples << "\tduration: " <<
|
||||
//chunk->duration<chronos::msec>().count() << "\n";
|
||||
|
||||
if (pcmBufferSize_ < samples)
|
||||
{
|
||||
pcmBufferSize_ = samples;
|
||||
pcmBuffer_ = (FLAC__int32*)realloc(pcmBuffer_, pcmBufferSize_ * sizeof(FLAC__int32));
|
||||
}
|
||||
if (pcmBufferSize_ < samples)
|
||||
{
|
||||
pcmBufferSize_ = samples;
|
||||
pcmBuffer_ = (FLAC__int32*)realloc(pcmBuffer_, pcmBufferSize_ * sizeof(FLAC__int32));
|
||||
}
|
||||
|
||||
if (sampleFormat_.sampleSize == 1)
|
||||
{
|
||||
FLAC__int8* buffer = (FLAC__int8*)chunk->payload;
|
||||
for(int i=0; i<samples; i++)
|
||||
pcmBuffer_[i] = (FLAC__int32)(buffer[i]);
|
||||
}
|
||||
else if (sampleFormat_.sampleSize == 2)
|
||||
{
|
||||
FLAC__int16* buffer = (FLAC__int16*)chunk->payload;
|
||||
for(int i=0; i<samples; i++)
|
||||
pcmBuffer_[i] = (FLAC__int32)(buffer[i]);
|
||||
}
|
||||
else if (sampleFormat_.sampleSize == 4)
|
||||
{
|
||||
FLAC__int32* buffer = (FLAC__int32*)chunk->payload;
|
||||
for(int i=0; i<samples; i++)
|
||||
pcmBuffer_[i] = (FLAC__int32)(buffer[i]);
|
||||
}
|
||||
if (sampleFormat_.sampleSize == 1)
|
||||
{
|
||||
FLAC__int8* buffer = (FLAC__int8*)chunk->payload;
|
||||
for (int i = 0; i < samples; i++)
|
||||
pcmBuffer_[i] = (FLAC__int32)(buffer[i]);
|
||||
}
|
||||
else if (sampleFormat_.sampleSize == 2)
|
||||
{
|
||||
FLAC__int16* buffer = (FLAC__int16*)chunk->payload;
|
||||
for (int i = 0; i < samples; i++)
|
||||
pcmBuffer_[i] = (FLAC__int32)(buffer[i]);
|
||||
}
|
||||
else if (sampleFormat_.sampleSize == 4)
|
||||
{
|
||||
FLAC__int32* buffer = (FLAC__int32*)chunk->payload;
|
||||
for (int i = 0; i < samples; i++)
|
||||
pcmBuffer_[i] = (FLAC__int32)(buffer[i]);
|
||||
}
|
||||
|
||||
|
||||
FLAC__stream_encoder_process_interleaved(encoder_, pcmBuffer_, frames);
|
||||
FLAC__stream_encoder_process_interleaved(encoder_, pcmBuffer_, frames);
|
||||
|
||||
if (encodedSamples_ > 0)
|
||||
{
|
||||
double resMs = encodedSamples_ / ((double)sampleFormat_.rate / 1000.);
|
||||
// LOG(INFO) << "encoded: " << chunk->payloadSize << "\tframes: " << encodedSamples_ << "\tres: " << resMs << "\n";
|
||||
encodedSamples_ = 0;
|
||||
listener_->onChunkEncoded(this, flacChunk_, resMs);
|
||||
flacChunk_ = new msg::PcmChunk(chunk->format, 0);
|
||||
}
|
||||
if (encodedSamples_ > 0)
|
||||
{
|
||||
double resMs = encodedSamples_ / ((double)sampleFormat_.rate / 1000.);
|
||||
// LOG(INFO) << "encoded: " << chunk->payloadSize << "\tframes: " << encodedSamples_ << "\tres: " << resMs << "\n";
|
||||
encodedSamples_ = 0;
|
||||
listener_->onChunkEncoded(this, flacChunk_, resMs);
|
||||
flacChunk_ = new msg::PcmChunk(chunk->format, 0);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
FLAC__StreamEncoderWriteStatus FlacEncoder::write_callback(const FLAC__StreamEncoder *encoder,
|
||||
const FLAC__byte buffer[],
|
||||
size_t bytes,
|
||||
unsigned samples,
|
||||
unsigned current_frame)
|
||||
FLAC__StreamEncoderWriteStatus FlacEncoder::write_callback(const FLAC__StreamEncoder* encoder, const FLAC__byte buffer[], size_t bytes, unsigned samples,
|
||||
unsigned current_frame)
|
||||
{
|
||||
// LOG(INFO) << "write_callback: " << bytes << ", " << samples << ", " << current_frame << "\n";
|
||||
if ((current_frame == 0) && (bytes > 0) && (samples == 0))
|
||||
{
|
||||
headerChunk_->payload = (char*)realloc(headerChunk_->payload, headerChunk_->payloadSize + bytes);
|
||||
memcpy(headerChunk_->payload + headerChunk_->payloadSize, buffer, bytes);
|
||||
headerChunk_->payloadSize += bytes;
|
||||
}
|
||||
else
|
||||
{
|
||||
flacChunk_->payload = (char*)realloc(flacChunk_->payload, flacChunk_->payloadSize + bytes);
|
||||
memcpy(flacChunk_->payload + flacChunk_->payloadSize, buffer, bytes);
|
||||
flacChunk_->payloadSize += bytes;
|
||||
encodedSamples_ += samples;
|
||||
}
|
||||
return FLAC__STREAM_ENCODER_WRITE_STATUS_OK;
|
||||
// LOG(INFO) << "write_callback: " << bytes << ", " << samples << ", " << current_frame << "\n";
|
||||
if ((current_frame == 0) && (bytes > 0) && (samples == 0))
|
||||
{
|
||||
headerChunk_->payload = (char*)realloc(headerChunk_->payload, headerChunk_->payloadSize + bytes);
|
||||
memcpy(headerChunk_->payload + headerChunk_->payloadSize, buffer, bytes);
|
||||
headerChunk_->payloadSize += bytes;
|
||||
}
|
||||
else
|
||||
{
|
||||
flacChunk_->payload = (char*)realloc(flacChunk_->payload, flacChunk_->payloadSize + bytes);
|
||||
memcpy(flacChunk_->payload + flacChunk_->payloadSize, buffer, bytes);
|
||||
flacChunk_->payloadSize += bytes;
|
||||
encodedSamples_ += samples;
|
||||
}
|
||||
return FLAC__STREAM_ENCODER_WRITE_STATUS_OK;
|
||||
}
|
||||
|
||||
|
||||
FLAC__StreamEncoderWriteStatus write_callback(const FLAC__StreamEncoder *encoder,
|
||||
const FLAC__byte buffer[],
|
||||
size_t bytes,
|
||||
unsigned samples,
|
||||
unsigned current_frame,
|
||||
void *client_data)
|
||||
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);
|
||||
FlacEncoder* flacEncoder = (FlacEncoder*)client_data;
|
||||
return flacEncoder->write_callback(encoder, buffer, bytes, samples, current_frame);
|
||||
}
|
||||
|
||||
|
||||
void FlacEncoder::initEncoder()
|
||||
{
|
||||
int quality(2);
|
||||
try
|
||||
{
|
||||
quality = cpt::stoi(codecOptions_);
|
||||
}
|
||||
catch(...)
|
||||
{
|
||||
throw SnapException("Invalid codec option: \"" + codecOptions_ + "\"");
|
||||
}
|
||||
if ((quality < 0) || (quality > 8))
|
||||
{
|
||||
throw SnapException("compression level has to be between 0 and 8");
|
||||
}
|
||||
int quality(2);
|
||||
try
|
||||
{
|
||||
quality = cpt::stoi(codecOptions_);
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
throw SnapException("Invalid codec option: \"" + codecOptions_ + "\"");
|
||||
}
|
||||
if ((quality < 0) || (quality > 8))
|
||||
{
|
||||
throw SnapException("compression level has to be between 0 and 8");
|
||||
}
|
||||
|
||||
FLAC__bool ok = true;
|
||||
FLAC__StreamEncoderInitStatus init_status;
|
||||
FLAC__StreamMetadata_VorbisComment_Entry entry;
|
||||
FLAC__bool ok = true;
|
||||
FLAC__StreamEncoderInitStatus init_status;
|
||||
FLAC__StreamMetadata_VorbisComment_Entry entry;
|
||||
|
||||
// allocate the encoder
|
||||
if ((encoder_ = FLAC__stream_encoder_new()) == NULL)
|
||||
throw SnapException("error allocating encoder");
|
||||
// allocate the encoder
|
||||
if ((encoder_ = FLAC__stream_encoder_new()) == NULL)
|
||||
throw SnapException("error allocating encoder");
|
||||
|
||||
ok &= FLAC__stream_encoder_set_verify(encoder_, true);
|
||||
// compression levels (0-8):
|
||||
// https://xiph.org/flac/api/group__flac__stream__encoder.html#gae49cf32f5256cb47eecd33779493ac85
|
||||
// latency:
|
||||
// 0-2: 1152 frames, ~26.1224ms
|
||||
// 3-8: 4096 frames, ~92.8798ms
|
||||
ok &= FLAC__stream_encoder_set_compression_level(encoder_, quality);
|
||||
ok &= FLAC__stream_encoder_set_channels(encoder_, sampleFormat_.channels);
|
||||
ok &= FLAC__stream_encoder_set_bits_per_sample(encoder_, sampleFormat_.bits);
|
||||
ok &= FLAC__stream_encoder_set_sample_rate(encoder_, sampleFormat_.rate);
|
||||
ok &= FLAC__stream_encoder_set_verify(encoder_, true);
|
||||
// compression levels (0-8):
|
||||
// https://xiph.org/flac/api/group__flac__stream__encoder.html#gae49cf32f5256cb47eecd33779493ac85
|
||||
// latency:
|
||||
// 0-2: 1152 frames, ~26.1224ms
|
||||
// 3-8: 4096 frames, ~92.8798ms
|
||||
ok &= FLAC__stream_encoder_set_compression_level(encoder_, quality);
|
||||
ok &= FLAC__stream_encoder_set_channels(encoder_, sampleFormat_.channels);
|
||||
ok &= FLAC__stream_encoder_set_bits_per_sample(encoder_, sampleFormat_.bits);
|
||||
ok &= FLAC__stream_encoder_set_sample_rate(encoder_, sampleFormat_.rate);
|
||||
|
||||
if (!ok)
|
||||
throw SnapException("error setting up encoder");
|
||||
if (!ok)
|
||||
throw SnapException("error setting up encoder");
|
||||
|
||||
// now add some metadata; we'll add some tags and a padding block
|
||||
if (
|
||||
(metadata_[0] = FLAC__metadata_object_new(FLAC__METADATA_TYPE_VORBIS_COMMENT)) == NULL ||
|
||||
(metadata_[1] = FLAC__metadata_object_new(FLAC__METADATA_TYPE_PADDING)) == NULL ||
|
||||
// there are many tag (vorbiscomment) functions but these are convenient for this particular use:
|
||||
!FLAC__metadata_object_vorbiscomment_entry_from_name_value_pair(&entry, "TITLE", "SnapStream") ||
|
||||
!FLAC__metadata_object_vorbiscomment_append_comment(metadata_[0], entry, false) ||
|
||||
!FLAC__metadata_object_vorbiscomment_entry_from_name_value_pair(&entry, "VERSION", VERSION) ||
|
||||
!FLAC__metadata_object_vorbiscomment_append_comment(metadata_[0], entry, false)
|
||||
)
|
||||
throw SnapException("out of memory or tag error");
|
||||
// now add some metadata; we'll add some tags and a padding block
|
||||
if ((metadata_[0] = FLAC__metadata_object_new(FLAC__METADATA_TYPE_VORBIS_COMMENT)) == NULL ||
|
||||
(metadata_[1] = FLAC__metadata_object_new(FLAC__METADATA_TYPE_PADDING)) == NULL ||
|
||||
// there are many tag (vorbiscomment) functions but these are convenient for this particular use:
|
||||
!FLAC__metadata_object_vorbiscomment_entry_from_name_value_pair(&entry, "TITLE", "SnapStream") ||
|
||||
!FLAC__metadata_object_vorbiscomment_append_comment(metadata_[0], entry, false) ||
|
||||
!FLAC__metadata_object_vorbiscomment_entry_from_name_value_pair(&entry, "VERSION", VERSION) ||
|
||||
!FLAC__metadata_object_vorbiscomment_append_comment(metadata_[0], entry, false))
|
||||
throw SnapException("out of memory or tag error");
|
||||
|
||||
metadata_[1]->length = 1234; // set the padding length
|
||||
ok = FLAC__stream_encoder_set_metadata(encoder_, metadata_, 2);
|
||||
if (!ok)
|
||||
throw SnapException("error setting meta data");
|
||||
metadata_[1]->length = 1234; // set the padding length
|
||||
ok = FLAC__stream_encoder_set_metadata(encoder_, metadata_, 2);
|
||||
if (!ok)
|
||||
throw SnapException("error setting meta data");
|
||||
|
||||
// initialize encoder
|
||||
init_status = FLAC__stream_encoder_init_stream(encoder_, ::write_callback, NULL, NULL, NULL, this);
|
||||
if(init_status != FLAC__STREAM_ENCODER_INIT_STATUS_OK)
|
||||
throw SnapException("ERROR: initializing encoder: " + string(FLAC__StreamEncoderInitStatusString[init_status]));
|
||||
// initialize encoder
|
||||
init_status = FLAC__stream_encoder_init_stream(encoder_, ::write_callback, NULL, NULL, NULL, this);
|
||||
if (init_status != FLAC__STREAM_ENCODER_INIT_STATUS_OK)
|
||||
throw SnapException("ERROR: initializing encoder: " + string(FLAC__StreamEncoderInitStatusString[init_status]));
|
||||
}
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
/***
|
||||
This file is part of snapcast
|
||||
Copyright (C) 2014-2018 Johannes Pohl
|
||||
Copyright (C) 2014-2019 Johannes Pohl
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
|
@ -33,19 +33,20 @@ public:
|
|||
FlacEncoder(const std::string& codecOptions = "");
|
||||
~FlacEncoder();
|
||||
virtual void encode(const msg::PcmChunk* chunk);
|
||||
virtual std::string getAvailableOptions() const;
|
||||
virtual std::string getDefaultOptions() const;
|
||||
virtual std::string name() const;
|
||||
virtual std::string getAvailableOptions() const;
|
||||
virtual std::string getDefaultOptions() const;
|
||||
virtual std::string name() const;
|
||||
|
||||
FLAC__StreamEncoderWriteStatus write_callback(const FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], size_t bytes, unsigned samples, unsigned current_frame);
|
||||
FLAC__StreamEncoderWriteStatus write_callback(const FLAC__StreamEncoder* encoder, const FLAC__byte buffer[], size_t bytes, unsigned samples,
|
||||
unsigned current_frame);
|
||||
|
||||
protected:
|
||||
virtual void initEncoder();
|
||||
|
||||
FLAC__StreamEncoder *encoder_;
|
||||
FLAC__StreamMetadata *metadata_[2];
|
||||
FLAC__StreamEncoder* encoder_;
|
||||
FLAC__StreamMetadata* metadata_[2];
|
||||
|
||||
FLAC__int32 *pcmBuffer_;
|
||||
FLAC__int32* pcmBuffer_;
|
||||
int pcmBufferSize_;
|
||||
|
||||
msg::PcmChunk* flacChunk_;
|
||||
|
@ -54,5 +55,3 @@ protected:
|
|||
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
/***
|
||||
This file is part of snapcast
|
||||
Copyright (C) 2014-2018 Johannes Pohl
|
||||
Copyright (C) 2014-2019 Johannes Pohl
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
|
@ -16,15 +16,15 @@
|
|||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
***/
|
||||
|
||||
#include <iostream>
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
|
||||
#include "oggEncoder.h"
|
||||
#include "aixlog.hpp"
|
||||
#include "common/snapException.h"
|
||||
#include "common/strCompat.h"
|
||||
#include "common/utils/string_utils.h"
|
||||
#include "common/utils.h"
|
||||
#include "aixlog.hpp"
|
||||
#include "common/utils/string_utils.h"
|
||||
#include "oggEncoder.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
@ -36,225 +36,224 @@ OggEncoder::OggEncoder(const std::string& codecOptions) : Encoder(codecOptions),
|
|||
|
||||
std::string OggEncoder::getAvailableOptions() const
|
||||
{
|
||||
return "VBR:[-0.1 - 1.0]";
|
||||
return "VBR:[-0.1 - 1.0]";
|
||||
}
|
||||
|
||||
|
||||
std::string OggEncoder::getDefaultOptions() const
|
||||
{
|
||||
return "VBR:0.9";
|
||||
return "VBR:0.9";
|
||||
}
|
||||
|
||||
|
||||
std::string OggEncoder::name() const
|
||||
{
|
||||
return "ogg";
|
||||
return "ogg";
|
||||
}
|
||||
|
||||
|
||||
void OggEncoder::encode(const msg::PcmChunk* chunk)
|
||||
{
|
||||
double res = 0;
|
||||
LOG(DEBUG) << "payload: " << chunk->payloadSize << "\tframes: " << chunk->getFrameCount() << "\tduration: " << chunk->duration<chronos::msec>().count() << "\n";
|
||||
int frames = chunk->getFrameCount();
|
||||
float **buffer=vorbis_analysis_buffer(&vd_, frames);
|
||||
double res = 0;
|
||||
LOG(DEBUG) << "payload: " << chunk->payloadSize << "\tframes: " << chunk->getFrameCount() << "\tduration: " << chunk->duration<chronos::msec>().count()
|
||||
<< "\n";
|
||||
int frames = chunk->getFrameCount();
|
||||
float** buffer = vorbis_analysis_buffer(&vd_, frames);
|
||||
|
||||
/* uninterleave samples */
|
||||
for (size_t channel = 0; channel < sampleFormat_.channels; ++channel)
|
||||
{
|
||||
if (sampleFormat_.sampleSize == 1)
|
||||
{
|
||||
int8_t* chunkBuffer = (int8_t*)chunk->payload;
|
||||
for (int i=0; i<frames; i++)
|
||||
buffer[channel][i]= chunkBuffer[sampleFormat_.channels*i + channel] / 128.f;
|
||||
}
|
||||
else if (sampleFormat_.sampleSize == 2)
|
||||
{
|
||||
int16_t* chunkBuffer = (int16_t*)chunk->payload;
|
||||
for (int i=0; i<frames; i++)
|
||||
buffer[channel][i]= chunkBuffer[sampleFormat_.channels*i + channel] / 32768.f;
|
||||
}
|
||||
else if (sampleFormat_.sampleSize == 4)
|
||||
{
|
||||
int32_t* chunkBuffer = (int32_t*)chunk->payload;
|
||||
for (int i=0; i<frames; i++)
|
||||
buffer[channel][i]= chunkBuffer[sampleFormat_.channels*i + channel] / 2147483648.f;
|
||||
}
|
||||
}
|
||||
/* uninterleave samples */
|
||||
for (size_t channel = 0; channel < sampleFormat_.channels; ++channel)
|
||||
{
|
||||
if (sampleFormat_.sampleSize == 1)
|
||||
{
|
||||
int8_t* chunkBuffer = (int8_t*)chunk->payload;
|
||||
for (int i = 0; i < frames; i++)
|
||||
buffer[channel][i] = chunkBuffer[sampleFormat_.channels * i + channel] / 128.f;
|
||||
}
|
||||
else if (sampleFormat_.sampleSize == 2)
|
||||
{
|
||||
int16_t* chunkBuffer = (int16_t*)chunk->payload;
|
||||
for (int i = 0; i < frames; i++)
|
||||
buffer[channel][i] = chunkBuffer[sampleFormat_.channels * i + channel] / 32768.f;
|
||||
}
|
||||
else if (sampleFormat_.sampleSize == 4)
|
||||
{
|
||||
int32_t* chunkBuffer = (int32_t*)chunk->payload;
|
||||
for (int i = 0; i < frames; i++)
|
||||
buffer[channel][i] = chunkBuffer[sampleFormat_.channels * i + channel] / 2147483648.f;
|
||||
}
|
||||
}
|
||||
|
||||
/* tell the library how much we actually submitted */
|
||||
vorbis_analysis_wrote(&vd_, frames);
|
||||
/* tell the library how much we actually submitted */
|
||||
vorbis_analysis_wrote(&vd_, frames);
|
||||
|
||||
msg::PcmChunk* oggChunk = new msg::PcmChunk(chunk->format, 0);
|
||||
msg::PcmChunk* oggChunk = new msg::PcmChunk(chunk->format, 0);
|
||||
|
||||
/* vorbis does some data preanalysis, then divvies up blocks for
|
||||
more involved (potentially parallel) processing. Get a single
|
||||
block for encoding now */
|
||||
size_t pos = 0;
|
||||
while (vorbis_analysis_blockout(&vd_, &vb_)==1)
|
||||
{
|
||||
/* analysis, assume we want to use bitrate management */
|
||||
vorbis_analysis(&vb_, NULL);
|
||||
vorbis_bitrate_addblock(&vb_);
|
||||
/* vorbis does some data preanalysis, then divvies up blocks for
|
||||
more involved (potentially parallel) processing. Get a single
|
||||
block for encoding now */
|
||||
size_t pos = 0;
|
||||
while (vorbis_analysis_blockout(&vd_, &vb_) == 1)
|
||||
{
|
||||
/* analysis, assume we want to use bitrate management */
|
||||
vorbis_analysis(&vb_, NULL);
|
||||
vorbis_bitrate_addblock(&vb_);
|
||||
|
||||
while (vorbis_bitrate_flushpacket(&vd_, &op_))
|
||||
{
|
||||
/* weld the packet into the bitstream */
|
||||
ogg_stream_packetin(&os_, &op_);
|
||||
while (vorbis_bitrate_flushpacket(&vd_, &op_))
|
||||
{
|
||||
/* weld the packet into the bitstream */
|
||||
ogg_stream_packetin(&os_, &op_);
|
||||
|
||||
/* write out pages (if any) */
|
||||
while (true)
|
||||
{
|
||||
int result = ogg_stream_flush(&os_, &og_);
|
||||
if (result == 0)
|
||||
break;
|
||||
res = os_.granulepos - lastGranulepos_;
|
||||
/* write out pages (if any) */
|
||||
while (true)
|
||||
{
|
||||
int result = ogg_stream_flush(&os_, &og_);
|
||||
if (result == 0)
|
||||
break;
|
||||
res = os_.granulepos - lastGranulepos_;
|
||||
|
||||
size_t nextLen = pos + og_.header_len + og_.body_len;
|
||||
// make chunk larger
|
||||
if (oggChunk->payloadSize < nextLen)
|
||||
oggChunk->payload = (char*)realloc(oggChunk->payload, nextLen);
|
||||
size_t nextLen = pos + og_.header_len + og_.body_len;
|
||||
// make chunk larger
|
||||
if (oggChunk->payloadSize < nextLen)
|
||||
oggChunk->payload = (char*)realloc(oggChunk->payload, nextLen);
|
||||
|
||||
memcpy(oggChunk->payload + pos, og_.header, og_.header_len);
|
||||
pos += og_.header_len;
|
||||
memcpy(oggChunk->payload + pos, og_.body, og_.body_len);
|
||||
pos += og_.body_len;
|
||||
memcpy(oggChunk->payload + pos, og_.header, og_.header_len);
|
||||
pos += og_.header_len;
|
||||
memcpy(oggChunk->payload + pos, og_.body, og_.body_len);
|
||||
pos += og_.body_len;
|
||||
|
||||
if (ogg_page_eos(&og_))
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (ogg_page_eos(&og_))
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (res > 0)
|
||||
{
|
||||
res /= (sampleFormat_.rate / 1000.);
|
||||
// LOG(INFO) << "res: " << res << "\n";
|
||||
lastGranulepos_ = os_.granulepos;
|
||||
// make oggChunk smaller
|
||||
oggChunk->payload = (char*)realloc(oggChunk->payload, pos);
|
||||
oggChunk->payloadSize = pos;
|
||||
listener_->onChunkEncoded(this, oggChunk, res);
|
||||
}
|
||||
else
|
||||
delete oggChunk;
|
||||
if (res > 0)
|
||||
{
|
||||
res /= (sampleFormat_.rate / 1000.);
|
||||
// LOG(INFO) << "res: " << res << "\n";
|
||||
lastGranulepos_ = os_.granulepos;
|
||||
// make oggChunk smaller
|
||||
oggChunk->payload = (char*)realloc(oggChunk->payload, pos);
|
||||
oggChunk->payloadSize = pos;
|
||||
listener_->onChunkEncoded(this, oggChunk, res);
|
||||
}
|
||||
else
|
||||
delete oggChunk;
|
||||
}
|
||||
|
||||
|
||||
void OggEncoder::initEncoder()
|
||||
{
|
||||
if (codecOptions_.find(":") == string::npos)
|
||||
throw SnapException("Invalid codec options: \"" + codecOptions_ + "\"");
|
||||
string mode = utils::string::trim_copy(codecOptions_.substr(0, codecOptions_.find(":")));
|
||||
if (mode != "VBR")
|
||||
throw SnapException("Unsupported codec mode: \"" + mode + "\". Available: \"VBR\"");
|
||||
if (codecOptions_.find(":") == string::npos)
|
||||
throw SnapException("Invalid codec options: \"" + codecOptions_ + "\"");
|
||||
string mode = utils::string::trim_copy(codecOptions_.substr(0, codecOptions_.find(":")));
|
||||
if (mode != "VBR")
|
||||
throw SnapException("Unsupported codec mode: \"" + mode + "\". Available: \"VBR\"");
|
||||
|
||||
string qual = utils::string::trim_copy(codecOptions_.substr(codecOptions_.find(":") + 1));
|
||||
double quality = 1.0;
|
||||
try
|
||||
{
|
||||
quality = cpt::stod(qual);
|
||||
}
|
||||
catch(...)
|
||||
{
|
||||
throw SnapException("Invalid codec option: \"" + codecOptions_ + "\"");
|
||||
}
|
||||
if ((quality < -0.1) || (quality > 1.0))
|
||||
{
|
||||
throw SnapException("compression level has to be between -0.1 and 1.0");
|
||||
}
|
||||
string qual = utils::string::trim_copy(codecOptions_.substr(codecOptions_.find(":") + 1));
|
||||
double quality = 1.0;
|
||||
try
|
||||
{
|
||||
quality = cpt::stod(qual);
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
throw SnapException("Invalid codec option: \"" + codecOptions_ + "\"");
|
||||
}
|
||||
if ((quality < -0.1) || (quality > 1.0))
|
||||
{
|
||||
throw SnapException("compression level has to be between -0.1 and 1.0");
|
||||
}
|
||||
|
||||
/********** Encode setup ************/
|
||||
vorbis_info_init(&vi_);
|
||||
/********** Encode setup ************/
|
||||
vorbis_info_init(&vi_);
|
||||
|
||||
/* choose an encoding mode. A few possibilities commented out, one
|
||||
actually used: */
|
||||
/* choose an encoding mode. A few possibilities commented out, one
|
||||
actually used: */
|
||||
|
||||
/*********************************************************************
|
||||
Encoding using a VBR quality mode. The usable range is -.1
|
||||
(lowest quality, smallest file) to 1. (highest quality, largest file).
|
||||
Example quality mode .4: 44kHz stereo coupled, roughly 128kbps VBR
|
||||
/*********************************************************************
|
||||
Encoding using a VBR quality mode. The usable range is -.1
|
||||
(lowest quality, smallest file) to 1. (highest quality, largest file).
|
||||
Example quality mode .4: 44kHz stereo coupled, roughly 128kbps VBR
|
||||
|
||||
ret = vorbis_encode_init_vbr(&vi,2,44100,.4);
|
||||
ret = vorbis_encode_init_vbr(&vi,2,44100,.4);
|
||||
|
||||
---------------------------------------------------------------------
|
||||
---------------------------------------------------------------------
|
||||
|
||||
Encoding using an average bitrate mode (ABR).
|
||||
example: 44kHz stereo coupled, average 128kbps VBR
|
||||
Encoding using an average bitrate mode (ABR).
|
||||
example: 44kHz stereo coupled, average 128kbps VBR
|
||||
|
||||
ret = vorbis_encode_init(&vi,2,44100,-1,128000,-1);
|
||||
ret = vorbis_encode_init(&vi,2,44100,-1,128000,-1);
|
||||
|
||||
---------------------------------------------------------------------
|
||||
---------------------------------------------------------------------
|
||||
|
||||
Encode using a quality mode, but select that quality mode by asking for
|
||||
an approximate bitrate. This is not ABR, it is true VBR, but selected
|
||||
using the bitrate interface, and then turning bitrate management off:
|
||||
Encode using a quality mode, but select that quality mode by asking for
|
||||
an approximate bitrate. This is not ABR, it is true VBR, but selected
|
||||
using the bitrate interface, and then turning bitrate management off:
|
||||
|
||||
ret = ( vorbis_encode_setup_managed(&vi,2,44100,-1,128000,-1) ||
|
||||
vorbis_encode_ctl(&vi,OV_ECTL_RATEMANAGE2_SET,NULL) ||
|
||||
vorbis_encode_setup_init(&vi));
|
||||
ret = ( vorbis_encode_setup_managed(&vi,2,44100,-1,128000,-1) ||
|
||||
vorbis_encode_ctl(&vi,OV_ECTL_RATEMANAGE2_SET,NULL) ||
|
||||
vorbis_encode_setup_init(&vi));
|
||||
|
||||
*********************************************************************/
|
||||
*********************************************************************/
|
||||
|
||||
int ret = vorbis_encode_init_vbr(&vi_, sampleFormat_.channels, sampleFormat_.rate, quality);
|
||||
int ret = vorbis_encode_init_vbr(&vi_, sampleFormat_.channels, sampleFormat_.rate, quality);
|
||||
|
||||
/* do not continue if setup failed; this can happen if we ask for a
|
||||
mode that libVorbis does not support (eg, too low a bitrate, etc,
|
||||
will return 'OV_EIMPL') */
|
||||
/* do not continue if setup failed; this can happen if we ask for a
|
||||
mode that libVorbis does not support (eg, too low a bitrate, etc,
|
||||
will return 'OV_EIMPL') */
|
||||
|
||||
if (ret)
|
||||
throw SnapException("failed to init encoder");
|
||||
if (ret)
|
||||
throw SnapException("failed to init encoder");
|
||||
|
||||
/* add a comment */
|
||||
vorbis_comment_init(&vc_);
|
||||
vorbis_comment_add_tag(&vc_, "TITLE", "SnapStream");
|
||||
vorbis_comment_add_tag(&vc_, "VERSION", VERSION);
|
||||
vorbis_comment_add_tag(&vc_, "SAMPLE_FORMAT", sampleFormat_.getFormat().c_str());
|
||||
/* add a comment */
|
||||
vorbis_comment_init(&vc_);
|
||||
vorbis_comment_add_tag(&vc_, "TITLE", "SnapStream");
|
||||
vorbis_comment_add_tag(&vc_, "VERSION", VERSION);
|
||||
vorbis_comment_add_tag(&vc_, "SAMPLE_FORMAT", sampleFormat_.getFormat().c_str());
|
||||
|
||||
/* set up the analysis state and auxiliary encoding storage */
|
||||
vorbis_analysis_init(&vd_, &vi_);
|
||||
vorbis_block_init(&vd_, &vb_);
|
||||
/* set up the analysis state and auxiliary encoding storage */
|
||||
vorbis_analysis_init(&vd_, &vi_);
|
||||
vorbis_block_init(&vd_, &vb_);
|
||||
|
||||
/* set up our packet->stream encoder */
|
||||
/* pick a random serial number; that way we can more likely build
|
||||
chained streams just by concatenation */
|
||||
srand(time(NULL));
|
||||
ogg_stream_init(&os_, rand());
|
||||
/* set up our packet->stream encoder */
|
||||
/* pick a random serial number; that way we can more likely build
|
||||
chained streams just by concatenation */
|
||||
srand(time(NULL));
|
||||
ogg_stream_init(&os_, rand());
|
||||
|
||||
/* Vorbis streams begin with three headers; the initial header (with
|
||||
most of the codec setup parameters) which is mandated by the Ogg
|
||||
bitstream spec. The second header holds any comment fields. The
|
||||
third header holds the bitstream codebook. We merely need to
|
||||
make the headers, then pass them to libvorbis one at a time;
|
||||
libvorbis handles the additional Ogg bitstream constraints */
|
||||
/* Vorbis streams begin with three headers; the initial header (with
|
||||
most of the codec setup parameters) which is mandated by the Ogg
|
||||
bitstream spec. The second header holds any comment fields. The
|
||||
third header holds the bitstream codebook. We merely need to
|
||||
make the headers, then pass them to libvorbis one at a time;
|
||||
libvorbis handles the additional Ogg bitstream constraints */
|
||||
|
||||
ogg_packet header;
|
||||
ogg_packet header_comm;
|
||||
ogg_packet header_code;
|
||||
ogg_packet header;
|
||||
ogg_packet header_comm;
|
||||
ogg_packet header_code;
|
||||
|
||||
vorbis_analysis_headerout(&vd_, &vc_, &header, &header_comm, &header_code);
|
||||
ogg_stream_packetin(&os_, &header);
|
||||
ogg_stream_packetin(&os_, &header_comm);
|
||||
ogg_stream_packetin(&os_, &header_code);
|
||||
vorbis_analysis_headerout(&vd_, &vc_, &header, &header_comm, &header_code);
|
||||
ogg_stream_packetin(&os_, &header);
|
||||
ogg_stream_packetin(&os_, &header_comm);
|
||||
ogg_stream_packetin(&os_, &header_code);
|
||||
|
||||
/* This ensures the actual
|
||||
* audio data will start on a new page, as per spec
|
||||
*/
|
||||
size_t pos(0);
|
||||
headerChunk_.reset(new msg::CodecHeader("ogg"));
|
||||
while (true)
|
||||
{
|
||||
int result = ogg_stream_flush(&os_, &og_);
|
||||
if (result == 0)
|
||||
break;
|
||||
headerChunk_->payloadSize += og_.header_len + og_.body_len;
|
||||
headerChunk_->payload = (char*)realloc(headerChunk_->payload, headerChunk_->payloadSize);
|
||||
LOG(DEBUG) << "HeadLen: " << og_.header_len << ", bodyLen: " << og_.body_len << ", result: " << result << "\n";
|
||||
memcpy(headerChunk_->payload + pos, og_.header, og_.header_len);
|
||||
pos += og_.header_len;
|
||||
memcpy(headerChunk_->payload + pos, og_.body, og_.body_len);
|
||||
pos += og_.body_len;
|
||||
}
|
||||
/* This ensures the actual
|
||||
* audio data will start on a new page, as per spec
|
||||
*/
|
||||
size_t pos(0);
|
||||
headerChunk_.reset(new msg::CodecHeader("ogg"));
|
||||
while (true)
|
||||
{
|
||||
int result = ogg_stream_flush(&os_, &og_);
|
||||
if (result == 0)
|
||||
break;
|
||||
headerChunk_->payloadSize += og_.header_len + og_.body_len;
|
||||
headerChunk_->payload = (char*)realloc(headerChunk_->payload, headerChunk_->payloadSize);
|
||||
LOG(DEBUG) << "HeadLen: " << og_.header_len << ", bodyLen: " << og_.body_len << ", result: " << result << "\n";
|
||||
memcpy(headerChunk_->payload + pos, og_.header, og_.header_len);
|
||||
pos += og_.header_len;
|
||||
memcpy(headerChunk_->payload + pos, og_.body, og_.body_len);
|
||||
pos += og_.body_len;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
/***
|
||||
This file is part of snapcast
|
||||
Copyright (C) 2014-2018 Johannes Pohl
|
||||
Copyright (C) 2014-2019 Johannes Pohl
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
|
@ -19,36 +19,34 @@
|
|||
#ifndef OGG_ENCODER_H
|
||||
#define OGG_ENCODER_H
|
||||
#include "encoder.h"
|
||||
#include <vorbis/vorbisenc.h>
|
||||
#include <ogg/ogg.h>
|
||||
#include <vorbis/vorbisenc.h>
|
||||
|
||||
class OggEncoder : public Encoder
|
||||
{
|
||||
public:
|
||||
OggEncoder(const std::string& codecOptions = "");
|
||||
virtual void encode(const msg::PcmChunk* chunk);
|
||||
virtual std::string getAvailableOptions() const;
|
||||
virtual std::string getDefaultOptions() const;
|
||||
virtual std::string name() const;
|
||||
OggEncoder(const std::string& codecOptions = "");
|
||||
virtual void encode(const msg::PcmChunk* chunk);
|
||||
virtual std::string getAvailableOptions() const;
|
||||
virtual std::string getDefaultOptions() const;
|
||||
virtual std::string name() const;
|
||||
|
||||
protected:
|
||||
virtual void initEncoder();
|
||||
virtual void initEncoder();
|
||||
|
||||
private:
|
||||
ogg_stream_state os_; /// take physical pages, weld into a logical stream of packets
|
||||
ogg_page og_; /// one Ogg bitstream page. Vorbis packets are inside
|
||||
ogg_packet op_; /// one raw packet of data for decode
|
||||
ogg_stream_state os_; /// take physical pages, weld into a logical stream of packets
|
||||
ogg_page og_; /// one Ogg bitstream page. Vorbis packets are inside
|
||||
ogg_packet op_; /// one raw packet of data for decode
|
||||
|
||||
vorbis_info vi_; /// struct that stores all the static vorbis bitstream settings
|
||||
vorbis_comment vc_; /// struct that stores all the user comments
|
||||
vorbis_info vi_; /// struct that stores all the static vorbis bitstream settings
|
||||
vorbis_comment vc_; /// struct that stores all the user comments
|
||||
|
||||
vorbis_dsp_state vd_; /// central working state for the packet->PCM decoder
|
||||
vorbis_block vb_; /// local working space for packet->PCM decode
|
||||
vorbis_dsp_state vd_; /// central working state for the packet->PCM decoder
|
||||
vorbis_block vb_; /// local working space for packet->PCM decode
|
||||
|
||||
ogg_int64_t lastGranulepos_;
|
||||
ogg_int64_t lastGranulepos_;
|
||||
};
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
/***
|
||||
This file is part of snapcast
|
||||
Copyright (C) 2014-2018 Johannes Pohl
|
||||
Copyright (C) 2014-2019 Johannes Pohl
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
|
@ -16,54 +16,52 @@
|
|||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
***/
|
||||
|
||||
#include <memory>
|
||||
#include "common/endian.hpp"
|
||||
#include "pcmEncoder.h"
|
||||
#include "common/endian.hpp"
|
||||
#include <memory>
|
||||
|
||||
|
||||
#define ID_RIFF 0x46464952
|
||||
#define ID_WAVE 0x45564157
|
||||
#define ID_FMT 0x20746d66
|
||||
#define ID_FMT 0x20746d66
|
||||
#define ID_DATA 0x61746164
|
||||
|
||||
|
||||
PcmEncoder::PcmEncoder(const std::string& codecOptions) : Encoder(codecOptions)
|
||||
{
|
||||
headerChunk_.reset(new msg::CodecHeader("pcm"));
|
||||
headerChunk_.reset(new msg::CodecHeader("pcm"));
|
||||
}
|
||||
|
||||
|
||||
void PcmEncoder::encode(const msg::PcmChunk* chunk)
|
||||
{
|
||||
msg::PcmChunk* pcmChunk = new msg::PcmChunk(*chunk);
|
||||
listener_->onChunkEncoded(this, pcmChunk, pcmChunk->duration<chronos::msec>().count());
|
||||
msg::PcmChunk* pcmChunk = new msg::PcmChunk(*chunk);
|
||||
listener_->onChunkEncoded(this, pcmChunk, pcmChunk->duration<chronos::msec>().count());
|
||||
}
|
||||
|
||||
|
||||
void PcmEncoder::initEncoder()
|
||||
{
|
||||
headerChunk_->payloadSize = 44;
|
||||
headerChunk_->payload = (char*)malloc(headerChunk_->payloadSize);
|
||||
char* payload = headerChunk_->payload;
|
||||
assign(payload, SWAP_32(ID_RIFF));
|
||||
assign(payload + 4, SWAP_32(36));
|
||||
assign(payload + 8, SWAP_32(ID_WAVE));
|
||||
assign(payload + 12, SWAP_32(ID_FMT));
|
||||
assign(payload + 16, SWAP_32(16));
|
||||
assign(payload + 20, SWAP_16(1));
|
||||
assign(payload + 22, SWAP_16(sampleFormat_.channels));
|
||||
assign(payload + 24, SWAP_32(sampleFormat_.rate));
|
||||
assign(payload + 28, SWAP_32(sampleFormat_.rate * sampleFormat_.bits * sampleFormat_.channels / 8));
|
||||
assign(payload + 32, SWAP_16(sampleFormat_.channels * ((sampleFormat_.bits + 7) / 8)));
|
||||
assign(payload + 34, SWAP_16(sampleFormat_.bits));
|
||||
assign(payload + 36, SWAP_32(ID_DATA));
|
||||
assign(payload + 40, SWAP_32(0));
|
||||
headerChunk_->payloadSize = 44;
|
||||
headerChunk_->payload = (char*)malloc(headerChunk_->payloadSize);
|
||||
char* payload = headerChunk_->payload;
|
||||
assign(payload, SWAP_32(ID_RIFF));
|
||||
assign(payload + 4, SWAP_32(36));
|
||||
assign(payload + 8, SWAP_32(ID_WAVE));
|
||||
assign(payload + 12, SWAP_32(ID_FMT));
|
||||
assign(payload + 16, SWAP_32(16));
|
||||
assign(payload + 20, SWAP_16(1));
|
||||
assign(payload + 22, SWAP_16(sampleFormat_.channels));
|
||||
assign(payload + 24, SWAP_32(sampleFormat_.rate));
|
||||
assign(payload + 28, SWAP_32(sampleFormat_.rate * sampleFormat_.bits * sampleFormat_.channels / 8));
|
||||
assign(payload + 32, SWAP_16(sampleFormat_.channels * ((sampleFormat_.bits + 7) / 8)));
|
||||
assign(payload + 34, SWAP_16(sampleFormat_.bits));
|
||||
assign(payload + 36, SWAP_32(ID_DATA));
|
||||
assign(payload + 40, SWAP_32(0));
|
||||
}
|
||||
|
||||
|
||||
std::string PcmEncoder::name() const
|
||||
{
|
||||
return "pcm";
|
||||
return "pcm";
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
/***
|
||||
This file is part of snapcast
|
||||
Copyright (C) 2014-2018 Johannes Pohl
|
||||
Copyright (C) 2014-2019 Johannes Pohl
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
|
@ -24,22 +24,20 @@
|
|||
class PcmEncoder : public Encoder
|
||||
{
|
||||
public:
|
||||
PcmEncoder(const std::string& codecOptions = "");
|
||||
virtual void encode(const msg::PcmChunk* chunk);
|
||||
virtual std::string name() const;
|
||||
PcmEncoder(const std::string& codecOptions = "");
|
||||
virtual void encode(const msg::PcmChunk* chunk);
|
||||
virtual std::string name() const;
|
||||
|
||||
protected:
|
||||
virtual void initEncoder();
|
||||
|
||||
template<typename T>
|
||||
void assign(void* pointer, T val)
|
||||
{
|
||||
T* p = (T*)pointer;
|
||||
*p = val;
|
||||
}
|
||||
template <typename T>
|
||||
void assign(void* pointer, T val)
|
||||
{
|
||||
T* p = (T*)pointer;
|
||||
*p = val;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue