forked from facebookarchive/360-Capture-SDK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFBCaptureSystem.h
321 lines (273 loc) · 11 KB
/
FBCaptureSystem.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/*
* Copyright (c) 2014, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/
#pragma once
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include "Common/Common.h"
#include "Common/ScopedCOMPtr.h"
#include "Graphics/IGraphicsDeviceCapture.h"
#include "Graphics/TextureRender.h"
#include "Graphics/ScreenPixelShader.h"
#include "Graphics/ScreenVertexShader.h"
#include "Graphics/YUVPixelShader.h"
#include <atomic>
#include <mfidl.h>
#include <VersionHelpers.h>
namespace FBCapture {
namespace Camera {
// Forward Declares
class CameraDevices;
class CameraDeviceManager;
class CameraReader;
}
}
namespace FBCapture {
namespace Microphone {
// Forward Declares
class MicDevices;
}
}
using namespace FBCapture::Microphone;
using namespace FBCapture::Camera;
using namespace VrDeviceType;
using namespace StereoMode;
using namespace ProjectionType;
namespace FBCapture {
namespace Common {
// Forward Declares
class EncoderMain;
// From User
struct LiveCaptureSettings {
uint32_t frameRate_;
float encodeCycle_; // derived from frameRate_
uint32_t videoBitRate_;
float flushCycleStart_;
float flushCycleAfter_;
float flushCycle_; // derived from flushCycleStart_ and flushCycleAfter_
std::wstring streamUrl_;
bool is360_;
bool verticalFlip_;
bool horizontalFlip_;
uint32_t width_;
uint32_t height_;
PROJECTIONTYPE projectionType_;
STEREO_MODE stereoMode_;
};
// From User
struct VodCaptureSettings {
uint32_t frameRate_;
float encodeCycle_; // derived from frameRate_
uint32_t videoBitRate_;
std::wstring fullSavePath_;
bool is360_;
bool verticalFlip_;
bool horizontalFlip_;
uint32_t width_;
uint32_t height_;
PROJECTIONTYPE projectionType_;
STEREO_MODE stereoMode_;
};
// From User
struct PreviewCaptureSettings {
uint32_t frameRate_;
float encodeCycle_; // derived from frameRate_
bool is360_;
bool verticalFlip_;
bool horizontalFlip_;
uint32_t width_;
uint32_t height_;
};
// From User
struct ScreenshotSettings {
std::wstring fullSavePath_;
bool is360_;
bool verticalFlip_;
bool horizontalFlip_;
uint32_t width_;
uint32_t height_;
};
// From User
struct CameraOverlaySettings {
float widthPercentage_;
uint32_t viewPortTopLeftX_;
uint32_t viewPortTopLeftY_;
};
// From User
struct CameraSettings {
uint32_t cameraDeviceIndexChosen_;
bool enabledDuringCapture_;
};
struct CameraOverlay {
std::unique_ptr<CameraReader> pCameraReader_ = {};
ScopedCOMPtr<IMFMediaSource> pCameraMediaSource_ = {};
ScopedCOMPtr<ID3D11Texture2D> pSharedCameraTexture_ = {};
ScopedCOMPtr<IDXGIKeyedMutex> pSharedCameraTextureKeyedMutex_ = {};
uint32_t cameraTextureOverlayWidth_ = {};
uint32_t cameraTextureOverlayHeight_ = {};
};
// From User
struct MicSettings {
uint32_t micDeviceIndexChosen_;
bool enabledDuringCapture_;
LPWSTR micDeviceIdChosen_; // derived from micDeviceIndexChosen_
};
/* TODO AudioRenderSettings
// From User
struct AudioRenderSettings {
uint32_t audioRenderDeviceIndexChosen_;
uint32_t bitRate_;
uint32_t sampleRate_;
};
*/
enum FBCaptureType {
kLive = 0,
kVod = 1,
kPreview = 2,
kScreenShot = 3,
kFBCaptureTypeCount
};
class FBCaptureSystem {
public:
FBCaptureSystem() {}
~FBCaptureSystem() { shutdown(); }
bool isInitialized() const { return initialized_; }
FBCAPTURE_STATUS initialize();
FBCAPTURE_STATUS getCaptureCapability();
ID3D11Device *getDevice() { return pDevice_; }
ID3D11DeviceContext *getDeviceContext() { return pContext_; }
FBCAPTURE_STATUS setGraphicsDeviceD3D11(ID3D11Device* device);
FBCAPTURE_STATUS setLiveCaptureSettings(int width, int height, int frameRate, int bitRate, float flushCycleStart, float flushCycleAfter, const TCHAR* streamUrl, bool is360, bool verticalFlip, bool horizontalFlip, PROJECTIONTYPE projectionType, STEREO_MODE stereoMode);
FBCAPTURE_STATUS setVodCaptureSettings(int width, int height, int frameRate, int bitRate, const TCHAR* fullSavePath, bool is360, bool verticalFlip, bool horizontalFlip, PROJECTIONTYPE projectionType, STEREO_MODE stereoMode);
FBCAPTURE_STATUS setPreviewCaptureSettings(int width, int height, int frameRate, bool is360, bool verticalFlip, bool horizontalFlip);
FBCAPTURE_STATUS setScreenshotSettings(int width, int height, const TCHAR* fullsavePath, bool is360, bool verticalFlip, bool horizontalFlip);
FBCAPTURE_STATUS setCameraOverlaySettings(float widthPercentage, uint32_t viewPortTopLeftX, uint32_t viewPortTopLeftY);
FBCAPTURE_STATUS enumerateMicDevices();
size_t getMicDevicesCount();
const char *getMicDeviceName(uint32_t index);
FBCAPTURE_STATUS setMicDevice(uint32_t index);
FBCAPTURE_STATUS unsetMicDevice();
FBCAPTURE_STATUS setMicEnabledDuringCapture(bool enabled);
FBCAPTURE_STATUS setAudioEnabledDuringCapture(bool enabled);
FBCAPTURE_STATUS enumerateCameraDevices();
size_t getCameraDevicesCount();
const char *getCameraDeviceName(uint32_t index);
FBCAPTURE_STATUS setCameraDevice(uint32_t index);
FBCAPTURE_STATUS unsetCameraDevice();
FBCAPTURE_STATUS setCameraEnabledDuringCapture(bool enabled);
FBCAPTURE_STATUS setMicAndAudioRenderDeviceByVRDeviceType(VRDeviceType vrDevice);
FBCAPTURE_STATUS startCapture(FBCaptureType type);
FBCAPTURE_STATUS startScreenshot(FBCaptureType type);
FBCAPTURE_STATUS captureTexture(void* texturePtr);
FBCAPTURE_STATUS previewCapture(void* texturePtr);
FBCAPTURE_STATUS previewCamera(void* texturePtr);
FBCAPTURE_STATUS saveScreenShot(void* texturePtr);
FBCAPTURE_STATUS getCaptureStatus();
FBCAPTURE_STATUS getScreenshotStatus();
GRAPHICS_CARD checkGPUManufacturer();
FBCAPTURE_STATUS sendAudioSample(float* audioRawData, uint32_t channel, uint32_t sampleRate, uint32_t bufferSize);
void setAudioRawDataByClient(bool enabled);
void stopCapture();
void shutdown();
private:
bool isCameraDevicesInitialized();
FBCAPTURE_STATUS initializeCameraDevices();
bool isMicDevicesInitialized();
FBCAPTURE_STATUS initializeMicDevices();
bool initialized_ = {};
ScopedCOMPtr<ID3D11Device> pDevice_ = {};
ID3D11Device* pClientDevice_ = {};
ScopedCOMPtr<ID3D11DeviceContext> pContext_ = {};
std::unique_ptr<TextureRender> pTextureFormatConversion_ = {};
std::unique_ptr<TextureRender> pPreviewTextureFormatConversion_ = {};
std::unique_ptr<TextureRender> pScreenShotTextureFormatConversion_ = {};
std::unique_ptr<IGraphicsDeviceCapture> pGraphicsDeviceCapture_ = {};
std::unique_ptr<IGraphicsDeviceCapture> pGraphicsDeviceScreenshot_ = {};
ScopedCOMPtr<ID3D11Texture2D> pEncodingTexture_ = {};
ScopedCOMPtr<ID3D11Texture2D> pPreviewTexture_ = {};
ScopedCOMPtr<ID3D11Texture2D> pScreenshotTexture_ = {};
std::unique_ptr<LiveCaptureSettings> pLiveCaptureSettings_ = {};
std::unique_ptr<VodCaptureSettings> pVodCaptureSettings_ = {};
std::unique_ptr<PreviewCaptureSettings> pPreviewCaptureSettings_ = {};
std::unique_ptr<ScreenshotSettings> pScreenshotSettings_ = {};
std::unique_ptr<MicSettings> pMicSettings_ = {};
std::unique_ptr<MicDevices> pMicDevices_ = {};
std::unique_ptr<CameraSettings> pCameraSettings_ = {};
std::unique_ptr<CameraOverlaySettings> pCameraOverlaySettings_ = {};
std::unique_ptr<CameraDevices> pCameraDevices_ = {};
std::unique_ptr<CameraDeviceManager> pCameraDeviceManager_ = {};
std::unique_ptr<CameraOverlay> pCameraOverlay_ = {};
std::unique_ptr<EncoderMain> pEncoder_ = {};
// TODO remove/replace with AudioRenderSettings
bool vrDeviceRequested_ = {};
bool audioEnabledDuringCapture_ = {};
VRDeviceType vrDevice_ = VRDeviceType::NONE;
// Runtime controls
FBCAPTURE_STATUS initializeSystemIfNeeded();
bool activateCameraDevice(std::lock_guard<std::mutex> &);
bool deactivateCameraDevice(std::lock_guard<std::mutex> &);
bool doesCameraReaderExist();
bool isCameraReaderInitialized();
bool isCameraReaderTextureAvailable();
void doCameraOverlay(bool fullQuad);
void doEncodeTextureRender(bool onlyCamera);
FBCAPTURE_STATUS doPreview(void* texturePtr, bool onlyCamera);
FBCAPTURE_STATUS doPreviewTextureRender(HANDLE* sharedHandle);
FBCAPTURE_STATUS doAudioCapture();
void doEncoderStopRoutine();
void captureFailed(FBCAPTURE_STATUS reason);
void screenshotFailed(FBCAPTURE_STATUS reason);
uint32_t getCaptureWidth(FBCaptureType type);
uint32_t getCaptureHeight(FBCaptureType type);
float getCaptureEncodeCycle(FBCaptureType type);
bool getCaptureNeedsVerticalFlip(FBCaptureType type);
bool getCaptureNeedsHorizontalFlip(FBCaptureType type);
bool getCaptureIs360(FBCaptureType type);
FBCAPTURE_STATUS checkOSCapability();
void muxThreadRun();
void audioThreadRun();
void encodeThreadRun();
void cameraThreadRun();
void stopRoutineThreadRun();
void screenshotThreadRun();
void screenshotThreadManagerRun();
std::thread *muxThread_ = {};
std::thread *audioThread_ = {};
std::thread *encodeThread_ = {};
std::thread *cameraThread_ = {};
std::thread *stopRoutineThread_ = {};
std::thread *screenshotThread_ = {};
std::thread *screenshotThreadManager_ = {};
std::mutex audioCaptureMutex_ = {};
std::mutex flushMutex_ = {};
std::mutex micModificationMutex_ = {};
std::mutex cameraModificationMutex_ = {};
std::mutex stopRoutineMutex_ = {};
std::atomic<bool> continueCapture_ = {};
std::atomic<bool> captureFailed_ = {};
std::atomic<bool> screenshotFailed_ = {};
FBCAPTURE_STATUS captureFailureReason_ = FBCAPTURE_STATUS_OK;
std::mutex captureFailedMutex_ = {};
std::mutex screenshotFailedMutex_ = {};
std::atomic<bool> encoding_ = {};
std::atomic<bool> flush_ = {};
std::atomic<bool> captureInProgress_ = {};
std::atomic<bool> stopRequested_ = {};
std::atomic<bool> screenshotInProgress_ = {};
std::atomic<bool> terminateScreenshotThread_ = {};
FBCaptureType captureInProgressType_ = {};
bool captureTextureReceieved_ = {};
bool screenshotTextureReceieved_ = {};
typedef BOOL(WINAPI *LPFN_ISWOW64PROCESS) (HANDLE, PBOOL);
LPFN_ISWOW64PROCESS fnIsWow64Process;
const int kResolutionAlignment = 16;
};
}
}