How to know if camera is already streaming
| Required Info | |
|---|---|
| Camera Model | D415/D435 |
| Firmware Version | 05.09.14.00 |
| Operating System & Version | Win 10 |
| Platform | PC |
| SDK Version | RealSense SDK 2.0 |
| Language | C++ |
Hello again, I was searching around the pipeline and device classes but can't find a busy flag or function. Now i'm thinking how to do that using the the functions for get frames that I have. But while, can you give some help?
Regards
[Realsense Customer Engineering Team Comment] Hi @DvdZmd,
How about checking if any error there like 'rs-color' example code?
I remember that I once tried to start a pipeline on a busy camera and the following snippets (cant' remember which one though, might be the first one) returned some kind of error of "busy state" for the related pipeline. Just add this after your int main(int argc, char * argv[]) try {}. It can be found in the rs-pointcloud example.
catch (const rs2::error & e)
{
std::cerr << "RealSense error calling " << e.get_failed_function() << "(" << e.get_failed_args() << "):\n " << e.what() << std::endl;
system("pause");
return EXIT_FAILURE;
}
catch (const std::exception& e)
{
std::cerr << e.what() << std::endl;
system("pause");
return EXIT_FAILURE;
}
[Realsense Customer Engineering Team Comment] Hi @DvdZmd,
Still need any further support for this topic?
Solved!
Before closing this issue, can you specify how did you solve your problem ? I'm looking to improve my own code, so I'm curious about your method. Without testing it (yet), the following snippet might be a cleaner way to check the state of a pipeline (from this document) instead of trying to use a pipeline and waiting for an error if it's busy :
/**
* Return the active device and streams profiles, used by the pipeline.
* The pipeline streams profiles are selected during \c start(). The method returns a valid result only when the pipeline is active -
* between calls to \c start() and \c stop().
* After \c stop() is called, the pipeline doesn't own the device, thus, the pipeline selected device may change in
* subsequent activations.
*
* \return The actual pipeline device and streams profile, which was successfully configured to the streaming device on start.
*/
pipeline_profile get_active_profile() const
{
rs2_error* e = nullptr;
auto p = std::shared_ptr<rs2_pipeline_profile>(
rs2_pipeline_get_active_profile(_pipeline.get(), &e),
rs2_delete_pipeline_profile);
error::handle(e);
return pipeline_profile(p);
}
Sure I didn't write the specific way to check if the camera is already streaming. But based in the main() here I made this checker that cover my needs.
#include <librealsense2/rsutil.h>
#include <librealsense2/rs.hpp> // Include RealSense Cross Platform API
/* Function calls to librealsense may raise errors of type rs_error*/
void check_errors()
{
rs2_error* e = 0;
// Create a context object. This object owns the handles to all connected realsense devices.
// The returned object should be released with rs2_delete_context(...)
rs2_context* ctx = rs2_create_context(RS2_API_VERSION, &e);
if (e)
logWriter(e);
/* Get a list of all the connected devices. */
// The returned object should be released with rs2_delete_device_list(...)
rs2_device_list* device_list = rs2_query_devices(ctx, &e);
if (e)
logWriter(e);
int dev_count = rs2_get_device_count(device_list, &e);
if (e)
logWriter(e);
if (dev_count == 0)
{
logLine = "There is no Intel RealSense camera conected";
logWriter(logLine);
return;
}
else
{
logLine = "There are " + std::to_string(dev_count);
logLine += " connected RealSense devices";
logWriter(logLine);
}
//TODO, hardcode for the first sensor, loop for every device detected
/*create this rs2::device hardware for make the hardware_reset, cannot use the rs2_device* dev
because rs2_device is a typedef and don't have the definition, so we can only use a pointer for
that type but not access the content*/
rs2::context cxt;
rs2::device_list devices = cxt.query_devices();
rs2::device hardware = devices[0];
// Get the first connected device
// The returned object should be released with rs2_delete_device(...)
rs2_device* dev = rs2_create_device(device_list, 0, &e);
m_intel->print_device_info(dev);
if (e)
logWriter(e);
// Create a pipeline to configure, start and stop camera streaming
// The returned object should be released with rs2_delete_pipeline(...)
rs2_pipeline* pipeline = rs2_create_pipeline(ctx, &e);
if (e)
logWriter(e);
// Create a config instance, used to specify hardware configuration
// The retunred object should be released with rs2_delete_config(...)
rs2_config* config = rs2_create_config(&e);
if (e)
logWriter(e);
// Request a specific configuration
rs2_config_enable_stream(config, RS2_STREAM_DEPTH, -1, m_DepthWidth, m_DepthHeight, RS2_FORMAT_RGB8, 30, &e);
if (e)
logWriter(e);
// Start the pipeline streaming
// The retunred object should be released with rs2_delete_pipeline_profile(...)
rs2_pipeline_profile* pipeline_profile = rs2_pipeline_start_with_config(pipeline, config, &e);
if (e)
logWriter(e);
rs2_delete_device(dev);
rs2_delete_device_list(device_list);
rs2_delete_context(ctx);
//Force a Hard reset because the app only will enter on this code if the pipeline.start() failed
hardware.hardware_reset();
return;
}
Thanks for the suggest @AntoineWefit I will add it to my code
Hello,
Is there a way to do (or a plan to provide) a proper API call to see if a camera is already in use?
I'm currently using exception handling to this end, and I'm not really happy with it:
def is_streaming(device_serial):
try:
c = rs.config()
c.enable_device(device_serial)
p = rs.pipeline()
p.start(c)
p.stop()
return False
except Exception:
return True