kopia lustrzana https://github.com/OpenRTX/OpenRTX
Removed exceptions from linux input stream driver
rodzic
07394cc8a0
commit
724e3196b4
|
@ -25,11 +25,9 @@
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <stdexcept>
|
#include <memory>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
#include <tuple>
|
|
||||||
#include <utility>
|
|
||||||
|
|
||||||
streamId gNextAvailableStreamId = 0;
|
streamId gNextAvailableStreamId = 0;
|
||||||
|
|
||||||
|
@ -45,11 +43,13 @@ class InputStream
|
||||||
: m_run_thread(true), m_func_running(false)
|
: m_run_thread(true), m_func_running(false)
|
||||||
{
|
{
|
||||||
if (bufLength % 2)
|
if (bufLength % 2)
|
||||||
throw std::runtime_error("Invalid bufLength: " +
|
{
|
||||||
std::to_string(bufLength));
|
fprintf(stderr, "InputStream error: invalid bufLength %lu\n",
|
||||||
m_db_ready[0] = m_db_ready[1] = false;
|
bufLength);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
changeId();
|
m_db_ready[0] = m_db_ready[1] = false;
|
||||||
|
|
||||||
std::string sourceString;
|
std::string sourceString;
|
||||||
switch (source)
|
switch (source)
|
||||||
|
@ -66,19 +66,36 @@ class InputStream
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
m_fp = fopen((sourceString + ".raw").c_str(), "rb");
|
m_fp = fopen((sourceString + ".raw").c_str(), "rb");
|
||||||
if (!m_fp)
|
if (!m_fp)
|
||||||
throw std::runtime_error("Cannot open: " + sourceString + ".raw");
|
{
|
||||||
|
fprintf(stderr, "InputStream error: cannot open: %s.raw\n",
|
||||||
|
sourceString.c_str());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
fseek(m_fp, 0, SEEK_END);
|
fseek(m_fp, 0, SEEK_END);
|
||||||
m_size = ftell(m_fp);
|
m_size = ftell(m_fp);
|
||||||
fseek(m_fp, 0, SEEK_SET);
|
fseek(m_fp, 0, SEEK_SET);
|
||||||
if (m_size % 2 || m_size == 0)
|
if (m_size % 2 || m_size == 0)
|
||||||
throw std::runtime_error("Invalid file: " + sourceString + ".raw");
|
{
|
||||||
|
fprintf(stderr, "InputStream error: invalid file: %s.raw\n",
|
||||||
|
sourceString.c_str());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
m_valid = true;
|
||||||
|
|
||||||
|
changeId();
|
||||||
setStreamData(priority, buf, bufLength, mode, sampleRate);
|
setStreamData(priority, buf, bufLength, mode, sampleRate);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool isValid() const
|
||||||
|
{
|
||||||
|
return m_valid;
|
||||||
|
}
|
||||||
|
|
||||||
~InputStream()
|
~InputStream()
|
||||||
{
|
{
|
||||||
stopThread();
|
stopThread();
|
||||||
|
@ -88,6 +105,8 @@ class InputStream
|
||||||
|
|
||||||
dataBlock_t getDataBlock()
|
dataBlock_t getDataBlock()
|
||||||
{
|
{
|
||||||
|
if (!m_valid) return {nullptr, 0};
|
||||||
|
|
||||||
switch (m_mode)
|
switch (m_mode)
|
||||||
{
|
{
|
||||||
case BufMode::BUF_LINEAR:
|
case BufMode::BUF_LINEAR:
|
||||||
|
@ -147,6 +166,8 @@ class InputStream
|
||||||
BufMode mode,
|
BufMode mode,
|
||||||
uint32_t sampleRate)
|
uint32_t sampleRate)
|
||||||
{
|
{
|
||||||
|
if (!m_valid) return;
|
||||||
|
|
||||||
stopThread();
|
stopThread();
|
||||||
m_run_thread = true; // set it as runnable again
|
m_run_thread = true; // set it as runnable again
|
||||||
|
|
||||||
|
@ -171,15 +192,17 @@ class InputStream
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
FILE* m_fp;
|
bool m_valid = false;
|
||||||
uint64_t m_size;
|
FILE* m_fp = nullptr;
|
||||||
|
uint64_t m_size = 0;
|
||||||
|
|
||||||
streamId m_id;
|
streamId m_id;
|
||||||
AudioPriority m_prio;
|
AudioPriority m_prio;
|
||||||
BufMode m_mode;
|
BufMode m_mode;
|
||||||
uint32_t m_sampleRate;
|
uint32_t m_sampleRate = 0;
|
||||||
|
|
||||||
stream_sample_t* m_buf;
|
stream_sample_t* m_buf = nullptr;
|
||||||
size_t m_bufLength;
|
size_t m_bufLength = 0;
|
||||||
|
|
||||||
size_t m_db_curwrite = 0;
|
size_t m_db_curwrite = 0;
|
||||||
size_t m_db_curread = 0;
|
size_t m_db_curread = 0;
|
||||||
|
@ -276,7 +299,7 @@ class InputStream
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
std::map<AudioSource, InputStream> gOpenStreams;
|
std::map<AudioSource, std::unique_ptr<InputStream>> gOpenStreams;
|
||||||
|
|
||||||
streamId inputStream_start(const enum AudioSource source,
|
streamId inputStream_start(const enum AudioSource source,
|
||||||
const enum AudioPriority priority,
|
const enum AudioPriority priority,
|
||||||
|
@ -289,32 +312,34 @@ streamId inputStream_start(const enum AudioSource source,
|
||||||
if (it != gOpenStreams.end())
|
if (it != gOpenStreams.end())
|
||||||
{
|
{
|
||||||
auto& inputStream = it->second;
|
auto& inputStream = it->second;
|
||||||
if (inputStream.priority() >= priority) return -1;
|
if (inputStream->priority() >= priority) return -1;
|
||||||
|
|
||||||
inputStream.changeId();
|
inputStream->changeId();
|
||||||
inputStream.setStreamData(priority, buf, bufLength, mode, sampleRate);
|
inputStream->setStreamData(priority, buf, bufLength, mode, sampleRate);
|
||||||
|
|
||||||
return inputStream.id();
|
return inputStream->id();
|
||||||
}
|
}
|
||||||
|
|
||||||
// New stream: allocate directly in the std::map
|
auto stream = std::make_unique<InputStream>(source, priority, buf,
|
||||||
auto res = gOpenStreams.emplace(
|
bufLength, mode, sampleRate);
|
||||||
std::piecewise_construct, std::forward_as_tuple(source),
|
|
||||||
std::forward_as_tuple(source, priority, buf, bufLength, mode,
|
|
||||||
sampleRate));
|
|
||||||
|
|
||||||
if (!res.second) return -1;
|
if (!stream->isValid()) return -1;
|
||||||
|
|
||||||
return res.first->second.id();
|
const auto id = stream->id();
|
||||||
|
|
||||||
|
// New stream, move it into the map
|
||||||
|
gOpenStreams[source] = std::move(stream);
|
||||||
|
|
||||||
|
return id;
|
||||||
}
|
}
|
||||||
|
|
||||||
dataBlock_t inputStream_getData(streamId id)
|
dataBlock_t inputStream_getData(streamId id)
|
||||||
{
|
{
|
||||||
InputStream* stream = nullptr;
|
InputStream* stream = nullptr;
|
||||||
for (auto& i : gOpenStreams)
|
for (auto& i : gOpenStreams)
|
||||||
if (i.second.id() == id)
|
if (i.second->id() == id)
|
||||||
{
|
{
|
||||||
stream = &i.second;
|
stream = i.second.get();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -328,7 +353,7 @@ void inputStream_stop(streamId id)
|
||||||
AudioSource src;
|
AudioSource src;
|
||||||
bool found = false;
|
bool found = false;
|
||||||
for (auto& i : gOpenStreams)
|
for (auto& i : gOpenStreams)
|
||||||
if (i.second.id() == id)
|
if (i.second->id() == id)
|
||||||
{
|
{
|
||||||
found = true;
|
found = true;
|
||||||
src = i.first;
|
src = i.first;
|
||||||
|
|
Ładowanie…
Reference in New Issue