From 38d47fd72d2cc26eca90606d8e9fa14e9f3886ef Mon Sep 17 00:00:00 2001 From: Hiroshi Horie <548776+hiroshihorie@users.noreply.github.com> Date: Fri, 25 Feb 2022 16:41:58 +0900 Subject: [PATCH] make video types comparable --- lib/src/options.dart | 1 - lib/src/types/other.dart | 2 -- lib/src/types/video_encoding.dart | 17 ++++++++++++++++- lib/src/types/video_parameters.dart | 25 ++++++++++++++++--------- lib/src/utils.dart | 2 -- 5 files changed, 32 insertions(+), 15 deletions(-) diff --git a/lib/src/options.dart b/lib/src/options.dart index 8f2ca71..be7b60d 100644 --- a/lib/src/options.dart +++ b/lib/src/options.dart @@ -6,7 +6,6 @@ import 'track/options.dart'; import 'track/track.dart'; import 'types/other.dart'; import 'types/video_encoding.dart'; -import 'types/video_parameters.dart'; /// Options used when connecting to the server. class ConnectOptions { diff --git a/lib/src/types/other.dart b/lib/src/types/other.dart index 4d81362..918db86 100644 --- a/lib/src/types/other.dart +++ b/lib/src/types/other.dart @@ -1,5 +1,3 @@ -import 'dart:math' as math; - import 'package:flutter/material.dart'; import '../extensions.dart'; diff --git a/lib/src/types/video_encoding.dart b/lib/src/types/video_encoding.dart index db96dcd..7774beb 100644 --- a/lib/src/types/video_encoding.dart +++ b/lib/src/types/video_encoding.dart @@ -3,7 +3,7 @@ import 'package:meta/meta.dart'; /// A type that represents video encoding information. @immutable -class VideoEncoding { +class VideoEncoding implements Comparable { final int maxFramerate; final int maxBitrate; @@ -28,6 +28,21 @@ class VideoEncoding { @override int get hashCode => Object.hash(maxFramerate, maxBitrate); + + // ---------------------------------------------------------------------- + // Comparable + + @override + int compareTo(VideoEncoding other) { + // compare bitrates + final result = maxBitrate.compareTo(other.maxBitrate); + // if bitrates are the same, compare by fps + if (result == 0) { + return maxFramerate.compareTo(other.maxFramerate); + } + + return result; + } } /// Convenience extension for [VideoEncoding]. diff --git a/lib/src/types/video_parameters.dart b/lib/src/types/video_parameters.dart index c0a9d88..069d484 100644 --- a/lib/src/types/video_parameters.dart +++ b/lib/src/types/video_parameters.dart @@ -4,7 +4,7 @@ import 'video_dimensions.dart'; import 'video_encoding.dart'; @immutable -class VideoParameters { +class VideoParameters implements Comparable { final String? description; final VideoDimensions dimensions; final VideoEncoding encoding; @@ -29,6 +29,21 @@ class VideoParameters { @override int get hashCode => Object.hash(description, dimensions, encoding); + // ---------------------------------------------------------------------- + // Comparable + + @override + int compareTo(VideoParameters other) { + // compare by dimension's area + final result = dimensions.area().compareTo(other.dimensions.area()); + // if dimensions have equal area, compare by encoding + if (result == 0) { + return encoding.compareTo(other.encoding); + } + + return result; + } + // // TODO: Return constraints that will work for all platforms (Web & Mobile) // https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices/getUserMedia @@ -38,14 +53,6 @@ class VideoParameters { 'height': dimensions.height, 'frameRate': encoding.maxFramerate, }; - - // Comparable - // @override - // int compareTo(VideoParameters other) { - // if (this.dimensions.area() == other.dimensions.area()) { - // return encoding.compare - // } - // } } extension VideoParametersPresets on VideoParameters { diff --git a/lib/src/utils.dart b/lib/src/utils.dart index c1a47ef..d67c79e 100644 --- a/lib/src/utils.dart +++ b/lib/src/utils.dart @@ -14,8 +14,6 @@ import 'livekit.dart'; import 'logger.dart'; import 'options.dart'; import 'support/platform.dart'; -import 'track/options.dart'; -import 'types/other.dart'; import 'types/video_dimensions.dart'; import 'types/video_encoding.dart'; import 'types/video_parameters.dart';