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.8KB

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