Skip to content

Commit 08024cc

Browse files
committed
Add support for more precision on buffer lengths; necessary for high bandwidth streams where 1 sec is too long.
1 parent 8d2d46c commit 08024cc

File tree

6 files changed

+61
-11
lines changed

6 files changed

+61
-11
lines changed

include/lsl/common.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,21 @@ typedef enum {
150150
_lsl_error_code_maxval = 0x7f000000
151151
} lsl_error_code_t;
152152

153+
/// Flags for outlet_ex and inlet_ex
154+
typedef enum {
155+
/// Keep legacy behavior: max_buffered / max_buflen is in seconds; use asynch transfer.
156+
transp_default = 0,
157+
158+
/// The supplied max_buf value is in samples.
159+
transp_bufsize_samples = 1,
160+
161+
/// The supplied max_buf should be scaled by 0.001.
162+
transp_bufsize_thousandths = 2,
163+
164+
// prevent compilers from assuming an instance fits in a single byte
165+
_lsl_transport_options_maxval = 0x7f000000
166+
} lsl_transport_options_t;
167+
153168
/// Return an explanation for the last error
154169
extern LIBLSL_C_API const char *lsl_last_error(void);
155170

include/lsl/inlet.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@
3939
* @return A newly created lsl_inlet handle or NULL in the event that an error occurred.
4040
*/
4141
extern LIBLSL_C_API lsl_inlet lsl_create_inlet(lsl_streaminfo info, int32_t max_buflen, int32_t max_chunklen, int32_t recover);
42+
/** @copydoc lsl_create_inlet()
43+
* @param flags An integer that is the result of bitwise OR'ing one or more options from
44+
* #lsl_transport_options_t together (e.g., #transp_bufsize_samples)
45+
*/
46+
extern LIBLSL_C_API lsl_inlet lsl_create_inlet_ex(lsl_streaminfo info, int32_t max_buflen, int32_t max_chunklen, int32_t recover, uint32_t flags);
4247

4348
/**
4449
* Destructor.

include/lsl/outlet.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@
3434
* @return A newly created lsl_outlet handle or NULL in the event that an error occurred.
3535
*/
3636
extern LIBLSL_C_API lsl_outlet lsl_create_outlet(lsl_streaminfo info, int32_t chunk_size, int32_t max_buffered);
37+
/** @copydoc lsl_create_outlet()
38+
* @param flags An integer that is the result of bitwise OR'ing one or more options from
39+
* #lsl_transport_options_t together (e.g., #transp_bufsize_samples|#transp_bufsize_thousandths)
40+
*/
41+
extern LIBLSL_C_API lsl_outlet lsl_create_outlet_ex(lsl_streaminfo info, int32_t chunk_size, int32_t max_buffered, uint32_t flags);
3742

3843
/**
3944
* Destroy an outlet.

include/lsl_cpp.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -396,10 +396,10 @@ class stream_outlet {
396396
* @param max_buffered Optionally the maximum amount of data to buffer (in seconds if there is a
397397
* nominal sampling rate, otherwise x100 in samples). The default is 6 minutes of data.
398398
*/
399-
stream_outlet(const stream_info &info, int32_t chunk_size = 0, int32_t max_buffered = 360)
399+
stream_outlet(const stream_info &info, int32_t chunk_size = 0, int32_t max_buffered = 360, uint32_t flags = transp_default)
400400
: channel_count(info.channel_count()),
401401
sample_rate(info.nominal_srate()),
402-
obj(lsl_create_outlet(info.handle().get(), chunk_size, max_buffered), &lsl_destroy_outlet) {}
402+
obj(lsl_create_outlet_ex(info.handle().get(), chunk_size, max_buffered, flags), &lsl_destroy_outlet) {}
403403

