Ticket #1503: 0001-Mediaplayer-resume-at-latest-position-and-restore-vo.patch

File 0001-Mediaplayer-resume-at-latest-position-and-restore-vo.patch, 12.9 KB (added by frizer23, 11 years ago)
  • src/apps/mediaplayer/Controller.cpp

    From 7aea2c3513bae23ad14c19e00319bd7b71f70ce3 Mon Sep 17 00:00:00 2001
    From: Julien Lepiller <julien@lepiller.eu>
    Date: Tue, 29 Apr 2014 18:44:47 +0200
    Subject: [PATCH] Mediaplayer: resume at latest position and restore volume
    
    ---
     src/apps/mediaplayer/Controller.cpp                | 47 +++++++++++++++++++++-
     src/apps/mediaplayer/Controller.h                  |  4 ++
     src/apps/mediaplayer/MainWin.cpp                   | 11 ++++-
     src/apps/mediaplayer/playlist/FilePlaylistItem.cpp | 20 ++++++++-
     src/apps/mediaplayer/playlist/FilePlaylistItem.h   |  2 +-
     src/apps/mediaplayer/playlist/PlaylistItem.cpp     | 30 ++++++++++++++
     src/apps/mediaplayer/playlist/PlaylistItem.h       |  9 ++++-
     src/apps/mediaplayer/settings/Settings.cpp         |  5 +++
     src/apps/mediaplayer/settings/Settings.h           |  6 +++
     src/apps/mediaplayer/settings/SettingsWindow.cpp   | 12 ++++++
     src/apps/mediaplayer/settings/SettingsWindow.h     |  1 +
     11 files changed, 141 insertions(+), 6 deletions(-)
    
    diff --git a/src/apps/mediaplayer/Controller.cpp b/src/apps/mediaplayer/Controller.cpp
    index 5176a3d..f6666b4 100644
    a b Controller::SetToAsync(const PlaylistItemRef& item)  
    219219    return B_OK;
    220220}
    221221
    222 
    223222status_t
    224223Controller::SetTo(const PlaylistItemRef& item)
    225224{
    Controller::TimePosition()  
    679678    return _TimePosition();
    680679}
    681680
     681status_t
     682Controller::SaveState(bool reset)
     683{
     684    if(fItem.Get() == NULL)
     685        return B_OK;
     686    if(reset)
     687            fCurrentFrame = 0;
     688    status_t status = fItem.Get()->SetLastVolume(50*fVolume);
     689    if(status == B_OK)
     690        status = fItem.Get()->SetLastFrame(fCurrentFrame);
     691    else
     692        fItem.Get()->SetLastFrame(fCurrentFrame);
     693    return status;
     694}
     695
     696void
     697Controller::RestoreState()
     698{
     699    PlaylistItem *item =fItem.Get();
     700    if(item == NULL)
     701        return;
     702
     703    Pause();
     704
     705    if(item->LastFrame() > 0) {
     706        bool resume = fResume == mpSettings::RESUME_ALWAYS;
     707        if(fResume == mpSettings::RESUME_ASK) {
     708            BString label = "Do you want to resume ";
     709            int32 time = (int32)((float)item->LastFrame() * TimeDuration() / (1000000*_FrameDuration()));
     710            label << item->Name() << " at " << (time/60) << "m" << (time%60) << "s?";
     711            BAlert *alert = new BAlert("Resume?", label, "Resume", "Reset");
     712            resume = alert->Go() == 0;
     713        }
     714
     715        if(resume)
     716            SetFramePosition(item->LastFrame());
     717    }
     718
     719    int32 lastVolume = item->LastVolume();
     720    if(lastVolume >= 0)
     721        SetVolume((float)lastVolume/50);
     722
     723    Play();
     724}
     725
    682726
    683727void
    684728Controller::SetVolume(float value)
    Controller::_AdoptGlobalSettings()  
    9741018    fLoopMovies = settings.loopMovie;
    9751019    fLoopSounds = settings.loopSound;
    9761020    fBackgroundMovieVolumeMode = settings.backgroundMovieVolumeMode;
     1021    fResume = settings.resume;
    9771022}
    9781023
    9791024
  • src/apps/mediaplayer/Controller.h

    diff --git a/src/apps/mediaplayer/Controller.h b/src/apps/mediaplayer/Controller.h
    index 1eeda17..451d55a 100644
    a b  
    2222#define __CONTROLLER_H
    2323
    2424
     25#include <Alert.h>
    2526#include <Entry.h>
    2627#include <MediaDefs.h>
    2728#include <MediaFormats.h>
    public:  
    115116
    116117            bigtime_t           TimeDuration();
    117118            bigtime_t           TimePosition();
     119            status_t            SaveState(bool reset = false);
     120            void                RestoreState();
    118121
    119122    virtual void                SetVolume(float factor);
    120123            float               Volume();
    private:  
    224227            bool                fLoopMovies;
    225228            bool                fLoopSounds;
    226229            uint32              fBackgroundMovieVolumeMode;
     230            uint32              fResume;
    227231
    228232            BList               fListeners;
    229233};
  • src/apps/mediaplayer/MainWin.cpp

    diff --git a/src/apps/mediaplayer/MainWin.cpp b/src/apps/mediaplayer/MainWin.cpp
    index 6038a61..d4aa8a5 100644
    a b MainWin::MessageReceived(BMessage* msg)  
    697697        {
    698698            BAutolock _(fPlaylist);
    699699
     700            //The file is finished. Open at start next time.
     701            fController->SaveState(true);
     702
    700703            bool hadNext = fPlaylist->SetCurrentItemIndex(
    701704                fPlaylist->CurrentItemIndex() + 1);
    702705            if (!hadNext) {
    MainWin::MessageReceived(BMessage* msg)  
    799802            float volume;
    800803            if (msg->FindFloat("volume", &volume) == B_OK)
    801804                fControls->SetVolume(volume);
     805            fController->SaveState();
    802806            break;
    803807        }
    804808        case MSG_CONTROLLER_MUTED_CHANGED:
    MainWin::WindowActivated(bool active)  
    10671071bool
    10681072MainWin::QuitRequested()
    10691073{
     1074    fController->SaveState();
    10701075    BMessage message(M_PLAYER_QUIT);
    10711076    GetQuitMessage(&message);
    10721077    be_app->PostMessage(&message);
    MainWin::_PlaylistItemOpened(const PlaylistItemRef& item, status_t result)  
    14011406        }
    14021407        fController->SetTimePosition(fInitialSeekPosition);
    14031408        fInitialSeekPosition = 0;
     1409
     1410        if(fPlaylist->CountItems() == 1)
     1411            fController->RestoreState();
    14041412    }
    14051413    _SetupWindow();
    14061414
    MainWin::_Wind(bigtime_t howMuch, int64 frames)  
    24522460        } else if (seekTime > fController->TimeDuration()) {
    24532461            fInitialSeekPosition = 0;
    24542462            PostMessage(M_SKIP_NEXT);
    2455         } else
     2463        } else {
    24562464            fController->SetTimePosition(seekTime);
     2465        }
    24572466    }
    24582467
    24592468    fController->Unlock();
  • src/apps/mediaplayer/playlist/FilePlaylistItem.cpp

    diff --git a/src/apps/mediaplayer/playlist/FilePlaylistItem.cpp b/src/apps/mediaplayer/playlist/FilePlaylistItem.cpp
    index 4b1cafd..5eb7e24 100644
    a b FilePlaylistItem::SetAttribute(const Attribute& attribute,  
    171171        case ATTR_INT32_RATING:
    172172            return _SetAttribute("Media:Rating", B_INT32_TYPE, &value,
    173173                sizeof(int32));
     174        case ATTR_INT32_FRAME:
     175            return _SetAttribute("Media:Frame", B_INT32_TYPE, &value,
     176                sizeof(int32));
     177        case ATTR_INT32_VOLUME:
     178            return _SetAttribute("Media:Volume", B_INT32_TYPE, &value,
     179                sizeof(int32));
    174180
    175181        default:
    176182            return B_NOT_SUPPORTED;
    status_t  
    182188FilePlaylistItem::GetAttribute(const Attribute& attribute,
    183189    int32& value) const
    184190{
    185     return B_NOT_SUPPORTED;
     191    switch (attribute) {
     192        case ATTR_INT32_FRAME:
     193            return _GetAttribute("Media:Frame", B_INT32_TYPE, &value,
     194                sizeof(int32));
     195        case ATTR_INT32_VOLUME:
     196            return _GetAttribute("Media:Volume", B_INT32_TYPE, &value,
     197                sizeof(int32));
     198
     199        default:
     200            return B_NOT_SUPPORTED;
     201    }
    186202}
    187203
    188204
    FilePlaylistItem::_SetAttribute(const char* attrName, type_code type,  
    407423
    408424status_t
    409425FilePlaylistItem::_GetAttribute(const char* attrName, type_code type,
    410     void* data, size_t size)
     426    void* data, size_t size) const
    411427{
    412428    BEntry entry(&fRefs[0], true);
    413429    BNode node(&entry);
  • src/apps/mediaplayer/playlist/FilePlaylistItem.h

    diff --git a/src/apps/mediaplayer/playlist/FilePlaylistItem.h b/src/apps/mediaplayer/playlist/FilePlaylistItem.h
    index a0c4da4..fb72f9e 100644
    a b private:  
    6767                                    size_t size);
    6868            status_t            _GetAttribute(const char* attrName,
    6969                                    type_code type, void* data,
    70                                     size_t size);
     70                                    size_t size) const;
    7171            status_t            _MoveIntoTrash(vector<entry_ref>* refs,
    7272                                    vector<BString>* namesInTrash);
    7373            status_t            _RestoreFromTrash(vector<entry_ref>* refs,
  • src/apps/mediaplayer/playlist/PlaylistItem.cpp

    diff --git a/src/apps/mediaplayer/playlist/PlaylistItem.cpp b/src/apps/mediaplayer/playlist/PlaylistItem.cpp
    index fde90a8..71cfec9 100644
    a b PlaylistItem::TrackNumber() const  
    106106    return trackNumber;
    107107}
    108108
     109int32
     110PlaylistItem::LastFrame() const
     111{
     112    int32 lastFrame;
     113    if(GetAttribute(ATTR_INT32_FRAME, lastFrame) != B_OK)
     114        lastFrame = 0;
     115    return lastFrame;
     116}
     117
     118int32
     119PlaylistItem::LastVolume() const
     120{
     121    int32 lastVolume;
     122    if(GetAttribute(ATTR_INT32_VOLUME, lastVolume) != B_OK)
     123        lastVolume = -1;
     124    return lastVolume;
     125}
     126
     127status_t
     128PlaylistItem::SetLastFrame(int32 value)
     129{
     130    return SetAttribute(ATTR_INT32_FRAME, value);
     131}
     132
     133status_t
     134PlaylistItem::SetLastVolume(int32 value)
     135{
     136    return SetAttribute(ATTR_INT32_VOLUME, value);
     137}
     138
    109139
    110140void
    111141PlaylistItem::SetPlaybackFailed()
  • src/apps/mediaplayer/playlist/PlaylistItem.h

    diff --git a/src/apps/mediaplayer/playlist/PlaylistItem.h b/src/apps/mediaplayer/playlist/PlaylistItem.h
    index b896df5..b7d3528 100644
    a b public:  
    5353
    5454        ATTR_INT32_TRACK            = 'trck',
    5555        ATTR_INT32_YEAR             = 'year',
    56         ATTR_INT32_RATING           = 'rtng'
     56        ATTR_INT32_RATING           = 'rtng',
     57        ATTR_INT32_FRAME            = 'fram',
     58        ATTR_INT32_VOLUME           = 'volu'
    5759    } Attribute;
    5860
    5961    virtual status_t            SetAttribute(const Attribute& attribute,
    public:  
    7880            BString             Title() const;
    7981
    8082            int32               TrackNumber() const;
     83            int32               LastFrame() const;
     84            int32               LastVolume() const;
     85
     86            status_t            SetLastFrame(int32 value);
     87            status_t            SetLastVolume(int32 value);
    8188
    8289    // methods
    8390    virtual BString             LocationURI() const = 0;
  • src/apps/mediaplayer/settings/Settings.cpp

    diff --git a/src/apps/mediaplayer/settings/Settings.cpp b/src/apps/mediaplayer/settings/Settings.cpp
    index 7f1d00d..a4b22f9 100644
    a b mpSettings::operator!=(const mpSettings& other) const  
    2626        || useOverlays != other.useOverlays
    2727        || scaleBilinear != other.scaleBilinear
    2828        || scaleFullscreenControls != other.scaleFullscreenControls
     29        || resume != other.resume
    2930        || subtitleSize != other.subtitleSize
    3031        || subtitlePlacement != other.subtitlePlacement
    3132        || backgroundMovieVolumeMode != other.backgroundMovieVolumeMode
    Settings::Get(mpSettings& settings) const  
    6162    settings.scaleFullscreenControls
    6263        = fSettingsMessage.GetValue("scaleFullscreenControls", true);
    6364
     65    settings.resume
     66        = fSettingsMessage.GetValue("resume",
     67            (uint32)mpSettings::RESUME_ASK);
    6468    settings.subtitleSize
    6569        = fSettingsMessage.GetValue("subtitleSize",
    6670            (uint32)mpSettings::SUBTITLE_SIZE_MEDIUM);
    Settings::Update(const mpSettings& settings)  
    9599    fSettingsMessage.SetValue("scaleFullscreenControls",
    96100        settings.scaleFullscreenControls);
    97101
     102    fSettingsMessage.SetValue("resume", settings.resume);
    98103    fSettingsMessage.SetValue("subtitleSize", settings.subtitleSize);
    99104    fSettingsMessage.SetValue("subtitlePlacement", settings.subtitlePlacement);
    100105
  • src/apps/mediaplayer/settings/Settings.h

    diff --git a/src/apps/mediaplayer/settings/Settings.h b/src/apps/mediaplayer/settings/Settings.h
    index f8b3590..5dbc499 100644
    a b struct mpSettings {  
    3434                BG_MOVIES_HALF_VLUME = 1,
    3535                BG_MOVIES_MUTED = 2
    3636            };
     37            enum {
     38                RESUME_NEVER = 0,
     39                RESUME_ASK = 1,
     40                RESUME_ALWAYS = 2
     41            };
    3742
    3843            bool                autostart;
    3944            bool                closeWhenDonePlayingMovie;
    struct mpSettings {  
    4348            bool                useOverlays;
    4449            bool                scaleBilinear;
    4550            bool                scaleFullscreenControls;
     51            uint32              resume;
    4652            uint32              subtitleSize;
    4753            uint32              subtitlePlacement;
    4854            uint32              backgroundMovieVolumeMode;
  • src/apps/mediaplayer/settings/SettingsWindow.cpp

    diff --git a/src/apps/mediaplayer/settings/SettingsWindow.cpp b/src/apps/mediaplayer/settings/SettingsWindow.cpp
    index ea1cc17..d3eaa62 100644
    a b SettingsWindow::SettingsWindow(BRect frame)  
    9999        B_TRANSLATE("Scale controls in full screen mode"),
    100100        new BMessage(M_SETTINGS_CHANGED));
    101101
     102    fResumeOP = new BOptionPopUp("resume",
     103        B_TRANSLATE("Resume:"), new BMessage(M_SETTINGS_CHANGED));
     104    fResumeOP->AddOption(
     105        B_TRANSLATE("never"), mpSettings::RESUME_NEVER);
     106    fResumeOP->AddOption(
     107        B_TRANSLATE("ask every time"), mpSettings::RESUME_ASK);
     108    fResumeOP->AddOption(
     109        B_TRANSLATE("always"), mpSettings::RESUME_ALWAYS);
     110
    102111    fSubtitleSizeOP = new BOptionPopUp("subtitleSize",
    103112        B_TRANSLATE("Subtitle size:"), new BMessage(M_SETTINGS_CHANGED));
    104113    fSubtitleSizeOP->AddOption(
    SettingsWindow::SettingsWindow(BRect frame)  
    156165                    .End()
    157166                    .Add(fLoopMoviesCB)
    158167                    .Add(fLoopSoundsCB)
     168                    .Add(fResumeOP)
    159169                .End()
    160170            .End()
    161171            .AddStrut(kSpacing)
    SettingsWindow::AdoptSettings()  
    270280    fScaleBilinearCB->SetValue(fSettings.scaleBilinear);
    271281    fScaleFullscreenControlsCB->SetValue(fSettings.scaleFullscreenControls);
    272282
     283    fResumeOP->SetValue(fSettings.resume);
    273284    fSubtitleSizeOP->SetValue(fSettings.subtitleSize);
    274285    fSubtitlePlacementOP->SetValue(fSettings.subtitlePlacement);
    275286
    SettingsWindow::ApplySettings()  
    300311    fSettings.scaleFullscreenControls
    301312        = fScaleFullscreenControlsCB->Value() == B_CONTROL_ON;
    302313
     314    fSettings.resume = fResumeOP->Value();
    303315    fSettings.subtitleSize = fSubtitleSizeOP->Value();
    304316    fSettings.subtitlePlacement = fSubtitlePlacementOP->Value();
    305317
  • src/apps/mediaplayer/settings/SettingsWindow.h

    diff --git a/src/apps/mediaplayer/settings/SettingsWindow.h b/src/apps/mediaplayer/settings/SettingsWindow.h
    index 6285620..312927a 100644
    a b private:  
    4747        BCheckBox*              fScaleBilinearCB;
    4848        BCheckBox*              fScaleFullscreenControlsCB;
    4949
     50        BOptionPopUp*           fResumeOP;
    5051        BOptionPopUp*           fSubtitleSizeOP;
    5152        BOptionPopUp*           fSubtitlePlacementOP;
    5253