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