@@ -66,28 +66,44 @@ using AVFrameRefPtr = std::unique_ptr<AVFrame, deleteAVFrameRef>;
6666}
6767
6868namespace Media {
69- class Video
69+ class Video : public std ::enable_shared_from_this<Video>
7070 {
7171 public:
7272 Video (VideoPlayer &player, Engine::BaseEngine &engine, const std::string &fileName)
7373 : player{player}, engine{engine}, fileName{fileName}
7474 {
7575 av_register_all ();
76-
77- view = new UI::ImageView (engine);
78- view->setHidden (true );
79- view->setRelativeSize (false );
80-
81- engine.getRootUIView ().addChild (view);
8276 }
8377
8478 ~Video ()
8579 {
86- view->setHidden (true );
80+ engine.getJobManager ().executeInMainThread <void >([videoPtr = std::weak_ptr<Video>{shared_from_this ()}](Engine::BaseEngine *engine) {
81+ auto video = videoPtr.lock ();
82+ if (!video) {
83+ LogWarn () << " Video object disappeared" ;
84+ return ;
85+ }
86+
87+ video->view ->setHidden (true );
88+ }).wait ();
8789 }
8890
8991 bool init (const uint16_t width, const uint16_t height)
9092 {
93+ engine.getJobManager ().executeInMainThread <void >([videoPtr = std::weak_ptr<Video>{shared_from_this ()}](Engine::BaseEngine *engine){
94+ auto video = videoPtr.lock ();
95+ if (!video) {
96+ LogWarn () << " Video object disappeared" ;
97+ return ;
98+ }
99+
100+ video->view = new UI::ImageView (*engine);
101+ video->view ->setHidden (true );
102+ video->view ->setRelativeSize (false );
103+
104+ engine->getRootUIView ().addChild (video->view );
105+ }).wait ();
106+
91107 av_init_packet (&packet);
92108 packet.data = nullptr ;
93109 packet.size = 0 ;
@@ -280,17 +296,25 @@ namespace Media {
280296 // TODO: this can be probably accelerated by doing conversion within the fragment shader
281297 auto data = yuv420pToRGBA (frame);
282298
283- view->setHidden (false );
284- view->setSize (Math::float2 (1 ,1 ));
299+ engine.getJobManager ().executeInMainThread <void >([videoPtr = std::weak_ptr<Video>{shared_from_this ()}, data](Engine::BaseEngine *engine) mutable {
300+ auto video = videoPtr.lock ();
301+ if (!video) {
302+ LogWarn () << " Video object disappeared" ;
303+ return ;
304+ }
305+
306+ video->view ->setHidden (false );
307+ video->view ->setSize (Math::float2 (1 , 1 ));
285308
286- Textures::TextureAllocator& alloc = engine. getEngineTextureAlloc ();
309+ Textures::TextureAllocator & alloc = engine-> getEngineTextureAlloc ();
287310
288- if (!texture.isValid ())
289- texture = alloc.loadTextureRGBA8 (data, videoCodecContext->width , videoCodecContext->height );
290- else
291- std::swap (alloc.getTexture (texture).imageData , data);
292- alloc.asyncFinalizeLoad (texture);
293- view->setImage (texture, videoCodecContext->width , videoCodecContext->height );
311+ if (!video->texture .isValid ())
312+ video->texture = alloc.loadTextureRGBA8 (data, video->videoCodecContext ->width , video->videoCodecContext ->height );
313+ else
314+ std::swap (alloc.getTexture (video->texture ).imageData , data);
315+ alloc.asyncFinalizeLoad (video->texture );
316+ video->view ->setImage (video->texture , video->videoCodecContext ->width , video->videoCodecContext ->height );
317+ }).wait ();
294318 }
295319
296320public:
@@ -343,7 +367,7 @@ void Media::VideoPlayer::play(const std::string &fileName)
343367{
344368 std::cout << " playVideo(" << fileName << " )" << std::endl;
345369#ifdef RE_ENABLE_FFMPEG
346- currentVideo = std::make_unique <Video>(*this , engine, fileName);
370+ currentVideo = std::make_shared <Video>(*this , engine, fileName);
347371#else
348372 LogWarn () << " No libavcodec support compiled, won't play" << fileName;
349373#endif
0 commit comments