webcamoid / akvirtualcamera

akvirtualcamera, virtual camera for Mac and Windows
GNU General Public License v3.0
405 stars 52 forks source link

How do I use this in visual studio code #24

Closed KaiH-0 closed 1 year ago

KaiH-0 commented 3 years ago

I have Cmake and Cmake tools installed in visual studio code but I'm having problems trying to link it up with my project so I can use this. I have already built akvirtualcamera but not sure how I'm supposed to then use it in my project. This is an amazing project as I have been looking for one that works for Windows and Mac and would love to know how to use it. I know this is a noob question, I have only recently started using C++.

hipersayanX commented 3 years ago

If this is your first time playing with the vcam, you should first learn how to use and manage it from the command line tool, all the setup and streaming is made from there. For the easy path:

Then learn how to compile and install it, the vcam won't never work unless you properly install and setup it. Then just call the manager from C++ using ShellExecute, CreateProcess or similar functions, here is an example for streaming. And all you need to know is explained in the wiki.

hipersayanX commented 3 years ago

??? Did you deleted the comment? can you post it again?

KaiH-0 commented 3 years ago

Hi, the reason why I deleted the comment is because I think the error is caused by something I did, I’m going to try and fix it tomorrow, if it doesn’t work, I’ll comment here.

On Sun, Oct 3, 2021 at 01:50 hipersayanX @.***> wrote:

??? Did you deleted the comment? can you post it again?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/webcamoid/akvirtualcamera/issues/24#issuecomment-932841643, or unsubscribe https://github.com/notifications/unsubscribe-auth/AMATOV3SEZKKJ7CKP523KHDUE6SFPANCNFSM5FGNIIUA .

hipersayanX commented 3 years ago

Oh, ok. Just in case remember that you can turn on debugging and trace in which part the error was throw.

KaiH-0 commented 3 years ago

I managed to fix VirtualCamera not working in cmd but the problem now is, using your example of streaming has caused this error, this is a brand new project, just a copy and paste to test if it all works, The video output (AkVCamVideoDevice0) is the correct one as well. Not sure what is going wrong here.

