sync-audio-buffering.c 4.7 KB

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