shared-memory-queue.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. #include <windows.h>
  2. #include "shared-memory-queue.h"
  3. #include "tiny-nv12-scale.h"
  4. #define VIDEO_NAME L"OBSVirtualCamVideo"
  5. enum queue_type {
  6. SHARED_QUEUE_TYPE_VIDEO,
  7. };
  8. struct queue_header {
  9. volatile uint32_t write_idx;
  10. volatile uint32_t read_idx;
  11. volatile uint32_t state;
  12. uint32_t offsets[3];
  13. uint32_t type;
  14. uint32_t cx;
  15. uint32_t cy;
  16. uint64_t interval;
  17. uint32_t reserved[8];
  18. };
  19. struct video_queue {
  20. HANDLE handle;
  21. bool ready_to_read;
  22. struct queue_header *header;
  23. uint64_t *ts[3];
  24. uint8_t *frame[3];
  25. long last_inc;
  26. int dup_counter;
  27. bool is_writer;
  28. };
  29. #define ALIGN_SIZE(size, align) size = (((size) + (align - 1)) & (~(align - 1)))
  30. #define FRAME_HEADER_SIZE 32
  31. video_queue_t *video_queue_create(uint32_t cx, uint32_t cy, uint64_t interval)
  32. {
  33. struct video_queue vq = {0};
  34. struct video_queue *pvq;
  35. DWORD frame_size = cx * cy * 3 / 2;
  36. uint32_t offset_frame[3];
  37. DWORD size;
  38. size = sizeof(struct queue_header);
  39. ALIGN_SIZE(size, 32);
  40. offset_frame[0] = size;
  41. size += frame_size + FRAME_HEADER_SIZE;
  42. ALIGN_SIZE(size, 32);
  43. offset_frame[1] = size;
  44. size += frame_size + FRAME_HEADER_SIZE;
  45. ALIGN_SIZE(size, 32);
  46. offset_frame[2] = size;
  47. size += frame_size + FRAME_HEADER_SIZE;
  48. ALIGN_SIZE(size, 32);
  49. struct queue_header header = {0};
  50. header.state = SHARED_QUEUE_STATE_STARTING;
  51. header.cx = cx;
  52. header.cy = cy;
  53. header.interval = interval;
  54. vq.is_writer = true;
  55. for (size_t i = 0; i < 3; i++) {
  56. uint32_t off = offset_frame[i];
  57. header.offsets[i] = off;
  58. }
  59. /* fail if already in use */
  60. vq.handle = OpenFileMappingW(FILE_MAP_READ, false, VIDEO_NAME);
  61. if (vq.handle) {
  62. CloseHandle(vq.handle);
  63. return NULL;
  64. }
  65. vq.handle = CreateFileMappingW(INVALID_HANDLE_VALUE, NULL,
  66. PAGE_READWRITE, 0, size, VIDEO_NAME);
  67. if (!vq.handle) {
  68. return NULL;
  69. }
  70. vq.header = (struct queue_header *)MapViewOfFile(
  71. vq.handle, FILE_MAP_ALL_ACCESS, 0, 0, 0);
  72. memcpy(vq.header, &header, sizeof(header));
  73. for (size_t i = 0; i < 3; i++) {
  74. uint32_t off = offset_frame[i];
  75. vq.ts[i] = (uint64_t *)(((uint8_t *)vq.header) + off);
  76. vq.frame[i] = ((uint8_t *)vq.header) + off + FRAME_HEADER_SIZE;
  77. }
  78. pvq = malloc(sizeof(vq));
  79. memcpy(pvq, &vq, sizeof(vq));
  80. return pvq;
  81. }
  82. video_queue_t *video_queue_open()
  83. {
  84. struct video_queue vq = {0};
  85. vq.handle = OpenFileMappingW(FILE_MAP_READ, false, VIDEO_NAME);
  86. if (!vq.handle) {
  87. return NULL;
  88. }
  89. vq.header = (struct queue_header *)MapViewOfFile(
  90. vq.handle, FILE_MAP_READ, 0, 0, 0);
  91. struct video_queue *pvq = malloc(sizeof(vq));
  92. memcpy(pvq, &vq, sizeof(vq));
  93. return pvq;
  94. }
  95. void video_queue_close(video_queue_t *vq)
  96. {
  97. if (!vq) {
  98. return;
  99. }
  100. if (vq->is_writer) {
  101. vq->header->state = SHARED_QUEUE_STATE_STOPPING;
  102. }
  103. UnmapViewOfFile(vq->header);
  104. CloseHandle(vq->handle);
  105. free(vq);
  106. }
  107. void video_queue_get_info(video_queue_t *vq, uint32_t *cx, uint32_t *cy,
  108. uint64_t *interval)
  109. {
  110. struct queue_header *qh = vq->header;
  111. *cx = qh->cx;
  112. *cy = qh->cy;
  113. *interval = qh->interval;
  114. }
  115. #define get_idx(inc) ((unsigned long)inc % 3)
  116. void video_queue_write(video_queue_t *vq, uint8_t **data, uint32_t *linesize,
  117. uint64_t timestamp)
  118. {
  119. struct queue_header *qh = vq->header;
  120. long inc = ++qh->write_idx;
  121. unsigned long idx = get_idx(inc);
  122. size_t size = linesize[0] * qh->cy;
  123. *vq->ts[idx] = timestamp;
  124. memcpy(vq->frame[idx], data[0], size);
  125. memcpy(vq->frame[idx] + size, data[1], size / 2);
  126. qh->read_idx = inc;
  127. qh->state = SHARED_QUEUE_STATE_READY;
  128. }
  129. enum queue_state video_queue_state(video_queue_t *vq)
  130. {
  131. if (!vq) {
  132. return SHARED_QUEUE_STATE_INVALID;
  133. }
  134. enum queue_state state = (enum queue_state)vq->header->state;
  135. if (!vq->ready_to_read && state == SHARED_QUEUE_STATE_READY) {
  136. for (size_t i = 0; i < 3; i++) {
  137. size_t off = vq->header->offsets[i];
  138. vq->ts[i] = (uint64_t *)(((uint8_t *)vq->header) + off);
  139. vq->frame[i] = ((uint8_t *)vq->header) + off +
  140. FRAME_HEADER_SIZE;
  141. }
  142. vq->ready_to_read = true;
  143. }
  144. return state;
  145. }
  146. bool video_queue_read(video_queue_t *vq, nv12_scale_t *scale, void *dst,
  147. uint64_t *ts)
  148. {
  149. struct queue_header *qh = vq->header;
  150. long inc = qh->read_idx;
  151. if (qh->state == SHARED_QUEUE_STATE_STOPPING) {
  152. return false;
  153. }
  154. if (inc == vq->last_inc) {
  155. if (++vq->dup_counter == 10) {
  156. return false;
  157. }
  158. } else {
  159. vq->dup_counter = 0;
  160. vq->last_inc = inc;
  161. }
  162. unsigned long idx = get_idx(inc);
  163. *ts = *vq->ts[idx];
  164. nv12_do_scale(scale, dst, vq->frame[idx]);
  165. return true;
  166. }