Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

pirms 10 mēnešiem
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. #include "aio-rtmp-transport.h"
  2. #include "aio-transport.h"
  3. #include "sys/sock.h"
  4. #include "sys/locker.h"
  5. #include "sys/system.h"
  6. #include "list.h"
  7. #include <errno.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <assert.h>
  11. #define VEC 8
  12. struct aio_rtmp_chunk_t
  13. {
  14. struct list_head node;
  15. uint8_t* data;
  16. size_t size;
  17. };
  18. struct aio_rtmp_transport_t
  19. {
  20. int code;
  21. int vecsize;
  22. socket_bufvec_t vec[VEC];
  23. aio_transport_t* aio;
  24. char buffer[2 * 1024];
  25. locker_t locker;
  26. int count; // list size
  27. size_t bytes; // list total bytes
  28. struct list_head root;
  29. struct aio_rtmp_handler_t handler;
  30. void* param;
  31. };
  32. static int aio_rtmp_send(struct aio_rtmp_transport_t* t)
  33. {
  34. struct list_head* p;
  35. struct aio_rtmp_chunk_t* c;
  36. locker_lock(&t->locker);
  37. if (0 != t->vecsize /*sending*/ || 0 == t->count /*no more data*/)
  38. {
  39. locker_unlock(&t->locker);
  40. return 0;
  41. }
  42. assert(0 == t->vecsize);
  43. for (p = t->root.next; t->vecsize < VEC && t->vecsize < t->count; t->vecsize++)
  44. {
  45. assert(p != &t->root);
  46. c = list_entry(p, struct aio_rtmp_chunk_t, node);
  47. socket_setbufvec(t->vec, t->vecsize, c->data, c->size);
  48. p = p->next;
  49. }
  50. assert(t->vecsize > 0);
  51. locker_unlock(&t->locker);
  52. return aio_transport_send_v(t->aio, t->vec, t->vecsize);
  53. }
  54. static void aio_rtmp_ondestroy(void* param)
  55. {
  56. struct list_head *n, *p;
  57. struct aio_rtmp_chunk_t* c;
  58. struct aio_rtmp_transport_t* t;
  59. t = (struct aio_rtmp_transport_t*)param;
  60. if(t->handler.ondestroy)
  61. t->handler.ondestroy(t->param);
  62. list_for_each_safe(p, n, &t->root)
  63. {
  64. c = list_entry(p, struct aio_rtmp_chunk_t, node);
  65. free(c);
  66. }
  67. locker_destroy(&t->locker);
  68. t->aio = NULL;
  69. free(t);
  70. }
  71. static void aio_rtmp_onrecv(void* param, int code, size_t bytes)
  72. {
  73. struct aio_rtmp_transport_t* t;
  74. t = (struct aio_rtmp_transport_t*)param;
  75. if (0 == code && 0 == bytes)
  76. code = ECONNRESET;
  77. t->handler.onrecv(t->param, code, t->buffer, bytes);
  78. if (0 == code)
  79. {
  80. code = aio_transport_recv(t->aio, t->buffer, sizeof(t->buffer));
  81. if (0 != code)
  82. t->handler.onrecv(t->param, code, t->buffer, 0);
  83. }
  84. }
  85. static void aio_rtmp_onsend(void* param, int code, size_t bytes)
  86. {
  87. struct aio_rtmp_chunk_t* c;
  88. struct aio_rtmp_transport_t* t;
  89. t = (struct aio_rtmp_transport_t*)param;
  90. if (0 == code)
  91. {
  92. locker_lock(&t->locker);
  93. for (assert(t->vecsize > 0); t->vecsize > 0; --t->vecsize)
  94. {
  95. assert(!list_empty(&t->root));
  96. c = list_entry(t->root.next, struct aio_rtmp_chunk_t, node);
  97. list_remove(t->root.next);
  98. free(c);
  99. t->count -= 1;
  100. }
  101. t->bytes -= bytes;
  102. locker_unlock(&t->locker);
  103. }
  104. t->handler.onsend(t->param, code, t->bytes); // callback
  105. if (0 == code)
  106. code = aio_rtmp_send(t); // send next
  107. if (0 != code)
  108. {
  109. t->code = code;
  110. t->handler.onsend(t->param, code, 0);
  111. }
  112. }
  113. struct aio_rtmp_transport_t* aio_rtmp_transport_create(aio_socket_t socket, struct aio_rtmp_handler_t* handler, void* param)
  114. {
  115. struct aio_rtmp_transport_t* t;
  116. struct aio_transport_handler_t h;
  117. h.ondestroy = aio_rtmp_ondestroy;
  118. h.onrecv = aio_rtmp_onrecv;
  119. h.onsend = aio_rtmp_onsend;
  120. t = (struct aio_rtmp_transport_t*)calloc(1, sizeof(*t));
  121. if (!t) return NULL;
  122. LIST_INIT_HEAD(&t->root);
  123. locker_create(&t->locker);
  124. memcpy(&t->handler, handler, sizeof(t->handler));
  125. t->param = param;
  126. t->aio = aio_transport_create2(socket, &h, t);
  127. return t;
  128. }
  129. int aio_rtmp_transport_destroy(struct aio_rtmp_transport_t* t)
  130. {
  131. return aio_transport_destroy(t->aio);
  132. }
  133. int aio_rtmp_transport_start(struct aio_rtmp_transport_t* t)
  134. {
  135. return aio_transport_recv(t->aio, t->buffer, sizeof(t->buffer));
  136. }
  137. int aio_rtmp_transport_send(struct aio_rtmp_transport_t* t, const void* header, size_t len, const void* payload, size_t bytes)
  138. {
  139. struct aio_rtmp_chunk_t* c;
  140. if (0 != t->code)
  141. return -ENOTCONN;
  142. c = (struct aio_rtmp_chunk_t*)malloc(sizeof(*c) + len + bytes + 12);
  143. if (NULL == c)
  144. return -ENOMEM;
  145. c->data = (uint8_t*)(c + 1);
  146. c->size = len + bytes;
  147. if(len > 0) memcpy(c->data, header, len);
  148. if (bytes > 0) memcpy(c->data + len, payload, bytes);
  149. locker_lock(&t->locker);
  150. t->count += 1;
  151. t->bytes += len + bytes;
  152. list_insert_before(&c->node, &t->root); // link to end
  153. locker_unlock(&t->locker);
  154. return 0 == aio_rtmp_send(t) ? (int)(len + bytes) : -1;
  155. }
  156. size_t aio_rtmp_transport_get_unsend(struct aio_rtmp_transport_t* t)
  157. {
  158. return t->bytes;
  159. }
  160. void aio_rtmp_transport_set_timeout(struct aio_rtmp_transport_t* t, int recv, int send)
  161. {
  162. aio_transport_set_timeout(t->aio, recv, send);
  163. }