media_streaming_loader_mtproto.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. #include "media/streaming/media_streaming_loader_mtproto.h"
  8. #include "apiwrap.h"
  9. #include "main/main_session.h"
  10. #include "storage/streamed_file_downloader.h"
  11. #include "storage/cache/storage_cache_types.h"
  12. namespace Media {
  13. namespace Streaming {
  14. namespace {
  15. constexpr auto kCheckStatsInterval = crl::time(1000);
  16. constexpr auto kInitialStatsWait = 5 * crl::time(1000);
  17. } // namespace
  18. LoaderMtproto::LoaderMtproto(
  19. not_null<Storage::DownloadManagerMtproto*> owner,
  20. const StorageFileLocation &location,
  21. int64 size,
  22. Data::FileOrigin origin)
  23. : DownloadMtprotoTask(owner, location, origin)
  24. , _size(size)
  25. , _api(&api().instance())
  26. , _statsTimer([=] { checkStats(); }) {
  27. }
  28. Storage::Cache::Key LoaderMtproto::baseCacheKey() const {
  29. return v::get<StorageFileLocation>(
  30. location().data
  31. ).bigFileBaseCacheKey();
  32. }
  33. int64 LoaderMtproto::size() const {
  34. return _size;
  35. }
  36. void LoaderMtproto::load(int64 offset) {
  37. crl::on_main(this, [=] {
  38. if (_downloader) {
  39. auto bytes = _downloader->readLoadedPart(offset);
  40. if (!bytes.isEmpty()) {
  41. cancelForOffset(offset);
  42. _parts.fire({ offset, std::move(bytes) });
  43. return;
  44. }
  45. }
  46. if (haveSentRequestForOffset(offset)) {
  47. return;
  48. } else if (_requested.add(offset)) {
  49. addToQueueWithPriority();
  50. }
  51. });
  52. }
  53. void LoaderMtproto::addToQueueWithPriority() {
  54. addToQueue(_priority);
  55. }
  56. void LoaderMtproto::stop() {
  57. crl::on_main(this, [=] {
  58. cancelAllRequests();
  59. _requested.clear();
  60. removeFromQueue();
  61. });
  62. }
  63. void LoaderMtproto::tryRemoveFromQueue() {
  64. crl::on_main(this, [=] {
  65. if (_requested.empty() && !haveSentRequests()) {
  66. removeFromQueue();
  67. }
  68. });
  69. }
  70. void LoaderMtproto::cancel(int64 offset) {
  71. crl::on_main(this, [=] {
  72. cancelForOffset(offset);
  73. });
  74. }
  75. void LoaderMtproto::cancelForOffset(int64 offset) {
  76. if (haveSentRequestForOffset(offset)) {
  77. cancelRequestForOffset(offset);
  78. if (!_requested.empty()) {
  79. addToQueueWithPriority();
  80. }
  81. } else {
  82. _requested.remove(offset);
  83. }
  84. }
  85. void LoaderMtproto::attachDownloader(
  86. not_null<Storage::StreamedFileDownloader*> downloader) {
  87. _downloader = downloader;
  88. }
  89. void LoaderMtproto::clearAttachedDownloader() {
  90. _downloader = nullptr;
  91. }
  92. void LoaderMtproto::resetPriorities() {
  93. crl::on_main(this, [=] {
  94. _requested.resetPriorities();
  95. });
  96. }
  97. void LoaderMtproto::setPriority(int priority) {
  98. if (_priority == priority) {
  99. return;
  100. }
  101. _priority = priority;
  102. if (haveSentRequests()) {
  103. addToQueueWithPriority();
  104. }
  105. }
  106. bool LoaderMtproto::readyToRequest() const {
  107. return !_requested.empty();
  108. }
  109. int64 LoaderMtproto::takeNextRequestOffset() {
  110. const auto offset = _requested.take();
  111. Assert(offset.has_value());
  112. const auto time = crl::now();
  113. if (!_firstRequestStart) {
  114. _firstRequestStart = time;
  115. }
  116. _stats.push_back({ .start = crl::now(), .offset = *offset });
  117. Ensures(offset.has_value());
  118. return *offset;
  119. }
  120. bool LoaderMtproto::feedPart(int64 offset, const QByteArray &bytes) {
  121. const auto time = crl::now();
  122. for (auto &entry : _stats) {
  123. if (entry.offset == offset && entry.start < time) {
  124. entry.end = time;
  125. if (!_statsTimer.isActive()) {
  126. const auto checkAt = std::max(
  127. time + kCheckStatsInterval,
  128. _firstRequestStart + kInitialStatsWait);
  129. _statsTimer.callOnce(checkAt - time);
  130. }
  131. break;
  132. }
  133. }
  134. _parts.fire({ offset, bytes });
  135. return true;
  136. }
  137. void LoaderMtproto::cancelOnFail() {
  138. _parts.fire({ LoadedPart::kFailedOffset });
  139. }
  140. rpl::producer<LoadedPart> LoaderMtproto::parts() const {
  141. return _parts.events();
  142. }
  143. rpl::producer<SpeedEstimate> LoaderMtproto::speedEstimate() const {
  144. return _speedEstimate.events();
  145. }
  146. void LoaderMtproto::checkStats() {
  147. const auto time = crl::now();
  148. const auto from = time - kInitialStatsWait;
  149. { // Erase all stats entries that are too old.
  150. for (auto i = begin(_stats); i != end(_stats);) {
  151. if (i->start >= from) {
  152. break;
  153. } else if (i->end && i->end < from) {
  154. i = _stats.erase(i);
  155. } else {
  156. ++i;
  157. }
  158. }
  159. }
  160. if (_stats.empty()) {
  161. return;
  162. }
  163. // Count duration for which at least one request was in progress.
  164. // This is the time we should consider for download speed.
  165. // We don't count time when no requests were in progress.
  166. auto durationCountedTill = _stats.front().start;
  167. auto duration = crl::time(0);
  168. auto received = int64(0);
  169. for (const auto &entry : _stats) {
  170. if (entry.start > durationCountedTill) {
  171. durationCountedTill = entry.start;
  172. }
  173. const auto till = entry.end ? entry.end : time;
  174. if (till > durationCountedTill) {
  175. duration += (till - durationCountedTill);
  176. durationCountedTill = till;
  177. }
  178. if (entry.end) {
  179. received += Storage::kDownloadPartSize;
  180. }
  181. }
  182. if (duration) {
  183. _speedEstimate.fire({
  184. .bytesPerSecond = int(std::clamp(
  185. int64(received * 1000 / duration),
  186. int64(0),
  187. int64(64 * 1024 * 1024))),
  188. .unreliable = (received < 3 * Storage::kDownloadPartSize),
  189. });
  190. }
  191. }
  192. } // namespace Streaming
  193. } // namespace Media