Merge pull request #2 from dbehnke/fix/audio-recorder-race-condition

fix(audio): Thread-safe AudioRecorder UUIDs to prevent 0-byte recordings
pull/23/head
Dave Behnke 1 month ago committed by GitHub
commit a74e7775a3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -3,6 +3,7 @@
#include <cstring>
#include <ctime>
#include <sstream>
#include <random>
// Opus settings for Voice 8kHz Mono
#define SAMPLE_RATE 8000
@ -41,10 +42,15 @@ std::string CAudioRecorder::Start(const std::string& directory)
std::lock_guard<std::mutex> lock(m_Mutex);
Cleanup();
// Use random_device for true randomness/seed
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<uint16_t> dist(0, 255);
// Generate UUIDv7 Filename
uint8_t uuid[16];
uint8_t rand_bytes[10];
for(int i=0; i<10; ++i) rand_bytes[i] = std::rand() & 0xFF; // Minimal entropy for now
for(int i=0; i<10; ++i) rand_bytes[i] = (uint8_t)dist(gen);
struct timespec ts;
clock_gettime(CLOCK_REALTIME, &ts);
@ -68,7 +74,7 @@ std::string CAudioRecorder::Start(const std::string& directory)
}
InitOpus();
InitOgg();
InitOgg(); // No longer calls srand
m_StartTime = std::time(nullptr);
m_TotalBytes = 0;
@ -91,8 +97,12 @@ void CAudioRecorder::InitOpus()
void CAudioRecorder::InitOgg()
{
// Initialize Ogg stream with random serial
std::srand(std::time(nullptr));
if (ogg_stream_init(&m_OggStream, std::rand()) != 0) {
// Use random_device for thread safety
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<int> dist; // full int range
if (ogg_stream_init(&m_OggStream, dist(gen)) != 0) {
std::cerr << "AudioRecorder: Failed to init Ogg stream" << std::endl;
return;
}
@ -169,6 +179,7 @@ void CAudioRecorder::WriteOggPage(bool flush)
m_File.write((const char*)m_OggPage.header, m_OggPage.header_len);
m_File.write((const char*)m_OggPage.body, m_OggPage.body_len);
m_TotalBytes += m_OggPage.header_len + m_OggPage.body_len;
m_File.flush();
}
}

@ -19,6 +19,10 @@
#include <string.h>
#include <iostream>
#include <map>
#include <ctime>
#include <iomanip>
#include "Global.h"
#include "DMRMMDVMClient.h"
#include "DMRMMDVMProtocol.h"
@ -677,7 +681,16 @@ bool CDmrmmdvmProtocol::IsValidDvFramePacket(const CIp &Ip, const CBuffer &Buffe
if ( !stream )
{
std::cout << std::showbase << std::hex;
std::cout << "Late entry DMR voice frame, creating DMR header for DMR stream ID " << ntohl(uiStreamId) << std::noshowbase << std::dec << " on " << Ip << std::endl;
static std::map<uint32_t, std::time_t> last_late_entry;
std::time_t now = std::time(nullptr);
uint32_t sid = ntohl(uiStreamId);
if (last_late_entry.find(sid) == last_late_entry.end() || (now - last_late_entry[sid]) > 60) {
std::cout << "Late entry DMR voice frame, creating DMR header for DMR stream ID " << std::hex << std::showbase << sid
<< std::noshowbase << std::dec << " on " << Ip
<< " (Suppressed for 60s)" << std::endl;
last_late_entry[sid] = now;
}
std::cout << std::noshowbase << std::dec;
uint8_t cmd;

@ -16,6 +16,10 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#include <iostream>
#include <map>
#include <ctime>
#include <iomanip>
#include "Defines.h"
#include "Global.h"
#include "Protocol.h"
@ -142,7 +146,17 @@ void CProtocol::OnDvFramePacketIn(std::unique_ptr<CDvFramePacket> &Frame, const
else
{
std::cout << std::showbase << std::hex;
std::cout << "Orphaned Frame with ID " << ntohs(Frame->GetStreamId()) << std::noshowbase << std::dec << " on " << *Ip << std::endl;
// Rate limit warnings: only log once every 60 seconds per stream ID
static std::map<uint16_t, std::time_t> last_warning;
std::time_t now = std::time(nullptr);
uint16_t sid = ntohs(Frame->GetStreamId());
if (last_warning.find(sid) == last_warning.end() || (now - last_warning[sid]) > 60) {
std::cout << "Orphaned Frame with ID " << std::hex << std::showbase << sid
<< std::noshowbase << std::dec << " on " << *Ip
<< " (Suppressed for 60s)" << std::endl;
last_warning[sid] = now;
}
Frame.reset();
}
//#endif

Loading…
Cancel
Save

Powered by TurnKey Linux.