2
0

mediahandlerelement.hpp 4.1 KB

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