From 94f46dc4998cfeafff82e98caa995e29b33d3995 Mon Sep 17 00:00:00 2001 From: Andy Taylor Date: Fri, 13 Nov 2026 17:20:41 +0000 Subject: [PATCH] AMBEd: skip stream ids still in use when opening a stream Stream ids were handed out round robin from 1 to the stream limit without checking whether the id was still taken. Each id has its own UDP port (LISTEN_PORT + id), so when a long stream still held the id the round robin came back to, binding its port failed with "Error opening socket" and the request was rejected, although other ids were free. xlxd then opened the stream without transcoding. OpenStream now tries each id once, starting after the last one used, and takes the first id not held by an open stream. Patch by Patrice, F4GEN. --- ambed/ccontroller.cpp | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/ambed/ccontroller.cpp b/ambed/ccontroller.cpp index 93da3c1..da50d35 100644 --- a/ambed/ccontroller.cpp +++ b/ambed/ccontroller.cpp @@ -221,9 +221,33 @@ CStream *CController::OpenStream(const CCallsign &Callsign, const CIp &Ip, uint8 return NULL; } + // pick the next stream id whose port is free: a long stream can still + // hold the id the round robin comes back to, and reusing it fails with + // "Error opening socket" although other ids are free + bool found = false; + for ( int n = 0; (n < (int)m_uiMaxStreams) && !found; n++ ) + { + m_uiLastStreamId = (m_uiLastStreamId + 1); + m_uiLastStreamId = (m_uiLastStreamId > m_uiMaxStreams) ? 1 : m_uiLastStreamId; + found = true; + Lock(); + for ( size_t i = 0; i < m_Streams.size(); i++ ) + { + if ( m_Streams[i]->GetId() == m_uiLastStreamId ) + { + found = false; + break; + } + } + Unlock(); + } + if ( !found ) + { + std::cout << "No free stream id, rejecting" << std::endl; + return NULL; + } + // create a new stream - m_uiLastStreamId = (m_uiLastStreamId + 1); - m_uiLastStreamId = (m_uiLastStreamId > m_uiMaxStreams) ? 1 : m_uiLastStreamId; stream = new CStream(m_uiLastStreamId, Callsign, Ip, CodecIn, CodecOut1, CodecOut2, CodecOut3); if ( stream->Init(g_Config.GetListenPort()+m_uiLastStreamId) ) {