2
0

mediachainablehandler.cpp 5.1 KB

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