NAP
sequenceplayer.h
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4 
5 #pragma once
6 
7 // internal includes
8 #include "sequence.h"
9 #include "sequenceplayeradapter.h"
10 #include "sequenceplayeroutput.h"
11 #include "sequenceservice.h"
12 #include "sequenceplayerclock.h"
13 
14 // external includes
15 #include <rtti/factory.h>
16 #include <nap/device.h>
17 #include <nap/signalslot.h>
18 #include <future>
19 #include <mutex>
20 #include <nap/timer.h>
21 #include <concurrentqueue.h>
22 #include <nap/numeric.h>
23 
24 namespace nap
25 {
27 
28  // forward declares
29  class SequenceService;
30 
37  class NAPAPI SequencePlayer : public Device
38  {
39  friend class SequenceEditor;
40  friend class SequenceController;
41 
42  RTTI_ENABLE(Device)
43  public:
48 
55  bool init(utility::ErrorState& errorState) override;
56 
63  bool save(const std::string& name, utility::ErrorState& errorState);
64 
71  bool load(const std::string& name, utility::ErrorState& errorState);
72 
79  bool loadBinary(const std::vector<nap::uint8>& buffer, utility::ErrorState& errorState);
80 
85  void setIsPlaying(bool isPlaying);
86 
91  void setIsPaused(bool isPaused);
92 
97  void setIsLooping(bool isLooping);
98 
103  void setPlayerTime(double time);
104 
109  void setPlaybackSpeed(float speed);
110 
114  double getPlayerTime() const;
115 
119  double getDuration() const;
120 
124  bool getIsPlaying() const;
125 
129  bool getIsLooping() const;
130 
134  bool getIsPaused() const;
135 
139  float getPlaybackSpeed() const;
140 
144  void stop() override;
145 
150  bool start(utility::ErrorState& errorState) override;
151 
156  const Sequence& getSequenceConst() const;
157 
161  const std::string& getSequenceFilename() const;
162 
163  // signals
164  /***
165  * playbackSpeedChanged signal is dispatched when setPlaybackSpeed(float) method is called on SequencePlayer
166  * This is useful when you want to sync or invoke other methods with the SequencePlayer
167  * Note: Signal is dispatched from whichever thread is calling the setPlaybackSpeed(float) method on SequencePlayer
168  */
170 
171  /***
172  * playerTimeChanged signal is dispatched when setPlayerTime(double) method is called on SequencePlayer
173  * This is useful when you want to sync or invoke other methods with the SequencePlayer
174  * Note: Signal is dispatched from whichever thread is calling the setPlayerTime(double) method on SequencePlayer
175  */
177 
178  /***
179  * playStateChanged signal is dispatched when setIsPlaying(bool) method is called on SequencePlayer
180  * This is useful when you want to sync or invoke other methods with the SequencePlayer
181  * Note: Signal is dispatched from whichever thread is calling the setIsPlaying(bool) method on SequencePlayer
182  * Note: Play state doesn't mean Paused or not. The SequencePlayer can be Paused but still be Playing, in which case time doesn't get updated but adapter will still be called
183  */
185 
186  /***
187  * pauseStateChanged signal is dispatched when setIsPaused(bool) method is called on SequencePlayer
188  * This is useful when you want to sync or invoke other methods with the SequencePlayer
189  * Note: Signal is dispatched from whichever thread is calling the setIsPaused(bool) method on SequencePlayer
190  * Note: Play state doesn't mean Paused or not. The SequencePlayer can be Paused but still be Playing, in which case time doesn't get updated but adapter will still be called
191  */
193 
194  /***
195  * preTick Signal is triggered on player thread, before updating the adapters
196  */
198 
203 
204  /***
205  * sequenceLoaded signal is dispatched when the load(name) method succeeds on SequencePlayer
206  */
208 
213 
214  // properties
215  std::string mSequenceFileName;
216  bool mCreateEmptySequenceOnLoadFail = true;
217  std::vector<ResourcePtr<SequencePlayerOutput>> mOutputs;
219  protected:
227  Signal<std::function<void(const std::string&, std::unique_ptr<SequencePlayerAdapter>)>&> adaptersCreated;
228 
229  // Reference to sequence service
231  private:
235  Sequence& getSequence();
236 
243  bool createAdapter(const std::string& objectID, const std::string& trackID);
244 
245  void tick(double deltaTime);
246 
247  // read objects from sequence
248  std::vector<std::unique_ptr<rtti::Object>> mReadObjects;
249 
250  // read object ids from sequence
251  std::unordered_set<std::string> mReadObjectIDs;
252 
257  void createAdapters();
258 
262  void destroyAdapters();
263 
268  void performEditAction(std::function<void()> &action);
269 
270  // mutex
271  std::mutex mMutex;
272 
273  // raw pointer to loaded sequence
274  Sequence *mSequence = nullptr;
275 
276  // is playing
277  bool mIsPlaying = false;
278 
279  // is paused
280  bool mIsPaused = false;
281 
282  // is looping
283  bool mIsLooping = false;
284 
285  // speed
286  float mSpeed = 1.0f;
287 
288  // current time
289  double mTime = 0.0;
290 
291  // list of instantiated adapters
292  std::unordered_map<std::string, std::unique_ptr<SequencePlayerAdapter>> mAdapters;
293  };
294 
296 }
nap::SequencePlayer
Definition: sequenceplayer.h:37
nap::SequencePlayer::mOutputs
std::vector< ResourcePtr< SequencePlayerOutput > > mOutputs
Property: 'Outputs' linked outputs.
Definition: sequenceplayer.h:217
nap::SequencePlayer::playerTimeChanged
Signal< SequencePlayer &, double > playerTimeChanged
Definition: sequenceplayer.h:176
nap::SequencePlayer::postTick
Signal< SequencePlayer & > postTick
Definition: sequenceplayer.h:202
nap::SequenceEditor
Definition: sequenceeditor.h:49
nap::SequenceController
Definition: sequencecontroller.h:25
nap::rtti::ObjectPtr
Definition: objectptr.h:154
nap::SequenceService
Definition: sequenceservice.h:42
nap::SequencePlayer::adaptersCreated
Signal< std::function< void(const std::string &, std::unique_ptr< SequencePlayerAdapter >)> & > adaptersCreated
Definition: sequenceplayer.h:227
nap::rtti::ObjectCreator
Definition: factory.h:49
nap::SequencePlayer::pauseStateChanged
Signal< SequencePlayer &, bool > pauseStateChanged
Definition: sequenceplayer.h:192
nap::utility::ErrorState
Definition: errorstate.h:19
nap::SequencePlayer::playbackSpeedChanged
Signal< SequencePlayer &, float > playbackSpeedChanged
Definition: sequenceplayer.h:169
nap::SequencePlayer::mClock
ResourcePtr< SequencePlayerClock > mClock
Property: 'Clock' Controls timing of playback.
Definition: sequenceplayer.h:218
nap::Signal
Definition: signalslot.h:28
nap::SequencePlayer::edited
Signal< SequencePlayer & > edited
Definition: sequenceplayer.h:212
nap::SequencePlayer::mSequenceFileName
std::string mSequenceFileName
Property: 'Default Sequence' linked default Sequence file.
Definition: sequenceplayer.h:215
nap
Definition: templateapp.h:17
nap::SequencePlayer::sequenceLoaded
Signal< SequencePlayer &, std::string > sequenceLoaded
Definition: sequenceplayer.h:207
nap::Sequence
Definition: sequence.h:22
nap::SequencePlayer::playStateChanged
Signal< SequencePlayer &, bool > playStateChanged
Definition: sequenceplayer.h:184
nap::SequencePlayer::preTick
Signal< SequencePlayer & > preTick
Definition: sequenceplayer.h:197
nap::SequencePlayer::mService
SequenceService & mService
Definition: sequenceplayer.h:230
nap::Device
Definition: device.h:20