WFCCVideoMessageContent.m 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. //
  2. // WFCCVideoMessageContent.m
  3. // WFChatClient
  4. //
  5. // Created by heavyrain on 2017/9/2.
  6. // Copyright © 2017年 wildfire chat. All rights reserved.
  7. //
  8. #import "WFCCVideoMessageContent.h"
  9. #import "WFCCNetworkService.h"
  10. #import "WFCCIMService.h"
  11. #import "WFCCUtilities.h"
  12. #import "Common.h"
  13. #import <AVFoundation/AVFoundation.h>
  14. @implementation WFCCVideoMessageContent
  15. + (instancetype)contentPath:(NSString *)localPath thumbnail:(UIImage *)image {
  16. WFCCVideoMessageContent *content = [[WFCCVideoMessageContent alloc] init];
  17. content.localPath = localPath;
  18. content.thumbnail = [WFCCUtilities imageWithRightOrientation:image];
  19. NSURL *videoUrl = [NSURL URLWithString:localPath];
  20. AVURLAsset *avUrl = [AVURLAsset assetWithURL:videoUrl];
  21. CMTime time = [avUrl duration];
  22. content.duration = ceil(time.value/time.timescale);
  23. return content;
  24. }
  25. - (WFCCMessagePayload *)encode {
  26. WFCCMediaMessagePayload *payload = [[WFCCMediaMessagePayload alloc] init];
  27. payload.extra = self.extra;
  28. payload.contentType = [self.class getContentType];
  29. payload.searchableContent = @"[视频]";
  30. payload.binaryContent = UIImageJPEGRepresentation(self.thumbnail, 0.45);
  31. payload.mediaType = Media_Type_VIDEO;
  32. payload.remoteMediaUrl = self.remoteUrl;
  33. payload.localMediaPath = self.localPath;
  34. NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
  35. [dict setObject:@(_duration) forKey:@"duration"];
  36. payload.content = [[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:dict options:kNilOptions error:nil] encoding:NSUTF8StringEncoding];
  37. return payload;
  38. }
  39. - (void)decode:(WFCCMessagePayload *)payload {
  40. [super decode:payload];
  41. if ([payload isKindOfClass:[WFCCMediaMessagePayload class]]) {
  42. WFCCMediaMessagePayload *mediaPayload = (WFCCMediaMessagePayload *)payload;
  43. self.thumbnail = [UIImage imageWithData:payload.binaryContent];
  44. self.remoteUrl = mediaPayload.remoteMediaUrl;
  45. self.localPath = mediaPayload.localMediaPath;
  46. NSError *__error = nil;
  47. NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:[payload.content dataUsingEncoding:NSUTF8StringEncoding]
  48. options:kNilOptions
  49. error:&__error];
  50. if (!__error) {
  51. self.duration = [dictionary[@"duration"] longValue];
  52. }
  53. }
  54. }
  55. + (int)getContentType {
  56. return MESSAGE_CONTENT_TYPE_VIDEO;
  57. }
  58. + (int)getContentFlags {
  59. return WFCCPersistFlag_PERSIST_AND_COUNT;
  60. }
  61. + (void)load {
  62. [[WFCCIMService sharedWFCIMService] registerMessageContent:self];
  63. }
  64. - (NSString *)digest:(WFCCMessage *)message {
  65. return @"[视频]";
  66. }
  67. @end