You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Video.js 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // @flow
  2. import PropTypes from 'prop-types';
  3. import React, { Component } from 'react';
  4. import { RTCView } from 'react-native-webrtc';
  5. import { Pressable } from '../../../react';
  6. import styles from './styles';
  7. import VideoTransform from './VideoTransform';
  8. /**
  9. * The React Native {@link Component} which is similar to Web's
  10. * {@code HTMLVideoElement} and wraps around react-native-webrtc's
  11. * {@link RTCView}.
  12. */
  13. export default class Video extends Component<*> {
  14. /**
  15. * {@code Video} component's property types.
  16. *
  17. * @static
  18. */
  19. static propTypes = {
  20. mirror: PropTypes.bool,
  21. onPlaying: PropTypes.func,
  22. /**
  23. * Callback to invoke when the {@code Video} is clicked/pressed.
  24. */
  25. onPress: PropTypes.func,
  26. stream: PropTypes.object,
  27. /**
  28. * Similarly to the CSS property z-index, specifies the z-order of this
  29. * Video in the stacking space of all Videos. When Videos overlap,
  30. * zOrder determines which one covers the other. A Video with a larger
  31. * zOrder generally covers a Video with a lower one.
  32. *
  33. * Non-overlapping Videos may safely share a z-order (because one does
  34. * not have to cover the other).
  35. *
  36. * The support for zOrder is platform-dependent and/or
  37. * implementation-specific. Thus, specifying a value for zOrder is to be
  38. * thought of as giving a hint rather than as imposing a requirement.
  39. * For example, video renderers such as Video are commonly implemented
  40. * using OpenGL and OpenGL views may have different numbers of layers in
  41. * their stacking space. Android has three: a layer bellow the window
  42. * (aka default), a layer bellow the window again but above the previous
  43. * layer (aka media overlay), and above the window. Consequently, it is
  44. * advisable to limit the number of utilized layers in the stacking
  45. * space to the minimum sufficient for the desired display. For example,
  46. * a video call application usually needs a maximum of two zOrder
  47. * values: 0 for the remote video(s) which appear in the background, and
  48. * 1 for the local video(s) which appear above the remote video(s).
  49. */
  50. zOrder: PropTypes.number,
  51. /**
  52. * Indicates whether zooming (pinch to zoom and/or drag) is enabled.
  53. */
  54. zoomEnabled: PropTypes.bool
  55. };
  56. /**
  57. * React Component method that executes once component is mounted.
  58. *
  59. * @inheritdoc
  60. */
  61. componentDidMount() {
  62. // RTCView currently does not support media events, so just fire
  63. // onPlaying callback when <RTCView> is rendered.
  64. const { onPlaying } = this.props;
  65. onPlaying && onPlaying();
  66. }
  67. /**
  68. * Implements React's {@link Component#render()}.
  69. *
  70. * @inheritdoc
  71. * @returns {ReactElement|null}
  72. */
  73. render() {
  74. const { onPress, stream, zoomEnabled } = this.props;
  75. if (stream) {
  76. // RTCView
  77. const style = styles.video;
  78. const objectFit
  79. = zoomEnabled
  80. ? 'contain'
  81. : (style && style.objectFit) || 'cover';
  82. const rtcView
  83. = (
  84. <RTCView
  85. mirror = { this.props.mirror }
  86. objectFit = { objectFit }
  87. streamURL = { stream.toURL() }
  88. style = { style }
  89. zOrder = { this.props.zOrder } />
  90. );
  91. // VideoTransform implements "pinch to zoom". As part of "pinch to
  92. // zoom", it implements onPress, of course.
  93. if (zoomEnabled) {
  94. return (
  95. <VideoTransform
  96. enabled = { zoomEnabled }
  97. onPress = { onPress }
  98. streamId = { stream.id }
  99. style = { style }>
  100. { rtcView }
  101. </VideoTransform>
  102. );
  103. }
  104. // XXX Unfortunately, VideoTransform implements a custom press
  105. // detection which has been observed to be very picky about the
  106. // precision of the press unlike the builtin/default/standard press
  107. // detection which is forgiving to imperceptible movements while
  108. // pressing. It's not acceptable to be so picky, especially when
  109. // "pinch to zoom" is not enabled.
  110. return (
  111. <Pressable onPress = { onPress }>
  112. { rtcView }
  113. </Pressable>
  114. );
  115. }
  116. // RTCView has peculiarities which may or may not be platform specific.
  117. // For example, it doesn't accept an empty streamURL. If the execution
  118. // reached here, it means that we explicitly chose to not initialize an
  119. // RTCView as a way of dealing with its idiosyncrasies.
  120. return null;
  121. }
  122. }