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::playbackStateChanged,
124 pConnecter, &CConnecterPlayer::slotPlaybackStateChanged);
126 check = connect(
this, SIGNAL(sigPositionChanged(qint64,qint64)),
127 pConnecter, SLOT(slotPositionChanged(qint64,qint64)));
130 check = connect(&m_Recorder, &QMediaRecorder::recorderStateChanged,
131 pConnecter, &CConnecterPlayer::slotRecordStateChanged);
136 check = connect(pConnecter, &CConnecterPlayer::sigScreenShot,
138 m_bScreenShot =
true;
144CConnectPlayer::~CConnectPlayer()
146 qDebug(log) << Q_FUNC_INFO;
151 qDebug(log) <<
"CConnectPlayer::OnInit()";
153 return OnInitReturnValue::NotUseOnProcess;
158 qDebug(log) <<
"CConnectPlayer::OnClean()";
164void CConnectPlayer::slotStart()
166 qDebug(log) << Q_FUNC_INFO;
167 slotEnableAudioInput(m_pParameters->GetEnableAudioInput());
168 slotEnableAudioOutput(m_pParameters->GetEnableAudioOutput());
170 switch (m_pParameters->GetType()) {
171 case CParameterPlayer::TYPE::Camera: {
173 const QList<QCameraDevice> cameras = QMediaDevices::videoInputs();
175 || -1 > m_pParameters->GetCamera()
176 || m_pParameters->GetCamera() > QMediaDevices::videoInputs().size())
178 m_pCamera =
new QCamera(cameras.at(m_pParameters->GetCamera()));
179 emit sigServerName(cameras.at(m_pParameters->GetCamera()).description());
182 m_CaptureSession.setVideoSink(&m_VideoSink);
183 m_CaptureSession.setCamera(m_pCamera);
188 case CParameterPlayer::TYPE::Url: {
189 QString szFile = m_pParameters->GetUrl();
190 QFileInfo fi(szFile);
191 emit sigServerName(fi.fileName());
194 url = QUrl::fromLocalFile(szFile);
195 m_Player.setSource(url);
196 m_Player.setVideoSink(&m_VideoSink);
198 if(m_pParameters->m_Record.GetEnableAudio())
199 m_Player.setAudioBufferOutput(&m_AudioBufferOutput);
202 m_nPosition = m_Player.position();
203 m_nDuration = m_Player.duration();
211void CConnectPlayer::slotStop()
213 qDebug(log) << Q_FUNC_INFO;
214 switch (m_pParameters->GetType()) {
215 case CParameterPlayer::TYPE::Camera:
218 m_CaptureSession.setVideoSink(
nullptr);
220 case CParameterPlayer::TYPE::Url:
222 m_Player.setVideoSink(
nullptr);
223 m_Player.setVideoOutput(
nullptr);
239void CConnectPlayer::slotRecord(
bool bRecord)
241 qDebug(log) << Q_FUNC_INFO << bRecord;
244 if(QMediaRecorder::StoppedState != m_Recorder.recorderState()) {
248 auto &record = m_pParameters->m_Record;
249 switch (m_pParameters->GetType()) {
250 case CParameterPlayer::TYPE::Camera: {
251 record >> m_Recorder;
252 m_CaptureSession.setRecorder(&m_Recorder);
256 case CParameterPlayer::TYPE::Url: {
257 record >> m_Recorder;
258 if(record.GetEnableAudio()) {
259 m_CaptureSession.setAudioBufferInput(&m_AudioBufferInput);
261 qDebug(log) <<
"Record: disable audio";
262 if(record.GetEnableVideo())
263 m_CaptureSession.setVideoFrameInput(&m_VideoFrameInput);
265 qDebug(log) <<
"Record: disable video";
266 m_CaptureSession.setRecorder(&m_Recorder);
273#ifndef HAVE_QVideoWidget
274 emit sigRecordVideo(bRecord);
279 if(QMediaRecorder::StoppedState != m_Recorder.recorderState()) {
281 m_CaptureSession.setRecorder(
nullptr);
282 m_CaptureSession.setVideoFrameInput(
nullptr);
283 m_CaptureSession.setAudioBufferInput(
nullptr);
288void CConnectPlayer::slotClipBoardChanged()
292void CConnectPlayer::slotVideoFrameChanged(
const QVideoFrame &frame)
294#ifndef HAVE_QVideoWidget
295 if(m_Video.width() != frame.width()
296 || m_Video.height() != frame.height())
298 m_Video = QRect(0, 0, frame.width(), frame.height());
299 emit sigSetDesktopSize(m_Video.width(), m_Video.height());
301 QImage img(frame.width(), frame.height(), QImage::Format_ARGB32);
302 QPainter painter(&img);
303 const QVideoFrame::PaintOptions option;
304 QVideoFrame f = frame;
305 f.paint(&painter, m_Video, option);
311 m_bScreenShot =
false;
312 QImage image = frame.toImage();
314 qCritical(log) <<
"frame.toImage() fail";
316 QString szFile = m_pParameters->m_Record.GetImageFile(
true);
317 if(!image.save(szFile,
"PNG"))
319 qCritical(log) <<
"Capture image save to file fail." << szFile;
322 qDebug(log) <<
"Capture image to file:" << szFile;
323 qDebug(log) <<
"End action:" << m_pParameters->m_Record.GetEndAction();
324 switch(m_pParameters->m_Record.GetEndAction())
326 case CParameterRecord::ENDACTION::OpenFile: {
327 bool bRet = QDesktopServices::openUrl(QUrl::fromLocalFile(szFile));
329 qCritical(log) <<
"Fail: Open capture image file" << szFile;
332 case CParameterRecord::ENDACTION::OpenFolder: {
333 QFileInfo fi(szFile);
334 QDesktopServices::openUrl(QUrl::fromLocalFile(fi.absolutePath()));
343#if defined(HAVE_QT6_RECORD) && defined(HAVE_QVideoWidget)
344 if(QMediaRecorder::RecordingState == m_Recorder.recorderState()) {
345 bool bRet = m_VideoFrameInput.sendVideoFrame(frame);
349 qDebug(log) <<
"m_VideoFrameInput.sendVideoFrame fail";
355void CConnectPlayer::slotEnableAudioInput(
bool bEnable)
357 if(bEnable && -1 < m_pParameters->GetAudioInput()
358 && m_pParameters->GetAudioInput() < QMediaDevices::audioInputs().size()) {
359 m_AudioInput.setDevice(QMediaDevices::audioInputs()
360 .at(m_pParameters->GetAudioInput()));
361 m_AudioInput.setMuted(m_pParameters->GetAudioInputMuted());
362 m_AudioInput.setVolume(m_pParameters->GetAudioInputVolume());
363 m_CaptureSession.setAudioInput(&m_AudioInput);
365 bool check = connect(m_pParameters,
366 &CParameterPlayer::sigAudioInputMuted,
367 &m_AudioInput, &QAudioInput::setMuted);
369 check = connect(m_pParameters, &CParameterPlayer::sigAudioInputVolume,
370 &m_AudioInput, &QAudioInput::setVolume);
372 check = connect(m_pParameters, &CParameterPlayer::sigAudioInput,
373 this, [&](
int nIndex) {
375 && nIndex < QMediaDevices::audioInputs().size())
376 m_AudioInput.setDevice(
377 QMediaDevices::audioInputs().at(nIndex));
381 qDebug(log) <<
"m_CaptureSession: disable audio input";
382 m_CaptureSession.setAudioInput(
nullptr);
386void CConnectPlayer::slotEnableAudioOutput(
bool bEnable)
388 if(bEnable && (-1 < m_pParameters->GetAudioOutput()
389 && m_pParameters->GetAudioOutput()
390 < QMediaDevices::audioInputs().size()))
392 m_AudioOutput.setDevice(
393 QMediaDevices::audioOutputs()
394 .at(m_pParameters->GetAudioOutput()));
395 m_AudioOutput.setMuted(m_pParameters->GetAudioOutputMuted());
396 m_AudioOutput.setVolume(m_pParameters->GetAudioOutputVolume());
397 m_AudioOutput.disconnect();
398 bool check = connect(m_pParameters,
399 &CParameterPlayer::sigAudioOutputMuted,
400 &m_AudioOutput, &QAudioOutput::setMuted);
402 check = connect(m_pParameters, &CParameterPlayer::sigAudioOutputVolume,
403 &m_AudioOutput, &QAudioOutput::setVolume);
405 check = connect(m_pParameters, &CParameterPlayer::sigAudioOutput,
406 this, [&](
int nIndex) {
408 && nIndex < QMediaDevices::audioOutputs().size())
409 m_AudioOutput.setDevice(
410 QMediaDevices::audioOutputs().at(nIndex));
413 switch (m_pParameters->GetType()) {
414 case CParameterPlayer::TYPE::Camera:
415 m_CaptureSession.setAudioOutput(&m_AudioOutput);
417 case CParameterPlayer::TYPE::Url:
418 m_Player.setAudioOutput(&m_AudioOutput);
424 m_Player.setAudioOutput(
nullptr);
425 m_CaptureSession.setAudioOutput(
nullptr);
426 m_AudioOutput.disconnect();
430void CConnectPlayer::slotPositionChanged(qint64 pos)
435 qint64 currentInfo = pos / 1000;
436 qint64 duration = m_nDuration / 1000;
438 if (currentInfo || duration) {
439 QTime currentTime((currentInfo / 3600) % 60, (currentInfo / 60) % 60, currentInfo % 60,
440 (currentInfo * 1000) % 1000);
441 QTime totalTime((duration / 3600) % 60, (duration / 60) % 60, duration % 60,
442 (duration * 1000) % 1000);
443 QString format =
"mm:ss";
446 szStr = currentTime.toString(format) +
" / " + totalTime.toString(format);
447 emit sigPositionChanged(m_nPosition, m_nDuration);
452void CConnectPlayer::slotDurationChanged(qint64 duration)
455 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.