mediachainablehandler.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /**
  2. * Copyright (c) 2020 Filip Klembara (in2core)
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version 2
  7. * of the License, or (at your option) any later version.
  8. *
  9. * This program 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
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #if RTC_ENABLE_MEDIA
  18. #include "mediachainablehandler.hpp"
  19. namespace rtc {
  20. MediaChainableHandler::MediaChainableHandler(shared_ptr<MediaHandlerRootElement> root): MediaHandler(), root(root), leaf(root) { }
  21. MediaChainableHandler::~MediaChainableHandler() {
  22. leaf->recursiveRemoveChain();
  23. }
  24. bool MediaChainableHandler::sendProduct(ChainedOutgoingProduct product) {
  25. bool result = true;
  26. if (product.control) {
  27. assert(product.control->type == Message::Control);
  28. auto sendResult = send(product.control);
  29. if(!sendResult) {
  30. LOG_DEBUG << "Failed to send control message";
  31. }
  32. result = result && sendResult;
  33. }
  34. if (product.messages) {
  35. auto messages = product.messages;
  36. for (unsigned i = 0; i < messages->size(); i++) {
  37. auto message = messages->at(i);
  38. if (!message) {
  39. LOG_DEBUG << "Invalid message to send " << i + 1 << "/" << messages->size();
  40. }
  41. auto sendResult = send(make_message(*message));
  42. if(!sendResult) {
  43. LOG_DEBUG << "Failed to send message " << i + 1 << "/" << messages->size();
  44. }
  45. result = result && sendResult;
  46. }
  47. }
  48. return result;
  49. }
  50. message_ptr MediaChainableHandler::handleIncomingBinary(message_ptr msg) {
  51. assert(msg->type == Message::Binary);
  52. auto messages = root->split(msg);
  53. auto incoming = leaf->formIncomingBinaryMessage(messages, [this](ChainedOutgoingProduct outgoing) {
  54. return sendProduct(outgoing);
  55. });
  56. if (incoming) {
  57. return root->reduce(incoming);
  58. } else {
  59. return nullptr;
  60. }
  61. }
  62. message_ptr MediaChainableHandler::handleIncomingControl(message_ptr msg) {
  63. assert(msg->type == Message::Control);
  64. auto incoming = leaf->formIncomingControlMessage(msg, [this](ChainedOutgoingProduct outgoing) {
  65. return sendProduct(outgoing);
  66. });
  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. std::lock_guard<std::mutex> guard(inoutMutex);
  117. if (ptr->type == Message::Binary) {
  118. return handleOutgoingBinary(ptr);
  119. } else if (ptr->type == Message::Control) {
  120. return handleOutgoingControl(ptr);
  121. }
  122. return ptr;
  123. }
  124. message_ptr MediaChainableHandler::incoming(message_ptr ptr) {
  125. if (!ptr) {
  126. LOG_ERROR << "Incoming message is nullptr, ignoring";
  127. return nullptr;
  128. }
  129. std::lock_guard<std::mutex> guard(inoutMutex);
  130. if (ptr->type == Message::Binary) {
  131. return handleIncomingBinary(ptr);
  132. } else if (ptr->type == Message::Control) {
  133. return handleIncomingControl(ptr);
  134. }
  135. return ptr;
  136. }
  137. bool MediaChainableHandler::send(message_ptr msg) {
  138. try {
  139. outgoingCallback(std::move(msg));
  140. return true;
  141. } catch (const std::exception &e) {
  142. LOG_DEBUG << "Send in RTCP chain handler failed: " << e.what();
  143. }
  144. return false;
  145. }
  146. void MediaChainableHandler::addToChain(shared_ptr<MediaHandlerElement> chainable) {
  147. assert(leaf);
  148. leaf = leaf->chainWith(chainable);
  149. }
  150. } // namespace rtc
  151. #endif /* RTC_ENABLE_MEDIA */