Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
darbyjohnston committed Sep 18, 2024
1 parent 378c2a5 commit d810338
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 57 deletions.
47 changes: 22 additions & 25 deletions lib/tlTimeline/Player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,18 @@ namespace tl

p.playerOptions = playerOptions;
p.timeline = timeline;
p.ioInfo = p.timeline->getIOInfo();
p.timeRange = timeline->getTimeRange();
p.ioInfo = timeline->getIOInfo();

// Create observers.
p.speed = observer::Value<double>::create(p.timeline->getTimeRange().duration().rate());
p.speed = observer::Value<double>::create(p.timeRange.duration().rate());
p.playback = observer::Value<Playback>::create(Playback::Stop);
p.loop = observer::Value<Loop>::create(Loop::Loop);
p.currentTime = observer::Value<otime::RationalTime>::create(
playerOptions.currentTime != time::invalidTime ?
playerOptions.currentTime :
p.timeline->getTimeRange().start_time());
p.inOutRange = observer::Value<otime::TimeRange>::create(p.timeline->getTimeRange());
p.timeRange.start_time());
p.inOutRange = observer::Value<otime::TimeRange>::create(p.timeRange);
p.compare = observer::List<std::shared_ptr<Timeline> >::create();
p.compareTime = observer::Value<CompareTimeMode>::create(CompareTimeMode::Relative);
p.ioOptions = observer::Value<io::Options>::create();
Expand Down Expand Up @@ -203,7 +204,7 @@ namespace tl

const otime::TimeRange& Player::getTimeRange() const
{
return _p->timeline->getTimeRange();
return _p->timeRange;
}

const io::Info& Player::getIOInfo() const
Expand All @@ -213,7 +214,7 @@ namespace tl

double Player::getDefaultSpeed() const
{
return _p->timeline->getTimeRange().duration().rate();
return _p->timeRange.duration().rate();
}

double Player::getSpeed() const
Expand Down Expand Up @@ -360,10 +361,9 @@ namespace tl
TLRENDER_P();

// Loop the time.
const auto& timeRange = p.timeline->getTimeRange();
const auto tmp = loop(
time.rescaled_to(timeRange.duration()).floor(),
timeRange);
time.rescaled_to(p.timeRange.duration()).floor(),
p.timeRange);

if (p.currentTime->setIfChanged(tmp))
{
Expand All @@ -389,7 +389,6 @@ namespace tl
void Player::timeAction(TimeAction time)
{
TLRENDER_P();
const auto& timeRange = p.timeline->getTimeRange();
const auto& currentTime = p.currentTime->get();
switch (time)
{
Expand All @@ -403,27 +402,27 @@ namespace tl
break;
case TimeAction::FramePrev:
setPlayback(timeline::Playback::Stop);
seek(currentTime - otime::RationalTime(1, timeRange.duration().rate()));
seek(currentTime - otime::RationalTime(1, p.timeRange.duration().rate()));
break;
case TimeAction::FramePrevX10:
setPlayback(timeline::Playback::Stop);
seek(currentTime - otime::RationalTime(10, timeRange.duration().rate()));
seek(currentTime - otime::RationalTime(10, p.timeRange.duration().rate()));
break;
case TimeAction::FramePrevX100:
setPlayback(timeline::Playback::Stop);
seek(currentTime - otime::RationalTime(100, timeRange.duration().rate()));
seek(currentTime - otime::RationalTime(100, p.timeRange.duration().rate()));
break;
case TimeAction::FrameNext:
setPlayback(timeline::Playback::Stop);
seek(currentTime + otime::RationalTime(1, timeRange.duration().rate()));
seek(currentTime + otime::RationalTime(1, p.timeRange.duration().rate()));
break;
case TimeAction::FrameNextX10:
setPlayback(timeline::Playback::Stop);
seek(currentTime + otime::RationalTime(10, timeRange.duration().rate()));
seek(currentTime + otime::RationalTime(10, p.timeRange.duration().rate()));
break;
case TimeAction::FrameNextX100:
setPlayback(timeline::Playback::Stop);
seek(currentTime + otime::RationalTime(100, timeRange.duration().rate()));
seek(currentTime + otime::RationalTime(100, p.timeRange.duration().rate()));
break;
case TimeAction::JumpBack1s:
seek(currentTime - otime::RationalTime(1.0, 1.0));
Expand Down Expand Up @@ -494,7 +493,7 @@ namespace tl
{
TLRENDER_P();
setInOutRange(otime::TimeRange::range_from_start_end_time(
p.timeline->getTimeRange().start_time(),
p.timeRange.start_time(),
p.inOutRange->get().end_time_exclusive()));
}

Expand All @@ -511,7 +510,7 @@ namespace tl
TLRENDER_P();
setInOutRange(otime::TimeRange::range_from_start_end_time_inclusive(
p.inOutRange->get().start_time(),
p.timeline->getTimeRange().end_time_inclusive()));
p.timeRange.end_time_inclusive()));
}

