MediaBrowserVideo.m 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2014-2020 Threema GmbH
  8. //
  9. // This program is free software: you can redistribute it and/or modify
  10. // it under the terms of the GNU Affero General Public License, version 3,
  11. // as published by the Free Software Foundation.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU Affero General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Affero General Public License
  19. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. #import "MediaBrowserVideo.h"
  21. #import <AVFoundation/AVFoundation.h>
  22. #import <MediaPlayer/MediaPlayer.h>
  23. #import "VideoMessage.h"
  24. #import "VideoMessageLoader.h"
  25. #import "NSString+Hex.m"
  26. @interface MediaBrowserVideo ()
  27. @property VideoMessage *message;
  28. @end
  29. @implementation MediaBrowserVideo
  30. @synthesize underlyingImage = _underlyingImage; // synth property from protocol
  31. + (instancetype)videoWithThumbnail:(UIImage *)image {
  32. MediaBrowserVideo *video = [[self alloc] initWithImage:image];
  33. return video;
  34. }
  35. - (instancetype)initWithImage:(UIImage *)image
  36. {
  37. self = [super init];
  38. if (self) {
  39. _image = image;
  40. _isUtiPreview = false;
  41. }
  42. return self;
  43. }
  44. - (void)setSourceReference:(id)sourceReference {
  45. _message = (VideoMessage *)sourceReference;
  46. }
  47. -(id)sourceReference {
  48. return _message;
  49. }
  50. - (void)play {
  51. if (_delegate) {
  52. [_delegate playVideo: self];
  53. }
  54. }
  55. #pragma mark - MWPhoto Protocol Methods
  56. -(BOOL)showControls {
  57. return YES;
  58. }
  59. -(void)handleSingleTap:(CGPoint)touchPoint {
  60. if (_message.video != nil) {
  61. [self play];
  62. } else {
  63. [self loadUnderlyingImageAndNotify];
  64. }
  65. }
  66. -(BOOL)canScaleImage {
  67. return NO;
  68. }
  69. -(NSURL *)urlForExportData:(NSString *)tmpFileName {
  70. if (_message == nil || _message.video == nil) {
  71. return nil;
  72. }
  73. NSString *fileName = [_message getFilename];
  74. if (fileName == nil) {
  75. fileName = tmpFileName;
  76. }
  77. NSURL *exportDirUrl = [NSURL fileURLWithPath:NSTemporaryDirectory() isDirectory:YES];
  78. NSURL *exportVideoUrl = [[exportDirUrl URLByAppendingPathComponent:fileName] URLByAppendingPathExtension: MEDIA_EXTENSION_VIDEO];
  79. NSData *data = [_message blobGetData];
  80. if (![data writeToURL:exportVideoUrl atomically:NO]) {
  81. return nil;
  82. }
  83. return exportVideoUrl;
  84. }
  85. - (UIImage *)underlyingImage {
  86. return _image;
  87. }
  88. - (void)loadUnderlyingImageAndNotify {
  89. // loaded already
  90. if (_message.video != nil) {
  91. return;
  92. }
  93. // loading
  94. if (_message.progress != nil) {
  95. return;
  96. }
  97. [self performLoadUnderlyingImageAndNotify];
  98. }
  99. - (void)performLoadUnderlyingImageAndNotify {
  100. VideoMessageLoader *loader = [[VideoMessageLoader alloc] init];
  101. [loader startWithMessage:_message onCompletion:^(BaseMessage *message) {
  102. [self postCompleteNotification];
  103. } onError:^(NSError *error) {
  104. [self postCompleteNotification];
  105. }];
  106. }
  107. - (void)postCompleteNotification {
  108. [[NSNotificationCenter defaultCenter] postNotificationName:MWPHOTO_LOADING_DID_END_NOTIFICATION object:self];
  109. }
  110. - (void)unloadUnderlyingImage {
  111. _image = nil;
  112. }
  113. - (void)cancelAnyLoading {
  114. }
  115. - (BOOL)isVideo {
  116. return YES;
  117. }
  118. - (void)getVideoURL:(void (^)(NSURL *))completion {
  119. completion([self urlForExportData:@"video"]);
  120. }
  121. - (NSString *)accessibilityLabelForContent {
  122. NSString *date = [DateFormatter accessibilityDateTime:_message.remoteSentDate];
  123. return [NSString stringWithFormat:@"%@. %@", NSLocalizedString(@"video", nil), date];
  124. }
  125. @end