data_audio_msg_id.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 "data/data_audio_msg_id.h"
  8. #include "data/data_document.h"
  9. namespace {
  10. constexpr auto kMinLengthForChangeablePlaybackSpeed = 20 * TimeId(60); // 20 minutes.
  11. } // namespace
  12. AudioMsgId::AudioMsgId() {
  13. }
  14. AudioMsgId::AudioMsgId(
  15. not_null<DocumentData*> audio,
  16. FullMsgId msgId,
  17. uint32 externalPlayId)
  18. : _audio(audio)
  19. , _contextId(msgId)
  20. , _externalPlayId(externalPlayId)
  21. , _changeablePlaybackSpeed(_audio->isVoiceMessage()
  22. || _audio->isVideoMessage()
  23. || (_audio->duration() >= kMinLengthForChangeablePlaybackSpeed)) {
  24. setTypeFromAudio();
  25. }
  26. uint32 AudioMsgId::CreateExternalPlayId() {
  27. static auto Result = uint32(0);
  28. return ++Result ? Result : ++Result;
  29. }
  30. AudioMsgId AudioMsgId::ForVideo() {
  31. auto result = AudioMsgId();
  32. result._externalPlayId = CreateExternalPlayId();
  33. result._type = Type::Video;
  34. return result;
  35. }
  36. void AudioMsgId::setTypeFromAudio() {
  37. if (_audio->isVoiceMessage() || _audio->isVideoMessage()) {
  38. _type = Type::Voice;
  39. } else if (_audio->isVideoFile()) {
  40. _type = Type::Video;
  41. } else if (_audio->isAudioFile()) {
  42. _type = Type::Song;
  43. } else {
  44. _type = Type::Unknown;
  45. }
  46. }
  47. AudioMsgId::Type AudioMsgId::type() const {
  48. return _type;
  49. }
  50. DocumentData *AudioMsgId::audio() const {
  51. return _audio;
  52. }
  53. FullMsgId AudioMsgId::contextId() const {
  54. return _contextId;
  55. }
  56. uint32 AudioMsgId::externalPlayId() const {
  57. return _externalPlayId;
  58. }
  59. bool AudioMsgId::changeablePlaybackSpeed() const {
  60. return _changeablePlaybackSpeed;
  61. }
  62. AudioMsgId::operator bool() const {
  63. return (_audio != nullptr) || (_externalPlayId != 0);
  64. }
  65. bool AudioMsgId::operator<(const AudioMsgId &other) const {
  66. if (quintptr(audio()) < quintptr(other.audio())) {
  67. return true;
  68. } else if (quintptr(other.audio()) < quintptr(audio())) {
  69. return false;
  70. } else if (contextId() < other.contextId()) {
  71. return true;
  72. } else if (other.contextId() < contextId()) {
  73. return false;
  74. }
  75. return (externalPlayId() < other.externalPlayId());
  76. }
  77. bool AudioMsgId::operator==(const AudioMsgId &other) const {
  78. return (audio() == other.audio())
  79. && (contextId() == other.contextId())
  80. && (externalPlayId() == other.externalPlayId());
  81. }
  82. bool AudioMsgId::operator!=(const AudioMsgId &other) const {
  83. return !(*this == other);
  84. }