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 
23 namespace nap
24 {
26 
27  // forward declares
28  class SequenceService;
29 
36  class NAPAPI SequencePlayer : public Device
37  {
38  friend class SequenceEditor;
39  friend class SequenceController;
40 
41  RTTI_ENABLE(Device)
42  public:
47 
54  bool init(utility::ErrorState& errorState) override;
55 
62  bool save(const std::string& name, utility::ErrorState& errorState);
63 
70  bool load(const std::string& name, utility::ErrorState& errorState);
71 
76  void setIsPlaying(bool isPlaying);
77 
82  void setIsPaused(bool isPaused);
83 
88  void setIsLooping(bool isLooping);
89 
94  void setPlayerTime(double time);
95 
100  void setPlaybackSpeed(float speed);
101 
105  double getPlayerTime() const;
106 
110  double getDuration() const;
111 
115  bool getIsPlaying() const;
116 
120  bool getIsLooping() const;
121 
125  bool getIsPaused() const;
126 
130  float getPlaybackSpeed() const;
131 
135  void stop() override;
136 
141  bool start(utility::ErrorState& errorState) override;
142 
147  const Sequence& getSequenceConst() const;
148 
152  const std::string& getSequenceFilename() const;
153 
154  // signals
155  /***
156  * playbackSpeedChanged signal is dispatched when setPlaybackSpeed(float) method is called on SequencePlayer
157  * This is useful when you want to sync or invoke other methods with the SequencePlayer
158  * Note: Signal is dispatched from whichever thread is calling the setPlaybackSpeed(float) method on SequencePlayer
159  */
161 
162  /***
163  * playerTimeChanged signal is dispatched when setPlayerTime(double) method is called on SequencePlayer
164  * This is useful when you want to sync or invoke other methods with the SequencePlayer
165  * Note: Signal is dispatched from whichever thread is calling the setPlayerTime(double) method on SequencePlayer
166  */
168 
169  /***
170  * playStateChanged signal is dispatched when setIsPlaying(bool) method is called on SequencePlayer
171  * This is useful when you want to sync or invoke other methods with the SequencePlayer
172  * Note: Signal is dispatched from whichever thread is calling the setIsPlaying(bool) method on SequencePlayer
173  * 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
174  */
176 
177  /***
178  * pauseStateChanged signal is dispatched when setIsPaused(bool) method is called on SequencePlayer
179  * This is useful when you want to sync or invoke other methods with the SequencePlayer
180  * Note: Signal is dispatched from whichever thread is calling the setIsPaused(bool) method on SequencePlayer
181  * 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
182  */
184 
185  /***
186  * preTick Signal is triggered on player thread, before updating the adapters
187  */
189 
194 
195  /***
196  * sequenceLoaded signal is dispatched when the load(name) method succeeds on SequencePlayer
197  */
199 
204 
205  // properties
206  std::string mSequenceFileName;
207  bool mCreateEmptySequenceOnLoadFail = true;
208  std::vector<ResourcePtr<SequencePlayerOutput>> mOutputs;
210  protected:
218  Signal<std::function<void(const std::string&, std::unique_ptr<SequencePlayerAdapter>)>&> adaptersCreated;
219 
220  // Reference to sequence service
222  private:
226  Sequence& getSequence();
227 
234  bool createAdapter(const std::string& objectID, const std::string& trackID);
235 
236  void tick(double deltaTime);
237 
238  // read objects from sequence
239  std::vector<std::unique_ptr<rtti::Object>> mReadObjects;
240 
241  // read object ids from sequence
242  std::unordered_set<std::string> mReadObjectIDs;
243 
248  void createAdapters();
249 
253  void destroyAdapters();
254 
259  void performEditAction(std::function<void()> &action);
260 
261  // mutex
262  std::mutex mMutex;
263 
264  // raw pointer to loaded sequence
265  Sequence *mSequence = nullptr;
266 
267  // is playing
268  bool mIsPlaying = false;
269 
270  // is paused
271  bool mIsPaused = false;
272 
273  // is looping
274  bool mIsLooping = false;
275 
276  // speed
277  float mSpeed = 1.0f;
278 
279  // current time
280  double mTime = 0.0;
281 
282  // list of instantiated adapters
283  std::unordered_map<std::string, std::unique_ptr<SequencePlayerAdapter>> mAdapters;
284  };
285 
287 }
nap::SequencePlayer
Definition: sequenceplayer.h:36
nap::SequencePlayer::mOutputs
std::vector< ResourcePtr< SequencePlayerOutput > > mOutputs
Property: 'Outputs' linked outputs.
Definition: sequenceplayer.h:208
nap::SequencePlayer::playerTimeChanged
Signal< SequencePlayer &, double > playerTimeChanged
Definition: sequenceplayer.h:167
nap::SequencePlayer::postTick
Signal< SequencePlayer & > postTick
Definition: sequenceplayer.h:193
nap::SequenceEditor
Definition: sequenceeditor.h:32
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:218
nap::rtti::ObjectCreator
Definition: factory.h:49
nap::SequencePlayer::pauseStateChanged
Signal< SequencePlayer &, bool > pauseStateChanged
Definition: sequenceplayer.h:183
nap::utility::ErrorState
Definition: errorstate.h:19
nap::SequencePlayer::playbackSpeedChanged
Signal< SequencePlayer &, float > playbackSpeedChanged
Definition: sequenceplayer.h:160
nap::SequencePlayer::mClock
ResourcePtr< SequencePlayerClock > mClock
Property: 'Clock' Controls timing of playback.
Definition: sequenceplayer.h:209
nap::Signal
Definition: signalslot.h:28
nap::SequencePlayer::edited
Signal< SequencePlayer & > edited
Definition: sequenceplayer.h:203
nap::SequencePlayer::mSequenceFileName
std::string mSequenceFileName
Property: 'Default Sequence' linked default Sequence file.
Definition: sequenceplayer.h:206
nap
Definition: templateapp.h:17
nap::SequencePlayer::sequenceLoaded
Signal< SequencePlayer &, std::string > sequenceLoaded
Definition: sequenceplayer.h:198
nap::Sequence
Definition: sequence.h:22
nap::SequencePlayer::playStateChanged
Signal< SequencePlayer &, bool > playStateChanged
Definition: sequenceplayer.h:175
nap::SequencePlayer::preTick
Signal< SequencePlayer & > preTick
Definition: sequenceplayer.h:188
nap::SequencePlayer::mService
SequenceService & mService
Definition: sequenceplayer.h:221
nap::Device
Definition: device.h:20