25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

129 satır
2.2KB

  1. #include "avpacket-queue.h"
  2. #include "sys/sync.hpp"
  3. #include <queue>
  4. struct avpacket_queue_t
  5. {
  6. std::queue<avpacket_t*>::size_type maxsize;
  7. std::queue<avpacket_t*> q;
  8. ThreadLocker locker;
  9. ThreadEvent event;
  10. };
  11. struct avpacket_queue_t* avpacket_queue_create(int size)
  12. {
  13. struct avpacket_queue_t* q = new struct avpacket_queue_t;
  14. q->maxsize = size;
  15. return q;
  16. }
  17. void avpacket_queue_destroy(struct avpacket_queue_t* q)
  18. {
  19. avpacket_queue_clear(q);
  20. delete q;
  21. }
  22. void avpacket_queue_clear(struct avpacket_queue_t* q)
  23. {
  24. AutoThreadLocker locker(q->locker);
  25. while (!q->q.empty())
  26. {
  27. struct avpacket_t* pkt = q->q.front();
  28. avpacket_release(pkt);
  29. q->q.pop();
  30. }
  31. }
  32. int avpacket_queue_count(struct avpacket_queue_t* q)
  33. {
  34. AutoThreadLocker locker(q->locker);
  35. return (int)q->q.size();
  36. }
  37. int avpacket_queue_pop(struct avpacket_queue_t* q)
  38. {
  39. struct avpacket_t* pkt;
  40. {
  41. AutoThreadLocker locker(q->locker);
  42. if (q->q.empty())
  43. return -1;
  44. pkt = q->q.front();
  45. q->q.pop();
  46. q->event.Signal();
  47. }
  48. avpacket_release(pkt);
  49. return 0;
  50. }
  51. struct avpacket_t* avpacket_queue_front(struct avpacket_queue_t* q)
  52. {
  53. struct avpacket_t* pkt;
  54. AutoThreadLocker locker(q->locker);
  55. if (q->q.empty())
  56. return NULL;
  57. pkt = q->q.front();
  58. avpacket_addref(pkt);
  59. return pkt;
  60. }
  61. int avpacket_queue_push(struct avpacket_queue_t* q, struct avpacket_t* pkt)
  62. {
  63. AutoThreadLocker locker(q->locker);
  64. if (q->q.size() >= q->maxsize)
  65. return -1;
  66. avpacket_addref(pkt);
  67. q->q.push(pkt);
  68. q->event.Signal();
  69. return 0;
  70. }
  71. struct avpacket_t* avpacket_queue_front_wait(struct avpacket_queue_t* q, int ms)
  72. {
  73. q->locker.Lock();
  74. if (q->q.empty())
  75. {
  76. q->locker.Unlock();
  77. if (0 != q->event.TimeWait(ms))
  78. return NULL;
  79. q->locker.Lock();
  80. }
  81. if (q->q.empty())
  82. {
  83. q->locker.Unlock();
  84. return NULL;
  85. }
  86. struct avpacket_t* pkt = q->q.front();
  87. avpacket_addref(pkt);
  88. q->locker.Unlock();
  89. return pkt;
  90. }
  91. int avpacket_queue_push_wait(struct avpacket_queue_t* q, struct avpacket_t* pkt, int ms)
  92. {
  93. q->locker.Lock();
  94. if (q->q.size() >= q->maxsize)
  95. {
  96. q->locker.Unlock();
  97. if (0 != q->event.TimeWait(ms))
  98. return -1;
  99. q->locker.Lock();
  100. }
  101. if (q->q.size() >= q->maxsize)
  102. {
  103. q->locker.Unlock();
  104. return -1;
  105. }
  106. avpacket_addref(pkt);
  107. q->q.push(pkt);
  108. q->locker.Unlock();
  109. return 0;
  110. }