404404
// ========================================
405405
// === Pushing a sample into the outlet ===
@@ -900,9 +900,9 @@ class stream_inlet {
900900
* lsl::lost_error if the stream's source is lost (e.g., due to an app or computer crash).
901901
*/
902902
stream_inlet(const stream_info &info, int32_t max_buflen = 360, int32_t max_chunklen = 0,
903-
bool recover = true)
903+
bool recover = true, uint32_t flags = transp_default)
904904
: channel_count(info.channel_count()),
905-
obj(lsl_create_inlet(info.handle().get(), max_buflen, max_chunklen, recover), &lsl_destroy_inlet) {}
905+
obj(lsl_create_inlet_ex(info.handle().get(), max_buflen, max_chunklen, recover, flags), &lsl_destroy_inlet) {}
906906

907907
/// Return a shared pointer to pass to C-API functions that aren't wrapped yet
908908
///

src/lsl_inlet_c.cpp

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,23 @@ extern "C" {
1414

1515
using namespace lsl;
1616

17+
LIBLSL_C_API lsl_inlet lsl_create_inlet_ex(
18+
lsl_streaminfo info, int32_t max_buflen, int32_t max_chunklen, int32_t recover, uint32_t flags) {
19+
int32_t buf_samples;
20+
if (flags & transp_bufsize_samples)
21+
buf_samples = max_buflen;
22+
else if (info->nominal_srate() == LSL_IRREGULAR_RATE)
23+
buf_samples = max_buflen * 100;
24+
else
25+
buf_samples = (int)(info->nominal_srate() * max_buflen);
26+
if (flags & transp_bufsize_thousandths)
27+
buf_samples /= 1000;
28+
buf_samples = (buf_samples > 0) ? buf_samples : 1;
29+
return create_object_noexcept<stream_inlet_impl>(*info, buf_samples, max_chunklen, recover != 0);
30+
}
1731
LIBLSL_C_API lsl_inlet lsl_create_inlet(
1832
lsl_streaminfo info, int32_t max_buflen, int32_t max_chunklen, int32_t recover) {
19-
return create_object_noexcept<stream_inlet_impl>(*info,
20-
(info->nominal_srate() ? (int)(info->nominal_srate() * max_buflen) : max_buflen * 100) + 1,
21-
max_chunklen, recover != 0);
33+
return lsl_create_inlet_ex(info, max_buflen, max_chunklen, recover, transp_default);
2234
}
2335

2436
LIBLSL_C_API void lsl_destroy_inlet(lsl_inlet in) {

src/lsl_outlet_c.cpp

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,25 @@ extern "C" {
1515
using namespace lsl;
1616

1717
// boilerplate wrapper code
18+
LIBLSL_C_API lsl_outlet lsl_create_outlet_ex(
19+
lsl_streaminfo info, int32_t chunk_size, int32_t max_buffered, uint32_t flags) {
20+
int32_t buf_samples;
21+
if (flags & transp_bufsize_samples)
22+
buf_samples = max_buffered;
23+
else if (info->nominal_srate() == LSL_IRREGULAR_RATE)
24+
buf_samples = max_buffered * 100;
25+
else
26+
buf_samples = info->nominal_srate() * max_buffered;
27+
if (flags & transp_bufsize_thousandths)
28+
buf_samples /= 1000;
29+
buf_samples = (buf_samples > 0) ? buf_samples : 1;
30+
return create_object_noexcept<stream_outlet_impl>(
31+
*info, chunk_size, buf_samples);
32+
}
33+
1834
LIBLSL_C_API lsl_outlet lsl_create_outlet(
1935
lsl_streaminfo info, int32_t chunk_size, int32_t max_buffered) {
20-
double buftime = info->nominal_srate();
21-
if (buftime <= 0) buftime = 100;
22-
return create_object_noexcept<stream_outlet_impl>(
23-
*info, chunk_size, static_cast<int>(buftime * max_buffered));
36+
return lsl_create_outlet_ex(info, chunk_size, max_buffered, transp_default);
2437
}
2538

2639
LIBLSL_C_API void lsl_destroy_outlet(lsl_outlet out) {

0 commit comments

Comments
 (0)