OpenShot Library | libopenshot  0.2.5
ChunkReader.h
Go to the documentation of this file.
1 /**
2  * @file
3  * @brief Header file for ChunkReader 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_CHUNK_READER_H
32 #define OPENSHOT_CHUNK_READER_H
33 
34 #include "ReaderBase.h"
35 #include <cmath>
36 #include <ctime>
37 #include <iostream>
38 #include <fstream>
39 #include <omp.h>
40 #include <QtCore/qdir.h>
41 #include <stdio.h>
42 #include <cstdlib>
43 #include <memory>
44 #include "Json.h"
45 #include "CacheMemory.h"
46 #include "Exceptions.h"
47 
48 namespace openshot
49 {
50 
51  /**
52  * @brief This struct holds the location of a frame within a chunk.
53  *
54  * Chunks are small video files, which typically contain a few seconds of video each.
55  * Locating a frame among these small video files is accomplished by using
56  * this struct.
57  */
59  {
60  int64_t number; ///< The chunk number
61  int64_t frame; ///< The frame number
62  };
63 
64  /**
65  * @brief This enumeration allows the user to choose which version
66  * of the chunk they would like (low, medium, or high quality).
67  *
68  * Since chunks contain multiple video streams, this version enumeration
69  * allows the user to choose which version of the chunk they would like.
70  * For example, if you want a small version with reduced quality, you can
71  * choose the THUMBNAIL version. This is used on the ChunkReader
72  * constructor.
73  */
75  {
76  THUMBNAIL, ///< The lowest quality stream contained in this chunk file
77  PREVIEW, ///< The medium quality stream contained in this chunk file
78  FINAL ///< The highest quality stream contained in this chunk file
79  };
80 
81  /**
82  * @brief This class reads a special chunk-formatted file, which can be easily
83  * shared in a distributed environment.
84  *
85  * It stores the video in small "chunks", which are really just short video clips,
86  * a few seconds each. A ChunkReader only needs the part of the chunk that contains
87  * the frames it is looking for. For example, if you only need the end of a video,
88  * only the last few chunks might be needed to successfully access those openshot::Frame objects.
89  *
90  * \code
91  * // This example demonstrates how to read a chunk folder and access frame objects inside it.
92  * ChunkReader r("/home/jonathan/apps/chunks/chunk1/", FINAL); // Load highest quality version of this chunk file
93  * r.DisplayInfo(); // Display all known details about this chunk file
94  * r.Open(); // Open the reader
95  *
96  * // Access frame 1
97  * r.GetFrame(1)->Display();
98  *
99  * // Close the reader
100  * r.Close();
101  * \endcode
102  */
103  class ChunkReader : public ReaderBase
104  {
105  private:
106  std::string path;
107  bool is_open;
108  int64_t chunk_size;
109  openshot::ReaderBase *local_reader;
110  ChunkLocation previous_location;
111  ChunkVersion version;
112  std::shared_ptr<openshot::Frame> last_frame;
113 
114  /// Check if folder path existing
115  bool does_folder_exist(std::string path);
116 
117  /// Find the location of a frame in a chunk
118  ChunkLocation find_chunk_frame(int64_t requested_frame);
119 
120  /// get a formatted path of a specific chunk
121  std::string get_chunk_path(int64_t chunk_number, std::string folder, std::string extension);
122 
123  /// Load JSON meta data about this chunk folder
124  void load_json();
125 
126  public:
127 
128  /// @brief Constructor for ChunkReader. This automatically opens the chunk file or folder and loads
129  /// frame 1, or it throws one of the following exceptions.
130  /// @param path The folder path / location of a chunk (chunks are stored as folders)
131  /// @param chunk_version Choose the video version / quality (THUMBNAIL, PREVIEW, or FINAL)
132  ChunkReader(std::string path, ChunkVersion chunk_version);
133 
134  /// Close the reader
135  void Close();
136 
137  /// @brief Get the chunk size (number of frames to write in each chunk)
138  /// @returns The number of frames in this chunk
139  int64_t GetChunkSize() { return chunk_size; };
140 
141  /// @brief Set the chunk size (number of frames to write in each chunk)
142  /// @param new_size The number of frames per chunk
143  void SetChunkSize(int64_t new_size) { chunk_size = new_size; };
144 
145  /// Get the cache object used by this reader (always return NULL for this reader)
146  openshot::CacheMemory* GetCache() { return NULL; };
147 
148  /// @brief Get an openshot::Frame object for a specific frame number of this reader.
149  /// @returns The requested frame (containing the image and audio)
150  /// @param requested_frame The frame number you want to retrieve
151  std::shared_ptr<openshot::Frame> GetFrame(int64_t requested_frame);
152 
153  /// Determine if reader is open or closed
154  bool IsOpen() { return is_open; };
155 
156  /// Return the type name of the class
157  std::string Name() { return "ChunkReader"; };
158 
159  /// Get and Set JSON methods
160  std::string Json() const override; ///< Generate JSON string of this object
161  void SetJson(const std::string value); ///< Load JSON string into this object
162  Json::Value JsonValue() const override; ///< Generate Json::Value for this object
163  void SetJsonValue(const Json::Value root); ///< Load Json::Value into this object
164 
165  /// Open the reader. This is required before you can access frames or data from the reader.
166  void Open();
167  };
168 
169 }
170 
171 #endif
Header file for CacheMemory class.
Header file for all Exception classes.
Header file for JSON class.
Header file for ReaderBase class.
This class is a memory-based cache manager for Frame objects.
Definition: CacheMemory.h:51
This class reads a special chunk-formatted file, which can be easily shared in a distributed environm...
Definition: ChunkReader.h:104
std::string Json() const override
Get and Set JSON methods.
std::string Name()
Return the type name of the class.
Definition: ChunkReader.h:157
void Open()
Open the reader. This is required before you can access frames or data from the reader.
void Close()
Close the reader.
openshot::CacheMemory * GetCache()
Get the cache object used by this reader (always return NULL for this reader)
Definition: ChunkReader.h:146
void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
bool IsOpen()
Determine if reader is open or closed.
Definition: ChunkReader.h:154
std::shared_ptr< openshot::Frame > GetFrame(int64_t requested_frame)
Get an openshot::Frame object for a specific frame number of this reader.
Json::Value JsonValue() const override
Generate Json::Value for this object.
ChunkReader(std::string path, ChunkVersion chunk_version)
Constructor for ChunkReader. This automatically opens the chunk file or folder and loads frame 1,...
Definition: ChunkReader.cpp:36
void SetJson(const std::string value)
Load JSON string into this object.
int64_t GetChunkSize()
Get the chunk size (number of frames to write in each chunk)
Definition: ChunkReader.h:139
void SetChunkSize(int64_t new_size)
Set the chunk size (number of frames to write in each chunk)
Definition: ChunkReader.h:143
This abstract class is the base class, used by all readers in libopenshot.
Definition: ReaderBase.h:98
This namespace is the default namespace for all code in the openshot library.
ChunkVersion
This enumeration allows the user to choose which version of the chunk they would like (low,...
Definition: ChunkReader.h:75
@ THUMBNAIL
The lowest quality stream contained in this chunk file.
Definition: ChunkReader.h:76
@ FINAL
The highest quality stream contained in this chunk file.
Definition: ChunkReader.h:78
@ PREVIEW
The medium quality stream contained in this chunk file.
Definition: ChunkReader.h:77
This struct holds the location of a frame within a chunk.
Definition: ChunkReader.h:59
int64_t number
The chunk number.
Definition: ChunkReader.h:60
int64_t frame
The frame number.
Definition: ChunkReader.h:61