Last active
December 17, 2022 19:07
-
-
Save kassane/e0c3ca9043b3098f3acc09d0ceef2c6a to your computer and use it in GitHub Desktop.
VLC cli-player example - with Async C++ (Asio)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// by: Matheus Catarino França | |
// License: CC0 | |
// usage: g++ -o asio_vlc asio_libvlc.cpp $(pkg-config --cflags --libs libvlc) | |
// STANDALONE ASIO | |
#include <asio.hpp> | |
#include <chrono> | |
#include <iostream> | |
#include <vlc/vlc.h> | |
class VLCWrapper { | |
public: | |
VLCWrapper(asio::io_context &service) | |
: service_(service), instance_(libvlc_new(0, nullptr)), | |
mediaPlayer_(libvlc_media_player_new(instance_)) {} | |
~VLCWrapper() { | |
libvlc_media_player_release(mediaPlayer_); | |
libvlc_release(instance_); | |
} | |
void playMedia(const std::string &path) { | |
// Create a timer that sleeps for 5 seconds | |
asio::steady_timer timer(service_, std::chrono::seconds(60)); | |
libvlc_media_t *media = libvlc_media_new_path(instance_, path.c_str()); | |
libvlc_media_player_set_media(mediaPlayer_, media); | |
libvlc_media_player_play(mediaPlayer_); | |
// Wait for the timer to expire | |
timer.wait(); | |
libvlc_media_release(media); | |
} | |
private: | |
asio::io_context &service_; | |
libvlc_instance_t *instance_; | |
libvlc_media_player_t *mediaPlayer_; | |
}; | |
int main(int argc, char **argv) { | |
// Initialize Asio and create a service object | |
asio::io_context service; | |
// Create a thread pool with 4 threads | |
asio::thread_pool threadPool(4); | |
// Create a VLC wrapper object | |
VLCWrapper vlcWrapper(service); | |
// Play a media file asynchronously using the thread pool | |
asio::post(threadPool, | |
[&vlcWrapper, &argv]() { vlcWrapper.playMedia(argv[1]); }); | |
// Run the Asio service object to process events asynchronously | |
service.run(); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// by: Matheus Catarino França | |
// License: CC0 | |
// usage: g++ -o asio_vlc asio_libvlc.cpp $(pkg-config --cflags --libs libvlc) | |
#include <asio.hpp> | |
#include <iostream> | |
#include <thread> | |
#include <vlc/vlc.h> | |
// This function will be called by libvlc when a time event occurs | |
void time_callback(const libvlc_event_t *event, void *data) { | |
// Print the current time of the media | |
std::cout << "Current time: " << event->u.media_player_time_changed.new_time | |
<< std::endl; | |
} | |
int main(int argc, char **argv) { | |
// Initialize the asio service | |
asio::io_context io_service; | |
// Create a libvlc instance | |
libvlc_instance_t *vlc = libvlc_new(0, nullptr); | |
// Create a media player using the instance | |
libvlc_media_player_t *media_player = libvlc_media_player_new(vlc); | |
// Set the media player to receive time events | |
libvlc_event_manager_t *event_manager = | |
libvlc_media_player_event_manager(media_player); | |
libvlc_event_attach(event_manager, libvlc_MediaPlayerTimeChanged, | |
time_callback, nullptr); | |
// Load a media file | |
libvlc_media_t *media = libvlc_media_new_path(vlc, argv[1]); | |
libvlc_media_player_set_media(media_player, media); | |
libvlc_media_release(media); | |
// Start the media player | |
libvlc_media_player_play(media_player); | |
// Run the asio service in a separate thread | |
std::thread t([&io_service]() { io_service.run(); }); | |
// Wait for the media player to finish | |
while (true) { | |
libvlc_state_t state = libvlc_media_player_get_state(media_player); | |
if (state == libvlc_Ended) { | |
break; | |
} | |
std::this_thread::sleep_for(std::chrono::milliseconds(100)); | |
} | |
// Stop the asio service and join the thread | |
io_service.stop(); | |
t.join(); | |
// Clean up | |
libvlc_media_player_release(media_player); | |
libvlc_release(vlc); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment