ff-packet-queue.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * Copyright (c) 2015 John R. Bradley <[email protected]>
  3. *
  4. * Permission to use, copy, modify, and distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include "ff-packet-queue.h"
  17. bool packet_queue_init(struct ff_packet_queue *q)
  18. {
  19. memset(q, 0, sizeof(struct ff_packet_queue));
  20. if (pthread_mutex_init(&q->mutex, NULL) != 0)
  21. goto fail;
  22. if (pthread_cond_init(&q->cond, NULL) != 0)
  23. goto fail1;
  24. av_init_packet(&q->flush_packet);
  25. q->flush_packet.data = (unsigned char *) "FLUSH";
  26. return true;
  27. fail1:
  28. pthread_mutex_destroy(&q->mutex);
  29. fail:
  30. return false;
  31. }
  32. void packet_queue_abort(struct ff_packet_queue *q)
  33. {
  34. pthread_mutex_lock(&q->mutex);
  35. q->abort = true;
  36. pthread_cond_signal(&q->cond);
  37. pthread_mutex_unlock(&q->mutex);
  38. }
  39. void packet_queue_free(struct ff_packet_queue *q)
  40. {
  41. packet_queue_flush(q);
  42. pthread_mutex_destroy(&q->mutex);
  43. pthread_cond_destroy(&q->cond);
  44. av_free_packet(&q->flush_packet);
  45. }
  46. int packet_queue_put(struct ff_packet_queue *q, AVPacket *packet)
  47. {
  48. AVPacketList *new_packet;
  49. if (packet != &q->flush_packet && av_dup_packet(packet) < 0)
  50. return FF_PACKET_FAIL;
  51. new_packet = av_malloc(sizeof(AVPacketList));
  52. if (new_packet == NULL)
  53. return FF_PACKET_FAIL;
  54. new_packet->pkt = *packet;
  55. new_packet->next = NULL;
  56. pthread_mutex_lock(&q->mutex);
  57. if (q->last_packet == NULL)
  58. q->first_packet = new_packet;
  59. else
  60. q->last_packet->next = new_packet;
  61. q->last_packet = new_packet;
  62. q->count++;
  63. q->total_size += new_packet->pkt.size;
  64. pthread_cond_signal(&q->cond);
  65. pthread_mutex_unlock(&q->mutex);
  66. return FF_PACKET_SUCCESS;
  67. }
  68. int packet_queue_put_flush_packet(struct ff_packet_queue *q)
  69. {
  70. return packet_queue_put(q, &q->flush_packet);
  71. }
  72. int packet_queue_get(struct ff_packet_queue *q, AVPacket *packet, bool block)
  73. {
  74. AVPacketList *potential_packet;
  75. int return_status;
  76. pthread_mutex_lock(&q->mutex);
  77. while (true) {
  78. potential_packet = q->first_packet;
  79. if (potential_packet != NULL) {
  80. q->first_packet = potential_packet->next;
  81. if (q->first_packet == NULL)
  82. q->last_packet = NULL;
  83. q->count--;
  84. q->total_size -= potential_packet->pkt.size;
  85. *packet = potential_packet->pkt;
  86. av_free(potential_packet);
  87. return_status = FF_PACKET_SUCCESS;
  88. break;
  89. } else if (!block) {
  90. return_status = FF_PACKET_EMPTY;
  91. break;
  92. } else {
  93. pthread_cond_wait(&q->cond, &q->mutex);
  94. if (q->abort) {
  95. return_status = FF_PACKET_FAIL;
  96. break;
  97. }
  98. }
  99. }
  100. pthread_mutex_unlock(&q->mutex);
  101. return return_status;
  102. }
  103. void packet_queue_flush(struct ff_packet_queue *q)
  104. {
  105. AVPacketList *packet;
  106. pthread_mutex_lock(&q->mutex);
  107. for (packet = q->first_packet; packet != NULL;
  108. packet = q->first_packet) {
  109. q->first_packet = packet->next;
  110. av_free_packet(&packet->pkt);
  111. av_freep(&packet);
  112. }
  113. q->last_packet = q->first_packet = NULL;
  114. q->count = 0;
  115. q->total_size = 0;
  116. pthread_mutex_unlock(&q->mutex);
  117. }