test-random.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #include <stdlib.h>
  2. #include <util/threading.h>
  3. #include <util/platform.h>
  4. #include <obs.h>
  5. struct random_tex {
  6. obs_source_t *source;
  7. os_event_t *stop_signal;
  8. pthread_t thread;
  9. bool initialized;
  10. };
  11. static const char *random_getname(void *unused)
  12. {
  13. UNUSED_PARAMETER(unused);
  14. return "20x20 Random Pixel Texture Source (Test)";
  15. }
  16. static void random_destroy(void *data)
  17. {
  18. struct random_tex *rt = data;
  19. if (rt) {
  20. if (rt->initialized) {
  21. os_event_signal(rt->stop_signal);
  22. pthread_join(rt->thread, NULL);
  23. }
  24. os_event_destroy(rt->stop_signal);
  25. bfree(rt);
  26. }
  27. }
  28. static inline void fill_texture(uint32_t *pixels)
  29. {
  30. size_t x, y;
  31. for (y = 0; y < 20; y++) {
  32. for (x = 0; x < 20; x++) {
  33. uint32_t pixel = 0;
  34. pixel |= (rand() % 256);
  35. pixel |= (rand() % 256) << 8;
  36. pixel |= (rand() % 256) << 16;
  37. //pixel |= (rand()%256) << 24;
  38. //pixel |= 0xFFFFFFFF;
  39. pixels[y * 20 + x] = pixel;
  40. }
  41. }
  42. }
  43. static void *video_thread(void *data)
  44. {
  45. struct random_tex *rt = data;
  46. uint32_t pixels[20 * 20];
  47. uint64_t cur_time = os_gettime_ns();
  48. struct obs_source_frame frame = {
  49. .data = {[0] = (uint8_t *)pixels},
  50. .linesize = {[0] = 20 * 4},
  51. .width = 20,
  52. .height = 20,
  53. .format = VIDEO_FORMAT_BGRX,
  54. };
  55. while (os_event_try(rt->stop_signal) == EAGAIN) {
  56. fill_texture(pixels);
  57. frame.timestamp = cur_time;
  58. obs_source_output_video(rt->source, &frame);
  59. os_sleepto_ns(cur_time += 250000000);
  60. }
  61. return NULL;
  62. }
  63. static void *random_create(obs_data_t *settings, obs_source_t *source)
  64. {
  65. struct random_tex *rt = bzalloc(sizeof(struct random_tex));
  66. rt->source = source;
  67. if (os_event_init(&rt->stop_signal, OS_EVENT_TYPE_MANUAL) != 0) {
  68. random_destroy(rt);
  69. return NULL;
  70. }
  71. if (pthread_create(&rt->thread, NULL, video_thread, rt) != 0) {
  72. random_destroy(rt);
  73. return NULL;
  74. }
  75. rt->initialized = true;
  76. UNUSED_PARAMETER(settings);
  77. UNUSED_PARAMETER(source);
  78. return rt;
  79. }
  80. struct obs_source_info test_random = {
  81. .id = "random",
  82. .type = OBS_SOURCE_TYPE_INPUT,
  83. .output_flags = OBS_SOURCE_ASYNC_VIDEO,
  84. .get_name = random_getname,
  85. .create = random_create,
  86. .destroy = random_destroy,
  87. };