mediachainablehandler.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 "mediachainablehandler.hpp"
  20. #include "impl/internals.hpp"
  21. #include <cassert>
  22. namespace rtc {
  23. MediaChainableHandler::MediaChainableHandler(shared_ptr<MediaHandlerRootElement> root)
  24. : MediaHandler(), root(root), leaf(root) {}
  25. MediaChainableHandler::~MediaChainableHandler() { leaf->recursiveRemoveChain(); }
  26. bool MediaChainableHandler::sendProduct(ChainedOutgoingProduct product) {
  27. bool result = true;
  28. if (product.control) {
  29. assert(product.control->type == Message::Control);
  30. auto sendResult = send(product.control);
  31. if (!sendResult) {
  32. LOG_DEBUG << "Failed to send control message";
  33. }
  34. result = result && sendResult;
  35. }
  36. if (product.messages) {
  37. auto messages = product.messages;
  38. for (unsigned i = 0; i < messages->size(); i++) {
  39. auto message = messages->at(i);
  40. if (!message) {
  41. LOG_DEBUG << "Invalid message to send " << i + 1 << "/" << messages->size();
  42. }
  43. auto sendResult = send(make_message(*message));
  44. if (!sendResult) {
  45. LOG_DEBUG << "Failed to send message " << i + 1 << "/" << messages->size();
  46. }
  47. result = result && sendResult;
  48. }
  49. }
  50. return result;
  51. }
  52. message_ptr MediaChainableHandler::handleIncomingBinary(message_ptr msg) {
  53. assert(msg->type == Message::Binary);
  54. auto messages = root->split(msg);
  55. auto incoming = getLeaf()->formIncomingBinaryMessage(
  56. messages, [this](ChainedOutgoingProduct outgoing) { return sendProduct(outgoing); });
  57. if (incoming) {
  58. return root->reduce(incoming);
  59. } else {
  60. return nullptr;
  61. }
  62. }
  63. message_ptr MediaChainableHandler::handleIncomingControl(message_ptr msg) {
  64. assert(msg->type == Message::Control);
  65. auto incoming = getLeaf()->formIncomingControlMessage(
  66. msg, [this](ChainedOutgoingProduct outgoing) { return sendProduct(outgoing); });
  67. assert(!incoming || incoming->type == Message::Control);
  68. return incoming;
  69. }
  70. message_ptr MediaChainableHandler::handleOutgoingBinary(message_ptr msg) {
  71. assert(msg->type == Message::Binary);
  72. auto messages = make_chained_messages_product(msg);
  73. auto optOutgoing = root->formOutgoingBinaryMessage(ChainedOutgoingProduct(messages));
  74. if (!optOutgoing.has_value()) {
  75. LOG_ERROR << "Generating outgoing message failed";
  76. return nullptr;
  77. }
  78. auto outgoing = optOutgoing.value();
  79. if (outgoing.control) {
  80. if (!send(outgoing.control)) {
  81. LOG_DEBUG << "Failed to send control message";
  82. }
  83. }
  84. auto lastMessage = outgoing.messages->back();
  85. if (!lastMessage) {
  86. LOG_DEBUG << "Invalid message to send";
  87. return nullptr;
  88. }
  89. for (unsigned i = 0; i < outgoing.messages->size() - 1; i++) {
  90. auto message = outgoing.messages->at(i);
  91. if (!message) {
  92. LOG_DEBUG << "Invalid message to send " << i + 1 << "/" << outgoing.messages->size();
  93. }
  94. if (!send(make_message(*message))) {
  95. LOG_DEBUG << "Failed to send message " << i + 1 << "/" << outgoing.messages->size();
  96. }
  97. }
  98. return make_message(*lastMessage);
  99. }
  100. message_ptr MediaChainableHandler::handleOutgoingControl(message_ptr msg) {
  101. assert(msg->type == Message::Control);
  102. auto outgoing = root->formOutgoingControlMessage(msg);
  103. assert(!outgoing || outgoing->type == Message::Control);
  104. if (!outgoing) {
  105. LOG_ERROR << "Generating outgoing control message failed";
  106. return nullptr;
  107. }
  108. return outgoing;
  109. }
  110. message_ptr MediaChainableHandler::outgoing(message_ptr ptr) {
  111. assert(ptr);
  112. if (!ptr) {
  113. LOG_ERROR << "Outgoing message is nullptr, ignoring";
  114. return nullptr;
  115. }
  116. if (ptr->type == Message::Binary) {
  117. return handleOutgoingBinary(ptr);
  118. } else if (ptr->type == Message::Control) {
  119. return handleOutgoingControl(ptr);
  120. }
  121. return ptr;
  122. }
  123. message_ptr MediaChainableHandler::incoming(message_ptr ptr) {
  124. if (!ptr) {
  125. LOG_ERROR << "Incoming message is nullptr, ignoring";
  126. return nullptr;
  127. }
  128. if (ptr->type == Message::Binary) {
  129. return handleIncomingBinary(ptr);
  130. } else if (ptr->type == Message::Control) {
  131. return handleIncomingControl(ptr);
  132. }
  133. return ptr;
  134. }
  135. bool MediaChainableHandler::send(message_ptr msg) {
  136. try {
  137. outgoingCallback(std::move(msg));
  138. return true;
  139. } catch (const std::exception &e) {
  140. LOG_DEBUG << "Send in RTCP chain handler failed: " << e.what();
  141. }
  142. return false;
  143. }
  144. shared_ptr<MediaHandlerElement> MediaChainableHandler::getLeaf() const {
  145. std::lock_guard lock(mutex);
  146. return leaf;
  147. }
  148. void MediaChainableHandler::addToChain(shared_ptr<MediaHandlerElement> chainable) {
  149. std::lock_guard lock(mutex);
  150. assert(leaf);
  151. leaf = leaf->chainWith(chainable);
  152. }
  153. } // namespace rtc
  154. #endif /* RTC_ENABLE_MEDIA */