抽象多媒体类, 修复gtest问题
This commit is contained in:
parent
628b95d37d
commit
7c049db4ac
@ -35,12 +35,18 @@ IF(UNIX)
|
|||||||
|
|
||||||
spdlog
|
spdlog
|
||||||
)
|
)
|
||||||
|
|
||||||
add_executable(${PROJECT_N_T}
|
add_executable(${PROJECT_N_T}
|
||||||
${srcs}
|
${srcs}
|
||||||
${tests}
|
${tests}
|
||||||
)
|
)
|
||||||
target_link_libraries(${PROJECT_N_T} PRIVATE
|
target_link_libraries(${PROJECT_N_T} PRIVATE
|
||||||
spdlog
|
sfml-system
|
||||||
|
sfml-window
|
||||||
|
sfml-graphics
|
||||||
|
sfml-network
|
||||||
|
sfml-audio
|
||||||
|
|
||||||
GTest::gtest_main
|
GTest::gtest_main
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -55,13 +61,13 @@ IF(UNIX)
|
|||||||
COMMAND ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_N_T}
|
COMMAND ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_N_T}
|
||||||
DEPENDS ${PROJECT_N_T}
|
DEPENDS ${PROJECT_N_T}
|
||||||
WORKING_DIRECTORY ${PROJECT_CURRENT_BINARY_DIR}
|
WORKING_DIRECTORY ${PROJECT_CURRENT_BINARY_DIR}
|
||||||
COMMENT "Starting ${PROJECT_N}"
|
COMMENT "Starting ${PROJECT_N_T}"
|
||||||
)
|
)
|
||||||
|
|
||||||
ELSE(WIN32)
|
ELSE(WIN32)
|
||||||
|
|
||||||
ENDIF()
|
ENDIF()
|
||||||
include(GoogleTest)
|
include(GoogleTest)
|
||||||
gtest_discover_tests(${PROJECT_N})
|
gtest_discover_tests(${PROJECT_N_T})
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,9 +1,27 @@
|
|||||||
|
#ifndef UTILTOOL_H
|
||||||
|
#define UTILTOOL_H
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <array>
|
#include <array>
|
||||||
|
|
||||||
|
enum class MediaType{
|
||||||
|
VIDEO,
|
||||||
|
AUDIO,
|
||||||
|
IMAGE,
|
||||||
|
UNSUPPORT,
|
||||||
|
};
|
||||||
|
|
||||||
class UtilTool
|
class UtilTool
|
||||||
{
|
{
|
||||||
public:
|
private:
|
||||||
constexpr static std::array<std::string, 2> ImageType{"jpg","png"};
|
constexpr static std::array<std::string, 2> ImageTypes{"jpg","png"};
|
||||||
|
constexpr static std::array<std::string, 1> VideoTypes{"mp4"};
|
||||||
|
constexpr static std::array<std::string, 1> AudioTypes{"mp3"};
|
||||||
static bool CheckFileIsImage(const std::string& filename);
|
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
|
18
include/imageService.h
Normal file
18
include/imageService.h
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
#include <SFML/Graphics.hpp>
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
|
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<float>(width) / imgSize.x, static_cast<float>(height) / imgSize.y);
|
||||||
|
}
|
||||||
|
};
|
25
include/mediaService.h
Normal file
25
include/mediaService.h
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
#include <imageService.h>
|
||||||
|
#include <UtilTool.h>
|
||||||
|
#include <string>
|
||||||
|
#include <SFML/Graphics.hpp>
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
};
|
12
include/stream.h
Normal file
12
include/stream.h
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
#include <libavcodec/codec.h>
|
||||||
|
extern "C" {
|
||||||
|
#include <libavcodec/avcodec.h>
|
||||||
|
#include <libavformat/avformat.h>
|
||||||
|
#include <libavutil/avutil.h>
|
||||||
|
}
|
||||||
|
|
||||||
|
class Stream {
|
||||||
|
private:
|
||||||
|
AVFormatContext* m_avFormatContext;
|
||||||
|
|
||||||
|
};
|
17
main.cc
17
main.cc
@ -4,29 +4,20 @@
|
|||||||
#include <SFML/Window.hpp>
|
#include <SFML/Window.hpp>
|
||||||
#include <spdlog/spdlog.h>
|
#include <spdlog/spdlog.h>
|
||||||
#include "UtilTool.h"
|
#include "UtilTool.h"
|
||||||
|
#include "mediaService.h"
|
||||||
|
|
||||||
constexpr int CLIENT_WIDTH = 800;
|
constexpr int CLIENT_WIDTH = 800;
|
||||||
constexpr int CLIENT_HEIGHT = 600;
|
constexpr int CLIENT_HEIGHT = 600;
|
||||||
|
|
||||||
|
|
||||||
int main(int argc, char** argv){
|
int main(int argc, char** argv){
|
||||||
spdlog::info("Current WorkDir Is: {}",argv[0]);
|
// spdlog::info("Current WorkDir Is: {}",argv[0]);
|
||||||
if(argc != 2){
|
if(argc != 2){
|
||||||
spdlog::error("Usage: mp filename ");
|
spdlog::error("Usage: mp filename ");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
if(!UtilTool::CheckFileIsImage(argv[1])){
|
MediaService mediaService(argv[1], CLIENT_WIDTH, CLIENT_HEIGHT);
|
||||||
spdlog::info("Unsupport File Type: {}",argv[1]);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
sf::Texture texture;
|
|
||||||
texture.loadFromFile(argv[1]);
|
|
||||||
sf::RenderWindow window(sf::VideoMode(CLIENT_WIDTH, CLIENT_HEIGHT), "mp");
|
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<float>(CLIENT_WIDTH)/imgSize.x,static_cast<float>(CLIENT_HEIGHT)/imgSize.y);
|
|
||||||
sprite.setScale(scale, scale);
|
|
||||||
bool running = true;
|
bool running = true;
|
||||||
|
|
||||||
while(running){
|
while(running){
|
||||||
@ -43,7 +34,7 @@ int main(int argc, char** argv){
|
|||||||
|
|
||||||
}
|
}
|
||||||
window.clear();
|
window.clear();
|
||||||
window.draw(sprite);
|
window.draw(mediaService.GetSprite());
|
||||||
window.display();
|
window.display();
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -1,15 +1,58 @@
|
|||||||
#include "UtilTool.h"
|
#include "UtilTool.h"
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
|
|
||||||
|
bool UtilTool::CheckFileIsImage(const std::string &filepath)
|
||||||
bool UtilTool::CheckFileIsImage(const std::string& filepath){
|
{
|
||||||
//简单实现, 通过后缀判断
|
// 简单实现, 通过后缀判断
|
||||||
auto path = std::filesystem::path(filepath);
|
auto path = std::filesystem::path(filepath);
|
||||||
auto su = path.extension().string();
|
auto su = path.extension().string();
|
||||||
for(const auto& type : ImageType){
|
for (const auto &type : ImageTypes)
|
||||||
if (su.find(type) != std::string::npos){
|
{
|
||||||
|
if (su.find(type) != std::string::npos)
|
||||||
|
{
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false;
|
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;
|
||||||
|
|
||||||
|
}
|
22
src/mediaService.cc
Normal file
22
src/mediaService.cc
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
#include <mediaService.h>
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
2
src/stream.cc
Normal file
2
src/stream.cc
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
#include "stream.h"
|
||||||
|
|
@ -1,12 +1,10 @@
|
|||||||
#include <gtest/gtest.h>
|
#include <gtest/gtest.h>
|
||||||
#include "UtilTool.h"
|
#include "UtilTool.h"
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
TEST(UtilTest, CheckIsImageTestShouldTrue) {
|
TEST(UtilTest, CheckIsImage)
|
||||||
EXPECT_EQ(UtilTool::CheckFileIsImage("ocean.jpg"), true);
|
{
|
||||||
EXPECT_EQ(UtilTool::CheckFileIsImage("test.png"), true);
|
EXPECT_TRUE(UtilTool::CheckFileType("file.jpg") == MediaType::IMAGE);
|
||||||
}
|
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user