sync-audio-buffering.c 4.5 KB

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