Ticket #6564: mediaplayer.3.patch

File mediaplayer.3.patch, 12.1 KB (added by TwoFx, 8 years ago)

v4.1

  • src/apps/mediaplayer/Controller.cpp

    From efa517ddd0c16099cc06145f15f6f842f0c66399 Mon Sep 17 00:00:00 2001
    From: Markus Himmel <markus@himmel-villmar.de>
    Date: Mon, 4 Jan 2016 23:06:28 +0100
    Subject: [PATCH 1/2] MediaPlayer: Add missing null check
    
    ---
     src/apps/mediaplayer/Controller.cpp | 3 ++-
     1 file changed, 2 insertions(+), 1 deletion(-)
    
    diff --git a/src/apps/mediaplayer/Controller.cpp b/src/apps/mediaplayer/Controller.cpp
    index 5176a3d..f555cc8 100644
    a b Controller::SetTo(const PlaylistItemRef& item)  
    302302    trackSupplierDeleter.Detach();
    303303
    304304    // prevent blocking the creation of new overlay buffers
    305     fVideoView->DisableOverlay();
     305    if (fVideoView)
     306        fVideoView->DisableOverlay();
    306307
    307308    // get video properties (if there is video at all)
    308309    bool useOverlays = fVideoView ? fVideoView->UseOverlays() : true;
  • src/apps/mediaplayer/playlist/FilePlaylistItem.cpp

    -- 
    2.2.2
    
    
    From b0c524d4655fa917f90466857e5450091885ae3a Mon Sep 17 00:00:00 2001
    From: Markus Himmel <markus@himmel-villmar.de>
    Date: Mon, 4 Jan 2016 23:08:04 +0100
    Subject: [PATCH 2/2] MediaPlayer: Show total playlist length
    
    ---
     src/apps/mediaplayer/playlist/FilePlaylistItem.cpp |  18 +-
     src/apps/mediaplayer/playlist/FilePlaylistItem.h   |   2 +-
     src/apps/mediaplayer/playlist/PlaylistItem.h       |   5 +-
     src/apps/mediaplayer/playlist/PlaylistWindow.cpp   | 187 ++++++++++++++++++++-
     src/apps/mediaplayer/playlist/PlaylistWindow.h     |  34 +++-
     5 files changed, 236 insertions(+), 10 deletions(-)
    
    diff --git a/src/apps/mediaplayer/playlist/FilePlaylistItem.cpp b/src/apps/mediaplayer/playlist/FilePlaylistItem.cpp
    index 4b1cafd..9279fe1 100644
    a b status_t  
    190190FilePlaylistItem::SetAttribute(const Attribute& attribute,
    191191    const int64& value)
    192192{
    193     return B_NOT_SUPPORTED;
     193    switch (attribute) {
     194        case ATTR_INT64_DURATION:
     195            return _SetAttribute("Media:Length", B_INT64_TYPE, &value,
     196                sizeof(int64));
     197        default:
     198            return B_NOT_SUPPORTED;
     199    }
    194200}
    195201
    196202
    status_t  
    198204FilePlaylistItem::GetAttribute(const Attribute& attribute,
    199205    int64& value) const
    200206{
    201     return B_NOT_SUPPORTED;
     207    switch (attribute) {
     208        case ATTR_INT64_DURATION:
     209            return _GetAttribute("Media:Length", B_INT64_TYPE, &value,
     210                sizeof(int64));
     211        default:
     212            return B_NOT_SUPPORTED;
     213    }
    202214}
    203215
    204216
    FilePlaylistItem::_SetAttribute(const char* attrName, type_code type,  
    407419
    408420status_t
    409421FilePlaylistItem::_GetAttribute(const char* attrName, type_code type,
    410     void* data, size_t size)
     422    void* data, size_t size) const
    411423{
    412424    BEntry entry(&fRefs[0], true);
    413425    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.h

    diff --git a/src/apps/mediaplayer/playlist/PlaylistItem.h b/src/apps/mediaplayer/playlist/PlaylistItem.h
    index b896df5..73de81a 100644
    a b public:  
    4949        ATTR_STRING_TITLE           = 'titl',
    5050        ATTR_STRING_AUDIO_BITRATE   = 'abtr',
    5151        ATTR_STRING_VIDEO_BITRATE   = 'vbtr',
    52         ATTR_STRING_DURATION        = 'drtn',
    5352
    5453        ATTR_INT32_TRACK            = 'trck',
    5554        ATTR_INT32_YEAR             = 'year',
    56         ATTR_INT32_RATING           = 'rtng'
     55        ATTR_INT32_RATING           = 'rtng',
     56
     57        ATTR_INT64_DURATION         = 'drtn'
    5758    } Attribute;
    5859
    5960    virtual status_t            SetAttribute(const Attribute& attribute,
  • src/apps/mediaplayer/playlist/PlaylistWindow.cpp

    diff --git a/src/apps/mediaplayer/playlist/PlaylistWindow.cpp b/src/apps/mediaplayer/playlist/PlaylistWindow.cpp
    index 08ce627..fbea517 100644
    a b  
    2222#include <File.h>
    2323#include <FilePanel.h>
    2424#include <Locale.h>
     25#include <MediaFile.h>
     26#include <MediaTrack.h>
    2527#include <Menu.h>
    2628#include <MenuBar.h>
    2729#include <MenuItem.h>
     
    3133#include <ScrollBar.h>
    3234#include <ScrollView.h>
    3335#include <String.h>
     36#include <StringView.h>
    3437
     38#include "AudioTrackSupplier.h"
    3539#include "CommandStack.h"
     40#include "DurationToString.h"
    3641#include "MainApp.h"
    3742#include "PlaylistListView.h"
    3843#include "RWLocker.h"
    39 
     44#include "TrackSupplier.h"
     45#include "VideoTrackSupplier.h"
    4046
    4147#undef B_TRANSLATION_CONTEXT
    4248#define B_TRANSLATION_CONTEXT "MediaPlayer-PlaylistWindow"
    PlaylistWindow::PlaylistWindow(BRect frame, Playlist* playlist,  
    8288    fPlaylist(playlist),
    8389    fLocker(new RWLocker("command stack lock")),
    8490    fCommandStack(new CommandStack(fLocker)),
    85     fCommandStackListener(this)
     91    fCommandStackListener(this),
     92    fDurationListener(new DurationListener(*this))
    8693{
    8794    frame = Bounds();
    8895
    PlaylistWindow::PlaylistWindow(BRect frame, Playlist* playlist,  
    9097        // will adjust frame to account for menubar
    9198
    9299    frame.right -= B_V_SCROLL_BAR_WIDTH;
     100    frame.bottom -= B_H_SCROLL_BAR_HEIGHT;
    93101    fListView = new PlaylistListView(frame, playlist, controller,
    94102        fCommandStack);
    95103
    PlaylistWindow::PlaylistWindow(BRect frame, Playlist* playlist,  
    104112        // make it so the frame of the menubar is also the frame of
    105113        // the scroll bar (appears to be)
    106114        scrollBar->MoveBy(0, -1);
    107         scrollBar->ResizeBy(0, -(B_H_SCROLL_BAR_HEIGHT - 2));
     115        scrollBar->ResizeBy(0, 2);
     116    }
     117
     118    frame.top += frame.Height();
     119    frame.bottom += B_H_SCROLL_BAR_HEIGHT;
     120
     121    fTotalDuration = new BStringView(frame, "fDuration", "",
     122        B_FOLLOW_BOTTOM | B_FOLLOW_LEFT_RIGHT);
     123    AddChild(fTotalDuration);
     124
     125    _UpdateTotalDuration(0);
     126
     127    {
     128        BAutolock _(fPlaylist);
     129
     130        _QueryInitialDurations();
     131        fPlaylist->AddListener(fDurationListener);
    108132    }
    109133
    110134    fCommandStack->AddListener(&fCommandStackListener);
    PlaylistWindow::~PlaylistWindow()  
    121145    fCommandStack->RemoveListener(&fCommandStackListener);
    122146    delete fCommandStack;
    123147    delete fLocker;
     148
     149    fPlaylist->RemoveListener(fDurationListener);
     150    BMessenger(fDurationListener).SendMessage(B_QUIT_REQUESTED);
    124151}
    125152
    126153
    PlaylistWindow::_SavePlaylist(BEntry& origEntry, BEntry& tempEntry,  
    422449    info.SetType("application/x-vnd.haiku-playlist");
    423450}
    424451
     452
     453void
     454PlaylistWindow::_QueryInitialDurations()
     455{
     456    BAutolock lock(fPlaylist);
     457
     458    BMessage addMessage(MSG_PLAYLIST_ITEM_ADDED);
     459    for (int32 i = 0; i < fPlaylist->CountItems(); i++) {
     460        addMessage.AddPointer("item", fPlaylist->ItemAt(i));
     461        addMessage.AddInt32("index", i);
     462    }
     463
     464    BMessenger(fDurationListener).SendMessage(&addMessage);
     465}
     466
     467
     468void
     469PlaylistWindow::_UpdateTotalDuration(bigtime_t duration)
     470{
     471    BAutolock lock(this);
     472
     473    char buffer[64];
     474    duration /= 1000000;
     475    duration_to_string(duration, buffer, sizeof(buffer));
     476
     477    BString text;
     478    text.SetToFormat(B_TRANSLATE("Total duration : %s"), buffer);
     479
     480    fTotalDuration->SetText(text.String());
     481}
     482
     483
     484// #pragma mark -
     485
     486
     487PlaylistWindow::DurationListener::DurationListener(PlaylistWindow& parent)
     488    :
     489    PlaylistObserver(this),
     490    fKnown(20, true),
     491    fTotalDuration(0),
     492    fParent(parent)
     493{
     494    Run();
     495}
     496
     497
     498PlaylistWindow::DurationListener::~DurationListener()
     499{
     500}
     501
     502
     503void
     504PlaylistWindow::DurationListener::MessageReceived(BMessage* message)
     505{
     506    switch (message->what) {
     507        case MSG_PLAYLIST_ITEM_ADDED:
     508        {
     509            void* item;
     510            int32 index;
     511
     512            int32 currentItem = 0;
     513            while (message->FindPointer("item", currentItem, &item) == B_OK
     514                && message->FindInt32("index", currentItem, &index) == B_OK) {
     515                _HandleItemAdded(static_cast<PlaylistItem*>(item), index);
     516                ++currentItem;
     517            }
     518
     519            break;
     520        }
     521
     522        case MSG_PLAYLIST_ITEM_REMOVED:
     523        {
     524            int32 index;
     525
     526            if (message->FindInt32("index", &index) == B_OK) {
     527                _HandleItemRemoved(index);
     528            }
     529
     530            break;
     531        }
     532
     533        default:
     534            BLooper::MessageReceived(message);
     535            break;
     536    }
     537}
     538
     539
     540bigtime_t
     541PlaylistWindow::DurationListener::TotalDuration()
     542{
     543    return fTotalDuration;
     544}
     545
     546
     547void
     548PlaylistWindow::DurationListener::_HandleItemAdded(PlaylistItem* item,
     549    int32 index)
     550{
     551    bigtime_t duration = _DetermineItemDuration(item);
     552    fTotalDuration += duration;
     553    fParent._UpdateTotalDuration(fTotalDuration);
     554    fKnown.AddItem(new bigtime_t(duration), index);
     555}
     556
     557
     558void
     559PlaylistWindow::DurationListener::_HandleItemRemoved(int32 index)
     560{
     561    bigtime_t* deleted = fKnown.RemoveItemAt(index);
     562    fTotalDuration -= *deleted;
     563    fParent._UpdateTotalDuration(fTotalDuration);
     564
     565    delete deleted;
     566}
     567
     568
     569bigtime_t
     570PlaylistWindow::DurationListener::_DetermineItemDuration(PlaylistItem* item)
     571{
     572    bigtime_t duration;
     573    if (item->GetAttribute(PlaylistItem::ATTR_INT64_DURATION, duration) == B_OK)
     574        return duration;
     575
     576    // We have to find out the duration ourselves
     577    if (FilePlaylistItem* file = dynamic_cast<FilePlaylistItem*>(item)) {
     578        // We are dealing with a file
     579        BMediaFile mediaFile(&file->Ref());
     580
     581        if (mediaFile.InitCheck() != B_OK || mediaFile.CountTracks() < 1)
     582            return 0;
     583
     584        duration =  mediaFile.TrackAt(0)->Duration();
     585    } else {
     586        // Not a file, so fall back to the generic TrackSupplier solution
     587        TrackSupplier* supplier = item->CreateTrackSupplier();
     588
     589        AudioTrackSupplier* au = supplier->CreateAudioTrackForIndex(0);
     590        VideoTrackSupplier* vi = supplier->CreateVideoTrackForIndex(0);
     591
     592        duration = max_c(au == NULL ? 0 : au->Duration(),
     593            vi == NULL ? 0 : vi->Duration());
     594
     595        delete vi;
     596        delete au;
     597        delete supplier;
     598    }
     599
     600    // Store the duration for later use
     601    item->SetAttribute(PlaylistItem::ATTR_INT64_DURATION, duration);
     602
     603    return duration;
     604}
     605
  • src/apps/mediaplayer/playlist/PlaylistWindow.h

    diff --git a/src/apps/mediaplayer/playlist/PlaylistWindow.h b/src/apps/mediaplayer/playlist/PlaylistWindow.h
    index 2372385..6b70e76 100644
    a b  
    1111
    1212
    1313#include <Entry.h>
     14#include <ObjectList.h>
    1415#include <Window.h>
    1516
     17#include "PlaylistObserver.h"
    1618#include "ListenerAdapter.h"
    1719
    1820
    class BMenuItem;  
    2123class CommandStack;
    2224class Controller;
    2325class Notifier;
    24 class Playlist;
    2526class PlaylistListView;
    2627class RWLocker;
    2728class BButton;
    2829class BFilePanel;
     30class BStringView;
    2931
    3032
    3133enum {
    public:  
    5355    virtual void                MessageReceived(BMessage* message);
    5456
    5557private:
     58
     59    class DurationListener : public PlaylistObserver, public BLooper {
     60    public:
     61
     62                                DurationListener(PlaylistWindow& parent);
     63                                ~DurationListener();
     64
     65            void                MessageReceived(BMessage* message);
     66
     67            bigtime_t           TotalDuration();
     68
     69    private:
     70            void                _HandleItemAdded(PlaylistItem* item,
     71                                    int32 index);
     72            void                _HandleItemRemoved(int32 index);
     73            bigtime_t           _DetermineItemDuration(PlaylistItem* item);
     74
     75            BObjectList<bigtime_t>
     76                                fKnown;
     77            bigtime_t           fTotalDuration;
     78            PlaylistWindow&     fParent;
     79    };
     80
     81    friend class DurationListener;
     82
    5683            void                _CreateMenu(BRect& frame);
    5784            void                _ObjectChanged(const Notifier* object);
    5885            void                _SavePlaylist(const BMessage* filePanelMessage);
    5986            void                _SavePlaylist(const entry_ref& ref);
    6087            void                _SavePlaylist(BEntry& origEntry,
    6188                                    BEntry& tempEntry, const char* finalName);
     89            void                _QueryInitialDurations();
     90            void                _UpdateTotalDuration(bigtime_t duration);
    6291
    6392            Playlist*           fPlaylist;
    6493            PlaylistListView*   fListView;
    private:  
    72101            ListenerAdapter     fCommandStackListener;
    73102
    74103            entry_ref           fSavedPlaylistRef;
     104
     105            DurationListener*   fDurationListener;
     106            BStringView*        fTotalDuration;
    75107};
    76108
    77109