media_streaming_loader.h 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 "media/streaming/media_streaming_common.h"
  9. namespace Storage {
  10. class StreamedFileDownloader;
  11. } // namespace Storage
  12. namespace Media::Streaming {
  13. struct LoadedPart {
  14. int64 offset = 0;
  15. QByteArray bytes;
  16. static constexpr auto kFailedOffset = int64(-1);
  17. [[nodiscard]] bool valid(int64 size) const;
  18. };
  19. class Loader {
  20. public:
  21. static constexpr auto kPartSize = int64(128 * 1024);
  22. [[nodiscard]] virtual Storage::Cache::Key baseCacheKey() const = 0;
  23. [[nodiscard]] virtual int64 size() const = 0;
  24. virtual void load(int64 offset) = 0;
  25. virtual void cancel(int64 offset) = 0;
  26. virtual void resetPriorities() = 0;
  27. virtual void setPriority(int priority) = 0;
  28. virtual void stop() = 0;
  29. // Remove from queue if no requests are in progress.
  30. virtual void tryRemoveFromQueue() = 0;
  31. // Parts will be sent from the main thread.
  32. [[nodiscard]] virtual rpl::producer<LoadedPart> parts() const = 0;
  33. [[nodiscard]] virtual auto speedEstimate() const
  34. -> rpl::producer<SpeedEstimate> = 0;
  35. virtual void attachDownloader(
  36. not_null<Storage::StreamedFileDownloader*> downloader) = 0;
  37. virtual void clearAttachedDownloader() = 0;
  38. virtual ~Loader() = default;
  39. };
  40. class PriorityQueue {
  41. public:
  42. bool add(int64 value);
  43. bool remove(int64 value);
  44. void resetPriorities();
  45. [[nodiscard]] bool empty() const;
  46. [[nodiscard]] std::optional<int64> front() const;
  47. [[nodiscard]] std::optional<int64> take();
  48. [[nodiscard]] base::flat_set<int64> takeInRange(int64 from, int64 till);
  49. void clear();
  50. private:
  51. struct Entry {
  52. int64 value = 0;
  53. int priority = 0;
  54. };
  55. friend bool operator<(const Entry &a, const Entry &b);
  56. base::flat_set<Entry> _data;
  57. int _priority = 0;
  58. };
  59. } // namespace Media::Streaming