Skip to content

Commit

Permalink
Wrapper: Add start_time & stop_time handling to Media
Browse files Browse the repository at this point in the history
  • Loading branch information
alexmercerind committed Sep 29, 2021
1 parent 44b6133 commit 7191cb7
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 29 deletions.
12 changes: 7 additions & 5 deletions dartvlc/api/api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,16 @@ void PlayerOpen(int32_t id, bool auto_start, const char** source,
int32_t source_size) {
std::vector<std::shared_ptr<Media>> medias{};
Player* player = g_players->Get(id);
for (int32_t index = 0; index < 2 * source_size; index += 2) {
for (int32_t index = 0; index < 4 * source_size; index += 4) {
std::shared_ptr<Media> media;
const char* type = source[index];
const char* resource = source[index + 1];
const char* start_time = source[index + 2];
const char* stop_time = source[index + 3];
if (strcmp(type, "MediaType.file") == 0)
media = Media::file(resource, false);
media = Media::file(resource, false, 10000, start_time, stop_time);
else if (strcmp(type, "MediaType.network") == 0)
media = Media::network(resource, false);
media = Media::network(resource, false, 10000, start_time, stop_time);
else
media = Media::directShow(resource);
medias.emplace_back(media);
Expand Down Expand Up @@ -238,7 +240,7 @@ const char** MediaParse(Dart_Handle object, const char* type,
Dart_NewFinalizableHandle_DL(
object, reinterpret_cast<void*>(values), sizeof(values),
static_cast<Dart_HandleFinalizer>(MediaClearVector));
for (const auto& [key, value] : *metas) {
for (const auto & [ key, value ] : *metas) {
values->emplace_back(value.c_str());
}
return values->data();
Expand Down Expand Up @@ -310,7 +312,7 @@ DartDeviceList* DevicesAll(Dart_Handle object) {
static DartEqualizer* EqualizerToDart(const Equalizer* equalizer, int32_t id,
Dart_Handle dart_handle) {
auto wrapper = new DartObjects::Equalizer();
for (const auto& [band, amp] : equalizer->band_amps()) {
for (const auto & [ band, amp ] : equalizer->band_amps()) {
wrapper->bands.emplace_back(band);
wrapper->amps.emplace_back(amp);
}
Expand Down
22 changes: 11 additions & 11 deletions dartvlc/api/eventmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ inline void OnPlayPauseStop(int32_t id, PlayerState* state) {

Dart_CObject type_object;
type_object.type = Dart_CObject_kString;
type_object.value.as_string = "playbackEvent";
type_object.value.as_string = const_cast<char*>("playbackEvent");

Dart_CObject is_playing_object;
is_playing_object.type = Dart_CObject_kBool;
Expand All @@ -66,7 +66,7 @@ inline void OnPosition(int32_t id, PlayerState* state) {

Dart_CObject type_object;
type_object.type = Dart_CObject_kString;
type_object.value.as_string = "positionEvent";
type_object.value.as_string = const_cast<char*>("positionEvent");

Dart_CObject index_object;
index_object.type = Dart_CObject_kInt32;
Expand Down Expand Up @@ -97,7 +97,7 @@ inline void OnComplete(int32_t id, PlayerState* state) {

Dart_CObject type_object;
type_object.type = Dart_CObject_kString;
type_object.value.as_string = "completeEvent";
type_object.value.as_string = const_cast<char*>("completeEvent");

Dart_CObject is_completed_object;
is_completed_object.type = Dart_CObject_kBool;
Expand All @@ -120,7 +120,7 @@ inline void OnVolume(int32_t id, PlayerState* state) {

Dart_CObject type_object;
type_object.type = Dart_CObject_kString;
type_object.value.as_string = "volumeEvent";
type_object.value.as_string = const_cast<char*>("volumeEvent");

Dart_CObject volume_object;
volume_object.type = Dart_CObject_kDouble;
Expand All @@ -142,7 +142,7 @@ inline void OnRate(int32_t id, PlayerState* state) {

Dart_CObject type_object;
type_object.type = Dart_CObject_kString;
type_object.value.as_string = "rateEvent";
type_object.value.as_string = const_cast<char*>("rateEvent");

Dart_CObject rate_object;
rate_object.type = Dart_CObject_kDouble;
Expand All @@ -166,7 +166,7 @@ inline void OnOpen(int32_t id, PlayerState* state) {

Dart_CObject type_object;
type_object.type = Dart_CObject_kString;
type_object.value.as_string = "openEvent";
type_object.value.as_string = const_cast<char*>("openEvent");

Dart_CObject index_object;
index_object.type = Dart_CObject_kInt32;
Expand All @@ -179,7 +179,7 @@ inline void OnOpen(int32_t id, PlayerState* state) {
auto types_objects =
std::unique_ptr<Dart_CObject[]>(new Dart_CObject[media_items.size()]);
auto types_object_refs =
std::unique_ptr<Dart_CObject*[]>(new Dart_CObject*[media_items.size()]);
std::unique_ptr<Dart_CObject* []>(new Dart_CObject*[media_items.size()]);
std::vector<std::string> types_str(media_items.size());
std::vector<const char*> types_ptr(media_items.size());
for (int32_t i = 0; i < media_items.size(); i++) {
Expand All @@ -198,7 +198,7 @@ inline void OnOpen(int32_t id, PlayerState* state) {
auto resources_objects =
std::unique_ptr<Dart_CObject[]>(new Dart_CObject[media_items.size()]);
auto resources_object_refs =
std::unique_ptr<Dart_CObject*[]>(new Dart_CObject*[media_items.size()]);
std::unique_ptr<Dart_CObject* []>(new Dart_CObject*[media_items.size()]);
std::vector<std::string> resources_str(media_items.size());
std::vector<const char*> resources_ptr(media_items.size());
for (int32_t i = 0; i < media_items.size(); i++) {
Expand Down Expand Up @@ -233,7 +233,7 @@ inline void OnVideoDimensions(int32_t id, int32_t video_width,

Dart_CObject type_object;
type_object.type = Dart_CObject_kString;
type_object.value.as_string = "videoDimensionsEvent";
type_object.value.as_string = const_cast<char*>("videoDimensionsEvent");

Dart_CObject video_width_object;
video_width_object.type = Dart_CObject_kInt32;
Expand All @@ -260,7 +260,7 @@ inline void OnVideo(int32_t id, int size, uint8_t* frame) {

Dart_CObject type_object;
type_object.type = Dart_CObject_kString;
type_object.value.as_string = "videoEvent";
type_object.value.as_string = const_cast<char*>("videoEvent");

Dart_CObject frame_object;
frame_object.type = Dart_CObject_kTypedData;
Expand All @@ -284,7 +284,7 @@ inline void OnBuffering(int32_t id, float buffering) {

Dart_CObject type_object;
type_object.type = Dart_CObject_kString;
type_object.value.as_string = "bufferingEvent";
type_object.value.as_string = const_cast<char*>("bufferingEvent");

Dart_CObject buffering_object;
buffering_object.type = Dart_CObject_kDouble;
Expand Down
13 changes: 6 additions & 7 deletions dartvlc/internal/events.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ class PlayerEvents : public PlayerGetters {
open_callback_(*vlc_media_ptr.get());
}

std::function<void(int32_t, int32_t)> video_dimension_callback_ =
[=](int32_t, int32_t) -> void {};
std::function<void(int32_t, int32_t)> video_dimension_callback_ = [=](
int32_t, int32_t) -> void {};

void OnVideoDimensionsCallback() {
int32_t video_width = 0;
Expand All @@ -144,9 +144,8 @@ class PlayerEvents : public PlayerGetters {
vlc_media_player_.setVideoCallbacks(
std::bind(&PlayerEvents::OnVideoLockCallback, this,
std::placeholders::_1),
nullptr,
std::bind(&PlayerEvents::OnVideoPictureCallback, 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 {
Expand Down Expand Up @@ -207,8 +206,8 @@ class PlayerEvents : public PlayerGetters {
stop_callback_();
}

std::function<void(int32_t)> position_callback_ =
[=](int32_t position) -> void {};
std::function<void(int32_t)> position_callback_ = [=](
int32_t position) -> void {};

void OnPositionCallback(float relative_position) {
state()->is_playing_ = vlc_media_player_.isPlaying();
Expand Down
12 changes: 12 additions & 0 deletions dartvlc/internal/setters.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ class PlayerSetters : public PlayerEvents {
std::dynamic_pointer_cast<Media>(media_source);
VLC::Media vlc_media = VLC::Media(vlc_instance_, media->location(),
VLC::Media::FromLocation);
if (media->start_time() != "") {
vlc_media.addOption(media->start_time());
}
if (media->stop_time() != "") {
vlc_media.addOption(media->stop_time());
}
vlc_media_list_.addMedia(vlc_media);
vlc_media_list_player_.setMediaList(vlc_media_list_);
state()->medias()->medias() = {media};
Expand All @@ -39,6 +45,12 @@ class PlayerSetters : public PlayerEvents {
for (std::shared_ptr<Media>& media : playlist->medias()) {
VLC::Media vlc_media = VLC::Media(vlc_instance_, media->location(),
VLC::Media::FromLocation);
if (media->start_time() != "") {
vlc_media.addOption(media->start_time());
}
if (media->stop_time() != "") {
vlc_media.addOption(media->stop_time());
}
vlc_media_list_.addMedia(vlc_media);
state()->medias()->medias().emplace_back(media);
}
Expand Down
24 changes: 18 additions & 6 deletions dartvlc/mediasource/media.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ class Media : public MediaSource {
std::string& media_type() { return media_type_; };
std::string& resource() { return resource_; };
std::string& location() { return location_; };
std::string& start_time() { return start_time_; };
std::string& stop_time() { return stop_time_; };
std::map<std::string, std::string>& metas() { return metas_; };

static std::shared_ptr<Media> create(std::string_view type,
Expand All @@ -45,21 +47,29 @@ class Media : public MediaSource {
}

static std::shared_ptr<Media> file(std::string path, bool parse = false,
int32_t timeout = 10000) {
int32_t timeout = 10000,
std::string start_time = "",
std::string stop_time = "") {
std::shared_ptr<Media> media = std::make_shared<Media>();
media->resource_ = path;
media->location_ = "file:///" + path;
media->media_type_ = kMediaTypeFile;
media->start_time_ = start_time;
media->stop_time_ = stop_time;
if (parse) media->parse(timeout);
return media;
}

static std::shared_ptr<Media> network(std::string url, bool parse = false,
int32_t timeout = 10000) {
int32_t timeout = 10000,
std::string start_time = "",
std::string stop_time = "") {
std::shared_ptr<Media> media = std::make_shared<Media>();
media->resource_ = url;
media->location_ = url;
media->media_type_ = kMediaTypeNetwork;
media->start_time_ = start_time;
media->stop_time_ = stop_time;
if (parse) media->parse(timeout);
return media;
}
Expand Down Expand Up @@ -114,10 +124,12 @@ class Media : public MediaSource {
std::string Type() { return "MediaSourceType.media"; }

private:
std::string media_type_;
std::string resource_;
std::string location_;
std::map<std::string, std::string> metas_;
std::string media_type_ = "";
std::string resource_ = "";
std::string location_ = "";
std::string start_time_ = "";
std::string stop_time_ = "";
std::map<std::string, std::string> metas_ = {};
};

#endif

0 comments on commit 7191cb7

Please sign in to comment.