Here is the error: [build] E:\Users\kaiha\OneDrive\Desktop\New folder - Copy\main.cpp(51,25): error C2734: 'cmd': 'const' object must be initialized if not 'extern' [E:\Users\kaiha\OneDrive\Desktop\New folder - Copy\build\opencv.vcxproj] [build] E:\Users\kaiha\OneDrive\Desktop\New folder - Copy\main.cpp(55,5): error C2664: 'int snprintf(char const ,const size_t,const char const ,...)': cannot convert argument 1 from 'const char [1024]' to 'char const ' [E:\Users\kaiha\OneDrive\Desktop\New folder - Copy\build\opencv.vcxproj] [build] E:\Users\kaiha\OneDrive\Desktop\New folder - Copy\main.cpp(55,14): message : Conversion loses qualifiers [E:\Users\kaiha\OneDrive\Desktop\New folder - Copy\build\opencv.vcxproj] [build] E:\Windows Kits\10\Include\10.0.19041.0\ucrt\stdio.h(1919,37): message : see declaration of 'snprintf' [E:\Users\kaiha\OneDrive\Desktop\New folder - Copy\build\opencv.vcxproj] [build] E:\Users\kaiha\OneDrive\Desktop\New folder - Copy\main.cpp(68,76): error C2027: use of undefined type 'main::SECURITY_ATTRIBUTES' [E:\Users\kaiha\OneDrive\Desktop\New folder - Copy\build\opencv.vcxproj] [build] E:\Users\kaiha\OneDrive\Desktop\New folder - Copy\main.cpp(68): message : see declaration of 'main::SECURITY_ATTRIBUTES' [E:\Users\kaiha\OneDrive\Desktop\New folder - Copy\build\opencv.vcxproj] [build] E:\Users\kaiha\OneDrive\Desktop\New folder - Copy\main.cpp(69,74): error C2027: use of undefined type 'main::SECURITY_ATTRIBUTES' [E:\Users\kaiha\OneDrive\Desktop\New folder - Copy\build\opencv.vcxproj] [build] E:\Users\kaiha\OneDrive\Desktop\New folder - Copy\main.cpp(68): message : see declaration of 'main::SECURITY_ATTRIBUTES' [E:\Users\kaiha\OneDrive\Desktop\New folder - Copy\build\opencv.vcxproj] [build] E:\Users\kaiha\OneDrive\Desktop\New folder - Copy\main.cpp(80,25): error C2079: 'startupInfo' uses undefined struct 'main::STARTUPINFOA' [E:\Users\kaiha\OneDrive\Desktop\New folder - Copy\build\opencv.vcxproj] [build] E:\Users\kaiha\OneDrive\Desktop\New folder - Copy\main.cpp(81,55): error C2027: use of undefined type 'main::STARTUPINFOA' [E:\Users\kaiha\OneDrive\Desktop\New folder - Copy\build\opencv.vcxproj] [build] E:\Users\kaiha\OneDrive\Desktop\New folder - Copy\main.cpp(80): message : see declaration of 'main::STARTUPINFOA' [E:\Users\kaiha\OneDrive\Desktop\New folder - Copy\build\opencv.vcxproj] [build] E:\Users\kaiha\OneDrive\Desktop\New folder - Copy\main.cpp(82,48): error C2027: use of undefined type 'main::STARTUPINFOA' [E:\Users\kaiha\OneDrive\Desktop\New folder - Copy\build\opencv.vcxproj] [build] E:\Users\kaiha\OneDrive\Desktop\New folder - Copy\main.cpp(80): message : see declaration of 'main::STARTUPINFOA' [E:\Users\kaiha\OneDrive\Desktop\New folder - Copy\build\opencv.vcxproj] [build] E:\Users\kaiha\OneDrive\Desktop\New folder - Copy\main.cpp(87,32): error C2079: 'procInfo' uses undefined struct 'main::PROCESS_INFORMATION' [E:\Users\kaiha\OneDrive\Desktop\New folder - Copy\build\opencv.vcxproj] [build] E:\Users\kaiha\OneDrive\Desktop\New folder - Copy\main.cpp(88,59): error C2027: use of undefined type 'main::PROCESS_INFORMATION' [E:\Users\kaiha\OneDrive\Desktop\New folder - Copy\build\opencv.vcxproj] [build] E:\Users\kaiha\OneDrive\Desktop\New folder - Copy\main.cpp(87): message : see declaration of 'main::PROCESS_INFORMATION' [E:\Users\kaiha\OneDrive\Desktop\New folder - Copy\build\opencv.vcxproj] [build] E:\Users\kaiha\OneDrive\Desktop\New folder - Copy\main.cpp(91,5): error C2664: 'BOOL CreateProcessA(LPCSTR,LPSTR,LPSECURITY_ATTRIBUTES,LPSECURITY_ATTRIBUTES,BOOL,DWORD,LPVOID,LPCSTR,LPSTARTUPINFOA,LPPROCESS_INFORMATION)': cannot convert argument 2 from 'const char [1024]' to 'LPSTR' [E:\Users\kaiha\OneDrive\Desktop\New folder - Copy\build\opencv.vcxproj] [build] E:\Users\kaiha\OneDrive\Desktop\New folder - Copy\main.cpp(92,20): message : Conversion loses qualifiers [E:\Users\kaiha\OneDrive\Desktop\New folder - Copy\build\opencv.vcxproj] [build] E:\Windows Kits\10\Include\10.0.19041.0\um\processthreadsapi.h(361,1): message : see declaration of 'CreateProcessA' [E:\Users\kaiha\OneDrive\Desktop\New folder - Copy\build\opencv.vcxproj] [build] E:\Users\kaiha\OneDrive\Desktop\New folder - Copy\main.cpp(104,39): error C2440: 'initializing': cannot convert from 'void ' to 'char ' [E:\Users\kaiha\OneDrive\Desktop\New folder - Copy\build\opencv.vcxproj] [build] E:\Users\kaiha\OneDrive\Desktop\New folder - Copy\main.cpp(104,18): message : Conversion from 'void' to pointer to non-'void' requires an explicit cast [E:\Users\kaiha\OneDrive\Desktop\New folder - Copy\build\opencv.vcxproj] [build] E:\Users\kaiha\OneDrive\Desktop\New folder - Copy\main.cpp(107,11): error C3861: 'time': identifier not found [E:\Users\kaiha\OneDrive\Desktop\New folder - Copy\build\opencv.vcxproj] [build] Build finished with exit code 1

