Skip to content

Commit

Permalink
[skottie] Add audio track support to viewer
Browse files Browse the repository at this point in the history
Based on SkAudioPlayer, Mac-only for now.

Change-Id: Ic4b0e2814aa341586634cbd270d7afd90e32716b
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/310056
Commit-Queue: Florin Malita <fmalita@chromium.org>
Commit-Queue: Florin Malita <fmalita@google.com>
Reviewed-by: Mike Reed <reed@google.com>
  • Loading branch information
fmalita authored and Skia Commit-Bot committed Aug 13, 2020
1 parent 3637440 commit cc01311
Showing 1 changed file with 55 additions and 1 deletion.
56 changes: 55 additions & 1 deletion tools/viewer/SkottieSlide.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "include/core/SkCanvas.h"
#include "include/core/SkFont.h"
#include "include/core/SkTime.h"
#include "include/ports/SkAudioPlayer.h"
#include "modules/skottie/include/Skottie.h"
#include "modules/skottie/utils/SkottieUtils.h"
#include "modules/skresources/include/SkResources.h"
Expand All @@ -22,6 +23,58 @@

#include "imgui.h"

namespace {

class Track final : public skresources::ExternalTrackAsset {
public:
explicit Track(std::unique_ptr<SkAudioPlayer> player) : fPlayer(std::move(player)) {}

private:
void seek(float t) override {
if (fPlayer->isStopped() && t >=0) {
fPlayer->play();
}

if (fPlayer->isPlaying()) {
if (t < 0) {
fPlayer->stop();
} else {
static constexpr float kTolerance = 0.075f;
const auto player_pos = fPlayer->time();

if (std::abs(player_pos - t) > kTolerance) {
fPlayer->setTime(t);
}
}
}
}

const std::unique_ptr<SkAudioPlayer> fPlayer;
};

class AudioProviderProxy final : public skresources::ResourceProviderProxyBase {
public:
explicit AudioProviderProxy(sk_sp<skresources::ResourceProvider> rp)
: INHERITED(std::move(rp)) {}

private:
sk_sp<skresources::ExternalTrackAsset> loadAudioAsset(const char path[],
const char name[],
const char[] /*id*/) override {
if (auto data = this->load(path, name)) {
if (auto player = SkAudioPlayer::Make(std::move(data))) {
return sk_make_sp<Track>(std::move(player));
}
}

return nullptr;
}

using INHERITED = skresources::ResourceProviderProxyBase;
};

} // namespace

static void draw_stats_box(SkCanvas* canvas, const skottie::Animation::Builder::Stats& stats) {
static constexpr SkRect kR = { 10, 10, 280, 120 };
static constexpr SkScalar kTextSize = 20;
Expand Down Expand Up @@ -104,10 +157,11 @@ void SkottieSlide::load(SkScalar w, SkScalar h) {
skottie::Animation::Builder builder(flags);

auto resource_provider =
sk_make_sp<AudioProviderProxy>(
skresources::DataURIResourceProviderProxy::Make(
skresources::FileResourceProvider::Make(SkOSPath::Dirname(fPath.c_str()),
/*predecode=*/true),
/*predecode=*/true);
/*predecode=*/true));

static constexpr char kInterceptPrefix[] = "__";
auto precomp_interceptor =
Expand Down

0 comments on commit cc01311

Please sign in to comment.