File indexing completed on 2024-06-02 04:51:54

0001 /****************************************************************************************
0002  * Copyright (c) 2009 Casey Link <unnamedrambler@gmail.com>                             *
0003  *                                                                                      *
0004  * This program is free software; you can redistribute it and/or modify it under        *
0005  * the terms of the GNU General Public License as published by the Free Software        *
0006  * Foundation; either version 2 of the License, or (at your option) any later           *
0007  * version.                                                                             *
0008  *                                                                                      *
0009  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
0010  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
0011  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
0012  *                                                                                      *
0013  * You should have received a copy of the GNU General Public License along with         *
0014  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
0015  ****************************************************************************************/
0016 
0017 #include "TimecodeObserver.h"
0018 
0019 #include "core/meta/Meta.h"
0020 #include "core/support/Debug.h"
0021 #include "core-impl/collections/support/CollectionManager.h"
0022 #include "core-impl/capabilities/timecode/TimecodeWriteCapability.h"
0023 
0024 const qint64 TimecodeObserver::m_threshold = 600 * 1000; // 6000000ms = 10 minutes
0025 
0026 TimecodeObserver::TimecodeObserver( QObject *parent )
0027     : QObject( parent )
0028     , m_trackTimecodeable ( false )
0029     , m_currentTrack ( nullptr )
0030     , m_currPos ( 0 )
0031 {
0032     EngineController *engine = The::engineController();
0033 
0034     connect( engine, &EngineController::stopped,
0035              this, &TimecodeObserver::stopped );
0036     connect( engine, &EngineController::trackPlaying,
0037              this, &TimecodeObserver::trackPlaying );
0038     connect( engine, &EngineController::trackPositionChanged,
0039              this, &TimecodeObserver::trackPositionChanged );
0040 }
0041 
0042 TimecodeObserver::~TimecodeObserver()
0043 {}
0044 
0045 void
0046 TimecodeObserver::stopped( qint64 finalPosition, qint64 trackLength )
0047 {
0048     DEBUG_BLOCK
0049 
0050     if( m_trackTimecodeable && finalPosition != trackLength && trackLength > m_threshold && finalPosition > 60 * 1000 )
0051     {
0052         Meta::TrackPtr currentTrack = The::engineController()->currentTrack();
0053         if( currentTrack )
0054         {
0055             Capabilities::TimecodeWriteCapability *tcw = currentTrack->create<Capabilities::TimecodeWriteCapability>();
0056             if( tcw )
0057             {
0058                 tcw->writeAutoTimecode ( finalPosition ); // save the timecode
0059                 delete tcw;
0060             }
0061         }
0062     }
0063 }
0064 
0065 void
0066 TimecodeObserver::trackPlaying( Meta::TrackPtr track )
0067 {
0068     if( track == m_currentTrack ) // no change, so do nothing
0069         return;
0070 
0071     if( m_currentTrack ) // this is really the track _just_ played
0072     {
0073         if( m_trackTimecodeable && m_currPos != m_currentTrack->length() && m_currentTrack->length() > m_threshold && m_currPos > 60 * 1000 )
0074         {
0075             QScopedPointer<Capabilities::TimecodeWriteCapability> tcw( m_currentTrack->create<Capabilities::TimecodeWriteCapability>() );
0076             if( tcw )
0077                 tcw->writeAutoTimecode ( m_currPos ); // save the timecode
0078         }
0079     }
0080 
0081     // now update to the new track
0082     if( track && track->has<Capabilities::TimecodeWriteCapability>() )
0083         m_trackTimecodeable = true;
0084 
0085     m_currentTrack = track;
0086     m_currPos = 0;
0087 }
0088 
0089 void TimecodeObserver::trackPositionChanged( qint64 position, bool userSeek )
0090 {
0091     Q_UNUSED ( userSeek )
0092 
0093     m_currPos = position;
0094 }
0095