labstreaminglayer/liblsl-Matlab

blocking call to pull_sample become unresponsive when no data available

Closed this issue · 5 comments

Setting:
In the pull_sample function, the timeout was set to 1 second.
Then, deliberately let no data stream coming from the sensor.

Expected:
The pull_sample function should return to the calling function whenever the time limit (1 second) is exceeded should there are no data available in the open_stream que.

Problem:

The application stuck infinitely in the lsl_pull_sample_d function despite the timeout (1 second) has exceeded.

Question
Either I mistakenly understand the role of the timeout var or there is something that I am missing in the sccn setup? Is there any ways to return to the calling function whenever there is no available data upon certain period of time?

Appreciate for any help to address this problem.

Tested using MATLAB 2019a and MATLAB 2020a. Evaluated also using latest commit from Github

This feature probably isn't included in the Matlab wrapper. I will try to get around to fixing it up in the next few days. It is actually quite difficult to deal with wrapping C++ libraries in Matlab, so please be patient.

Hmmm, I've looked at the backend for this and my theory that the timeout is somehow not implemented in the Matlab wrapper is definitely wrong. I don't see how this could be the case. The wrapper calls the function from the library and the timeout flag is most definitely implemented there. Can you please show me your code that uses pull_sample? The function should return after the value assigned to timeout.

Further update, I ran my own test and the timeout argument is working just fine. I modified ReceiveData.m so that the calls to pull_sample had a timeout value of 1.0 seconds. I ran the code on Matlab 2019b, halted the inlet stream, and every second the loop printed an empty line. This means that after 1 second, the calls to pull_sample return nothing. Then I restarted the data outlet and the samples streamed as before.

Full code:

%% instantiate the library
disp('Loading the library...');
lib = lsl_loadlib();

% resolve a stream...
disp('Resolving an EEG stream...');
result = {};
while isempty(result)
    result = lsl_resolve_byprop(lib,'type','EEG'); end

% create a new inlet
disp('Opening an inlet...');
inlet = lsl_inlet(result{1});

disp('Now receiving data...');
while true
    % get data from the inlet
    [vec,ts] = inlet.pull_sample(1.0);
    % and display it
    fprintf('%.2f\t',vec);
    fprintf('%.5f\n',ts);
end

Hi @dmedine , thanks for the quick response.
I try to run your code example but MATLAB (2019a & 2020a) stuck at result = lsl_resolve_byprop(lib,'type','EEG'); end .

To make the troubleshoot easier, Im using what I believe to be your current liblsl-Matlab setup which can be found from the commit here and downloadable via the link..

I believe all the setup is correct since I can run the example ReceiveStringMarkers.m without any hiccup on both MATLAB 2019a & 2020a.

Also, I know by using actual device implementation, the pull_sample has successfully return the sensor output as long as there is/are data available in the open_stream que except for the opposite.
The pull_sample in my code were assigned as
[vec,ts] = inlet_88.pull_sample(3,[]);

I will try to address your issues one by one.

If you don't have an outlet broadcasting that is of 'type' 'EEG', you will have to change the arguments in lsl_resolve_byprop to the appropriate values.

Good that you are using the latest version.

Receiving markers is a different business than receiving a continuous data stream because markers come sporadically whereas device data comes regularly according to the sample rate.

Of course pull sample will not give you data if data is not available from the connected outlet. You can't get blood out of a turnip.

I am going to close this issue because the timeout does function properly in the Matlab bindings to the library. You can check out the LSL slack channel for more general discussions of how to interact with LSL. There is a link at the bottom of the repository landing page (https://github.com/sccn/labstreaminglayer) under the 'Getting Help' section.