clang-format new sources

This commit is contained in:
Marcus Weseloh 2024-05-30 16:08:49 +02:00 committed by Johannes Pohl
parent ff67399162
commit d30e1a5ea3
2 changed files with 102 additions and 90 deletions

View file

@ -41,7 +41,8 @@ static constexpr auto LOG_TAG = "JackStream";
void float_to_s32(char* dst, jack_default_audio_sample_t* src, unsigned long nsamples, unsigned long dst_skip) void float_to_s32(char* dst, jack_default_audio_sample_t* src, unsigned long nsamples, unsigned long dst_skip)
{ {
while (nsamples--) { while (nsamples--)
{
// float to S32 conversion // float to S32 conversion
double clipped = fmin(1.0f, fmax((double)(*src), -1.0f)); double clipped = fmin(1.0f, fmax((double)(*src), -1.0f));
double scaled = clipped * 2147483647.0; double scaled = clipped * 2147483647.0;
@ -56,14 +57,20 @@ void float_to_s24(char *dst, jack_default_audio_sample_t *src, unsigned long nsa
{ {
int32_t tmp; int32_t tmp;
while (nsamples--) { while (nsamples--)
{
// float to S24 conversion // float to S24 conversion
if (*src <= -1.0f) { if (*src <= -1.0f)
{
tmp = -8388607; tmp = -8388607;
} else if (*src >= 1.0f) { }
else if (*src >= 1.0f)
{
tmp = 8388607; tmp = 8388607;
} else { }
else
{
tmp = lrintf(*src * 8388607.0); tmp = lrintf(*src * 8388607.0);
} }
@ -79,14 +86,20 @@ void float_to_s24(char *dst, jack_default_audio_sample_t *src, unsigned long nsa
void float_to_s16(char* dst, jack_default_audio_sample_t* src, unsigned long nsamples, unsigned long dst_skip) void float_to_s16(char* dst, jack_default_audio_sample_t* src, unsigned long nsamples, unsigned long dst_skip)
{ {
while (nsamples--) { while (nsamples--)
{
// float to S16 conversion // float to S16 conversion
if (*src <= -1.0f) { if (*src <= -1.0f)
{
*((int16_t*)dst) = -32767; *((int16_t*)dst) = -32767;
} else if (*src >= 1.0f) { }
else if (*src >= 1.0f)
{
*((int16_t*)dst) = 32767; *((int16_t*)dst) = 32767;
} else { }
else
{
*((int16_t*)dst) = lrintf(*src * 32767.0); *((int16_t*)dst) = lrintf(*src * 32767.0);
} }
@ -177,14 +190,17 @@ void JackStream::stop()
*/ */
void JackStream::tryConnect() void JackStream::tryConnect()
{ {
try { try
if (!openJackConnection()) { {
if (!openJackConnection())
{
LOG(WARNING, LOG_TAG) << "Jack connection failed, trying again in 5 seconds\n"; LOG(WARNING, LOG_TAG) << "Jack connection failed, trying again in 5 seconds\n";
wait(read_timer_, 5s, [this] { tryConnect(); }); wait(read_timer_, 5s, [this] { tryConnect(); });
return; return;
} }
if (!createJackPorts()) { if (!createJackPorts())
{
LOG(ERROR, LOG_TAG) << "Failed to create Jack ports, trying again in 5 seconds\n"; LOG(ERROR, LOG_TAG) << "Failed to create Jack ports, trying again in 5 seconds\n";
closeJackConnection(); closeJackConnection();
wait(read_timer_, 5s, [this] { tryConnect(); }); wait(read_timer_, 5s, [this] { tryConnect(); });
@ -193,7 +209,8 @@ void JackStream::tryConnect()
PcmStream::start(); PcmStream::start();
} }
catch (exception& e) { catch (exception& e)
{
LOG(ERROR, LOG_TAG) << "Error during Jack connection: " << e.what() << "\n"; LOG(ERROR, LOG_TAG) << "Error during Jack connection: " << e.what() << "\n";
stop(); stop();
} }
@ -206,28 +223,26 @@ bool JackStream::openJackConnection()
jack_options_t options = (jack_options_t)(JackNoStartServer | JackServerName); jack_options_t options = (jack_options_t)(JackNoStartServer | JackServerName);
client_ = jack_client_open(name_.c_str(), options, nullptr, serverName); client_ = jack_client_open(name_.c_str(), options, nullptr, serverName);
if (client_ == NULL) { if (client_ == NULL)
{
return false; return false;
} }
LOG(INFO, LOG_TAG) << "Connected Jack client " << jack_get_client_name(client_) << "\n"; LOG(INFO, LOG_TAG) << "Connected Jack client " << jack_get_client_name(client_) << "\n";
jack_nframes_t jack_sample_rate = jack_get_sample_rate(client_); jack_nframes_t jack_sample_rate = jack_get_sample_rate(client_);
if (jack_sample_rate != sampleFormat_.rate()) { if (jack_sample_rate != sampleFormat_.rate())
throw SnapException( {
"Jack streams must match the sample rate of the Jack server. " throw SnapException("Jack streams must match the sample rate of the Jack server. "
"The server sample rate is " + cpt::to_string(jack_sample_rate) + "." "The server sample rate is " +
); cpt::to_string(jack_sample_rate) + ".");
} }
jack_time_t connect_time = jack_get_time(); jack_time_t connect_time = jack_get_time();
jackConnectTime_ = std::chrono::steady_clock::now(); jackConnectTime_ = std::chrono::steady_clock::now();
jackTimeAdjust_ = chrono::duration_cast<chrono::microseconds>( jackTimeAdjust_ = chrono::duration_cast<chrono::microseconds>(jackConnectTime_.time_since_epoch()).count() - connect_time;
jackConnectTime_.time_since_epoch()
).count() - connect_time;
LOG(DEBUG, LOG_TAG) << name_ << ": Jack server time adjustment is " LOG(DEBUG, LOG_TAG) << name_ << ": Jack server time adjustment is " << jackTimeAdjust_ << "\n";
<< jackTimeAdjust_ << "\n";
jack_set_process_callback(client_, processCallback, this); jack_set_process_callback(client_, processCallback, this);
jack_on_shutdown(client_, jackShutdown, this); jack_on_shutdown(client_, jackShutdown, this);
@ -236,8 +251,7 @@ bool JackStream::openJackConnection()
int err = jack_activate(client_); int err = jack_activate(client_);
if (err) if (err)
{ {
LOG(ERROR, LOG_TAG) << "Failed to activate Jack client " LOG(ERROR, LOG_TAG) << "Failed to activate Jack client " << name_ << ": " << err << "\n";
<< name_ << ": " << err << "\n";
closeJackConnection(); closeJackConnection();
return false; return false;
} }
@ -247,7 +261,8 @@ bool JackStream::openJackConnection()
bool JackStream::createJackPorts() bool JackStream::createJackPorts()
{ {
if (ports_.size() > 0) { if (ports_.size() > 0)
{
throw SnapException("Jack ports already created!"); throw SnapException("Jack ports already created!");
} }
@ -258,8 +273,7 @@ bool JackStream::createJackPorts()
for (int i = 0; i < channelCount; ++i) for (int i = 0; i < channelCount; ++i)
{ {
std::string portName = "input_" + std::to_string(i); std::string portName = "input_" + std::to_string(i);
jack_port_t *port = jack_port_register(client_, portName.c_str(), jack_port_t* port = jack_port_register(client_, portName.c_str(), JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
if (port == NULL) if (port == NULL)
{ {
LOG(ERROR, LOG_TAG) << name_ << ": failed to register port " << portName << "\n"; LOG(ERROR, LOG_TAG) << name_ << ": failed to register port " << portName << "\n";
@ -283,8 +297,7 @@ int JackStream::readJackBuffers(jack_nframes_t nframes)
jack_time_t next_usecs; jack_time_t next_usecs;
float period_usecs; float period_usecs;
int err = jack_get_cycle_times(client_, &current_frames, &current_usecs, int err = jack_get_cycle_times(client_, &current_frames, &current_usecs, &next_usecs, &period_usecs);
&next_usecs, &period_usecs);
if (err) if (err)
{ {
LOG(ERROR, LOG_TAG) << "Unable to get Jack cycle times: " << err << "\n"; LOG(ERROR, LOG_TAG) << "Unable to get Jack cycle times: " << err << "\n";
@ -315,9 +328,7 @@ int JackStream::readJackBuffers(jack_nframes_t nframes)
connectedPorts++; connectedPorts++;
} }
jack_default_audio_sample_t *buf = static_cast<jack_default_audio_sample_t *>( jack_default_audio_sample_t* buf = static_cast<jack_default_audio_sample_t*>(jack_port_get_buffer(port, nframes));
jack_port_get_buffer(port, nframes)
);
if (buf == NULL) if (buf == NULL)
{ {
@ -346,9 +357,7 @@ int JackStream::readJackBuffers(jack_nframes_t nframes)
{ {
// We use the (adjusted) Jack server time as chunk time, that way all Jack // We use the (adjusted) Jack server time as chunk time, that way all Jack
// streams will play simultaneously (hopefully!) // streams will play simultaneously (hopefully!)
tvEncodedChunk_ = std::chrono::time_point<std::chrono::steady_clock>( tvEncodedChunk_ = std::chrono::time_point<std::chrono::steady_clock>(static_cast<chrono::microseconds>(current_usecs + jackTimeAdjust_));
static_cast<chrono::microseconds>(current_usecs + jackTimeAdjust_)
);
// TODO: should we chunkRead() in a separate thread? // TODO: should we chunkRead() in a separate thread?
chunkRead(*chunk_); chunkRead(*chunk_);
@ -359,7 +368,8 @@ int JackStream::readJackBuffers(jack_nframes_t nframes)
void JackStream::closeJackConnection() void JackStream::closeJackConnection()
{ {
if (client_ == NULL) { if (client_ == NULL)
{
return; return;
} }
@ -373,16 +383,19 @@ void JackStream::closeJackConnection()
void JackStream::onJackPortRegistration(jack_port_id_t port_id, int registered) void JackStream::onJackPortRegistration(jack_port_id_t port_id, int registered)
{ {
if (!doAutoConnect_ || !registered) { if (!doAutoConnect_ || !registered)
{
return; return;
} }
jack_port_t* port = jack_port_by_id(client_, port_id); jack_port_t* port = jack_port_by_id(client_, port_id);
if (port == NULL) { if (port == NULL)
{
return; return;
} }
if (jack_port_is_mine(client_, port)) { if (jack_port_is_mine(client_, port))
{
return; return;
} }
@ -391,10 +404,10 @@ void JackStream::onJackPortRegistration(jack_port_id_t port_id, int registered)
void JackStream::autoConnectPorts() void JackStream::autoConnectPorts()
{ {
const char **portNames = jack_get_ports(client_, autoConnectRegex_.c_str(), const char** portNames = jack_get_ports(client_, autoConnectRegex_.c_str(), NULL, JackPortIsOutput);
NULL, JackPortIsOutput);
if (portNames == NULL) { if (portNames == NULL)
{
return; return;
} }
@ -409,13 +422,14 @@ void JackStream::autoConnectPorts()
continue; continue;
} }
if (!jack_port_connected_to(ports_[portIdx], portNames[nameIdx])) { if (!jack_port_connected_to(ports_[portIdx], portNames[nameIdx]))
{
const char* localPortName = jack_port_name(ports_[portIdx]); const char* localPortName = jack_port_name(ports_[portIdx]);
int err = jack_connect(client_, portNames[nameIdx], localPortName); int err = jack_connect(client_, portNames[nameIdx], localPortName);
if (err != 0) { if (err != 0)
LOG(ERROR, LOG_TAG) << "Unable to autoconnect " << localPortName {
<< " to " << portNames[nameIdx] << "(Error: " << err << ")\n"; LOG(ERROR, LOG_TAG) << "Unable to autoconnect " << localPortName << " to " << portNames[nameIdx] << "(Error: " << err << ")\n";
} }
} }
portIdx++; portIdx++;
@ -460,4 +474,3 @@ void JackStream::jackPortRegistration(jack_port_id_t port_id, int registered, vo
} }
} // namespace streamreader } // namespace streamreader

View file

@ -25,8 +25,8 @@
#include <server/server_settings.hpp> #include <server/server_settings.hpp>
// 3rd party headers // 3rd party headers
#include <boost/asio/spawn.hpp>
#include <boost/asio/io_context.hpp> #include <boost/asio/io_context.hpp>
#include <boost/asio/spawn.hpp>
#include <boost/asio/steady_timer.hpp> #include <boost/asio/steady_timer.hpp>
@ -96,4 +96,3 @@ protected:
}; };
} // namespace streamreader } // namespace streamreader