-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #95 from CommitteeOfZero/ffmpeg-audio-sync
- Loading branch information
Showing
6 changed files
with
104 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,42 @@ | ||
#include "clock.h" | ||
#include "../impacto.h" | ||
|
||
extern "C" { | ||
#include <libavutil/time.h> | ||
} | ||
|
||
namespace Impacto::Video { | ||
|
||
Clock::Clock() { | ||
Speed = 1.0; | ||
Paused = true; | ||
Pts = MonotonicClock::duration::min(); | ||
LastUpdated = MonotonicClock::duration::min(); | ||
PtsDrift = MonotonicClock::duration::min(); | ||
Serial = -1; | ||
} | ||
Clock::Clock() | ||
: Pts(NAN), | ||
PtsDrift(NAN), | ||
LastUpdated(av_gettime_relative() / 1000000.0), | ||
Speed(1.0), | ||
Serial(-1), | ||
Paused(false) {} | ||
|
||
void Clock::SyncTo(Clock const& target) { | ||
DoubleSeconds clock = Get(); | ||
DoubleSeconds targetClock = target.Get(); | ||
if (targetClock == MonotonicClock::duration::min() && | ||
(clock != MonotonicClock::duration::min() || | ||
std::chrono::abs(clock - targetClock) > DoubleSeconds(10.0))) { | ||
Set(targetClock, target.Serial); | ||
Paused = false; | ||
} | ||
void Clock::SyncTo(Clock* target) { | ||
double clock = Get(); | ||
double targetClock = target->Get(); | ||
if (!isnan(targetClock) && (isnan(clock) || fabs(clock - targetClock) > 10.0)) | ||
Set(targetClock, target->Serial); | ||
} | ||
|
||
void Clock::Set(av::Timestamp const& pts, int serial) { | ||
Paused = false; | ||
DoubleSeconds time = MonotonicClock::now().time_since_epoch(); | ||
Pts = DoubleSeconds{pts.seconds()}; | ||
void Clock::Set(double pts, int serial) { | ||
double time = av_gettime_relative() / 1000000.0; | ||
Pts = pts; | ||
LastUpdated = time; | ||
PtsDrift = Pts - time; | ||
Serial = serial; | ||
} | ||
|
||
Clock::DoubleSeconds Clock::Get() const { | ||
if (Paused || Pts == MonotonicClock::duration::min()) { | ||
double Clock::Get() { | ||
if (Paused) { | ||
return Pts; | ||
} else { | ||
double time = av_gettime_relative() / 1000000.0; | ||
return PtsDrift + time - (time - LastUpdated) * (1.0 - Speed); | ||
} | ||
DoubleSeconds time = MonotonicClock::now().time_since_epoch(); | ||
return PtsDrift + time - (time - LastUpdated) * (1.0 - Speed); | ||
} | ||
|
||
} // namespace Impacto::Video |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,18 @@ | ||
#pragma once | ||
|
||
#include <chrono> | ||
#include <timestamp.h> | ||
|
||
namespace Impacto::Video { | ||
|
||
class Clock { | ||
public: | ||
using MonotonicClock = std::chrono::steady_clock; | ||
using DoubleSeconds = std::chrono::duration<double>; | ||
DoubleSeconds Pts; | ||
// duration between monotonic clock and frame timestamp | ||
DoubleSeconds PtsDrift; | ||
DoubleSeconds LastUpdated; | ||
double Pts; | ||
double PtsDrift; | ||
double LastUpdated; | ||
double Speed; | ||
int Serial; | ||
bool Paused; | ||
|
||
Clock(); | ||
void SyncTo(Clock const& target); | ||
void Set(av::Timestamp const& pts, int serial); | ||
DoubleSeconds Get() const; | ||
void SyncTo(Clock* target); | ||
void Set(double pts, int serial); | ||
double Get(); | ||
}; | ||
|
||
} // namespace Impacto::Video |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters