Skip to content

Commit fcb323a

Browse files
committed
Fix crashes
1 parent a1eca66 commit fcb323a

File tree

3 files changed

+72
-71
lines changed

3 files changed

+72
-71
lines changed

codec/av_to_d3d.cpp

+5-2
Original file line numberDiff line numberDiff line change
@@ -370,8 +370,11 @@ std::optional<QString> AvToDx::processFrame() {
370370
auto spoutDevice = spoutOutput.GetDX11Device();
371371
auto spoutContext = spoutOutput.GetDX11Context();
372372
auto spoutTex = bgra->getSharedTargetTexture(spoutDevice, spoutContext);
373-
spoutOutput.SendTexture(spoutTex);
374-
spoutTex->Release();
373+
374+
if (spoutTex != nullptr) {
375+
spoutOutput.SendTexture(spoutTex);
376+
spoutTex->Release();
377+
}
375378

376379
fps.add(t.nsecsElapsed());
377380

codec/nv12_to_bgra.cpp

+66-69
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ typedef struct _VERTEX {
2222

2323
// Vertices for drawing whole texture
2424
static VERTEX Vertices[NUMVERTICES] =
25-
{
26-
{XMFLOAT3(-1.0f, -1.0f, 0), XMFLOAT2(0.0f, 1.0f)},
27-
{XMFLOAT3(-1.0f, 1.0f, 0), XMFLOAT2(0.0f, 0.0f)},
28-
{XMFLOAT3(1.0f, -1.0f, 0), XMFLOAT2(1.0f, 1.0f)},
29-
{XMFLOAT3(1.0f, -1.0f, 0), XMFLOAT2(1.0f, 1.0f)},
30-
{XMFLOAT3(-1.0f, 1.0f, 0), XMFLOAT2(0.0f, 0.0f)},
31-
{XMFLOAT3(1.0f, 1.0f, 0), XMFLOAT2(1.0f, 0.0f)},
32-
};
25+
{
26+
{XMFLOAT3(-1.0f, -1.0f, 0), XMFLOAT2(0.0f, 1.0f)},
27+
{XMFLOAT3(-1.0f, 1.0f, 0), XMFLOAT2(0.0f, 0.0f)},
28+
{XMFLOAT3(1.0f, -1.0f, 0), XMFLOAT2(1.0f, 1.0f)},
29+
{XMFLOAT3(1.0f, -1.0f, 0), XMFLOAT2(1.0f, 1.0f)},
30+
{XMFLOAT3(-1.0f, 1.0f, 0), XMFLOAT2(0.0f, 0.0f)},
31+
{XMFLOAT3(1.0f, 1.0f, 0), XMFLOAT2(1.0f, 0.0f)},
32+
};
3333

3434
static FLOAT blendFactor[4] = {0.f, 0.f, 0.f, 0.f};
3535

@@ -65,7 +65,7 @@ Nv12ToBgra::~Nv12ToBgra() {
6565
}
6666

6767
bool Nv12ToBgra::compileShader() {
68-
ID3DBlob* errs;
68+
ID3DBlob *errs;
6969

7070
QFile f1(":/shader/nv12bgra_vertex.hlsl");
7171
f1.open(QIODevice::ReadOnly);
@@ -84,7 +84,7 @@ bool Nv12ToBgra::compileShader() {
8484
"PS", "ps_5_0", 0, 0, _pixel_shader.GetAddressOf(), &errs);
8585
if (FAILED(hr)) {
8686
qCritical() << "failed compiling nv12bgra pixel shader";
87-
auto e = std::string((char*)errs->GetBufferPointer(), errs->GetBufferSize());
87+
auto e = std::string((char *) errs->GetBufferPointer(), errs->GetBufferSize());
8888
qCritical("%s", e.c_str());
8989
return false;
9090
}
@@ -110,7 +110,7 @@ void Nv12ToBgra::releaseSharedSurf() {
110110
this->_height = 0;
111111
}
112112

113-
ID3D11Device* Nv12ToBgra::getDevice() {
113+
ID3D11Device *Nv12ToBgra::getDevice() {
114114
_d3d11_device->AddRef();
115115
return _d3d11_device.Get();
116116
}
@@ -124,35 +124,35 @@ bool Nv12ToBgra::init() {
124124

125125
// Driver types supported
126126
D3D_DRIVER_TYPE DriverTypes[] =
127-
{
128-
D3D_DRIVER_TYPE_UNKNOWN,
129-
//D3D_DRIVER_TYPE_HARDWARE,
130-
//D3D_DRIVER_TYPE_WARP,
131-
//D3D_DRIVER_TYPE_REFERENCE,
132-
};
127+
{
128+
D3D_DRIVER_TYPE_UNKNOWN,
129+
//D3D_DRIVER_TYPE_HARDWARE,
130+
//D3D_DRIVER_TYPE_WARP,
131+
//D3D_DRIVER_TYPE_REFERENCE,
132+
};
133133
UINT NumDriverTypes = ARRAYSIZE(DriverTypes);
134134

135135
// Feature levels supported
136136
D3D_FEATURE_LEVEL FeatureLevels[] =
137-
{
138-
D3D_FEATURE_LEVEL_11_0,
139-
};
137+
{
138+
D3D_FEATURE_LEVEL_11_0,
139+
};
140140
UINT NumFeatureLevels = ARRAYSIZE(FeatureLevels);
141141
D3D_FEATURE_LEVEL FeatureLevel;
142142
// This flag adds support for surfaces with a different color channel ordering
143143
// than the default. It is required for compatibility with Direct2D.
144144
UINT creationFlags =
145-
D3D11_CREATE_DEVICE_SINGLETHREADED |
146-
D3D11_CREATE_DEVICE_BGRA_SUPPORT;
145+
D3D11_CREATE_DEVICE_SINGLETHREADED |
146+
D3D11_CREATE_DEVICE_BGRA_SUPPORT;
147147

148148
if (IsDebuggerPresent() && DX_DEBUG_LAYER) {
149149
creationFlags |= D3D11_CREATE_DEVICE_DEBUG;
150150
}
151151

152152
// We need dxgi to share texture
153-
IDXGIFactory2* pDXGIFactory;
154-
IDXGIAdapter* pAdapter = NULL;
155-
hr = CreateDXGIFactory(IID_IDXGIFactory2, (void**)&pDXGIFactory);
153+
IDXGIFactory2 *pDXGIFactory;
154+
IDXGIAdapter *pAdapter = NULL;
155+
hr = CreateDXGIFactory(IID_IDXGIFactory2, (void **) &pDXGIFactory);
156156
if (FAILED(hr))
157157
return false;
158158

@@ -202,14 +202,14 @@ bool Nv12ToBgra::init() {
202202
qDebug() << "nv12bgra create vertex shader";
203203

204204
constexpr std::array<D3D11_INPUT_ELEMENT_DESC, 2> Layout =
205-
{
206-
{
207-
// 3D 32bit float vector
208-
{"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0},
209-
// 2D 32bit float vector
210-
{"TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0},
211-
}
212-
};
205+
{
206+
{
207+
// 3D 32bit float vector
208+
{"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0},
209+
// 2D 32bit float vector
210+
{"TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0},
211+
}
212+
};
213213

214214
hr = this->_d3d11_device->CreateInputLayout(Layout.data(), Layout.size(),
215215
_vertex_shader->GetBufferPointer(), _vertex_shader->GetBufferSize(),
@@ -268,9 +268,9 @@ void Nv12ToBgra::resetDeviceContext() {
268268
qDebug() << "nv12bgra set context params";
269269

270270
//SharedSurf
271-
std::array<ID3D11ShaderResourceView*, 2> const textureViews = {
272-
this->_luminanceView.Get(),
273-
this->_chrominanceView.Get()
271+
std::array<ID3D11ShaderResourceView *, 2> const textureViews = {
272+
this->_luminanceView.Get(),
273+
this->_chrominanceView.Get()
274274
};
275275
this->_d3d11_deviceCtx->PSSetShaderResources(0, textureViews.size(), textureViews.data());
276276
this->_d3d11_deviceCtx->OMSetRenderTargets(1, this->_renderTargetView.GetAddressOf(), nullptr);
@@ -287,9 +287,9 @@ void Nv12ToBgra::resetDeviceContext() {
287287
this->_d3d11_deviceCtx->RSSetViewports(1, &VP);
288288
//this->_d3d11_deviceCtx->Dispatch(8, 8, 1);
289289
this->_d3d11_deviceCtx->Dispatch(
290-
(UINT)ceil(_width * 1.0 / 8),
291-
(UINT)ceil(_height * 2 * 1.0 / 8),
292-
1);
290+
(UINT) ceil(_width * 1.0 / 8),
291+
(UINT) ceil(_height * 2 * 1.0 / 8),
292+
1);
293293

294294
qDebug() << "nv12bgra set context viewport";
295295
}
@@ -323,8 +323,8 @@ bool Nv12ToBgra::createSharedSurf() {
323323

324324
//
325325
D3D11_SHADER_RESOURCE_VIEW_DESC const luminancePlaneDesc
326-
= CD3D11_SHADER_RESOURCE_VIEW_DESC(this->_texture_nv12.Get(), D3D11_SRV_DIMENSION_TEXTURE2D,
327-
DXGI_FORMAT_R8_UNORM);
326+
= CD3D11_SHADER_RESOURCE_VIEW_DESC(this->_texture_nv12.Get(), D3D11_SRV_DIMENSION_TEXTURE2D,
327+
DXGI_FORMAT_R8_UNORM);
328328
hr = this->_d3d11_device->CreateShaderResourceView(this->_texture_nv12.Get(), &luminancePlaneDesc,
329329
this->_luminanceView.GetAddressOf());
330330
if (FAILED(hr))
@@ -334,8 +334,8 @@ bool Nv12ToBgra::createSharedSurf() {
334334

335335
//
336336
D3D11_SHADER_RESOURCE_VIEW_DESC const chrominancePlaneDesc
337-
= CD3D11_SHADER_RESOURCE_VIEW_DESC(this->_texture_nv12.Get(), D3D11_SRV_DIMENSION_TEXTURE2D,
338-
DXGI_FORMAT_R8G8_UNORM);
337+
= CD3D11_SHADER_RESOURCE_VIEW_DESC(this->_texture_nv12.Get(), D3D11_SRV_DIMENSION_TEXTURE2D,
338+
DXGI_FORMAT_R8G8_UNORM);
339339
hr = this->_d3d11_device->CreateShaderResourceView(this->_texture_nv12.Get(), &chrominancePlaneDesc,
340340
this->_chrominanceView.GetAddressOf());
341341
if (FAILED(hr))
@@ -373,8 +373,8 @@ bool Nv12ToBgra::createSharedSurf() {
373373

374374
qDebug() << "nv12bgra create texture copy bgra";
375375

376-
IDXGIResource* pDXGIResource = NULL;
377-
_texture_rgba_copy->QueryInterface(__uuidof(IDXGIResource), (LPVOID*)&pDXGIResource);
376+
IDXGIResource *pDXGIResource = NULL;
377+
_texture_rgba_copy->QueryInterface(__uuidof(IDXGIResource), (LPVOID *) &pDXGIResource);
378378
pDXGIResource->GetSharedHandle(&_texture_rgba_copy_shared);
379379
pDXGIResource->Release();
380380
if (!_texture_rgba_copy_shared) {
@@ -400,7 +400,7 @@ bool Nv12ToBgra::createSharedSurf() {
400400
return true;
401401
}
402402

403-
bool Nv12ToBgra::nv12ToBgra(AVFrame* f) {
403+
bool Nv12ToBgra::nv12ToBgra(AVFrame *f) {
404404
HRESULT hr{0};
405405

406406
if (!_inited) {
@@ -414,14 +414,14 @@ bool Nv12ToBgra::nv12ToBgra(AVFrame* f) {
414414

415415
D3D11_BOX srcBox = {0, 0, 0, _width, _height * 2, 1};
416416

417-
ID3D11Texture2D* textureRgb = (ID3D11Texture2D*)f->data[0];
418-
const int textureRgbIndex = (int)f->data[1];
417+
ID3D11Texture2D *textureRgb = (ID3D11Texture2D *) f->data[0];
418+
const int textureRgbIndex = (int) f->data[1];
419419
//qDebug() << "copy rgb" << textureRgb << textureRgbIndex;
420420

421421
//bind/copy ffmpeg hw texture -> local d3d11 texture
422422
this->_d3d11_deviceCtx->CopySubresourceRegion(
423-
this->_texture_nv12.Get(), 0, 0, 0, 0,
424-
textureRgb, textureRgbIndex, &srcBox
423+
this->_texture_nv12.Get(), 0, 0, 0, 0,
424+
textureRgb, textureRgbIndex, &srcBox
425425
);
426426

427427
//saveTextureToFile(_d3d11_deviceCtx.Get(), _texture_nv12.Get(), "./nv12_bgra_input.png");
@@ -435,7 +435,6 @@ bool Nv12ToBgra::nv12ToBgra(AVFrame* f) {
435435

436436
lock.lock();
437437

438-
439438
//qDebug() << "copy to buffer";
440439
this->_d3d11_deviceCtx->CopyResource(this->_texture_rgba_copy.Get(), this->_texture_rgba_target.Get());
441440
this->_d3d11_deviceCtx->Flush();
@@ -446,40 +445,39 @@ bool Nv12ToBgra::nv12ToBgra(AVFrame* f) {
446445
return true;
447446
}
448447

449-
bool Nv12ToBgra::copyTo(ID3D11Device* dev, ID3D11DeviceContext* ctx, ID3D11Texture2D* dest) {
448+
bool Nv12ToBgra::copyTo(ID3D11Device *dev, ID3D11DeviceContext *ctx, ID3D11Texture2D *dest) {
450449
if (!_inited)
451450
return false;
452451

453452
//qDebug() << "copy from buffer";
454453

455454
lock.lock();
456455

457-
ID3D11Texture2D* src;
458-
dev->OpenSharedResource(_texture_rgba_copy_shared, __uuidof(ID3D11Texture2D), (LPVOID*)&src);
459-
if (src == nullptr)
460-
return false;
461-
462-
D3D11_BOX box = {0, 0, 0, _width, _height, 1};
463-
ctx->CopySubresourceRegion(dest, 0, 0, 0, 0, src, 0, &box);
464-
ctx->Flush();
465-
466-
src->Release();
456+
ID3D11Texture2D *src;
457+
dev->OpenSharedResource(_texture_rgba_copy_shared, __uuidof(ID3D11Texture2D), (LPVOID *) &src);
458+
if (src != nullptr) {
459+
D3D11_BOX box = {0, 0, 0, _width, _height, 1};
460+
ctx->CopySubresourceRegion(dest, 0, 0, 0, 0, src, 0, &box);
461+
ctx->Flush();
462+
src->Release();
463+
}
467464

468465
lock.unlock();
469466

470467
return true;
471468
}
472469

473-
ID3D11Texture2D* Nv12ToBgra::getSharedTargetTexture(ID3D11Device* dev, ID3D11DeviceContext* ctx) {
470+
ID3D11Texture2D *Nv12ToBgra::getSharedTargetTexture(ID3D11Device *dev, ID3D11DeviceContext *ctx) {
474471
if (!_inited)
475472
return nullptr;
476473

474+
if (!_texture_rgba_copy_shared)
475+
return nullptr;
476+
477477
lock.lock();
478478

479-
ID3D11Texture2D* src;
480-
dev->OpenSharedResource(_texture_rgba_copy_shared, __uuidof(ID3D11Texture2D), (LPVOID*)&src);
481-
if (src == nullptr)
482-
return nullptr;
479+
ID3D11Texture2D *src;
480+
dev->OpenSharedResource(_texture_rgba_copy_shared, __uuidof(ID3D11Texture2D), (LPVOID *) &src);
483481

484482
lock.unlock();
485483

@@ -495,8 +493,7 @@ ComPtr<ID3D11Texture2D> DxFrameBuffer::getFree() {
495493
if (!free.empty()) {
496494
ret = free.front();
497495
free.pop();
498-
}
499-
else if (!queue.empty()) {
496+
} else if (!queue.empty()) {
500497
ret = queue.front();
501498
queue.pop();
502499
}

ui/windows/collabroom.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,7 @@ void CollabRoom::roomInfoSucceed(const vts::server::RspRoomInfo &info) {
403403
frameSendThread->start();
404404

405405
if (!isServer) {
406+
ScopedQMutex _(&peersLock);
406407
serverPeer = std::make_unique<Peer>(this, QString::fromStdString(info.hostpeerid()));
407408
serverPeer->startClient();
408409
}

0 commit comments

Comments
 (0)