mediahandlerelement.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /**
  2. * Copyright (c) 2020 Filip Klembara (in2core)
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with this library; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #if RTC_ENABLE_MEDIA
  19. #include "mediahandlerelement.hpp"
  20. #include "impl/internals.hpp"
  21. #include <cassert>
  22. namespace rtc {
  23. ChainedMessagesProduct make_chained_messages_product() {
  24. return std::make_shared<std::vector<binary_ptr>>();
  25. }
  26. ChainedMessagesProduct make_chained_messages_product(message_ptr msg) {
  27. std::vector<binary_ptr> msgs = {msg};
  28. return std::make_shared<std::vector<binary_ptr>>(msgs);
  29. }
  30. ChainedOutgoingProduct::ChainedOutgoingProduct(ChainedMessagesProduct messages, message_ptr control)
  31. : messages(messages), control(control) { }
  32. ChainedIncomingProduct::ChainedIncomingProduct(ChainedMessagesProduct incoming, ChainedMessagesProduct outgoing)
  33. : incoming(incoming), outgoing(outgoing) { }
  34. ChainedIncomingControlProduct::ChainedIncomingControlProduct(message_ptr incoming, optional<ChainedOutgoingProduct> outgoing)
  35. : incoming(incoming), outgoing(outgoing) { }
  36. MediaHandlerElement::MediaHandlerElement() { }
  37. void MediaHandlerElement::removeFromChain() {
  38. if (upstream) {
  39. upstream->downstream = downstream;
  40. }
  41. if (downstream) {
  42. downstream->upstream = upstream;
  43. }
  44. upstream = nullptr;
  45. downstream = nullptr;
  46. }
  47. void MediaHandlerElement::recursiveRemoveChain() {
  48. if (downstream) {
  49. // `recursiveRemoveChain` removes last strong reference to downstream element
  50. // we need to keep strong reference to prevent deallocation of downstream element
  51. // during `recursiveRemoveChain`
  52. auto strongDownstreamPtr = downstream;
  53. downstream->recursiveRemoveChain();
  54. }
  55. removeFromChain();
  56. }
  57. optional<ChainedOutgoingProduct> MediaHandlerElement::processOutgoingResponse(ChainedOutgoingProduct messages) {
  58. if (messages.messages) {
  59. if (upstream) {
  60. auto msgs = upstream->formOutgoingBinaryMessage(ChainedOutgoingProduct(messages.messages, messages.control));
  61. if (msgs.has_value()) {
  62. return msgs.value();
  63. } else {
  64. LOG_ERROR << "Generating outgoing message failed";
  65. return nullopt;
  66. }
  67. } else {
  68. return messages;
  69. }
  70. } else if (messages.control) {
  71. if (upstream) {
  72. auto control = upstream->formOutgoingControlMessage(messages.control);
  73. if (control) {
  74. return ChainedOutgoingProduct(nullptr, control);
  75. } else {
  76. LOG_ERROR << "Generating outgoing control message failed";
  77. return nullopt;
  78. }
  79. } else {
  80. return messages;
  81. }
  82. } else {
  83. return ChainedOutgoingProduct();
  84. }
  85. }
  86. void MediaHandlerElement::prepareAndSendResponse(optional<ChainedOutgoingProduct> outgoing, std::function<bool (ChainedOutgoingProduct)> send) {
  87. if (outgoing.has_value()) {
  88. auto message = outgoing.value();
  89. auto response = processOutgoingResponse(message);
  90. if (response.has_value()) {
  91. if(!send(response.value())) {
  92. LOG_DEBUG << "Send failed";
  93. }
  94. } else {
  95. LOG_DEBUG << "No response to send";
  96. }
  97. }
  98. }
  99. message_ptr MediaHandlerElement::formIncomingControlMessage(message_ptr message, std::function<bool (ChainedOutgoingProduct)> send) {
  100. assert(message);
  101. auto product = processIncomingControlMessage(message);
  102. prepareAndSendResponse(product.outgoing, send);
  103. if (product.incoming) {
  104. if (downstream) {
  105. return downstream->formIncomingControlMessage(product.incoming, send);
  106. } else {
  107. return product.incoming;
  108. }
  109. } else {
  110. return nullptr;
  111. }
  112. }
  113. ChainedMessagesProduct MediaHandlerElement::formIncomingBinaryMessage(ChainedMessagesProduct messages, std::function<bool (ChainedOutgoingProduct)> send) {
  114. assert(messages && !messages->empty());
  115. auto product = processIncomingBinaryMessage(messages);
  116. prepareAndSendResponse(product.outgoing, send);
  117. if (product.incoming) {
  118. if (downstream) {
  119. return downstream->formIncomingBinaryMessage(product.incoming, send);
  120. } else {
  121. return product.incoming;
  122. }
  123. } else {
  124. return nullptr;
  125. }
  126. }
  127. message_ptr MediaHandlerElement::formOutgoingControlMessage(message_ptr message) {
  128. assert(message);
  129. auto newMessage = processOutgoingControlMessage(message);
  130. assert(newMessage);
  131. if(!newMessage) {
  132. LOG_ERROR << "Failed to generate outgoing message";
  133. return nullptr;
  134. }
  135. if (upstream) {
  136. return upstream->formOutgoingControlMessage(newMessage);
  137. } else {
  138. return newMessage;
  139. }
  140. }
  141. optional<ChainedOutgoingProduct> MediaHandlerElement::formOutgoingBinaryMessage(ChainedOutgoingProduct product) {
  142. assert(product.messages && !product.messages->empty());
  143. auto newProduct = processOutgoingBinaryMessage(product.messages, product.control);
  144. assert(!product.control || newProduct.control);
  145. assert(newProduct.messages && !newProduct.messages->empty());
  146. if (product.control && !newProduct.control) {
  147. LOG_ERROR << "Outgoing message must not remove control message";
  148. return nullopt;
  149. }
  150. if (!newProduct.messages || newProduct.messages->empty()) {
  151. LOG_ERROR << "Failed to generate message";
  152. return nullopt;
  153. }
  154. if (upstream) {
  155. return upstream->formOutgoingBinaryMessage(newProduct);
  156. } else {
  157. return newProduct;
  158. }
  159. }
  160. ChainedIncomingControlProduct MediaHandlerElement::processIncomingControlMessage(message_ptr messages) {
  161. return {messages};
  162. }
  163. message_ptr MediaHandlerElement::processOutgoingControlMessage(message_ptr messages) {
  164. return messages;
  165. }
  166. ChainedIncomingProduct MediaHandlerElement::processIncomingBinaryMessage(ChainedMessagesProduct messages) {
  167. return {messages};
  168. }
  169. ChainedOutgoingProduct MediaHandlerElement::processOutgoingBinaryMessage(ChainedMessagesProduct messages, message_ptr control) {
  170. return {messages, control};
  171. }
  172. shared_ptr<MediaHandlerElement> MediaHandlerElement::chainWith(shared_ptr<MediaHandlerElement> upstream) {
  173. assert(this->upstream == nullptr);
  174. assert(upstream->downstream == nullptr);
  175. this->upstream = upstream;
  176. upstream->downstream = shared_from_this();
  177. return upstream;
  178. }
  179. } // namespace rtc
  180. #endif /* RTC_ENABLE_MEDIA */