1/*
2 * Copyright 2020 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "modules/skottie/src/SkottiePriv.h"
9
10#include "modules/skottie/src/SkottieJson.h"
11#include "modules/skottie/src/SkottieValue.h"
12#include "modules/skottie/src/animator/Animator.h"
13#include "modules/sksg/include/SkSGRenderNode.h"
14
15namespace skottie::internal {
16
17namespace {
18
19class ForwardingPlaybackController final : public Animator {
20public:
21 ForwardingPlaybackController(sk_sp<skresources::ExternalTrackAsset> track,
22 float in_point,
23 float out_point,
24 float fps )
25 : fTrack(std::move(track))
26 , fInPoint(in_point)
27 , fOutPoint(out_point)
28 , fFps(fps) {}
29
30private:
31 StateChanged onSeek(float t) override {
32 // Adjust t relative to the track time (s).
33 if (t < fInPoint || t > fOutPoint) {
34 t = -1;
35 } else {
36 t = (t - fInPoint) / fFps;
37 }
38
39 fTrack->seek(t);
40
41 // does not interact with the render tree.
42 return false;
43 }
44
45 const sk_sp<skresources::ExternalTrackAsset> fTrack;
46 const float fInPoint,
47 fOutPoint,
48 fFps;
49};
50
51} // namespace
52
53sk_sp<sksg::RenderNode> AnimationBuilder::attachAudioLayer(const skjson::ObjectValue& jlayer,
54 LayerInfo* layer_info) const {
55 const ScopedAssetRef audio_asset(this, jlayer);
56
57 if (audio_asset) {
58 const auto& jaudio = *audio_asset;
59 const skjson::StringValue* name = jaudio["p"];
60 const skjson::StringValue* path = jaudio["u"];
61 const skjson::StringValue* id = jaudio["id"];
62
63 if (name && path && id) {
64 auto track = fResourceProvider->loadAudioAsset(path->begin(),
65 name->begin(),
66 id ->begin());
67 if (track) {
68 fCurrentAnimatorScope->push_back(
69 sk_make_sp<ForwardingPlaybackController>(std::move(track),
70 layer_info->fInPoint,
71 layer_info->fOutPoint,
72 fFrameRate));
73 } else {
74 this->log(Logger::Level::kWarning, nullptr,
75 "Could not load audio asset '%s'.", name->begin());
76 }
77 }
78 }
79
80 // no render node, playback is controlled from the Animator tree.
81 return nullptr;
82}
83
84} // namespace skottie::internal
85