Skip to content
This repository has been archived by the owner on Mar 15, 2021. It is now read-only.

Commit

Permalink
add source clock counting
Browse files Browse the repository at this point in the history
  • Loading branch information
CatxFish committed May 2, 2019
1 parent 45c1c42 commit dfc4c9b
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 7 deletions.
2 changes: 2 additions & 0 deletions src/virtual-source/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ set(virtualsource_SOURCES
dllmain.cpp
virtual-cam.cpp
virtual-audio.cpp
clock.cpp
virtua-source.def
../queue/share_queue_read.cpp
)

set(virtualsource_HEADERS
virtual-cam.h
virtual-audio.h
clock.h
../queue/share_queue_read.h
../queue/share_queue.h
)
Expand Down
37 changes: 37 additions & 0 deletions src/virtual-source/clock.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include "clock.h"

static bool have_clockfreq = false;
static LARGE_INTEGER clock_freq;

//directshow use 100ns as time unit

uint64_t get_current_time(void)
{
LARGE_INTEGER current_time;
double time_val;

if (!have_clockfreq) {
QueryPerformanceFrequency(&clock_freq);
have_clockfreq = true;
}

QueryPerformanceCounter(&current_time);
time_val = (double)current_time.QuadPart;
time_val *= 10000000.0;
time_val /= (double)clock_freq.QuadPart;

return (uint64_t)time_val;
}

bool sleepto(uint64_t time_target)
{
uint64_t t = get_current_time();
uint32_t milliseconds;

if (t >= time_target)
return false;

milliseconds = (uint32_t)((time_target - t) / 10000);
if (milliseconds > 1)
Sleep(milliseconds - 1);
}
5 changes: 5 additions & 0 deletions src/virtual-source/clock.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include <windows.h>
#include <stdint.h>

uint64_t get_current_time(void);
bool sleepto(uint64_t time_target);
12 changes: 6 additions & 6 deletions src/virtual-source/virtual-audio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <dvdmedia.h>
#include <commctrl.h>
#include "virtual-audio.h"
#include "clock.h"

CUnknown * WINAPI CVAudio::CreateInstance(LPUNKNOWN lpunk, HRESULT *phr)
{
Expand Down Expand Up @@ -85,6 +86,11 @@ HRESULT CVAudioStream::FillBuffer(IMediaSample *pms)
if (!queue.hwnd)
shared_queue_open(&queue, ModeAudio);

if (prev_end_ts <= 0)
prev_end_ts = get_current_time();

sleepto(prev_end_ts);

while (queue.header && !get_sample){

if (get_times > 20 || queue.header->state != OutputReady)
Expand Down Expand Up @@ -120,16 +126,10 @@ HRESULT CVAudioStream::FillBuffer(IMediaSample *pms)
dshow_start_ts = 0;
}

if (prev_end_ts != start_time)
int a = 0;

REFERENCE_TIME duration = (REFERENCE_TIME)10000000 * size /
(REFERENCE_TIME)SAMPLE_SIZE;
end_time = start_time + duration;
prev_end_ts = end_time;



pms->SetTime(&start_time, &end_time);
pms->SetSyncPoint(TRUE);
return NOERROR;
Expand Down
7 changes: 6 additions & 1 deletion src/virtual-source/virtual-cam.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <dvdmedia.h>
#include <commctrl.h>
#include "virtual-cam.h"
#include "clock.h"

#define MIN_WIDTH 320
#define MIN_HEIGHT 240
Expand Down Expand Up @@ -158,7 +159,10 @@ HRESULT CVCamStream::FillBuffer(IMediaSample *pms)
Sleep(5);
get_times++;
}
}
}

if (prev_end_ts <= 0)
prev_end_ts = get_current_time();

if (get_sample && !obs_start_ts) {
obs_start_ts = timestamp;
Expand All @@ -170,6 +174,7 @@ HRESULT CVCamStream::FillBuffer(IMediaSample *pms)
duration = time_perframe;
} else {
int size = pms->GetActualDataLength();
sleepto(prev_end_ts);
memset(dst, 127, size);
start_time = prev_end_ts;
duration = ((VIDEOINFOHEADER*)m_mt.pbFormat)->AvgTimePerFrame;
Expand Down

0 comments on commit dfc4c9b

Please sign in to comment.