mp-sfml/include/videoService.h
2024-10-08 14:34:52 +08:00

60 lines
1.6 KiB
C++

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