Ticket #6564: mediaplayer.patch

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

No functional change compared to previous verson

  • src/apps/mediaplayer/Controller.cpp

    From d7a26102209b6cbc19bc09d98b4b67464f4cee1f 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/PlaylistWindow.cpp

    -- 
    2.2.2
    
    
    From 433d5e42b9ed6080fd81cc892483b0f34da64a38 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/PlaylistWindow.cpp | 179 ++++++++++++++++++++++-
     src/apps/mediaplayer/playlist/PlaylistWindow.h   |  34 ++++-
     2 files changed, 209 insertions(+), 4 deletions(-)
    
    diff --git a/src/apps/mediaplayer/playlist/PlaylistWindow.cpp b/src/apps/mediaplayer/playlist/PlaylistWindow.cpp
    index 08ce627..faedf92 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(*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    fDuration = new BStringView(frame, "fDuration", "",
     122        B_FOLLOW_BOTTOM | B_FOLLOW_LEFT_RIGHT);
     123    AddChild(fDuration);
     124
     125    _UpdateDuration(0);
     126
     127    {
     128        BAutolock _(fPlaylist);
     129
     130        _GetInitialDuration();
     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);
    124150}
    125151
    126152
    PlaylistWindow::_SavePlaylist(BEntry& origEntry, BEntry& tempEntry,  
    422448    info.SetType("application/x-vnd.haiku-playlist");
    423449}
    424450
     451
     452void
     453PlaylistWindow::_GetInitialDuration()
     454{
     455    BAutolock lock(fPlaylist);
     456
     457    for (int32 i = 0; i < fPlaylist->CountItems(); i++) {
     458        BMessage addMessage(MSG_PLAYLIST_ITEM_ADDED);
     459        addMessage.AddPointer("item", fPlaylist->ItemAt(i));
     460        addMessage.AddInt32("index", i);
     461
     462        BMessenger(&fDurationListener).SendMessage(&addMessage);
     463    }
     464}
     465
     466
     467void
     468PlaylistWindow::_UpdateDuration(bigtime_t duration)
     469{
     470    BAutolock lock(this);
     471
     472    BString text;
     473
     474    char buffer[64];
     475    duration /= 1000000;
     476    duration_to_string(duration, buffer, sizeof(buffer));
     477
     478    text << B_TRANSLATE("Total duration:") << " " << buffer;
     479
     480    fDuration->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    fDuration(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            if (message->FindPointer("item", &item) == B_OK
     513                && message->FindInt32("index", &index) == B_OK) {
     514                _HandleItemAdded(static_cast<PlaylistItem*>(item), index);
     515            }
     516
     517            break;
     518        }
     519
     520        case MSG_PLAYLIST_ITEM_REMOVED:
     521        {
     522            int32 index;
     523
     524            if (message->FindInt32("index", &index) == B_OK) {
     525                _HandleItemRemoved(index);
     526            }
     527
     528            break;
     529        }
     530
     531        default:
     532            BLooper::MessageReceived(message);
     533            break;
     534    }
     535}
     536
     537
     538bigtime_t
     539PlaylistWindow::DurationListener::Duration()
     540{
     541    return fDuration;
     542}
     543
     544
     545void
     546PlaylistWindow::DurationListener::_HandleItemAdded(PlaylistItem* item,
     547    int32 index)
     548{
     549    bigtime_t duration = _DetermineDuration(item);
     550    fDuration += duration;
     551    fParent._UpdateDuration(fDuration);
     552    fKnown.AddItem(new bigtime_t(duration), index);
     553}
     554
     555
     556void
     557PlaylistWindow::DurationListener::_HandleItemRemoved(int32 index)
     558{
     559    bigtime_t* deleted = fKnown.RemoveItemAt(index);
     560    fDuration -= *deleted;
     561    fParent._UpdateDuration(fDuration);
     562
     563    delete deleted;
     564}
     565
     566
     567bigtime_t
     568PlaylistWindow::DurationListener::_DetermineDuration(PlaylistItem* item)
     569{
     570    // TODO: Save track duration to an attributee
     571    if (FilePlaylistItem* file = dynamic_cast<FilePlaylistItem*>(item)) {
     572        // We are dealing with a file
     573        BMediaFile mediaFile(&file->Ref());
     574
     575        if (mediaFile.InitCheck() != B_OK || mediaFile.CountTracks() < 1)
     576            return 0;
     577
     578        return mediaFile.TrackAt(0)->Duration();
     579    } else {
     580        // Not a file, so fall back to the generic TrackSupplier solution
     581
     582        TrackSupplier* supplier = item->CreateTrackSupplier();
     583
     584        AudioTrackSupplier* au = supplier->CreateAudioTrackForIndex(0);
     585        VideoTrackSupplier* vi = supplier->CreateVideoTrackForIndex(0);
     586
     587        bigtime_t duration = max_c(au == NULL ? 0 : au->Duration(),
     588            vi == NULL ? 0 : vi->Duration());
     589
     590        delete vi;
     591        delete au;
     592        delete supplier;
     593
     594        return duration;
     595    }
     596}
     597
  • src/apps/mediaplayer/playlist/PlaylistWindow.h

    diff --git a/src/apps/mediaplayer/playlist/PlaylistWindow.h b/src/apps/mediaplayer/playlist/PlaylistWindow.h
    index 2372385..9d799e2 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           Duration();
     68
     69    private:
     70            void                _HandleItemAdded(PlaylistItem* item,
     71                                    int32 index);
     72            void                _HandleItemRemoved(int32 index);
     73            bigtime_t           _DetermineDuration(PlaylistItem* item);
     74
     75            BObjectList<bigtime_t>
     76                                fKnown;
     77            bigtime_t           fDuration;
     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                _GetInitialDuration();
     90            void                _UpdateDuration(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*        fDuration;
    75107};
    76108
    77109