From 2a90aa09a93c62c4b1eeb059cf13f55c12b1fa53 Mon Sep 17 00:00:00 2001 From: "FeRD (Frank Dana)" Date: Mon, 19 Apr 2021 20:38:03 -0400 Subject: [PATCH] ReaderBase: Make DisplayInfo testable - The function now takes a pointer to the output stream it will write to. The _default_ for that argument is a pointer to std::cout. - Any unit tests which wish to test the functionality can capture the output by passing an alternate buffer: std::stringstream output; reader.DisplayInfo(&output); CHECK(output.str() == "Expected output"); --- src/ReaderBase.cpp | 86 ++++++++++++++++++++++-------------------- src/ReaderBase.h | 8 ++-- tests/FFmpegReader.cpp | 46 ++++++++++++++++++---- tests/ReaderBase.cpp | 2 +- 4 files changed, 88 insertions(+), 54 deletions(-) diff --git a/src/ReaderBase.cpp b/src/ReaderBase.cpp index 127fefbe..63b5cada 100644 --- a/src/ReaderBase.cpp +++ b/src/ReaderBase.cpp @@ -28,8 +28,14 @@ * along with OpenShot Library. If not, see . */ +#include +#include +#include + #include "ReaderBase.h" +#include "Json.h" + using namespace openshot; /// Constructor for the base reader, where many things are initialized. @@ -67,49 +73,49 @@ ReaderBase::ReaderBase() } // Display file information -void ReaderBase::DisplayInfo() { - std::cout << std::fixed << std::setprecision(2) << std::boolalpha; - std::cout << "----------------------------" << std::endl; - std::cout << "----- File Information -----" << std::endl; - std::cout << "----------------------------" << std::endl; - std::cout << "--> Has Video: " << info.has_video << std::endl; - std::cout << "--> Has Audio: " << info.has_audio << std::endl; - std::cout << "--> Has Single Image: " << info.has_single_image << std::endl; - std::cout << "--> Duration: " << info.duration << " Seconds" << std::endl; - std::cout << "--> File Size: " << double(info.file_size) / 1024 / 1024 << " MB" << std::endl; - std::cout << "----------------------------" << std::endl; - std::cout << "----- Video Attributes -----" << std::endl; - std::cout << "----------------------------" << std::endl; - std::cout << "--> Width: " << info.width << std::endl; - std::cout << "--> Height: " << info.height << std::endl; - std::cout << "--> Pixel Format: " << info.pixel_format << std::endl; - std::cout << "--> Frames Per Second: " << info.fps.ToDouble() << " (" << info.fps.num << "/" << info.fps.den << ")" << std::endl; - std::cout << "--> Video Bit Rate: " << info.video_bit_rate/1000 << " kb/s" << std::endl; - std::cout << "--> Pixel Ratio: " << info.pixel_ratio.ToDouble() << " (" << info.pixel_ratio.num << "/" << info.pixel_ratio.den << ")" << std::endl; - std::cout << "--> Display Aspect Ratio: " << info.display_ratio.ToDouble() << " (" << info.display_ratio.num << "/" << info.display_ratio.den << ")" << std::endl; - std::cout << "--> Video Codec: " << info.vcodec << std::endl; - std::cout << "--> Video Length: " << info.video_length << " Frames" << std::endl; - std::cout << "--> Video Stream Index: " << info.video_stream_index << std::endl; - std::cout << "--> Video Timebase: " << info.video_timebase.ToDouble() << " (" << info.video_timebase.num << "/" << info.video_timebase.den << ")" << std::endl; - std::cout << "--> Interlaced: " << info.interlaced_frame << std::endl; - std::cout << "--> Interlaced: Top Field First: " << info.top_field_first << std::endl; - std::cout << "----------------------------" << std::endl; - std::cout << "----- Audio Attributes -----" << std::endl; - std::cout << "----------------------------" << std::endl; - std::cout << "--> Audio Codec: " << info.acodec << std::endl; - std::cout << "--> Audio Bit Rate: " << info.audio_bit_rate/1000 << " kb/s" << std::endl; - std::cout << "--> Sample Rate: " << info.sample_rate << " Hz" << std::endl; - std::cout << "--> # of Channels: " << info.channels << std::endl; - std::cout << "--> Channel Layout: " << info.channel_layout << std::endl; - std::cout << "--> Audio Stream Index: " << info.audio_stream_index << std::endl; - std::cout << "--> Audio Timebase: " << info.audio_timebase.ToDouble() << " (" << info.audio_timebase.num << "/" << info.audio_timebase.den << ")" << std::endl; - std::cout << "----------------------------" << std::endl; - std::cout << "--------- Metadata ---------" << std::endl; - std::cout << "----------------------------" << std::endl; +void ReaderBase::DisplayInfo(std::ostream* out) { + *out << std::fixed << std::setprecision(2) << std::boolalpha; + *out << "----------------------------" << std::endl; + *out << "----- File Information -----" << std::endl; + *out << "----------------------------" << std::endl; + *out << "--> Has Video: " << info.has_video << std::endl; + *out << "--> Has Audio: " << info.has_audio << std::endl; + *out << "--> Has Single Image: " << info.has_single_image << std::endl; + *out << "--> Duration: " << info.duration << " Seconds" << std::endl; + *out << "--> File Size: " << double(info.file_size) / 1024 / 1024 << " MB" << std::endl; + *out << "----------------------------" << std::endl; + *out << "----- Video Attributes -----" << std::endl; + *out << "----------------------------" << std::endl; + *out << "--> Width: " << info.width << std::endl; + *out << "--> Height: " << info.height << std::endl; + *out << "--> Pixel Format: " << info.pixel_format << std::endl; + *out << "--> Frames Per Second: " << info.fps.ToDouble() << " (" << info.fps.num << "/" << info.fps.den << ")" << std::endl; + *out << "--> Video Bit Rate: " << info.video_bit_rate/1000 << " kb/s" << std::endl; + *out << "--> Pixel Ratio: " << info.pixel_ratio.ToDouble() << " (" << info.pixel_ratio.num << "/" << info.pixel_ratio.den << ")" << std::endl; + *out << "--> Display Aspect Ratio: " << info.display_ratio.ToDouble() << " (" << info.display_ratio.num << "/" << info.display_ratio.den << ")" << std::endl; + *out << "--> Video Codec: " << info.vcodec << std::endl; + *out << "--> Video Length: " << info.video_length << " Frames" << std::endl; + *out << "--> Video Stream Index: " << info.video_stream_index << std::endl; + *out << "--> Video Timebase: " << info.video_timebase.ToDouble() << " (" << info.video_timebase.num << "/" << info.video_timebase.den << ")" << std::endl; + *out << "--> Interlaced: " << info.interlaced_frame << std::endl; + *out << "--> Interlaced: Top Field First: " << info.top_field_first << std::endl; + *out << "----------------------------" << std::endl; + *out << "----- Audio Attributes -----" << std::endl; + *out << "----------------------------" << std::endl; + *out << "--> Audio Codec: " << info.acodec << std::endl; + *out << "--> Audio Bit Rate: " << info.audio_bit_rate/1000 << " kb/s" << std::endl; + *out << "--> Sample Rate: " << info.sample_rate << " Hz" << std::endl; + *out << "--> # of Channels: " << info.channels << std::endl; + *out << "--> Channel Layout: " << info.channel_layout << std::endl; + *out << "--> Audio Stream Index: " << info.audio_stream_index << std::endl; + *out << "--> Audio Timebase: " << info.audio_timebase.ToDouble() << " (" << info.audio_timebase.num << "/" << info.audio_timebase.den << ")" << std::endl; + *out << "----------------------------" << std::endl; + *out << "--------- Metadata ---------" << std::endl; + *out << "----------------------------" << std::endl; // Iterate through metadata for (auto it : info.metadata) - std::cout << "--> " << it.first << ": " << it.second << std::endl; + *out << "--> " << it.first << ": " << it.second << std::endl; } // Generate Json::Value for this object diff --git a/src/ReaderBase.h b/src/ReaderBase.h index 7b7847a8..4d2d3afa 100644 --- a/src/ReaderBase.h +++ b/src/ReaderBase.h @@ -31,11 +31,9 @@ #ifndef OPENSHOT_READER_BASE_H #define OPENSHOT_READER_BASE_H -#include -#include #include -#include -#include +#include + #include "CacheMemory.h" #include "ChannelLayouts.h" #include "ClipBase.h" @@ -120,7 +118,7 @@ namespace openshot virtual void Close() = 0; /// Display file information in the standard output stream (stdout) - void DisplayInfo(); + void DisplayInfo(std::ostream* out=&std::cout); /// Get the cache object used by this reader (note: not all readers use cache) virtual openshot::CacheBase* GetCache() = 0; diff --git a/tests/FFmpegReader.cpp b/tests/FFmpegReader.cpp index 217d601c..7911487a 100644 --- a/tests/FFmpegReader.cpp +++ b/tests/FFmpegReader.cpp @@ -39,7 +39,6 @@ #include "Timeline.h" #include "Json.h" -using namespace std; using namespace openshot; TEST_CASE( "Invalid_Path", "[libopenshot][ffmpegreader]" ) @@ -51,7 +50,7 @@ TEST_CASE( "Invalid_Path", "[libopenshot][ffmpegreader]" ) TEST_CASE( "GetFrame_Before_Opening", "[libopenshot][ffmpegreader]" ) { // Create a reader - stringstream path; + std::stringstream path; path << TEST_MEDIA_PATH << "piano.wav"; FFmpegReader r(path.str()); @@ -62,7 +61,7 @@ TEST_CASE( "GetFrame_Before_Opening", "[libopenshot][ffmpegreader]" ) TEST_CASE( "Check_Audio_File", "[libopenshot][ffmpegreader]" ) { // Create a reader - stringstream path; + std::stringstream path; path << TEST_MEDIA_PATH << "piano.wav"; FFmpegReader r(path.str()); r.Open(); @@ -92,7 +91,7 @@ TEST_CASE( "Check_Audio_File", "[libopenshot][ffmpegreader]" ) TEST_CASE( "Check_Video_File", "[libopenshot][ffmpegreader]" ) { // Create a reader - stringstream path; + std::stringstream path; path << TEST_MEDIA_PATH << "test.mp4"; FFmpegReader r(path.str()); r.Open(); @@ -138,7 +137,7 @@ TEST_CASE( "Check_Video_File", "[libopenshot][ffmpegreader]" ) TEST_CASE( "Seek", "[libopenshot][ffmpegreader]" ) { // Create a reader - stringstream path; + std::stringstream path; path << TEST_MEDIA_PATH << "sintel_trailer-720p.mp4"; FFmpegReader r(path.str()); r.Open(); @@ -195,7 +194,7 @@ TEST_CASE( "Seek", "[libopenshot][ffmpegreader]" ) TEST_CASE( "Frame_Rate", "[libopenshot][ffmpegreader]" ) { // Create a reader - stringstream path; + std::stringstream path; path << TEST_MEDIA_PATH << "sintel_trailer-720p.mp4"; FFmpegReader r(path.str()); r.Open(); @@ -211,7 +210,7 @@ TEST_CASE( "Frame_Rate", "[libopenshot][ffmpegreader]" ) TEST_CASE( "Multiple_Open_and_Close", "[libopenshot][ffmpegreader]" ) { // Create a reader - stringstream path; + std::stringstream path; path << TEST_MEDIA_PATH << "sintel_trailer-720p.mp4"; FFmpegReader r(path.str()); r.Open(); @@ -247,7 +246,7 @@ TEST_CASE( "Multiple_Open_and_Close", "[libopenshot][ffmpegreader]" ) TEST_CASE( "verify parent Timeline", "[libopenshot][ffmpegreader]" ) { // Create a reader - stringstream path; + std::stringstream path; path << TEST_MEDIA_PATH << "sintel_trailer-720p.mp4"; FFmpegReader r(path.str()); r.Open(); @@ -273,3 +272,34 @@ TEST_CASE( "verify parent Timeline", "[libopenshot][ffmpegreader]" ) CHECK(r.GetFrame(1)->GetImage()->width() == 640); CHECK(r.GetFrame(1)->GetImage()->height() == 360); } + +TEST_CASE( "DisplayInfo", "[libopenshot][clip]" ) +{ + // Create a reader + std::stringstream path; + path << TEST_MEDIA_PATH << "sintel_trailer-720p.mp4"; + FFmpegReader r(path.str()); + r.Open(); + + std::string expected(R"(---------------------------- +----- File Information ----- +---------------------------- +--> Has Video: true +--> Has Audio: true +--> Has Single Image: false +--> Duration: 51.95 Seconds +--> File Size: 7.26 MB +---------------------------- +----- Video Attributes ----- +---------------------------- +--> Width: 1280 +--> Height: 720)"); + + // Store the DisplayInfo() text in 'output' + std::stringstream output; + r.DisplayInfo(&output); + + // Compare a [0, expected.size()) substring of output to expected + auto compare_value = output.str().compare(0, expected.size(), expected); + CHECK(compare_value == 0); +} diff --git a/tests/ReaderBase.cpp b/tests/ReaderBase.cpp index 94880e00..d071d36d 100644 --- a/tests/ReaderBase.cpp +++ b/tests/ReaderBase.cpp @@ -42,7 +42,7 @@ using namespace openshot; // Since it is not possible to instantiate an abstract class, this test creates // a new derived class, in order to test the base class file info struct. -TEST_CASE( "ReaderBase_Derived_Class", "[libopenshot][readerbase]" ) +TEST_CASE( "derived class", "[libopenshot][readerbase]" ) { // Create a new derived class from type ReaderBase class TestReader : public ReaderBase