From d7080641bda6fab850abd932c99d0978cc437323 Mon Sep 17 00:00:00 2001 From: Sahil Kumar Date: Tue, 15 Mar 2022 14:24:20 +0530 Subject: [PATCH] refactor(ui): remove video compression Signed-off-by: xsahil03x --- .../lib/src/message_input.dart | 75 +++---------------- .../lib/src/message_input/message_input.dart | 44 +---------- .../stream_attachment_picker.dart | 46 +----------- .../lib/src/video_service.dart | 28 ------- packages/stream_chat_flutter/pubspec.yaml | 2 - 5 files changed, 17 insertions(+), 178 deletions(-) diff --git a/packages/stream_chat_flutter/lib/src/message_input.dart b/packages/stream_chat_flutter/lib/src/message_input.dart index 4aa46e56..b0cce913 100644 --- a/packages/stream_chat_flutter/lib/src/message_input.dart +++ b/packages/stream_chat_flutter/lib/src/message_input.dart @@ -18,12 +18,8 @@ import 'package:stream_chat_flutter/src/media_list_view.dart'; import 'package:stream_chat_flutter/src/multi_overlay.dart'; import 'package:stream_chat_flutter/src/quoted_message_widget.dart'; import 'package:stream_chat_flutter/src/user_mentions_overlay.dart'; -import 'package:stream_chat_flutter/src/video_service.dart'; import 'package:stream_chat_flutter/src/video_thumbnail_image.dart'; import 'package:stream_chat_flutter/stream_chat_flutter.dart'; -import 'package:video_compress/video_compress.dart'; - -export 'package:video_compress/video_compress.dart' show VideoQuality; /// A callback that can be passed to [MessageInput.onError]. /// @@ -191,8 +187,6 @@ class MessageInput extends StatefulWidget { this.mentionsTileBuilder, this.userMentionsTileBuilder, this.maxAttachmentSize = _kDefaultMaxAttachmentSize, - this.compressedVideoQuality = VideoQuality.DefaultQuality, - this.compressedVideoFrameRate = 30, this.onError, this.attachmentLimit = 10, this.onAttachmentLimitExceed, @@ -213,12 +207,6 @@ class MessageInput extends StatefulWidget { /// Message to edit final Message? editMessage; - /// Video quality to use when compressing the videos - final VideoQuality compressedVideoQuality; - - /// Frame rate to use when compressing the videos - final int compressedVideoFrameRate; - /// Max attachment size in bytes /// Defaults to 20 MB /// do not set it if you're using our default CDN @@ -1119,41 +1107,18 @@ class MessageInputState extends State { if (mediaFile == null) return; - var file = AttachmentFile( + final file = AttachmentFile( path: mediaFile.path, size: await mediaFile.length(), bytes: mediaFile.readAsBytesSync(), ); if (file.size! > widget.maxAttachmentSize) { - if (medium.type == AssetType.video && file.path != null) { - final mediaInfo = await StreamVideoService.compressVideo( - file.path!, - frameRate: widget.compressedVideoFrameRate, - quality: widget.compressedVideoQuality, - ); - - if (mediaInfo == null || - mediaInfo.filesize! > widget.maxAttachmentSize) { - _showErrorAlert( - context.translations.fileTooLargeAfterCompressionError( - widget.maxAttachmentSize / (1024 * 1024), - ), - ); - return; - } - file = AttachmentFile( - name: file.name, - size: mediaInfo.filesize, - bytes: await mediaInfo.file?.readAsBytes(), - path: mediaInfo.path, - ); - } else { - _showErrorAlert(context.translations.fileTooLargeError( + return _showErrorAlert( + context.translations.fileTooLargeError( widget.maxAttachmentSize / (1024 * 1024), - )); - return; - } + ), + ); } setState(() { @@ -1678,33 +1643,11 @@ class MessageInputState extends State { ); if (file.size! > widget.maxAttachmentSize) { - if (attachmentType == 'video' && file.path != null) { - final mediaInfo = await (StreamVideoService.compressVideo( - file.path!, - frameRate: widget.compressedVideoFrameRate, - quality: widget.compressedVideoQuality, - ) as FutureOr); - - if (mediaInfo.filesize! > widget.maxAttachmentSize) { - _showErrorAlert( - context.translations.fileTooLargeAfterCompressionError( - widget.maxAttachmentSize / (1024 * 1024), - ), - ); - return; - } - file = AttachmentFile( - name: file.name, - size: mediaInfo.filesize, - bytes: await mediaInfo.file!.readAsBytes(), - path: mediaInfo.path, - ); - } else { - _showErrorAlert(context.translations.fileTooLargeError( + return _showErrorAlert( + context.translations.fileTooLargeError( widget.maxAttachmentSize / (1024 * 1024), - )); - return; - } + ), + ); } setState(() { diff --git a/packages/stream_chat_flutter/lib/src/message_input/message_input.dart b/packages/stream_chat_flutter/lib/src/message_input/message_input.dart index e7ec69f1..21d5775d 100644 --- a/packages/stream_chat_flutter/lib/src/message_input/message_input.dart +++ b/packages/stream_chat_flutter/lib/src/message_input/message_input.dart @@ -18,12 +18,8 @@ import 'package:stream_chat_flutter/src/message_input/tld.dart'; import 'package:stream_chat_flutter/src/multi_overlay.dart'; import 'package:stream_chat_flutter/src/quoted_message_widget.dart'; import 'package:stream_chat_flutter/src/user_mentions_overlay.dart'; -import 'package:stream_chat_flutter/src/video_service.dart'; import 'package:stream_chat_flutter/src/video_thumbnail_image.dart'; import 'package:stream_chat_flutter/stream_chat_flutter.dart'; -import 'package:video_compress/video_compress.dart'; - -export 'package:video_compress/video_compress.dart' show VideoQuality; /// A function that returns true if the message is valid and can be sent. typedef MessageValidator = bool Function(Message message); @@ -202,8 +198,6 @@ class StreamMessageInput extends StatefulWidget { this.mentionsTileBuilder, this.userMentionsTileBuilder, this.maxAttachmentSize = _kDefaultMaxAttachmentSize, - this.compressedVideoQuality = VideoQuality.DefaultQuality, - this.compressedVideoFrameRate = 30, this.onError, this.attachmentLimit = 10, this.onAttachmentLimitExceed, @@ -224,12 +218,6 @@ class StreamMessageInput extends StatefulWidget { /// List of options for showing overlays. final List customOverlays; - /// Video quality to use when compressing the videos. - final VideoQuality compressedVideoQuality; - - /// Frame rate to use when compressing the videos. - final int compressedVideoFrameRate; - /// Max attachment size in bytes: /// - Defaults to 20 MB /// - Do not set it if you're using our default CDN @@ -1126,8 +1114,6 @@ class StreamMessageInputState extends State attachmentLimit: widget.attachmentLimit, onAttachmentLimitExceeded: widget.onAttachmentLimitExceed, maxAttachmentSize: widget.maxAttachmentSize, - compressedVideoQuality: widget.compressedVideoQuality, - compressedVideoFrameRate: widget.compressedVideoFrameRate, onError: _showErrorAlert, ); @@ -1639,33 +1625,11 @@ class StreamMessageInputState extends State ); if (file.size! > widget.maxAttachmentSize) { - if (attachmentType == 'video' && file.path != null) { - final mediaInfo = await (StreamVideoService.compressVideo( - file.path!, - frameRate: widget.compressedVideoFrameRate, - quality: widget.compressedVideoQuality, - ) as FutureOr); - - if (mediaInfo.filesize! > widget.maxAttachmentSize) { - _showErrorAlert( - context.translations.fileTooLargeAfterCompressionError( - widget.maxAttachmentSize / (1024 * 1024), - ), - ); - return; - } - file = AttachmentFile( - name: file.name, - size: mediaInfo.filesize, - bytes: await mediaInfo.file!.readAsBytes(), - path: mediaInfo.path, - ); - } else { - _showErrorAlert(context.translations.fileTooLargeError( + return _showErrorAlert( + context.translations.fileTooLargeError( widget.maxAttachmentSize / (1024 * 1024), - )); - return; - } + ), + ); } _addAttachments([ diff --git a/packages/stream_chat_flutter/lib/src/message_input/stream_attachment_picker.dart b/packages/stream_chat_flutter/lib/src/message_input/stream_attachment_picker.dart index 1e1b99c7..2c254223 100644 --- a/packages/stream_chat_flutter/lib/src/message_input/stream_attachment_picker.dart +++ b/packages/stream_chat_flutter/lib/src/message_input/stream_attachment_picker.dart @@ -5,9 +5,7 @@ import 'package:flutter_svg/flutter_svg.dart'; import 'package:photo_manager/photo_manager.dart'; import 'package:stream_chat_flutter/src/extension.dart'; import 'package:stream_chat_flutter/src/media_list_view.dart'; -import 'package:stream_chat_flutter/src/video_service.dart'; import 'package:stream_chat_flutter/stream_chat_flutter.dart'; -import 'package:video_compress/video_compress.dart'; /// Callback for when a file has to be picked. typedef FilePickerCallback = void Function( @@ -34,8 +32,6 @@ class StreamAttachmentPicker extends StatefulWidget { this.attachmentLimit = 10, this.onAttachmentLimitExceeded, this.maxAttachmentSize = 20971520, - this.compressedVideoQuality = VideoQuality.DefaultQuality, - this.compressedVideoFrameRate = 30, this.onError, this.allowedAttachmentTypes = const [ DefaultAttachmentTypes.image, @@ -66,12 +62,6 @@ class StreamAttachmentPicker extends StatefulWidget { /// Callback for when file is picked. final FilePickerCallback onFilePicked; - /// Video quality to use when compressing the videos. - final VideoQuality compressedVideoQuality; - - /// Frame rate to use when compressing the videos. - final int compressedVideoFrameRate; - /// Max attachment size in bytes: /// - Defaults to 20 MB /// - Do not set it if you're using our default CDN @@ -94,8 +84,6 @@ class StreamAttachmentPicker extends StatefulWidget { int? attachmentLimit, AttachmentLimitExceedListener? onAttachmentLimitExceeded, int? maxAttachmentSize, - VideoQuality? compressedVideoQuality, - int? compressedVideoFrameRate, ValueChanged? onChangeInputState, ValueChanged? onError, List? allowedAttachmentTypes, @@ -112,10 +100,6 @@ class StreamAttachmentPicker extends StatefulWidget { onAttachmentLimitExceeded: onAttachmentLimitExceeded ?? this.onAttachmentLimitExceeded, maxAttachmentSize: maxAttachmentSize ?? this.maxAttachmentSize, - compressedVideoQuality: - compressedVideoQuality ?? this.compressedVideoQuality, - compressedVideoFrameRate: - compressedVideoFrameRate ?? this.compressedVideoFrameRate, onError: onError ?? this.onError, allowedAttachmentTypes: allowedAttachmentTypes ?? this.allowedAttachmentTypes, @@ -375,33 +359,11 @@ class _StreamAttachmentPickerState extends State { ); if (file.size! > widget.maxAttachmentSize) { - if (medium.type == AssetType.video && file.path != null) { - final mediaInfo = await (StreamVideoService.compressVideo( - file.path!, - frameRate: widget.compressedVideoFrameRate, - quality: widget.compressedVideoQuality, - ) as FutureOr); - - if (mediaInfo.filesize! > widget.maxAttachmentSize) { - widget.onError?.call( - context.translations.fileTooLargeAfterCompressionError( - widget.maxAttachmentSize / (1024 * 1024), - ), - ); - return; - } - file = AttachmentFile( - name: file.name, - size: mediaInfo.filesize, - bytes: await mediaInfo.file?.readAsBytes(), - path: mediaInfo.path, - ); - } else { - widget.onError?.call(context.translations.fileTooLargeError( + return widget.onError?.call( + context.translations.fileTooLargeError( widget.maxAttachmentSize / (1024 * 1024), - )); - return; - } + ), + ); } setState(() { diff --git a/packages/stream_chat_flutter/lib/src/video_service.dart b/packages/stream_chat_flutter/lib/src/video_service.dart index fb1db9ed..120460be 100644 --- a/packages/stream_chat_flutter/lib/src/video_service.dart +++ b/packages/stream_chat_flutter/lib/src/video_service.dart @@ -1,8 +1,6 @@ import 'dart:async'; import 'dart:typed_data'; -import 'package:synchronized/synchronized.dart'; -import 'package:video_compress/video_compress.dart'; import 'package:video_thumbnail/video_thumbnail.dart'; /// @@ -12,32 +10,6 @@ class _IVideoService { /// Singleton instance of [_IVideoService] static final _IVideoService instance = _IVideoService._(); - final _lock = Lock(); - - /// compress video from [path] - /// compress video from [path] return [Future] - /// - /// you can choose its [quality] and [frameRate] - /// - /// ## example - /// ```dart - /// final info = await _flutterVideoCompress.compressVideo( - /// file.path, - /// ); - /// debugPrint(info.toJson()); - /// ``` - Future compressVideo( - String path, { - int frameRate = 30, - VideoQuality quality = VideoQuality.DefaultQuality, - }) async => - _lock.synchronized( - () => VideoCompress.compressVideo( - path, - frameRate: frameRate, - quality: quality, - ), - ); /// Generates a thumbnail image data in memory as UInt8List, /// it can be easily used by Image.memory(...). diff --git a/packages/stream_chat_flutter/pubspec.yaml b/packages/stream_chat_flutter/pubspec.yaml index c2d90eb0..5dab31a1 100644 --- a/packages/stream_chat_flutter/pubspec.yaml +++ b/packages/stream_chat_flutter/pubspec.yaml @@ -38,9 +38,7 @@ dependencies: shimmer: ^2.0.0 stream_chat_flutter_core: ^3.5.0 substring_highlight: ^1.0.26 - synchronized: ^3.0.0 url_launcher: ^6.0.3 - video_compress: ^3.0.0 video_player: ^2.1.0 video_thumbnail: ^0.4.3 visibility_detector: ^0.2.0