您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

Video.js 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import React, { Component } from 'react';
  2. import { View } from 'react-native';
  3. import { RTCView } from 'react-native-webrtc';
  4. import { Platform } from '../../../react';
  5. import { styles } from './styles';
  6. /**
  7. * Indicates whether RTCView (is to be considered that it) natively supports
  8. * i.e. implements mirroring the video it renders. If false, a workaround will
  9. * be used in an attempt to support mirroring in Video. If RTCView does not
  10. * implement mirroring on a specific platform but the workaround causes issues,
  11. * set to true for that platform to disable the workaround.
  12. */
  13. const RTCVIEW_SUPPORTS_MIRROR = Platform.OS === 'android';
  14. /**
  15. * The React Native component which is similar to Web's video element and wraps
  16. * around react-native-webrtc's RTCView.
  17. */
  18. export class Video extends Component {
  19. /**
  20. * Video component's property types.
  21. *
  22. * @static
  23. */
  24. static propTypes = {
  25. mirror: React.PropTypes.bool,
  26. muted: React.PropTypes.bool,
  27. onPlaying: React.PropTypes.func,
  28. stream: React.PropTypes.object,
  29. /**
  30. * Similarly to the CSS property z-index, specifies the z-order of this
  31. * Video in the stacking space of all Videos. When Videos overlap,
  32. * zOrder determines which one covers the other. A Video with a larger
  33. * zOrder generally covers a Video with a lower one.
  34. *
  35. * Non-overlapping Videos may safely share a z-order (because one does
  36. * not have to cover the other).
  37. *
  38. * The support for zOrder is platform-dependent and/or
  39. * implementation-specific. Thus, specifying a value for zOrder is to be
  40. * thought of as giving a hint rather than as imposing a requirement.
  41. * For example, video renderers such as Video are commonly implemented
  42. * using OpenGL and OpenGL views may have different numbers of layers in
  43. * their stacking space. Android has three: a layer bellow the window
  44. * (aka default), a layer bellow the window again but above the previous
  45. * layer (aka media overlay), and above the window. Consequently, it is
  46. * advisable to limit the number of utilized layers in the stacking
  47. * space to the minimum sufficient for the desired display. For example,
  48. * a video call application usually needs a maximum of two zOrder
  49. * values: 0 for the remote video(s) which appear in the background, and
  50. * 1 for the local video(s) which appear above the remote video(s).
  51. */
  52. zOrder: React.PropTypes.number
  53. }
  54. /**
  55. * React Component method that executes once component is mounted.
  56. *
  57. * @inheritdoc
  58. */
  59. componentDidMount() {
  60. // RTCView currently does not support media events, so just fire
  61. // onPlaying callback when <RTCView> is rendered.
  62. if (this.props.onPlaying) {
  63. this.props.onPlaying();
  64. }
  65. }
  66. /**
  67. * Implements React's {@link Component#render()}.
  68. *
  69. * @inheritdoc
  70. * @returns {ReactElement|null}
  71. */
  72. render() {
  73. const stream = this.props.stream;
  74. if (stream) {
  75. const streamURL = stream.toURL();
  76. // XXX The CSS style object-fit that we utilize on Web is not
  77. // supported on React Native. Adding objectFit to React Native's
  78. // StyleSheet appears to be impossible without hacking and an
  79. // unjustified amount of effort. Consequently, I've chosen to define
  80. // objectFit on RTCView itself. Anyway, prepare to accommodate a
  81. // future definition of objectFit in React Native's StyleSheet.
  82. const style = styles.video;
  83. const objectFit = (style && style.objectFit) || 'cover';
  84. const mirror = this.props.mirror;
  85. // XXX RTCView doesn't currently support mirroring, even when
  86. // providing a transform style property. As a workaround, wrap the
  87. // RTCView inside another View and apply the transform style
  88. // property to that View instead.
  89. const mirrorWorkaround = mirror && !RTCVIEW_SUPPORTS_MIRROR;
  90. // eslint-disable-next-line no-extra-parens
  91. const video = (
  92. <RTCView
  93. mirror = { !mirrorWorkaround }
  94. objectFit = { objectFit }
  95. streamURL = { streamURL }
  96. style = { style }
  97. zOrder = { this.props.zOrder } />
  98. );
  99. if (mirrorWorkaround) {
  100. return (
  101. <View style = { styles.mirroredVideo }>{ video }</View>
  102. );
  103. }
  104. return video;
  105. }
  106. // RTCView has peculiarities which may or may not be platform specific.
  107. // For example, it doesn't accept an empty streamURL. If the execution
  108. // reached here, it means that we explicitly chose to not initialize an
  109. // RTCView as a way of dealing with its idiosyncrasies.
  110. return null;
  111. }
  112. }