Program Listing for File rs_pipeline.hpp

Return to documentation for file (/tmp/ws/src/librealsense2/include/librealsense2/hpp/rs_pipeline.hpp)

// License: Apache 2.0. See LICENSE file in root directory.
// Copyright(c) 2017 Intel Corporation. All Rights Reserved.

#ifndef LIBREALSENSE_RS2_PIPELINE_HPP
#define LIBREALSENSE_RS2_PIPELINE_HPP

#include "rs_types.hpp"
#include "rs_frame.hpp"
#include "rs_context.hpp"

namespace rs2
{
    class pipeline_profile
    {
    public:

        pipeline_profile() : _pipeline_profile(nullptr) {}

        std::vector<stream_profile> get_streams() const
        {
            std::vector<stream_profile> results;

            rs2_error* e = nullptr;
            std::shared_ptr<rs2_stream_profile_list> list(
                rs2_pipeline_profile_get_streams(_pipeline_profile.get(), &e),
                rs2_delete_stream_profiles_list);
            error::handle(e);

            auto size = rs2_get_stream_profiles_count(list.get(), &e);
            error::handle(e);

            for (auto i = 0; i < size; i++)
            {
                stream_profile profile(rs2_get_stream_profile(list.get(), i, &e));
                error::handle(e);
                results.push_back(profile);
            }

            return results;
        }

        stream_profile get_stream(rs2_stream stream_type, int stream_index = -1) const
        {
            for (auto&& s : get_streams())
            {
                if (s.stream_type() == stream_type &&  (stream_index == -1 || s.stream_index() == stream_index))
                {
                    return s;
                }
            }
            throw std::runtime_error("Profile does not contain the requested stream");
        }

        device get_device() const
        {
            rs2_error* e = nullptr;
            std::shared_ptr<rs2_device> dev(
                rs2_pipeline_profile_get_device(_pipeline_profile.get(), &e),
                rs2_delete_device);

            error::handle(e);

            return device(dev);
        }

        operator bool() const
        {
            return _pipeline_profile != nullptr;
        }

        explicit operator std::shared_ptr<rs2_pipeline_profile>() { return _pipeline_profile; }
        pipeline_profile(std::shared_ptr<rs2_pipeline_profile> profile) :
            _pipeline_profile(profile){}
    private:

        std::shared_ptr<rs2_pipeline_profile> _pipeline_profile;
        friend class config;
        friend class pipeline;
    };

    class pipeline;

    class config
    {
    public:
        config()
        {
            rs2_error* e = nullptr;
            _config = std::shared_ptr<rs2_config>(
                rs2_create_config(&e),
                rs2_delete_config);
            error::handle(e);
        }

        void enable_stream(rs2_stream stream_type, int stream_index, int width, int height, rs2_format format = RS2_FORMAT_ANY, int framerate = 0)
        {
            rs2_error* e = nullptr;
            rs2_config_enable_stream(_config.get(), stream_type, stream_index, width, height, format, framerate, &e);
            error::handle(e);
        }

        void enable_stream(rs2_stream stream_type, int stream_index = -1)
        {
            enable_stream(stream_type, stream_index, 0, 0, RS2_FORMAT_ANY, 0);
        }

        void enable_stream(rs2_stream stream_type, int width, int height, rs2_format format = RS2_FORMAT_ANY, int framerate = 0)
        {
            enable_stream(stream_type, -1, width, height, format, framerate);
        }

        void enable_stream(rs2_stream stream_type, rs2_format format, int framerate = 0)
        {
            enable_stream(stream_type, -1, 0, 0, format, framerate);
        }

        void enable_stream(rs2_stream stream_type, int stream_index, rs2_format format, int framerate = 0)
        {
            enable_stream(stream_type, stream_index, 0, 0, format, framerate);
        }

        void enable_all_streams()
        {
            rs2_error* e = nullptr;
            rs2_config_enable_all_stream(_config.get(), &e);
            error::handle(e);
        }

        void enable_device(const std::string& serial)
        {
            rs2_error* e = nullptr;
            rs2_config_enable_device(_config.get(), serial.c_str(), &e);
            error::handle(e);
        }

        void enable_device_from_file(const std::string& file_name, bool repeat_playback = true)
        {
            rs2_error* e = nullptr;
            rs2_config_enable_device_from_file_repeat_option(_config.get(), file_name.c_str(), repeat_playback, &e);
            error::handle(e);
        }

        void enable_record_to_file(const std::string& file_name)
        {
            rs2_error* e = nullptr;
            rs2_config_enable_record_to_file(_config.get(), file_name.c_str(), &e);
            error::handle(e);
        }

        void disable_stream(rs2_stream stream, int index = -1)
        {
            rs2_error* e = nullptr;
            rs2_config_disable_indexed_stream(_config.get(), stream, index, &e);
            error::handle(e);
        }

