From 20ef3e8f44d01de33141e6b76664cf0833f57601 Mon Sep 17 00:00:00 2001 From: Danmei Chen Date: Wed, 11 Jul 2018 16:13:04 +0200 Subject: [PATCH] fix recv video --- Classes/LinphoneUI/UIChatBubblePhotoCell.m | 49 +++++----------- Classes/Utils/FileTransferDelegate.m | 66 +++++++++++++++++++--- Classes/Utils/Utils.m | 17 ++++++ 3 files changed, 90 insertions(+), 42 deletions(-) diff --git a/Classes/LinphoneUI/UIChatBubblePhotoCell.m b/Classes/LinphoneUI/UIChatBubblePhotoCell.m index 39345b48a..558a3f79c 100644 --- a/Classes/LinphoneUI/UIChatBubblePhotoCell.m +++ b/Classes/LinphoneUI/UIChatBubblePhotoCell.m @@ -211,9 +211,6 @@ // we did not load the image yet, so start doing so if (_messageImageView.image == nil) { [_messageImageView startLoading]; - if([localImage isEqualToString:@"uknown"]) { - //TODO add default image - } PHFetchResult *assets = [PHAsset fetchAssetsWithLocalIdentifiers:[NSArray arrayWithObject:localImage] options:nil]; UIImage *img = [chatTableView.imagesInChatroom objectForKey:localImage]; if (![assets firstObject]) @@ -226,21 +223,19 @@ } } else if (localVideo) { if (_messageImageView.image == nil) { - [_messageImageView startLoading]; + PHFetchResult *assets = [PHAsset fetchAssetsWithLocalIdentifiers:[NSArray arrayWithObject:localVideo] options:nil]; - UIImage *img = [chatTableView.imagesInChatroom objectForKey:localImage]; - if (![assets firstObject]) - [self loadPlaceholder]; - PHAsset *asset = [assets firstObject]; - if (img) - [self loadImageAsset:asset image:img]; - else - [self loadAsset:asset]; - // read video from Documents - /* NSURL *url = [(AVURLAsset*)assets URL]; - AVAsset *asset = [AVAsset assetWithURL:url]; - if (asset) - [self loadVideoAsset:asset];*/ + if([assets firstObject] .mediaType == PHAssetMediaTypeVideo) { + [_messageImageView startLoading]; + UIImage *img = [chatTableView.imagesInChatroom objectForKey:localImage]; + if (![assets firstObject]) + [self loadPlaceholder]; + PHAsset *asset = [assets firstObject]; + if (img) + [self loadImageAsset:asset image:img]; + else + [self loadAsset:asset]; + } } } else if (localFile) { NSString *text = [NSString stringWithFormat:@"📎 %@",localFile]; @@ -295,29 +290,15 @@ [[PHImageManager defaultManager] requestPlayerItemForVideo:asset options:options resultHandler:^(AVPlayerItem * _Nullable playerItem, NSDictionary * _Nullable info) { if(playerItem) { AVPlayer *player = [AVPlayer playerWithPlayerItem:playerItem]; - AVPlayerViewController *controller = [[AVPlayerViewController alloc] init]; - [PhoneMainView.instance presentViewController:controller animated:YES completion:nil]; + AVPlayerViewController *controller = [[AVPlayerViewController alloc] init]; + [PhoneMainView.instance presentViewController:controller animated:YES completion:nil]; controller.player = player; [player play]; - } + } else { LOGE(@"Can't read video"); } }]; - /* NSString *localVideo = [LinphoneManager getMessageAppDataForKey:@"localvideo" inMessage:self.message]; - NSString *filePath = [LinphoneManager documentFile:localVideo]; - NSFileManager *fileManager = [NSFileManager defaultManager]; - - if ([fileManager fileExistsAtPath:filePath]) { - // create a player view controller - AVPlayer *player = [AVPlayer playerWithURL:[[NSURL alloc] initFileURLWithPath:filePath]]; - AVPlayerViewController *controller = [[AVPlayerViewController alloc] init]; - [PhoneMainView.instance presentViewController:controller animated:YES completion:nil]; - controller.player = player; - [player play]; - } else { - [self fileErrorBlock]; - }*/ } - (IBAction)onOpenClick:(id)event { diff --git a/Classes/Utils/FileTransferDelegate.m b/Classes/Utils/FileTransferDelegate.m index b84c8e403..a27dd8c8e 100644 --- a/Classes/Utils/FileTransferDelegate.m +++ b/Classes/Utils/FileTransferDelegate.m @@ -116,10 +116,63 @@ static void linphone_iphone_file_transfer_recv(LinphoneChatMessage *message, con CFRelease((__bridge CFTypeRef)thiz); }); }]; - } else { + } else if([fileType isEqualToString:@"video"]) { + CFBridgingRetain(thiz); [[LinphoneManager.instance fileTransferDelegates] removeObject:thiz]; + NSString *name =[NSString stringWithUTF8String:linphone_content_get_name(content)]; + NSString *filePath = [[LinphoneManager cacheDirectory] stringByAppendingPathComponent:name]; + [[NSFileManager defaultManager] createFileAtPath:filePath + contents:thiz.data + attributes:nil]; + // until image is properly saved, keep a reminder on it so that the + // chat bubble is aware of the fact that image is being saved to device + [LinphoneManager setValueInMessageAppData:@"saving..." forKey:@"localvideo" inMessage:message]; - NSString *key = [fileType isEqualToString:@"file"] ? @"localfile" : @"localvideo"; + __block PHObjectPlaceholder *placeHolder; + [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{ + PHAssetCreationRequest *request = [PHAssetCreationRequest creationRequestForAssetFromVideoAtFileURL:[NSURL fileURLWithPath:filePath]]; + placeHolder = [request placeholderForCreatedAsset]; + } completionHandler:^(BOOL success, NSError * _Nullable error) { + dispatch_async(dispatch_get_main_queue(), ^{ + if (error) { + LOGE(@"Cannot save video data downloaded [%@]", [error localizedDescription]); + [LinphoneManager setValueInMessageAppData:nil forKey:@"localvideo" inMessage:message]; + UIAlertController *errView = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"Transfer error", nil) + message:NSLocalizedString(@"Cannot write video to photo library", + nil) + preferredStyle:UIAlertControllerStyleAlert]; + + UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" + style:UIAlertActionStyleDefault + handler:^(UIAlertAction * action) {}]; + + [errView addAction:defaultAction]; + [PhoneMainView.instance presentViewController:errView animated:YES completion:nil]; + } else { + LOGI(@"video saved to [%@]", [placeHolder localIdentifier]); + [LinphoneManager setValueInMessageAppData:[placeHolder localIdentifier] + forKey:@"localvideo" + inMessage:message]; + } + [NSNotificationCenter.defaultCenter + postNotificationName:kLinphoneFileTransferRecvUpdate + object:thiz + userInfo:@{ + @"state" : @(LinphoneChatMessageStateDelivered), // we dont want to + // trigger + // FileTransferDone here + @"progress" : @(1.f), + }]; + + [thiz stopAndDestroy]; + CFRelease((__bridge CFTypeRef)thiz); + }); + }]; + + } else { + [[LinphoneManager.instance fileTransferDelegates] removeObject:thiz]; + LOGE(@"xxxxxxxxxx"); + NSString *key = @"localfile" ; NSString *name =[NSString stringWithUTF8String:linphone_content_get_name(content)]; [LinphoneManager setValueInMessageAppData:@"saving..." forKey:key inMessage:message]; @@ -232,15 +285,12 @@ static LinphoneBuffer *linphone_iphone_file_transfer_send(LinphoneChatMessage *m if (phAssetId) [self uploadData:UIImageJPEGRepresentation(image, quality) forChatRoom:chatRoom type:@"image" subtype:@"jpeg" name:name key:@"localimage" keyData:phAssetId qualityData:[NSNumber numberWithFloat:quality]]; else - [self uploadData:UIImageJPEGRepresentation(image, quality) forChatRoom:chatRoom type:@"image" subtype:@"jpeg" name:name key:@"localimage" keyData:@"unknown" qualityData:nil]; + [self uploadData:UIImageJPEGRepresentation(image, quality) forChatRoom:chatRoom type:@"image" subtype:@"jpeg" name:name key:@"localimage" keyData:nil qualityData:nil]; } - (void)uploadVideo:(NSData *)data withassetId:(NSString *)phAssetId forChatRoom:(LinphoneChatRoom *)chatRoom { NSString *name = [NSString stringWithFormat:@"%f.mov", [NSDate timeIntervalSinceReferenceDate]]; - if (phAssetId) - [self uploadData:data forChatRoom:chatRoom type:@"image" subtype:@"jpeg" name:name key:@"localvideo" keyData:phAssetId qualityData:nil]; - else - [self uploadData:data forChatRoom:chatRoom type:@"image" subtype:@"jpeg" name:name key:@"localvideo" keyData:nil qualityData:nil]; + [self uploadData:data forChatRoom:chatRoom type:@"video" subtype:nil name:name key:@"localvideo" keyData:phAssetId qualityData:nil]; } - (void)uploadFile:(NSData *)data forChatRoom:(LinphoneChatRoom *)chatRoom withUrl:(NSURL *)url { @@ -252,7 +302,7 @@ static LinphoneBuffer *linphone_iphone_file_transfer_send(LinphoneChatMessage *m attributes:nil]; if ([[url pathExtension] isEqualToString:@"MOV"]) - [self uploadData:data forChatRoom:chatRoom type:nil subtype:nil name:name key:@"localvideo" keyData:name qualityData:nil]; + [self uploadData:data forChatRoom:chatRoom type:@"video" subtype:nil name:name key:@"localvideo" keyData:name qualityData:nil]; else [self uploadData:data forChatRoom:chatRoom type:@"file" subtype:nil name:name key:@"localfile" keyData:name qualityData:nil]; } diff --git a/Classes/Utils/Utils.m b/Classes/Utils/Utils.m index e5e9c8f2c..8227fa484 100644 --- a/Classes/Utils/Utils.m +++ b/Classes/Utils/Utils.m @@ -73,6 +73,23 @@ return assetDict; } +/*+ (NSMutableDictionary *)videoAssetsDictionary { + NSMutableDictionary *assetDict = [NSMutableDictionary dictionary]; + + PHFetchOptions *options = [[PHFetchOptions alloc] init]; + [options setIncludeHiddenAssets:YES]; + [options setIncludeAllBurstAssets:YES]; + + PHFetchResult *fetchRes = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeVideo options:options]; + + for (PHAsset *asset in fetchRes) { + NSString *key = [asset valueForKey:@"filename"]; + [assetDict setObject:asset forKey:[[key componentsSeparatedByString:@"."] firstObject]]; + } + + return assetDict; +}*/ + + (NSString *)timeToString:(time_t)time withFormat:(LinphoneDateFormat)format { NSString *formatstr; NSDate *todayDate = [[NSDate alloc] init];