|
| 1 | +/* |
| 2 | + * dart_vlc: A media playback library for Dart & Flutter. Based on libVLC & |
| 3 | + * libVLC++. |
| 4 | + * |
| 5 | + * Hitesh Kumar Saini |
| 6 | + * https://github.com/alexmercerind |
| 7 | + * saini123hitesh@gmail.com; alexmercerind@gmail.com |
| 8 | + * |
| 9 | + * GNU Lesser General Public License v2.1 |
| 10 | + */ |
| 11 | + |
| 12 | +#include <unordered_map> |
| 13 | +#include "include/dart_vlc/dart_vlc_plugin.h" |
| 14 | +#include "native/dart_vlc.cpp" |
| 15 | +#include "video_outlet.hpp" |
| 16 | + |
| 17 | +namespace { |
| 18 | + |
| 19 | +class DartVlcPlugin : public flutter::Plugin { |
| 20 | + public: |
| 21 | + static void RegisterWithRegistrar(flutter::PluginRegistrarWindows* registrar); |
| 22 | + |
| 23 | + flutter::MethodChannel<flutter::EncodableValue>* channel() const { |
| 24 | + return channel_.get(); |
| 25 | + } |
| 26 | + |
| 27 | + DartVlcPlugin( |
| 28 | + std::unique_ptr<flutter::MethodChannel<flutter::EncodableValue>> channel, |
| 29 | + flutter::TextureRegistrar* texture_registrar); |
| 30 | + |
| 31 | + virtual ~DartVlcPlugin(); |
| 32 | + |
| 33 | + private: |
| 34 | + void HandleMethodCall( |
| 35 | + const flutter::MethodCall<flutter::EncodableValue>& method_call, |
| 36 | + std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result); |
| 37 | + |
| 38 | + flutter::TextureRegistrar* texture_registrar_; |
| 39 | + std::unique_ptr<flutter::MethodChannel<flutter::EncodableValue>> channel_; |
| 40 | + std::unordered_map<int, std::unique_ptr<VideoOutlet>> outlets_; |
| 41 | +}; |
| 42 | + |
| 43 | +void DartVlcPlugin::RegisterWithRegistrar( |
| 44 | + flutter::PluginRegistrarWindows* registrar) { |
| 45 | + auto plugin = std::make_unique<DartVlcPlugin>( |
| 46 | + std::make_unique<flutter::MethodChannel<flutter::EncodableValue>>( |
| 47 | + registrar->messenger(), "dart_vlc", |
| 48 | + &flutter::StandardMethodCodec::GetInstance()), |
| 49 | + registrar->texture_registrar()); |
| 50 | + plugin->channel()->SetMethodCallHandler([plugin_pointer = plugin.get()]( |
| 51 | + const auto& call, auto result) { |
| 52 | + plugin_pointer->HandleMethodCall(call, std::move(result)); |
| 53 | + }); |
| 54 | + registrar->AddPlugin(std::move(plugin)); |
| 55 | +} |
| 56 | + |
| 57 | +DartVlcPlugin::DartVlcPlugin( |
| 58 | + std::unique_ptr<flutter::MethodChannel<flutter::EncodableValue>> channel, |
| 59 | + flutter::TextureRegistrar* texture_registrar) |
| 60 | + : channel_(std::move(channel)), texture_registrar_(texture_registrar) {} |
| 61 | + |
| 62 | +DartVlcPlugin::~DartVlcPlugin() {} |
| 63 | + |
| 64 | +void DartVlcPlugin::HandleMethodCall( |
| 65 | + const flutter::MethodCall<flutter::EncodableValue>& method_call, |
| 66 | + std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result) { |
| 67 | + if (method_call.method_name() == "PlayerRegisterTexture") { |
| 68 | + flutter::EncodableMap arguments = |
| 69 | + std::get<flutter::EncodableMap>(*method_call.arguments()); |
| 70 | + int32_t player_id = |
| 71 | + std::get<int>(arguments[flutter::EncodableValue("playerId")]); |
| 72 | + if (outlets_.find(player_id) == outlets_.end()) { |
| 73 | + outlets_[player_id] = std::make_unique<VideoOutlet>(texture_registrar_); |
| 74 | + Player* player = g_players->Get(player_id); |
| 75 | + player->OnVideo( |
| 76 | + [=](uint8_t* frame, int32_t width, int32_t height) -> void { |
| 77 | + outlets_[player_id]->OnVideo(frame, width, height); |
| 78 | + }); |
| 79 | + return result->Success( |
| 80 | + flutter::EncodableValue(outlets_[player_id]->texture_id())); |
| 81 | + } |
| 82 | + result->Error("-1", "Texture was already registered."); |
| 83 | + } else if (method_call.method_name() == "PlayerUnregisterTexture") { |
| 84 | + flutter::EncodableMap arguments = |
| 85 | + std::get<flutter::EncodableMap>(*method_call.arguments()); |
| 86 | + int player_id = |
| 87 | + std::get<int>(arguments[flutter::EncodableValue("playerId")]); |
| 88 | + if (outlets_.find(player_id) == outlets_.end()) { |
| 89 | + return result->Error("-2", "Texture was not found."); |
| 90 | + } |
| 91 | + outlets_.erase(player_id); |
| 92 | + result->Success(flutter::EncodableValue(nullptr)); |
| 93 | + } else { |
| 94 | + result->NotImplemented(); |
| 95 | + } |
| 96 | +} |
| 97 | +} // namespace |
| 98 | + |
| 99 | +void DartVlcPluginRegisterWithRegistrar( |
| 100 | + FlutterDesktopPluginRegistrarRef registrar) { |
| 101 | + DartVlcPlugin::RegisterWithRegistrar( |
| 102 | + flutter::PluginRegistrarManager::GetInstance() |
| 103 | + ->GetRegistrar<flutter::PluginRegistrarWindows>(registrar)); |
| 104 | +} |
0 commit comments