sync-audio-buffering.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. #include <stdlib.h>
  2. #include <util/threading.h>
  3. #include <util/platform.h>
  4. #include <obs.h>
  5. struct buffering_async_sync_test {
  6. obs_source_t *source;
  7. os_event_t *stop_signal;
  8. pthread_t thread;
  9. bool initialized;
  10. bool buffer_audio;
  11. };
  12. #define CYCLE_COUNT 7
  13. static const double aud_rates[CYCLE_COUNT] = {
  14. 220.00 / 48000.0, /* A */
  15. 233.08 / 48000.0, /* A# */
  16. 246.94 / 48000.0, /* B */
  17. 261.63 / 48000.0, /* C */
  18. 277.18 / 48000.0, /* C# */
  19. 293.67 / 48000.0, /* D */
  20. 311.13 / 48000.0, /* D# */
  21. };
  22. #define MAKE_COL_CHAN(x, y) (((0xFF / 7) * x) << (y * 8))
  23. #define MAKE_GRAYSCALE(x) \
  24. (MAKE_COL_CHAN(x, 0) | MAKE_COL_CHAN(x, 1) | MAKE_COL_CHAN(x, 2))
  25. static const uint32_t vid_colors[CYCLE_COUNT] = {
  26. 0xFF000000,
  27. 0xFF000000 + MAKE_GRAYSCALE(1),
  28. 0xFF000000 + MAKE_GRAYSCALE(2),
  29. 0xFF000000 + MAKE_GRAYSCALE(3),
  30. 0xFF000000 + MAKE_GRAYSCALE(4),
  31. 0xFF000000 + MAKE_GRAYSCALE(5),
  32. 0xFF000000 + MAKE_GRAYSCALE(6),
  33. };
  34. #ifndef M_PI
  35. #define M_PI 3.1415926535897932384626433832795
  36. #endif
  37. #define M_PI_X2 M_PI * 2
  38. static const char *bast_getname(void *unused)
  39. {
  40. UNUSED_PARAMETER(unused);
  41. return "Audio Buffering Sync Test (Async Video/Audio Source)";
  42. }
  43. static void bast_destroy(void *data)
  44. {
  45. struct buffering_async_sync_test *bast = data;
  46. if (bast->initialized) {
  47. os_event_signal(bast->stop_signal);
  48. pthread_join(bast->thread, NULL);
  49. }
  50. os_event_destroy(bast->stop_signal);
  51. bfree(bast);
  52. }
  53. static inline void fill_texture(uint32_t *pixels, uint32_t color)
  54. {
  55. size_t x, y;
  56. for (y = 0; y < 20; y++) {
  57. for (x = 0; x < 20; x++) {
  58. pixels[y * 20 + x] = color;
  59. }
  60. }
  61. }
  62. static void *video_thread(void *data)
  63. {
  64. struct buffering_async_sync_test *bast = data;
  65. uint32_t sample_rate = audio_output_get_sample_rate(obs_get_audio());
  66. uint32_t *pixels = bmalloc(20 * 20 * sizeof(uint32_t));
  67. float *samples = bmalloc(sample_rate * sizeof(float));
  68. uint64_t cur_time = os_gettime_ns();
  69. int cur_vid_pos = 0;
  70. int cur_aud_pos = 0;
  71. double cos_val = 0.0;
  72. uint64_t start_time = cur_time;
  73. bool audio_buffering_enabled = false;
  74. struct obs_source_frame frame = {
  75. .data = {[0] = (uint8_t *)pixels},
  76. .linesize = {[0] = 20 * 4},
  77. .width = 20,
  78. .height = 20,
  79. .format = VIDEO_FORMAT_BGRX,
  80. };
  81. struct obs_source_audio audio = {
  82. .speakers = SPEAKERS_MONO,
  83. .data = {[0] = (uint8_t *)samples},
  84. .samples_per_sec = sample_rate,
  85. .frames = sample_rate / 4,
  86. .format = AUDIO_FORMAT_FLOAT,
  87. };
  88. while (os_event_try(bast->stop_signal) == EAGAIN) {
  89. fill_texture(pixels, vid_colors[cur_vid_pos]);
  90. if (!audio_buffering_enabled && bast->buffer_audio) {
  91. audio_buffering_enabled = true;
  92. blog(LOG_DEBUG, "okay, buffering audio: now");
  93. /* 1 second = 4 cycles when running at
  94. * 250ms per cycle */
  95. cur_aud_pos -= 4;
  96. if (cur_aud_pos < 0)
  97. cur_aud_pos += CYCLE_COUNT;
  98. }
  99. /* should cause approximately 750 milliseconds of audio
  100. * buffering */
  101. frame.timestamp = cur_time - start_time;
  102. audio.timestamp = cur_time - start_time -
  103. (audio_buffering_enabled ? 1000000000 : 0);
  104. const double rate = aud_rates[cur_aud_pos];
  105. for (size_t i = 0; i < sample_rate / 4; i++) {
  106. cos_val += rate * M_PI_X2;
  107. if (cos_val > M_PI_X2)
  108. cos_val -= M_PI_X2;
  109. samples[i] = (float)(cos(cos_val) * 0.5);
  110. }
  111. obs_source_output_video(bast->source, &frame);
  112. obs_source_output_audio(bast->source, &audio);
  113. os_sleepto_ns(cur_time += 250000000);
  114. if (++cur_vid_pos == CYCLE_COUNT)
  115. cur_vid_pos = 0;
  116. if (++cur_aud_pos == CYCLE_COUNT)
  117. cur_aud_pos = 0;
  118. }
  119. bfree(pixels);
  120. bfree(samples);
  121. return NULL;
  122. }
  123. static void bast_buffer_audio(void *data, obs_hotkey_id id,
  124. obs_hotkey_t *hotkey, bool pressed)
  125. {
  126. struct buffering_async_sync_test *bast = data;
  127. UNUSED_PARAMETER(id);
  128. UNUSED_PARAMETER(hotkey);
  129. if (pressed)
  130. bast->buffer_audio = true;
  131. }
  132. static void *bast_create(obs_data_t *settings, obs_source_t *source)
  133. {
  134. struct buffering_async_sync_test *bast = bzalloc(sizeof(*bast));
  135. bast->source = source;
  136. if (os_event_init(&bast->stop_signal, OS_EVENT_TYPE_MANUAL) != 0) {
  137. bast_destroy(bast);
  138. return NULL;
  139. }
  140. if (pthread_create(&bast->thread, NULL, video_thread, bast) != 0) {
  141. bast_destroy(bast);
  142. return NULL;
  143. }
  144. obs_hotkey_register_source(source, "AudioBufferingSyncTest.Buffer",
  145. "Buffer Audio", bast_buffer_audio, bast);
  146. bast->initialized = true;
  147. UNUSED_PARAMETER(settings);
  148. UNUSED_PARAMETER(source);
  149. return bast;
  150. }
  151. struct obs_source_info buffering_async_sync_test = {
  152. .id = "buffering_async_sync_test",
  153. .type = OBS_SOURCE_TYPE_INPUT,
  154. .output_flags = OBS_SOURCE_ASYNC_VIDEO | OBS_SOURCE_AUDIO,
  155. .get_name = bast_getname,
  156. .create = bast_create,
  157. .destroy = bast_destroy,
  158. };