DDraceNetwork Docs
video.h
Go to the documentation of this file.
1#ifndef ENGINE_CLIENT_VIDEO_H
2#define ENGINE_CLIENT_VIDEO_H
3
4#include <base/lock.h>
5
6extern "C" {
7#include <libavcodec/avcodec.h>
8#include <libavformat/avformat.h>
9};
10
11#include <engine/shared/video.h>
12
13#include <atomic>
14#include <condition_variable>
15#include <mutex>
16#include <thread>
17#include <vector>
18
19class IGraphics;
20class ISound;
21class IStorage;
22
23extern CLock g_WriteLock;
24
25// a wrapper around a single output AVStream
27{
28public:
29 AVStream *m_pStream = nullptr;
30 AVCodecContext *m_pCodecContext = nullptr;
31
32 /* pts of the next frame that will be generated */
33 int64_t m_SamplesCount = 0;
35
36 std::vector<AVFrame *> m_vpFrames;
37 std::vector<AVFrame *> m_vpTmpFrames;
38
39 std::vector<struct SwsContext *> m_vpSwsContexts;
40 std::vector<struct SwrContext *> m_vpSwrContexts;
41};
42
43class CVideo : public IVideo
44{
45public:
46 CVideo(IGraphics *pGraphics, ISound *pSound, IStorage *pStorage, int Width, int Height, const char *pName);
47 ~CVideo();
48
49 bool Start() override REQUIRES(!g_WriteLock);
50 void Stop() override;
51 void Pause(bool Pause) override;
52 bool IsRecording() override { return m_Recording; }
53
54 void NextVideoFrame() override;
55 void NextVideoFrameThread() override;
56
57 void NextAudioFrame(ISoundMixFunc Mix) override;
58 void NextAudioFrameTimeline(ISoundMixFunc Mix) override;
59
61
62 static void Init();
63
64private:
65 void RunVideoThread(size_t ParentThreadIndex, size_t ThreadIndex) REQUIRES(!g_WriteLock);
66 void FillVideoFrame(size_t ThreadIndex) REQUIRES(!g_WriteLock);
67 void UpdateVideoBufferFromGraphics(size_t ThreadIndex);
68
69 void RunAudioThread(size_t ParentThreadIndex, size_t ThreadIndex) REQUIRES(!g_WriteLock);
70 void FillAudioFrame(size_t ThreadIndex);
71
72 bool OpenVideo();
73 bool OpenAudio();
74 AVFrame *AllocPicture(enum AVPixelFormat PixFmt, int Width, int Height);
75 AVFrame *AllocAudioFrame(enum AVSampleFormat SampleFmt, uint64_t ChannelLayout, int SampleRate, int NbSamples);
76
77 void WriteFrame(COutputStream *pStream, size_t ThreadIndex) REQUIRES(g_WriteLock);
78 void FinishFrames(COutputStream *pStream);
79 void CloseStream(COutputStream *pStream);
80
81 bool AddStream(COutputStream *pStream, AVFormatContext *pFormatContext, const AVCodec **ppCodec, enum AVCodecID CodecId) const;
82
86
89 char m_aName[256];
90 uint64_t m_VideoFrameIndex = 0;
91 uint64_t m_AudioFrameIndex = 0;
92
93 int m_FPS;
94
98
99 size_t m_VideoThreads = 2;
101 size_t m_AudioThreads = 2;
103
105 {
106 public:
107 std::thread m_Thread;
108 std::mutex m_Mutex;
109 std::condition_variable m_Cond;
110
111 bool m_Started = false;
112 bool m_Finished = false;
113 bool m_HasVideoFrame = false;
114
116 std::condition_variable m_VideoFillCond;
117 uint64_t m_VideoFrameToFill = 0;
118 };
119
120 std::vector<std::unique_ptr<CVideoRecorderThread>> m_vpVideoThreads;
121
123 {
124 public:
125 std::thread m_Thread;
126 std::mutex m_Mutex;
127 std::condition_variable m_Cond;
128
129 bool m_Started = false;
130 bool m_Finished = false;
131 bool m_HasAudioFrame = false;
132
134 std::condition_variable m_AudioFillCond;
135 uint64_t m_AudioFrameToFill = 0;
137 };
138
139 std::vector<std::unique_ptr<CAudioRecorderThread>> m_vpAudioThreads;
140
141 std::atomic<int32_t> m_ProcessingVideoFrame;
142 std::atomic<int32_t> m_ProcessingAudioFrame;
143
145
147 {
148 public:
149 std::vector<uint8_t> m_vBuffer;
150 };
151 std::vector<CVideoBuffer> m_vVideoBuffers;
153 {
154 public:
155 int16_t m_aBuffer[4096];
156 };
157 std::vector<CAudioBuffer> m_vAudioBuffers;
158
161
162 const AVCodec *m_pVideoCodec;
163 const AVCodec *m_pAudioCodec;
164
165 AVDictionary *m_pOptDict;
166
167 AVFormatContext *m_pFormatContext;
168 const AVOutputFormat *m_pFormat;
169};
170
171#endif
Definition: lock.h:88
Definition: video.h:27
std::vector< AVFrame * > m_vpFrames
Definition: video.h:36
AVCodecContext * m_pCodecContext
Definition: video.h:30
int64_t m_SamplesFrameCount
Definition: video.h:34
std::vector< struct SwrContext * > m_vpSwrContexts
Definition: video.h:40
std::vector< struct SwsContext * > m_vpSwsContexts
Definition: video.h:39
std::vector< AVFrame * > m_vpTmpFrames
Definition: video.h:37
int64_t m_SamplesCount
Definition: video.h:33
AVStream * m_pStream
Definition: video.h:29
Definition: video.h:153
int16_t m_aBuffer[4096]
Definition: video.h:155
Definition: video.h:123
std::condition_variable m_Cond
Definition: video.h:127
int64_t m_SampleCountStart
Definition: video.h:136
bool m_HasAudioFrame
Definition: video.h:131
bool m_Started
Definition: video.h:129
std::mutex m_Mutex
Definition: video.h:126
std::thread m_Thread
Definition: video.h:125
bool m_Finished
Definition: video.h:130
std::condition_variable m_AudioFillCond
Definition: video.h:134
std::mutex m_AudioFillMutex
Definition: video.h:133
uint64_t m_AudioFrameToFill
Definition: video.h:135
Definition: video.h:147
std::vector< uint8_t > m_vBuffer
Definition: video.h:149
Definition: video.h:105
bool m_Started
Definition: video.h:111
std::mutex m_Mutex
Definition: video.h:108
bool m_Finished
Definition: video.h:112
std::mutex m_VideoFillMutex
Definition: video.h:115
uint64_t m_VideoFrameToFill
Definition: video.h:117
std::condition_variable m_VideoFillCond
Definition: video.h:116
bool m_HasVideoFrame
Definition: video.h:113
std::thread m_Thread
Definition: video.h:107
std::condition_variable m_Cond
Definition: video.h:109
Definition: video.h:44
void NextAudioFrame(ISoundMixFunc Mix) override
Definition: video.cpp:420
bool OpenVideo()
Definition: video.cpp:690
bool m_HasAudio
Definition: video.h:144
void NextVideoFrame() override
Definition: video.cpp:398
ISound * m_pSound
Definition: video.h:85
void NextAudioFrameTimeline(ISoundMixFunc Mix) override
Definition: video.cpp:407
IGraphics * m_pGraphics
Definition: video.h:83
std::vector< CVideoBuffer > m_vVideoBuffers
Definition: video.h:151
void NextVideoFrameThread() override
Definition: video.cpp:347
std::vector< std::unique_ptr< CAudioRecorderThread > > m_vpAudioThreads
Definition: video.h:139
bool m_Recording
Definition: video.h:97
void FillVideoFrame(size_t ThreadIndex) REQUIRES(!g_WriteLock)
Definition: video.cpp:613
bool OpenAudio()
Definition: video.cpp:754
size_t m_CurVideoThreadIndex
Definition: video.h:100
static void Init()
Definition: video.cpp:74
COutputStream m_AudioStream
Definition: video.h:160
void RunVideoThread(size_t ParentThreadIndex, size_t ThreadIndex) REQUIRES(!g_WriteLock)
Definition: video.cpp:569
void UpdateVideoBufferFromGraphics(size_t ThreadIndex)
Definition: video.cpp:621
std::atomic< int32_t > m_ProcessingVideoFrame
Definition: video.h:141
size_t m_CurAudioThreadIndex
Definition: video.h:102
IStorage * m_pStorage
Definition: video.h:84
uint64_t m_VideoFrameIndex
Definition: video.h:90
bool AddStream(COutputStream *pStream, AVFormatContext *pFormatContext, const AVCodec **ppCodec, enum AVCodecID CodecId) const
Definition: video.cpp:863
AVFrame * AllocAudioFrame(enum AVSampleFormat SampleFmt, uint64_t ChannelLayout, int SampleRate, int NbSamples)
Definition: video.cpp:657
void RunAudioThread(size_t ParentThreadIndex, size_t ThreadIndex) REQUIRES(!g_WriteLock)
Definition: video.cpp:480
AVFormatContext * m_pFormatContext
Definition: video.h:167
std::vector< CAudioBuffer > m_vAudioBuffers
Definition: video.h:157
size_t m_AudioThreads
Definition: video.h:101
AVDictionary * m_pOptDict
Definition: video.h:165
void CloseStream(COutputStream *pStream)
Definition: video.cpp:1078
const AVOutputFormat * m_pFormat
Definition: video.h:168
std::atomic< int32_t > m_ProcessingAudioFrame
Definition: video.h:142
void WriteFrame(COutputStream *pStream, size_t ThreadIndex) REQUIRES(g_WriteLock)
Definition: video.cpp:985
bool m_Started
Definition: video.h:95
CVideo(IGraphics *pGraphics, ISound *pSound, IStorage *pStorage, int Width, int Height, const char *pName)
Definition: video.cpp:79
size_t m_VideoThreads
Definition: video.h:99
char m_aName[256]
Definition: video.h:89
static IVideo * Current()
Definition: video.h:60
bool IsRecording() override
Definition: video.h:52
void FinishFrames(COutputStream *pStream)
Definition: video.cpp:1030
uint64_t m_AudioFrameIndex
Definition: video.h:91
void FillAudioFrame(size_t ThreadIndex)
Definition: video.cpp:525
~CVideo()
Definition: video.cpp:111
COutputStream m_VideoStream
Definition: video.h:159
std::vector< std::unique_ptr< CVideoRecorderThread > > m_vpVideoThreads
Definition: video.h:120
bool Start() override REQUIRES(!g_WriteLock)
Definition: video.cpp:116
int m_Height
Definition: video.h:88
void Stop() override
Definition: video.cpp:283
bool m_Stopped
Definition: video.h:96
const AVCodec * m_pAudioCodec
Definition: video.h:163
void Pause(bool Pause) override
Definition: video.cpp:277
AVFrame * AllocPicture(enum AVPixelFormat PixFmt, int Width, int Height)
Definition: video.cpp:631
const AVCodec * m_pVideoCodec
Definition: video.h:162
int m_FPS
Definition: video.h:93
int m_Width
Definition: video.h:87
Definition: graphics.h:183
Definition: sound.h:12
Definition: storage.h:20
Definition: video.h:11
static IVideo * ms_pCurrentVideo
Definition: video.h:34
CLock g_WriteLock
Definition: video.cpp:37
#define REQUIRES(...)
Definition: lock.h:32
std::function< void(short *pFinalOut, unsigned Frames)> ISoundMixFunc
Definition: video.h:8