File indexing completed on 2024-11-10 05:11:08

0001 /*
0002  * Copyright 2023 by Aditya Mehra <aix.m@outlook.com>
0003  *
0004  * Licensed under the Apache License, Version 2.0 (the "License");
0005  * you may not use this file except in compliance with the License.
0006  * You may obtain a copy of the License at
0007  *
0008  *    http://www.apache.org/licenses/LICENSE-2.0
0009  *
0010  * Unless required by applicable law or agreed to in writing, software
0011  * distributed under the License is distributed on an "AS IS" BASIS,
0012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0013  * See the License for the specific language governing permissions and
0014  * limitations under the License.
0015  *
0016  */
0017 
0018 #include "videoproviderservice.h"
0019 
0020 VideoProviderService::VideoProviderService(QObject *parent)
0021     : QObject(parent)
0022 {
0023     m_mediaPlayer = new QMediaPlayer(this);
0024     m_audioOutput = new QAudioOutput(QMediaDevices::defaultAudioOutput(), this);
0025     m_mediaPlayer->setAudioOutput(m_audioOutput);
0026 
0027     QAudioDevice info(QMediaDevices::defaultAudioOutput());
0028     m_format = info.preferredFormat();
0029 
0030     connect(m_mediaPlayer, &QMediaPlayer::durationChanged, this, &VideoProviderService::durationUpdated);
0031     connect(m_mediaPlayer, &QMediaPlayer::positionChanged, this, &VideoProviderService::positionUpdated);
0032     connect(m_mediaPlayer, &QMediaPlayer::playbackStateChanged , this, &VideoProviderService::updatePlaybackState);
0033     connect(m_mediaPlayer, &QMediaPlayer::mediaStatusChanged, this, &VideoProviderService::updateMediaState);
0034     connect(m_mediaPlayer, &QMediaPlayer::errorOccurred, this, &VideoProviderService::errorOccurred);
0035 }
0036 
0037 VideoProviderService::~VideoProviderService()
0038 {
0039     m_mediaPlayer->deleteLater();
0040     m_audioOutput->deleteLater();
0041     emit destroyedService();
0042 }
0043 
0044 QVideoSink *VideoProviderService::videoSink() const
0045 {
0046     return m_videoSink;
0047 }
0048 
0049 void VideoProviderService::setVideoSink(QVideoSink *videoSink)
0050 {
0051     if (videoSink != m_videoSink) {
0052         m_videoSink = videoSink;
0053         emit signalVideoSinkChanged();
0054     }
0055 }
0056 
0057 QObject *VideoProviderService::videoOutput() const
0058 {
0059     return m_videoOutput;
0060 }
0061 
0062 void VideoProviderService::setVideoOutput(QObject *videoOutput)
0063 {
0064     if (videoOutput != m_videoOutput) {
0065         m_videoOutput = videoOutput;
0066         emit signalVideoOutputChanged();
0067     }
0068 }
0069 
0070 void VideoProviderService::syncStates()
0071 {
0072     emit playBackStateChanged(m_currentPlaybackState);
0073     emit mediaStateChanged(m_currentMediaState);
0074 }
0075 
0076 void VideoProviderService::mediaPlay(const QUrl &url)
0077 {
0078     m_currentMediaUrl = url;
0079     m_mediaPlayer->setVideoSink(m_videoSink);
0080     m_mediaPlayer->setVideoOutput(m_videoOutput);
0081     m_mediaPlayer->setSource(url);
0082     m_mediaPlayer->play();
0083 }
0084 
0085 void VideoProviderService::mediaPause()
0086 {
0087     m_mediaPlayer->pause();
0088 }
0089 
0090 void VideoProviderService::mediaStop()
0091 {
0092     m_mediaPlayer->stop();
0093 }
0094 
0095 void VideoProviderService::mediaContinue()
0096 {
0097     m_mediaPlayer->play();
0098 }
0099 
0100 void VideoProviderService::mediaRestart()
0101 {
0102     m_mediaPlayer->setPosition(0);
0103 }
0104 
0105 void VideoProviderService::mediaSeek(qint64 seekValue)
0106 {
0107     m_mediaPlayer->setPosition(seekValue);
0108 }
0109 
0110 void VideoProviderService::updatePlaybackState(QMediaPlayer::PlaybackState state)
0111 {
0112     switch (state) {
0113     case QMediaPlayer::PlayingState:
0114         m_currentPlaybackState = PlayingState;
0115         break;
0116     case QMediaPlayer::PausedState:
0117         m_currentPlaybackState = PausedState;
0118         break;
0119     case QMediaPlayer::StoppedState:
0120         m_currentPlaybackState = StoppedState;
0121         break;
0122     default:
0123         break;
0124     }
0125     emit playBackStateChanged(m_currentPlaybackState);
0126 }
0127 
0128 void VideoProviderService::updateMediaState(QMediaPlayer::MediaStatus status)
0129 {
0130     switch (status) {
0131     case QMediaPlayer::NoMedia:
0132         m_currentMediaState = NoMedia;
0133         break;
0134     case QMediaPlayer::LoadingMedia:
0135         m_currentMediaState = LoadingMedia;
0136         break;
0137     case QMediaPlayer::LoadedMedia:
0138         m_currentMediaState = LoadedMedia;
0139         break;
0140     case QMediaPlayer::StalledMedia:
0141         m_currentMediaState = StalledMedia;
0142         break;
0143     case QMediaPlayer::BufferingMedia:
0144         m_currentMediaState = BufferingMedia;
0145         break;
0146     case QMediaPlayer::BufferedMedia:
0147         m_currentMediaState = BufferedMedia;
0148         break;
0149     case QMediaPlayer::EndOfMedia:
0150         m_currentMediaState = EndOfMedia;
0151         break;
0152     case QMediaPlayer::InvalidMedia:
0153         m_currentMediaState = InvalidMedia;
0154         break;
0155     default:
0156         break;
0157     }
0158     emit mediaStateChanged(m_currentMediaState);
0159 }
0160 
0161 void VideoProviderService::durationUpdated(qint64 duration)
0162 {
0163     emit durationChanged(duration);
0164 }
0165 
0166 void VideoProviderService::positionUpdated(qint64 position)
0167 {
0168     emit positionChanged(position);
0169 }
0170 
0171 void VideoProviderService::errorOccurred(QMediaPlayer::Error error)
0172 {
0173     qDebug() << "VideoProviderService::errorOccurred" << error;
0174 }