Changeset 10892

Show
Ignore:
Timestamp:
12/15/09 12:27:28 (5 years ago)
Author:
robert
Message:

From Julen Garcia, "So here is the code with a proper audio sync (at least in my computer)"

Location:
OpenSceneGraph/trunk/src/osgPlugins/ffmpeg
Files:
5 modified

Legend:

Unmodified
Added
Removed
  • OpenSceneGraph/trunk/src/osgPlugins/ffmpeg/FFmpegClocks.cpp

    r10851 r10892  
    4040    m_audio_delay(0.0), 
    4141    m_audio_disabled(false), 
    42     m_rewind(false), 
    4342    m_paused(false), 
    4443    m_last_current_time(0.0) 
     
    6059    m_frame_time = start_time; 
    6160 
     61    m_pause_time = 0; 
     62    m_seek_time = 0; 
     63     
    6264    m_audio_buffer_end_pts = start_time; 
    6365    m_audio_timer.setStartTick(); 
     
    6971        m_paused = true; 
    7072    else 
     73    { 
    7174        m_paused = false; 
    72 } 
    73  
    74  
    75  
    76 void FFmpegClocks::rewindAudio() 
    77 { 
    78     ScopedLock lock(m_mutex); 
    79  
     75        if(!m_audio_disabled) m_audio_timer.setStartTick(); 
     76    } 
     77} 
     78 
     79 
     80 
     81void FFmpegClocks::rewind() 
     82{ 
     83    ScopedLock lock(m_mutex); 
     84 
     85    m_pause_time = 0; 
     86    m_seek_time = 0; 
     87     
    8088    m_audio_buffer_end_pts = m_start_time; 
    8189    m_audio_timer.setStartTick(); 
    8290 
    83     m_rewind = ! m_rewind; 
    84 } 
    85  
    86  
    87  
    88 void FFmpegClocks::rewindVideo() 
    89 { 
    90     ScopedLock lock(m_mutex); 
     91    m_last_frame_delay = 0.040; 
     92    m_frame_time = m_start_time; 
    9193 
    9294    if (m_audio_disabled) 
    9395        return; 
    9496 
    95     m_video_clock = m_start_time; 
    96  
     97    m_video_clock = m_start_time;     
     98} 
     99 
     100void FFmpegClocks::seek(double seek_time) 
     101{ 
     102    ScopedLock lock(m_mutex); 
     103     
     104    m_video_clock = seek_time; 
    97105    m_last_frame_delay = 0.040; 
    98     m_last_frame_pts = m_start_time - m_last_frame_delay; 
    99     m_frame_time = m_start_time; 
    100  
    101     m_rewind = ! m_rewind; 
    102 } 
    103  
     106    m_frame_time = seek_time; 
     107} 
    104108 
    105109 
     
    180184 
    181185    // If incorrect delay, use previous one 
     186     
    182187    if (delay <= 0.0 || delay >= 1.0) 
     188    { 
    183189        delay = m_last_frame_delay; 
     190        if(!m_audio_disabled) m_frame_time = pts - delay; 
     191    } 
     192     
    184193 
    185194    // Save for next time 
     
    194203 
    195204    const double audio_time = getAudioTime(); 
    196     const double actual_delay = (! m_rewind) ? 
    197         clamp(m_frame_time - audio_time, -0.5*delay, 2.5*delay) : 
    198         m_last_actual_delay; // when rewinding audio or video (but the other has yet to be), get the last used delay 
     205    const double actual_delay = clamp(m_frame_time - audio_time, -0.5*delay, 2.5*delay); 
    199206 
    200207    //m_frame_time += delay; 
     
    225232void FFmpegClocks::setSeekTime(double seek_time) 
    226233{ 
    227     m_seek_time = getAudioTime() - seek_time; 
     234    m_seek_time += getAudioTime() - seek_time; 
    228235} 
    229236 
     
    232239double FFmpegClocks::getAudioTime() const 
    233240{ 
    234     return m_audio_buffer_end_pts + m_audio_timer.time_s() - m_pause_time - m_audio_delay; 
     241    if(m_audio_disabled) 
     242        return m_audio_buffer_end_pts + m_audio_timer.time_s() - m_pause_time - m_audio_delay - m_seek_time; 
     243    else 
     244        return m_audio_buffer_end_pts + m_audio_timer.time_s() - m_audio_delay; 
    235245} 
    236246 
     
    239249{ 
    240250    if(!m_paused) 
    241         m_last_current_time = getAudioTime() - m_seek_time; // synced with audio 
     251        m_last_current_time = getAudioTime(); 
    242252     
    243253    return m_last_current_time;   
  • OpenSceneGraph/trunk/src/osgPlugins/ffmpeg/FFmpegClocks.hpp

    r10851 r10892  
    2424    void reset(double start_time); 
    2525    void pause(bool pause); 
    26     void rewindAudio(); 
    27     void rewindVideo(); 
     26    void seek(double seek_time); 
     27    void rewind(); 
    2828 
    2929    void audioSetBufferEndPts(double pts); 
     
    6464    Timer     m_audio_timer; 
    6565    bool      m_audio_disabled; 
    66     bool      m_rewind; 
    6766    bool      m_paused; 
    6867    double    m_last_current_time; 
  • OpenSceneGraph/trunk/src/osgPlugins/ffmpeg/FFmpegDecoder.cpp

    r10851 r10892  
    368368        throw std::runtime_error("av_seek_frame failed()"); 
    369369 
     370    m_clocks.rewind(); 
    370371    m_state = REWINDING; 
    371372} 
     
    393394        throw std::runtime_error("av_seek_frame failed()"); 
    394395 
     396    m_clocks.seek(time); 
    395397    m_state = SEEKING;     
    396398} 
  • OpenSceneGraph/trunk/src/osgPlugins/ffmpeg/FFmpegDecoderAudio.cpp

    r10851 r10892  
    323323        { 
    324324            avcodec_flush_buffers(m_context); 
    325             m_clocks.rewindAudio(); 
    326325        } 
    327326 
  • OpenSceneGraph/trunk/src/osgPlugins/ffmpeg/FFmpegDecoderVideo.cpp

    r10851 r10892  
    228228            { 
    229229                avcodec_flush_buffers(m_context); 
    230                 m_clocks.rewindVideo(); 
    231230            } 
    232231        }