mediahandlerelement.hpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /**
  2. * Copyright (c) 2020 Filip Klembara (in2core)
  3. *
  4. * This Source Code Form is subject to the terms of the Mozilla Public
  5. * License, v. 2.0. If a copy of the MPL was not distributed with this
  6. * file, You can obtain one at https://mozilla.org/MPL/2.0/.
  7. */
  8. #ifndef RTC_MEDIA_HANDLER_ELEMENT_H
  9. #define RTC_MEDIA_HANDLER_ELEMENT_H
  10. #if RTC_ENABLE_MEDIA
  11. #include "common.hpp"
  12. #include "message.hpp"
  13. #include "rtp.hpp"
  14. namespace rtc {
  15. using ChainedMessagesProduct = shared_ptr<std::vector<binary_ptr>>;
  16. RTC_CPP_EXPORT ChainedMessagesProduct make_chained_messages_product();
  17. RTC_CPP_EXPORT ChainedMessagesProduct make_chained_messages_product(message_ptr msg);
  18. /// Ougoing messages
  19. struct RTC_CPP_EXPORT ChainedOutgoingProduct {
  20. ChainedOutgoingProduct(ChainedMessagesProduct messages = nullptr,
  21. message_ptr control = nullptr);
  22. const ChainedMessagesProduct messages;
  23. const message_ptr control;
  24. };
  25. /// Incoming messages with response
  26. struct RTC_CPP_EXPORT ChainedIncomingProduct {
  27. ChainedIncomingProduct(ChainedMessagesProduct incoming = nullptr,
  28. ChainedMessagesProduct outgoing = nullptr);
  29. const ChainedMessagesProduct incoming;
  30. const ChainedOutgoingProduct outgoing;
  31. };
  32. /// Incoming control messages with response
  33. struct RTC_CPP_EXPORT ChainedIncomingControlProduct {
  34. ChainedIncomingControlProduct(message_ptr incoming,
  35. optional<ChainedOutgoingProduct> outgoing = nullopt);
  36. const message_ptr incoming;
  37. const optional<ChainedOutgoingProduct> outgoing;
  38. };
  39. /// Chainable handler
  40. class RTC_CPP_EXPORT MediaHandlerElement
  41. : public std::enable_shared_from_this<MediaHandlerElement> {
  42. shared_ptr<MediaHandlerElement> upstream = nullptr;
  43. shared_ptr<MediaHandlerElement> downstream = nullptr;
  44. void prepareAndSendResponse(optional<ChainedOutgoingProduct> outgoing,
  45. std::function<bool(ChainedOutgoingProduct)> send);
  46. void removeFromChain();
  47. public:
  48. MediaHandlerElement();
  49. /// Creates response to incoming message
  50. /// @param messages Current repsonse
  51. /// @returns New response
  52. optional<ChainedOutgoingProduct> processOutgoingResponse(ChainedOutgoingProduct messages);
  53. // Process incoming and ougoing messages
  54. message_ptr formIncomingControlMessage(message_ptr message,
  55. std::function<bool(ChainedOutgoingProduct)> send);
  56. ChainedMessagesProduct
  57. formIncomingBinaryMessage(ChainedMessagesProduct messages,
  58. std::function<bool(ChainedOutgoingProduct)> send);
  59. message_ptr formOutgoingControlMessage(message_ptr message);
  60. optional<ChainedOutgoingProduct> formOutgoingBinaryMessage(ChainedOutgoingProduct product);
  61. /// Process current control message
  62. /// @param messages current message
  63. /// @returns Modified message and response
  64. virtual ChainedIncomingControlProduct processIncomingControlMessage(message_ptr messages);
  65. /// Process current control message
  66. /// @param messages current message
  67. /// @returns Modified message
  68. virtual message_ptr processOutgoingControlMessage(message_ptr messages);
  69. /// Process current binary message
  70. /// @param messages current message
  71. /// @returns Modified message and response
  72. virtual ChainedIncomingProduct processIncomingBinaryMessage(ChainedMessagesProduct messages);
  73. /// Process current binary message
  74. /// @param messages current message
  75. /// @param control current control message
  76. /// @returns Modified binary message and control message
  77. virtual ChainedOutgoingProduct processOutgoingBinaryMessage(ChainedMessagesProduct messages,
  78. 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