        void disable_all_streams()
        {
            rs2_error* e = nullptr;
            rs2_config_disable_all_streams(_config.get(), &e);
            error::handle(e);
        }

        pipeline_profile resolve(std::shared_ptr<rs2_pipeline> p) const
        {
            rs2_error* e = nullptr;
            auto profile = std::shared_ptr<rs2_pipeline_profile>(
                rs2_config_resolve(_config.get(), p.get(), &e),
                rs2_delete_pipeline_profile);

            error::handle(e);
            return pipeline_profile(profile);
        }

        bool can_resolve(std::shared_ptr<rs2_pipeline> p) const
        {
            rs2_error* e = nullptr;
            int res = rs2_config_can_resolve(_config.get(), p.get(), &e);
            error::handle(e);
            return res != 0;
        }

        std::shared_ptr<rs2_config> get() const
        {
            return _config;
        }
        explicit operator std::shared_ptr<rs2_config>() const
        {
            return _config;
        }

        config(std::shared_ptr<rs2_config> cfg) : _config(cfg) {}
    private:
        std::shared_ptr<rs2_config> _config;
    };

    class pipeline
    {
    public:

        pipeline(context ctx = context())
        {
            rs2_error* e = nullptr;
            _pipeline = std::shared_ptr<rs2_pipeline>(
                rs2_create_pipeline(ctx._context.get(), &e),
                rs2_delete_pipeline);
            error::handle(e);
        }

        pipeline_profile start()
        {
            rs2_error* e = nullptr;
            auto p = std::shared_ptr<rs2_pipeline_profile>(
                rs2_pipeline_start(_pipeline.get(), &e),
                rs2_delete_pipeline_profile);

            error::handle(e);
            return pipeline_profile(p);
        }

        pipeline_profile start(const config& config)
        {
            rs2_error* e = nullptr;
            auto p = std::shared_ptr<rs2_pipeline_profile>(
                rs2_pipeline_start_with_config(_pipeline.get(), config.get().get(), &e),
                rs2_delete_pipeline_profile);

            error::handle(e);
            return pipeline_profile(p);
        }

        template<class S>
        pipeline_profile start(S callback)
        {
            rs2_error* e = nullptr;
            auto p = std::shared_ptr<rs2_pipeline_profile>(
                rs2_pipeline_start_with_callback_cpp(_pipeline.get(), new frame_callback<S>(callback), &e),
                rs2_delete_pipeline_profile);

            error::handle(e);
            return pipeline_profile(p);
        }

        template<class S>
        pipeline_profile start(const config& config, S callback)
        {
            rs2_error* e = nullptr;
            auto p = std::shared_ptr<rs2_pipeline_profile>(
                rs2_pipeline_start_with_config_and_callback_cpp(_pipeline.get(), config.get().get(), new frame_callback<S>(callback), &e),
                rs2_delete_pipeline_profile);

            error::handle(e);
            return pipeline_profile(p);
        }

        void stop()
        {
            rs2_error* e = nullptr;
            rs2_pipeline_stop(_pipeline.get(), &e);
            error::handle(e);
        }

        frameset wait_for_frames(unsigned int timeout_ms = RS2_DEFAULT_TIMEOUT) const
        {
            rs2_error* e = nullptr;
            frame f(rs2_pipeline_wait_for_frames(_pipeline.get(), timeout_ms, &e));
            error::handle(e);

            return frameset(f);
        }

        bool poll_for_frames(frameset* f) const
        {
            if (!f)
            {
                throw std::invalid_argument("null frameset");
            }
            rs2_error* e = nullptr;
            rs2_frame* frame_ref = nullptr;
            auto res = rs2_pipeline_poll_for_frames(_pipeline.get(), &frame_ref, &e);
            error::handle(e);

            if (res) *f = frameset(frame(frame_ref));
            return res > 0;
        }

        bool try_wait_for_frames(frameset* f, unsigned int timeout_ms = RS2_DEFAULT_TIMEOUT) const
        {
            if (!f)
            {
                throw std::invalid_argument("null frameset");
            }
            rs2_error* e = nullptr;
            rs2_frame* frame_ref = nullptr;
            auto res = rs2_pipeline_try_wait_for_frames(_pipeline.get(), &frame_ref, timeout_ms, &e);
            error::handle(e);
            if (res) *f = frameset(frame(frame_ref));
            return res > 0;
        }

        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);
        }

        operator std::shared_ptr<rs2_pipeline>() const
        {
            return _pipeline;
        }
        explicit pipeline(std::shared_ptr<rs2_pipeline> ptr) : _pipeline(ptr) {}

    private:
        std::shared_ptr<rs2_pipeline> _pipeline;
        friend class config;
    };
}
#endif // LIBREALSENSE_RS2_PROCESSING_HPP