From 55418655bd2ddca22f7e67cb20c9a631e77bf33a Mon Sep 17 00:00:00 2001 From: Tai Date: Sun, 23 Jul 2017 15:43:11 +0800 Subject: [PATCH] add init resolution with Obs function --- src/queue/share_queue.cpp | 28 ++++++++++++++----- src/queue/share_queue.h | 2 +- src/virtual-source/virtual-cam.cpp | 43 +++++++++++++++++++++++++----- src/virtual-source/virtual-cam.h | 7 +++++ 4 files changed, 65 insertions(+), 15 deletions(-) diff --git a/src/queue/share_queue.cpp b/src/queue/share_queue.cpp index a5e0b16..32c8bc2 100644 --- a/src/queue/share_queue.cpp +++ b/src/queue/share_queue.cpp @@ -172,17 +172,31 @@ bool share_queue_init_index(share_queue* q) return true; } -bool shared_queue_get_video_format(share_queue* q,int* format ,int* width, +bool shared_queue_get_video_format(int* format ,int* width, int* height, int64_t* avgtime) { - if (!q || !q->header) + bool success =true; + HANDLE hwnd; + queue_header* header; + + hwnd = OpenFileMappingA(FILE_MAP_ALL_ACCESS, FALSE, MAPPING_NAMEV); + if (hwnd) + header = (queue_header*)MapViewOfFile(hwnd, FILE_MAP_ALL_ACCESS, 0, 0, 0); + else return false; - *format = q->header->format; - *width = q->header->frame_width; - *height = q->header->frame_height; - *avgtime = (q->header->frame_time)/100; - return true; + if (header){ + *format = header->format; + *width = header->frame_width; + *height = header->frame_height; + *avgtime = (header->frame_time) / 100; + UnmapViewOfFile(header); + } + else + success = false; + + CloseHandle(hwnd); + return success; } bool shared_queue_get_video(share_queue* q, uint8_t** data, diff --git a/src/queue/share_queue.h b/src/queue/share_queue.h index 206e9b5..f4d7ef5 100644 --- a/src/queue/share_queue.h +++ b/src/queue/share_queue.h @@ -66,7 +66,7 @@ void shared_queue_close(share_queue* q); bool shared_queue_check(int mode); bool shared_queue_set_delay(share_queue* q, int delay_video_frame); bool share_queue_init_index(share_queue* q); -bool shared_queue_get_video_format(share_queue* q, int* format, int* width, +bool shared_queue_get_video_format(int* format, int* width, int* height, int64_t* avgtime); bool shared_queue_get_video(share_queue* q, uint8_t** dst_ptr, uint32_t*linesize, uint64_t* timestamp); diff --git a/src/virtual-source/virtual-cam.cpp b/src/virtual-source/virtual-cam.cpp index 55e3575..1f0e702 100644 --- a/src/virtual-source/virtual-cam.cpp +++ b/src/virtual-source/virtual-cam.cpp @@ -32,6 +32,7 @@ HRESULT CVCam::NonDelegatingQueryInterface(REFIID riid, void **ppv) CVCamStream::CVCamStream(HRESULT *phr, CVCam *pParent, LPCWSTR pPinName) : CSourceStream(NAME("Video"), phr, pParent, pPinName), parent(pParent) { + use_obs_format_init = CheckObsSetting(); GetMediaType(0,&m_mt); prev_end_ts = 0; } @@ -40,13 +41,28 @@ CVCamStream::~CVCamStream() { } +bool CVCamStream::CheckObsSetting() +{ + bool get= shared_queue_get_video_format(&obs_format, &obs_width, + &obs_height, &obs_time_perframe); + + if (obs_width < 320 || obs_height < 240) + return false; + else if (obs_time_perframe < 166666 || obs_time_perframe>1000000) + return false; + + if (obs_height % 2 != 0) + obs_height += 1; + + return get; +} + HRESULT CVCamStream::ChangeMediaType(int nMediatype) { GetMediaType(0,&m_mt); return S_OK; } - void CVCamStream::SetConvertContext(int width, int height, AVPixelFormat fotmat) { VIDEOINFOHEADER *pvi = (VIDEOINFOHEADER *)(m_mt.Format()); @@ -83,7 +99,7 @@ HRESULT CVCamStream::FillBuffer(IMediaSample *pms) if (!queue.hwnd){ if (shared_queue_open(&queue, ModeVideo)){ - shared_queue_get_video_format(&queue, &format, &frame_width, + shared_queue_get_video_format(&format, &frame_width, &frame_height, &time_perframe); SetConvertContext(frame_width, frame_height, (AVPixelFormat)format); } @@ -152,7 +168,10 @@ HRESULT CVCamStream::SetMediaType(const CMediaType *pmt) HRESULT CVCamStream::GetMediaType(int iPosition,CMediaType *pmt) { - if (iPosition < 0 || iPosition>4) + if (!use_obs_format_init) + iPosition += 1; + + if (iPosition < 0 || iPosition>5) return E_INVALIDARG; DECLARE_PTR(VIDEOINFOHEADER, pvi, @@ -161,24 +180,32 @@ HRESULT CVCamStream::GetMediaType(int iPosition,CMediaType *pmt) switch (iPosition){ case 0: + pvi->bmiHeader.biWidth = obs_width; + pvi->bmiHeader.biHeight = obs_height; + pvi->AvgTimePerFrame = obs_time_perframe; + break; + case 1: pvi->bmiHeader.biWidth = 1920; pvi->bmiHeader.biHeight = 1080; + pvi->AvgTimePerFrame = 333333; break; - case 1: + case 2: pvi->bmiHeader.biWidth = 1280; pvi->bmiHeader.biHeight = 720; + pvi->AvgTimePerFrame = 333333; break; - case 2: + case 3: pvi->bmiHeader.biWidth = 960; pvi->bmiHeader.biHeight = 540; + pvi->AvgTimePerFrame = 333333; break; - case 3: + case 4: pvi->bmiHeader.biWidth = 640; pvi->bmiHeader.biHeight = 360; + pvi->AvgTimePerFrame = 333333; break; } - pvi->AvgTimePerFrame = 333333; pvi->bmiHeader.biCompression = MAKEFOURCC('Y', 'U', 'Y', '2');; pvi->bmiHeader.biBitCount = 16; pvi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER); @@ -235,6 +262,8 @@ bool CVCamStream::ValidateResolution(long width,long height) return true; else if (width * 3 == height * 4) return true; + else if (use_obs_format_init && width == obs_width && height == obs_height) + return true; else return false; } diff --git a/src/virtual-source/virtual-cam.h b/src/virtual-source/virtual-cam.h index 0230c5c..12ba119 100644 --- a/src/virtual-source/virtual-cam.h +++ b/src/virtual-source/virtual-cam.h @@ -80,6 +80,7 @@ class CVCamStream : public CSourceStream, public IAMStreamConfig,public IKsPrope HRESULT OnThreadCreate(void); HRESULT OnThreadDestroy(void); HRESULT ChangeMediaType(int nMediatype); + bool CheckObsSetting(); bool ValidateResolution(long width, long height); void SetConvertContext(int width, int height, AVPixelFormat fotmat); @@ -99,4 +100,10 @@ class CVCamStream : public CSourceStream, public IAMStreamConfig,public IKsPrope int frame_height = 0; int64_t time_perframe = 0; struct SwsContext *convert_ctx = nullptr; + + bool use_obs_format_init = false; + int obs_format = 0; + int obs_width = 1920; + int obs_height = 1080; + int64_t obs_time_perframe = 333333; };