{"size": 1158, "ext": "js", "max_stars_repo_path": "src/components/CallVideo/CallVideoStream.js", "max_stars_repo_name": "gadzhimari/dialog-web-components", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/components/CallVideo/CallVideoStream.js", "max_issues_repo_name": "gadzhimari/dialog-web-components", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/components/CallVideo/CallVideoStream.js", "max_forks_repo_name": "gadzhimari/dialog-web-components", "max_forks_repo_forks_event_min_datetime": "2018-10-01T11:58:33.000Z", "max_forks_repo_forks_event_max_datetime": "2018-10-01T11:58:33.000Z"}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": 1, "avg_line_length": 18.380952381, "max_line_length": 59, "alphanum_fraction": 0.5872193437}
/*
 * Copyright 2018 dialog LLC <info@dlg.im>
 * @flow
 */

import React, { PureComponent } from 'react';
import classNames from 'classnames';
import styles from './CallVideo.css';

type Props = {
  className: string,
  stream: MediaSource,
  isMirrored: boolean
};

class CallVideoStream extends PureComponent<Props> {
  video: ?HTMLVideoElement;

  componentDidMount() {
    const { video } = this;
    if (video) {
      if ('srcObject' in video) {
        video.srcObject = this.props.stream;
      } else {
        video.src = URL.createObjectURL(this.props.stream);
      }

      video.play();
    }
  }

  componentWillUnmount() {
    const { video } = this;
    if (video) {
      if ('srcObject' in video) {
        video.srcObject = null;
      } else {
        URL.revokeObjectURL(video.src);
      }
    }
  }

  setVideo = (video: *) => {
    this.video = video;
  };

  render() {
    const className = classNames(
      this.props.className,
      this.props.isMirrored ? styles.mirrored : null
    );

    return (
      <video
        ref={this.setVideo}
        className={className}
      />
    );
  }
}

export default CallVideoStream;
