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

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