DDNet 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, int64_t LocalStartTime, 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() const 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
58 int64_t Time() const override { return m_Time; }
59 float LocalTime() const override { return m_LocalTime; }
60 void SetLocalStartTime(int64_t LocalStartTime) override { m_LocalStartTime = LocalStartTime; }
61
62 static void Init();
63
64private:
65 void RunVideoThread(size_t ParentThreadIndex, size_t ThreadIndex) REQUIRES(!m_WriteLock);
66 void FillVideoFrame(size_t ThreadIndex) REQUIRES(!m_WriteLock);
67 void UpdateVideoBufferFromGraphics(size_t ThreadIndex);
68
69 void RunAudioThread(size_t ParentThreadIndex, size_t ThreadIndex) REQUIRES(!m_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(m_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 int64_t m_TickTime;
97 int64_t m_Time;
98
102
104 size_t m_VideoThreads = 2;
106 size_t m_AudioThreads = 2;
108
110 {
111 public:
112 std::thread m_Thread;
113 std::mutex m_Mutex;
114 std::condition_variable m_Cond;
115
116 bool m_Started = false;
117 bool m_Finished = false;
118 bool m_HasVideoFrame = false;
119
121 std::condition_variable m_VideoFillCond;
122 uint64_t m_VideoFrameToFill = 0;
123 };
124
125 std::vector<std::unique_ptr<CVideoRecorderThread>> m_vpVideoThreads;
126
128 {
129 public:
130 std::thread m_Thread;
131 std::mutex m_Mutex;
132 std::condition_variable m_Cond;
133
134 bool m_Started = false;
135 bool m_Finished = false;
136 bool m_HasAudioFrame = false;
137
139 std::condition_variable m_AudioFillCond;
140 uint64_t m_AudioFrameToFill = 0;
142 };
143
144 std::vector<std::unique_ptr<CAudioRecorderThread>> m_vpAudioThreads;
145
146 std::atomic<int32_t> m_ProcessingVideoFrame;
147 std::atomic<int32_t> m_ProcessingAudioFrame;
148
150
152 {
153 public:
154 std::vector<uint8_t> m_vBuffer;
155 };
156 std::vector<CVideoBuffer> m_vVideoBuffers;
158 {
159 public:
160 int16_t m_aBuffer[4096];
161 };
162 std::vector<CAudioBuffer> m_vAudioBuffers;
163
166
167 const AVCodec *m_pVideoCodec;
168 const AVCodec *m_pAudioCodec;
169
170 AVDictionary *m_pOptDict;
171
172 AVFormatContext *m_pFormatContext;
173 const AVOutputFormat *m_pFormat;
174};
175
176#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:158
int16_t m_aBuffer[4096]
Definition video.h:160
Definition video.h:128
std::condition_variable m_Cond
Definition video.h:132
int64_t m_SampleCountStart
Definition video.h:141
bool m_HasAudioFrame
Definition video.h:136
bool m_Started
Definition video.h:134
std::mutex m_Mutex
Definition video.h:131
std::thread m_Thread
Definition video.h:130
bool m_Finished
Definition video.h:135
std::condition_variable m_AudioFillCond
Definition video.h:139
std::mutex m_AudioFillMutex
Definition video.h:138
uint64_t m_AudioFrameToFill
Definition video.h:140
Definition video.h:152
std::vector< uint8_t > m_vBuffer
Definition video.h:154
Definition video.h:110
bool m_Started
Definition video.h:116
std::mutex m_Mutex
Definition video.h:113
bool m_Finished
Definition video.h:117
std::mutex m_VideoFillMutex
Definition video.h:120
uint64_t m_VideoFrameToFill
Definition video.h:122
std::condition_variable m_VideoFillCond
Definition video.h:121
bool m_HasVideoFrame
Definition video.h:118
std::thread m_Thread
Definition video.h:112
std::condition_variable m_Cond
Definition video.h:114
int64_t m_LocalStartTime
Definition video.h:95
void NextAudioFrame(ISoundMixFunc Mix) override
Definition video.cpp:428
CVideo(IGraphics *pGraphics, ISound *pSound, IStorage *pStorage, int Width, int Height, int64_t LocalStartTime, const char *pName)
Definition video.cpp:83
bool OpenVideo()
Definition video.cpp:702
bool m_HasAudio
Definition video.h:149
float LocalTime() const override
Definition video.h:59
void NextVideoFrame() override
Definition video.cpp:406
void RunAudioThread(size_t ParentThreadIndex, size_t ThreadIndex) REQUIRES(!m_WriteLock)
Definition video.cpp:488
ISound * m_pSound
Definition video.h:85
void NextAudioFrameTimeline(ISoundMixFunc Mix) override
Definition video.cpp:415
IGraphics * m_pGraphics
Definition video.h:83
std::vector< CVideoBuffer > m_vVideoBuffers
Definition video.h:156
void NextVideoFrameThread() override
Definition video.cpp:355
std::vector< std::unique_ptr< CAudioRecorderThread > > m_vpAudioThreads
Definition video.h:144
CLock m_WriteLock
Definition video.h:103
void WriteFrame(COutputStream *pStream, size_t ThreadIndex) REQUIRES(m_WriteLock)
Definition video.cpp:998
bool m_Recording
Definition video.h:101
bool OpenAudio()
Definition video.cpp:766
size_t m_CurVideoThreadIndex
Definition video.h:105
static void Init()
Definition video.cpp:78
float m_LocalTime
Definition video.h:96
int64_t m_Time
Definition video.h:97
COutputStream m_AudioStream
Definition video.h:165
void UpdateVideoBufferFromGraphics(size_t ThreadIndex)
Definition video.cpp:633
std::atomic< int32_t > m_ProcessingVideoFrame
Definition video.h:146
size_t m_CurAudioThreadIndex
Definition video.h:107
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:875
~CVideo() override
Definition video.cpp:115
AVFrame * AllocAudioFrame(enum AVSampleFormat SampleFmt, uint64_t ChannelLayout, int SampleRate, int NbSamples)
Definition video.cpp:669
AVFormatContext * m_pFormatContext
Definition video.h:172
std::vector< CAudioBuffer > m_vAudioBuffers
Definition video.h:162
size_t m_AudioThreads
Definition video.h:106
AVDictionary * m_pOptDict
Definition video.h:170
void CloseStream(COutputStream *pStream)
Definition video.cpp:1091
const AVOutputFormat * m_pFormat
Definition video.h:173
std::atomic< int32_t > m_ProcessingAudioFrame
Definition video.h:147
bool m_Started
Definition video.h:99
int64_t m_TickTime
Definition video.h:94
size_t m_VideoThreads
Definition video.h:104
char m_aName[256]
Definition video.h:89
void FinishFrames(COutputStream *pStream)
Definition video.cpp:1043
uint64_t m_AudioFrameIndex
Definition video.h:91
void FillAudioFrame(size_t ThreadIndex)
Definition video.cpp:533
void FillVideoFrame(size_t ThreadIndex) REQUIRES(!m_WriteLock)
Definition video.cpp:625
COutputStream m_VideoStream
Definition video.h:164
int64_t Time() const override
Definition video.h:58
void SetLocalStartTime(int64_t LocalStartTime) override
Definition video.h:60
std::vector< std::unique_ptr< CVideoRecorderThread > > m_vpVideoThreads
Definition video.h:125
int m_Height
Definition video.h:88
void Stop() override
Definition video.cpp:291
bool m_Stopped
Definition video.h:100
const AVCodec * m_pAudioCodec
Definition video.h:168
void RunVideoThread(size_t ParentThreadIndex, size_t ThreadIndex) REQUIRES(!m_WriteLock)
Definition video.cpp:577
bool Start() override REQUIRES(!m_WriteLock)
Definition video.cpp:120
void Pause(bool Pause) override
Definition video.cpp:285
bool IsRecording() const override
Definition video.h:50
AVFrame * AllocPicture(enum AVPixelFormat PixFmt, int Width, int Height)
Definition video.cpp:643
const AVCodec * m_pVideoCodec
Definition video.h:167
int m_FPS
Definition video.h:93
int m_Width
Definition video.h:87
Definition graphics.h:188
Definition sound.h:12
Definition storage.h:21
Definition video.h:10
#define REQUIRES(...)
Definition lock.h:34
std::function< void(short *pFinalOut, unsigned Frames)> ISoundMixFunc
Definition video.h:7