DDraceNetwork Documentation
Loading...
Searching...
No Matches
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
23// a wrapper around a single output AVStream
25{
26public:
27 AVStream *m_pStream = nullptr;
28 AVCodecContext *m_pCodecContext = nullptr;
29
30 /* pts of the next frame that will be generated */
31 int64_t m_SamplesCount = 0;
33
34 std::vector<AVFrame *> m_vpFrames;
35 std::vector<AVFrame *> m_vpTmpFrames;
36
37 std::vector<struct SwsContext *> m_vpSwsContexts;
38 std::vector<struct SwrContext *> m_vpSwrContexts;
39};
40
41class CVideo : public IVideo
42{
43public:
44 CVideo(IGraphics *pGraphics, ISound *pSound, IStorage *pStorage, int Width, int Height, const char *pName);
45 ~CVideo() override;
46
47 bool Start() override REQUIRES(!m_WriteLock);
48 void Stop() override;
49 void Pause(bool Pause) override;
50 bool IsRecording() override { return m_Recording; }
51
52 void NextVideoFrame() override;
53 void NextVideoFrameThread() override;
54
55 void NextAudioFrame(ISoundMixFunc Mix) override;
56 void NextAudioFrameTimeline(ISoundMixFunc Mix) override;
57
59
60 static void Init();
61
62private:
63 void RunVideoThread(size_t ParentThreadIndex, size_t ThreadIndex) REQUIRES(!m_WriteLock);
64 void FillVideoFrame(size_t ThreadIndex) REQUIRES(!m_WriteLock);
65 void UpdateVideoBufferFromGraphics(size_t ThreadIndex);
66
67 void RunAudioThread(size_t ParentThreadIndex, size_t ThreadIndex) REQUIRES(!m_WriteLock);
68 void FillAudioFrame(size_t ThreadIndex);
69
70 bool OpenVideo();
71 bool OpenAudio();
72 AVFrame *AllocPicture(enum AVPixelFormat PixFmt, int Width, int Height);
73 AVFrame *AllocAudioFrame(enum AVSampleFormat SampleFmt, uint64_t ChannelLayout, int SampleRate, int NbSamples);
74
75 void WriteFrame(COutputStream *pStream, size_t ThreadIndex) REQUIRES(m_WriteLock);
76 void FinishFrames(COutputStream *pStream);
77 void CloseStream(COutputStream *pStream);
78
79 bool AddStream(COutputStream *pStream, AVFormatContext *pFormatContext, const AVCodec **ppCodec, enum AVCodecID CodecId) const;
80
84
87 char m_aName[256];
88 uint64_t m_VideoFrameIndex = 0;
89 uint64_t m_AudioFrameIndex = 0;
90
91 int m_FPS;
92
96
98 size_t m_VideoThreads = 2;
100 size_t m_AudioThreads = 2;
102
104 {
105 public:
106 std::thread m_Thread;
107 std::mutex m_Mutex;
108 std::condition_variable m_Cond;
109
110 bool m_Started = false;
111 bool m_Finished = false;
112 bool m_HasVideoFrame = false;
113
115 std::condition_variable m_VideoFillCond;
116 uint64_t m_VideoFrameToFill = 0;
117 };
118
119 std::vector<std::unique_ptr<CVideoRecorderThread>> m_vpVideoThreads;
120
122 {
123 public:
124 std::thread m_Thread;
125 std::mutex m_Mutex;
126 std::condition_variable m_Cond;
127
128 bool m_Started = false;
129 bool m_Finished = false;
130 bool m_HasAudioFrame = false;
131
133 std::condition_variable m_AudioFillCond;
134 uint64_t m_AudioFrameToFill = 0;
136 };
137
138 std::vector<std::unique_ptr<CAudioRecorderThread>> m_vpAudioThreads;
139
140 std::atomic<int32_t> m_ProcessingVideoFrame;
141 std::atomic<int32_t> m_ProcessingAudioFrame;
142
144
146 {
147 public:
148 std::vector<uint8_t> m_vBuffer;
149 };
150 std::vector<CVideoBuffer> m_vVideoBuffers;
152 {
153 public:
154 int16_t m_aBuffer[4096];
155 };
156 std::vector<CAudioBuffer> m_vAudioBuffers;
157
160
161 const AVCodec *m_pVideoCodec;
162 const AVCodec *m_pAudioCodec;
163
164 AVDictionary *m_pOptDict;
165
166 AVFormatContext *m_pFormatContext;
167 const AVOutputFormat *m_pFormat;
168};
169
170#endif
Definition lock.h:90
Definition video.h:25
std::vector< AVFrame * > m_vpFrames
Definition video.h:34
AVCodecContext * m_pCodecContext
Definition video.h:28
int64_t m_SamplesFrameCount
Definition video.h:32
std::vector< struct SwrContext * > m_vpSwrContexts
Definition video.h:38
std::vector< struct SwsContext * > m_vpSwsContexts
Definition video.h:37
std::vector< AVFrame * > m_vpTmpFrames
Definition video.h:35
int64_t m_SamplesCount
Definition video.h:31
AVStream * m_pStream
Definition video.h:27
Definition video.h:152
int16_t m_aBuffer[4096]
Definition video.h:154
Definition video.h:122
std::condition_variable m_Cond
Definition video.h:126
int64_t m_SampleCountStart
Definition video.h:135
bool m_HasAudioFrame
Definition video.h:130
bool m_Started
Definition video.h:128
std::mutex m_Mutex
Definition video.h:125
std::thread m_Thread
Definition video.h:124
bool m_Finished
Definition video.h:129
std::condition_variable m_AudioFillCond
Definition video.h:133
std::mutex m_AudioFillMutex
Definition video.h:132
uint64_t m_AudioFrameToFill
Definition video.h:134
Definition video.h:146
std::vector< uint8_t > m_vBuffer
Definition video.h:148
Definition video.h:104
bool m_Started
Definition video.h:110
std::mutex m_Mutex
Definition video.h:107
bool m_Finished
Definition video.h:111
std::mutex m_VideoFillMutex
Definition video.h:114
uint64_t m_VideoFrameToFill
Definition video.h:116
std::condition_variable m_VideoFillCond
Definition video.h:115
bool m_HasVideoFrame
Definition video.h:112
std::thread m_Thread
Definition video.h:106
std::condition_variable m_Cond
Definition video.h:108
void NextAudioFrame(ISoundMixFunc Mix) override
Definition video.cpp:423
bool OpenVideo()
Definition video.cpp:697
bool m_HasAudio
Definition video.h:143
void NextVideoFrame() override
Definition video.cpp:401
void RunAudioThread(size_t ParentThreadIndex, size_t ThreadIndex) REQUIRES(!m_WriteLock)
Definition video.cpp:483
ISound * m_pSound
Definition video.h:83
void NextAudioFrameTimeline(ISoundMixFunc Mix) override
Definition video.cpp:410
IGraphics * m_pGraphics
Definition video.h:81
std::vector< CVideoBuffer > m_vVideoBuffers
Definition video.h:150
void NextVideoFrameThread() override
Definition video.cpp:350
std::vector< std::unique_ptr< CAudioRecorderThread > > m_vpAudioThreads
Definition video.h:138
CLock m_WriteLock
Definition video.h:97
void WriteFrame(COutputStream *pStream, size_t ThreadIndex) REQUIRES(m_WriteLock)
Definition video.cpp:993
bool m_Recording
Definition video.h:95
bool OpenAudio()
Definition video.cpp:761
size_t m_CurVideoThreadIndex
Definition video.h:99
static void Init()
Definition video.cpp:74
COutputStream m_AudioStream
Definition video.h:159
void UpdateVideoBufferFromGraphics(size_t ThreadIndex)
Definition video.cpp:628
std::atomic< int32_t > m_ProcessingVideoFrame
Definition video.h:140
size_t m_CurAudioThreadIndex
Definition video.h:101
IStorage * m_pStorage
Definition video.h:82
uint64_t m_VideoFrameIndex
Definition video.h:88
bool AddStream(COutputStream *pStream, AVFormatContext *pFormatContext, const AVCodec **ppCodec, enum AVCodecID CodecId) const
Definition video.cpp:870
~CVideo() override
Definition video.cpp:111
AVFrame * AllocAudioFrame(enum AVSampleFormat SampleFmt, uint64_t ChannelLayout, int SampleRate, int NbSamples)
Definition video.cpp:664
AVFormatContext * m_pFormatContext
Definition video.h:166
std::vector< CAudioBuffer > m_vAudioBuffers
Definition video.h:156
size_t m_AudioThreads
Definition video.h:100
AVDictionary * m_pOptDict
Definition video.h:164
void CloseStream(COutputStream *pStream)
Definition video.cpp:1086
const AVOutputFormat * m_pFormat
Definition video.h:167
std::atomic< int32_t > m_ProcessingAudioFrame
Definition video.h:141
bool m_Started
Definition video.h:93
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:98
char m_aName[256]
Definition video.h:87
static IVideo * Current()
Definition video.h:58
bool IsRecording() override
Definition video.h:50
void FinishFrames(COutputStream *pStream)
Definition video.cpp:1038
uint64_t m_AudioFrameIndex
Definition video.h:89
void FillAudioFrame(size_t ThreadIndex)
Definition video.cpp:528
void FillVideoFrame(size_t ThreadIndex) REQUIRES(!m_WriteLock)
Definition video.cpp:620
COutputStream m_VideoStream
Definition video.h:158
std::vector< std::unique_ptr< CVideoRecorderThread > > m_vpVideoThreads
Definition video.h:119
int m_Height
Definition video.h:86
void Stop() override
Definition video.cpp:286
bool m_Stopped
Definition video.h:94
const AVCodec * m_pAudioCodec
Definition video.h:162
void RunVideoThread(size_t ParentThreadIndex, size_t ThreadIndex) REQUIRES(!m_WriteLock)
Definition video.cpp:572
bool Start() override REQUIRES(!m_WriteLock)
Definition video.cpp:116
void Pause(bool Pause) override
Definition video.cpp:280
AVFrame * AllocPicture(enum AVPixelFormat PixFmt, int Width, int Height)
Definition video.cpp:638
const AVCodec * m_pVideoCodec
Definition video.h:161
int m_FPS
Definition video.h:91
int m_Width
Definition video.h:85
Definition graphics.h:189
Definition sound.h:12
Definition storage.h:21
Definition video.h:11
static IVideo * ms_pCurrentVideo
Definition video.h:34
#define REQUIRES(...)
Definition lock.h:34
std::function< void(short *pFinalOut, unsigned Frames)> ISoundMixFunc
Definition video.h:8