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