sync-async-source.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #include <stdlib.h>
  2. #include <util/threading.h>
  3. #include <util/platform.h>
  4. #include <obs.h>
  5. struct async_sync_test {
  6. obs_source_t *source;
  7. os_event_t *stop_signal;
  8. pthread_t thread;
  9. bool initialized;
  10. };
  11. /* middle C */
  12. static const double rate = 261.63 / 48000.0;
  13. #ifndef M_PI
  14. #define M_PI 3.1415926535897932384626433832795
  15. #endif
  16. #define M_PI_X2 M_PI * 2
  17. static const char *ast_getname(void *unused)
  18. {
  19. UNUSED_PARAMETER(unused);
  20. return "Sync Test (Async Video/Audio Source)";
  21. }
  22. static void ast_destroy(void *data)
  23. {
  24. struct async_sync_test *ast = data;
  25. if (ast->initialized) {
  26. os_event_signal(ast->stop_signal);
  27. pthread_join(ast->thread, NULL);
  28. }
  29. os_event_destroy(ast->stop_signal);
  30. bfree(ast);
  31. }
  32. static inline void fill_texture(uint32_t *pixels, uint32_t color)
  33. {
  34. size_t x, y;
  35. for (y = 0; y < 20; y++) {
  36. for (x = 0; x < 20; x++) {
  37. pixels[y * 20 + x] = color;
  38. }
  39. }
  40. }
  41. static void *video_thread(void *data)
  42. {
  43. struct async_sync_test *ast = data;
  44. uint32_t sample_rate = audio_output_get_sample_rate(obs_get_audio());
  45. uint32_t *pixels = bmalloc(20 * 20 * sizeof(uint32_t));
  46. float *samples = bmalloc(sample_rate * sizeof(float));
  47. uint64_t cur_time = os_gettime_ns();
  48. bool whitelist = false;
  49. double cos_val = 0.0;
  50. uint64_t start_time = cur_time;
  51. struct obs_source_frame frame = {
  52. .data = {[0] = (uint8_t *)pixels},
  53. .linesize = {[0] = 20 * 4},
  54. .width = 20,
  55. .height = 20,
  56. .format = VIDEO_FORMAT_BGRX,
  57. };
  58. struct obs_source_audio audio = {
  59. .speakers = SPEAKERS_MONO,
  60. .data = {[0] = (uint8_t *)samples},
  61. .samples_per_sec = sample_rate,
  62. .frames = sample_rate,
  63. .format = AUDIO_FORMAT_FLOAT,
  64. };
  65. while (os_event_try(ast->stop_signal) == EAGAIN) {
  66. fill_texture(pixels, whitelist ? 0xFFFFFFFF : 0xFF000000);
  67. frame.timestamp = cur_time - start_time;
  68. audio.timestamp = cur_time - start_time;
  69. if (whitelist) {
  70. for (size_t i = 0; i < sample_rate; i++) {
  71. cos_val += rate * M_PI_X2;
  72. if (cos_val > M_PI_X2)
  73. cos_val -= M_PI_X2;
  74. samples[i] = (float)(cos(cos_val) * 0.5);
  75. }
  76. } else {
  77. for (size_t i = 0; i < sample_rate; i++)
  78. samples[i] = 0.0f;
  79. }
  80. obs_source_output_video(ast->source, &frame);
  81. obs_source_output_audio(ast->source, &audio);
  82. os_sleepto_ns(cur_time += 1000000000);
  83. whitelist = !whitelist;
  84. }
  85. bfree(pixels);
  86. bfree(samples);
  87. return NULL;
  88. }
  89. static void *ast_create(obs_data_t *settings, obs_source_t *source)
  90. {
  91. struct async_sync_test *ast = bzalloc(sizeof(struct async_sync_test));
  92. ast->source = source;
  93. if (os_event_init(&ast->stop_signal, OS_EVENT_TYPE_MANUAL) != 0) {
  94. ast_destroy(ast);
  95. return NULL;
  96. }
  97. if (pthread_create(&ast->thread, NULL, video_thread, ast) != 0) {
  98. ast_destroy(ast);
  99. return NULL;
  100. }
  101. ast->initialized = true;
  102. UNUSED_PARAMETER(settings);
  103. UNUSED_PARAMETER(source);
  104. return ast;
  105. }
  106. struct obs_source_info async_sync_test = {
  107. .id = "async_sync_test",
  108. .type = OBS_SOURCE_TYPE_INPUT,
  109. .output_flags = OBS_SOURCE_ASYNC_VIDEO | OBS_SOURCE_AUDIO,
  110. .get_name = ast_getname,
  111. .create = ast_create,
  112. .destroy = ast_destroy,
  113. };