media_addon_server: cleanup of MediaFilePlayer

This commit is contained in:
Dario Casalinuovo
2015-07-09 23:45:17 +02:00
parent 7bcdb36249
commit 8208641822
2 changed files with 74 additions and 48 deletions
+47 -26
View File
@@ -4,34 +4,39 @@
* Distributed under the terms of the MIT License. * Distributed under the terms of the MIT License.
*/ */
#include <MediaFiles.h>
#include "MediaFilePlayer.h"
#include "ObjectList.h"
#include <stdio.h> #include "MediaFilePlayer.h"
#include <MediaFiles.h>
#include <ObjectList.h>
#include <stdlib.h> #include <stdlib.h>
BObjectList<MediaFilePlayer> list; BObjectList<MediaFilePlayer> list;
MediaFilePlayer *
FindMediaFilePlayer(MediaFilePlayer *player, void *media_name) MediaFilePlayer*
FindMediaFilePlayer(MediaFilePlayer* player, void* media_name)
{ {
if (!strcmp(player->Name(), (const char *)media_name)) if (strcmp(player->Name(), (const char*)media_name) == 0)
return player; return player;
return NULL; return NULL;
} }
void void
PlayMediaFile(const char *media_type, const char *media_name) PlayMediaFile(const char* media_type, const char* media_name)
{ {
entry_ref ref; entry_ref ref;
if (BMediaFiles().GetRefFor(media_type, media_name, &ref)!=B_OK if (BMediaFiles().GetRefFor(media_type, media_name, &ref) != B_OK
|| !BEntry(&ref).Exists()) || !BEntry(&ref).Exists())
return; return;
MediaFilePlayer *player = list.EachElement(FindMediaFilePlayer, (void *)media_name); MediaFilePlayer* player = list.EachElement(FindMediaFilePlayer,
if (player) { (void*)media_name);
if (player != NULL) {
if (*(player->Ref()) == ref) { if (*(player->Ref()) == ref) {
player->Restart(); player->Restart();
return; return;
@@ -42,7 +47,7 @@ PlayMediaFile(const char *media_type, const char *media_name)
player = NULL; player = NULL;
} }
if (!player) { if (player == NULL) {
player = new MediaFilePlayer(media_type, media_name, &ref); player = new MediaFilePlayer(media_type, media_name, &ref);
if (player->InitCheck() == B_OK) if (player->InitCheck() == B_OK)
list.AddItem(player); list.AddItem(player);
@@ -53,8 +58,9 @@ PlayMediaFile(const char *media_type, const char *media_name)
MediaFilePlayer::MediaFilePlayer(const char *media_type, MediaFilePlayer::MediaFilePlayer(const char* media_type,
const char *media_name, entry_ref *ref) : const char* media_name, entry_ref* ref)
:
fInitCheck(B_ERROR), fInitCheck(B_ERROR),
fRef(*ref), fRef(*ref),
fSoundPlayer(NULL), fSoundPlayer(NULL),
@@ -63,14 +69,14 @@ MediaFilePlayer::MediaFilePlayer(const char *media_type,
fName = strdup(media_name); fName = strdup(media_name);
fPlayFile = new BMediaFile(&fRef); fPlayFile = new BMediaFile(&fRef);
if ((fInitCheck = fPlayFile->InitCheck()) <B_OK) { fInitCheck = fPlayFile->InitCheck();
if (fInitCheck != B_OK)
return; return;
}
memset(&fPlayFormat, 0, sizeof(fPlayFormat)); memset(&fPlayFormat, 0, sizeof(fPlayFormat));
for (int i=0; i < fPlayFile->CountTracks(); i++) { for (int i=0; i < fPlayFile->CountTracks(); i++) {
BMediaTrack *track = fPlayFile->TrackAt(i); BMediaTrack* track = fPlayFile->TrackAt(i);
if (track == NULL) if (track == NULL)
continue; continue;
fPlayFormat.type = B_MEDIA_RAW_AUDIO; fPlayFormat.type = B_MEDIA_RAW_AUDIO;
@@ -88,11 +94,12 @@ MediaFilePlayer::MediaFilePlayer(const char *media_type,
return; return;
} }
fSoundPlayer = new BSoundPlayer(&fPlayFormat.u.raw_audio, media_name, PlayFunction, fSoundPlayer = new BSoundPlayer(&fPlayFormat.u.raw_audio,
NULL, this); media_name, PlayFunction, NULL, this);
if ((fInitCheck = fSoundPlayer->InitCheck()) != B_OK) {
fInitCheck = fSoundPlayer->InitCheck();
if (fInitCheck != B_OK)
return; return;
}
fSoundPlayer->SetVolume(1.0f); fSoundPlayer->SetVolume(1.0f);
fSoundPlayer->SetHasData(true); fSoundPlayer->SetHasData(true);
@@ -115,10 +122,24 @@ MediaFilePlayer::InitCheck()
} }
const char*
MediaFilePlayer::Name()
{
return fName;
}
const entry_ref*
MediaFilePlayer::Ref()
{
return &fRef;
}
bool bool
MediaFilePlayer::IsPlaying() MediaFilePlayer::IsPlaying()
{ {
return (fSoundPlayer && fSoundPlayer->HasData()); return (fSoundPlayer != NULL && fSoundPlayer->HasData());
} }
void void
@@ -140,13 +161,13 @@ MediaFilePlayer::Stop()
void void
MediaFilePlayer::PlayFunction(void *cookie, void * buffer, size_t size, const media_raw_audio_format & format) MediaFilePlayer::PlayFunction(void* cookie, void* buffer,
size_t size, const media_raw_audio_format& format)
{ {
MediaFilePlayer *player = (MediaFilePlayer *)cookie; MediaFilePlayer* player = (MediaFilePlayer*)cookie;
int64 frames = 0; int64 frames = 0;
player->fPlayTrack->ReadFrames(buffer, &frames); player->fPlayTrack->ReadFrames(buffer, &frames);
if (frames <=0) { if (frames <= 0)
player->fSoundPlayer->SetHasData(false); player->fSoundPlayer->SetHasData(false);
}
} }
+27 -22
View File
@@ -5,41 +5,46 @@
#ifndef _MEDIA_FILE_PLAYER_H #ifndef _MEDIA_FILE_PLAYER_H
#define _MEDIA_FILE_PLAYER_H #define _MEDIA_FILE_PLAYER_H
#include <Entry.h> #include <Entry.h>
#include <SoundPlayer.h> #include <MediaDefs.h>
#include <MediaFile.h> #include <MediaFile.h>
#include <MediaTrack.h> #include <MediaTrack.h>
#include <MediaDefs.h> #include <SoundPlayer.h>
void PlayMediaFile(const char *media_type, const char *media_name); void PlayMediaFile(const char* media_type, const char* media_name);
class MediaFilePlayer class MediaFilePlayer
{ {
public: public:
MediaFilePlayer(const char *media_type, const char *media_name, MediaFilePlayer(const char* media_type,
entry_ref *ref); const char* media_name,
~MediaFilePlayer(); entry_ref* ref);
~MediaFilePlayer();
bool IsPlaying(); status_t InitCheck();
void Restart();
void Stop(); bool IsPlaying();
status_t InitCheck(); void Restart();
const char *Name() { return fName; }; void Stop();
const entry_ref *Ref() { return &fRef; };
const char* Name();
static void PlayFunction(void *cookie, void * buffer, const entry_ref* Ref();
size_t size, const media_raw_audio_format & format);
static void PlayFunction(void* cookie, void* buffer,
size_t size,
const media_raw_audio_format& format);
private: private:
char *fName; char* fName;
status_t fInitCheck; status_t fInitCheck;
entry_ref fRef; entry_ref fRef;
BSoundPlayer *fSoundPlayer; BSoundPlayer* fSoundPlayer;
BMediaFile *fPlayFile; BMediaFile* fPlayFile;
BMediaTrack *fPlayTrack; BMediaTrack* fPlayTrack;
media_format fPlayFormat; media_format fPlayFormat;
}; };
#endif // _MEDIA_FILE_PLAYER_H #endif // _MEDIA_FILE_PLAYER_H