From e3d41808b997c8a30a7385997bd68ea4151cb974 Mon Sep 17 00:00:00 2001 From: Jonathan Thomas Date: Sat, 7 Dec 2013 16:52:09 -0600 Subject: [PATCH] Added Json methods to readers and readerBase classes. --- include/FFmpegReader.h | 4 ++ include/ImageReader.h | 4 ++ include/ReaderBase.h | 7 ++- include/TextReader.h | 4 ++ include/WriterBase.h | 7 ++- src/FFmpegReader.cpp | 24 ++++++++++ src/ImageReader.cpp | 25 +++++++++++ src/ReaderBase.cpp | 97 +++++++++++++++++++++++++++++++++++++++-- src/TextReader.cpp | 51 ++++++++++++++++++++++ src/WriterBase.cpp | 99 ++++++++++++++++++++++++++++++++++++++++-- 10 files changed, 312 insertions(+), 10 deletions(-) diff --git a/include/FFmpegReader.h b/include/FFmpegReader.h index 0065f4f1..77506b80 100644 --- a/include/FFmpegReader.h +++ b/include/FFmpegReader.h @@ -238,6 +238,10 @@ namespace openshot /// @param requested_frame The frame number that is requested. tr1::shared_ptr GetFrame(int requested_frame) throw(ReaderClosed, TooManySeeks); + /// Get and Set JSON methods + Json::Value JsonValue(); ///< Generate Json::JsonValue for this object + void Json(Json::Value root) throw(InvalidFile); ///< Load Json::JsonValue into this object + /// Open File - which is called by the constructor automatically void Open() throw(InvalidFile, NoStreamsFound, InvalidCodec); }; diff --git a/include/ImageReader.h b/include/ImageReader.h index ab001490..0e59d256 100644 --- a/include/ImageReader.h +++ b/include/ImageReader.h @@ -87,6 +87,10 @@ namespace openshot /// @param requested_frame The frame number that is requested. tr1::shared_ptr GetFrame(int requested_frame) throw(ReaderClosed); + /// Get and Set JSON methods + Json::Value JsonValue(); ///< Generate Json::JsonValue for this object + void Json(Json::Value root) throw(InvalidFile); ///< Load Json::JsonValue into this object + /// Open File - which is called by the constructor automatically void Open() throw(InvalidFile); }; diff --git a/include/ReaderBase.h b/include/ReaderBase.h index fdf6884d..7e52eab6 100644 --- a/include/ReaderBase.h +++ b/include/ReaderBase.h @@ -104,8 +104,11 @@ namespace openshot /// this method, or the ReaderInfo struct values will not be initialized. void InitFileInfo(); - /// Generate JSON string of reader info - string Json(); + /// Get and Set JSON methods + string Json(); ///< Generate JSON string of this object + Json::Value JsonValue(); ///< Generate Json::JsonValue for this object + void Json(string value) throw(InvalidJSON); ///< Load JSON string into this object + void Json(Json::Value root); ///< Load Json::JsonValue into this object /// Open the reader (and start consuming resources, such as images or video files) virtual void Open() = 0; diff --git a/include/TextReader.h b/include/TextReader.h index 8e5aa4cd..25fcb4b1 100644 --- a/include/TextReader.h +++ b/include/TextReader.h @@ -120,6 +120,10 @@ namespace openshot /// @param requested_frame The frame number that is requested. tr1::shared_ptr GetFrame(int requested_frame) throw(ReaderClosed); + /// Get and Set JSON methods + Json::Value JsonValue(); ///< Generate Json::JsonValue for this object + void Json(Json::Value root) throw(InvalidFile); ///< Load Json::JsonValue into this object + /// Open Reader - which is called by the constructor automatically void Open(); }; diff --git a/include/WriterBase.h b/include/WriterBase.h index c304298e..dc93b937 100644 --- a/include/WriterBase.h +++ b/include/WriterBase.h @@ -97,8 +97,11 @@ namespace openshot /// this method, or the WriterInfo struct values will not be initialized. void InitFileInfo(); - /// Generate JSON string of writer info - string Json(); + /// Get and Set JSON methods + string Json(); ///< Generate JSON string of this object + Json::Value JsonValue(); ///< Generate Json::JsonValue for this object + void Json(string value) throw(InvalidJSON); ///< Load JSON string into this object + void Json(Json::Value root); ///< Load Json::JsonValue into this object /// Display file information in the standard output stream (stdout) void DisplayInfo(); diff --git a/src/FFmpegReader.cpp b/src/FFmpegReader.cpp index 7504907f..d38a16e9 100644 --- a/src/FFmpegReader.cpp +++ b/src/FFmpegReader.cpp @@ -1554,4 +1554,28 @@ int FFmpegReader::GetSmallestAudioFrame() return smallest_frame; } +// Generate Json::JsonValue for this object +Json::Value FFmpegReader::JsonValue() { + // Create root json object + Json::Value root = ReaderBase::JsonValue(); // get parent properties + root["path"] = path; + + // return JsonValue + return root; +} + +// Load Json::JsonValue into this object +void FFmpegReader::Json(Json::Value root) throw(InvalidFile) { + + // Set parent data + ReaderBase::Json(root); + + // Set data from Json (if key is found) + if (root["path"] != Json::nullValue) + path = root["path"].asString(); + + // Open path, and re-init everything + Close(); + Open(); +} diff --git a/src/ImageReader.cpp b/src/ImageReader.cpp index b1800f83..d773ade8 100644 --- a/src/ImageReader.cpp +++ b/src/ImageReader.cpp @@ -123,3 +123,28 @@ tr1::shared_ptr ImageReader::GetFrame(int requested_frame) throw(ReaderCl } +// Generate Json::JsonValue for this object +Json::Value ImageReader::JsonValue() { + + // Create root json object + Json::Value root = ReaderBase::JsonValue(); // get parent properties + root["path"] = path; + + // return JsonValue + return root; +} + +// Load Json::JsonValue into this object +void ImageReader::Json(Json::Value root) throw(InvalidFile) { + + // Set parent data + ReaderBase::Json(root); + + // Set data from Json (if key is found) + if (root["path"] != Json::nullValue) + path = root["path"].asString(); + + // Open path, and re-init everything + Close(); + Open(); +} diff --git a/src/ReaderBase.cpp b/src/ReaderBase.cpp index 3bd6c851..5a257a7e 100644 --- a/src/ReaderBase.cpp +++ b/src/ReaderBase.cpp @@ -95,9 +95,16 @@ void ReaderBase::DisplayInfo() { cout << "----------------------------" << endl; } -// Generate JSON string of reader info +// Generate JSON string of this object string ReaderBase::Json() { + // Return formatted string + return JsonValue().toStyledString(); +} + +// Generate Json::JsonValue for this object +Json::Value ReaderBase::JsonValue() { + // Create root json object Json::Value root; root["has_video"] = info.has_video; @@ -138,8 +145,92 @@ string ReaderBase::Json() { root["audio_timebase"]["num"] = info.audio_timebase.num; root["audio_timebase"]["den"] = info.audio_timebase.den; - // return formatted json string - return root.toStyledString(); + // return JsonValue + return root; } +// Load JSON string into this object +void ReaderBase::Json(string value) throw(InvalidJSON) { + // Parse JSON string into JSON objects + Json::Value root; + Json::Reader reader; + bool success = reader.parse( value, root ); + if (!success) + // Raise exception + throw InvalidJSON("JSON could not be parsed (or is invalid)", ""); + + try + { + // Set all values that match + Json(root); + } + catch (exception e) + { + // Error parsing JSON (or missing keys) + throw InvalidJSON("JSON is invalid (missing keys or invalid data types)", ""); + } +} + +// Load Json::JsonValue into this object +void ReaderBase::Json(Json::Value root) { + + // Set data from Json (if key is found) + if (root["has_video"] != Json::nullValue) + info.has_video = root["has_video"].asBool(); + if (root["has_audio"] != Json::nullValue) + info.has_audio = root["has_audio"].asBool(); + if (root["duration"] != Json::nullValue) + info.duration = root["duration"].asDouble(); + if (root["file_size"] != Json::nullValue) + info.file_size = (long long) root["file_size"].asUInt(); + if (root["height"] != Json::nullValue) + info.height = root["height"].asInt(); + if (root["width"] != Json::nullValue) + info.width = root["width"].asInt(); + if (root["pixel_format"] != Json::nullValue) + info.pixel_format = root["pixel_format"].asInt(); + if (root["fps"] != Json::nullValue) { + info.fps.num = root["fps"]["num"].asInt(); + info.fps.den = root["fps"]["den"].asInt(); + } + if (root["video_bit_rate"] != Json::nullValue) + info.video_bit_rate = root["video_bit_rate"].asInt(); + if (root["pixel_ratio"] != Json::nullValue) { + info.pixel_ratio.num = root["pixel_ratio"]["num"].asInt(); + info.pixel_ratio.den = root["pixel_ratio"]["den"].asInt(); + } + if (root["display_ratio"] != Json::nullValue) { + info.display_ratio.num = root["display_ratio"]["num"].asInt(); + info.display_ratio.den = root["display_ratio"]["den"].asInt(); + } + if (root["vcodec"] != Json::nullValue) + info.vcodec = root["vcodec"].asString(); + if (root["video_length"] != Json::nullValue) + info.video_length = (long int) root["video_length"].asUInt(); + if (root["video_stream_index"] != Json::nullValue) + info.video_stream_index = root["video_stream_index"].asInt(); + if (root["video_timebase"] != Json::nullValue) { + info.video_timebase.num = root["video_timebase"]["num"].asInt(); + info.video_timebase.den = root["video_timebase"]["den"].asInt(); + } + if (root["interlaced_frame"] != Json::nullValue) + info.interlaced_frame = root["interlaced_frame"].asBool(); + if (root["top_field_first"] != Json::nullValue) + info.top_field_first = root["top_field_first"].asBool(); + if (root["acodec"] != Json::nullValue) + info.acodec = root["acodec"].asString(); + + if (root["audio_bit_rate"] != Json::nullValue) + info.audio_bit_rate = root["audio_bit_rate"].asInt(); + if (root["sample_rate"] != Json::nullValue) + info.sample_rate = root["sample_rate"].asInt(); + if (root["channels"] != Json::nullValue) + info.channels = root["channels"].asInt(); + if (root["audio_stream_index"] != Json::nullValue) + info.audio_stream_index = root["audio_stream_index"].asInt(); + if (root["audio_timebase"] != Json::nullValue) { + info.audio_timebase.num = root["audio_timebase"]["num"].asInt(); + info.audio_timebase.den = root["audio_timebase"]["den"].asInt(); + } +} diff --git a/src/TextReader.cpp b/src/TextReader.cpp index 94b47c9a..4e0146eb 100644 --- a/src/TextReader.cpp +++ b/src/TextReader.cpp @@ -157,4 +157,55 @@ tr1::shared_ptr TextReader::GetFrame(int requested_frame) throw(ReaderClo } +// Generate Json::JsonValue for this object +Json::Value TextReader::JsonValue() { + // Create root json object + Json::Value root = ReaderBase::JsonValue(); // get parent properties + root["width"] = width; + root["height"] = height; + root["x_offset"] = x_offset; + root["y_offset"] = y_offset; + root["text"] = text; + root["font"] = font; + root["size"] = size; + root["text_color"] = text_color; + root["background_color"] = background_color; + root["gravity"] = gravity; + + // return JsonValue + return root; +} + +// Load Json::JsonValue into this object +void TextReader::Json(Json::Value root) throw(InvalidFile) { + + // Set parent data + ReaderBase::Json(root); + + // Set data from Json (if key is found) + if (root["width"] != Json::nullValue) + width = root["width"].asInt(); + if (root["height"] != Json::nullValue) + height = root["height"].asInt(); + if (root["x_offset"] != Json::nullValue) + x_offset = root["x_offset"].asInt(); + if (root["y_offset"] != Json::nullValue) + y_offset = root["y_offset"].asInt(); + if (root["text"] != Json::nullValue) + text = root["text"].asString(); + if (root["font"] != Json::nullValue) + font = root["font"].asString(); + if (root["size"] != Json::nullValue) + size = root["size"].asDouble(); + if (root["text_color"] != Json::nullValue) + text_color = root["text_color"].asString(); + if (root["background_color"] != Json::nullValue) + background_color = root["background_color"].asString(); + if (root["gravity"] != Json::nullValue) + gravity = (GravityType) root["gravity"].asInt(); + + // Open path, and re-init everything + Close(); + Open(); +} diff --git a/src/WriterBase.cpp b/src/WriterBase.cpp index 46326839..de38e3f6 100644 --- a/src/WriterBase.cpp +++ b/src/WriterBase.cpp @@ -128,9 +128,16 @@ void WriterBase::DisplayInfo() { cout << "----------------------------" << endl; } -// Generate JSON string of writer info +// Generate JSON string of this object string WriterBase::Json() { + // Return formatted string + return JsonValue().toStyledString(); +} + +// Generate Json::JsonValue for this object +Json::Value WriterBase::JsonValue() { + // Create root json object Json::Value root; root["has_video"] = info.has_video; @@ -171,6 +178,92 @@ string WriterBase::Json() { root["audio_timebase"]["num"] = info.audio_timebase.num; root["audio_timebase"]["den"] = info.audio_timebase.den; - // return formatted json string - return root.toStyledString(); + // return JsonValue + return root; +} + +// Load JSON string into this object +void WriterBase::Json(string value) throw(InvalidJSON) { + + // Parse JSON string into JSON objects + Json::Value root; + Json::Reader reader; + bool success = reader.parse( value, root ); + if (!success) + // Raise exception + throw InvalidJSON("JSON could not be parsed (or is invalid)", ""); + + try + { + // Set all values that match + Json(root); + } + catch (exception e) + { + // Error parsing JSON (or missing keys) + throw InvalidJSON("JSON is invalid (missing keys or invalid data types)", ""); + } +} + +// Load Json::JsonValue into this object +void WriterBase::Json(Json::Value root) { + + // Set data from Json (if key is found) + if (root["has_video"] != Json::nullValue) + info.has_video = root["has_video"].asBool(); + if (root["has_audio"] != Json::nullValue) + info.has_audio = root["has_audio"].asBool(); + if (root["duration"] != Json::nullValue) + info.duration = root["duration"].asDouble(); + if (root["file_size"] != Json::nullValue) + info.file_size = (long long) root["file_size"].asUInt(); + if (root["height"] != Json::nullValue) + info.height = root["height"].asInt(); + if (root["width"] != Json::nullValue) + info.width = root["width"].asInt(); + if (root["pixel_format"] != Json::nullValue) + info.pixel_format = root["pixel_format"].asInt(); + if (root["fps"] != Json::nullValue) { + info.fps.num = root["fps"]["num"].asInt(); + info.fps.den = root["fps"]["den"].asInt(); + } + if (root["video_bit_rate"] != Json::nullValue) + info.video_bit_rate = root["video_bit_rate"].asInt(); + if (root["pixel_ratio"] != Json::nullValue) { + info.pixel_ratio.num = root["pixel_ratio"]["num"].asInt(); + info.pixel_ratio.den = root["pixel_ratio"]["den"].asInt(); + } + if (root["display_ratio"] != Json::nullValue) { + info.display_ratio.num = root["display_ratio"]["num"].asInt(); + info.display_ratio.den = root["display_ratio"]["den"].asInt(); + } + if (root["vcodec"] != Json::nullValue) + info.vcodec = root["vcodec"].asString(); + if (root["video_length"] != Json::nullValue) + info.video_length = (long int) root["video_length"].asUInt(); + if (root["video_stream_index"] != Json::nullValue) + info.video_stream_index = root["video_stream_index"].asInt(); + if (root["video_timebase"] != Json::nullValue) { + info.video_timebase.num = root["video_timebase"]["num"].asInt(); + info.video_timebase.den = root["video_timebase"]["den"].asInt(); + } + if (root["interlaced_frame"] != Json::nullValue) + info.interlaced_frame = root["interlaced_frame"].asBool(); + if (root["top_field_first"] != Json::nullValue) + info.top_field_first = root["top_field_first"].asBool(); + if (root["acodec"] != Json::nullValue) + info.acodec = root["acodec"].asString(); + + if (root["audio_bit_rate"] != Json::nullValue) + info.audio_bit_rate = root["audio_bit_rate"].asInt(); + if (root["sample_rate"] != Json::nullValue) + info.sample_rate = root["sample_rate"].asInt(); + if (root["channels"] != Json::nullValue) + info.channels = root["channels"].asInt(); + if (root["audio_stream_index"] != Json::nullValue) + info.audio_stream_index = root["audio_stream_index"].asInt(); + if (root["audio_timebase"] != Json::nullValue) { + info.audio_timebase.num = root["audio_timebase"]["num"].asInt(); + info.audio_timebase.den = root["audio_timebase"]["den"].asInt(); + } }