const std::vector<std::shared_ptr<Timeline> >& Player::getCompare() const
Expand Down Expand Up @@ -675,8 +674,7 @@ namespace tl
p.timeline->tick();

// Calculate the current time.
const auto& timeRange = p.timeline->getTimeRange();
const double timelineSpeed = timeRange.duration().rate();
const double timelineSpeed = p.timeRange.duration().rate();
const auto playback = p.playback->get();
if (playback != Playback::Stop && timelineSpeed > 0.0)
{
Expand Down Expand Up @@ -708,7 +706,7 @@ namespace tl
t -= frame * p.speed->get() / timelineSpeed;
}
const otime::RationalTime currentTime = p.loopPlayback(
p.timeline->getTimeRange().start_time() +
p.timeRange.start_time() +
otime::RationalTime(t, rate).rescaled_to(p.speed->get()).floor());
//const double currentTimeDiff = abs(currentTime.value() - p.currentTime->get().value());
if (p.currentTime->setIfChanged(currentTime))
Expand Down Expand Up @@ -782,7 +780,6 @@ namespace tl
p.cacheUpdate();

// Update the current video data.
const auto& timeRange = p.timeline->getTimeRange();
if (!p.ioInfo.video.empty())
{
const auto i = p.thread.videoDataCache.find(p.thread.currentTime);
Expand All @@ -793,7 +790,7 @@ namespace tl
}
else if (p.thread.playback != Playback::Stop)
{
if (!timeRange.contains(p.thread.currentTime))
if (!p.timeRange.contains(p.thread.currentTime))
{
std::unique_lock<std::mutex> lock(p.mutex.mutex);
p.mutex.currentVideoData.clear();
Expand All @@ -812,7 +809,7 @@ namespace tl
else
{
std::unique_lock<std::mutex> lock(p.mutex.mutex);
if (!timeRange.contains(p.thread.currentTime))
if (!p.timeRange.contains(p.thread.currentTime))
{
p.mutex.currentVideoData.clear();
}
Expand All @@ -825,7 +822,7 @@ namespace tl
std::vector<AudioData> audioDataList;
{
const int64_t seconds = p.thread.currentTime.rescaled_to(1.0).value() -
timeRange.start_time().rescaled_to(1.0).value();
p.timeRange.start_time().rescaled_to(1.0).value();
std::unique_lock<std::mutex> lock(p.audioMutex.mutex);
for (int64_t s : { seconds - 1, seconds, seconds + 1 })
{
Expand Down
43 changes: 16 additions & 27 deletions lib/tlTimeline/PlayerAudio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,7 @@ namespace tl
void Player::Private::audioReset(const otime::RationalTime& time)
{
audioMutex.reset = true;
audioMutex.start = (time - timeline->getTimeRange().start_time()).rescaled_to(ioInfo.audio.sampleRate).value();
audioMutex.frame = 0;
audioMutex.start = (time - timeRange.start_time()).rescaled_to(ioInfo.audio.sampleRate).value();
}

size_t Player::Private::getAudioChannelCount(
Expand Down Expand Up @@ -277,7 +276,6 @@ namespace tl
std::chrono::steady_clock::time_point muteTimeout;
bool reset = false;
int64_t start = 0;
int64_t frame = 0;
{
std::unique_lock<std::mutex> lock(p->audioMutex.mutex);
speed = p->audioMutex.speed;
Expand All @@ -287,32 +285,24 @@ namespace tl
reset = p->audioMutex.reset;
p->audioMutex.reset = false;
start = p->audioMutex.start;
frame = p->audioMutex.frame;
}
//std::cout << "playback: " << playback << std::endl;
//std::cout << "playbackStartTime: " << playbackStartTime << std::endl;
//std::cout << "reset: " << reset << std::endl;

// Check if the timers should be initialized.
const audio::Info& inputInfo = p->ioInfo.audio;
if (playback != p->audioThread.playback ||
speed != p->audioThread.speed ||
reset)
{
p->audioThread.playback = playback;
p->audioThread.speed = speed;
}
//std::cout << "start: " << start << std::endl;

// Zero output audio data.
const audio::Info& outputInfo = p->audioThread.info;
std::memset(outputBuffer, 0, nFrames * outputInfo.getByteCount());

const audio::Info& inputInfo = p->ioInfo.audio;
if (playback != Playback::Stop && inputInfo.sampleRate > 0)
{
// Flush the audio resampler and buffer when the RtAudio
// playback is reset.
if (reset)
{
p->audioThread.frame = 0;
if (p->audioThread.resample)
{
p->audioThread.resample->flush();
Expand All @@ -330,19 +320,17 @@ namespace tl
}

// Get audio from the cache.
const int64_t bufferSize = getSampleCount(p->audioThread.buffer);
int64_t size = otio::RationalTime(
nFrames * 2 - bufferSize,
outputInfo.sampleRate).
int64_t size = otio::RationalTime(nFrames * 2, outputInfo.sampleRate).
rescaled_to(inputInfo.sampleRate).value();
size -= getSampleCount(p->audioThread.buffer);
int64_t t = start;
if (Playback::Forward == playback)
{
t += frame;
t += p->audioThread.frame;
}
else
{
t -= frame;
t -= p->audioThread.frame;
}
int64_t seconds = t / inputInfo.sampleRate;
int64_t offset = t - (seconds * inputInfo.sampleRate);
Expand Down Expand Up @@ -405,7 +393,7 @@ namespace tl
const auto now = std::chrono::steady_clock::now();
if (mute ||
now < muteTimeout ||
speed != p->timeline->getTimeRange().duration().rate())
speed != p->timeRange.duration().rate())
{
volume = 0.F;
}
Expand Down Expand Up @@ -446,18 +434,19 @@ namespace tl

// Update the frame counter.
int64_t inc = 0;
if (!found)
if (found && size > 0)
{
inc = OTIO_NS::RationalTime(nFrames, outputInfo.sampleRate).
rescaled_to(inputInfo.sampleRate).value();
inc = size;
}
else if (size > 0)
else
{
inc = size;
inc = otio::RationalTime(nFrames, outputInfo.sampleRate).
rescaled_to(inputInfo.sampleRate).value();
}
p->audioThread.frame += inc;
{
std::unique_lock<std::mutex> lock(p->audioMutex.mutex);
p->audioMutex.frame += inc;
p->audioMutex.frame = p->audioThread.frame;
}
}

Expand Down
4 changes: 1 addition & 3 deletions lib/tlTimeline/PlayerPrivate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,6 @@ namespace tl
void Player::Private::cacheUpdate()
{
// Get the video ranges to be cached.
const otime::TimeRange& timeRange = timeline->getTimeRange();
const otime::RationalTime readAheadDivided(
thread.cacheOptions.readAhead.value() / static_cast<double>(1 + thread.compare.size()),
thread.cacheOptions.readAhead.rate());
Expand Down Expand Up @@ -533,7 +532,7 @@ namespace tl
void Player::Private::playbackReset(const otime::RationalTime& time)
{
noAudio.playbackTimer = std::chrono::steady_clock::now();
noAudio.start = (time - timeline->getTimeRange().start_time()).rescaled_to(ioInfo.videoTime.duration().rate()).value();
noAudio.start = (time - timeRange.start_time()).rescaled_to(ioInfo.videoTime.duration().rate()).value();
}

void Player::Private::log(const std::shared_ptr<system::Context>& context)
Expand All @@ -559,7 +558,6 @@ namespace tl
}

// Create an array of characters to draw the timeline.
const auto& timeRange = timeline->getTimeRange();
const size_t lineLength = 80;
std::string currentTimeDisplay(lineLength, '.');
double n = (currentTime - timeRange.start_time()).value() / timeRange.duration().value();
Expand Down
4 changes: 2 additions & 2 deletions lib/tlTimeline/PlayerPrivate.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ namespace tl

PlayerOptions playerOptions;
std::shared_ptr<Timeline> timeline;
otime::TimeRange timeRange;
io::Info ioInfo;

std::shared_ptr<observer::Value<double> > speed;
Expand Down Expand Up @@ -146,8 +147,7 @@ namespace tl
struct AudioThread
{
audio::Info info;
Playback playback = Playback::Stop;
double speed = 0.0;
int64_t frame = 0;
std::shared_ptr<audio::AudioResample> resample;
std::list<std::shared_ptr<audio::Audio> > buffer;
std::shared_ptr<audio::Audio> silence;
Expand Down

0 comments on commit d810338

Please sign in to comment.