media_streaming_common.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 "data/data_audio_msg_id.h"
  9. #include "ui/image/image_prepare.h"
  10. #include "ui/rect_part.h"
  11. namespace Media {
  12. inline constexpr auto kTimeUnknown = std::numeric_limits<crl::time>::min();
  13. inline constexpr auto kDurationMax = crl::time(std::numeric_limits<int>::max());
  14. inline constexpr auto kDurationUnavailable = std::numeric_limits<crl::time>::max();
  15. namespace Audio {
  16. bool SupportsSpeedControl();
  17. } // namespace Audio
  18. namespace Streaming {
  19. inline bool SupportsSpeedControl() {
  20. return Media::Audio::SupportsSpeedControl();
  21. }
  22. class VideoTrack;
  23. class AudioTrack;
  24. enum class Mode {
  25. Both,
  26. Audio,
  27. Video,
  28. Inspection,
  29. };
  30. struct PlaybackOptions {
  31. Mode mode = Mode::Both;
  32. crl::time position = 0;
  33. crl::time durationOverride = 0;
  34. float64 speed = 1.; // Valid values between 0.5 and 2.
  35. AudioMsgId audioId;
  36. bool syncVideoByAudio = true;
  37. bool waitForMarkAsShown = false;
  38. bool hwAllowed = false;
  39. bool seekable = true;
  40. bool loop = false;
  41. };
  42. struct TrackState {
  43. crl::time position = kTimeUnknown;
  44. crl::time receivedTill = kTimeUnknown;
  45. crl::time duration = kTimeUnknown;
  46. };
  47. struct VideoInformation {
  48. TrackState state;
  49. QSize size;
  50. QImage cover;
  51. int rotation = 0;
  52. bool alpha = false;
  53. };
  54. struct AudioInformation {
  55. TrackState state;
  56. };
  57. struct Information {
  58. VideoInformation video;
  59. AudioInformation audio;
  60. int headerSize = 0;
  61. };
  62. template <typename Track>
  63. struct PreloadedUpdate {
  64. crl::time till = kTimeUnknown;
  65. };
  66. template <typename Track>
  67. struct PlaybackUpdate {
  68. crl::time position = kTimeUnknown;
  69. };
  70. using PreloadedVideo = PreloadedUpdate<VideoTrack>;
  71. using UpdateVideo = PlaybackUpdate<VideoTrack>;
  72. using PreloadedAudio = PreloadedUpdate<AudioTrack>;
  73. using UpdateAudio = PlaybackUpdate<AudioTrack>;
  74. struct WaitingForData {
  75. bool waiting = false;
  76. };
  77. struct SpeedEstimate {
  78. int bytesPerSecond = 0;
  79. bool unreliable = false;
  80. };
  81. struct MutedByOther {
  82. };
  83. struct Finished {
  84. };
  85. struct Update {
  86. std::variant<
  87. Information,
  88. PreloadedVideo,
  89. UpdateVideo,
  90. PreloadedAudio,
  91. UpdateAudio,
  92. WaitingForData,
  93. SpeedEstimate,
  94. MutedByOther,
  95. Finished> data;
  96. };
  97. enum class Error {
  98. OpenFailed,
  99. LoadFailed,
  100. InvalidData,
  101. NotStreamable,
  102. };
  103. struct FrameRequest {
  104. QSize resize;
  105. QSize outer;
  106. Images::CornersMaskRef rounding;
  107. QImage mask;
  108. QColor colored = QColor(0, 0, 0, 0);
  109. bool blurredBackground = false;
  110. bool requireARGB32 = true;
  111. bool keepAlpha = false;
  112. bool strict = true;
  113. static FrameRequest NonStrict() {
  114. auto result = FrameRequest();
  115. result.strict = false;
  116. return result;
  117. }
  118. [[nodiscard]] bool empty() const {
  119. return blurredBackground ? outer.isEmpty() : resize.isEmpty();
  120. }
  121. [[nodiscard]] bool operator==(const FrameRequest &other) const {
  122. return (resize == other.resize)
  123. && (outer == other.outer)
  124. && (rounding == other.rounding)
  125. && (mask.constBits() == other.mask.constBits())
  126. && (colored == other.colored)
  127. && (keepAlpha == other.keepAlpha)
  128. && (requireARGB32 == other.requireARGB32)
  129. && (blurredBackground == other.blurredBackground);
  130. }
  131. [[nodiscard]] bool operator!=(const FrameRequest &other) const {
  132. return !(*this == other);
  133. }
  134. [[nodiscard]] bool goodFor(const FrameRequest &other) const {
  135. return (blurredBackground == other.blurredBackground)
  136. && (requireARGB32 == other.requireARGB32)
  137. && (keepAlpha == other.keepAlpha)
  138. && (colored == other.colored)
  139. && ((strict && !other.strict) || (*this == other));
  140. }
  141. };
  142. enum class FrameFormat {
  143. None,
  144. ARGB32,
  145. YUV420,
  146. NV12,
  147. };
  148. struct FrameChannel {
  149. const void *data = nullptr;
  150. int stride = 0;
  151. };
  152. struct FrameYUV {
  153. QSize size;
  154. QSize chromaSize;
  155. FrameChannel y;
  156. FrameChannel u;
  157. FrameChannel v;
  158. };
  159. struct FrameWithInfo {
  160. QImage image;
  161. FrameYUV *yuv = nullptr;
  162. FrameFormat format = FrameFormat::None;
  163. int index = -1;
  164. bool alpha = false;
  165. };
  166. } // namespace Streaming
  167. } // namespace Media