perf: compact [propId, value] wire format for property events
This commit is contained in:
@@ -249,7 +249,8 @@ std::string MpvPlayer::GetProperty(const std::string& name) {
|
||||
}
|
||||
|
||||
void MpvPlayer::ObserveProperty(const std::string& name,
|
||||
const std::string& format) {
|
||||
const std::string& format,
|
||||
int id) {
|
||||
if (disposed_ || !mpv_) return;
|
||||
|
||||
// Check if already observing.
|
||||
@@ -257,6 +258,8 @@ void MpvPlayer::ObserveProperty(const std::string& name,
|
||||
return;
|
||||
}
|
||||
|
||||
name_to_id_[name] = id;
|
||||
|
||||
mpv_format mpv_fmt = MPV_FORMAT_NONE;
|
||||
if (format == "string") {
|
||||
mpv_fmt = MPV_FORMAT_STRING;
|
||||
@@ -515,22 +518,24 @@ FlValue* MpvPlayer::NodeToFlValue(mpv_node* node) {
|
||||
}
|
||||
|
||||
void MpvPlayer::SendPropertyChange(const char* name, mpv_node* data) {
|
||||
FlValue* event_map = fl_value_new_map();
|
||||
fl_value_set_string_take(event_map, "type", fl_value_new_string("property"));
|
||||
fl_value_set_string_take(event_map, "name",
|
||||
fl_value_new_string(name ? name : ""));
|
||||
if (!name) return;
|
||||
|
||||
auto it = name_to_id_.find(name);
|
||||
if (it == name_to_id_.end()) return;
|
||||
|
||||
FlValue* list = fl_value_new_list();
|
||||
fl_value_append_take(list, fl_value_new_int(it->second));
|
||||
if (data) {
|
||||
fl_value_set_string_take(event_map, "value", NodeToFlValue(data));
|
||||
fl_value_append_take(list, NodeToFlValue(data));
|
||||
} else {
|
||||
fl_value_set_string_take(event_map, "value", fl_value_new_null());
|
||||
fl_value_append_take(list, fl_value_new_null());
|
||||
}
|
||||
|
||||
std::lock_guard<std::mutex> lock(callback_mutex_);
|
||||
if (event_callback_) {
|
||||
event_callback_(event_map);
|
||||
event_callback_(list);
|
||||
}
|
||||
fl_value_unref(event_map);
|
||||
fl_value_unref(list);
|
||||
}
|
||||
|
||||
void MpvPlayer::SendEvent(const std::string& name, FlValue* data) {
|
||||
|
||||
@@ -71,7 +71,9 @@ class MpvPlayer {
|
||||
/// Changes will be reported via the event callback.
|
||||
/// @param name Property name to observe.
|
||||
/// @param format Format type ("string", "flag", "int64", "double", "node").
|
||||
void ObserveProperty(const std::string& name, const std::string& format);
|
||||
/// @param id Property ID assigned by Dart for compact event encoding.
|
||||
void ObserveProperty(const std::string& name, const std::string& format,
|
||||
int id);
|
||||
|
||||
/// Renders a frame to the current OpenGL context.
|
||||
/// Must be called from the GTK render callback.
|
||||
@@ -133,6 +135,7 @@ class MpvPlayer {
|
||||
|
||||
uint64_t next_reply_userdata_ = 1;
|
||||
std::map<std::string, uint64_t> observed_properties_;
|
||||
std::map<std::string, int> name_to_id_;
|
||||
|
||||
// Pending async commands: request_id -> callback
|
||||
std::map<uint64_t, CommandCallback> pending_commands_;
|
||||
|
||||
@@ -411,6 +411,7 @@ static void mpv_plugin_handle_method_call(FlMethodChannel* channel,
|
||||
} else {
|
||||
FlValue* name_value = fl_value_lookup_string(args, "name");
|
||||
FlValue* format_value = fl_value_lookup_string(args, "format");
|
||||
FlValue* id_value = fl_value_lookup_string(args, "id");
|
||||
|
||||
if (name_value == nullptr ||
|
||||
fl_value_get_type(name_value) != FL_VALUE_TYPE_STRING) {
|
||||
@@ -420,9 +421,14 @@ static void mpv_plugin_handle_method_call(FlMethodChannel* channel,
|
||||
fl_value_get_type(format_value) != FL_VALUE_TYPE_STRING) {
|
||||
response = FL_METHOD_RESPONSE(fl_method_error_response_new(
|
||||
"INVALID_ARGS", "Missing 'format'", nullptr));
|
||||
} else if (id_value == nullptr ||
|
||||
fl_value_get_type(id_value) != FL_VALUE_TYPE_INT) {
|
||||
response = FL_METHOD_RESPONSE(fl_method_error_response_new(
|
||||
"INVALID_ARGS", "Missing 'id'", nullptr));
|
||||
} else {
|
||||
self->player->ObserveProperty(fl_value_get_string(name_value),
|
||||
fl_value_get_string(format_value));
|
||||
fl_value_get_string(format_value),
|
||||
static_cast<int>(fl_value_get_int(id_value)));
|
||||
response = FL_METHOD_RESPONSE(fl_method_success_response_new(nullptr));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user