test-random.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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(const char *locale)
  12. {
  13. UNUSED_PARAMETER(locale);
  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 = 0xFF000000;
  34. pixel |= (rand()%256);
  35. pixel |= (rand()%256) << 8;
  36. pixel |= (rand()%256) << 16;
  37. //pixel |= 0xFFFFFFFF;
  38. pixels[y*20 + x] = pixel;
  39. }
  40. }
  41. }
  42. static void *video_thread(void *data)
  43. {
  44. struct random_tex *rt = data;
  45. uint32_t pixels[20*20];
  46. uint64_t cur_time = os_gettime_ns();
  47. struct source_frame frame = {
  48. .data = {[0] = (uint8_t*)pixels},
  49. .linesize = {[0] = 20*4},
  50. .width = 20,
  51. .height = 20,
  52. .format = VIDEO_FORMAT_BGRX
  53. };
  54. while (os_event_try(rt->stop_signal) == EAGAIN) {
  55. fill_texture(pixels);
  56. frame.timestamp = cur_time;
  57. obs_source_output_video(rt->source, &frame);
  58. os_sleepto_ns(cur_time += 250000000);
  59. }
  60. return NULL;
  61. }
  62. static void *random_create(obs_data_t settings, obs_source_t source)
  63. {
  64. struct random_tex *rt = bzalloc(sizeof(struct random_tex));
  65. rt->source = source;
  66. if (os_event_init(&rt->stop_signal, OS_EVENT_TYPE_MANUAL) != 0) {
  67. random_destroy(rt);
  68. return NULL;
  69. }
  70. if (pthread_create(&rt->thread, NULL, video_thread, rt) != 0) {
  71. random_destroy(rt);
  72. return NULL;
  73. }
  74. rt->initialized = true;
  75. UNUSED_PARAMETER(settings);
  76. UNUSED_PARAMETER(source);
  77. return rt;
  78. }
  79. struct obs_source_info test_random = {
  80. .id = "random",
  81. .type = OBS_SOURCE_TYPE_INPUT,
  82. .output_flags = OBS_SOURCE_ASYNC_VIDEO,
  83. .getname = random_getname,
  84. .create = random_create,
  85. .destroy = random_destroy,
  86. };