Skip to content

Commit

Permalink
Merge pull request #20 from zshahaf/master
Browse files Browse the repository at this point in the history
Seek working in ui. Playback status changed callback
  • Loading branch information
dorodnic authored and GitHub Enterprise committed Jul 25, 2017
2 parents 744ccde + 4e0a428 commit 017aa7c
Show file tree
Hide file tree
Showing 15 changed files with 338 additions and 151 deletions.
18 changes: 16 additions & 2 deletions CMake/realsense.def
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ EXPORTS
rs2_create_recording_context
rs2_create_mock_context
rs2_get_context_time

rs2_context_add_device
rs2_context_remove_device

rs2_query_devices
rs2_get_device_count
rs2_delete_device_list
Expand Down Expand Up @@ -94,6 +96,7 @@ EXPORTS
rs2_get_librealsense_exception_type
rs2_exception_type_to_string
rs2_extension_type_to_string
rs2_playback_status_to_string
rs2_log_severity_to_string

rs2_stream_to_string
Expand Down Expand Up @@ -161,4 +164,15 @@ EXPORTS
rs2_create_record_device
rs2_create_playback_device
rs2_record_device_pause
rs2_record_device_resume
rs2_record_device_resume

rs2_playback_device_get_file_path
rs2_playback_get_duration
rs2_playback_seek
rs2_playback_get_position
rs2_playback_device_resume
rs2_playback_device_pause
rs2_playback_device_set_real_time
rs2_playback_device_is_real_time
rs2_playback_device_set_status_changed_callback
rs2_playback_device_get_current_status
72 changes: 71 additions & 1 deletion common/model-views.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1058,7 +1058,7 @@ namespace rs2
ImGui::PopItemWidth();
}

void device_model::draw_device_details(device& dev)
void device_model::draw_device_details(device& dev, context& ctx)
{
for (auto i = 0; i < RS2_CAMERA_INFO_COUNT; i++)
{
Expand All @@ -1084,6 +1084,76 @@ namespace rs2
}
}
}
if(dev.is<playback>())
{
auto p = dev.as<playback>();
if (ImGui::SmallButton("Remove Device"))
{
for (auto&& subdevice : subdevices)
{
subdevice->stop();
}
ctx.unload_device(p.file_name());
}
else
{
int64_t total_duration = p.get_duration().count();
static int seek_pos = 0;
static int64_t progress = 0;
progress = p.get_position();

double part = (1.0 * progress) / total_duration;
seek_pos = static_cast<int>(std::max(0.0, std::min(part, 1.0)) * 100);

if(seek_pos != 0 && p.current_status() == RS2_PLAYBACK_STATUS_STOPPED)
{
seek_pos = 0;
}
int prev_seek_progress = seek_pos;

ImGui::SeekSlider("Seek Bar", &seek_pos);
if (prev_seek_progress != seek_pos)
{
//Seek was dragged
auto duration_db =
std::chrono::duration_cast<std::chrono::duration<double,
std::nano>>(p.get_duration());
auto single_percent = duration_db.count() / 100;
auto seek_time = std::chrono::duration<double, std::nano>(seek_pos * single_percent);
p.seek(std::chrono::duration_cast<std::chrono::nanoseconds>(seek_time));
}
// if (ImGui::CollapsingHeader("Playback Options"))
// {
// static bool is_paused = false;
// if (!is_paused && ImGui::Button("Pause"))
// {
// p.pause();
// for (auto&& sub : model.subdevices)
// {
// if (sub->streaming) sub->pause();
// }
// is_paused = !is_paused;
// }
// if (ImGui::IsItemHovered())
// {
// ImGui::SetTooltip("Pause playback");
// }
// if (is_paused && ImGui::Button("Resume"))
// {
// p.resume();
// for (auto&& sub : model.subdevices)
// {
// if (sub->streaming) sub->resume();
// }
// is_paused = !is_paused;
// }
// if (ImGui::IsItemHovered())
// {
// ImGui::SetTooltip("Continue playback");
// }
// }
}
}
}

