VideoPlayer.cpp 9.66 KB
#include <QUrl>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QSize>
#include <QMediaContent>

#include "VideoPlayer.h"

/////////////////
// constructor //
/////////////////

VideoPlayer::VideoPlayer(int nVignettes, QWidget *parent)
  : QWidget(parent),
    m_segIdx(0)
{
  m_player = new QMediaPlayer(this);

  m_movieMonitor = new MovieMonitor;

  m_vignetteWidget = new VignetteWidget(nVignettes, 480);

  m_defaultLabel = new QLabel;
  m_defaultLabel->setPixmap(QPixmap("pictures/LogoLIA2.png"));

  m_videoWidget = new VideoWidget;
  m_player->setVideoOutput(m_videoWidget);

  m_subLabel = new QLabel;
  m_subLabel->setAlignment(Qt::AlignHCenter);

  QHBoxLayout *audioLayout = new QHBoxLayout;
  audioLayout->addWidget(m_subLabel);

  m_controls = new PlayerControls;

  m_probe = new QVideoProbe;
  m_probe->setSource(m_player);

  m_segments.push_back(QPair<qint64, qint64>(0, 5000000));

  QVBoxLayout *vLayout = new QVBoxLayout;
  vLayout->addWidget(m_vignetteWidget);
  vLayout->addWidget(m_defaultLabel);
  vLayout->addWidget(m_videoWidget);
  vLayout->addLayout(audioLayout);
  vLayout->addWidget(m_controls);

  QHBoxLayout *layout = new QHBoxLayout;
  layout->addWidget(m_movieMonitor);
  layout->addLayout(vLayout);
  setLayout(layout);
  
  m_videoWidget->hide();
  m_controls->setEnabled(false);
  
  connect(m_controls, SIGNAL(play()), m_player, SLOT(play()));
  connect(m_controls, SIGNAL(pause()), m_player, SLOT(pause()));
  connect(m_controls, SIGNAL(stop()), m_player, SLOT(stop()));
  connect(m_controls, SIGNAL(stop()), m_videoWidget, SLOT(update()));
  connect(m_controls, SIGNAL(setMuted(bool)), m_player, SLOT(setMuted(bool)));
  connect(m_controls, SIGNAL(positionManuallyChanged(qint64)), this, SLOT(positionManuallyChanged(qint64)));
  connect(m_controls, SIGNAL(playbackRateChanged(qreal)), m_player, SLOT(setPlaybackRate(qreal)));

  connect(m_player, SIGNAL(durationChanged(qint64)), m_controls, SLOT(durationChanged(qint64)));
  connect(m_player, SIGNAL(durationChanged(qint64)), m_movieMonitor, SLOT(durationChanged(qint64)));
  connect(m_player, SIGNAL(positionChanged(qint64)), m_controls, SLOT(positionChanged(qint64)));
  connect(m_player, SIGNAL(positionChanged(qint64)), this, SLOT(positionChanged(qint64)));
  connect(m_player, SIGNAL(positionChanged(qint64)), m_movieMonitor, SLOT(positionChanged(qint64)));
  connect(m_player, SIGNAL(stateChanged(QMediaPlayer::State)), this, SLOT(stateChanged(QMediaPlayer::State)));
  connect(m_player, SIGNAL(mediaStatusChanged(QMediaPlayer::MediaStatus)), m_controls, SLOT(mediaStatusChanged(QMediaPlayer::MediaStatus)));
  connect(m_probe, SIGNAL(videoFrameProbed(const QVideoFrame &)), m_movieMonitor, SLOT(processFrame(QVideoFrame)));
  connect(this, SIGNAL(showHisto(bool)), m_movieMonitor, SLOT(showHisto(bool)));
  connect(this, SIGNAL(viewSegmentation(bool, bool)), m_movieMonitor, SLOT(viewSegmentation(bool, bool)));
  connect(this, SIGNAL(showUtteranceTree(bool)), m_movieMonitor, SLOT(viewUtteranceTree(bool)));
  connect(this, SIGNAL(initDiarData(const arma::mat &, const arma::mat &, const arma::mat &)), m_movieMonitor, SLOT(setDiarData(const arma::mat &, const arma::mat &, const arma::mat &)));
  connect(this, SIGNAL(grabCurrentPattern(const QList<QPair<int, qreal>> &)), m_movieMonitor, SLOT(grabCurrentPattern(const QList<QPair<int, qreal>> &)));
  connect(this, SIGNAL(grabPatternFirstShot(const QList<QPair<int, qreal>> &)), m_movieMonitor, SLOT(grabPatternFirstShot(const QList<QPair<int, qreal>> &)));
  connect(this, SIGNAL(grabPatternSecondShot(const QList<QPair<int, qreal>> &)), m_movieMonitor, SLOT(grabPatternSecondShot(const QList<QPair<int, qreal>> &)));
  connect(this, SIGNAL(showSpeakers(bool)), m_movieMonitor, SLOT(viewSpeakers(bool)));
  connect(this, SIGNAL(grabSpokenFrame(qint64, const QString &, const QString &)), m_movieMonitor, SLOT(getSpokenFrame(qint64, const QString &, const QString &)));
  connect(this, SIGNAL(grabShot(Segment *)), m_movieMonitor, SLOT(getShot(Segment *)));
  connect(this, SIGNAL(segmentationGrabbed()), m_movieMonitor, SLOT(segmentationRetrieved()));
  connect(this, SIGNAL(speakersGrabbed(QList<QString>)), m_movieMonitor, SLOT(speakersGrabbed(QList<QString>)));
  connect(m_movieMonitor, SIGNAL(playbackSegments(QList<QPair<qint64, qint64>>)), this, SLOT(playSegments(QList<QPair<qint64, qint64>>)));
  connect(m_movieMonitor, SIGNAL(newSpeaker(qint64, qint64, const QString &, VideoFrame::SpeakerSource)), this, SLOT(setSpeaker(qint64, qint64, const QString &, VideoFrame::SpeakerSource)));
  connect(m_movieMonitor, SIGNAL(renewSpeaker(qint64, qint64, qint64, qint64, bool, VideoFrame::SpeakerSource)), this, SLOT(resetSpeaker(qint64, qint64, qint64, qint64, bool, VideoFrame::SpeakerSource)));
  connect(m_movieMonitor, SIGNAL(playSubtitle(QList<int>)), this, SLOT(playSubtitle(QList<int>)));
  connect(this, SIGNAL(currSubtitle(int)), m_movieMonitor, SLOT(currSubtitle(int)));
  connect(this, SIGNAL(releasePos(bool)), m_movieMonitor, SLOT(releasePos(bool)));
}

