Skip to content

Commit

Permalink
FIX: don't destroy VideoPlayer when video stops by itself
Browse files Browse the repository at this point in the history
  • Loading branch information
ivan-mogilko committed Feb 28, 2024
1 parent a4aa6b0 commit 540a1a2
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 17 deletions.
38 changes: 38 additions & 0 deletions Engine/ac/video_script.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,114 +50,152 @@ ScriptVideoPlayer *VideoPlayer_Open(const char *filename, bool auto_play, int re

void VideoPlayer_Play(ScriptVideoPlayer *sc_video)
{
if (sc_video->GetID() < 0)
return;
VideoControl *video_ctrl = get_video_control(sc_video->GetID());
video_ctrl->Play();
}

void VideoPlayer_Pause(ScriptVideoPlayer *sc_video)
{
if (sc_video->GetID() < 0)
return;
VideoControl *video_ctrl = get_video_control(sc_video->GetID());
video_ctrl->Pause();
}

void VideoPlayer_NextFrame(ScriptVideoPlayer *sc_video)
{
if (sc_video->GetID() < 0)
return;
VideoControl *video_ctrl = get_video_control(sc_video->GetID());
video_ctrl->NextFrame();
}

int VideoPlayer_SeekFrame(ScriptVideoPlayer *sc_video, int frame)
{
if (sc_video->GetID() < 0)
return 0;
VideoControl *video_ctrl = get_video_control(sc_video->GetID());
return video_ctrl->SeekFrame(frame);
}

int VideoPlayer_SeekMs(ScriptVideoPlayer *sc_video, int pos)
{
if (sc_video->GetID() < 0)
return 0;
VideoControl *video_ctrl = get_video_control(sc_video->GetID());
return video_ctrl->SeekMs(pos);
}

void VideoPlayer_Stop(ScriptVideoPlayer *sc_video)
{
if (sc_video->GetID() < 0)
return;
video_stop(sc_video->GetID());
sc_video->Invalidate();
}

int VideoPlayer_GetFrame(ScriptVideoPlayer *sc_video)
{
if (sc_video->GetID() < 0)
return 0;
VideoControl *video_ctrl = get_video_control(sc_video->GetID());
return video_ctrl->GetFrame();
}

int VideoPlayer_GetFrameCount(ScriptVideoPlayer *sc_video)
{
if (sc_video->GetID() < 0)
return 0;
VideoControl *video_ctrl = get_video_control(sc_video->GetID());
return video_ctrl->GetFrameCount();
}

float VideoPlayer_GetFrameRate(ScriptVideoPlayer *sc_video)
{
if (sc_video->GetID() < 0)
return 0.f;
VideoControl *video_ctrl = get_video_control(sc_video->GetID());
return video_ctrl->GetFrameRate();
}

int VideoPlayer_GetGraphic(ScriptVideoPlayer *sc_video)
{
if (sc_video->GetID() < 0)
return 0;
VideoControl *video_ctrl = get_video_control(sc_video->GetID());
return video_ctrl->GetSpriteID();
}

int VideoPlayer_GetLengthMs(ScriptVideoPlayer *sc_video)
{
if (sc_video->GetID() < 0)
return 0;
VideoControl *video_ctrl = get_video_control(sc_video->GetID());
return video_ctrl->GetLengthMs();
}

int VideoPlayer_GetLooping(ScriptVideoPlayer *sc_video)
{
if (sc_video->GetID() < 0)
return false;
VideoControl *video_ctrl = get_video_control(sc_video->GetID());
return video_ctrl->GetLooping();
}

void VideoPlayer_SetLooping(ScriptVideoPlayer *sc_video, bool loop)
{
if (sc_video->GetID() < 0)
return;
VideoControl *video_ctrl = get_video_control(sc_video->GetID());
return video_ctrl->SetLooping(loop);
}

int VideoPlayer_GetPositionMs(ScriptVideoPlayer *sc_video)
{
if (sc_video->GetID() < 0)
return 0;
VideoControl *video_ctrl = get_video_control(sc_video->GetID());
return video_ctrl->GetPositionMs();
}

float VideoPlayer_GetSpeed(ScriptVideoPlayer *sc_video)
{
if (sc_video->GetID() < 0)
return 0.f;
VideoControl *video_ctrl = get_video_control(sc_video->GetID());
return video_ctrl->GetSpeed();
}

void VideoPlayer_SetSpeed(ScriptVideoPlayer *sc_video, float speed)
{
if (sc_video->GetID() < 0)
return;
VideoControl *video_ctrl = get_video_control(sc_video->GetID());
video_ctrl->SetSpeed(speed);
}

int VideoPlayer_GetState(ScriptVideoPlayer *sc_video)
{
if (sc_video->GetID() < 0)
return PlaybackState::PlayStateInvalid;
VideoControl *video_ctrl = get_video_control(sc_video->GetID());
return video_ctrl->GetState();
}

int VideoPlayer_GetVolume(ScriptVideoPlayer *sc_video)
{
if (sc_video->GetID() < 0)
return 0;
VideoControl *video_ctrl = get_video_control(sc_video->GetID());
return video_ctrl->GetVolume();
}

void VideoPlayer_SetVolume(ScriptVideoPlayer *sc_video, int volume)
{
if (sc_video->GetID() < 0)
return;
VideoControl *video_ctrl = get_video_control(sc_video->GetID());
video_ctrl->SetVolume(volume);
}
Expand Down
20 changes: 3 additions & 17 deletions Engine/media/video/video.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -498,30 +498,16 @@ void video_stop(int video_id)
free_dynamic_sprite(it->second->GetSpriteID());
gl_VideoObjects.erase(video_slot);

// FIXME: must invalidate script ref if present

if (gl_VideoObjects.empty())
video_core_shutdown(); // stop the thread to avoid redundant processing
}

void sync_video_playback()
{
for (auto it = gl_VideoObjects.begin(); it != gl_VideoObjects.end();)
for (auto &obj : gl_VideoObjects)
{
const int video_slot = it->first;
VideoControl *video_ctrl = it->second.get();
const int sprite_slot = video_ctrl->GetSpriteID();

if (video_ctrl->Update())
{
++it;
}
else
{
video_core_slot_stop(video_slot);
free_dynamic_sprite(sprite_slot);
it = gl_VideoObjects.erase(it);
}
VideoControl *video_ctrl = obj.second.get();
video_ctrl->Update();
}

if (gl_VideoObjects.empty())
Expand Down

0 comments on commit 540a1a2

Please sign in to comment.