OpenShot Library | libopenshot  0.2.6
ClipBase.h
Go to the documentation of this file.
1 /**
2  * @file
3  * @brief Header file for ClipBase class
4  * @author Jonathan Thomas <jonathan@openshot.org>
5  *
6  * @ref License
7  */
8 
9 /* LICENSE
10  *
11  * Copyright (c) 2008-2019 OpenShot Studios, LLC
12  * <http://www.openshotstudios.com/>. This file is part of
13  * OpenShot Library (libopenshot), an open-source project dedicated to
14  * delivering high quality video editing and animation solutions to the
15  * world. For more information visit <http://www.openshot.org/>.
16  *
17  * OpenShot Library (libopenshot) is free software: you can redistribute it
18  * and/or modify it under the terms of the GNU Lesser General Public License
19  * as published by the Free Software Foundation, either version 3 of the
20  * License, or (at your option) any later version.
21  *
22  * OpenShot Library (libopenshot) is distributed in the hope that it will be
23  * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25  * GNU Lesser General Public License for more details.
26  *
27  * You should have received a copy of the GNU Lesser General Public License
28  * along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
29  */
30 
31 #ifndef OPENSHOT_CLIPBASE_H
32 #define OPENSHOT_CLIPBASE_H
33 
34 #include <memory>
35 #include <sstream>
36 #include "CacheMemory.h"
37 #include "Frame.h"
38 #include "Point.h"
39 #include "KeyFrame.h"
40 #include "Json.h"
41 #include "TimelineBase.h"
42 
43 
44 namespace openshot {
45  /**
46  * @brief This abstract class is the base class, used by all clips in libopenshot.
47  *
48  * Clips are objects that attach to the timeline and can be layered and positioned
49  * together. There are 2 primary types of clips: Effects and Video/Audio Clips.
50  */
51  class ClipBase {
52  protected:
53  std::string id; ///< ID Property for all derived Clip and Effect classes.
54  float position; ///< The position on the timeline where this clip should start playing
55  int layer; ///< The layer this clip is on. Lower clips are covered up by higher clips.
56  float start; ///< The position in seconds to start playing (used to trim the beginning of a clip)
57  float end; ///< The position in seconds to end playing (used to trim the ending of a clip)
58  std::string previous_properties; ///< This string contains the previous JSON properties
59  openshot::TimelineBase* timeline; ///< Pointer to the parent timeline instance (if any)
60 
61  /// Generate JSON for a property
62  Json::Value add_property_json(std::string name, float value, std::string type, std::string memo, const Keyframe* keyframe, float min_value, float max_value, bool readonly, int64_t requested_frame) const;
63 
64  /// Generate JSON choice for a property (dropdown properties)
65  Json::Value add_property_choice_json(std::string name, int value, int selected_value) const;
66 
67  public:
69 
70  /// Constructor for the base clip
72  // Initialize values
73  position = 0.0;
74  layer = 0;
75  start = 0.0;
76  end = 0.0;
77  previous_properties = "";
78  timeline = NULL;
79  };
80 
81  // Compare a clip using the Position() property
82  bool operator< ( ClipBase& a) { return (Position() < a.Position()); }
83  bool operator<= ( ClipBase& a) { return (Position() <= a.Position()); }
84  bool operator> ( ClipBase& a) { return (Position() > a.Position()); }
85  bool operator>= ( ClipBase& a) { return (Position() >= a.Position()); }
86 
87  /// @brief This method is required for all derived classes of ClipBase, and returns a
88  /// new openshot::Frame object. All Clip keyframes and effects are resolved into
89  /// pixels.
90  ///
91  /// @returns A new openshot::Frame object
92  /// @param frame_number The frame number (starting at 1) of the clip or effect on the timeline.
93  virtual std::shared_ptr<openshot::Frame> GetFrame(int64_t frame_number) = 0;
94 
95  /// @brief This method is required for all derived classes of ClipBase, and returns a
96  /// modified openshot::Frame object
97  ///
98  /// The frame object is passed into this method and used as a starting point / background (pixels).
99  /// All Clip keyframes and effects are resolved into pixels.
100  ///
101  /// @returns The modified openshot::Frame object
102  /// @param frame This is ignored on Clip, due to caching optimizations. This frame instance is clobbered with the source frame.
103  /// @param frame_number The frame number (starting at 1) of the clip or effect on the timeline.
104  virtual std::shared_ptr<openshot::Frame> GetFrame(std::shared_ptr<openshot::Frame> frame, int64_t frame_number) = 0;
105 
106  // Get basic properties
107  std::string Id() const { return id; } ///< Get the Id of this clip object
108  float Position() const { return position; } ///< Get position on timeline (in seconds)
109  int Layer() const { return layer; } ///< Get layer of clip on timeline (lower number is covered by higher numbers)
110  float Start() const { return start; } ///< Get start position (in seconds) of clip (trim start of video)
111  float End() const { return end; } ///< Get end position (in seconds) of clip (trim end of video)
112  float Duration() const { return end - start; } ///< Get the length of this clip (in seconds)
113  openshot::TimelineBase* ParentTimeline() { return timeline; } ///< Get the associated Timeline pointer (if any)
114 
115  // Set basic properties
116  void Id(std::string value) { id = value; } ///> Set the Id of this clip object
117  void Position(float value) { position = value; } ///< Set position on timeline (in seconds)
118  void Layer(int value) { layer = value; } ///< Set layer of clip on timeline (lower number is covered by higher numbers)
119  void Start(float value) { start = value; } ///< Set start position (in seconds) of clip (trim start of video)
120  void End(float value) { end = value; } ///< Set end position (in seconds) of clip (trim end of video)
121  void ParentTimeline(openshot::TimelineBase* new_timeline) { timeline = new_timeline; } ///< Set associated Timeline pointer
122 
123  // Get and Set JSON methods
124  virtual std::string Json() const = 0; ///< Generate JSON string of this object
125  virtual void SetJson(const std::string value) = 0; ///< Load JSON string into this object
126  virtual Json::Value JsonValue() const = 0; ///< Generate Json::Value for this object
127  virtual void SetJsonValue(const Json::Value root) = 0; ///< Load Json::Value into this object
128 
129  /// Get all properties for a specific frame (perfect for a UI to display the current state
130  /// of all properties at any time)
131  virtual std::string PropertiesJSON(int64_t requested_frame) const = 0;
132 
133  virtual ~ClipBase() = default;
134  };
135 
136 
137 }
138 
139 #endif
bool operator>(ClipBase &a)
Definition: ClipBase.h:84
void Layer(int value)
Set layer of clip on timeline (lower number is covered by higher numbers)
Definition: ClipBase.h:118
bool operator<=(ClipBase &a)
Definition: ClipBase.h:83
This class represents a timeline (used for building generic timeline implementations) ...
Definition: TimelineBase.h:54
std::string Id() const
Get the Id of this clip object.
Definition: ClipBase.h:107
virtual void SetJson(const std::string value)=0
Load JSON string into this object.
float Start() const
Get start position (in seconds) of clip (trim start of video)
Definition: ClipBase.h:110
void Start(float value)
Set start position (in seconds) of clip (trim start of video)
Definition: ClipBase.h:119
std::string id
ID Property for all derived Clip and Effect classes.
Definition: ClipBase.h:53
Header file for Point class.
virtual std::shared_ptr< openshot::Frame > GetFrame(int64_t frame_number)=0
This method is required for all derived classes of ClipBase, and returns a new openshot::Frame object...
float position
The position on the timeline where this clip should start playing.
Definition: ClipBase.h:54
virtual std::string Json() const =0
Generate JSON string of this object.
openshot::TimelineBase * timeline
Pointer to the parent timeline instance (if any)
Definition: ClipBase.h:59
Header file for the Keyframe class.
Header file for CacheMemory class.
int layer
The layer this clip is on. Lower clips are covered up by higher clips.
Definition: ClipBase.h:55
virtual Json::Value JsonValue() const =0
Generate Json::Value for this object.
Definition: ClipBase.cpp:36
virtual void SetJsonValue(const Json::Value root)=0
Load Json::Value into this object.
Definition: ClipBase.cpp:52
Header file for Frame class.
virtual std::string PropertiesJSON(int64_t requested_frame) const =0
Header file for JSON class.
Json::Value add_property_choice_json(std::string name, int value, int selected_value) const
Generate JSON choice for a property (dropdown properties)
Definition: ClipBase.cpp:104
void Position(float value)
Set the Id of this clip object
Definition: ClipBase.h:117
void ParentTimeline(openshot::TimelineBase *new_timeline)
Set associated Timeline pointer.
Definition: ClipBase.h:121
float start
The position in seconds to start playing (used to trim the beginning of a clip)
Definition: ClipBase.h:56
ClipBase()
Constructor for the base clip.
Definition: ClipBase.h:71
This abstract class is the base class, used by all clips in libopenshot.
Definition: ClipBase.h:51
void End(float value)
Set end position (in seconds) of clip (trim end of video)
Definition: ClipBase.h:120
virtual ~ClipBase()=default
float end
The position in seconds to end playing (used to trim the ending of a clip)
Definition: ClipBase.h:57
float Duration() const
Get the length of this clip (in seconds)
Definition: ClipBase.h:112
This namespace is the default namespace for all code in the openshot library.
Definition: Compressor.h:46
Header file for Timeline class.
bool operator>=(ClipBase &a)
Definition: ClipBase.h:85
openshot::TimelineBase * ParentTimeline()
Get the associated Timeline pointer (if any)
Definition: ClipBase.h:113
float Position() const
Get position on timeline (in seconds)
Definition: ClipBase.h:108
float End() const
Get end position (in seconds) of clip (trim end of video)
Definition: ClipBase.h:111
std::string previous_properties
This string contains the previous JSON properties.
Definition: ClipBase.h:58
A Keyframe is a collection of Point instances, which is used to vary a number or property over time...
Definition: KeyFrame.h:72
Json::Value add_property_json(std::string name, float value, std::string type, std::string memo, const Keyframe *keyframe, float min_value, float max_value, bool readonly, int64_t requested_frame) const
Generate JSON for a property.
Definition: ClipBase.cpp:68
void Id(std::string value)
Definition: ClipBase.h:116
This class is a memory-based cache manager for Frame objects.
Definition: CacheMemory.h:50
CacheMemory cache
Definition: ClipBase.h:68
int Layer() const
Get layer of clip on timeline (lower number is covered by higher numbers)
Definition: ClipBase.h:109
bool operator<(ClipBase &a)
Definition: ClipBase.h:82