3#include <QLoggingCategory>
4#include <QMediaDevices>
7#include <QDesktopServices>
11#include "BackendPlayer.h"
13static Q_LOGGING_CATEGORY(log,
"Player.Backend")
18 , m_bScreenShot(false)
23 qDebug(log) << Q_FUNC_INFO;
25 m_pParameters = qobject_cast<CParameterPlayer*>(pOperate->GetParameter());
28 check = connect(&m_VideoSink, &QVideoSink::videoFrameChanged,
29 pOperate->GetVideoSink(), &QVideoSink::videoFrameChanged);
34 &m_VideoSink, SIGNAL(videoFrameChanged(
const QVideoFrame&)),
35 this, SLOT(slotVideoFrameChanged(QVideoFrame)));
40 &m_AudioBufferOutput, &QAudioBufferOutput::audioBufferReceived,
41 this, [&](
const QAudioBuffer &buffer){
43 if(QMediaRecorder::RecordingState != m_Recorder.recorderState())
45 bool bRet = m_AudioBufferInput.sendAudioBuffer(buffer);
49 qDebug(log) <<
"m_AudioBufferInput.sendAudioBuffer fail";
55 check = connect(pOperate, &COperatePlayer::sigStart,
56 this, [&](
bool bStart){
64 pOperate, &COperatePlayer::sigPause,
65 this, [&](
bool bPause){
66 switch (m_pParameters->GetType()) {
67 case CParameterPlayer::TYPE::Camera:
69 if(m_pCamera->isActive())
73 if(!m_pCamera->isActive())
77 case CParameterPlayer::TYPE::Url:
79 if(QMediaPlayer::PlayingState == m_Player.playbackState())
83 if(QMediaPlayer::PausedState == m_Player.playbackState())
92 &m_Player, SIGNAL(positionChanged(qint64)),
93 this, SLOT(slotPositionChanged(qint64)));
96 &m_Player, SIGNAL(durationChanged(qint64)),
97 this, SLOT(slotDurationChanged(qint64)));
99 check = connect(pOperate, SIGNAL(sigChangePosition(qint64)),
100 &m_Player, SLOT(setPosition(qint64)));
102 check = connect(m_pParameters, &CParameterPlayer::sigEnableAudioInput,
103 this, &CBackendPlayer::slotEnableAudioInput);
105 check = connect(m_pParameters, &CParameterPlayer::sigEnableAudioOutput,
106 this, &CBackendPlayer::slotEnableAudioOutput);
110 &m_Player, &QMediaPlayer::errorOccurred,
111 this, [&](QMediaPlayer::Error error,
const QString &errorString){
112 qCritical(log) <<
"Player error occurred:" << error << errorString
113 << m_Player.source();
115 emit sigError(error, errorString);
118 check = connect(&m_Player, &QMediaPlayer::playbackStateChanged,
119 this, [&](QMediaPlayer::PlaybackState state){
120 qDebug(log) <<
"Player state changed:" << state
121 << m_Player.source();
125 check = connect(&m_Player, &QMediaPlayer::errorOccurred,
126 pOperate, &COperatePlayer::slotPlaybackError);
128 check = connect(&m_Player, &QMediaPlayer::playbackStateChanged,
129 pOperate, &COperatePlayer::slotPlaybackStateChanged);
131 check = connect(
this, SIGNAL(sigPositionChanged(qint64,qint64)),
132 pOperate, SLOT(slotPositionChanged(qint64,qint64)));
135 check = connect(&m_Recorder, &QMediaRecorder::recorderStateChanged,
136 pOperate, &COperatePlayer::slotRecordStateChanged);
141 check = connect(pOperate, &COperatePlayer::sigScreenShot,
143 m_bScreenShot =
true;
149CBackendPlayer::~CBackendPlayer()
151 qDebug(log) << Q_FUNC_INFO;
156 qDebug(log) << Q_FUNC_INFO;
158 return OnInitReturnValue::NotUseOnProcess;
163 qDebug(log) << Q_FUNC_INFO;
169void CBackendPlayer::slotStart()
171 qDebug(log) << Q_FUNC_INFO;
172 slotEnableAudioInput(m_pParameters->GetEnableAudioInput());
173 slotEnableAudioOutput(m_pParameters->GetEnableAudioOutput());
175 switch (m_pParameters->GetType()) {
176 case CParameterPlayer::TYPE::Camera: {
178 const QList<QCameraDevice> cameras = QMediaDevices::videoInputs();
180 || -1 > m_pParameters->GetCamera()
181 || m_pParameters->GetCamera() > QMediaDevices::videoInputs().size())
183 m_pCamera =
new QCamera(cameras.at(m_pParameters->GetCamera()));
184 emit sigServerName(cameras.at(m_pParameters->GetCamera()).description());
187 m_CaptureSession.setVideoSink(&m_VideoSink);
188 m_CaptureSession.setCamera(m_pCamera);
193 case CParameterPlayer::TYPE::Url: {
194 QString szFile = m_pParameters->GetUrl();
195 QFileInfo fi(szFile);
196 emit sigServerName(fi.fileName());
199 url = QUrl::fromLocalFile(szFile);
200 m_Player.setSource(url);
201 m_Player.setVideoSink(&m_VideoSink);
203 if(m_pParameters->m_Record.GetEnableAudio())
204 m_Player.setAudioBufferOutput(&m_AudioBufferOutput);
207 m_nPosition = m_Player.position();
208 m_nDuration = m_Player.duration();
216void CBackendPlayer::slotStop()
218 qDebug(log) << Q_FUNC_INFO;
219 switch (m_pParameters->GetType()) {
220 case CParameterPlayer::TYPE::Camera:
223 m_CaptureSession.setVideoSink(
nullptr);
225 case CParameterPlayer::TYPE::Url:
227 m_Player.setVideoSink(
nullptr);
228 m_Player.setVideoOutput(
nullptr);
244void CBackendPlayer::slotRecord(
bool bRecord)
246 qDebug(log) << Q_FUNC_INFO << bRecord;
249 if(QMediaRecorder::StoppedState != m_Recorder.recorderState()) {
253 auto &record = m_pParameters->m_Record;
254 switch (m_pParameters->GetType()) {
255 case CParameterPlayer::TYPE::Camera: {
256 record >> m_Recorder;
257 m_CaptureSession.setRecorder(&m_Recorder);
261 case CParameterPlayer::TYPE::Url: {
262 record >> m_Recorder;
263 if(record.GetEnableAudio()) {
264 m_CaptureSession.setAudioBufferInput(&m_AudioBufferInput);
266 qDebug(log) <<
"Record: disable audio";
267 if(record.GetEnableVideo())
268 m_CaptureSession.setVideoFrameInput(&m_VideoFrameInput);
270 qDebug(log) <<
"Record: disable video";
271 m_CaptureSession.setRecorder(&m_Recorder);
278#ifndef HAVE_QVideoWidget
279 emit sigRecordVideo(bRecord, m_pParameters->m_Record.GetVideoFrameRate());
284 if(QMediaRecorder::StoppedState != m_Recorder.recorderState()) {
286 m_CaptureSession.setRecorder(
nullptr);
287 m_CaptureSession.setVideoFrameInput(
nullptr);
288 m_CaptureSession.setAudioBufferInput(
nullptr);
293void CBackendPlayer::slotClipBoardChanged()
297void CBackendPlayer::slotVideoFrameChanged(
const QVideoFrame &frame)
299#ifndef HAVE_QVideoWidget
300 if(m_Video.width() != frame.width()
301 || m_Video.height() != frame.height())
303 m_Video = QRect(0, 0, frame.width(), frame.height());
304 emit sigSetDesktopSize(m_Video.width(), m_Video.height());
306 QImage img(frame.width(), frame.height(), QImage::Format_ARGB32);
307 QPainter painter(&img);
308 const QVideoFrame::PaintOptions option;
309 QVideoFrame f = frame;
310 f.paint(&painter, m_Video, option);
316 m_bScreenShot =
false;
317 QImage image = frame.toImage();
319 qCritical(log) <<
"frame.toImage() fail";
321 QString szFile = m_pParameters->m_Record.GetImageFile(
true);
322 if(!image.save(szFile,
"PNG"))
324 qCritical(log) <<
"Capture image save to file fail." << szFile;
327 qDebug(log) <<
"Capture image to file:" << szFile;
328 qDebug(log) <<
"End action:" << m_pParameters->m_Record.GetEndAction();
329 switch(m_pParameters->m_Record.GetEndAction())
331 case CParameterRecord::ENDACTION::OpenFile: {
332 bool bRet = QDesktopServices::openUrl(QUrl::fromLocalFile(szFile));
334 qCritical(log) <<
"Fail: Open capture image file" << szFile;
337 case CParameterRecord::ENDACTION::OpenFolder: {
338 QFileInfo fi(szFile);
339 QDesktopServices::openUrl(QUrl::fromLocalFile(fi.absolutePath()));
348#if defined(HAVE_QT6_RECORD) && defined(HAVE_QVideoWidget)
349 if(QMediaRecorder::RecordingState == m_Recorder.recorderState()) {
350 bool bRet = m_VideoFrameInput.sendVideoFrame(frame);
354 qDebug(log) <<
"m_VideoFrameInput.sendVideoFrame fail";
360void CBackendPlayer::slotEnableAudioInput(
bool bEnable)
362 if(bEnable && -1 < m_pParameters->GetAudioInput()
363 && m_pParameters->GetAudioInput() < QMediaDevices::audioInputs().size()) {
364 m_AudioInput.setDevice(QMediaDevices::audioInputs()
365 .at(m_pParameters->GetAudioInput()));
366 m_AudioInput.setMuted(m_pParameters->GetAudioInputMuted());
367 m_AudioInput.setVolume(m_pParameters->GetAudioInputVolume());
368 m_CaptureSession.setAudioInput(&m_AudioInput);
370 bool check = connect(m_pParameters,
371 &CParameterPlayer::sigAudioInputMuted,
372 &m_AudioInput, &QAudioInput::setMuted);
374 check = connect(m_pParameters, &CParameterPlayer::sigAudioInputVolume,
375 &m_AudioInput, &QAudioInput::setVolume);
377 check = connect(m_pParameters, &CParameterPlayer::sigAudioInput,
378 this, [&](
int nIndex) {
380 && nIndex < QMediaDevices::audioInputs().size())
381 m_AudioInput.setDevice(
382 QMediaDevices::audioInputs().at(nIndex));
386 qDebug(log) <<
"m_CaptureSession: disable audio input";
387 m_CaptureSession.setAudioInput(
nullptr);
391void CBackendPlayer::slotEnableAudioOutput(
bool bEnable)
393 if(bEnable && (-1 < m_pParameters->GetAudioOutput()
394 && m_pParameters->GetAudioOutput()
395 < QMediaDevices::audioInputs().size()))
397 m_AudioOutput.setDevice(
398 QMediaDevices::audioOutputs()
399 .at(m_pParameters->GetAudioOutput()));
400 m_AudioOutput.setMuted(m_pParameters->GetAudioOutputMuted());
401 m_AudioOutput.setVolume(m_pParameters->GetAudioOutputVolume());
402 m_AudioOutput.disconnect();
403 bool check = connect(m_pParameters,
404 &CParameterPlayer::sigAudioOutputMuted,
405 &m_AudioOutput, &QAudioOutput::setMuted);
407 check = connect(m_pParameters, &CParameterPlayer::sigAudioOutputVolume,
408 &m_AudioOutput, &QAudioOutput::setVolume);
410 check = connect(m_pParameters, &CParameterPlayer::sigAudioOutput,
411 this, [&](
int nIndex) {
413 && nIndex < QMediaDevices::audioOutputs().size())
414 m_AudioOutput.setDevice(
415 QMediaDevices::audioOutputs().at(nIndex));
418 switch (m_pParameters->GetType()) {
419 case CParameterPlayer::TYPE::Camera:
420 m_CaptureSession.setAudioOutput(&m_AudioOutput);
422 case CParameterPlayer::TYPE::Url:
423 m_Player.setAudioOutput(&m_AudioOutput);
429 m_Player.setAudioOutput(
nullptr);
430 m_CaptureSession.setAudioOutput(
nullptr);
431 m_AudioOutput.disconnect();
435void CBackendPlayer::slotPositionChanged(qint64 pos)
440 qint64 currentInfo = pos / 1000;
441 qint64 duration = m_nDuration / 1000;
443 if (currentInfo || duration) {
444 QTime currentTime((currentInfo / 3600) % 60, (currentInfo / 60) % 60, currentInfo % 60,
445 (currentInfo * 1000) % 1000);
446 QTime totalTime((duration / 3600) % 60, (duration / 60) % 60, duration % 60,
447 (duration * 1000) % 1000);
448 QString format =
"mm:ss";
451 szStr = currentTime.toString(format) +
" / " + totalTime.toString(format);
452 emit sigPositionChanged(m_nPosition, m_nDuration);
457void CBackendPlayer::slotDurationChanged(qint64 duration)
460 m_nDuration = duration;
Remote desktop interface.
void sigUpdateRect(const QRect &r, const QImage &image)
Notify the CFrmView update image.
virtual int OnClean() override
Clean.
virtual OnInitReturnValue OnInit() override
Initialization.
void sigRunning()
Emitted when the plugin is successfully started.
void sigFinished()
Successful stopped signal.