data_streaming.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. This file is part of Telegram Desktop,
  3. the official desktop application for the Telegram messaging service.
  4. For license and copyright information please follow this link:
  5. https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
  6. */
  7. #pragma once
  8. #include "base/timer.h"
  9. class PhotoData;
  10. class DocumentData;
  11. namespace Media::Streaming {
  12. class Reader;
  13. class Document;
  14. } // namespace Media::Streaming
  15. namespace Data {
  16. class Session;
  17. struct FileOrigin;
  18. class Streaming final {
  19. public:
  20. explicit Streaming(not_null<Session*> owner);
  21. Streaming(const Streaming &other) = delete;
  22. Streaming &operator=(const Streaming &other) = delete;
  23. ~Streaming();
  24. using Reader = ::Media::Streaming::Reader;
  25. using Document = ::Media::Streaming::Document;
  26. [[nodiscard]] std::shared_ptr<Reader> sharedReader(
  27. not_null<DocumentData*> document,
  28. FileOrigin origin,
  29. bool forceRemoteLoader = false);
  30. [[nodiscard]] std::shared_ptr<Document> sharedDocument(
  31. not_null<DocumentData*> document,
  32. FileOrigin origin);
  33. [[nodiscard]] std::shared_ptr<Document> sharedDocument(
  34. not_null<DocumentData*> quality,
  35. not_null<DocumentData*> original,
  36. HistoryItem *context,
  37. FileOrigin origin);
  38. [[nodiscard]] std::shared_ptr<Reader> sharedReader(
  39. not_null<PhotoData*> photo,
  40. FileOrigin origin,
  41. bool forceRemoteLoader = false);
  42. [[nodiscard]] std::shared_ptr<Document> sharedDocument(
  43. not_null<PhotoData*> photo,
  44. FileOrigin origin);
  45. void keepAlive(not_null<DocumentData*> document);
  46. void keepAlive(not_null<PhotoData*> photo);
  47. private:
  48. void clearKeptAlive();
  49. template <typename Data>
  50. [[nodiscard]] std::shared_ptr<Reader> sharedReader(
  51. base::flat_map<not_null<Data*>, std::weak_ptr<Reader>> &readers,
  52. not_null<Data*> data,
  53. FileOrigin origin,
  54. bool forceRemoteLoader = false);
  55. template <typename Data>
  56. [[nodiscard]] std::shared_ptr<Document> sharedDocument(
  57. base::flat_map<not_null<Data*>, std::weak_ptr<Document>> &documents,
  58. base::flat_map<not_null<Data*>, std::weak_ptr<Reader>> &readers,
  59. not_null<Data*> data,
  60. DocumentData *original,
  61. HistoryItem *context,
  62. FileOrigin origin);
  63. template <typename Data>
  64. void keepAlive(
  65. base::flat_map<not_null<Data*>, std::weak_ptr<Document>> &documents,
  66. not_null<Data*> data);
  67. const not_null<Session*> _owner;
  68. base::flat_map<
  69. not_null<DocumentData*>,
  70. std::weak_ptr<Reader>> _fileReaders;
  71. base::flat_map<
  72. not_null<DocumentData*>,
  73. std::weak_ptr<Document>> _fileDocuments;
  74. base::flat_map<not_null<PhotoData*>, std::weak_ptr<Reader>> _photoReaders;
  75. base::flat_map<
  76. not_null<PhotoData*>,
  77. std::weak_ptr<Document>> _photoDocuments;
  78. base::flat_map<std::shared_ptr<Document>, crl::time> _keptAlive;
  79. base::Timer _keptAliveTimer;
  80. };
  81. } // namespace Data