BufferNegotiator.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /* BufferNegotiator.cpp/Open GoPro, Version 2.0 (C) Copyright 2021 GoPro, Inc. (http://gopro.com/OpenGoPro). */
  2. /* This copyright was auto-generated on Sat Mar 5 01:05:52 UTC 2022 */
  3. #include "BufferNegotiator.h"
  4. #include "GoProStreamDemo.h"
  5. #include <mutex>
  6. #include <condition_variable>
  7. #include <algorithm>
  8. #include "stdafx.h"
  9. std::mutex locker;
  10. std::mutex inputlock;
  11. std::condition_variable sleepy;
  12. extern "C" uint8_t* video_dst_data[4];
  13. extern "C" long video_dst_bufsize;
  14. #define MAX_INPUT_BUF_SIZE 232768
  15. uint8_t inputbuffer[MAX_INPUT_BUF_SIZE] = { 0 };
  16. int inputbuffersize = 0;
  17. static int quit_now = 0;
  18. long GetBuffer(uint8_t* dest, long buflen)
  19. {
  20. long ret = 0;
  21. int blackbuf = 0;
  22. locker.lock();
  23. #ifndef USE_PREVIEW_STREAM
  24. //for 1080
  25. if (video_dst_bufsize == 0 ||
  26. (video_dst_data[0][0] == 0 //upper left
  27. && video_dst_data[0][1035840] == 0 //center
  28. && video_dst_data[0][1919] == 0 //upper right
  29. && video_dst_data[0][2071680] == 0 //lower left
  30. && video_dst_data[0][2073599] == 0)) //lower right
  31. blackbuf = 1;
  32. #else
  33. //for 720
  34. if (video_dst_bufsize == 0 ||
  35. (video_dst_data[0][0] == 0 //upper left
  36. && video_dst_data[0][460800] == 0 //center
  37. && video_dst_data[0][1279] == 0 //upper right
  38. && video_dst_data[0][920320] == 0 //lower left
  39. && video_dst_data[0][921599] == 0)) //lower right
  40. blackbuf = 1;
  41. #endif
  42. if (video_dst_bufsize>0 && buflen >= video_dst_bufsize && !blackbuf)
  43. {
  44. memcpy(dest, video_dst_data[0], video_dst_bufsize);
  45. ret = video_dst_bufsize;
  46. }
  47. else
  48. {
  49. ret = 0;
  50. }
  51. locker.unlock();
  52. return ret;
  53. }
  54. void LockBuffer()
  55. {
  56. locker.lock();
  57. }
  58. void UnlockBuffer()
  59. {
  60. locker.unlock();
  61. }
  62. /*
  63. This function stores the buffer for consumption by the decoding/demuxing thread
  64. */
  65. void WriteInputBuffer(uint8_t* buf, int buf_size)
  66. {
  67. inputlock.lock();
  68. if (buf_size <= MAX_INPUT_BUF_SIZE)
  69. {
  70. //buffer is full
  71. if (inputbuffersize + buf_size > MAX_INPUT_BUF_SIZE)
  72. {
  73. //clear the buffer
  74. inputbuffersize = 0;
  75. OutputDebugStringA("clearing the buffer\n");
  76. }
  77. if (buf[0] == 'G' && (buf_size % 188 == 0))
  78. {
  79. memcpy(&inputbuffer[inputbuffersize], buf, buf_size);
  80. inputbuffersize += buf_size;
  81. }
  82. sleepy.notify_all();
  83. }
  84. inputlock.unlock();
  85. }
  86. void StopInput()
  87. {
  88. inputlock.lock();
  89. quit_now = 1;
  90. sleepy.notify_all();
  91. inputlock.unlock();
  92. }
  93. void StartInput()
  94. {
  95. inputlock.lock();
  96. quit_now = 0;
  97. inputlock.unlock();
  98. }
  99. int ReadInputBuffer(uint8_t* buf, int buf_size)
  100. {
  101. if (quit_now)
  102. {
  103. return -1;
  104. }
  105. std::unique_lock<std::mutex> lck(inputlock);
  106. while (inputbuffersize == 0 && quit_now==0)
  107. {
  108. sleepy.wait(lck);
  109. }
  110. if (quit_now)
  111. {
  112. return -1;
  113. }
  114. int new_size = min(buf_size, inputbuffersize);
  115. /* copy internal buffer data to buf */
  116. memcpy(buf, inputbuffer, new_size);
  117. memmove(inputbuffer, &inputbuffer[new_size], inputbuffersize - new_size);
  118. inputbuffersize -= new_size;
  119. return new_size;
  120. }