mediahandlerelement.hpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. #ifndef RTC_MEDIA_HANDLER_ELEMENT_H
  19. #define RTC_MEDIA_HANDLER_ELEMENT_H
  20. #if RTC_ENABLE_MEDIA
  21. #include "common.hpp"
  22. #include "message.hpp"
  23. #include "rtp.hpp"
  24. namespace rtc {
  25. using ChainedMessagesProduct = shared_ptr<std::vector<binary_ptr>>;
  26. RTC_CPP_EXPORT ChainedMessagesProduct make_chained_messages_product();
  27. RTC_CPP_EXPORT ChainedMessagesProduct make_chained_messages_product(message_ptr msg);
  28. /// Ougoing messages
  29. struct RTC_CPP_EXPORT ChainedOutgoingProduct {
  30. ChainedOutgoingProduct(ChainedMessagesProduct messages = nullptr,
  31. message_ptr control = nullptr);
  32. const ChainedMessagesProduct messages;
  33. const message_ptr control;
  34. };
  35. /// Incoming messages with response
  36. struct RTC_CPP_EXPORT ChainedIncomingProduct {
  37. ChainedIncomingProduct(ChainedMessagesProduct incoming = nullptr,
  38. ChainedMessagesProduct outgoing = nullptr);
  39. const ChainedMessagesProduct incoming;
  40. const ChainedOutgoingProduct outgoing;
  41. };
  42. /// Incoming control messages with response
  43. struct RTC_CPP_EXPORT ChainedIncomingControlProduct {
  44. ChainedIncomingControlProduct(message_ptr incoming,
  45. optional<ChainedOutgoingProduct> outgoing = nullopt);
  46. const message_ptr incoming;
  47. const optional<ChainedOutgoingProduct> outgoing;
  48. };
  49. /// Chainable handler
  50. class RTC_CPP_EXPORT MediaHandlerElement
  51. : public std::enable_shared_from_this<MediaHandlerElement> {
  52. shared_ptr<MediaHandlerElement> upstream = nullptr;
  53. shared_ptr<MediaHandlerElement> downstream = nullptr;
  54. void prepareAndSendResponse(optional<ChainedOutgoingProduct> outgoing,
  55. std::function<bool(ChainedOutgoingProduct)> send);
  56. void removeFromChain();
  57. public:
  58. MediaHandlerElement();
  59. /// Creates response to incoming message
  60. /// @param messages Current repsonse
  61. /// @returns New response
  62. optional<ChainedOutgoingProduct> processOutgoingResponse(ChainedOutgoingProduct messages);
  63. // Process incoming and ougoing messages
  64. message_ptr formIncomingControlMessage(message_ptr message,
  65. std::function<bool(ChainedOutgoingProduct)> send);
  66. ChainedMessagesProduct
  67. formIncomingBinaryMessage(ChainedMessagesProduct messages,
  68. std::function<bool(ChainedOutgoingProduct)> send);
  69. message_ptr formOutgoingControlMessage(message_ptr message);
  70. optional<ChainedOutgoingProduct> formOutgoingBinaryMessage(ChainedOutgoingProduct product);
  71. /// Process current control message
  72. /// @param messages current message
  73. /// @returns Modified message and response
  74. virtual ChainedIncomingControlProduct processIncomingControlMessage(message_ptr messages);
  75. /// Process current control message
  76. /// @param messages current message
  77. /// @returns Modified message
  78. virtual message_ptr processOutgoingControlMessage(message_ptr messages);
  79. /// Process current binary message
  80. /// @param messages current message
  81. /// @returns Modified message and response
  82. virtual ChainedIncomingProduct processIncomingBinaryMessage(ChainedMessagesProduct messages);
  83. /// Process current binary message
  84. /// @param messages current message
  85. /// @param control current control message
  86. /// @returns Modified binary message and control message
  87. virtual ChainedOutgoingProduct processOutgoingBinaryMessage(ChainedMessagesProduct messages,
  88. message_ptr control);
  89. /// Set given element as upstream to this
  90. /// @param upstream Upstream element
  91. /// @returns Upstream element
  92. shared_ptr<MediaHandlerElement> chainWith(shared_ptr<MediaHandlerElement> upstream);
  93. /// Remove all downstream elements from chain
  94. void recursiveRemoveChain();
  95. };
  96. } // namespace rtc
  97. #endif // RTC_ENABLE_MEDIA
  98. #endif // RTC_MEDIA_HANDLER_ELEMENT_H