openhmd.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  1. // Copyright 2013, Fredrik Hultin.
  2. // Copyright 2013, Jakob Bornecrantz.
  3. // SPDX-License-Identifier: BSL-1.0
  4. /*
  5. * OpenHMD - Free and Open Source API and drivers for immersive technology.
  6. */
  7. /* Main Lib Implementation */
  8. #include "openhmdi.h"
  9. #include "shaders.h"
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <stdio.h>
  13. // Running automatic updates at 1000 Hz
  14. #define AUTOMATIC_UPDATE_SLEEP (1.0 / 1000.0)
  15. ohmd_context* OHMD_APIENTRY ohmd_ctx_create(void)
  16. {
  17. ohmd_context* ctx = calloc(1, sizeof(ohmd_context));
  18. if(!ctx){
  19. LOGE("could not allocate RAM for context");
  20. return NULL;
  21. }
  22. ohmd_monotonic_init(ctx);
  23. #if DRIVER_OCULUS_RIFT
  24. ctx->drivers[ctx->num_drivers++] = ohmd_create_oculus_rift_drv(ctx);
  25. #endif
  26. #if DRIVER_DEEPOON
  27. ctx->drivers[ctx->num_drivers++] = ohmd_create_deepoon_drv(ctx);
  28. #endif
  29. #if DRIVER_HTC_VIVE
  30. ctx->drivers[ctx->num_drivers++] = ohmd_create_htc_vive_drv(ctx);
  31. #endif
  32. #if DRIVER_WMR
  33. ctx->drivers[ctx->num_drivers++] = ohmd_create_wmr_drv(ctx);
  34. #endif
  35. #if DRIVER_PSVR
  36. ctx->drivers[ctx->num_drivers++] = ohmd_create_psvr_drv(ctx);
  37. #endif
  38. #if DRIVER_NOLO
  39. ctx->drivers[ctx->num_drivers++] = ohmd_create_nolo_drv(ctx);
  40. #endif
  41. #if DRIVER_XGVR
  42. ctx->drivers[ctx->num_drivers++] = ohmd_create_xgvr_drv(ctx);
  43. #endif
  44. #if DRIVER_ANDROID
  45. ctx->drivers[ctx->num_drivers++] = ohmd_create_android_drv(ctx);
  46. #endif
  47. #if DRIVER_EXTERNAL
  48. ctx->drivers[ctx->num_drivers++] = ohmd_create_external_drv(ctx);
  49. #endif
  50. // add dummy driver last to make it the lowest priority
  51. ctx->drivers[ctx->num_drivers++] = ohmd_create_dummy_drv(ctx);
  52. ctx->update_request_quit = false;
  53. return ctx;
  54. }
  55. void OHMD_APIENTRY ohmd_ctx_destroy(ohmd_context* ctx)
  56. {
  57. ctx->update_request_quit = true;
  58. for(int i = 0; i < ctx->num_active_devices; i++){
  59. ctx->active_devices[i]->close(ctx->active_devices[i]);
  60. }
  61. for(int i = 0; i < ctx->num_drivers; i++){
  62. ctx->drivers[i]->destroy(ctx->drivers[i]);
  63. }
  64. if(ctx->update_thread){
  65. ohmd_destroy_thread(ctx->update_thread);
  66. ohmd_destroy_mutex(ctx->update_mutex);
  67. }
  68. free(ctx);
  69. }
  70. void OHMD_APIENTRY ohmd_ctx_update(ohmd_context* ctx)
  71. {
  72. for(int i = 0; i < ctx->num_active_devices; i++){
  73. ohmd_device* dev = ctx->active_devices[i];
  74. if(!dev->settings.automatic_update && dev->update)
  75. dev->update(dev);
  76. ohmd_lock_mutex(ctx->update_mutex);
  77. dev->getf(dev, OHMD_POSITION_VECTOR, (float*)&dev->position);
  78. dev->getf(dev, OHMD_ROTATION_QUAT, (float*)&dev->rotation);
  79. ohmd_unlock_mutex(ctx->update_mutex);
  80. }
  81. }
  82. const char* OHMD_APIENTRY ohmd_ctx_get_error(ohmd_context* ctx)
  83. {
  84. return ctx->error_msg;
  85. }
  86. int OHMD_APIENTRY ohmd_ctx_probe(ohmd_context* ctx)
  87. {
  88. memset(&ctx->list, 0, sizeof(ohmd_device_list));
  89. for(int i = 0; i < ctx->num_drivers; i++){
  90. ctx->drivers[i]->get_device_list(ctx->drivers[i], &ctx->list);
  91. }
  92. return ctx->list.num_devices;
  93. }
  94. int OHMD_APIENTRY ohmd_gets(ohmd_string_description type, const char ** out)
  95. {
  96. switch(type){
  97. case OHMD_GLSL_DISTORTION_VERT_SRC:
  98. *out = distortion_vert;
  99. return OHMD_S_OK;
  100. case OHMD_GLSL_DISTORTION_FRAG_SRC:
  101. *out = distortion_frag;
  102. return OHMD_S_OK;
  103. case OHMD_GLSL_330_DISTORTION_VERT_SRC:
  104. *out = distortion_vert_330;
  105. return OHMD_S_OK;
  106. case OHMD_GLSL_330_DISTORTION_FRAG_SRC:
  107. *out = distortion_frag_330;
  108. return OHMD_S_OK;
  109. case OHMD_GLSL_ES_DISTORTION_VERT_SRC:
  110. *out = distortion_vert_es;
  111. return OHMD_S_OK;
  112. case OHMD_GLSL_ES_DISTORTION_FRAG_SRC:
  113. *out = distortion_frag_es;
  114. return OHMD_S_OK;
  115. default:
  116. return OHMD_S_UNSUPPORTED;
  117. }
  118. }
  119. const char* OHMD_APIENTRY ohmd_list_gets(ohmd_context* ctx, int index, ohmd_string_value type)
  120. {
  121. if(index >= ctx->list.num_devices)
  122. return NULL;
  123. switch(type){
  124. case OHMD_VENDOR:
  125. return ctx->list.devices[index].vendor;
  126. case OHMD_PRODUCT:
  127. return ctx->list.devices[index].product;
  128. case OHMD_PATH:
  129. return ctx->list.devices[index].path;
  130. default:
  131. return NULL;
  132. }
  133. }
  134. int OHMD_APIENTRY ohmd_list_geti(ohmd_context* ctx, int index, ohmd_int_value type, int* out)
  135. {
  136. if(index >= ctx->list.num_devices)
  137. return OHMD_S_INVALID_PARAMETER;
  138. switch(type){
  139. case OHMD_DEVICE_CLASS:
  140. *out = ctx->list.devices[index].device_class;
  141. return OHMD_S_OK;
  142. case OHMD_DEVICE_FLAGS:
  143. *out = ctx->list.devices[index].device_flags;
  144. return OHMD_S_OK;
  145. default:
  146. return OHMD_S_INVALID_PARAMETER;
  147. }
  148. }
  149. static unsigned int ohmd_update_thread(void* arg)
  150. {
  151. ohmd_context* ctx = (ohmd_context*)arg;
  152. while(!ctx->update_request_quit)
  153. {
  154. ohmd_lock_mutex(ctx->update_mutex);
  155. for(int i = 0; i < ctx->num_active_devices; i++){
  156. if(ctx->active_devices[i]->settings.automatic_update && ctx->active_devices[i]->update)
  157. ctx->active_devices[i]->update(ctx->active_devices[i]);
  158. }
  159. ohmd_unlock_mutex(ctx->update_mutex);
  160. ohmd_sleep(AUTOMATIC_UPDATE_SLEEP);
  161. }
  162. return 0;
  163. }
  164. static void ohmd_set_up_update_thread(ohmd_context* ctx)
  165. {
  166. if(!ctx->update_thread){
  167. ctx->update_mutex = ohmd_create_mutex(ctx);
  168. ctx->update_thread = ohmd_create_thread(ctx, ohmd_update_thread, ctx);
  169. }
  170. }
  171. ohmd_device* OHMD_APIENTRY ohmd_list_open_device_s(ohmd_context* ctx, int index, ohmd_device_settings* settings)
  172. {
  173. ohmd_lock_mutex(ctx->update_mutex);
  174. if(index >= 0 && index < ctx->list.num_devices){
  175. ohmd_device_desc* desc = &ctx->list.devices[index];
  176. ohmd_driver* driver = (ohmd_driver*)desc->driver_ptr;
  177. ohmd_device* device = driver->open_device(driver, desc);
  178. if (device == NULL) {
  179. ohmd_set_error(ctx, "Could not open device with index: %d, check device permissions?", index);
  180. ohmd_unlock_mutex(ctx->update_mutex);
  181. return NULL;
  182. }
  183. device->rotation_correction.w = 1;
  184. device->settings = *settings;
  185. device->ctx = ctx;
  186. device->active_device_idx = ctx->num_active_devices;
  187. ctx->active_devices[ctx->num_active_devices++] = device;
  188. ohmd_unlock_mutex(ctx->update_mutex);
  189. if(device->settings.automatic_update)
  190. ohmd_set_up_update_thread(ctx);
  191. return device;
  192. }
  193. ohmd_unlock_mutex(ctx->update_mutex);
  194. ohmd_set_error(ctx, "no device with index: %d", index);
  195. return NULL;
  196. }
  197. ohmd_device* OHMD_APIENTRY ohmd_list_open_device(ohmd_context* ctx, int index)
  198. {
  199. ohmd_device_settings settings;
  200. settings.automatic_update = true;
  201. return ohmd_list_open_device_s(ctx, index, &settings);
  202. }
  203. int OHMD_APIENTRY ohmd_close_device(ohmd_device* device)
  204. {
  205. ohmd_lock_mutex(device->ctx->update_mutex);
  206. ohmd_context* ctx = device->ctx;
  207. int idx = device->active_device_idx;
  208. memmove(ctx->active_devices + idx, ctx->active_devices + idx + 1,
  209. sizeof(ohmd_device*) * (ctx->num_active_devices - idx - 1));
  210. device->close(device);
  211. ctx->num_active_devices--;
  212. for(int i = idx; i < ctx->num_active_devices; i++)
  213. ctx->active_devices[i]->active_device_idx--;
  214. ohmd_unlock_mutex(ctx->update_mutex);
  215. return OHMD_S_OK;
  216. }
  217. static int ohmd_device_getf_unp(ohmd_device* device, ohmd_float_value type, float* out)
  218. {
  219. switch(type){
  220. case OHMD_LEFT_EYE_GL_MODELVIEW_MATRIX: {
  221. vec3f point = {{0, 0, 0}};
  222. quatf rot = device->rotation;
  223. oquatf_mult_me(&rot, &device->rotation_correction);
  224. mat4x4f orient, world_shift, result;
  225. omat4x4f_init_look_at(&orient, &rot, &point);
  226. omat4x4f_init_translate(&world_shift, -device->position.x +(device->properties.ipd / 2.0f), -device->position.y, -device->position.z);
  227. omat4x4f_mult(&world_shift, &orient, &result);
  228. omat4x4f_transpose(&result, (mat4x4f*)out);
  229. return OHMD_S_OK;
  230. }
  231. case OHMD_RIGHT_EYE_GL_MODELVIEW_MATRIX: {
  232. vec3f point = {{0, 0, 0}};
  233. quatf rot = device->rotation;
  234. oquatf_mult_me(&rot, &device->rotation_correction);
  235. mat4x4f orient, world_shift, result;
  236. omat4x4f_init_look_at(&orient, &rot, &point);
  237. omat4x4f_init_translate(&world_shift, -device->position.x + -(device->properties.ipd / 2.0f), -device->position.y, -device->position.z);
  238. omat4x4f_mult(&world_shift, &orient, &result);
  239. omat4x4f_transpose(&result, (mat4x4f*)out);
  240. return OHMD_S_OK;
  241. }
  242. case OHMD_LEFT_EYE_GL_PROJECTION_MATRIX:
  243. omat4x4f_transpose(&device->properties.proj_left, (mat4x4f*)out);
  244. return OHMD_S_OK;
  245. case OHMD_RIGHT_EYE_GL_PROJECTION_MATRIX:
  246. omat4x4f_transpose(&device->properties.proj_right, (mat4x4f*)out);
  247. return OHMD_S_OK;
  248. case OHMD_SCREEN_HORIZONTAL_SIZE:
  249. *out = device->properties.hsize;
  250. return OHMD_S_OK;
  251. case OHMD_SCREEN_VERTICAL_SIZE:
  252. *out = device->properties.vsize;
  253. return OHMD_S_OK;
  254. case OHMD_LENS_HORIZONTAL_SEPARATION:
  255. *out = device->properties.lens_sep;
  256. return OHMD_S_OK;
  257. case OHMD_LENS_VERTICAL_POSITION:
  258. *out = device->properties.lens_vpos;
  259. return OHMD_S_OK;
  260. case OHMD_RIGHT_EYE_FOV:
  261. case OHMD_LEFT_EYE_FOV:
  262. *out = device->properties.fov;
  263. return OHMD_S_OK;
  264. case OHMD_RIGHT_EYE_ASPECT_RATIO:
  265. case OHMD_LEFT_EYE_ASPECT_RATIO:
  266. *out = device->properties.ratio;
  267. return OHMD_S_OK;
  268. case OHMD_EYE_IPD:
  269. *out = device->properties.ipd;
  270. return OHMD_S_OK;
  271. case OHMD_PROJECTION_ZFAR:
  272. *out = device->properties.zfar;
  273. return OHMD_S_OK;
  274. case OHMD_PROJECTION_ZNEAR:
  275. *out = device->properties.znear;
  276. return OHMD_S_OK;
  277. case OHMD_ROTATION_QUAT:
  278. {
  279. *(quatf*)out = device->rotation;
  280. oquatf_mult_me((quatf*)out, &device->rotation_correction);
  281. return OHMD_S_OK;
  282. }
  283. case OHMD_POSITION_VECTOR:
  284. {
  285. *(vec3f*)out = device->position;
  286. for(int i = 0; i < 3; i++)
  287. out[i] += device->position_correction.arr[i];
  288. return OHMD_S_OK;
  289. }
  290. case OHMD_UNIVERSAL_DISTORTION_K: {
  291. for (int i = 0; i < 4; i++) {
  292. out[i] = device->properties.universal_distortion_k[i];
  293. }
  294. return OHMD_S_OK;
  295. }
  296. case OHMD_UNIVERSAL_ABERRATION_K: {
  297. for (int i = 0; i < 3; i++) {
  298. out[i] = device->properties.universal_aberration_k[i];
  299. }
  300. return OHMD_S_OK;
  301. }
  302. default:
  303. return device->getf(device, type, out);
  304. }
  305. }
  306. int OHMD_APIENTRY ohmd_device_getf(ohmd_device* device, ohmd_float_value type, float* out)
  307. {
  308. ohmd_lock_mutex(device->ctx->update_mutex);
  309. int ret = ohmd_device_getf_unp(device, type, out);
  310. ohmd_unlock_mutex(device->ctx->update_mutex);
  311. return ret;
  312. }
  313. int ohmd_device_setf_unp(ohmd_device* device, ohmd_float_value type, const float* in)
  314. {
  315. switch(type){
  316. case OHMD_EYE_IPD:
  317. device->properties.ipd = *in;
  318. return OHMD_S_OK;
  319. case OHMD_PROJECTION_ZFAR:
  320. device->properties.zfar = *in;
  321. return OHMD_S_OK;
  322. case OHMD_PROJECTION_ZNEAR:
  323. device->properties.znear = *in;
  324. return OHMD_S_OK;
  325. case OHMD_ROTATION_QUAT:
  326. {
  327. // adjust rotation correction
  328. quatf q;
  329. int ret = device->getf(device, OHMD_ROTATION_QUAT, (float*)&q);
  330. if(ret != 0){
  331. return ret;
  332. }
  333. oquatf_diff(&q, (quatf*)in, &device->rotation_correction);
  334. return OHMD_S_OK;
  335. }
  336. case OHMD_POSITION_VECTOR:
  337. {
  338. // adjust position correction
  339. vec3f v;
  340. int ret = device->getf(device, OHMD_POSITION_VECTOR, (float*)&v);
  341. if(ret != 0){
  342. return ret;
  343. }
  344. for(int i = 0; i < 3; i++)
  345. device->position_correction.arr[i] = in[i] - v.arr[i];
  346. return OHMD_S_OK;
  347. }
  348. case OHMD_EXTERNAL_SENSOR_FUSION:
  349. {
  350. if(device->setf == NULL)
  351. return OHMD_S_UNSUPPORTED;
  352. return device->setf(device, type, in);
  353. }
  354. default:
  355. return OHMD_S_INVALID_PARAMETER;
  356. }
  357. }
  358. int OHMD_APIENTRY ohmd_device_setf(ohmd_device* device, ohmd_float_value type, const float* in)
  359. {
  360. ohmd_lock_mutex(device->ctx->update_mutex);
  361. int ret = ohmd_device_setf_unp(device, type, in);
  362. ohmd_unlock_mutex(device->ctx->update_mutex);
  363. return ret;
  364. }
  365. int OHMD_APIENTRY ohmd_device_geti(ohmd_device* device, ohmd_int_value type, int* out)
  366. {
  367. switch(type){
  368. case OHMD_SCREEN_HORIZONTAL_RESOLUTION:
  369. *out = device->properties.hres;
  370. return OHMD_S_OK;
  371. case OHMD_SCREEN_VERTICAL_RESOLUTION:
  372. *out = device->properties.vres;
  373. return OHMD_S_OK;
  374. case OHMD_CONTROL_COUNT:
  375. *out = device->properties.control_count;
  376. return OHMD_S_OK;
  377. case OHMD_CONTROLS_TYPES:
  378. memcpy(out, device->properties.controls_types, device->properties.control_count * sizeof(int));
  379. return OHMD_S_OK;
  380. case OHMD_CONTROLS_HINTS:
  381. memcpy(out, device->properties.controls_hints, device->properties.control_count * sizeof(int));
  382. return OHMD_S_OK;
  383. default:
  384. return OHMD_S_INVALID_PARAMETER;
  385. }
  386. }
  387. int OHMD_APIENTRY ohmd_device_seti(ohmd_device* device, ohmd_int_value type, const int* in)
  388. {
  389. switch(type){
  390. default:
  391. return OHMD_S_INVALID_PARAMETER;
  392. }
  393. }
  394. int ohmd_device_set_data_unp(ohmd_device* device, ohmd_data_value type, const void* in)
  395. {
  396. switch(type){
  397. case OHMD_DRIVER_DATA:
  398. device->set_data(device, OHMD_DRIVER_DATA, in);
  399. return OHMD_S_OK;
  400. case OHMD_DRIVER_PROPERTIES:
  401. device->set_data(device, OHMD_DRIVER_PROPERTIES, in);
  402. return OHMD_S_OK;
  403. default:
  404. return OHMD_S_INVALID_PARAMETER;
  405. }
  406. }
  407. int OHMD_APIENTRY ohmd_device_set_data(ohmd_device* device, ohmd_data_value type, const void* in)
  408. {
  409. ohmd_lock_mutex(device->ctx->update_mutex);
  410. int ret = ohmd_device_set_data_unp(device, type, in);
  411. ohmd_unlock_mutex(device->ctx->update_mutex);
  412. return ret;
  413. }
  414. ohmd_status OHMD_APIENTRY ohmd_device_settings_seti(ohmd_device_settings* settings, ohmd_int_settings key, const int* val)
  415. {
  416. switch(key){
  417. case OHMD_IDS_AUTOMATIC_UPDATE:
  418. settings->automatic_update = val[0] == 0 ? false : true;
  419. return OHMD_S_OK;
  420. default:
  421. return OHMD_S_INVALID_PARAMETER;
  422. }
  423. }
  424. ohmd_device_settings* OHMD_APIENTRY ohmd_device_settings_create(ohmd_context* ctx)
  425. {
  426. return ohmd_alloc(ctx, sizeof(ohmd_device_settings));
  427. }
  428. void OHMD_APIENTRY ohmd_device_settings_destroy(ohmd_device_settings* settings)
  429. {
  430. free(settings);
  431. }
  432. void* ohmd_allocfn(ohmd_context* ctx, const char* e_msg, size_t size)
  433. {
  434. void* ret = calloc(1, size);
  435. if(!ret)
  436. ohmd_set_error(ctx, "%s", e_msg);
  437. return ret;
  438. }
  439. void ohmd_set_default_device_properties(ohmd_device_properties* props)
  440. {
  441. props->ipd = 0.061f;
  442. props->znear = 0.1f;
  443. props->zfar = 1000.0f;
  444. ohmd_set_universal_distortion_k(props, 0, 0, 0, 1);
  445. ohmd_set_universal_aberration_k(props, 1.0, 1.0, 1.0);
  446. }
  447. void ohmd_calc_default_proj_matrices(ohmd_device_properties* props)
  448. {
  449. mat4x4f proj_base; // base projection matrix
  450. // Calculate where the lens is on each screen,
  451. // and with the given value offset the projection matrix.
  452. float screen_center = props->hsize / 4.0f;
  453. float lens_shift = screen_center - props->lens_sep / 2.0f;
  454. // XXX: on CV1, props->hsize > props->lens_sep / 2.0,
  455. // I am not sure about the implications, but just taking the absolute
  456. // value of the offset seems to work.
  457. float proj_offset = fabs(4.0f * lens_shift / props->hsize);
  458. // Setup the base projection matrix. Each eye mostly have the
  459. // same projection matrix with the exception of the offset.
  460. omat4x4f_init_perspective(&proj_base, props->fov, props->ratio, props->znear, props->zfar);
  461. // Setup the two adjusted projection matrices. Each is setup to deal
  462. // with the fact that the lens is not in the center of the screen.
  463. // These matrices only change of the hardware changes, so static.
  464. mat4x4f translate;
  465. omat4x4f_init_translate(&translate, proj_offset, 0, 0);
  466. omat4x4f_mult(&translate, &proj_base, &props->proj_left);
  467. omat4x4f_init_translate(&translate, -proj_offset, 0, 0);
  468. omat4x4f_mult(&translate, &proj_base, &props->proj_right);
  469. }
  470. void ohmd_set_universal_distortion_k(ohmd_device_properties* props, float a, float b, float c, float d)
  471. {
  472. props->universal_distortion_k[0] = a;
  473. props->universal_distortion_k[1] = b;
  474. props->universal_distortion_k[2] = c;
  475. props->universal_distortion_k[3] = d;
  476. }
  477. void ohmd_set_universal_aberration_k(ohmd_device_properties* props, float r, float g, float b)
  478. {
  479. props->universal_aberration_k[0] = r;
  480. props->universal_aberration_k[1] = g;
  481. props->universal_aberration_k[2] = b;
  482. }
  483. uint64_t ohmd_monotonic_per_sec(ohmd_context* ctx)
  484. {
  485. return ctx->monotonic_ticks_per_sec;
  486. }
  487. /*
  488. * Grabbed from druntime, good thing it's BOOST v1.0 as well.
  489. */
  490. uint64_t ohmd_monotonic_conv(uint64_t ticks, uint64_t srcTicksPerSecond, uint64_t dstTicksPerSecond)
  491. {
  492. // This would be more straightforward with floating point arithmetic,
  493. // but we avoid it here in order to avoid the rounding errors that that
  494. // introduces. Also, by splitting out the units in this way, we're able
  495. // to deal with much larger values before running into problems with
  496. // integer overflow.
  497. return ticks / srcTicksPerSecond * dstTicksPerSecond +
  498. ticks % srcTicksPerSecond * dstTicksPerSecond / srcTicksPerSecond;
  499. }