Changeset View
Changeset View
Standalone View
Standalone View
src/widgets/videomonitorwidget.cpp
Show All 20 Lines | |||||
21 | #include <QPushButton> | 21 | #include <QPushButton> | ||
22 | #include <QGridLayout> | 22 | #include <QGridLayout> | ||
23 | #include <QtMultimediaWidgets/QVideoWidget> | 23 | #include <QtMultimediaWidgets/QVideoWidget> | ||
24 | #include <KLocalizedString> | 24 | #include <KLocalizedString> | ||
25 | 25 | | |||
26 | 26 | | |||
27 | VideoMonitorWidget::VideoMonitorWidget(QWidget *parent) : | 27 | VideoMonitorWidget::VideoMonitorWidget(QWidget *parent) : | ||
28 | QWidget(parent), | 28 | QWidget(parent), | ||
29 | _mediaplayer(nullptr, QMediaPlayer::VideoSurface) | 29 | _mediaplayer(nullptr, QMediaPlayer::StreamPlayback) | ||
30 | { | 30 | { | ||
31 | auto _layout = new QGridLayout(); | 31 | auto _layout = new QGridLayout(); | ||
32 | auto _label = new QLabel(i18n("Source url:")); | 32 | auto _label = new QLabel(i18n("Source url:")); | ||
33 | _layout->addWidget(_label, 0, 0); | 33 | _layout->addWidget(_label, 0, 0); | ||
34 | 34 | | |||
35 | auto _sourceCB = new QComboBox(); | 35 | auto _sourceCB = new QComboBox(); | ||
36 | _sourceCB->setEditable(true); | 36 | _sourceCB->setEditable(true); | ||
37 | _sourceCB->setToolTip(i18n("Valid Urls:\n\ | 37 | _sourceCB->setToolTip(i18n("Valid Urls:\n\ | ||
38 | http://www.example.com/stream.avi\n\ | 38 | http://www.example.com/stream.avi\n\ | ||
39 | rtp://@:1234\n\ | 39 | rtp://@:1234\n\ | ||
40 | mms://mms.examples.com/stream.asx\n\ | 40 | mms://mms.examples.com/stream.asx\n\ | ||
41 | rtsp://server.example.org:8080/test.sdp")); | 41 | rtsp://server.example.org:8080/test.sdp")); | ||
42 | _layout->addWidget(_sourceCB, 0, 1); | 42 | _layout->addWidget(_sourceCB, 0, 1); | ||
43 | 43 | | |||
44 | auto _playPB = new QPushButton(); | 44 | auto _playPB = new QPushButton(); | ||
45 | _playPB->setCheckable(true); | 45 | _playPB->setCheckable(true); | ||
46 | _playPB->setIcon(style()->standardIcon(QStyle::SP_MediaPlay)); | 46 | _playPB->setIcon(QIcon::fromTheme("media-playback-start", style()->standardIcon(QStyle::SP_MediaPlay))); | ||
47 | _layout->addWidget(_playPB, 0, 2); | 47 | _layout->addWidget(_playPB, 0, 2); | ||
48 | 48 | | |||
49 | auto _videoWidget = new QVideoWidget(); | 49 | auto _videoWidget = new QVideoWidget(); | ||
50 | _layout->addWidget(_videoWidget, 1, 0, -1, -1); | 50 | _layout->addWidget(_videoWidget, 1, 0, -1, -1); | ||
51 | 51 | | |||
52 | _errorlabel = new QLabel; | 52 | _errorlabel = new QLabel; | ||
53 | _layout->addWidget(_errorlabel, 2, 0, 0, -1); | 53 | _layout->addWidget(_errorlabel, 2, 0, 0, -1); | ||
54 | 54 | | |||
55 | this->setLayout(_layout); | 55 | this->setLayout(_layout); | ||
56 | 56 | | |||
57 | _mediaplayer.setVideoOutput(_videoWidget); | 57 | _mediaplayer.setVideoOutput(_videoWidget); | ||
58 | 58 | | |||
59 | #ifdef Q_OS_LINUX | 59 | #ifdef Q_OS_LINUX | ||
60 | QStringList sources; | 60 | QStringList sources; | ||
61 | sources << QString("video*"); | 61 | sources << QString("video*"); | ||
62 | _sourceCB->addItems(QDir("/dev/")\ | 62 | _sourceCB->addItems(QDir("/dev/")\ | ||
63 | .entryList(sources, QDir::System)\ | 63 | .entryList(sources, QDir::System)\ | ||
64 | .replaceInStrings( QRegExp("^"), "v4l2:///dev/")); | 64 | .replaceInStrings( QRegExp("^"), "v4l2:///dev/")); | ||
65 | #endif | 65 | #endif | ||
66 | 66 | | |||
67 | connect(_playPB, &QPushButton::clicked, [this, _playPB, _sourceCB](bool b){ | 67 | connect(_playPB, &QPushButton::clicked, [this, _playPB, _sourceCB, _videoWidget](bool b){ | ||
68 | if(b){ | 68 | if(b){ | ||
69 | _playPB->setIcon(style()->standardIcon(QStyle::SP_MediaPause)); | 69 | if(_mediaplayer.state() != QMediaPlayer::PausedState) { | ||
70 | QString source = _sourceCB->currentText(); | 70 | QString source = _sourceCB->currentText(); | ||
71 | _mediaplayer.setMedia(QUrl(source)); | 71 | _mediaplayer.setMedia(QUrl(source)); | ||
72 | } | ||||
73 | _playPB->setIcon(QIcon::fromTheme("media-playback-pause", style()->standardIcon(QStyle::SP_MediaPause))); | ||||
72 | _mediaplayer.play(); | 74 | _mediaplayer.play(); | ||
73 | }else{ | 75 | }else{ | ||
74 | _mediaplayer.pause(); | 76 | _mediaplayer.pause(); | ||
75 | _playPB->setIcon(style()->standardIcon(QStyle::SP_MediaPlay)); | 77 | _playPB->setIcon(QIcon::fromTheme("media-playback-start", style()->standardIcon(QStyle::SP_MediaPlay))); | ||
76 | } | 78 | } | ||
79 | _videoWidget->setVisible(b); | ||||
77 | }); | 80 | }); | ||
78 | 81 | | |||
79 | typedef void (QMediaPlayer::*ErrorSignal)(QMediaPlayer::Error); | 82 | typedef void (QMediaPlayer::*ErrorSignal)(QMediaPlayer::Error); | ||
80 | connect(&_mediaplayer, static_cast<ErrorSignal>(&QMediaPlayer::error), | 83 | connect(&_mediaplayer, static_cast<ErrorSignal>(&QMediaPlayer::error), | ||
81 | this, &VideoMonitorWidget::handleError); | 84 | this, &VideoMonitorWidget::handleError); | ||
82 | } | 85 | } | ||
83 | 86 | | |||
84 | | ||||
85 | VideoMonitorWidget::~VideoMonitorWidget() | | |||
86 | { | | |||
87 | | ||||
88 | } | | |||
89 | | ||||
90 | void VideoMonitorWidget::handleError() | 87 | void VideoMonitorWidget::handleError() | ||
91 | { | 88 | { | ||
92 | const QString errorString = _mediaplayer.errorString(); | 89 | const QString errorString = _mediaplayer.errorString(); | ||
93 | QString message = "Error: "; | 90 | QString message = "Error: "; | ||
94 | if (errorString.isEmpty()) | 91 | if (errorString.isEmpty()) | ||
95 | message += " #" + QString::number(int(_mediaplayer.error())); | 92 | message += " #" + QString::number(int(_mediaplayer.error())); | ||
96 | else | 93 | else | ||
97 | message += errorString; | 94 | message += errorString; | ||
98 | _errorlabel->setText(message); | 95 | _errorlabel->setText(message); | ||
99 | } | 96 | } |