mediahandlerelement.hpp 4.2 KB

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