////////////////
// destructor //
////////////////

VideoPlayer::~VideoPlayer()
{
}

void VideoPlayer::reset()
{
  m_player->setMedia(QMediaContent());
  m_videoWidget->hide();
  m_defaultLabel->show();
  m_controls->reset();
  update();
}

///////////
// slots //
///////////

void VideoPlayer::setPlayer(const QString &fName, const QSize &resolution)
{
  QMediaContent mediaContent(QUrl::fromLocalFile(fName));

  if (m_player->media() != mediaContent) {
    m_vignetteWidget->setVideoCapture(fName);
    m_player->setMedia(mediaContent);
    m_player->pause();
    m_player->setNotifyInterval(40);
    m_videoWidget->setFixedSize(resolution);
    m_defaultLabel->hide();
    m_videoWidget->show();
    m_controls->setEnabled(true);
  }
}

void VideoPlayer::setPlayerPosition(qint64 position)
{
  if (m_player->state() == QMediaPlayer::PausedState || m_player->state() == QMediaPlayer::StoppedState) {
    m_player->pause();
    m_player->setPosition(position);
  }
}

void VideoPlayer::positionChanged(qint64 position)
{
  if (m_player->state() == QMediaPlayer::PlayingState) {

    // playing current segment
    if (position < m_segments[m_segIdx].second) {
      emit positionUpdated(position);
      emit releasePos(false);
    }

    // end of segment
    else {
      m_player->setPosition(m_segments[m_segIdx].second);
      m_segIdx++;

      // no more segment to play
      if (m_segIdx == m_segments.size()) {
	m_segments.clear();
	m_segments.push_back(QPair<qint64, qint64>(0, m_player->duration()));
	m_segIdx = 0;
	m_player->pause();
	m_controls->playClicked();
	emit releasePos(true);
      }

      // playing following segments
      else {
	m_player->pause();
	m_player->setPosition(m_segments[m_segIdx].first);
	emit releasePos(false);
	m_player->play();
      }
    }
  }
}

