From 16c8a2489fe979d97c937bb1ce96520ce83b8c64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20R=C4=85bel?= Date: Thu, 17 Nov 2022 17:50:25 +0100 Subject: [PATCH] feat: add implementation to handle custom vimeo url --- .../pod_getx_video_controller.dart | 18 +++++++ .../pod_video_quality_controller.dart | 12 +++++ lib/src/models/play_video_from.dart | 18 +++++++ lib/src/utils/enums.dart | 1 + lib/src/utils/video_apis.dart | 54 ++++++++++++++----- 5 files changed, 89 insertions(+), 14 deletions(-) diff --git a/lib/src/controllers/pod_getx_video_controller.dart b/lib/src/controllers/pod_getx_video_controller.dart index 1197369b..67164bcf 100644 --- a/lib/src/controllers/pod_getx_video_controller.dart +++ b/lib/src/controllers/pod_getx_video_controller.dart @@ -149,6 +149,24 @@ class PodGetXVideoController extends _PodGesturesController { playingVideoUrl = _url; break; + case PodVideoPlayerType.vimeoCustomUrl: + await getQualityUrlsFromVimeoUrl(playVideoFrom.dataSource!); + final _url = await getUrlFromVideoQualityUrls( + qualityList: podPlayerConfig.videoQualityPriority, + videoUrls: vimeoOrVideoUrls, + ); + + _videoCtr = VideoPlayerController.network( + _url, + closedCaptionFile: playVideoFrom.closedCaptionFile, + formatHint: playVideoFrom.formatHint, + videoPlayerOptions: playVideoFrom.videoPlayerOptions, + httpHeaders: playVideoFrom.httpHeaders, + ); + playingVideoUrl = _url; + + break; + case PodVideoPlayerType.asset: /// diff --git a/lib/src/controllers/pod_video_quality_controller.dart b/lib/src/controllers/pod_video_quality_controller.dart index 9f4b8dee..b245d7bf 100644 --- a/lib/src/controllers/pod_video_quality_controller.dart +++ b/lib/src/controllers/pod_video_quality_controller.dart @@ -26,6 +26,18 @@ class _PodVideoQualityController extends _PodVideoController { } } + Future getQualityUrlsFromVimeoUrl(String videoUrl) async { + try { + podVideoStateChanger(PodVideoState.loading); + final _vimeoVideoUrls = await VideoApis.getVimeoVideoQualityUrlsFromCustomLink(videoUrl); + + /// + vimeoOrVideoUrls = _vimeoVideoUrls ?? []; + } catch (e) { + rethrow; + } + } + Future getQualityUrlsFromVimeoPrivateId( String videoId, Map httpHeader, diff --git a/lib/src/models/play_video_from.dart b/lib/src/models/play_video_from.dart index 87849a61..8041caa2 100644 --- a/lib/src/models/play_video_from.dart +++ b/lib/src/models/play_video_from.dart @@ -105,6 +105,24 @@ class PlayVideoFrom { httpHeaders: httpHeaders, ); } + + factory PlayVideoFrom.vimeoCustomUrl( + String dataSource, { + VideoFormat? formatHint, + Future? closedCaptionFile, + VideoPlayerOptions? videoPlayerOptions, + Map httpHeaders = const {}, + }) { + return PlayVideoFrom._( + playerType: PodVideoPlayerType.vimeoCustomUrl, + dataSource: dataSource, + formatHint: formatHint, + closedCaptionFile: closedCaptionFile, + videoPlayerOptions: videoPlayerOptions, + httpHeaders: httpHeaders, + ); + } + factory PlayVideoFrom.youtube( String dataSource, { bool live = false, diff --git a/lib/src/utils/enums.dart b/lib/src/utils/enums.dart index 905c6e95..2126dec5 100644 --- a/lib/src/utils/enums.dart +++ b/lib/src/utils/enums.dart @@ -13,4 +13,5 @@ enum PodVideoPlayerType { vimeo, youtube, vimeoPrivateVideos, + vimeoCustomUrl, } diff --git a/lib/src/utils/video_apis.dart b/lib/src/utils/video_apis.dart index e319469e..3a71e13a 100644 --- a/lib/src/utils/video_apis.dart +++ b/lib/src/utils/video_apis.dart @@ -11,6 +11,31 @@ String podErrorString(String val) { } class VideoApis { + static Future?> getVimeoVideoQualityUrlsFromCustomLink( + String videoLink, { + Map? headers, + }) async { + try { + final response = await http.get( + Uri.parse(videoLink), + headers: {'Accept': 'application/json', 'Content-type': 'application/json', ...headers ?? {}}, + ); + final jsonData = jsonDecode(response.body)['files']; + + return _parseVimeoPrivateUrlResponse(jsonData); + } catch (error) { + if (error.toString().contains('XMLHttpRequest')) { + log( + podErrorString( + '(INFO) To play vimeo video in WEB, Please enable CORS in your browser', + ), + ); + } + debugPrint('===== VIMEO API ERROR: $error =========='); + rethrow; + } + } + static Future?> getVimeoVideoQualityUrls( String videoId, ) async { @@ -18,8 +43,7 @@ class VideoApis { final response = await http.get( Uri.parse('https://player.vimeo.com/video/$videoId/config'), ); - final jsonData = - jsonDecode(response.body)['request']['files']['progressive']; + final jsonData = jsonDecode(response.body)['request']['files']['progressive']; return List.generate( jsonData.length, (index) => VideoQalityUrls( @@ -53,16 +77,7 @@ class VideoApis { ); final jsonData = jsonDecode(response.body)['files']; - final List list = []; - for (int i = 0; i < jsonData.length; i++) { - final String quality = - (jsonData[i]['rendition'] as String?)?.split('p').first ?? '0'; - final int? number = int.tryParse(quality); - if (number != null && number != 0) { - list.add(VideoQalityUrls(quality: number, url: jsonData[i]['link'])); - } - } - return list; + return _parseVimeoPrivateUrlResponse(jsonData); } catch (error) { if (error.toString().contains('XMLHttpRequest')) { log( @@ -94,8 +109,7 @@ class VideoApis { ), ); } else { - final manifest = - await yt.videos.streamsClient.getManifest(youtubeIdOrUrl); + final manifest = await yt.videos.streamsClient.getManifest(youtubeIdOrUrl); urls.addAll( manifest.muxed.map( (element) => VideoQalityUrls( @@ -120,4 +134,16 @@ class VideoApis { rethrow; } } + + static List _parseVimeoPrivateUrlResponse(jsonData) { + final List list = []; + for (int i = 0; i < jsonData.length; i++) { + final String quality = (jsonData[i]['rendition'] as String?)?.split('p').first ?? '0'; + final int? number = int.tryParse(quality); + if (number != null && number != 0) { + list.add(VideoQalityUrls(quality: number, url: jsonData[i]['link'])); + } + } + return list; + } }