SDRAngel  4.11.5
Developer docs for <a href="https://github.com/f4exb/sdrangel">SDRangel<\a>, an Open Source Qt5 / OpenGL 3.0+ SDR and signal analyzer frontend to various hardware.
remoteoutputthread.cpp
Go to the documentation of this file.
1 // Copyright (C) 2017 Edouard Griffiths, F4EXB //
3 // //
4 // This program is free software; you can redistribute it and/or modify //
5 // it under the terms of the GNU General Public License as published by //
6 // the Free Software Foundation as version 3 of the License, or //
7 // (at your option) any later version. //
8 // //
9 // This program is distributed in the hope that it will be useful, //
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of //
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
12 // GNU General Public License V3 for more details. //
13 // //
14 // You should have received a copy of the GNU General Public License //
15 // along with this program. If not, see <http://www.gnu.org/licenses/>. //
17 
18 #include <stdio.h>
19 #include <errno.h>
20 #include <assert.h>
21 #include <algorithm>
22 #include <QDebug>
23 
24 #include "dsp/samplesourcefifo.h"
25 #include "util/timeutil.h"
26 #include "remoteoutputthread.h"
27 
29  QThread(parent),
30  m_running(false),
31  m_samplesChunkSize(0),
32  m_sampleFifo(sampleFifo),
33  m_samplesCount(0),
34  m_chunkCorrection(0),
35  m_samplerate(0),
36  m_throttlems(REMOTEOUTPUT_THROTTLE_MS),
37  m_maxThrottlems(50),
38  m_throttleToggle(false)
39 {
40 }
41 
43 {
44  if (m_running) {
45  stopWork();
46  }
47 }
48 
50 {
51  qDebug() << "RemoteOutputThread::startWork: ";
53  m_maxThrottlems = 0;
54  m_startWaitMutex.lock();
55  m_elapsedTimer.start();
56  start();
57  while(!m_running)
58  m_startWaiter.wait(&m_startWaitMutex, 100);
59  m_startWaitMutex.unlock();
60 }
61 
63 {
64  qDebug() << "RemoteOutputThread::stopWork";
65  m_running = false;
66  wait();
68 }
69 
71 {
72  if (samplerate != m_samplerate)
73  {
74  qDebug() << "RemoteOutputThread::setSamplerate:"
75  << " new:" << samplerate
76  << " old:" << m_samplerate;
77 
78  bool wasRunning = false;
79 
80  if (m_running)
81  {
82  stopWork();
83  wasRunning = true;
84  }
85 
86  // resize sample FIFO
87  if (m_sampleFifo) {
88  m_sampleFifo->resize(samplerate); // 1s buffer
89  }
90 
91  m_samplerate = samplerate;
92  m_samplesChunkSize = (m_samplerate * m_throttlems) / 1000;
93  m_udpSinkFEC.setSampleRate(m_samplerate);
94 
95  if (wasRunning) {
96  startWork();
97  }
98  }
99 }
100 
102 {
103  m_running = true;
104  m_startWaiter.wakeAll();
105 
106  while(m_running) // actual work is in the tick() function
107  {
108  sleep(1);
109  }
110 
111  m_running = false;
112 }
113 
114 void RemoteOutputThread::connectTimer(const QTimer& timer)
115 {
116  qDebug() << "RemoteOutputThread::connectTimer";
117  connect(&timer, SIGNAL(timeout()), this, SLOT(tick()));
118 }
119 
121 {
122  if (m_running)
123  {
124  qint64 throttlems = m_elapsedTimer.restart();
125 
126  if (throttlems != m_throttlems)
127  {
128  m_throttlems = throttlems;
132  }
133 
134  SampleVector::iterator readUntil;
135 
136  m_sampleFifo->readAdvance(readUntil, m_samplesChunkSize); // pull samples
137  SampleVector::iterator beginRead = readUntil - m_samplesChunkSize;
139 
140  m_udpSinkFEC.write(beginRead, m_samplesChunkSize);
141  }
142 }
143 
145 {
146  ts_usecs = TimeUtil::nowus();
147  return m_samplesCount;
148 }
uint32_t getSamplesCount(uint64_t &ts_usecs) const
void setSamplerate(int samplerate)
void stop()
Definition: udpsinkfec.cpp:64
void write(const SampleVector::iterator &begin, uint32_t sampleChunkSize)
Definition: udpsinkfec.cpp:112
unsigned int uint32_t
Definition: rtptypes_win.h:46
volatile bool m_running
void start()
Definition: udpsinkfec.cpp:57
void readAdvance(SampleVector::iterator &readUntil, unsigned int nbSamples)
SampleSourceFifo * m_sampleFifo
void resize(uint32_t size)
static uint64_t nowus()
returns the current epoch in microseconds
Definition: timeutil.cpp:30
QWaitCondition m_startWaiter
#define REMOTEOUTPUT_THROTTLE_MS
QElapsedTimer m_elapsedTimer
RemoteOutputThread(SampleSourceFifo *sampleFifo, QObject *parent=0)
void connectTimer(const QTimer &timer)
void setSampleRate(uint32_t sampleRate)
Definition: udpsinkfec.cpp:94
unsigned __int64 uint64_t
Definition: rtptypes_win.h:48