I have been trying to make an application that utilizes the desktop duplication api, but having no experience with directx it is turning out to be quite a challenge. Everything seems to work until I call output1->DuplicateOutput()at which point it returns E_NOINTERFACE. This error is not defined in the msdn documentation so I am having trouble diagnosing the problem. I think that this code should work, but I must be missing something.
#include <windows.h>
#include <d3d12.h>
#include <dxgi1_5.h>
int main()
{
HRESULT hr;
ID3D12Debug *debug;
hr = D3D12GetDebugInterface(IID_PPV_ARGS(&debug));
debug->EnableDebugLayer();
IDXGIFactory1 *factory;
hr = CreateDXGIFactory1(IID_PPV_ARGS(&factory));
IDXGIAdapter1 *adapter;
hr = factory->EnumAdapters1(0, &adapter);
factory->Release();
IDXGIOutput *junkput;
hr = adapter->EnumOutputs(0, &junkput);
IDXGIOutput1 *output1;
hr = junkput->QueryInterface(IID_PPV_ARGS(&output1));
junkput->Release();
ID3D12Device *device;
hr = D3D12CreateDevice(adapter, D3D_FEATURE_LEVEL_11_0, IID_PPV_ARGS(&device));
IDXGIOutputDuplication *dupl;
hr = output1->DuplicateOutput(device, &dupl);
return 0;
}
In my debug window I notice that I am getting two _com_errors when I call output1->DuplicateOutput.
Update:
I narrowed the problem down to the fact that I am using a ID3D12Device instead of an ID3D11Device. As exemplified by the fact that this code works:
ID3D11Device *device;
D3D_FEATURE_LEVEL reallevel;
ID3D11DeviceContext *context;
hr = D3D11CreateDevice(adapter, D3D_DRIVER_TYPE_UNKNOWN, nullptr, NULL, featurelevels, ARRAYSIZE(featurelevels), D3D11_SDK_VERSION, &device, &reallevel, &context);
IDXGIOutputDuplication *dupl;
hr = output1->DuplicateOutput(device, &dupl);
What I don't understand is why that is a problem. Isn't the desktop duplication api compatible with directx 12?
DXGI DuplicateOutput does not support DirectX 12 devices yet. As you have no experience using DirectX, you should be using DirectX 11 anyhow. DirectX 12 is an API designed for graphics experts who are assumed to already be deeply familiar with DirectX 11.
Note that D3D11On12CreateDevice devices should work with DXGI DuplicateOutput, but I've not tried it myself.
Related
I'm trying to rewrite some code that calls a local COM Server from C# to C++. The C# code works without issue. The key part is:
Guid lr_FactoryGuid = Guid.Parse("AE7CFA4B-985A-4F76-8CC6-2011649FC8A9");
Guid lr_FactoryClass = Guid.Parse("1CA0D073-4ABB-4D06-B318-BFFDE38E4903");
IntPtr lk_FactoryPtr = new IntPtr();
CoGetClassObject(
ref lr_FactoryClass,
4,
new IntPtr(),
ref lr_FactoryGuid,
out lk_FactoryPtr);
if (lk_FactoryPtr == IntPtr.Zero)
{
MessageBox.Show("lk_FactoryPtr == IntPtr.Zero");
return false;
}
I've tried to rewrite this into C++ and I can't get any further than here, the error is give as "No such interface supported":
HRESULT hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
CLSID clsid;
HRESULT hr = CLSIDFromString(L"{1CA0D073-4ABB-4D06-B318-BFFDE38E4903}", &clsid);
CLSID iid;
hr = CLSIDFromString(L"{AE7CFA4B-985A-4F76-8CC6-2011649FC8A9}", &iid);
void* pIFace;
hr = CoCreateInstance(clsid, NULL, CLSCTX_LOCAL_SERVER, iid, &pIFace);
if (!SUCCEEDED(hr))
{
_com_error err(hr);
LPCTSTR errMsg = err.ErrorMessage();
MessageBox(NULL, errMsg, L"SiteKiosk demo", MB_ICONEXCLAMATION | MB_OK);
}
There is a .tlb file that I used to generate the interop DLL for C# and to import into the C++, however it's currently commented out of the C++ in an attempt to keep the code smaller and I still get this error from CoCreateInstance.
The COM application I'm calling is a 32 bit app, so both my C# and C++ clients applications are also 32 bit. Both of the clients are Windows Console applications.
Is there anything else I need to set/do to get the C++ working?
The suggestion by Hans solved the problem, I used CoGetClassObject and the rest of the code then clicked into place.
I am doing a windows based app where I use a camera. I want to allow the user to change camera settings (VfwCaptureDialog_Source) as it is common in skype or amcap application. For now, I found out that the dialog is from vfw and it can be activated from dshow api. When I do it from AmCap, it is working. Can you help me to get this code working for given webcam (multiple camera support is required)? I use Qt and OpenCV while working with camera. In opencv just camera index is used to select proper camera. The index is from Qt where can I get nice list of camera names. The error I get from this code is 1170 : "The property set specified does not exist on the object"
//libs -lDxva2 -lstrmiids -lvfw32 -lole32 -loleaut32
#include <Windows.h>
#include "strmif.h"
#include "dshow.h"
#include "Vfw.h"
...
HRESULT hr ;
IGraphBuilder* graph= nullptr;
hr = CoCreateInstance( CLSID_FilterGraph, 0, CLSCTX_INPROC,IID_IGraphBuilder, (void **)&graph );
IMediaControl* ctrl = nullptr;
hr = graph->QueryInterface( IID_IMediaControl, (void **)&ctrl );
IMediaEventEx* mediaEvent=nullptr;
hr = graph->QueryInterface(IID_IMediaEvent, (LPVOID *) &mediaEvent);
ICreateDevEnum* devs = nullptr;
hr = CoCreateInstance (CLSID_SystemDeviceEnum, nullptr, CLSCTX_INPROC, IID_ICreateDevEnum, (void **) &devs);
IEnumMoniker* cams = nullptr;
hr = devs?devs->CreateClassEnumerator (CLSID_VideoInputDeviceCategory, &cams, 0):0;
IMoniker* mon = nullptr;
hr = cams?cams->Next (1, &mon, nullptr):0;
IBaseFilter* cam = nullptr;
hr = mon?mon->BindToObject(nullptr,nullptr,IID_IBaseFilter, (void**)&cam):0;
IEnumPins* pins = nullptr;
hr = cam?cam->EnumPins(&pins):0;
IPin* cap = nullptr;
hr = pins?pins->Next(1,&cap, nullptr):0;
IAMVfwCaptureDialogs *pVfw = nullptr;
hr = cap->QueryInterface(IID_IAMVfwCaptureDialogs, (void**)&pVfw);
if (SUCCEEDED(hr))
{
// Check if the device supports this dialog box.
if (S_OK == pVfw->HasDialog(VfwCaptureDialog_Source))
{
// Show the dialog box.
hr = pVfw->ShowDialog(VfwCaptureDialog_Source, HWND(this->winId()));
}
}
else
{
error("cap->QueryInterface");
}
First of all, you skipped a really important part. In your Qt/OpenCV application what is the API used and what exactly you have for given web camera. If it is Video for Windows, then you should look into VFW API on dialog interface. If it is DirectShow then you are basically not interested in VFW dialogs.
Presumably you interact with cameras via DirectShow (well, it does not make much sense to use VFW, esp. for multiple cameras). Then I doubt that AMCap uses exactly the code path you mentioned in the question. Note AMCap source comment:
// we use this interface to bring up the 3 dialogs
// NOTE: Only the VfW capture filter supports this. This app only brings
// up dialogs for legacy VfW capture drivers, since only those have dialogs
hr = gcap.pBuilder->FindInterface(&PIN_CATEGORY_CAPTURE,
&MEDIATYPE_Video, gcap.pVCap,
IID_IAMVfwCaptureDialogs, (void **)&gcap.pDlg);
With DirectShow you would typically pop up configuration interface with ISpecifyPropertyPages and OleCreatePropertyFrame. AMCap has relevant code for this as well:
else if(id - MENU_DIALOG0 == gcap.iVCapDialogPos)
{
ISpecifyPropertyPages *pSpec;
CAUUID cauuid;
hr = gcap.pVCap->QueryInterface(IID_ISpecifyPropertyPages,
(void **)&pSpec);
if(hr == S_OK)
{
hr = pSpec->GetPages(&cauuid);
hr = OleCreatePropertyFrame(ghwndApp, 30, 30, NULL, 1,
(IUnknown **)&gcap.pVCap, cauuid.cElems,
(GUID *)cauuid.pElems, 0, 0, NULL);
CoTaskMemFree(cauuid.pElems);
pSpec->Release();
}
}
Displaying a Filter's Property Pages on MSDN should be even of more help for you (you already have IBaseFilter interface pointer in your code snippet in the question).
Excuse me for my low english skills, I hope you will edit my post if you got enough karma.
I am trying to initialize ShaderResourceView from the Win32 Program Resource. I am using Visual Studio 2015.
Next code fails:
HMODULE hMod = GetModuleHandleW(0);
HRSRC hRes = FindResourceW(hMod, (const wchar*)MAKEINTRESOURCE(IDB_BITMAP1), RT_BITMAP);
HGLOBAL hGlob = LoadResource(hMod, hRes);
DWORD dwImageSize = SizeofResource(hMod, hRes);
byte *lpResourceData = (byte*)LockResource(hGlob);
HRESULT hr = D3DX11CreateShaderResourceViewFromMemory(lpDevice, lpResourceData, dwImageSize, NULL, NULL, &lpNodeTexture, NULL);
if(FAILED(hr)) {
Int3();
}
FreeResource(hGlob);
All of these WinApi functions are succeeded and I really have this resource in my program, but hresult always returns E_FAIL. Can you pls give me an advice. Thank you.
EDITED:
I forgot to add next code as well to the first post:
D3DX11CreateShaderResourceViewFromFileW(lpDevice, L"Node.bmp", 0, 0, &texture, 0).
Its working for me, but I need to pack my textures into application. Can the problem be more difficult than creating D3D Device with Debug Layer flag? Thanks.
How do I from C or C++ use the MP3 decoder supposedly built in with Windows since Windows Media Player 6.1?
I want to play an mp3 file without having to depend on any other third party library such as for instance LAME.DLL.
I updated the question to better fit the answers I got, since I liked them a lot. Related question.
Sure. Like lots of other things in the Windows API, there's more than one way to go about playing .mp3 files. The "easiest" way to do this programmatically is using DirectShow. The MSDN docs even include a minimal code example on a page aptly called "How To Play a File" to get you started:
// Visual C++ example
#include <dshow.h>
#include <cstdio>
// For IID_IGraphBuilder, IID_IMediaControl, IID_IMediaEvent
#pragma comment(lib, "strmiids.lib")
// Obviously change this to point to a valid mp3 file.
const wchar_t* filePath = L"C:/example.mp3";
int main()
{
IGraphBuilder *pGraph = NULL;
IMediaControl *pControl = NULL;
IMediaEvent *pEvent = NULL;
// Initialize the COM library.
HRESULT hr = ::CoInitialize(NULL);
if (FAILED(hr))
{
::printf("ERROR - Could not initialize COM library");
return 0;
}
// Create the filter graph manager and query for interfaces.
hr = ::CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER,
IID_IGraphBuilder, (void **)&pGraph);
if (FAILED(hr))
{
::printf("ERROR - Could not create the Filter Graph Manager.");
return 0;
}
hr = pGraph->QueryInterface(IID_IMediaControl, (void **)&pControl);
hr = pGraph->QueryInterface(IID_IMediaEvent, (void **)&pEvent);
// Build the graph.
hr = pGraph->RenderFile(filePath, NULL);
if (SUCCEEDED(hr))
{
// Run the graph.
hr = pControl->Run();
if (SUCCEEDED(hr))
{
// Wait for completion.
long evCode;
pEvent->WaitForCompletion(INFINITE, &evCode);
// Note: Do not use INFINITE in a real application, because it
// can block indefinitely.
}
}
// Clean up in reverse order.
pEvent->Release();
pControl->Release();
pGraph->Release();
::CoUninitialize();
}
Make sure you read through the DirectShow documentation to get an idea of what's supposed to happen in a proper DirectShow application.
To "feed" media data into a graph, you need to implement a IAsyncReader. Fortunately, the Windows SDK includes a sample that implements an IAsyncReader called CAsyncReader. The sample reads a media file into a memory buffer then uses CAsyncReader to stream the data into the graph. This may be what you want. On my machine the sample is located in the folder C:\Program Files\Microsoft SDKs\Windows\v7.0\Samples\multimedia\directshow\filters\async.
You can control an audio channel (in order to make it play anything, including MP3) with mciSendString http://msdn.microsoft.com/en-us/library/ms709492%28VS.85%29.aspx
Here's an example (it's in C#, but it's basically the same principle):
http://social.msdn.microsoft.com/forums/en-US/Vsexpressvcs/thread/152f0149-a62a-446d-a205-91256da7845d
Here is the same principle in C:
http://www.daniweb.com/software-development/c/code/268167
I have created a custom allocator/presenter that works fine for playback of normal media files. However, when I use the following code to try to playback a DVD, it fails with a stack overflow exception.
vmr9_ap = new vmr9ap();
HMONITOR monitor = MonitorFromWindow(hwnd, NULL);
IGraphBuilder *graph;
IBaseFilter *filter;
IDvdGraphBuilder *builder;
CoCreateInstance(CLSID_DvdGraphBuilder, NULL, CLSCTX_INPROC_SERVER, IID_IDvdGraphBuilder, reinterpret_cast<void**>(&builder));
CoCreateInstance(::CLSID_VideoMixingRenderer9, NULL, CLSCTX_INPROC_SERVER, IID_IBaseFilter, reinterpret_cast<void**>(&filter));
builder->GetDvdInterface(IID_IVMRFilterConfig9, (void**)&vmr9_config);
vmr9_ap->Initialize(g_pd3dDevice, monitor, vmr9_config);
HRESULT hr = builder->RenderDvdVideoVolume(L"G:\\VIDEO_TS", AM_DVD_SWDEC_PREFER | AM_DVD_VMR9_ONLY, &status);
builder->GetFiltergraph(&graph);
IDvdControl2 *dvdControl;
builder->GetDvdInterface(::IID_IDvdControl2, (void**)&dvdControl);
graph->QueryInterface(::IID_IMediaControl, (void**)&control);
HRESULT h = control->Run();
The stack overflow happens immediately after the call to control->Run(). It's driving me nuts, as I'm sure I'm just forgetting something really really simple.
Thanks.
Your graph should look something like this. Make sure there aren't any buggy filters in your graph.
Because you are using a custom allocator, I would look there for the issue and set some breakpoints there. You code you pasted might be incomplete as I do not see you configure the VMR9 with the custom allocator, nor do I see it being added to the graph. I avoid using the DVDGraphBuilder as I had too difficult of a time getting it RenderVolume correctly with my VMR9+Allocator. I would build the graph a little more manually.
I have a custom allocator in my open source project, along w/ a DVD player. You can check that out for reference, though there is a lot of code noise in there due to me needing to hack a few things in there for WPF compatiblity. http://wpfmediakit.codeplex.com
What you are seeing should NOT be a DRM issue.
alt text http://img29.imageshack.us/img29/7798/capturelu.jpg
Could it be a form of DRM protection? Decoders in DVD graphs will typically try to prevent you building graphs that get access to the uncompressed data as you do here. Normally they do that by a cleaner method, such as refusing to connect to unauthorised renderers, but it's possible that this might be caused by something like that -- certainly there are mpeg-2 decoders which use deliberate crashes to prevent reverse engineering.
G
Thanks to the code Jeremiah Morrill pointed me to, I managed to get playback mostly working.
It works fine, as long as you don't try to resize the D3DImage that it's played in. Devil's in the details, I suppose.
Thanks to all the answers. DVD playback doesn't work with a debugger attached, which according to Google, is not DRM, but is an anti-reverse engineering measure. Could be particular to the DVD codec I'm using too.
extern "C" __declspec(dllexport) LPDIRECT3DSURFACE9 InitializeDvd(HWND hWnd)
{
CoInitialize(NULL);
IPin *dvdVideoOut;
IPin *vmr9VideoIn;
HRESULT hr = S_OK;
hr = CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER,
IID_IGraphBuilder, (void **)&graph);
if(graph)
{
hr = CoCreateInstance(CLSID_DVDNavigator, NULL, CLSCTX_INPROC_SERVER,
IID_IBaseFilter, (void **)&dvdNavigator);
if(dvdNavigator)
{
hr = graph->AddFilter(dvdNavigator, L"DVD Navigator");
if(SUCCEEDED(hr))
{
hr = CoCreateInstance(CLSID_VideoMixingRenderer9, NULL, CLSCTX_INPROC_SERVER,
IID_IBaseFilter, (void **)&vmr9);
if(vmr9)
{
hr = vmr9->QueryInterface(IID_IVMRFilterConfig9, reinterpret_cast<void**>(&p_fConfig));
p_Ap = new VMR9AllocatorPresenter();
p_Dh = new DeviceHandler();
p_device = p_Dh->Initialize(hWnd);
p_fConfig->SetRenderingMode(VMR9Mode_Renderless);
p_fConfig->SetNumberOfStreams(1);
p_Ap->Initialize(hWnd, p_device, p_fConfig);
if(SUCCEEDED(hr))
{
hr = graph->AddFilter(vmr9, L"Video Mixing Renderer 9");
if(p_fConfig)
{
dvdNavigator->FindPin(L"Video", &dvdVideoOut);
if(dvdVideoOut)
{
hr = graph->Render(dvdVideoOut);
}
hr = graph->QueryInterface(IID_IMediaControl, reinterpret_cast<void**>(&control));
if(control)
{
control->Run();
}
}
}
}
}
}
}
return p_Dh->g_surface9;
}