diff --git a/CMakeLists.txt b/CMakeLists.txt index 0561235..9f2ec2a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -35,12 +35,18 @@ IF(UNIX) spdlog ) + add_executable(${PROJECT_N_T} ${srcs} ${tests} ) target_link_libraries(${PROJECT_N_T} PRIVATE - spdlog + sfml-system + sfml-window + sfml-graphics + sfml-network + sfml-audio + GTest::gtest_main ) @@ -55,13 +61,13 @@ IF(UNIX) COMMAND ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_N_T} DEPENDS ${PROJECT_N_T} WORKING_DIRECTORY ${PROJECT_CURRENT_BINARY_DIR} - COMMENT "Starting ${PROJECT_N}" + COMMENT "Starting ${PROJECT_N_T}" ) ELSE(WIN32) ENDIF() include(GoogleTest) -gtest_discover_tests(${PROJECT_N}) +gtest_discover_tests(${PROJECT_N_T}) diff --git a/include/UtilTool.h b/include/UtilTool.h index 272329d..0265c0c 100644 --- a/include/UtilTool.h +++ b/include/UtilTool.h @@ -1,9 +1,27 @@ +#ifndef UTILTOOL_H +#define UTILTOOL_H + #include #include +enum class MediaType{ + VIDEO, + AUDIO, + IMAGE, + UNSUPPORT, +}; + class UtilTool { -public: - constexpr static std::array ImageType{"jpg","png"}; +private: + constexpr static std::array ImageTypes{"jpg","png"}; + constexpr static std::array VideoTypes{"mp4"}; + constexpr static std::array AudioTypes{"mp3"}; static bool CheckFileIsImage(const std::string& filename); + static bool CheckFileIsAudio(const std::string& filename); + static bool CheckFileIsVideo(const std::string& filename); +public: + static MediaType CheckFileType(const std::string& filename); }; + +#endif // !UTILTOOL_H \ No newline at end of file diff --git a/include/imageService.h b/include/imageService.h new file mode 100644 index 0000000..b82de82 --- /dev/null +++ b/include/imageService.h @@ -0,0 +1,18 @@ +#include +#include + +class ImageService { +private: + sf::Texture texture; +public: + ImageService(const std::string& name){ + texture.loadFromFile(name); + } + sf::Texture& GetTexture(){ + return texture; + } + float GetScale(int width, int height){ + auto imgSize = texture.getSize(); + return std::min(static_cast(width) / imgSize.x, static_cast(height) / imgSize.y); + } +}; diff --git a/include/mediaService.h b/include/mediaService.h new file mode 100644 index 0000000..04bb367 --- /dev/null +++ b/include/mediaService.h @@ -0,0 +1,25 @@ +#include +#include +#include +#include + +class MediaService +{ +private: + ImageService *imageService = nullptr; + MediaType type; + sf::Texture *texture = nullptr; + sf::Sprite *sprite = nullptr; + int client_width = 0; + int client_height = 0; +public: + MediaService(const std::string &filename, int width, int height); + ~MediaService(){ + delete texture; + delete sprite; + } + sf::Sprite GetSprite() + { + return *sprite; + } +}; \ No newline at end of file diff --git a/include/stream.h b/include/stream.h new file mode 100644 index 0000000..ad5ffdf --- /dev/null +++ b/include/stream.h @@ -0,0 +1,12 @@ +#include +extern "C" { + #include + #include + #include +} + +class Stream { +private: + AVFormatContext* m_avFormatContext; + +}; diff --git a/main.cc b/main.cc index 2a6f939..ffc80a9 100644 --- a/main.cc +++ b/main.cc @@ -4,29 +4,20 @@ #include #include #include "UtilTool.h" +#include "mediaService.h" constexpr int CLIENT_WIDTH = 800; constexpr int CLIENT_HEIGHT = 600; int main(int argc, char** argv){ - spdlog::info("Current WorkDir Is: {}",argv[0]); + // spdlog::info("Current WorkDir Is: {}",argv[0]); if(argc != 2){ spdlog::error("Usage: mp filename "); return 0; } - if(!UtilTool::CheckFileIsImage(argv[1])){ - spdlog::info("Unsupport File Type: {}",argv[1]); - return 0; - } - sf::Texture texture; - texture.loadFromFile(argv[1]); + MediaService mediaService(argv[1], CLIENT_WIDTH, CLIENT_HEIGHT); sf::RenderWindow window(sf::VideoMode(CLIENT_WIDTH, CLIENT_HEIGHT), "mp"); - sf::Sprite sprite; - sprite.setTexture(texture); - auto imgSize = texture.getSize(); - auto scale = std::min(static_cast(CLIENT_WIDTH)/imgSize.x,static_cast(CLIENT_HEIGHT)/imgSize.y); - sprite.setScale(scale, scale); bool running = true; while(running){ @@ -43,7 +34,7 @@ int main(int argc, char** argv){ } window.clear(); - window.draw(sprite); + window.draw(mediaService.GetSprite()); window.display(); } return 0; diff --git a/src/UtilTool.cc b/src/UtilTool.cc index 43da0e7..a218b2a 100644 --- a/src/UtilTool.cc +++ b/src/UtilTool.cc @@ -1,15 +1,58 @@ #include "UtilTool.h" #include - -bool UtilTool::CheckFileIsImage(const std::string& filepath){ - //简单实现, 通过后缀判断 +bool UtilTool::CheckFileIsImage(const std::string &filepath) +{ + // 简单实现, 通过后缀判断 auto path = std::filesystem::path(filepath); auto su = path.extension().string(); - for(const auto& type : ImageType){ - if (su.find(type) != std::string::npos){ + for (const auto &type : ImageTypes) + { + if (su.find(type) != std::string::npos) + { return true; } } return false; } + +bool UtilTool::CheckFileIsVideo(const std::string &filepath) +{ + // 简单实现, 通过后缀判断 + auto path = std::filesystem::path(filepath); + auto su = path.extension().string(); + for (const auto &type : VideoTypes) + { + if (su.find(type) != std::string::npos) + { + return true; + } + } + return false; +} +bool UtilTool::CheckFileIsAudio(const std::string &filepath) +{ + // 简单实现, 通过后缀判断 + auto path = std::filesystem::path(filepath); + auto su = path.extension().string(); + for (const auto &type : AudioTypes) + { + if (su.find(type) != std::string::npos) + { + return true; + } + } + return false; +} + +MediaType UtilTool::CheckFileType(const std::string &filepath) +{ + if (CheckFileIsImage(filepath)) + return MediaType::IMAGE; + if (CheckFileIsAudio(filepath)) + return MediaType::AUDIO; + if (CheckFileIsVideo(filepath)) + return MediaType::VIDEO; + return MediaType::UNSUPPORT; + +} \ No newline at end of file diff --git a/src/mediaService.cc b/src/mediaService.cc new file mode 100644 index 0000000..aed5af7 --- /dev/null +++ b/src/mediaService.cc @@ -0,0 +1,22 @@ +#include + +MediaService::MediaService(const std::string& filename, int width, int height){ + auto type = UtilTool::CheckFileType(filename); + client_width = width; + client_height = height; + float scale = .0f; + switch (type) + { + case MediaType::IMAGE: + imageService = new ImageService(filename); + texture = &imageService->GetTexture(); + sprite = new sf::Sprite(); + sprite->setTexture(*texture); + scale = imageService->GetScale(client_width, client_height); + sprite->setScale(scale, scale); + break; + + default: + break; + } +} \ No newline at end of file diff --git a/src/stream.cc b/src/stream.cc new file mode 100644 index 0000000..6351daf --- /dev/null +++ b/src/stream.cc @@ -0,0 +1,2 @@ +#include "stream.h" + diff --git a/test/util_test.cc b/test/util_test.cc index f5f55d7..9a06b7a 100644 --- a/test/util_test.cc +++ b/test/util_test.cc @@ -1,12 +1,10 @@ #include #include "UtilTool.h" +#include -TEST(UtilTest, CheckIsImageTestShouldTrue) { - EXPECT_EQ(UtilTool::CheckFileIsImage("ocean.jpg"), true); - EXPECT_EQ(UtilTool::CheckFileIsImage("test.png"), true); -} -TEST(UtilTest, CheckIsImageTestShouldFalse) { - EXPECT_EQ(UtilTool::CheckFileIsImage("test.mp4"), false); - EXPECT_EQ(UtilTool::CheckFileIsImage("test.wav"), false); - EXPECT_EQ(UtilTool::CheckFileIsImage("--gtest_list_tests"), false); +TEST(UtilTest, CheckIsImage) +{ + EXPECT_TRUE(UtilTool::CheckFileType("file.jpg") == MediaType::IMAGE); } + +