Switch Controller
Loading...
Searching...
No Matches
FFmpegFrameSink.h
Go to the documentation of this file.
1#pragma once
2
3// TODO
4// FFmpeg has a lot of warnings
5// Changing level to 2 on MSCV
6// This does have the potential to break
7// in the future and on other compilers
8#ifdef _MSC_VER
9#pragma warning(push, 2)
10#endif
11
12extern "C" {
13#include <libavcodec/avcodec.h>
14#include <libavformat/avformat.h>
15}
16
17#ifdef _MSC_VER
18#pragma warning(pop)
19#endif
20
21#include "pch.h"
22
24private:
25 mutable std::mutex initMutex;
26 std::condition_variable initCV;
27 bool initialized = false;
28
29 mutable std::mutex dataMutex;
30 std::condition_variable dataCV;
31 long long lastFrame = 0;
32
33protected:
34 virtual void virtualInit(AVCodecContext *decoderContext) = 0;
35 virtual void virtualOutputFrame(AVFrame *frame) = 0;
36 virtual void getDataWithoutLock(std::vector<uint8_t> &data) = 0;
37
38public:
39 virtual ~FFmpegFrameSink() {}
40
41 void init(AVCodecContext *decoderContext) {
42 virtualInit(decoderContext);
43 std::lock_guard<std::mutex> lock(initMutex);
44 this->initialized = true;
45 initCV.notify_all();
46 }
47
48 void waitForInit() {
49 std::unique_lock<std::mutex> lock(initMutex);
50 while (!initialized) {
51 initCV.wait(lock);
52 }
53 }
54
55 void outputFrame(AVFrame *frame) {
56 std::lock_guard<std::mutex> lock(dataMutex);
57 virtualOutputFrame(frame);
58 lastFrame++;
59 dataCV.notify_all();
60 }
61
62 long long getData(std::vector<uint8_t> &data) {
63 std::lock_guard<std::mutex> lock(dataMutex);
65 return lastFrame;
66 }
67
68 long long getNextData(std::vector<uint8_t> &data, long long lastFrameSeen) {
69 std::unique_lock<std::mutex> lock(dataMutex);
70 while (this->lastFrame == lastFrameSeen) {
71 dataCV.wait(lock);
72 }
74 return this->lastFrame;
75 }
76
77 virtual AVMediaType getType() const = 0;
78};
Definition: FFmpegFrameSink.h:23
virtual ~FFmpegFrameSink()
Definition: FFmpegFrameSink.h:39
virtual void virtualOutputFrame(AVFrame *frame)=0
long long getNextData(std::vector< uint8_t > &data, long long lastFrameSeen)
Definition: FFmpegFrameSink.h:68
virtual void getDataWithoutLock(std::vector< uint8_t > &data)=0
void outputFrame(AVFrame *frame)
Definition: FFmpegFrameSink.h:55
virtual AVMediaType getType() const =0
void waitForInit()
Definition: FFmpegFrameSink.h:48
virtual void virtualInit(AVCodecContext *decoderContext)=0
long long getData(std::vector< uint8_t > &data)
Definition: FFmpegFrameSink.h:62
void init(AVCodecContext *decoderContext)
Definition: FFmpegFrameSink.h:41