mp-sfml/include/videoService.h

60 lines
1.6 KiB
C
Raw Permalink Normal View History

2024-09-30 13:34:05 +08:00
#ifndef VIDEOSERVICE_H
#define VIDEOSERVICE_H
#include "thread_queue.h"
2024-09-30 13:34:05 +08:00
#include <SFML/Graphics.hpp>
#include <functional>
2024-09-30 13:34:05 +08:00
#include <memory>
#include <thread>
2024-09-30 13:34:05 +08:00
extern "C" {
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libavutil/avutil.h>
2024-10-08 14:34:52 +08:00
#include <libavutil/imgutils.h>
#include <libswscale/swscale.h>
}
void AVFormatContextDeleter(AVFormatContext *context);
void AVCodecContextDeleter(AVCodecContext *context);
2024-09-30 13:34:05 +08:00
class VideoService {
private:
std::unique_ptr<AVFormatContext, std::function<void(AVFormatContext *)>>
formatContext;
std::unique_ptr<AVCodecContext, std::function<void(AVCodecContext *)>>
codecContext;
2024-10-08 14:34:52 +08:00
ThreadQueue<AVPacket *> packetQueue;
ThreadQueue<AVFrame *> frameQueue;
ThreadQueue<AVPacket *> audioQueue;
std::string_view filename;
2024-10-08 14:34:52 +08:00
std::jthread decodePacketThread;
std::jthread decodeFrameThread;
int videoStreamIndex;
int audioStreamIndex;
int waitDelay = 50;
unsigned int width;
unsigned int height;
std::stop_source stopSource;
2024-10-08 14:34:52 +08:00
std::shared_ptr<sf::RenderWindow> window;
std::unique_ptr<sf::Sprite> sprite;
std::unique_ptr<sf::Texture> texture;
public:
2024-10-08 14:34:52 +08:00
VideoService(std::string_view filename, std::shared_ptr<sf::RenderWindow> window);
~VideoService() {
2024-10-08 14:34:52 +08:00
if (decodePacketThread.joinable()) {
decodePacketThread.join();
}
if (decodeFrameThread.joinable()) {
decodeFrameThread.join();
}
}
void InitContext();
void Decode();
2024-10-08 14:34:52 +08:00
void PaintFrame();
void Play();
2024-09-30 13:34:05 +08:00
};
#endif