OpenShot Library | libopenshot  0.2.3
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  * @section LICENSE
7  *
8  * Copyright (c) 2008-2014 OpenShot Studios, LLC
9  * <http://www.openshotstudios.com/>. This file is part of
10  * OpenShot Library (libopenshot), an open-source project dedicated to
11  * delivering high quality video editing and animation solutions to the
12  * world. For more information visit <http://www.openshot.org/>.
13  *
14  * OpenShot Library (libopenshot) is free software: you can redistribute it
15  * and/or modify it under the terms of the GNU Lesser General Public License
16  * as published by the Free Software Foundation, either version 3 of the
17  * License, or (at your option) any later version.
18  *
19  * OpenShot Library (libopenshot) is distributed in the hope that it will be
20  * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22  * GNU Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public License
25  * along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
26  */
27 
28 #ifndef OPENSHOT_CHUNK_READER_H
29 #define OPENSHOT_CHUNK_READER_H
30 
31 #include "ReaderBase.h"
32 #include <cmath>
33 #include <ctime>
34 #include <iostream>
35 #include <fstream>
36 #include <omp.h>
37 #include <QtCore/qdir.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <memory>
41 #include "Json.h"
42 #include "CacheMemory.h"
43 #include "Exceptions.h"
44 
45 using namespace std;
46 
47 namespace openshot
48 {
49 
50  /**
51  * @brief This struct holds the location of a frame within a chunk.
52  *
53  * Chunks are small video files, which typically contain a few seconds of video each.
54  * Locating a frame among these small video files is accomplished by using
55  * this struct.
56  */
58  {
59  int64_t number; ///< The chunk number
60  int64_t frame; ///< The frame number
61  };
62 
63  /**
64  * @brief This enumeration allows the user to choose which version
65  * of the chunk they would like (low, medium, or high quality).
66  *
67  * Since chunks contain multiple video streams, this version enumeration
68  * allows the user to choose which version of the chunk they would like.
69  * For example, if you want a small version with reduced quality, you can
70  * choose the THUMBNAIL version. This is used on the ChunkReader
71  * constructor.
72  */
74  {
75  THUMBNAIL, ///< The lowest quality stream contained in this chunk file
76  PREVIEW, ///< The medium quality stream contained in this chunk file
77  FINAL ///< The highest quality stream contained in this chunk file
78  };
79 
80  /**
81  * @brief This class reads a special chunk-formatted file, which can be easily
82  * shared in a distributed environment.
83  *
84  * It stores the video in small "chunks", which are really just short video clips,
85  * a few seconds each. A ChunkReader only needs the part of the chunk that contains
86  * the frames it is looking for. For example, if you only need the end of a video,
87  * only the last few chunks might be needed to successfully access those openshot::Frame objects.
88  *
89  * \code
90  * // This example demonstrates how to read a chunk folder and access frame objects inside it.
91  * ChunkReader r("/home/jonathan/apps/chunks/chunk1/", FINAL); // Load highest quality version of this chunk file
92  * r.DisplayInfo(); // Display all known details about this chunk file
93  * r.Open(); // Open the reader
94  *
95  * // Access frame 1
96  * r.GetFrame(1)->Display();
97  *
98  * // Close the reader
99  * r.Close();
100  * \endcode
101  */
102  class ChunkReader : public ReaderBase
103  {
104  private:
105  string path;
106  bool is_open;
107  int64_t chunk_size;
108  ReaderBase *local_reader;
109  ChunkLocation previous_location;
110  ChunkVersion version;
111  std::shared_ptr<Frame> last_frame;
112 
113  /// Check if folder path existing
114  bool does_folder_exist(string path);
115 
116  /// Find the location of a frame in a chunk
117  ChunkLocation find_chunk_frame(int64_t requested_frame);
118 
119  /// get a formatted path of a specific chunk
120  string get_chunk_path(int64_t chunk_number, string folder, string extension);
121 
122  /// Load JSON meta data about this chunk folder
123  void load_json();
124 
125  public:
126 
127  /// @brief Constructor for ChunkReader. This automatically opens the chunk file or folder and loads
128  /// frame 1, or it throws one of the following exceptions.
129  /// @param path The folder path / location of a chunk (chunks are stored as folders)
130  /// @param chunk_version Choose the video version / quality (THUMBNAIL, PREVIEW, or FINAL)
131  ChunkReader(string path, ChunkVersion chunk_version);
132 
133  /// Close the reader
134  void Close();
135 
136  /// @brief Get the chunk size (number of frames to write in each chunk)
137  /// @returns The number of frames in this chunk
138  int64_t GetChunkSize() { return chunk_size; };
139 
140  /// @brief Set the chunk size (number of frames to write in each chunk)
141  /// @param new_size The number of frames per chunk
142  void SetChunkSize(int64_t new_size) { chunk_size = new_size; };
143 
144  /// Get the cache object used by this reader (always return NULL for this reader)
145  CacheMemory* GetCache() { return NULL; };
146 
147  /// @brief Get an openshot::Frame object for a specific frame number of this reader.
148  /// @returns The requested frame (containing the image and audio)
149  /// @param requested_frame The frame number you want to retrieve
150  std::shared_ptr<Frame> GetFrame(int64_t requested_frame);
151 
152  /// Determine if reader is open or closed
153  bool IsOpen() { return is_open; };
154 
155  /// Return the type name of the class
156  string Name() { return "ChunkReader"; };
157 
158  /// Get and Set JSON methods
159  string Json(); ///< Generate JSON string of this object
160  void SetJson(string value); ///< Load JSON string into this object
161  Json::Value JsonValue(); ///< Generate Json::JsonValue for this object
162  void SetJsonValue(Json::Value root); ///< Load Json::JsonValue into this object
163 
164  /// Open the reader. This is required before you can access frames or data from the reader.
165  void Open();
166  };
167 
168 }
169 
170 #endif
This class reads a special chunk-formatted file, which can be easily shared in a distributed environm...
Definition: ChunkReader.h:102
Header file for ReaderBase class.
The lowest quality stream contained in this chunk file.
Definition: ChunkReader.h:75
This abstract class is the base class, used by all readers in libopenshot.
Definition: ReaderBase.h:97
Header file for CacheMemory class.
bool IsOpen()
Determine if reader is open or closed.
Definition: ChunkReader.h:153
Header file for all Exception classes.
The highest quality stream contained in this chunk file.
Definition: ChunkReader.h:77
The medium quality stream contained in this chunk file.
Definition: ChunkReader.h:76
Header file for JSON class.
void SetChunkSize(int64_t new_size)
Set the chunk size (number of frames to write in each chunk)
Definition: ChunkReader.h:142
CacheMemory * GetCache()
Get the cache object used by this reader (always return NULL for this reader)
Definition: ChunkReader.h:145
int64_t number
The chunk number.
Definition: ChunkReader.h:59
ChunkVersion
This enumeration allows the user to choose which version of the chunk they would like (low...
Definition: ChunkReader.h:73
int64_t GetChunkSize()
Get the chunk size (number of frames to write in each chunk)
Definition: ChunkReader.h:138
string Name()
Return the type name of the class.
Definition: ChunkReader.h:156
int64_t frame
The frame number.
Definition: ChunkReader.h:60
This struct holds the location of a frame within a chunk.
Definition: ChunkReader.h:57
This class is a memory-based cache manager for Frame objects.
Definition: CacheMemory.h:48