1
0

hframe.cpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #include "hframe.h"
  2. #include "hlog.h"
  3. int HFrameBuf::push(HFrame* pFrame) {
  4. if (pFrame->isNull())
  5. return -10;
  6. frame_stats.push_cnt++;
  7. std::lock_guard<std::mutex> locker(mutex);
  8. if (frames.size() >= (size_t)cache_num) {
  9. hlogd("frame cache full!");
  10. if (policy == HFrameBuf::DISCARD) {
  11. return -20; // note: cache full, discard frame
  12. }
  13. HFrame& frame = frames.front();
  14. frames.pop_front();
  15. free(frame.buf.len);
  16. if (frame.userdata) {
  17. hlogd("free userdata");
  18. ::free(frame.userdata);
  19. frame.userdata = NULL;
  20. }
  21. }
  22. int ret = 0;
  23. if (isNull()) {
  24. resize(pFrame->buf.len * cache_num);
  25. ret = 1; // note: first push
  26. frame_info.w = pFrame->w;
  27. frame_info.h = pFrame->h;
  28. frame_info.type = pFrame->type;
  29. frame_info.bpp = pFrame->bpp;
  30. }
  31. HFrame frame;
  32. frame.buf.base = alloc(pFrame->buf.len);
  33. frame.buf.len = pFrame->buf.len;
  34. frame.copy(*pFrame);
  35. frames.push_back(frame);
  36. frame_stats.push_ok_cnt++;
  37. return ret;
  38. }
  39. int HFrameBuf::pop(HFrame* pFrame) {
  40. frame_stats.pop_cnt++;
  41. std::lock_guard<std::mutex> locker(mutex);
  42. if (isNull())
  43. return -10;
  44. if (frames.size() == 0) {
  45. hlogd("frame cache empty!");
  46. return -20;
  47. }
  48. HFrame& frame = frames.front();
  49. frames.pop_front();
  50. free(frame.buf.len);
  51. if (frame.isNull())
  52. return -30;
  53. pFrame->copy(frame);
  54. frame_stats.pop_ok_cnt++;
  55. return 0;
  56. }