Skip to content

Commit

Permalink
feat: refactor resize prop handler (#3286)
Browse files Browse the repository at this point in the history
  • Loading branch information
KrzysztofMoch authored Oct 11, 2023
1 parent 03a579e commit 7fd7b3f
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -119,16 +119,6 @@ public void onDropViewInstance(ReactExoplayerView view) {
return builder.build();
}

@Override
public @Nullable Map<String, Object> getExportedViewConstants() {
return MapBuilder.<String, Object>of(
"ScaleNone", Integer.toString(ResizeMode.RESIZE_MODE_FIT),
"ScaleAspectFit", Integer.toString(ResizeMode.RESIZE_MODE_FIT),
"ScaleToFill", Integer.toString(ResizeMode.RESIZE_MODE_FILL),
"ScaleAspectFill", Integer.toString(ResizeMode.RESIZE_MODE_CENTER_CROP)
);
}

@ReactProp(name = PROP_DRM)
public void setDRM(final ReactExoplayerView videoView, @Nullable ReadableMap drm) {
if (drm != null && drm.hasKey(PROP_DRM_TYPE)) {
Expand Down Expand Up @@ -212,8 +202,23 @@ public void setAdTagUrl(final ReactExoplayerView videoView, final String uriStri


@ReactProp(name = PROP_RESIZE_MODE)
public void setResizeMode(final ReactExoplayerView videoView, final String resizeModeOrdinalString) {
videoView.setResizeModeModifier(convertToIntDef(resizeModeOrdinalString));
public void setResizeMode(final ReactExoplayerView videoView, final String resizeMode) {
switch (resizeMode) {
case "none":
case "contain":
videoView.setResizeModeModifier(ResizeMode.RESIZE_MODE_FIT);
break;
case "cover":
videoView.setResizeModeModifier(ResizeMode.RESIZE_MODE_CENTER_CROP);
break;
case "stretch":
videoView.setResizeModeModifier(ResizeMode.RESIZE_MODE_FILL);
break;
default:
DebugLog.w("ExoPlayer Warning", "Unsupported resize mode: " + resizeMode + " - falling back to fit");
videoView.setResizeModeModifier(ResizeMode.RESIZE_MODE_FIT);
break;
}
}

@ReactProp(name = PROP_REPEAT, defaultBoolean = false)
Expand Down Expand Up @@ -430,12 +435,4 @@ private boolean startsWithValidScheme(String uriString) {
|| lowerCaseUri.startsWith("file://")
|| lowerCaseUri.startsWith("asset://");
}

private @ResizeMode.Mode int convertToIntDef(String resizeModeOrdinalString) {
if (!TextUtils.isEmpty(resizeModeOrdinalString)) {
int resizeModeOrdinal = Integer.parseInt(resizeModeOrdinalString);
return ResizeMode.toResizeMode(resizeModeOrdinal);
}
return ResizeMode.RESIZE_MODE_FIT;
}
}
28 changes: 24 additions & 4 deletions ios/Video/RCTVideo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class RCTVideo: UIView, RCTVideoPlayerViewControllerDelegate, RCTPlayerObserverH
private var _playWhenInactive:Bool = false
private var _ignoreSilentSwitch:String! = "inherit" // inherit, ignore, obey
private var _mixWithOthers:String! = "inherit" // inherit, mix, duck
private var _resizeMode:String! = "AVLayerVideoGravityResizeAspectFill"
private var _resizeMode:String! = "cover"
private var _fullscreen:Bool = false
private var _fullscreenAutorotate:Bool = true
private var _fullscreenOrientation:String! = "all"
Expand Down Expand Up @@ -429,12 +429,32 @@ class RCTVideo: UIView, RCTVideoPlayerViewControllerDelegate, RCTPlayerObserverH
// MARK: - Prop setters

@objc
func setResizeMode(_ mode: String?) {
func setResizeMode(_ mode: String) {
var resizeMode: AVLayerVideoGravity = .resizeAspect

switch mode {
case "contain":
resizeMode = .resizeAspect
break
case "none":
resizeMode = .resizeAspect
break
case "cover":
resizeMode = .resizeAspectFill
break
case "stretch":
resizeMode = .resize
break
default:
resizeMode = .resizeAspect
}

if _controls {
_playerViewController?.videoGravity = AVLayerVideoGravity(rawValue: mode ?? "")
_playerViewController?.videoGravity = resizeMode
} else {
_playerLayer?.videoGravity = AVLayerVideoGravity(rawValue: mode ?? "")
_playerLayer?.videoGravity = resizeMode
}

_resizeMode = mode
}

Expand Down
9 changes: 0 additions & 9 deletions ios/Video/RCTVideoManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,6 @@ class RCTVideoManager: RCTViewManager {
})
}

override func constantsToExport() -> [AnyHashable : Any]? {
return [
"ScaleNone": AVLayerVideoGravity.resizeAspect,
"ScaleToFill": AVLayerVideoGravity.resize,
"ScaleAspectFit": AVLayerVideoGravity.resizeAspect,
"ScaleAspectFill": AVLayerVideoGravity.resizeAspectFill
]
}

override class func requiresMainQueueSetup() -> Bool {
return true
}
Expand Down
18 changes: 2 additions & 16 deletions src/Video.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ import React, {
type ComponentRef,
} from 'react';
import {View, StyleSheet, Image, Platform} from 'react-native';
import NativeVideoComponent, {RCTVideoConstants} from './VideoNativeComponent';
import NativeVideoComponent from './VideoNativeComponent';
import type {
NativeVideoResizeMode,
OnAudioFocusChangedData,
OnAudioTracksData,
OnPlaybackStateChangedData,
Expand Down Expand Up @@ -150,19 +149,6 @@ const Video = forwardRef<VideoRef, ReactVideoProps>(
};
}, [source]);

const _resizeMode: NativeVideoResizeMode = useMemo(() => {
switch (resizeMode) {
case 'contain':
return RCTVideoConstants.ScaleAspectFit;
case 'cover':
return RCTVideoConstants.ScaleAspectFill;
case 'stretch':
return RCTVideoConstants.ScaleToFill;
default:
return RCTVideoConstants.ScaleNone;
}
}, [resizeMode]);

const _drm = useMemo(() => {
if (!drm) {
return;
Expand Down Expand Up @@ -476,7 +462,7 @@ const Video = forwardRef<VideoRef, ReactVideoProps>(
src={src}
drm={_drm}
style={StyleSheet.absoluteFill}
resizeMode={_resizeMode}
resizeMode={resizeMode}
fullscreen={isFullscreen}
restoreUserInterfaceForPIPStopCompletionHandler={
_restoreUserInterfaceForPIPStopCompletionHandler
Expand Down
18 changes: 1 addition & 17 deletions src/VideoNativeComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import type {
ViewProps,
} from 'react-native';
import {NativeModules, requireNativeComponent} from 'react-native';
import {getViewManagerConfig} from './utils';

// -------- There are types for native component (future codegen) --------
// if you are looking for types for react component, see src/types/video.ts
Expand Down Expand Up @@ -233,15 +232,13 @@ export type OnVideoErrorData = Readonly<{
export type OnAudioFocusChangedData = Readonly<{
hasFocus: boolean;
}>;

export type NativeVideoResizeMode = unknown;
export interface VideoNativeProps extends ViewProps {
src?: VideoSrc;
drm?: Drm;
adTagUrl?: string;
allowsExternalPlayback?: boolean; // ios, true
maxBitRate?: number;
resizeMode?: NativeVideoResizeMode;
resizeMode?: 'none' | 'cover' | 'contain' | 'stretch';
repeat?: boolean;
automaticallyWaitsToMinimizeStalling?: boolean;
textTracks?: TextTracks;
Expand Down Expand Up @@ -364,22 +361,9 @@ export interface VideoDecoderPropertiesType {
isHEVCSupported: () => Promise<'unsupported' | 'hardware' | 'software'>;
}

export type VideoViewManagerConfig = {
Constants: {
ScaleNone: unknown;
ScaleToFill: unknown;
ScaleAspectFit: unknown;
ScaleAspectFill: unknown;
};
Commands: {[key: string]: number};
};

export const VideoManager = NativeModules.VideoManager as VideoManagerType;
export const VideoDecoderProperties =
NativeModules.VideoDecoderProperties as VideoDecoderPropertiesType;
export const RCTVideoConstants = (
getViewManagerConfig('RCTVideo') as VideoViewManagerConfig
).Constants;

export default requireNativeComponent<VideoNativeProps>(
'RCTVideo',
Expand Down
10 changes: 1 addition & 9 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type {Component, RefObject, ComponentClass} from 'react';
import {Image, UIManager, findNodeHandle} from 'react-native';
import {Image, findNodeHandle} from 'react-native';
import type {ImageSourcePropType} from 'react-native';
import type {ReactVideoSource} from './types/video';

Expand Down Expand Up @@ -35,11 +35,3 @@ export function getReactTag(

return reactTag;
}

export function getViewManagerConfig(name: string) {
if ('getViewManagerConfig' in UIManager) {
return UIManager.getViewManagerConfig(name);
}

return UIManager[name];
}

0 comments on commit 7fd7b3f

Please sign in to comment.