-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathStreaming.cpp
417 lines (356 loc) · 11.9 KB
/
Streaming.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
#include <SoapySDR/Device.hpp>
#include <SoapySDR/Logger.hpp>
#include <SoapySDR/Formats.h>
#include <SoapySDR/Registry.hpp>
#include <SoapySDR/Formats.hpp>
#include <SoapySDR/Time.hpp>
#include <cstring>
#include "SoapyRX888.hpp"
/*******************************************************************
* Stream API
******************************************************************/
std::vector<std::string> SoapyRX888::getStreamFormats(const int direction, const size_t channel) const {
(void)channel; //unused
(void)direction; //unused
std::vector<std::string> formats;
formats.push_back(SOAPY_SDR_CF32);
formats.push_back(SOAPY_SDR_S16);
return formats;
}
std::string SoapyRX888::getNativeStreamFormat(const int direction, const size_t channel, double &fullScale) const {
(void)channel; //unused
//check that direction is SOAPY_SDR_RX
if (direction != SOAPY_SDR_RX) {
throw std::runtime_error("RX888 is RX only, use SOAPY_SDR_RX");
}
fullScale = 32768;
return SOAPY_SDR_S16;
}
SoapySDR::ArgInfoList SoapyRX888::getStreamArgsInfo(const int direction, const size_t channel) const {
(void)channel; //unused
//check that direction is SOAPY_SDR_RX
if (direction != SOAPY_SDR_RX) {
throw std::runtime_error("RX888 is RX only, use SOAPY_SDR_RX");
}
SoapySDR::ArgInfoList streamArgs;
SoapySDR::ArgInfo bufflenArg;
bufflenArg.key = "bufflen";
bufflenArg.value = std::to_string(DEFAULT_BUFFER_LENGTH);
bufflenArg.name = "Buffer Size";
bufflenArg.description = "Number of bytes per buffer, multiples of 512 only.";
bufflenArg.units = "bytes";
bufflenArg.type = SoapySDR::ArgInfo::INT;
streamArgs.push_back(bufflenArg);
SoapySDR::ArgInfo buffersArg;
buffersArg.key = "buffers";
buffersArg.value = std::to_string(DEFAULT_NUM_BUFFERS);
buffersArg.name = "Ring buffers";
buffersArg.description = "Number of buffers in the ring.";
buffersArg.units = "buffers";
buffersArg.type = SoapySDR::ArgInfo::INT;
streamArgs.push_back(buffersArg);
SoapySDR::ArgInfo asyncbuffsArg;
asyncbuffsArg.key = "asyncBuffs";
asyncbuffsArg.value = "0";
asyncbuffsArg.name = "Async buffers";
asyncbuffsArg.description = "Number of async usb buffers (advanced).";
asyncbuffsArg.units = "buffers";
asyncbuffsArg.type = SoapySDR::ArgInfo::INT;
streamArgs.push_back(asyncbuffsArg);
return streamArgs;
}
/*******************************************************************
* Async thread work
******************************************************************/
static void _rx_callback(unsigned char *buf, uint32_t len, void *ctx)
{
//printf("_rx_callback\n");
SoapyRX888 *self = static_cast<SoapyRX888*>(ctx);
self->rx_callback(buf, len);
}
void SoapyRX888::rx_async_operation(void)
{
//printf("rx_async_operation\n");
rx888_read_async(dev, &_rx_callback, this, asyncBuffs, bufferLength);
//printf("rx_async_operation done!\n");
}
void SoapyRX888::rx_callback(unsigned char *buf, uint32_t len)
{
//printf("_rx_callback %d _buf_head=%d, numBuffers=%d\n", len, _buf_head, _buf_tail);
// atomically add len to ticks but return the previous value
unsigned long long tick = ticks.fetch_add(len);
//overflow condition: the caller is not reading fast enough
if (_buf_count == numBuffers)
{
_overflowEvent = true;
return;
}
//copy into the buffer queue
auto &buff = _buffs[_buf_tail];
buff.tick = tick;
buff.data.resize(len);
std::memcpy(buff.data.data(), buf, len);
//increment the tail pointer
_buf_tail = (_buf_tail + 1) % numBuffers;
//increment buffers available under lock
//to avoid race in acquireReadBuffer wait
{
std::lock_guard<std::mutex> lock(_buf_mutex);
_buf_count++;
}
//notify readStream()
_buf_cond.notify_one();
}
/*******************************************************************
* Stream API
******************************************************************/
SoapySDR::Stream *SoapyRX888::setupStream(
const int direction,
const std::string &format,
const std::vector<size_t> &channels,
const SoapySDR::Kwargs &args)
{
if (direction != SOAPY_SDR_RX)
{
throw std::runtime_error("RX888 is RX only, use SOAPY_SDR_RX");
}
//check the channel configuration
if (channels.size() > 1 or (channels.size() > 0 and channels.at(0) != 0))
{
throw std::runtime_error("setupStream invalid channel selection");
}
//check the format
if (format == SOAPY_SDR_S16)
{
SoapySDR_log(SOAPY_SDR_INFO, "Using format S16.");
rxFormat = RX888_RX_FORMAT_INT16;
}
else if (format == SOAPY_SDR_CF32)
{
SoapySDR_log(SOAPY_SDR_INFO, "Using format CF32 with imaginary component set to zero.");
rxFormat = RX888_RX_FORMAT_FLOAT32;
}
else
{
throw std::runtime_error(
"setupStream invalid format '" + format
+ "' -- Only S16 and CF32 with imag=0 are supported by SoapyRX888 module.");
}
bufferLength = DEFAULT_BUFFER_LENGTH;
if (args.count("bufflen") != 0)
{
try
{
int bufferLength_in = std::stoi(args.at("bufflen"));
if (bufferLength_in > 0)
{
bufferLength = bufferLength_in;
}
}
catch (const std::invalid_argument &){}
}
SoapySDR_logf(SOAPY_SDR_DEBUG, "RX888 Using buffer length %d", bufferLength);
numBuffers = DEFAULT_NUM_BUFFERS;
if (args.count("buffers") != 0)
{
try
{
int numBuffers_in = std::stoi(args.at("buffers"));
if (numBuffers_in > 0)
{
numBuffers = numBuffers_in;
}
}
catch (const std::invalid_argument &){}
}
SoapySDR_logf(SOAPY_SDR_DEBUG, "RX888 Using %d buffers", numBuffers);
asyncBuffs = 0;
if (args.count("asyncBuffs") != 0)
{
try
{
int asyncBuffs_in = std::stoi(args.at("asyncBuffs"));
if (asyncBuffs_in > 0)
{
asyncBuffs = asyncBuffs_in;
}
}
catch (const std::invalid_argument &){}
}
//clear async fifo counts
_buf_tail = 0;
_buf_count = 0;
_buf_head = 0;
//allocate buffers
_buffs.resize(numBuffers);
for (auto &buff : _buffs) buff.data.reserve(bufferLength);
for (auto &buff : _buffs) buff.data.resize(bufferLength);
return (SoapySDR::Stream *) this;
}
void SoapyRX888::closeStream(SoapySDR::Stream *stream)
{
this->deactivateStream(stream, 0, 0);
_buffs.clear();
}
size_t SoapyRX888::getStreamMTU(SoapySDR::Stream *stream) const
{
(void) stream; //unused
return bufferLength / BYTES_PER_SAMPLE;
}
int SoapyRX888::activateStream(
SoapySDR::Stream *stream,
const int flags,
const long long timeNs,
const size_t numElems)
{
(void) stream; //unused
(void) timeNs; //unused
(void) numElems; //unused
if (flags != 0) return SOAPY_SDR_NOT_SUPPORTED;
resetBuffer = true;
bufferedElems = 0;
//start the async thread
if (not _rx_async_thread.joinable())
{
//rx888_reset_buffer(dev);
_rx_async_thread = std::thread(&SoapyRX888::rx_async_operation, this);
}
return 0;
}
int SoapyRX888::deactivateStream(SoapySDR::Stream *stream, const int flags, const long long timeNs)
{
(void) timeNs; //unused
(void) stream; //unused
if (flags != 0) return SOAPY_SDR_NOT_SUPPORTED;
if (_rx_async_thread.joinable())
{
rx888_cancel_async(dev);
_rx_async_thread.join();
}
return 0;
}
int SoapyRX888::readStream(
SoapySDR::Stream *stream,
void * const *buffs,
const size_t numElems,
int &flags,
long long &timeNs,
const long timeoutUs)
{
//drop remainder buffer on reset
if (resetBuffer and bufferedElems != 0)
{
bufferedElems = 0;
this->releaseReadBuffer(stream, _currentHandle);
}
//this is the user's buffer for channel 0
void *buff0 = buffs[0];
//are elements left in the buffer? if not, do a new read.
if (bufferedElems == 0)
{
int ret = this->acquireReadBuffer(stream, _currentHandle, (const void **)&_currentBuff, flags, timeNs, timeoutUs);
if (ret < 0) return ret;
bufferedElems = ret;
}
//otherwise just update return time to the current tick count
else
{
flags |= SOAPY_SDR_HAS_TIME;
timeNs = SoapySDR::ticksToTimeNs(bufTicks, sampleRate);
}
size_t returnedElems = std::min(bufferedElems, numElems);
//convert into user's buff0
if (rxFormat == RX888_RX_FORMAT_INT16)
{
int16_t *itarget = reinterpret_cast<int16_t*>(buff0);
for (size_t i = 0; i < returnedElems; i++)
{
itarget[i] = *((int16_t*) &_currentBuff[2 * i]);
}
}
else if (rxFormat == RX888_RX_FORMAT_FLOAT32)
{
float *ftarget = reinterpret_cast<float*>(buff0);
for (size_t i = 0; i < returnedElems; i++)
{
int16_t val = *((int16_t*) &_currentBuff[2 * i]);
ftarget[i * 2] = float(val) / 32768.0f; // scale int16_t to [-1, 1] range.
ftarget[i * 2 + 1] = 0.0f; // imaginary part is zero
}
}
//bump variables for next call into readStream
bufferedElems -= returnedElems;
_currentBuff += returnedElems * 2; // Each int16_t sample consists of 2 int8_t bytes
bufTicks += returnedElems; //for the next call to readStream if there is a remainder
//return number of elements written to buff0
if (bufferedElems != 0) flags |= SOAPY_SDR_MORE_FRAGMENTS;
else this->releaseReadBuffer(stream, _currentHandle);
return returnedElems;
}
/*******************************************************************
* Direct buffer access API
******************************************************************/
size_t SoapyRX888::getNumDirectAccessBuffers(SoapySDR::Stream *stream)
{
(void)stream; //unused
return _buffs.size();
}
int SoapyRX888::getDirectAccessBufferAddrs(SoapySDR::Stream *stream, const size_t handle, void **buffs)
{
(void)stream; //unused
buffs[0] = static_cast<void*>(_buffs[handle].data.data());
return 0;
}
int SoapyRX888::acquireReadBuffer(
SoapySDR::Stream *stream,
size_t &handle,
const void **buffs,
int &flags,
long long &timeNs,
const long timeoutUs)
{
(void)stream; //unused
//reset is issued by various settings
//to drain old data out of the queue
if (resetBuffer)
{
//drain all buffers from the fifo
_buf_head = (_buf_head + _buf_count.exchange(0)) % numBuffers;
resetBuffer = false;
_overflowEvent = false;
}
//handle overflow from the rx callback thread
if (_overflowEvent)
{
//drain the old buffers from the fifo
_buf_head = (_buf_head + _buf_count.exchange(0)) % numBuffers;
_overflowEvent = false;
SoapySDR::log(SOAPY_SDR_SSI, "O");
return SOAPY_SDR_OVERFLOW;
}
//wait for a buffer to become available
if (_buf_count == 0)
{
std::unique_lock <std::mutex> lock(_buf_mutex);
_buf_cond.wait_for(lock, std::chrono::microseconds(timeoutUs), [this]{return _buf_count != 0;});
if (_buf_count == 0) return SOAPY_SDR_TIMEOUT;
}
//extract handle and buffer
handle = _buf_head;
_buf_head = (_buf_head + 1) % numBuffers;
bufTicks = _buffs[handle].tick;
timeNs = SoapySDR::ticksToTimeNs(_buffs[handle].tick, sampleRate);
buffs[0] = (void *)_buffs[handle].data.data();
flags = SOAPY_SDR_HAS_TIME;
//return number available
return _buffs[handle].data.size() / BYTES_PER_SAMPLE;
}
void SoapyRX888::releaseReadBuffer(
SoapySDR::Stream *stream,
const size_t handle)
{
(void)stream; //unused
(void)handle; //unused
//TODO this wont handle out of order releases
_buf_count--;
}