├── .gitignore ├── .gitattributes ├── data └── locale │ └── en-US.ini ├── src ├── droidcam.rc ├── plugin.h ├── structs.h ├── sys-win.cc ├── queue.h ├── yuv420_yuyv.c └── plugin.cc ├── README.md └── LICENSE /.gitignore: -------------------------------------------------------------------------------- 1 | build/ 2 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text eol=lf -------------------------------------------------------------------------------- /data/locale/en-US.ini: -------------------------------------------------------------------------------- 1 | DroidCam="DroidCam" 2 | Active="Active" 3 | AutoStart="Auto-Start When OBS Loads" 4 | OutputStartFailed="Error starting output, something went wrong.\nThe obs logs may contain more info." 5 | -------------------------------------------------------------------------------- /src/droidcam.rc: -------------------------------------------------------------------------------- 1 | 1 VERSIONINFO 2 | FILEVERSION 0,2,2,0 3 | BEGIN 4 | BLOCK "StringFileInfo" 5 | BEGIN 6 | BLOCK "040904B0" 7 | BEGIN 8 | VALUE "CompanyName", "DEV47APPS" 9 | VALUE "FileDescription", "Virtual Output Plugin for OBS" 10 | VALUE "InternalName", "droidcam-virtual-output.dll" 11 | VALUE "OriginalFilename", "droidcam-virtual-output.dll" 12 | VALUE "ProductName", "DroidCam" 13 | VALUE "ProductVersion", "7" 14 | VALUE "LegalCopyright", "DEV47APPS, 2025" 15 | END 16 | END 17 | 18 | BLOCK "VarFileInfo" 19 | BEGIN 20 | VALUE "Translation", 0x0409, 0x04B0 21 | END 22 | END 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | An alternative virtual output plugin that connects [OBS Studio](https://obsproject.com) 3 | with the DroidCam virtual camera drivers on Windows. 4 | 5 | Important: 6 | 7 | * You must install both the OBS plugin and the DroidCam drivers. 8 | Both are available under [releases](../../releases). 9 | 10 | * This plugin is NOT compatible with the legacy (Classic) DroidCam drivers 11 | ("DroidCam Source 3", "DroidCam Virtual Microphone"). 12 | 13 | * The new drivers will be listed as "Droidcam Video" and "Droidcam Audio" in Device Manager. 14 | The new drivers support up to 1440p video and 48kHz stereo audio. 15 | 16 | * Output from OBS will get matched with the active resolution, fps, and sampling rate of the drivers (as set by 3rd party apps). For best performance your OBS settings should match the parameters of the 3rd party apps. 17 | -------------------------------------------------------------------------------- /src/plugin.h: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2022 DEV47APPS, github.com/dev47apps 2 | #pragma once 3 | #include 4 | 5 | #define xlog(log_level, format, ...) \ 6 | blog(log_level, "[DroidcamVirtualOut] " format, ##__VA_ARGS__) 7 | 8 | #ifdef DEBUG 9 | #define dlog(format, ...) xlog(LOG_INFO, format, ##__VA_ARGS__) 10 | #else 11 | #define dlog(format, ...) /* */ 12 | #endif 13 | #define ilog(format, ...) xlog(LOG_INFO, format, ##__VA_ARGS__) 14 | #define elog(format, ...) xlog(LOG_WARNING, format, ##__VA_ARGS__) 15 | 16 | #define ARRAY_LEN(a) (sizeof(a) / sizeof(a[0])) 17 | 18 | #ifdef _WIN32 19 | #define _WIN32_WINNT 0x0501 20 | #define _WIN32_IE 0x0500 21 | #define _WIN32_DCOM 22 | #define WIN32_LEAN_AND_MEAN 23 | #include 24 | 25 | bool CreateSharedMem(LPHANDLE phFileMapping, LPVOID* ppSharedMem, 26 | const LPCWSTR name, DWORD size); 27 | 28 | int GetRegValInt(const LPCWSTR path, const LPCWSTR entry); 29 | void SetRegValInt(const LPCWSTR path, const LPCWSTR entry, int data); 30 | #endif 31 | -------------------------------------------------------------------------------- /src/structs.h: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2021 DEV47APPS, github.com/dev47apps 2 | #pragma once 3 | #pragma warning(disable : 4505) 4 | 5 | #define CONTROL 0x02020101 6 | #define MAX_WIDTH 3860 7 | #define MAX_HEIGHT 2160 8 | #define DEF_WIDTH 1280 9 | #define DEF_HEIGHT 720 10 | 11 | #define RGB_BUFFER_SIZE(w,h) (w*h*3) 12 | 13 | #define ALIGNMENT 32 14 | #define ALIGN_SIZE(size, align) size = (((size) + (align - 1)) & (~(align - 1))) 15 | 16 | #define VIDEO_MAP_SIZE (sizeof(VideoHeader) + RGB_BUFFER_SIZE(MAX_WIDTH,MAX_HEIGHT)) 17 | 18 | #define SAMPLE_BITS 16 19 | #define OBS_AUDIO_FMT AUDIO_FORMAT_16BIT 20 | #define MAX_CHANNELZ 2 21 | #define DEF_FRAMES 1024 22 | #define CHUNKS_COUNT 2 23 | #define AUDIO_DATA_SIZE ((SAMPLE_BITS/8) * DEF_FRAMES * MAX_CHANNELZ) 24 | #define AUDIO_MAP_SIZE (sizeof(AudioHeader) + (AUDIO_DATA_SIZE * CHUNKS_COUNT)) 25 | 26 | #define AUDIO_MAP_NAME L"DroidCamOBS_AudioOut0" 27 | #define VIDEO_MAP_NAME L"DroidCamOBS_VideoOut1" 28 | #define VIDEO_WR_LOCK_NAME L"DroidCamOBS_VideoWr1" 29 | #define VIDEO_RD_LOCK_NAME L"DroidCamOBS_VideoRd1" 30 | 31 | #define REG_WEBCAM_SIZE_KEY L"SOFTWARE\\DroidCam" 32 | #define REG_WEBCAM_SIZE_VAL L"Size" 33 | 34 | #ifdef __cplusplus 35 | extern "C" { 36 | #endif // __cplusplus 37 | 38 | /* Video Header */ 39 | typedef struct { 40 | int version; 41 | int control; 42 | int checksum; 43 | int width, height; 44 | int interval; 45 | int format; 46 | } DroidCamVideoInfo; 47 | 48 | typedef union { 49 | struct { 50 | DroidCamVideoInfo info; 51 | }; 52 | char pad[1024]; 53 | } VideoHeader; 54 | 55 | /* Audio Header */ 56 | typedef struct { 57 | int version; 58 | int control; 59 | int checksum; 60 | int sample_rate; 61 | int channels; 62 | } DroidCamAudioInfo; 63 | 64 | typedef union { 65 | struct { 66 | DroidCamAudioInfo info; 67 | int data_valid; 68 | }; 69 | char pad[1024]; 70 | } AudioHeader; 71 | 72 | #ifdef __cplusplus 73 | } // "C" 74 | #endif 75 | 76 | // See also dshow CRefTime 77 | #define MS_TO_100NS_UNITS(ms) ((ms) * (UNITS / MILLI_SEC)) 78 | enum RefTime { 79 | MILLI_SEC = 1000ULL, // 10 ^ 3 80 | NANO_SEC = 1000000000ULL, // 10 ^ 9 81 | UNITS = NANO_SEC/100, // 10 ^ 7 82 | }; 83 | -------------------------------------------------------------------------------- /src/sys-win.cc: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) 2022 DEV47APPS, github.com/dev47apps 3 | 4 | This program is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation, either version 2 of the License, or 7 | (at your option) any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program. If not, see . 16 | */ 17 | #include "plugin.h" 18 | // #pragma comment(lib, "advapi32") 19 | 20 | bool CreateSharedMem(LPHANDLE phFileMapping, LPVOID* ppSharedMem, 21 | const LPCWSTR name, DWORD size) 22 | { 23 | *phFileMapping = CreateFileMappingW( 24 | INVALID_HANDLE_VALUE, // use paging file 25 | NULL, // default security attributes 26 | PAGE_READWRITE, 27 | 0, // size: high 32-bits 28 | size, // size: low 32-bits 29 | name); 30 | 31 | if(*phFileMapping == NULL) { 32 | *ppSharedMem = NULL; 33 | elog("CreateFileMapping Failed !! "); 34 | return false; 35 | } 36 | 37 | *ppSharedMem = MapViewOfFile(*phFileMapping, FILE_MAP_WRITE, 0,0,0); 38 | if(*ppSharedMem == NULL) { 39 | elog("MapViewOfFile Failed !! "); 40 | CloseHandle(*phFileMapping); 41 | *phFileMapping = NULL; 42 | return false; 43 | } 44 | 45 | return true; 46 | } 47 | 48 | #if 0 49 | int GetRegValInt(const LPCWSTR path, const LPCWSTR entry) { 50 | HKEY key; 51 | DWORD data = 0; 52 | DWORD size = sizeof(data); 53 | 54 | if (ERROR_SUCCESS == RegOpenKeyExW(HKEY_CURRENT_USER, path, 0, 55 | KEY_QUERY_VALUE | KEY_WOW64_64KEY, &key)) 56 | { 57 | RegQueryValueExW(key, entry, 0, 0, (BYTE*) &data, &size); 58 | RegCloseKey(key); 59 | } 60 | 61 | return (int) data; 62 | } 63 | 64 | void SetRegValInt(const LPCWSTR path, const LPCWSTR entry, int data) { 65 | HKEY key; 66 | DWORD value = data; 67 | 68 | if (ERROR_SUCCESS == RegCreateKeyExW(HKEY_CURRENT_USER, path, 0, 0, 0, 69 | KEY_SET_VALUE | KEY_WOW64_64KEY, 0, &key, 0)) 70 | { 71 | RegSetValueExW(key, entry, 0, REG_DWORD, (BYTE*) &value, sizeof(value)); 72 | RegCloseKey(key); 73 | } 74 | } 75 | #endif 76 | -------------------------------------------------------------------------------- /src/queue.h: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2022 DEV47APPS, github.com/dev47apps 2 | #pragma once 3 | 4 | #include 5 | #include 6 | 7 | struct DataPacket { 8 | uint8_t *data; 9 | size_t size; 10 | size_t used; 11 | uint64_t pts; 12 | 13 | DataPacket(size_t new_size) { 14 | size = 0; 15 | data = 0; 16 | resize(new_size); 17 | } 18 | 19 | ~DataPacket(void) { 20 | if (data) bfree(data); 21 | } 22 | 23 | void resize(size_t new_size) { 24 | if (size < new_size){ 25 | data = (uint8_t*) brealloc(data, new_size); 26 | size = new_size; 27 | } 28 | } 29 | }; 30 | 31 | struct DataQueue { 32 | std::vector readyQueue; 33 | std::vector emptyQueue; 34 | size_t alloc_count; 35 | std::mutex mutex; 36 | 37 | inline void lock() { mutex.lock(); } 38 | inline void unlock() { mutex.unlock(); } 39 | 40 | DataQueue(void) { 41 | alloc_count = 0; 42 | } 43 | 44 | void clear() { 45 | DataPacket* packet; 46 | while ((packet = pull_ready_packet()) != NULL) { 47 | delete packet; 48 | alloc_count --; 49 | } 50 | while ((packet = pull_empty_packet(0)) != NULL){ 51 | delete packet; 52 | alloc_count --; 53 | } 54 | } 55 | 56 | ~DataQueue(void) { 57 | clear(); 58 | ilog("~alloc_count=%lu", alloc_count); 59 | } 60 | 61 | inline DataPacket* pull_ready_packet(void) { 62 | DataPacket* packet; 63 | if (readyQueue.size()) { 64 | packet = readyQueue.front(); 65 | readyQueue.erase(readyQueue.begin()); 66 | } 67 | else { 68 | packet = NULL; 69 | } 70 | return packet; 71 | } 72 | 73 | DataPacket* pull_empty_packet(size_t size) { 74 | DataPacket* packet; 75 | if (emptyQueue.size()) { 76 | packet = emptyQueue.front(); 77 | emptyQueue.erase(emptyQueue.begin()); 78 | packet->resize(size); 79 | } 80 | else if (size) { 81 | packet = new DataPacket(size); 82 | ilog("alloc: size=%ld", size); 83 | alloc_count ++; 84 | } 85 | else { 86 | return NULL; 87 | } 88 | packet->used = 0; 89 | return packet; 90 | } 91 | 92 | inline void push_empty_packet(DataPacket* packet) { 93 | emptyQueue.push_back(packet); 94 | } 95 | 96 | inline void push_ready_packet(DataPacket* packet) { 97 | readyQueue.push_back(packet); 98 | } 99 | }; 100 | -------------------------------------------------------------------------------- /src/yuv420_yuyv.c: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) 2022 DEV47APPS, github.com/dev47apps 3 | 4 | This program is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation, either version 2 of the License, or 7 | (at your option) any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program. If not, see . 16 | */ 17 | #include 18 | 19 | #if defined(__aarch64__) || defined(_M_ARM64) 20 | #define HAVE_NEON 1 21 | #include 22 | 23 | #elif defined(_MSC_VER) 24 | /* MSVC */ 25 | #if defined(_M_AMD64) || defined(_M_X64) || \ 26 | (defined(_M_IX86_FP) && (_M_IX86_FP == 2)) 27 | #define HAVE_SSE2 1 28 | #include 29 | #endif 30 | 31 | #elif defined(__x86_64__) 32 | /* GCC / Clang */ 33 | #if defined(__SSE2__) 34 | #define HAVE_SSE2 1 35 | #include 36 | #endif 37 | 38 | #endif 39 | 40 | void map_yuv420_yuyv(uint8_t** data, uint32_t *linesize, uint8_t* dst, 41 | int shift_x, int shift_y, int is_aligned_128b, 42 | const int dest_width, const int dest_height, 43 | const int width, const int height) 44 | { 45 | uint8_t* src_y = data[0]; 46 | uint8_t* src_u = data[1]; 47 | uint8_t* src_v = data[2]; 48 | const int linesize_dst = width<<1; 49 | int shift_x2; 50 | (void) dest_height; 51 | 52 | // dst can only shift in even amounts, pixels come in pairs: yu-yv 53 | // in case of odd pixel shifts, we need separate left & right shift amounts 54 | if (shift_x) { 55 | shift_x2 = dest_width - width - shift_x; 56 | shift_x <<= 1; 57 | shift_x2 <<= 1; 58 | } 59 | 60 | if (shift_y) { 61 | dst += (shift_y * dest_width) << 1; 62 | } 63 | 64 | // Each row N and N+1 use the same UV values (4:2:0 -> 4:2:2) 65 | #if HAVE_SSE2 66 | if (is_aligned_128b) 67 | { 68 | for (int y = 0; y < (height>>1); ++y) { 69 | #define CONVERT_ROW \ 70 | if (shift_x) dst += shift_x; \ 71 | for (int x = 0; x < width; x += 16) { \ 72 | __m128i y = _mm_load_si128((__m128i*)(src_y + x)); \ 73 | __m128i u = _mm_loadl_epi64((__m128i*)(src_u + (x>>1))); \ 74 | __m128i v = _mm_loadl_epi64((__m128i*)(src_v + (x>>1))); \ 75 | \ 76 | __m128i uv = _mm_unpacklo_epi8(u, v); \ 77 | __m128i yuv0 = _mm_unpacklo_epi8(y, uv); \ 78 | __m128i yuv1 = _mm_unpackhi_epi8(y, uv); \ 79 | _mm_stream_si128((__m128i*)(dst + (x<<1)), yuv0); \ 80 | _mm_stream_si128((__m128i*)(dst + (x<<1) + 16), yuv1); \ 81 | } \ 82 | if (shift_x) dst += shift_x2; 83 | 84 | CONVERT_ROW 85 | dst += linesize_dst; 86 | src_y += linesize[0]; 87 | 88 | CONVERT_ROW 89 | dst += linesize_dst; 90 | src_y += linesize[0]; 91 | src_u += linesize[1]; 92 | src_v += linesize[2]; 93 | } 94 | 95 | return; 96 | } 97 | #elif HAVE_NEON 98 | if (is_aligned_128b) 99 | { 100 | for (int y = 0; y < (height>>1); ++y) { 101 | #define CONVERT_ROW \ 102 | if (shift_x) dst += shift_x; \ 103 | for (int x = 0; x < width; x += 16) { \ 104 | uint8x16_t yq = vld1q_u8(src_y + x); \ 105 | uint8x8_t u8 = vld1_u8(src_u + (x >> 1)); \ 106 | uint8x8_t v8 = vld1_u8(src_v + (x >> 1)); \ 107 | /*interleave u and v */ \ 108 | uint8x8x2_t uvz = vzip_u8(u8, v8); \ 109 | /* combine into one 16-byte vector */ \ 110 | uint8x16_t uvq = vcombine_u8(uvz.val[0], uvz.val[1]); \ 111 | /* interleave Y and UV bytes */ \ 112 | uint8x16x2_t yuv = vzipq_u8(yq, uvq); \ 113 | vst1q_u8(dst + (x << 1), yuv.val[0]); \ 114 | vst1q_u8(dst + (x << 1) + 16, yuv.val[1]); \ 115 | } \ 116 | if (shift_x) dst += shift_x2; 117 | 118 | CONVERT_ROW 119 | dst += linesize_dst; 120 | src_y += linesize[0]; 121 | 122 | CONVERT_ROW 123 | dst += linesize_dst; 124 | src_y += linesize[0]; 125 | src_u += linesize[1]; 126 | src_v += linesize[2]; 127 | } 128 | 129 | return; 130 | } 131 | #else // not __SSE2__ 132 | (void) is_aligned_128b; 133 | #endif 134 | 135 | #undef CONVERT_ROW 136 | for (int y = 0; y < (height>>1); y++) { 137 | #define CONVERT_ROW \ 138 | if (shift_x) dst += shift_x; \ 139 | for (int x = 0; x < (width>>1); x++) { \ 140 | *dst++ = *src_y++; \ 141 | *dst++ = *src_u++; \ 142 | *dst++ = *src_y++; \ 143 | *dst++ = *src_v++; \ 144 | } \ 145 | if (shift_x) dst += shift_x2; 146 | 147 | uint8_t* src_u0 = src_u; 148 | uint8_t* src_v0 = src_v; 149 | CONVERT_ROW 150 | 151 | src_u = src_u0; 152 | src_v = src_v0; 153 | CONVERT_ROW 154 | } 155 | 156 | return; 157 | } 158 | 159 | void clear_yuyv(uint8_t* dst, int size, int color) { 160 | int* ptr = (int*)dst; 161 | for (int i = 0; i < size / sizeof(int); i++) { 162 | *ptr++ = color; 163 | } 164 | } 165 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc., 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Lesser General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS -------------------------------------------------------------------------------- /src/plugin.cc: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) 2025 DEV47APPS, github.com/dev47apps 3 | 4 | This program is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation, either version 2 of the License, or 7 | (at your option) any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program. If not, see . 16 | */ 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include "plugin.h" 22 | #include "queue.h" 23 | #include "structs.h" 24 | 25 | #if DROIDCAM_OVERRIDE==0 26 | 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include "obs-frontend-api.h" 32 | 33 | QAction *auto_start_action; 34 | QAction *tools_menu_action; 35 | #endif 36 | 37 | const char *PluginVer = "022"; 38 | const char *PluginName = "DroidCam Virtual Output"; 39 | obs_output_t *droidcam_virtual_output = NULL; 40 | config_t *obs_config = NULL; 41 | 42 | void map_yuv420_yuyv(uint8_t** data, uint32_t *linesize, uint8_t* dst, 43 | int shift_x, int shift_y, int is_aligned_128b, 44 | const int dest_width, const int dest_height, 45 | const int width, const int height); 46 | 47 | void clear_yuyv(uint8_t* dst, int size, int color); 48 | 49 | struct droidcam_output_plugin { 50 | // video 51 | int webcam_w, webcam_h; 52 | int default_w, default_h; 53 | int default_interval; 54 | int shift_x, shift_y; 55 | int is_aligned_128b; 56 | 57 | // audio 58 | int default_sample_rate; 59 | enum speaker_layout default_speaker_layout; 60 | 61 | // 62 | bool have_video; 63 | bool have_audio; 64 | 65 | int audio_frame_size_bytes; 66 | struct audio_convert_info audio_conv; 67 | struct video_scale_info video_conv; 68 | 69 | // 70 | obs_output_t *output; 71 | pthread_t audio_thread; 72 | pthread_t control_thread; 73 | os_event_t *stop_signal; 74 | 75 | // 76 | #ifdef _WIN32 77 | volatile VideoHeader *pVideoHeader; 78 | volatile AudioHeader *pAudioHeader; 79 | BYTE *pVideoData; 80 | BYTE *pAudioData; 81 | 82 | LPVOID pVideoMem; 83 | HANDLE hVideoMapping; 84 | HANDLE hVideoWrLock; 85 | HANDLE hVideoRdLock; 86 | 87 | LPVOID pAudioMem; 88 | HANDLE hAudioMapping; 89 | DataQueue audioDataQueue; 90 | #endif 91 | }; 92 | 93 | static inline enum speaker_layout to_speaker_layout(int channels) { 94 | switch (channels) { 95 | case 1: 96 | return SPEAKERS_MONO; 97 | case 2: 98 | return SPEAKERS_STEREO; 99 | default: 100 | return SPEAKERS_UNKNOWN; 101 | } 102 | } 103 | 104 | static inline int to_channels(enum speaker_layout speaker_layout) { 105 | switch (speaker_layout) { 106 | case SPEAKERS_STEREO: 107 | return 2; 108 | case SPEAKERS_MONO: 109 | default: 110 | return 1; 111 | } 112 | } 113 | 114 | #define AUDIO_CUSHION 4 115 | 116 | static void *audio_thread(void *data) { 117 | droidcam_output_plugin *plugin = reinterpret_cast(data); 118 | dlog("audio_thread start"); 119 | 120 | int waiting = 0; 121 | while ((os_event_timedwait(plugin->stop_signal, 5) != 0) && (plugin->pAudioData)) 122 | { 123 | if (!plugin->have_audio) { 124 | if (!waiting) waiting = 1; 125 | continue; 126 | } 127 | 128 | if (waiting) { 129 | if (plugin->audioDataQueue.readyQueue.size() < AUDIO_CUSHION) 130 | continue; 131 | 132 | waiting = 0; 133 | } 134 | else { 135 | if (plugin->audioDataQueue.readyQueue.size() == 0) 136 | waiting = 1; 137 | } 138 | 139 | if (plugin->pAudioHeader->data_valid) 140 | continue; 141 | 142 | plugin->audioDataQueue.lock(); 143 | DataPacket *packet = plugin->audioDataQueue.pull_ready_packet(); 144 | if (packet) { 145 | memcpy(plugin->pAudioData, packet->data, packet->used); 146 | plugin->pAudioHeader->data_valid = 1; 147 | plugin->audioDataQueue.push_empty_packet(packet); 148 | } else { 149 | dlog("missed frame"); 150 | } 151 | plugin->audioDataQueue.unlock(); 152 | } 153 | 154 | dlog("audio_thread end"); 155 | return 0; 156 | } 157 | 158 | static void video_conversion(droidcam_output_plugin *plugin) { 159 | int shift_x, shift_y; 160 | int src_w = plugin->default_w; 161 | int src_h = plugin->default_h; 162 | int dst_w = plugin->webcam_w; 163 | int dst_h = plugin->webcam_h; 164 | 165 | if (src_w == dst_w && src_h == dst_h) { 166 | plugin->shift_x = 0; 167 | plugin->shift_y = 0; 168 | plugin->video_conv.width = dst_w; 169 | plugin->video_conv.height = dst_h; 170 | return; 171 | } 172 | 173 | float src_ar = (float) src_w / (float) src_h; 174 | float dst_ar = (float) dst_w / (float) dst_h; 175 | 176 | float h_ar = (float)dst_h / (float)src_h; 177 | float w_ar = (float)dst_w / (float)src_w; 178 | 179 | if (abs(src_ar - dst_ar) < 0.01f) { 180 | // same aspect ratio 181 | shift_x = 0; 182 | shift_y = 0; 183 | 184 | } else if (h_ar < w_ar) { 185 | const int dst_w0 = dst_w; 186 | dst_w = dst_h * src_w / src_h; 187 | shift_x = (dst_w0 - dst_w) / 2; 188 | shift_y = 0; 189 | } 190 | else { 191 | const int dst_h0 = dst_h; 192 | dst_h = dst_w * src_h / src_w; 193 | shift_x = 0; 194 | shift_y = (dst_h0 - dst_h) / 2; 195 | } 196 | 197 | // force even amounts 198 | shift_x &= ~1; 199 | shift_y &= ~1; 200 | dst_w &= ~1; 201 | dst_h &= ~1; 202 | 203 | ilog("video scaling: %dx%d -> %dx%d inside %dx%d at %d,%d", 204 | src_w, src_h, dst_w, dst_h, 205 | plugin->webcam_w, plugin->webcam_h, 206 | shift_x, shift_y); 207 | plugin->video_conv.width = dst_w; 208 | plugin->video_conv.height = dst_h; 209 | plugin->shift_x = shift_x; 210 | plugin->shift_y = shift_y; 211 | } 212 | 213 | static void *control_thread(void *data) { 214 | droidcam_output_plugin *plugin = reinterpret_cast(data); 215 | dlog("control_thread start"); 216 | 217 | volatile VideoHeader *vh = plugin->pVideoHeader; 218 | volatile AudioHeader *ah = plugin->pAudioHeader; 219 | 220 | while (os_event_timedwait(plugin->stop_signal, 999) != 0) { 221 | 222 | bool have_video = 223 | vh->info.control == CONTROL 224 | && vh->info.checksum == (vh->info.interval ^ 225 | vh->info.width ^ vh->info.height); 226 | 227 | bool have_audio = 228 | ah->info.control == CONTROL 229 | && ah->info.checksum == (ah->info.sample_rate ^ ah->info.channels); 230 | 231 | //dlog("audio queue size: %d / %d", 232 | // (int) plugin->audioDataQueue.emptyQueue.size(), 233 | // (int) plugin->audioDataQueue.readyQueue.size()); 234 | 235 | if (!have_video && !have_audio) { 236 | if (obs_output_active(plugin->output)) { 237 | ilog("webcam became inactive"); 238 | obs_output_end_data_capture(plugin->output); 239 | } 240 | 241 | continue; 242 | } 243 | 244 | int flags = 0; 245 | int webcam_w, webcam_h, webcam_interval; 246 | 247 | int webcam_audio_rate; 248 | enum speaker_layout webcam_speaker_layout; 249 | 250 | if (have_video) { 251 | webcam_w = vh->info.width; 252 | webcam_h = vh->info.height; 253 | webcam_interval = vh->info.interval; 254 | flags |= OBS_OUTPUT_VIDEO; 255 | } 256 | else { 257 | webcam_w = plugin->default_w; 258 | webcam_h = plugin->default_h; 259 | webcam_interval = plugin->default_interval; 260 | } 261 | 262 | if (have_audio) { 263 | webcam_audio_rate = ah->info.sample_rate; 264 | webcam_speaker_layout = to_speaker_layout(ah->info.channels); 265 | 266 | if (webcam_speaker_layout != SPEAKERS_UNKNOWN) { 267 | flags |= OBS_OUTPUT_AUDIO; 268 | } 269 | else { 270 | elog("WARN: unknown webcam speaker layout, channels=%d", ah->info.channels); 271 | have_audio = false; 272 | } 273 | } 274 | if (!have_audio) { 275 | webcam_audio_rate = plugin->default_sample_rate; 276 | webcam_speaker_layout = plugin->default_speaker_layout; 277 | } 278 | 279 | const bool video_ok = 280 | (webcam_w - plugin->shift_x - plugin->shift_x - plugin->video_conv.width <= 4) && 281 | (webcam_h - plugin->shift_y - plugin->shift_y - plugin->video_conv.height <= 4); 282 | 283 | const bool audio_ok = 284 | plugin->audio_conv.speakers == webcam_speaker_layout && 285 | plugin->audio_conv.samples_per_sec == webcam_audio_rate; 286 | 287 | if (obs_output_active(plugin->output)) { 288 | if (audio_ok && video_ok) 289 | continue; 290 | 291 | dlog("output conversion needs to be updated"); 292 | obs_output_end_data_capture(plugin->output); 293 | 294 | // todo: this can probably be improved.. 295 | while (obs_output_active(plugin->output)) { 296 | os_sleep_ms(5); 297 | if (os_event_try(plugin->stop_signal) != EAGAIN) 298 | return 0; 299 | } 300 | } 301 | 302 | if (have_video) 303 | ilog("webcam video active %dx%d %dfps, video_ok=%d", 304 | webcam_w, webcam_h, 305 | (int)(RefTime::UNITS / webcam_interval), 306 | (int) video_ok); 307 | 308 | if (have_audio) 309 | ilog("webcam audio active %d Hz %d channels, audio_ok=%d", 310 | webcam_audio_rate, webcam_speaker_layout, (int) audio_ok); 311 | 312 | if (!video_ok) { 313 | plugin->webcam_w = webcam_w; 314 | plugin->webcam_h = webcam_h; 315 | video_conversion(plugin); 316 | plugin->is_aligned_128b = (plugin->video_conv.width % 16 == 0); 317 | obs_output_set_video_conversion(plugin->output, &plugin->video_conv); 318 | } 319 | 320 | if (!audio_ok) { 321 | plugin->audio_frame_size_bytes = (SAMPLE_BITS/8) * to_channels(webcam_speaker_layout); 322 | plugin->audio_conv.speakers = webcam_speaker_layout; 323 | plugin->audio_conv.samples_per_sec = webcam_audio_rate; 324 | obs_output_set_audio_conversion(plugin->output, &plugin->audio_conv); 325 | } 326 | 327 | plugin->have_video = have_video; 328 | plugin->have_audio = have_audio; 329 | plugin->audioDataQueue.lock(); 330 | plugin->audioDataQueue.clear(); 331 | plugin->audioDataQueue.unlock(); 332 | memset(plugin->pAudioData, 0, AUDIO_DATA_SIZE * CHUNKS_COUNT); 333 | clear_yuyv(plugin->pVideoData, MAX_WIDTH*MAX_HEIGHT*2, 0x80008000); 334 | obs_output_begin_data_capture(plugin->output, 0); 335 | } 336 | 337 | dlog("control_thread end"); 338 | return 0; 339 | } 340 | 341 | static void output_stop(void *data, uint64_t ts) { 342 | droidcam_output_plugin *plugin = reinterpret_cast(data); 343 | dlog("output_stop"); 344 | os_event_signal(plugin->stop_signal); 345 | pthread_join(plugin->audio_thread, NULL); 346 | pthread_join(plugin->control_thread, NULL); 347 | obs_output_end_data_capture(plugin->output); 348 | 349 | UNUSED_PARAMETER(ts); 350 | } 351 | 352 | static bool output_start(void *data) { 353 | droidcam_output_plugin *plugin = reinterpret_cast(data); 354 | if (!(plugin->pVideoMem && plugin->pAudioMem)) { 355 | elog("Cannot start without memory mapping !! "); 356 | return false; 357 | } 358 | if (os_event_try(plugin->stop_signal) != 0) { 359 | output_stop(data, 0); 360 | } 361 | 362 | video_t *video = obs_output_video(plugin->output); 363 | int width = video_output_get_width(video); 364 | int height = video_output_get_height(video); 365 | int format = video_output_get_format(video); 366 | 367 | struct obs_video_info ovi; 368 | obs_get_video_info(&ovi); 369 | int interval = ovi.fps_den * RefTime::UNITS / ovi.fps_num; 370 | dlog("output_start: video %dx%d interval %lld format %d", width, height, interval, format); 371 | 372 | #if DEBUG==1 373 | if (ovi.output_width != width) 374 | elog("output width mismatch !!"); 375 | if (ovi.output_height != height) 376 | elog("output height mismatch !!"); 377 | if (ovi.output_format != format) 378 | elog("output format mismatch !!"); 379 | #endif 380 | 381 | plugin->have_video = false; 382 | plugin->shift_x = 0; 383 | plugin->shift_y = 0; 384 | plugin->default_w = width; 385 | plugin->default_h = height; 386 | plugin->default_interval = interval; 387 | plugin->video_conv.format = VIDEO_FORMAT_I420; 388 | plugin->video_conv.width = width; 389 | plugin->video_conv.height = height; 390 | plugin->is_aligned_128b = (width % 16 == 0); 391 | obs_output_set_video_conversion(plugin->output, &plugin->video_conv); 392 | 393 | audio_t *audio = obs_output_audio(plugin->output); 394 | int channels = audio_output_get_channels(audio); 395 | int sample_rate = (int) audio_output_get_sample_rate(audio); 396 | dlog(" : audio channels %d sample_rate %d", channels, sample_rate); 397 | 398 | plugin->have_audio = false; 399 | plugin->default_sample_rate = sample_rate; 400 | plugin->default_speaker_layout = to_speaker_layout(channels); 401 | plugin->audio_frame_size_bytes = (SAMPLE_BITS/8) * channels; 402 | plugin->audio_conv.format = OBS_AUDIO_FMT; 403 | plugin->audio_conv.samples_per_sec = sample_rate; 404 | plugin->audio_conv.speakers = plugin->default_speaker_layout; 405 | obs_output_set_audio_conversion(plugin->output, &plugin->audio_conv); 406 | 407 | os_event_reset(plugin->stop_signal); 408 | pthread_create(&plugin->audio_thread, NULL, audio_thread, plugin); 409 | pthread_create(&plugin->control_thread, NULL, control_thread, plugin); 410 | return true; 411 | } 412 | 413 | static void output_destroy(void *data) { 414 | droidcam_output_plugin *plugin = reinterpret_cast(data); 415 | 416 | dlog("output_destroy"); 417 | if (plugin) { 418 | if (os_event_try(plugin->stop_signal) != 0) { 419 | output_stop(data, 0); 420 | } 421 | 422 | #ifdef _WIN32 423 | plugin->pVideoData = NULL; 424 | plugin->pVideoHeader = NULL; 425 | if (plugin->pVideoMem) { 426 | ilog("closing shared memory [video]"); 427 | UnmapViewOfFile(plugin->pVideoMem); 428 | CloseHandle(plugin->hVideoMapping); 429 | } 430 | 431 | plugin->pAudioData = NULL; 432 | plugin->pAudioHeader = NULL; 433 | if (plugin->pAudioMem) { 434 | ilog("closing shared memory [audio]"); 435 | UnmapViewOfFile(plugin->pAudioMem); 436 | CloseHandle(plugin->hAudioMapping); 437 | } 438 | 439 | if (plugin->hVideoWrLock) CloseHandle(plugin->hVideoWrLock); 440 | if (plugin->hVideoRdLock) CloseHandle(plugin->hVideoRdLock); 441 | #endif 442 | 443 | os_event_destroy(plugin->stop_signal); 444 | delete plugin; 445 | ilog("plugin destroyed"); 446 | } 447 | } 448 | 449 | static void *output_create(obs_data_t *settings, obs_output_t *output) { 450 | ilog("output_create: %p r%s", output, PluginVer); 451 | droidcam_output_plugin *plugin = new droidcam_output_plugin(); 452 | plugin->output = output; 453 | os_event_init(&plugin->stop_signal, OS_EVENT_TYPE_MANUAL); 454 | 455 | #ifdef _WIN32 456 | { 457 | const LPCWSTR name = VIDEO_MAP_NAME; 458 | DWORD size = VIDEO_MAP_SIZE; 459 | ALIGN_SIZE(size, ALIGNMENT); 460 | 461 | if (CreateSharedMem(&plugin->hVideoMapping, &plugin->pVideoMem, name, size)) { 462 | ilog("mapped %8d bytes @ %p [video]", size, plugin->pVideoMem); 463 | plugin->pVideoHeader = (VideoHeader *) plugin->pVideoMem; 464 | plugin->pVideoData = (BYTE*)(plugin->pVideoHeader + 1); 465 | } 466 | 467 | plugin->hVideoWrLock = CreateEventW( NULL, TRUE, TRUE, VIDEO_WR_LOCK_NAME ); 468 | plugin->hVideoRdLock = CreateEventW( NULL, TRUE, TRUE, VIDEO_RD_LOCK_NAME ); 469 | } 470 | { 471 | const LPCWSTR name = AUDIO_MAP_NAME; 472 | DWORD size = AUDIO_MAP_SIZE; 473 | ALIGN_SIZE(size, ALIGNMENT); 474 | 475 | if (CreateSharedMem(&plugin->hAudioMapping, &plugin->pAudioMem, name, size)) { 476 | ilog("mapped %8d bytes @ %p [audio]", size, plugin->pAudioMem); 477 | plugin->pAudioHeader = (AudioHeader *) plugin->pAudioMem; 478 | plugin->pAudioData = (BYTE*)plugin->pAudioMem + sizeof(AudioHeader); 479 | } 480 | } 481 | #endif // _WIN32 482 | 483 | UNUSED_PARAMETER(settings); 484 | return plugin; 485 | } 486 | 487 | static void on_video(void *data, struct video_data *frame) { 488 | droidcam_output_plugin *plugin = reinterpret_cast(data); 489 | if (plugin->have_video && plugin->pVideoData) { 490 | #ifdef _WIN32 491 | if (plugin->hVideoWrLock && plugin->hVideoRdLock) { 492 | ResetEvent(plugin->hVideoWrLock); 493 | if (WaitForSingleObject(plugin->hVideoRdLock, 5) == 0) 494 | { 495 | uint8_t* dst = plugin->pVideoData; 496 | map_yuv420_yuyv(frame->data, frame->linesize, dst, 497 | plugin->shift_x, plugin->shift_y, 498 | plugin->is_aligned_128b, 499 | plugin->webcam_w, plugin->webcam_h, 500 | plugin->video_conv.width, plugin->video_conv.height); 501 | } 502 | else 503 | { 504 | dlog("video lock fail/timeout: frame dropped"); 505 | } 506 | SetEvent(plugin->hVideoWrLock); 507 | } 508 | #endif 509 | } 510 | } 511 | 512 | static void on_audio(void *data, struct audio_data *frame) { 513 | droidcam_output_plugin *plugin = reinterpret_cast(data); 514 | if (plugin->have_audio) { 515 | 516 | #ifdef _WIN32 517 | if (plugin->audioDataQueue.readyQueue.size() < AUDIO_CUSHION) { 518 | const int frames = frame->frames > DEF_FRAMES ? DEF_FRAMES : frame->frames; 519 | const int size = frames * plugin->audio_frame_size_bytes; 520 | 521 | plugin->audioDataQueue.lock(); 522 | DataPacket *packet = plugin->audioDataQueue.pull_empty_packet(size); 523 | memcpy(packet->data, frame->data[0], size); 524 | packet->used = size; 525 | // packet->pts = frame->timestamp; 526 | plugin->audioDataQueue.push_ready_packet(packet); 527 | plugin->audioDataQueue.unlock(); 528 | } 529 | #endif // _WIN32 530 | 531 | } 532 | } 533 | 534 | static const char *output_getname(void *data) { 535 | UNUSED_PARAMETER(data); 536 | return PluginName; 537 | } 538 | 539 | struct obs_output_info droidcam_virtual_output_info; 540 | 541 | OBS_DECLARE_MODULE() 542 | OBS_MODULE_USE_DEFAULT_LOCALE("droidcam-virtual-output", "en-US") 543 | MODULE_EXPORT const char *obs_module_description(void) { 544 | return PluginName; 545 | } 546 | 547 | bool obs_module_load(void) { 548 | memset(&droidcam_virtual_output_info, 0, sizeof(struct obs_output_info)); 549 | 550 | droidcam_virtual_output_info.id = "droidcam_virtual_output", 551 | droidcam_virtual_output_info.flags = OBS_OUTPUT_AV, 552 | droidcam_virtual_output_info.get_name = output_getname, 553 | droidcam_virtual_output_info.create = output_create, 554 | droidcam_virtual_output_info.destroy = output_destroy, 555 | droidcam_virtual_output_info.start = output_start, 556 | droidcam_virtual_output_info.stop = output_stop, 557 | droidcam_virtual_output_info.raw_video = on_video, 558 | droidcam_virtual_output_info.raw_audio = on_audio, 559 | obs_register_output(&droidcam_virtual_output_info); 560 | 561 | #if DROIDCAM_OVERRIDE 562 | 563 | obs_data_t *obs_settings = obs_data_create(); 564 | obs_data_set_bool(obs_settings, "vcamEnabled", true); 565 | obs_apply_private_data(obs_settings); 566 | obs_data_release(obs_settings); 567 | 568 | #else 569 | blog(LOG_INFO, "[droidcam-virtual-output] module loaded release %s", PluginVer); 570 | 571 | obs_config = obs_frontend_get_profile_config(); 572 | config_set_default_bool(obs_config, "DroidCamVirtualOutput", "AutoStart", false); 573 | 574 | QMainWindow *main_window = (QMainWindow *)obs_frontend_get_main_window(); 575 | QAction *action = (QAction*)obs_frontend_add_tools_menu_qaction(PluginName); 576 | QMenu *menu = new QMenu(); 577 | action->setMenu(menu); 578 | 579 | tools_menu_action = menu->addAction(obs_module_text("Active")); 580 | tools_menu_action->setCheckable(true); 581 | tools_menu_action->setChecked(false); 582 | 583 | tools_menu_action->connect(tools_menu_action, &QAction::triggered, [=] (bool checked) { 584 | if (!droidcam_virtual_output) { 585 | obs_data_t *obs_settings = obs_data_create(); 586 | droidcam_virtual_output = obs_output_create( 587 | "droidcam_virtual_output", "DroidCamVirtualOutput", obs_settings, NULL); 588 | ilog("droidcam_virtual_output=%p", droidcam_virtual_output); 589 | obs_data_release(obs_settings); 590 | } 591 | 592 | if (checked) { 593 | obs_output_set_media(droidcam_virtual_output, 594 | obs_get_video(), obs_get_audio()); 595 | 596 | if (!obs_output_start(droidcam_virtual_output)) { 597 | obs_output_force_stop(droidcam_virtual_output); 598 | tools_menu_action->setChecked(false); 599 | 600 | QMessageBox mb(QMessageBox::Warning, PluginName, 601 | obs_module_text("OutputStartFailed"), QMessageBox::Ok, main_window); 602 | mb.setButtonText(QMessageBox::Ok, "OK"); 603 | mb.exec(); 604 | } 605 | } 606 | else { 607 | // Force stop since we may not be actively capturing data, 608 | // if the webcam is not in use. 609 | obs_output_force_stop(droidcam_virtual_output); 610 | } 611 | }); 612 | 613 | auto_start_action = menu->addAction(obs_module_text("AutoStart")); 614 | auto_start_action->setCheckable(true); 615 | auto_start_action->setChecked(config_get_bool(obs_config, "DroidCamVirtualOutput", "AutoStart")); 616 | auto_start_action->connect(auto_start_action, &QAction::triggered, [=] (bool checked) { 617 | config_set_bool(obs_config, "DroidCamVirtualOutput", "AutoStart", checked); 618 | config_save(obs_config); 619 | }); 620 | 621 | // todo - investigate: there seems to be a race condition in obs_graphics_thread, 622 | // causing a crash when exiting while the output is enabled and capturing. 623 | // I'm guessing the pthread_joins here are creating delays and triggering it. 624 | // Comment this out to reproduce. 625 | obs_frontend_add_event_callback([] (enum obs_frontend_event event, void*) { 626 | if (event == OBS_FRONTEND_EVENT_EXIT && droidcam_virtual_output) { 627 | obs_output_force_stop(droidcam_virtual_output); 628 | obs_output_release(droidcam_virtual_output); 629 | droidcam_virtual_output = NULL; 630 | return; 631 | } 632 | 633 | if (event == OBS_FRONTEND_EVENT_FINISHED_LOADING) { 634 | if (config_get_bool(obs_config, "DroidCamVirtualOutput", "AutoStart")) { 635 | ilog("AutoStart Trigger"); 636 | tools_menu_action->activate(QAction::Trigger); 637 | } 638 | 639 | } 640 | 641 | }, NULL); 642 | 643 | #endif // DROIDCAM_OVERRIDE 644 | return true; 645 | } 646 | 647 | void obs_module_unload(void) { 648 | } 649 | --------------------------------------------------------------------------------