File indexing completed on 2024-04-28 04:50:47

0001 /*
0002  * abstractmediawidget.cpp
0003  *
0004  * Copyright (C) 2010-2012 Christoph Pfister <christophpfister@gmail.com>
0005  *
0006  * This program is free software; you can redistribute it and/or modify
0007  * it under the terms of the GNU General Public License as published by
0008  * the Free Software Foundation; either version 2 of the License, or
0009  * (at your option) any later version.
0010  *
0011  * This program is distributed in the hope that it will be useful,
0012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0014  * GNU General Public License for more details.
0015  *
0016  * You should have received a copy of the GNU General Public License along
0017  * with this program; if not, write to the Free Software Foundation, Inc.,
0018  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
0019  */
0020 
0021 #include <QCoreApplication>
0022 
0023 #include "abstractmediawidget.h"
0024 
0025 AbstractMediaWidget::AbstractMediaWidget(QWidget *parent) : QWidget(parent), mediaWidget(NULL)
0026 {
0027     playbackStatus = MediaWidget::Idle;
0028     currentTime = 0;
0029     totalTime = 0;
0030     seekable = false;
0031     currentAudioStream = -1;
0032     currentSubtitle = -1;
0033     titleCount = 0;
0034     currentTitle = -1;
0035     chapterCount = 0;
0036     currentChapter = -1;
0037     angleCount = 0;
0038     currentAngle = -1;
0039     dvdMenu = false;
0040 }
0041 
0042 void AbstractMediaWidget::connectToMediaWidget(MediaWidget *mediaWidget_)
0043 {
0044     mediaWidget = mediaWidget_;
0045     addPendingUpdates(PlaybackStatus | CurrentTotalTime | Seekable | Metadata | AudioStreams |
0046         Subtitles | Titles | Chapters | Angles | DvdMenu | VideoSize);
0047 }
0048 
0049 void AbstractMediaWidget::addPendingUpdates(PendingUpdates pendingUpdatesToBeAdded)
0050 {
0051     while (true) {
0052         int oldValue = pendingUpdates;
0053         int newValue = (oldValue | pendingUpdatesToBeAdded);
0054 
0055         if (!pendingUpdates.testAndSetRelaxed(oldValue, newValue)) {
0056             continue;
0057         }
0058 
0059         if (oldValue == 0) {
0060             QCoreApplication::postEvent(this, new QEvent(QEvent::User));
0061         }
0062 
0063         break;
0064     }
0065 }
0066 
0067 void AbstractMediaWidget::customEvent(QEvent *event)
0068 {
0069     Q_UNUSED(event)
0070 
0071     while (true) {
0072         int oldValue = pendingUpdates;
0073         int lowestPendingUpdate = (oldValue & (~(oldValue - 1)));
0074         int newValue = (oldValue & (~lowestPendingUpdate));
0075 
0076         if (!pendingUpdates.testAndSetRelaxed(oldValue, newValue)) {
0077             continue;
0078         }
0079 
0080         switch (static_cast<PendingUpdate>(lowestPendingUpdate)) {
0081         case PlaybackFinished:
0082             mediaWidget->playbackFinished();
0083             break;
0084         case PlaybackStatus:
0085             if (updatePlaybackStatus())
0086                 mediaWidget->playbackStatusChanged();
0087             break;
0088         case CurrentTotalTime:
0089             updateCurrentTotalTime();
0090             mediaWidget->currentTotalTimeChanged();
0091             break;
0092         case Seekable:
0093             updateSeekable();
0094             mediaWidget->seekableChanged();
0095             break;
0096         case Metadata:
0097             updateMetadata();
0098             mediaWidget->metadataChanged();
0099             break;
0100         case AudioStreams:
0101             updateAudioStreams();
0102             mediaWidget->audioStreamsChanged();
0103             break;
0104         case Subtitles:
0105             updateSubtitles();
0106             mediaWidget->subtitlesChanged();
0107             break;
0108         case Titles:
0109             updateTitles();
0110             mediaWidget->titlesChanged();
0111             break;
0112         case Chapters:
0113             updateChapters();
0114             mediaWidget->chaptersChanged();
0115             break;
0116         case Angles:
0117             updateAngles();
0118             mediaWidget->anglesChanged();
0119             break;
0120         case DvdMenu:
0121             updateDvdMenu();
0122             mediaWidget->dvdMenuChanged();
0123             break;
0124         case VideoSize:
0125             updateVideoSize();
0126             mediaWidget->videoSizeChanged();
0127             break;
0128         case Nothing:
0129             break;
0130         }
0131 
0132         if (newValue == 0) {
0133             break;
0134         }
0135     }
0136 }