Also how would I input a opencv frame into the cam.

hipersayanX commented 3 years ago

But those errors are related to your own code, I can't help you with that.

Also how would I input a opencv frame into the cam.

As I said before

Then just call the manager from C++ using ShellExecute, CreateProcess or similar functions, here is an example for streaming.

KaiH-0 commented 3 years ago

I made a whole new project and copy and paste the code from the example. All the code is from the example.

Here is a screenshot of the code: https://s9.gifyu.com/images/image8295f6ea1f71609a.png

hipersayanX commented 3 years ago

Oh ok, I understand now, you did copied the example code to main.c, I thought that the codes was from your project. I've fixed the code, try again.

KaiH-0 commented 3 years ago

Hi, I'm still getting some errors but a lot less: 'StreamProcess::pipeAttributes' uses undefined struct 'SECURITY_ATTRIBUTES' [E:\Users\kaiha\OneDrive\Desktop\New folder - Copy\build\opencv.vcxproj] 'StreamProcess::startupInfo' uses undefined struct 'STARTUPINFOA' [E:\Users\kaiha\OneDrive\Desktop\New folder - Copy\build\opencv.vcxproj] 'StreamProcess::procInfo' uses undefined struct 'PROCESS_INFORMATION' [E:\Users\kaiha\OneDrive\Desktop\New folder - Copy\build\opencv.vcxproj] use of undefined type 'SECURITY_ATTRIBUTES' [E:\Users\kaiha\OneDrive\Desktop\New folder - Copy\build\opencv.vcxproj] use of undefined type 'SECURITY_ATTRIBUTES' [E:\Users\kaiha\OneDrive\Desktop\New folder - Copy\build\opencv.vcxproj] 'BOOL CreatePipe(PHANDLE,PHANDLE,LPSECURITY_ATTRIBUTES,DWORD)': cannot convert argument 3 from 'int ' to 'LPSECURITY_ATTRIBUTES' [E:\Users\kaiha\OneDrive\Desktop\New folder - Copy\build\opencv.vcxproj] 'startupInfo' uses undefined struct 'STARTUPINFOA' [E:\Users\kaiha\OneDrive\Desktop\New folder - Copy\build\opencv.vcxproj] use of undefined type 'STARTUPINFOA' [E:\Users\kaiha\OneDrive\Desktop\New folder - Copy\build\opencv.vcxproj] 'procInfo' uses undefined struct 'PROCESS_INFORMATION' [E:\Users\kaiha\OneDrive\Desktop\New folder - Copy\build\opencv.vcxproj] use of undefined type 'PROCESS_INFORMATION' [E:\Users\kaiha\OneDrive\Desktop\New folder - Copy\build\opencv.vcxproj] 'BOOL CreateProcessA(LPCSTR,LPSTR,LPSECURITY_ATTRIBUTES,LPSECURITY_ATTRIBUTES,BOOL,DWORD,LPVOID,LPCSTR,LPSTARTUPINFOA,LPPROCESS_INFORMATION)': cannot convert argument 9 from 'int ' to 'LPSTARTUPINFOA' [E:\Users\kaiha\OneDrive\Desktop\New folder - Copy\build\opencv.vcxproj] 'initializing': cannot convert from 'void ' to 'char' [E:\Users\kaiha\OneDrive\Desktop\New folder - Copy\build\opencv.vcxproj] 'time': identifier not found [E:\Users\kaiha\OneDrive\Desktop\New folder - Copy\build\opencv.vcxproj] subscript requires array or pointer type [E:\Users\kaiha\OneDrive\Desktop\New folder - Copy\build\opencv.vcxproj] 'BOOL WriteFile(HANDLE,LPCVOID,DWORD,LPDWORD,LPOVERLAPPED)': cannot convert argument 2 from 'char' to 'LPCVOID' [E:\Users\kaiha\OneDrive\Desktop\New folder - Copy\build\opencv.vcxproj] 'void free(void )': cannot convert argument 1 from 'char' to 'void *' [E:\Users\kaiha\OneDrive\Desktop\New folder - Copy\build\opencv.vcxproj]

hipersayanX commented 3 years ago

Dis you copied to code as-is? because SECURITY_ATTRIBUTES is defined in windows.h, you can check it in Microsoft's official documentation.

KaiH-0 commented 3 years ago

