Ticket #11981: 0001-media_addon_server-cleanup-of-MediaFilePlayer.patch

File 0001-media_addon_server-cleanup-of-MediaFilePlayer.patch, 6.2 KB (added by Barrett, 5 years ago)
  • src/servers/media_addon/MediaFilePlayer.cpp

    From 3d8afd5dd9feaedac2cda7e95306516259d72db9 Mon Sep 17 00:00:00 2001
    From: Dario Casalinuovo <b.vitruvio@gmail.com>
    Date: Fri, 17 Apr 2015 18:31:12 +0200
    Subject: [PATCH 1/2] media_addon_server: cleanup of MediaFilePlayer.
    
    ---
     src/servers/media_addon/MediaFilePlayer.cpp | 71 +++++++++++++++++++----------
     src/servers/media_addon/MediaFilePlayer.h   | 51 +++++++++++----------
     2 files changed, 74 insertions(+), 48 deletions(-)
    
    diff --git a/src/servers/media_addon/MediaFilePlayer.cpp b/src/servers/media_addon/MediaFilePlayer.cpp
    index de872ea..9ce7818 100644
    a b  
    44 * Distributed under the terms of the MIT License.
    55 */
    66
    7 #include <MediaFiles.h>
     7
    88#include "MediaFilePlayer.h"
    9 #include "ObjectList.h"
    109
    11 #include <stdio.h>
     10#include <MediaFiles.h>
     11#include <ObjectList.h>
     12
    1213#include <stdlib.h>
    1314
     15
    1416BObjectList<MediaFilePlayer> list;
    1517
    16 MediaFilePlayer *
    17 FindMediaFilePlayer(MediaFilePlayer *player, void *media_name)
     18
     19MediaFilePlayer*
     20FindMediaFilePlayer(MediaFilePlayer* player, void* media_name)
    1821{
    19     if (!strcmp(player->Name(), (const char *)media_name))
     22    if (strcmp(player->Name(), (const char*)media_name) == 0)
    2023        return player;
    2124    return NULL;
    2225}
    2326
    2427
    2528void
    26 PlayMediaFile(const char *media_type, const char *media_name)
     29PlayMediaFile(const char* media_type, const char* media_name)
    2730{
    2831    entry_ref ref;
    29     if (BMediaFiles().GetRefFor(media_type, media_name, &ref)!=B_OK
     32    if (BMediaFiles().GetRefFor(media_type, media_name, &ref) != B_OK
    3033        || !BEntry(&ref).Exists())
    3134        return;
    3235
    33     MediaFilePlayer *player = list.EachElement(FindMediaFilePlayer, (void *)media_name);
    34     if (player) {
     36    MediaFilePlayer* player = list.EachElement(FindMediaFilePlayer,
     37        (void*)media_name);
     38
     39    if (player != NULL) {
    3540        if (*(player->Ref()) == ref) {
    3641            player->Restart();
    3742            return;
    PlayMediaFile(const char *media_type, const char *media_name)  
    4247        player = NULL;
    4348    }
    4449
    45     if (!player) {
     50    if (player == NULL) {
    4651        player = new MediaFilePlayer(media_type, media_name, &ref);
    4752        if (player->InitCheck() == B_OK)
    4853            list.AddItem(player);
    PlayMediaFile(const char *media_type, const char *media_name)  
    5358
    5459
    5560
    56 MediaFilePlayer::MediaFilePlayer(const char *media_type,
    57     const char *media_name, entry_ref *ref) :
     61MediaFilePlayer::MediaFilePlayer(const char* media_type,
     62    const char* media_name, entry_ref* ref)
     63    :
    5864    fInitCheck(B_ERROR),
    5965    fRef(*ref),
    6066    fSoundPlayer(NULL),
    MediaFilePlayer::MediaFilePlayer(const char *media_type,  
    6369    fName = strdup(media_name);
    6470
    6571    fPlayFile = new BMediaFile(&fRef);
    66     if ((fInitCheck = fPlayFile->InitCheck()) <B_OK) {
     72    fInitCheck = fPlayFile->InitCheck();
     73    if (fInitCheck != B_OK)
    6774        return;
    68     }
    6975
    7076    memset(&fPlayFormat, 0, sizeof(fPlayFormat));
    7177
    7278    for (int i=0; i < fPlayFile->CountTracks(); i++) {
    73         BMediaTrack *track = fPlayFile->TrackAt(i);
     79        BMediaTrack* track = fPlayFile->TrackAt(i);
    7480        if (track == NULL)
    7581            continue;
    7682        fPlayFormat.type = B_MEDIA_RAW_AUDIO;
    MediaFilePlayer::MediaFilePlayer(const char *media_type,  
    8894        return;
    8995    }
    9096
    91     fSoundPlayer = new BSoundPlayer(&fPlayFormat.u.raw_audio, media_name, PlayFunction,
    92         NULL, this);
    93     if ((fInitCheck = fSoundPlayer->InitCheck()) != B_OK) {
     97    fSoundPlayer = new BSoundPlayer(&fPlayFormat.u.raw_audio,
     98        media_name, PlayFunction, NULL, this);
     99
     100    fInitCheck = fSoundPlayer->InitCheck();
     101    if (fInitCheck != B_OK)
    94102        return;
    95     }
    96103
    97104    fSoundPlayer->SetVolume(1.0f);
    98105    fSoundPlayer->SetHasData(true);
    MediaFilePlayer::InitCheck()  
    115122}
    116123
    117124
     125const char*
     126MediaFilePlayer::Name()
     127{
     128    return fName;
     129}
     130
     131
     132const entry_ref*
     133MediaFilePlayer::Ref()
     134{
     135    return &fRef;
     136}
     137
     138
    118139bool
    119140MediaFilePlayer::IsPlaying()
    120141{
    121     return (fSoundPlayer && fSoundPlayer->HasData());
     142    return (fSoundPlayer != NULL && fSoundPlayer->HasData());
    122143}
    123144
    124145void
    MediaFilePlayer::Stop()  
    140161
    141162
    142163void
    143 MediaFilePlayer::PlayFunction(void *cookie, void * buffer, size_t size, const media_raw_audio_format & format)
     164MediaFilePlayer::PlayFunction(void* cookie, void* buffer,
     165    size_t size, const media_raw_audio_format& format)
    144166{
    145     MediaFilePlayer *player = (MediaFilePlayer *)cookie;
     167    MediaFilePlayer* player = (MediaFilePlayer*)cookie;
    146168    int64 frames = 0;
    147169    player->fPlayTrack->ReadFrames(buffer, &frames);
    148170
    149     if (frames <=0) {
     171    if (frames <= 0)
    150172        player->fSoundPlayer->SetHasData(false);
    151     }
    152173}
  • src/servers/media_addon/MediaFilePlayer.h

    diff --git a/src/servers/media_addon/MediaFilePlayer.h b/src/servers/media_addon/MediaFilePlayer.h
    index 4c85458..508be95 100644
    a b  
    55#ifndef _MEDIA_FILE_PLAYER_H
    66#define _MEDIA_FILE_PLAYER_H
    77
     8
    89#include <Entry.h>
    9 #include <SoundPlayer.h>
     10#include <MediaDefs.h>
    1011#include <MediaFile.h>
    1112#include <MediaTrack.h>
    12 #include <MediaDefs.h>
     13#include <SoundPlayer.h>
    1314
    1415
    15 void PlayMediaFile(const char *media_type, const char *media_name);
     16void PlayMediaFile(const char* media_type, const char* media_name);
    1617
    1718
    1819class MediaFilePlayer
    1920{
    2021public:
    21     MediaFilePlayer(const char *media_type, const char *media_name,
    22         entry_ref *ref);
    23     ~MediaFilePlayer();
    24 
    25     bool IsPlaying();
    26     void Restart();
    27     void Stop();
    28     status_t InitCheck();
    29     const char *Name() { return fName; };
    30     const entry_ref *Ref() { return &fRef; };
    31    
    32     static void PlayFunction(void *cookie, void * buffer,
    33         size_t size, const media_raw_audio_format & format);
     22                            MediaFilePlayer(const char* media_type,
     23                                const char* media_name,
     24                                entry_ref* ref);
     25                            ~MediaFilePlayer();
     26
     27    status_t                InitCheck();
     28
     29    bool                    IsPlaying();
     30    void                    Restart();
     31    void                    Stop();
     32
     33    const char*             Name();
     34    const entry_ref*        Ref();
     35
     36    static void             PlayFunction(void* cookie, void* buffer,
     37                                size_t size,
     38                                const media_raw_audio_format& format);
    3439
    3540private:
    36     char        *fName;
    37     status_t    fInitCheck;
    38     entry_ref   fRef;
    39     BSoundPlayer *fSoundPlayer;
    40     BMediaFile *fPlayFile;
    41     BMediaTrack *fPlayTrack;
    42     media_format fPlayFormat;
     41    char*                   fName;
     42    status_t                fInitCheck;
     43    entry_ref               fRef;
     44    BSoundPlayer*           fSoundPlayer;
     45    BMediaFile*             fPlayFile;
     46    BMediaTrack*            fPlayTrack;
     47    media_format            fPlayFormat;
    4348};
    4449
    4550#endif // _MEDIA_FILE_PLAYER_H