Skip to content

Commit

Permalink
Wrapper: Improve OnVideo & OnVideoDimensions callback setup
Browse files Browse the repository at this point in the history
  • Loading branch information
alexmercerind committed Oct 30, 2021
1 parent 6b39e98 commit d56e243
Showing 1 changed file with 101 additions and 54 deletions.
155 changes: 101 additions & 54 deletions dartvlc/internal/events.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

#include "internal/getters.h"

typedef std::function<void(uint8_t*, int32_t, int32_t)> VideoFrameCallback;
using VideoFrameCallback = std::function<void(uint8_t*, int32_t, int32_t)>;

class PlayerEvents : public PlayerGetters {
public:
Expand All @@ -29,8 +29,53 @@ class PlayerEvents : public PlayerGetters {

void OnVideoDimensions(std::function<void(int32_t, int32_t)> callback) {
video_dimension_callback_ = callback;
vlc_media_player_.eventManager().onPlaying(
std::bind(&PlayerEvents::OnVideoDimensionsCallback, this));
vlc_media_player_.setVideoCallbacks(
std::bind(&PlayerEvents::OnVideoLockCallback, this,
std::placeholders::_1),
nullptr, std::bind(&PlayerEvents::OnVideoPictureCallback, this,
std::placeholders::_1));
vlc_media_player_.setVideoFormatCallbacks(
[=](char* chroma, uint32_t* w, uint32_t* h, uint32_t* p,
uint32_t* l) -> int32_t {
int32_t video_width = video_width_, video_height = video_height_,
pitch = video_width_ * 4;
if (video_height_ != static_cast<int32_t>(*h) ||
video_width_ != static_cast<int32_t>(*w)) {
video_height_ = static_cast<int32_t>(*h);
video_width_ = static_cast<int32_t>(*w);
video_dimension_callback_(video_width_, video_height_);
if (preferred_video_width_.has_value() &&
preferred_video_height_.has_value()) {
video_width = preferred_video_width_.value_or(0);
video_height = preferred_video_height_.value_or(0);
pitch = video_width_ * 4;
vlc_media_player_.setVideoFormat("RGBA", video_width,
video_height, pitch);
} else {
video_height = video_height_;
video_width = video_width_;
pitch = video_width_ * 4;
#ifndef __APPLE__
vlc_media_player_.setVideoFormat("RGBA", video_width,
video_height, pitch);
#else
vlc_media_player_.setVideoFormat("RV32", video_width,
video_height, pitch);
#endif
}
}
#ifndef __APPLE__
strncpy(chroma, "RGBA", 4);
#else
strncpy(chroma, "RV32", 4);
#endif
*w = video_width;
*h = video_height;
*p = pitch;
*l = video_height;
return 1;
},
nullptr);
}

void OnPause(std::function<void()> callback) {
Expand Down Expand Up @@ -120,56 +165,56 @@ class PlayerEvents : public PlayerGetters {
std::function<void(int32_t, int32_t)> video_dimension_callback_ = [=](
int32_t, int32_t) -> void {};

void OnVideoDimensionsCallback() {
int32_t video_width = 0;
int32_t video_height = 0;
if (preferred_video_width_.has_value() &&
preferred_video_height_.has_value()) {
video_width = preferred_video_width_.value_or(0);
video_height = preferred_video_height_.value_or(0);
} else {
uint32_t px = 0, py = 0;
libvlc_video_get_size(vlc_media_player_.get(), 0, &px, &py);
video_width = static_cast<int32_t>(px);
video_height = static_cast<int32_t>(py);
}
video_dimension_callback_(video_width, video_height);
if (video_width_ != video_width || video_height_ != video_height) {
video_width_ = video_width;
video_height_ = video_height;
int32_t pitch = video_width * 4;
// https://github.com/alexmercerind/dart_vlc/pull/137
// int32_t size = video_height * pitch;
// video_frame_buffer_.reset(new uint8_t[size]);
vlc_media_player_.setVideoCallbacks(
std::bind(&PlayerEvents::OnVideoLockCallback, this,
std::placeholders::_1),
nullptr, std::bind(&PlayerEvents::OnVideoPictureCallback, this,
std::placeholders::_1));
vlc_media_player_.setVideoFormatCallbacks(
[=](char* chroma, uint32_t* w, uint32_t* h, uint32_t* p,
uint32_t* l) -> int32_t {
#ifndef __APPLE__
strncpy(chroma, "RGBA", 4);
#else
strncpy(chroma, "RV32", 4);
#endif
*w = video_width;
*h = video_height;
*p = pitch;
*l = video_height;
return 1;
},
nullptr);
#ifndef __APPLE__
vlc_media_player_.setVideoFormat("RGBA", video_width, video_height,
pitch);
#else
vlc_media_player_.setVideoFormat("RV32", video_width, video_height,
pitch);
#endif
}
}
// void OnVideoDimensionsCallback() {
// int32_t video_width = 0;
// int32_t video_height = 0;
// if (preferred_video_width_.has_value() &&
// preferred_video_height_.has_value()) {
// video_width = preferred_video_width_.value_or(0);
// video_height = preferred_video_height_.value_or(0);
// } else {
// uint32_t px = 0, py = 0;
// libvlc_video_get_size(vlc_media_player_.get(), 0, &px, &py);
// video_width = static_cast<int32_t>(px);
// video_height = static_cast<int32_t>(py);
// }
// video_dimension_callback_(video_width, video_height);
// if (video_width_ != video_width || video_height_ != video_height) {
// video_width_ = video_width;
// video_height_ = video_height;
// int32_t pitch = video_width * 4;
// // https://github.com/alexmercerind/dart_vlc/pull/137
// // int32_t size = video_height * pitch;
// // video_frame_buffer_.reset(new uint8_t[size]);
// vlc_media_player_.setVideoCallbacks(
// std::bind(&PlayerEvents::OnVideoLockCallback, this,
// std::placeholders::_1),
// nullptr, std::bind(&PlayerEvents::OnVideoPictureCallback, this,
// std::placeholders::_1));
// vlc_media_player_.setVideoFormatCallbacks(
// [=](char* chroma, uint32_t* w, uint32_t* h, uint32_t* p,
// uint32_t* l) -> int32_t {
// #ifndef __APPLE__
// strncpy(chroma, "RGBA", 4);
// #else
// strncpy(chroma, "RV32", 4);
// #endif
// *w = video_width;
// *h = video_height;
// *p = pitch;
// *l = video_height;
// return 1;
// },
// nullptr);
// #ifndef __APPLE__
// vlc_media_player_.setVideoFormat("RGBA", video_width, video_height,
// pitch);
// #else
// vlc_media_player_.setVideoFormat("RV32", video_width, video_height,
// pitch);
// #endif
// }
// }

std::function<void()> play_callback_ = [=]() -> void {};

Expand Down Expand Up @@ -259,7 +304,9 @@ class PlayerEvents : public PlayerGetters {

void OnVideoPictureCallback(void* picture) {
if (video_callback_) {
video_callback_(video_frame_buffer_.get(), video_width_, video_height_);
video_callback_(video_frame_buffer_.get(),
preferred_video_width_.value_or(video_width_),
preferred_video_height_.value_or(video_height_));
}
}
};

0 comments on commit d56e243

Please sign in to comment.