Skip to content

Commit

Permalink
emit clipEnded after rendering current frame.
Browse files Browse the repository at this point in the history
  • Loading branch information
rectalogic committed Jan 10, 2024
1 parent 9a6563c commit b2f65c7
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 33 deletions.
18 changes: 9 additions & 9 deletions src/MediaFX/media_clip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,6 @@ void MediaClip::render()
if (!isActive())
return;

if (m_currentFrameTime.start() >= m_clipEnd) {
if (m_audioTrack)
m_audioTrack->stop();
if (m_videoTrack)
m_videoTrack->stop();
emit clipEnded();
return;
}

emit clipCurrentTimeChanged();

if (m_audioTrack)
Expand All @@ -101,6 +92,15 @@ void MediaClip::render()
m_videoTrack->render(m_currentFrameTime);

m_currentFrameTime = m_currentFrameTime.nextInterval(MediaManager::singletonInstance()->session()->frameDuration());

if (m_currentFrameTime.start() >= m_clipEnd) {
if (m_audioTrack)
m_audioTrack->stop();
if (m_videoTrack)
m_videoTrack->stop();
emit clipEnded();
return;
}
}

void MediaClip::setActive(bool active)
Expand Down
2 changes: 1 addition & 1 deletion src/MediaFX/media_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ MediaManager::MediaManager(Session* session, QObject* parent)
: QObject(parent)
, m_session(session)
{
connect(this, &MediaManager::finishEncoding, [this](bool immediate) { this->setEncodingState(immediate ? EncodingState::Stopped : EncodingState::Stopping); emit this->session()->exitApp(0); });
connect(this, &MediaManager::finishEncoding, [this]() { this->finishedEncoding = true; });
}

MediaManager* MediaManager::singletonInstance()
Expand Down
13 changes: 3 additions & 10 deletions src/MediaFX/media_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,22 +48,15 @@ class MediaManager : public QObject {

void render();

enum EncodingState {
Encoding,
Stopping,
Stopped,
};

EncodingState encodingState() const { return m_encodingState; };
void setEncodingState(EncodingState state) { m_encodingState = state; };
bool isFinishedEncoding() { return finishedEncoding; }

signals:
void finishEncoding(bool immediate = true);
void finishEncoding();

private:
Session* m_session;
QList<MediaClip*> activeClips;
EncodingState m_encodingState = Encoding;
bool finishedEncoding = false;
};

// https://doc.qt.io/qt-6/qqmlengine.html#QML_SINGLETON
Expand Down
15 changes: 5 additions & 10 deletions src/MediaFX/session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,21 +144,16 @@ void Session::render()
{
manager->render();

switch (manager->encodingState()) {
case MediaManager::EncodingState::Encoding:
break;
case MediaManager::EncodingState::Stopped:
return;
case MediaManager::EncodingState::Stopping:
manager->setEncodingState(MediaManager::EncodingState::Stopped);
break;
}

auto frameData = renderControl->renderVideoFrame();

if (write(encoder->videofd(), frameData.size(), frameData.constData()) == -1)
return;

if (manager->isFinishedEncoding()) {
emit exitApp(0);
return;
}

animationDriver->advance();

QCoreApplication::postEvent(this, new QEvent(renderEventType));
Expand Down
2 changes: 1 addition & 1 deletion tests/qml/animated.qml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Item {
transitions: Transition {
onRunningChanged: {
if (!running)
MediaManager.finishEncoding(false);
MediaManager.finishEncoding();
}

AnchorAnimation {
Expand Down
2 changes: 1 addition & 1 deletion tests/qml/static.qml
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ import MediaFX
Rectangle {
color: "red"

Component.onCompleted: MediaManager.finishEncoding(false)
Component.onCompleted: MediaManager.finishEncoding()
}

0 comments on commit b2f65c7

Please sign in to comment.