Oxygen Engine
Modern C++ 3D Engine using OpenGL
Loading...
Searching...
No Matches
video.h
1#ifndef OE_EXT_VIDEO_H
2#define OE_EXT_VIDEO_H
3
4#include <vlc/vlc.h>
5
12{
14 {
25 VideoPlayer(const int argc = 0, const char *const* argv = nullptr)
26 {
27 vlc = libvlc_new(argc, argv);
28 }
29
31 {
32 stop();
33
34 if (vlc)
35 {
36 libvlc_release(vlc);
37 }
38 }
39
40 bool loadMediaFromFile(const char* path)
41 {
42 media = libvlc_media_new_path(vlc, path);
43
44 if (media == NULL)
45 {
46 fprintf(stderr, "unable to create media %s", path);
47 return false;
48 }
49
50 return loadMedia(media);
51 }
52
53 bool loadMediaFromUrl(const char* url)
54 {
55 media = libvlc_media_new_location(vlc, url);
56
57 if (media == NULL)
58 {
59 fprintf(stderr, "unable to create media %s", url);
60 return false;
61 }
62
63 return loadMedia(media);
64 }
65
66 bool loadMedia(libvlc_media_t* media)
67 {
68 media_player = libvlc_media_player_new_from_media(media);
69
70 if (media_player == NULL)
71 {
72 fprintf(stderr, "unable to create media player");
73 libvlc_media_release(media);
74 return false;
75 }
76
77 buffer.resize(width*height*3);
78 libvlc_video_set_callbacks(media_player, lock_callback, unlock_callback, nullptr, this);
79 libvlc_video_set_format(media_player, "RV24", width, height, width*3);
80
81 libvlc_media_player_pause(media_player);
82
83 return true;
84 }
85
86 void play()
87 {
88 libvlc_media_player_play(media_player);
89 }
90
91 void pause()
92 {
93 libvlc_media_player_pause(media_player);
94 }
95
96 bool isPlaying()
97 {
98 return libvlc_media_player_is_playing(media_player);
99 }
100
104 void seekPercentagePosition(const float position)
105 {
106 libvlc_media_player_set_position(media_player, position);
107 }
108
113 {
114 return libvlc_media_player_get_position(media_player);
115 }
116
117 void stop()
118 {
119 if (media_player)
120 {
121 libvlc_media_player_stop(media_player);
122 libvlc_media_player_release(media_player);
123 media_player = nullptr;
124 }
125
126 if (media)
127 {
128 libvlc_media_release(media);
129 media = nullptr;
130 }
131 }
132
134 static void* lock_callback(void *object, void **planes)
135 {
136 VideoPlayer* video_player = static_cast<VideoPlayer*>(object);
137
138 video_player->mutex.lock();
139 planes[0] = static_cast<void*>(video_player->buffer.data());
140
141 return nullptr;
142 }
143
145 static void unlock_callback(void *object, void *picture, void * const *planes)
146 {
147 VideoPlayer* video_player = static_cast<VideoPlayer*>(object);
148 video_player->need_frame_update = true;
149
150 video_player->mutex.unlock();
151 }
152
153 bool need_frame_update = false;
154 std::mutex mutex;
155
157 uint32_t width = 800, height = 600;
158
160 std::vector<std::byte> buffer;
161
163 libvlc_instance_t* vlc = nullptr;
164
166 libvlc_media_player_t* media_player = nullptr;
167
169 libvlc_media_t* media = nullptr;
170 };
171}
172#endif
Video player based on libvlc.
Definition video.h:12
Definition video.h:14
VideoPlayer(const int argc=0, const char *const *argv=nullptr)
Definition video.h:25
float getPercentagePosition()
Definition video.h:112
void seekPercentagePosition(const float position)
Definition video.h:104