std::map<rs2_stream, rect> device_model::calc_layout(float x0, float y0, float width, float height)
Expand Down
3 changes: 2 additions & 1 deletion common/model-views.h
Original file line number Diff line number Diff line change
Expand Up @@ -339,10 +339,11 @@ namespace rs2
{
public:
device_model(){}

void reset();
explicit device_model(device& dev, std::string& error_message);
bool draw_combo_box(const std::vector<std::string>& device_names, int& new_index);
void draw_device_details(device& dev);
void draw_device_details(device& dev, context& ctx);
std::map<rs2_stream, rect> calc_layout(float x0, float y0, float width, float height);
void upload_frame(frame&& f);
void start_recording(device& dev, const std::string& path, std::string& error_message);
Expand Down
37 changes: 36 additions & 1 deletion include/librealsense/rs2.h
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,15 @@ typedef enum rs2_extension
RS2_EXTENSION_COUNT
} rs2_extension;

typedef enum rs2_playback_status
{
RS2_PLAYBACK_STATUS_UNKNOWN, /**< Unknown state */
RS2_PLAYBACK_STATUS_PLAYING, /**< One or more sensors were started, playback is reading and raising data */
RS2_PLAYBACK_STATUS_PAUSED, /**< One or more sensors were started, but playback paused reading and paused raising data*/
RS2_PLAYBACK_STATUS_STOPPED, /**< All sensors were stopped, or playback has ended (all data was read). This is the initial playback status*/
RS2_PLAYBACK_STATUS_COUNT
} rs2_playback_status;

/** \brief Video stream intrinsics */
typedef struct rs2_intrinsics
{
Expand Down Expand Up @@ -293,12 +302,14 @@ typedef struct rs2_device_serializer rs2_device_serializer;
typedef struct rs2_source rs2_source;
typedef struct rs2_processing_block rs2_processing_block;
typedef struct rs2_frame_processor_callback rs2_frame_processor_callback;
typedef struct rs2_playback_status_changed_callback rs2_playback_status_changed_callback;

typedef void (*rs2_frame_callback_ptr)(rs2_frame*, void*);
typedef void (*rs2_frame_processor_callback_ptr)(rs2_frame**, int, rs2_source*, void*);
typedef void (*rs2_notification_callback_ptr)(rs2_notification*, void*);
typedef void (*rs2_devices_changed_callback_ptr)(rs2_device_list*, rs2_device_list*, void*);
typedef void (*rs2_log_callback_ptr)(rs2_log_severity min_severity, const char* message, void* user);
typedef void (*rs2_playback_status_changed_callback_ptr)(rs2_playback_status);

typedef double rs2_time_t; /**< Timestamp format. units are milliseconds */
typedef long long rs2_metadata_t; /**< Metadata attribute type is defined as 64 bit signed integer*/
Expand Down Expand Up @@ -1029,6 +1040,7 @@ const char * rs2_log_severity_to_string (rs2_log_severity info);
const char * rs2_visual_preset_to_string (rs2_ivcam_visual_preset preset);
const char * rs2_exception_type_to_string (rs2_exception_type type);
const char * rs2_extension_type_to_string (rs2_extension type);
const char * rs2_playback_status_to_string (rs2_playback_status status);

void rs2_log_to_console(rs2_log_severity min_severity, rs2_error ** error);
void rs2_log_to_file(rs2_log_severity min_severity, const char * file_path, rs2_error ** error);
Expand Down Expand Up @@ -1166,17 +1178,40 @@ void rs2_playback_device_pause(const rs2_device* device, rs2_error** error);
* In non real time mode, playback will wait for each callback to finish handling the data before
* reading the next frame. In this mode no frames will be dropped, and the application controls the
* frame rate of the playback (according to the callback handler duration).
* \param real_time Indicates if real time is requested, 0 means false, otherwise true
* \param[in] device A playback device
* \param[in] real_time Indicates if real time is requested, 0 means false, otherwise true
* \param[out] error If non-null, receives any error that occurs during this call, otherwise, errors are ignored
* \return True on successfully setting the requested mode
*/
void rs2_playback_device_set_real_time(const rs2_device* device, int real_time, rs2_error** error);

/**
* Indicates if playback is in real time mode or non real time
* \param[in] device A playback device
* \param[out] error If non-null, receives any error that occurs during this call, otherwise, errors are ignored
* \return True iff playback is in real time mode. 0 means false, otherwise true
*/
int rs2_playback_device_is_real_time(const rs2_device* device, rs2_error** error);

/**
* Register to receive callback from playback device upon its status changes
*
* Callbacks are invoked from the reading thread, any heaving processing in the callback handler will affect
* the reading thread and may cause frame drops\ high latency
* \param[in] device A playback device
* \param[in] callback A callback handler that will be invoked when the playback status changes
* \param[out] error If non-null, receives any error that occurs during this call, otherwise, errors are ignored
*/
void rs2_playback_device_set_status_changed_callback(const rs2_device* device, rs2_playback_status_changed_callback* callback, rs2_error** error);

/**
* Returns the current state of the playback device
* \param[in] device A playback device
* \param[out] error If non-null, receives any error that occurs during this call, otherwise, errors are ignored
* \return Current state of the playback
*/
rs2_playback_status rs2_playback_device_get_current_status(const rs2_device* device, rs2_error** error);

rs2_frame* rs2_allocate_synthetic_video_frame(rs2_source* source, rs2_stream new_stream, rs2_frame* original,
rs2_format new_format, int new_bpp, int new_width, int new_height, int new_stride, rs2_error** error);

Expand Down
33 changes: 32 additions & 1 deletion include/librealsense/rs2.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1455,6 +1455,21 @@ namespace rs2
std::shared_ptr<rs2_device_list> _list;
};

template<class T>
class status_changed_callback : public rs2_playback_status_changed_callback
{
T on_status_changed_function;
public:
explicit status_changed_callback(T on_status_changed) : on_status_changed_function(on_status_changed) {}

void on_playback_status_changed(rs2_playback_status status) override
{
on_status_changed_function(status);
}

void release() override { delete this; }
};

class playback : public device
{
public:
Expand Down Expand Up @@ -1510,7 +1525,7 @@ namespace rs2
bool is_real_time() const
{
rs2_error* e = nullptr;
bool real_time = rs2_playback_device_is_real_time(_dev.get(), &e) == 0 ? false : true;
bool real_time = rs2_playback_device_is_real_time(_dev.get(), &e) != 0;
error::handle(e);
return real_time;
}
Expand All @@ -1521,6 +1536,21 @@ namespace rs2
rs2_playback_device_set_real_time(_dev.get(), (real_time ? 1 : 0), &e);
error::handle(e);
}
template <typename T>
void set_status_changed_callback(T callback)
{
rs2_error * e = nullptr;
rs2_playback_device_set_status_changed_callback(_dev.get(), new status_changed_callback<T>(std::move(callback)), &e);
error::handle(e);
}

rs2_playback_status current_status() const
{
rs2_error* e = nullptr;
rs2_playback_status sts = rs2_playback_device_get_current_status(_dev.get(), &e);
error::handle(e);
return sts;
}
protected:
friend context;
explicit playback(std::shared_ptr<rs2_device> dev) : device(dev)
Expand Down Expand Up @@ -1976,5 +2006,6 @@ inline std::ostream & operator << (std::ostream & o, rs2_timestamp_domain domain
inline std::ostream & operator << (std::ostream & o, rs2_notification_category notificaton) { return o << rs2_notification_category_to_string(notificaton); }
inline std::ostream & operator << (std::ostream & o, rs2_ivcam_visual_preset preset) { return o << rs2_visual_preset_to_string(preset); }
inline std::ostream & operator << (std::ostream & o, rs2_exception_type exception_type) { return o << rs2_exception_type_to_string(exception_type); }
inline std::ostream & operator << (std::ostream & o, rs2_playback_status status) { return o << rs2_playback_status_to_string(status); }

#endif // LIBREALSENSE_RS2_HPP
6 changes: 6 additions & 0 deletions include/librealsense/rscore2.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,10 @@ struct rs2_devices_changed_callback
virtual ~rs2_devices_changed_callback() {}
};

struct rs2_playback_status_changed_callback
{
virtual void on_playback_status_changed(rs2_playback_status status) = 0;
virtual void release() = 0;
virtual ~rs2_playback_status_changed_callback() {}
};
#endif
Loading

0 comments on commit 017aa7c

Please sign in to comment.