media_stories_sibling.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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/stories/media_stories_sibling.h"
  8. #include "base/weak_ptr.h"
  9. #include "data/data_document.h"
  10. #include "data/data_document_media.h"
  11. #include "data/data_file_origin.h"
  12. #include "data/data_peer.h"
  13. #include "data/data_photo.h"
  14. #include "data/data_photo_media.h"
  15. #include "data/data_session.h"
  16. #include "lang/lang_keys.h"
  17. #include "main/main_session.h"
  18. #include "media/stories/media_stories_controller.h"
  19. #include "media/stories/media_stories_view.h"
  20. #include "media/streaming/media_streaming_instance.h"
  21. #include "media/streaming/media_streaming_player.h"
  22. #include "ui/painter.h"
  23. #include "styles/style_media_view.h"
  24. namespace Media::Stories {
  25. namespace {
  26. constexpr auto kGoodFadeDuration = crl::time(200);
  27. constexpr auto kSiblingFade = 0.5;
  28. constexpr auto kSiblingFadeOver = 0.4;
  29. constexpr auto kSiblingNameOpacity = 0.8;
  30. constexpr auto kSiblingNameOpacityOver = 1.;
  31. constexpr auto kSiblingScaleOver = 0.05;
  32. [[nodiscard]] StoryId LookupShownId(
  33. const Data::StoriesSource &source,
  34. StoryId suggestedId) {
  35. const auto i = suggestedId
  36. ? source.ids.lower_bound(Data::StoryIdDates{ suggestedId })
  37. : end(source.ids);
  38. return (i != end(source.ids) && i->id == suggestedId)
  39. ? suggestedId
  40. : source.toOpen().id;
  41. }
  42. } // namespace
  43. class Sibling::Loader {
  44. public:
  45. virtual ~Loader() = default;
  46. virtual QImage blurred() = 0;
  47. virtual QImage good() = 0;
  48. };
  49. class Sibling::LoaderPhoto final : public Sibling::Loader {
  50. public:
  51. LoaderPhoto(
  52. not_null<PhotoData*> photo,
  53. Data::FileOrigin origin,
  54. Fn<void()> update);
  55. QImage blurred() override;
  56. QImage good() override;
  57. private:
  58. const not_null<PhotoData*> _photo;
  59. const Fn<void()> _update;
  60. std::shared_ptr<Data::PhotoMedia> _media;
  61. rpl::lifetime _waitingLoading;
  62. };
  63. class Sibling::LoaderVideo final
  64. : public Sibling::Loader
  65. , public base::has_weak_ptr {
  66. public:
  67. LoaderVideo(
  68. not_null<DocumentData*> video,
  69. Data::FileOrigin origin,
  70. Fn<void()> update);
  71. QImage blurred() override;
  72. QImage good() override;
  73. private:
  74. void waitForGoodThumbnail();
  75. bool updateAfterGoodCheck();
  76. void createStreamedPlayer();
  77. void streamedFailed();
  78. const not_null<DocumentData*> _video;
  79. const Data::FileOrigin _origin;
  80. const Fn<void()> _update;
  81. std::shared_ptr<Data::DocumentMedia> _media;
  82. std::unique_ptr<Streaming::Instance> _streamed;
  83. rpl::lifetime _waitingGoodGeneration;
  84. bool _checkingGoodInCache = false;
  85. bool _failed = false;
  86. };
  87. Sibling::LoaderPhoto::LoaderPhoto(
  88. not_null<PhotoData*> photo,
  89. Data::FileOrigin origin,
  90. Fn<void()> update)
  91. : _photo(photo)
  92. , _update(std::move(update))
  93. , _media(_photo->createMediaView()) {
  94. _photo->load(origin, LoadFromCloudOrLocal, true);
  95. }
  96. QImage Sibling::LoaderPhoto::blurred() {
  97. if (const auto image = _media->thumbnailInline()) {
  98. return image->original();
  99. }
  100. const auto ratio = style::DevicePixelRatio();
  101. auto result = QImage(ratio, ratio, QImage::Format_ARGB32_Premultiplied);
  102. result.fill(Qt::black);
  103. result.setDevicePixelRatio(ratio);
  104. return result;
  105. }
  106. QImage Sibling::LoaderPhoto::good() {
  107. if (const auto image = _media->image(Data::PhotoSize::Large)) {
  108. return image->original();
  109. } else if (!_waitingLoading) {
  110. _photo->session().downloaderTaskFinished(
  111. ) | rpl::start_with_next([=] {
  112. if (_media->loaded()) {
  113. _update();
  114. }
  115. }, _waitingLoading);
  116. }
  117. return QImage();
  118. }
  119. Sibling::LoaderVideo::LoaderVideo(
  120. not_null<DocumentData*> video,
  121. Data::FileOrigin origin,
  122. Fn<void()> update)
  123. : _video(video)
  124. , _origin(origin)
  125. , _update(std::move(update))
  126. , _media(_video->createMediaView()) {
  127. _media->goodThumbnailWanted();
  128. }
  129. QImage Sibling::LoaderVideo::blurred() {
  130. if (const auto image = _media->thumbnailInline()) {
  131. return image->original();
  132. }
  133. const auto ratio = style::DevicePixelRatio();
  134. auto result = QImage(ratio, ratio, QImage::Format_ARGB32_Premultiplied);
  135. result.fill(Qt::black);
  136. result.setDevicePixelRatio(ratio);
  137. return result;
  138. }
  139. QImage Sibling::LoaderVideo::good() {
  140. if (const auto image = _media->goodThumbnail()) {
  141. return image->original();
  142. } else if (!_video->goodThumbnailChecked()
  143. && !_video->goodThumbnailNoData()) {
  144. if (!_checkingGoodInCache) {
  145. waitForGoodThumbnail();
  146. }
  147. } else if (_failed) {
  148. return QImage();
  149. } else if (!_streamed) {
  150. createStreamedPlayer();
  151. } else if (_streamed->ready()) {
  152. return _streamed->info().video.cover;
  153. }
  154. return QImage();
  155. }
  156. void Sibling::LoaderVideo::createStreamedPlayer() {
  157. _streamed = std::make_unique<Streaming::Instance>(
  158. _video,
  159. _origin,
  160. [] {}); // waitingCallback
  161. _streamed->lockPlayer();
  162. _streamed->player().updates(
  163. ) | rpl::start_with_next_error([=](Streaming::Update &&update) {
  164. v::match(update.data, [&](Streaming::Information &update) {
  165. _update();
  166. }, [](const auto &update) {
  167. });
  168. }, [=](Streaming::Error &&error) {
  169. streamedFailed();
  170. }, _streamed->lifetime());
  171. if (_streamed->ready()) {
  172. _update();
  173. } else if (!_streamed->valid()) {
  174. streamedFailed();
  175. } else if (!_streamed->player().active()
  176. && !_streamed->player().finished()) {
  177. _streamed->play({
  178. .mode = Streaming::Mode::Video,
  179. });
  180. _streamed->pause();
  181. }
  182. }
  183. void Sibling::LoaderVideo::streamedFailed() {
  184. _failed = true;
  185. _streamed = nullptr;
  186. _update();
  187. }
  188. void Sibling::LoaderVideo::waitForGoodThumbnail() {
  189. _checkingGoodInCache = true;
  190. const auto weak = make_weak(this);
  191. _video->owner().cache().get({}, [=](const auto &) {
  192. crl::on_main([=] {
  193. if (const auto strong = weak.get()) {
  194. if (!strong->updateAfterGoodCheck()) {
  195. strong->_video->session().downloaderTaskFinished(
  196. ) | rpl::start_with_next([=] {
  197. strong->updateAfterGoodCheck();
  198. }, strong->_waitingGoodGeneration);
  199. }
  200. }
  201. });
  202. });
  203. }
  204. bool Sibling::LoaderVideo::updateAfterGoodCheck() {
  205. if (!_video->goodThumbnailChecked()
  206. && !_video->goodThumbnailNoData()) {
  207. return false;
  208. }
  209. _checkingGoodInCache = false;
  210. _waitingGoodGeneration.destroy();
  211. _update();
  212. return true;
  213. }
  214. Sibling::Sibling(
  215. not_null<Controller*> controller,
  216. const Data::StoriesSource &source,
  217. StoryId suggestedId)
  218. : _controller(controller)
  219. , _id{ source.peer->id, LookupShownId(source, suggestedId) }
  220. , _peer(source.peer) {
  221. checkStory();
  222. _goodShown.stop();
  223. }
  224. Sibling::~Sibling() = default;
  225. void Sibling::checkStory() {
  226. const auto maybeStory = _peer->owner().stories().lookup(_id);
  227. if (!maybeStory) {
  228. if (_blurred.isNull()) {
  229. setBlackThumbnail();
  230. if (maybeStory.error() == Data::NoStory::Unknown) {
  231. _peer->owner().stories().resolve(_id, crl::guard(this, [=] {
  232. checkStory();
  233. }));
  234. }
  235. }
  236. return;
  237. }
  238. const auto story = *maybeStory;
  239. const auto origin = Data::FileOrigin();
  240. v::match(story->media().data, [&](not_null<PhotoData*> photo) {
  241. _loader = std::make_unique<LoaderPhoto>(photo, origin, [=] {
  242. check();
  243. });
  244. }, [&](not_null<DocumentData*> document) {
  245. _loader = std::make_unique<LoaderVideo>(document, origin, [=] {
  246. check();
  247. });
  248. }, [&](v::null_t) {
  249. _loader = nullptr;
  250. });
  251. if (!_loader) {
  252. setBlackThumbnail();
  253. return;
  254. }
  255. _blurred = _loader->blurred();
  256. check();
  257. }
  258. void Sibling::setBlackThumbnail() {
  259. _blurred = QImage(
  260. st::storiesMaxSize,
  261. QImage::Format_ARGB32_Premultiplied);
  262. _blurred.fill(Qt::black);
  263. }
  264. FullStoryId Sibling::shownId() const {
  265. return _id;
  266. }
  267. not_null<PeerData*> Sibling::peer() const {
  268. return _peer;
  269. }
  270. bool Sibling::shows(
  271. const Data::StoriesSource &source,
  272. StoryId suggestedId) const {
  273. const auto fullId = FullStoryId{
  274. source.peer->id,
  275. LookupShownId(source, suggestedId),
  276. };
  277. return (_id == fullId);
  278. }
  279. SiblingView Sibling::view(const SiblingLayout &layout, float64 over) {
  280. const auto name = nameImage(layout);
  281. return {
  282. .image = _good.isNull() ? _blurred : _good,
  283. .layout = {
  284. .geometry = layout.geometry,
  285. .fade = kSiblingFade * (1 - over) + kSiblingFadeOver * over,
  286. .radius = st::storiesRadius,
  287. },
  288. .userpic = userpicImage(layout),
  289. .userpicPosition = layout.userpic.topLeft(),
  290. .name = name,
  291. .namePosition = namePosition(layout, name),
  292. .nameOpacity = (kSiblingNameOpacity * (1 - over)
  293. + kSiblingNameOpacityOver * over),
  294. .scale = 1. + (over * kSiblingScaleOver),
  295. };
  296. }
  297. QImage Sibling::userpicImage(const SiblingLayout &layout) {
  298. const auto ratio = style::DevicePixelRatio();
  299. const auto size = layout.userpic.width() * ratio;
  300. const auto key = _peer->userpicUniqueKey(_userpicView);
  301. if (_userpicImage.width() != size || _userpicKey != key) {
  302. _userpicKey = key;
  303. _userpicImage = PeerData::GenerateUserpicImage(
  304. _peer,
  305. _userpicView,
  306. size);
  307. _userpicImage.setDevicePixelRatio(ratio);
  308. }
  309. return _userpicImage;
  310. }
  311. QImage Sibling::nameImage(const SiblingLayout &layout) {
  312. if (_nameFontSize != layout.nameFontSize) {
  313. _nameFontSize = layout.nameFontSize;
  314. const auto family = 0; // Default font family.
  315. const auto font = style::font(
  316. _nameFontSize,
  317. style::FontFlag::Semibold,
  318. family);
  319. _name.reset();
  320. _nameStyle = std::make_unique<style::TextStyle>(style::TextStyle{
  321. .font = font,
  322. });
  323. };
  324. const auto text = _peer->isSelf()
  325. ? tr::lng_stories_my_name(tr::now)
  326. : _peer->shortName();
  327. if (_nameText != text) {
  328. _name.reset();
  329. _nameText = text;
  330. }
  331. if (!_name) {
  332. _nameAvailableWidth = 0;
  333. _name.emplace(*_nameStyle, _nameText);
  334. }
  335. const auto available = layout.nameBoundingRect.width();
  336. const auto wasCut = (_nameAvailableWidth < _name->maxWidth());
  337. const auto nowCut = (available < _name->maxWidth());
  338. if (_nameImage.isNull()
  339. || _nameAvailableWidth != layout.nameBoundingRect.width()) {
  340. _nameAvailableWidth = layout.nameBoundingRect.width();
  341. if (_nameImage.isNull() || nowCut || wasCut) {
  342. const auto w = std::min(_nameAvailableWidth, _name->maxWidth());
  343. const auto h = _nameStyle->font->height;
  344. const auto ratio = style::DevicePixelRatio();
  345. _nameImage = QImage(
  346. QSize(w, h) * ratio,
  347. QImage::Format_ARGB32_Premultiplied);
  348. _nameImage.setDevicePixelRatio(ratio);
  349. _nameImage.fill(Qt::transparent);
  350. auto p = Painter(&_nameImage);
  351. auto hq = PainterHighQualityEnabler(p);
  352. p.setFont(_nameStyle->font);
  353. p.setPen(Qt::white);
  354. _name->drawLeftElided(p, 0, 0, w, w);
  355. }
  356. }
  357. return _nameImage;
  358. }
  359. QPoint Sibling::namePosition(
  360. const SiblingLayout &layout,
  361. const QImage &image) const {
  362. const auto size = image.size() / image.devicePixelRatio();
  363. const auto width = size.width();
  364. const auto bounding = layout.nameBoundingRect;
  365. const auto left = layout.geometry.x()
  366. + (layout.geometry.width() - width) / 2;
  367. const auto top = bounding.y() + bounding.height() - size.height();
  368. if (left < bounding.x()) {
  369. return { bounding.x(), top };
  370. } else if (left + width > bounding.x() + bounding.width()) {
  371. return { bounding.x() + bounding.width() - width, top };
  372. }
  373. return { left, top };
  374. }
  375. void Sibling::check() {
  376. Expects(_loader != nullptr);
  377. auto good = _loader->good();
  378. if (good.isNull()) {
  379. return;
  380. }
  381. _loader = nullptr;
  382. _good = std::move(good);
  383. _goodShown.start([=] {
  384. _controller->repaintSibling(this);
  385. }, 0., 1., kGoodFadeDuration, anim::linear);
  386. }
  387. } // namespace Media::Stories