void VideoPlayer::stateChanged(QMediaPlayer::State state)
{
  if (state == QMediaPlayer::PlayingState)
    emit playerPaused(false);
  else if (state == QMediaPlayer::PausedState)
    emit playerPaused(true);
}

void VideoPlayer::positionManuallyChanged(qint64 position)
{
  m_player->pause();
  m_player->setPosition(position);
  emit positionUpdated(position);
}

void VideoPlayer::activeHisto(bool dispHist)
{
  emit showHisto(dispHist);
}

void VideoPlayer::currentShot(QList<qint64> positionList)
{
  emit updateVignette(positionList);
}

void VideoPlayer::showVignette(bool showed)
{
  if (showed) {
    connect(this, SIGNAL(updateVignette(QList<qint64>)), m_vignetteWidget, SLOT(updateVignette(QList<qint64>)));
    m_vignetteWidget->show();
  }
  else {
    disconnect(this, SIGNAL(updateVignette(QList<qint64>)), m_vignetteWidget, SLOT(updateVignette(QList<qint64>)));
    m_vignetteWidget->hide();
  }
}

void VideoPlayer::displaySubtitle(const QString &subtitle)
{
  m_subLabel->setText(subtitle);
}

void VideoPlayer::showSegmentation(bool checked, bool annot)
{
  emit viewSegmentation(checked, annot);
}

void VideoPlayer::viewUtteranceTree(bool checked)
{
  emit showUtteranceTree(checked);
}


void VideoPlayer::setDiarData(const arma::mat &E, const arma::mat &Sigma, const arma::mat &W)
{
  emit initDiarData(E, Sigma, W);
}

void VideoPlayer::getCurrentPattern(const QList<QPair<int, qreal>> &subFeatures)
{
  emit grabCurrentPattern(subFeatures);
}

void VideoPlayer::getPatternFirstShot(const QList<QPair<int, qreal>> &subFeatures)
{
  emit grabPatternFirstShot(subFeatures);
}

void VideoPlayer::getPatternSecondShot(const QList<QPair<int, qreal>> &subFeatures)
{
  emit grabPatternSecondShot(subFeatures);
}

void VideoPlayer::viewSpeakers(bool checked)
{
  emit showSpeakers(checked);
}

void VideoPlayer::speakersRetrieved(QList<QString> speakers)
{
  emit speakersGrabbed(speakers);
}

void VideoPlayer::getShot(Segment *shot)
{
  emit grabShot(shot);
}

void VideoPlayer::segmentationRetrieved()
{
  emit segmentationGrabbed();
}

void VideoPlayer::getSpokenFrame(qint64 position, const QString &text, const QString &speaker)
{
  emit grabSpokenFrame(position, text, speaker);
}

void VideoPlayer::playSegments(QList<QPair<qint64, qint64>> utterances)
{
  if (m_player->state() == QMediaPlayer::PausedState) {
    m_segments = utterances;
    m_segIdx = 0;
  
    m_player->setPosition(m_segments[0].first);
    m_player->play();
    m_controls->playClicked();
  }
}

void VideoPlayer::setSpeaker(qint64 start, qint64 end, const QString &speaker, VideoFrame::SpeakerSource source)
{
  emit newSpeaker(start, end, speaker, source);
}

void VideoPlayer::resetSpeaker(qint64 prevStart, qint64 prevEnd, qint64 start, qint64 end, bool resetSub, VideoFrame::SpeakerSource source)
{
  emit renewSpeaker(prevStart, prevEnd, start, end, resetSub, source);
}

void VideoPlayer::playSubtitle(QList<int> utter)
{
  emit playSub(utter);
}

void VideoPlayer::currentSubtitle(int subIdx)
{
  emit currSubtitle(subIdx);
}