3#include <QLoggingCategory>
4#include <QMediaDevices>
7#include <QDesktopServices>
9#include "ConnectPlayer.h"
11static Q_LOGGING_CATEGORY(log,
"Player.Connect")
16 , m_bScreenShot(false)
21 qDebug(log) << Q_FUNC_INFO;
23 m_pParameters = qobject_cast<CParameterPlayer*>(pConnecter->GetParameter());
26 check = connect(&m_VideoSink, &QVideoSink::videoFrameChanged,
27 pConnecter->GetVideoSink(), &QVideoSink::videoFrameChanged);
32 &m_VideoSink, SIGNAL(videoFrameChanged(
const QVideoFrame&)),
33 this, SLOT(slotVideoFrameChanged(QVideoFrame)));
38 &m_AudioBufferOutput, &QAudioBufferOutput::audioBufferReceived,
39 this, [&](
const QAudioBuffer &buffer){
41 if(QMediaRecorder::RecordingState != m_Recorder.recorderState())
43 bool bRet = m_AudioBufferInput.sendAudioBuffer(buffer);
47 qDebug(log) <<
"m_AudioBufferInput.sendAudioBuffer fail";
53 check = connect(pConnecter, &CConnecterPlayer::sigStart,
54 this, [&](
bool bStart){
62 pConnecter, &CConnecterPlayer::sigPause,
63 this, [&](
bool bPause){
64 switch (m_pParameters->GetType()) {
65 case CParameterPlayer::TYPE::Camera:
67 if(m_pCamera->isActive())
71 if(!m_pCamera->isActive())
75 case CParameterPlayer::TYPE::Url:
77 if(QMediaPlayer::PlayingState == m_Player.playbackState())
81 if(QMediaPlayer::PausedState == m_Player.playbackState())
90 &m_Player, SIGNAL(positionChanged(qint64)),
91 this, SLOT(slotPositionChanged(qint64)));
94 &m_Player, SIGNAL(durationChanged(qint64)),
95 this, SLOT(slotDurationChanged(qint64)));
97 check = connect(pConnecter, SIGNAL(sigChangePosition(qint64)),
98 &m_Player, SLOT(setPosition(qint64)));
100 check = connect(m_pParameters, &CParameterPlayer::sigEnableAudioInput,
101 this, &CConnectPlayer::slotEnableAudioInput);
103 check = connect(m_pParameters, &CParameterPlayer::sigEnableAudioOutput,
104 this, &CConnectPlayer::slotEnableAudioOutput);
108 &m_Player, &QMediaPlayer::errorOccurred,
109 this, [&](QMediaPlayer::Error error,
const QString &errorString){
110 qCritical(log) <<
"Player error occurred:" << error << errorString
111 << m_Player.source();
113 emit sigError(error, errorString);
116 check = connect(&m_Player, &QMediaPlayer::playbackStateChanged,
117 this, [&](QMediaPlayer::PlaybackState state){
118 qDebug(log) <<
"Player state changed:" << state
119 << m_Player.source();
123 check = connect(&m_Player, &QMediaPlayer::errorOccurred,
124 pConnecter, &CConnecterPlayer::slotPlaybackError);
126 check = connect(&m_Player, &QMediaPlayer::playbackStateChanged,
127 pConnecter, &CConnecterPlayer::slotPlaybackStateChanged);
129 check = connect(
this, SIGNAL(sigPositionChanged(qint64,qint64)),
130 pConnecter, SLOT(slotPositionChanged(qint64,qint64)));
133 check = connect(&m_Recorder, &QMediaRecorder::recorderStateChanged,
134 pConnecter, &CConnecterPlayer::slotRecordStateChanged);
139 check = connect(pConnecter, &CConnecterPlayer::sigScreenShot,
141 m_bScreenShot =
true;
147CConnectPlayer::~CConnectPlayer()
149 qDebug(log) << Q_FUNC_INFO;
154 qDebug(log) <<
"CConnectPlayer::OnInit()";
156 return OnInitReturnValue::NotUseOnProcess;
161 qDebug(log) <<
"CConnectPlayer::OnClean()";
167void CConnectPlayer::slotStart()
169 qDebug(log) << Q_FUNC_INFO;
170 slotEnableAudioInput(m_pParameters->GetEnableAudioInput());
171 slotEnableAudioOutput(m_pParameters->GetEnableAudioOutput());
173 switch (m_pParameters->GetType()) {
174 case CParameterPlayer::TYPE::Camera: {
176 const QList<QCameraDevice> cameras = QMediaDevices::videoInputs();
178 || -1 > m_pParameters->GetCamera()
179 || m_pParameters->GetCamera() > QMediaDevices::videoInputs().size())
181 m_pCamera =
new QCamera(cameras.at(m_pParameters->GetCamera()));
182 emit sigServerName(cameras.at(m_pParameters->GetCamera()).description());
185 m_CaptureSession.setVideoSink(&m_VideoSink);
186 m_CaptureSession.setCamera(m_pCamera);
191 case CParameterPlayer::TYPE::Url: {
192 QString szFile = m_pParameters->GetUrl();
193 QFileInfo fi(szFile);
194 emit sigServerName(fi.fileName());
197 url = QUrl::fromLocalFile(szFile);
198 m_Player.setSource(url);
199 m_Player.setVideoSink(&m_VideoSink);
201 if(m_pParameters->m_Record.GetEnableAudio())
202 m_Player.setAudioBufferOutput(&m_AudioBufferOutput);
205 m_nPosition = m_Player.position();
206 m_nDuration = m_Player.duration();
214void CConnectPlayer::slotStop()
216 qDebug(log) << Q_FUNC_INFO;
217 switch (m_pParameters->GetType()) {
218 case CParameterPlayer::TYPE::Camera:
221 m_CaptureSession.setVideoSink(
nullptr);
223 case CParameterPlayer::TYPE::Url:
225 m_Player.setVideoSink(
nullptr);
226 m_Player.setVideoOutput(
nullptr);
242void CConnectPlayer::slotRecord(
bool bRecord)
244 qDebug(log) << Q_FUNC_INFO << bRecord;
247 if(QMediaRecorder::StoppedState != m_Recorder.recorderState()) {
251 auto &record = m_pParameters->m_Record;
252 switch (m_pParameters->GetType()) {
253 case CParameterPlayer::TYPE::Camera: {
254 record >> m_Recorder;
255 m_CaptureSession.setRecorder(&m_Recorder);
259 case CParameterPlayer::TYPE::Url: {
260 record >> m_Recorder;
261 if(record.GetEnableAudio()) {
262 m_CaptureSession.setAudioBufferInput(&m_AudioBufferInput);
264 qDebug(log) <<
"Record: disable audio";
265 if(record.GetEnableVideo())
266 m_CaptureSession.setVideoFrameInput(&m_VideoFrameInput);
268 qDebug(log) <<
"Record: disable video";
269 m_CaptureSession.setRecorder(&m_Recorder);
276#ifndef HAVE_QVideoWidget
277 emit sigRecordVideo(bRecord);
282 if(QMediaRecorder::StoppedState != m_Recorder.recorderState()) {
284 m_CaptureSession.setRecorder(
nullptr);
285 m_CaptureSession.setVideoFrameInput(
nullptr);
286 m_CaptureSession.setAudioBufferInput(
nullptr);
291void CConnectPlayer::slotClipBoardChanged()
295void CConnectPlayer::slotVideoFrameChanged(
const QVideoFrame &frame)
297#ifndef HAVE_QVideoWidget
298 if(m_Video.width() != frame.width()
299 || m_Video.height() != frame.height())
301 m_Video = QRect(0, 0, frame.width(), frame.height());
302 emit sigSetDesktopSize(m_Video.width(), m_Video.height());
304 QImage img(frame.width(), frame.height(), QImage::Format_ARGB32);
305 QPainter painter(&img);
306 const QVideoFrame::PaintOptions option;
307 QVideoFrame f = frame;
308 f.paint(&painter, m_Video, option);
314 m_bScreenShot =
false;
315 QImage image = frame.toImage();
317 qCritical(log) <<
"frame.toImage() fail";
319 QString szFile = m_pParameters->m_Record.GetImageFile(
true);
320 if(!image.save(szFile,
"PNG"))
322 qCritical(log) <<
"Capture image save to file fail." << szFile;
325 qDebug(log) <<
"Capture image to file:" << szFile;
326 qDebug(log) <<
"End action:" << m_pParameters->m_Record.GetEndAction();
327 switch(m_pParameters->m_Record.GetEndAction())
329 case CParameterRecord::ENDACTION::OpenFile: {
330 bool bRet = QDesktopServices::openUrl(QUrl::fromLocalFile(szFile));
332 qCritical(log) <<
"Fail: Open capture image file" << szFile;
335 case CParameterRecord::ENDACTION::OpenFolder: {
336 QFileInfo fi(szFile);
337 QDesktopServices::openUrl(QUrl::fromLocalFile(fi.absolutePath()));
346#if defined(HAVE_QT6_RECORD) && defined(HAVE_QVideoWidget)
347 if(QMediaRecorder::RecordingState == m_Recorder.recorderState()) {
348 bool bRet = m_VideoFrameInput.sendVideoFrame(frame);
352 qDebug(log) <<
"m_VideoFrameInput.sendVideoFrame fail";
358void CConnectPlayer::slotEnableAudioInput(
bool bEnable)
360 if(bEnable && -1 < m_pParameters->GetAudioInput()
361 && m_pParameters->GetAudioInput() < QMediaDevices::audioInputs().size()) {
362 m_AudioInput.setDevice(QMediaDevices::audioInputs()
363 .at(m_pParameters->GetAudioInput()));
364 m_AudioInput.setMuted(m_pParameters->GetAudioInputMuted());
365 m_AudioInput.setVolume(m_pParameters->GetAudioInputVolume());
366 m_CaptureSession.setAudioInput(&m_AudioInput);
368 bool check = connect(m_pParameters,
369 &CParameterPlayer::sigAudioInputMuted,
370 &m_AudioInput, &QAudioInput::setMuted);
372 check = connect(m_pParameters, &CParameterPlayer::sigAudioInputVolume,
373 &m_AudioInput, &QAudioInput::setVolume);
375 check = connect(m_pParameters, &CParameterPlayer::sigAudioInput,
376 this, [&](
int nIndex) {
378 && nIndex < QMediaDevices::audioInputs().size())
379 m_AudioInput.setDevice(
380 QMediaDevices::audioInputs().at(nIndex));
384 qDebug(log) <<
"m_CaptureSession: disable audio input";
385 m_CaptureSession.setAudioInput(
nullptr);
389void CConnectPlayer::slotEnableAudioOutput(
bool bEnable)
391 if(bEnable && (-1 < m_pParameters->GetAudioOutput()
392 && m_pParameters->GetAudioOutput()
393 < QMediaDevices::audioInputs().size()))
395 m_AudioOutput.setDevice(
396 QMediaDevices::audioOutputs()
397 .at(m_pParameters->GetAudioOutput()));
398 m_AudioOutput.setMuted(m_pParameters->GetAudioOutputMuted());
399 m_AudioOutput.setVolume(m_pParameters->GetAudioOutputVolume());
400 m_AudioOutput.disconnect();
401 bool check = connect(m_pParameters,
402 &CParameterPlayer::sigAudioOutputMuted,
403 &m_AudioOutput, &QAudioOutput::setMuted);
405 check = connect(m_pParameters, &CParameterPlayer::sigAudioOutputVolume,
406 &m_AudioOutput, &QAudioOutput::setVolume);
408 check = connect(m_pParameters, &CParameterPlayer::sigAudioOutput,
409 this, [&](
int nIndex) {
411 && nIndex < QMediaDevices::audioOutputs().size())
412 m_AudioOutput.setDevice(
413 QMediaDevices::audioOutputs().at(nIndex));
416 switch (m_pParameters->GetType()) {
417 case CParameterPlayer::TYPE::Camera:
418 m_CaptureSession.setAudioOutput(&m_AudioOutput);
420 case CParameterPlayer::TYPE::Url:
421 m_Player.setAudioOutput(&m_AudioOutput);
427 m_Player.setAudioOutput(
nullptr);
428 m_CaptureSession.setAudioOutput(
nullptr);
429 m_AudioOutput.disconnect();
433void CConnectPlayer::slotPositionChanged(qint64 pos)
438 qint64 currentInfo = pos / 1000;
439 qint64 duration = m_nDuration / 1000;
441 if (currentInfo || duration) {
442 QTime currentTime((currentInfo / 3600) % 60, (currentInfo / 60) % 60, currentInfo % 60,
443 (currentInfo * 1000) % 1000);
444 QTime totalTime((duration / 3600) % 60, (duration / 60) % 60, duration % 60,
445 (duration * 1000) % 1000);
446 QString format =
"mm:ss";
449 szStr = currentTime.toString(format) +
" / " + totalTime.toString(format);
450 emit sigPositionChanged(m_nPosition, m_nDuration);
455void CConnectPlayer::slotDurationChanged(qint64 duration)
458 m_nDuration = duration;
Remote desktop connect interface.
void sigUpdateRect(const QRect &r, const QImage &image)
Notify the CFrmView update image.
virtual int OnClean() override
Clean.
virtual OnInitReturnValue OnInit() override
Specific plug-in realizes connection initialization.
void sigConnected()
Emitted when the plugin is successfully connected.
void sigDisconnected()
Successful disconnection signal.