Yeah I did, I'll take a look at the Microsoft documentation and managed to make it work, no more errors! But I'm not quite sure how I would put a opencv frame into it.

include

include

include

include

/* For the sake of simplicity, the program won't print anything to the terminal,

// We'll assume this is a valid akvcam output device.

define VIDEO_OUTPUT "AkVCamVideoDevice0"

// Send frames for about 30 seconds in a 30 FPS stream.

define FPS 30

define DURATION_SECONDS 30

define N_FRAMES (FPS * DURATION_SECONDS)

int main() { struct StreamProcess { HANDLE stdinReadPipe; HANDLE stdinWritePipe; SECURITY_ATTRIBUTES pipeAttributes; STARTUPINFOA startupInfo; PROCESS_INFORMATION procInfo; }; typedef struct _SECURITY_ATTRIBUTES { // sa DWORD nLength; LPVOID lpSecurityDescriptor; BOOL bInheritHandle; } SECURITY_ATTRIBUTES; // Set the parameters of the stream. char cmd[1024]; const char format[] = "RGB24"; int width = 640; int height = 480; memset(cmd, 0, 1024); snprintf(cmd, 1024, "AkVCamManager stream %s %s %d %d", VIDEO_OUTPUT, format, width, height);

// Get the handles to the standard input and standard output.
struct StreamProcess streamProc;
memset(&streamProc, 0, sizeof(struct StreamProcess));
streamProc.stdinReadPipe = NULL;
streamProc.stdinWritePipe = NULL;
memset(&streamProc.pipeAttributes, 0, sizeof(SECURITY_ATTRIBUTES));
streamProc.pipeAttributes.nLength = sizeof(SECURITY_ATTRIBUTES);
streamProc.pipeAttributes.bInheritHandle = true;
streamProc.pipeAttributes.lpSecurityDescriptor = NULL;
CreatePipe(&streamProc.stdinReadPipe,
           &streamProc.stdinWritePipe,
           &streamProc.pipeAttributes,
           0);
SetHandleInformation(streamProc.stdinWritePipe,
                     HANDLE_FLAG_INHERIT,
                     0);

STARTUPINFOA startupInfo;
memset(&startupInfo, 0, sizeof(STARTUPINFOA));
startupInfo.cb = sizeof(STARTUPINFOA);
startupInfo.hStdInput = streamProc.stdinReadPipe;
startupInfo.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
startupInfo.wShowWindow = SW_HIDE;

PROCESS_INFORMATION procInfo;
memset(&procInfo, 0, sizeof(PROCESS_INFORMATION));

// Start the stream.
CreateProcessA(NULL,
               cmd,
               NULL,
               NULL,
               true,
               0,
               NULL,
               NULL,
               &startupInfo,
               &procInfo);

// Allocate the frame buffer.
size_t buffer_size = 3 * width;
char *buffer = (char*)malloc(buffer_size);

// Generate some random noise frames.
srand(time(0));

for (int i = 0; i < N_FRAMES; i++) {
    // Write the frame line by line.
    for (int y = 0; y < height; y++) {
        for (size_t byte = 0; byte < buffer_size; byte++)
            buffer[byte] = rand() & 0xff;

        // Write the frame data to the buffer.
        DWORD bytesWritten = 0;
        WriteFile(streamProc.stdinWritePipe,
                  buffer,
                  DWORD(buffer_size),
                  &bytesWritten,
                  NULL);
    }

    Sleep(1000 / FPS);
}

// Release the frame buffer.
free(buffer);

// Close the standard input and standard output handles.
CloseHandle(streamProc.stdinWritePipe);
CloseHandle(streamProc.stdinReadPipe);

// Stop the stream.
WaitForSingleObject(streamProc.procInfo.hProcess, INFINITE);
CloseHandle(streamProc.procInfo.hProcess);
CloseHandle(streamProc.procInfo.hThread);

return 0;

}

hipersayanX commented 3 years ago

Sorry for being late, I was busy with other things.

But I'm not quite sure how I would put a opencv frame into it.

There is a very long time since I've used OpenCV, so I don't remember how to work with frames, but you basically must read a 24 bits RGB frame (8 bits for red, 8 bits for green, and 8 bits for blue), from the top left corner to the bottom right corner of the picture, and you write it line by line using WriteFile.