media_addon_server: cleanup of MediaFilePlayer

This commit is contained in:
Dario Casalinuovo 2015-04-17 18:31:12 +02:00
parent 7bcdb36249
commit 8208641822
2 changed files with 74 additions and 48 deletions

View File

@ -4,34 +4,39 @@
* 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>
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 NULL;
}
void
PlayMediaFile(const char *media_type, const char *media_name)
PlayMediaFile(const char* media_type, const char* media_name)
{
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())
return;
MediaFilePlayer *player = list.EachElement(FindMediaFilePlayer, (void *)media_name);
if (player) {
MediaFilePlayer* player = list.EachElement(FindMediaFilePlayer,
(void*)media_name);
if (player != NULL) {
if (*(player->Ref()) == ref) {
player->Restart();
return;
@ -42,7 +47,7 @@ PlayMediaFile(const char *media_type, const char *media_name)
player = NULL;
}
if (!player) {
if (player == NULL) {
player = new MediaFilePlayer(media_type, media_name, &ref);
if (player->InitCheck() == B_OK)
list.AddItem(player);
@ -53,8 +58,9 @@ PlayMediaFile(const char *media_type, const char *media_name)
MediaFilePlayer::MediaFilePlayer(const char *media_type,
const char *media_name, entry_ref *ref) :
MediaFilePlayer::MediaFilePlayer(const char* media_type,
const char* media_name, entry_ref* ref)
:
fInitCheck(B_ERROR),
fRef(*ref),
fSoundPlayer(NULL),
@ -63,14 +69,14 @@ MediaFilePlayer::MediaFilePlayer(const char *media_type,
fName = strdup(media_name);
fPlayFile = new BMediaFile(&fRef);
if ((fInitCheck = fPlayFile->InitCheck()) <B_OK) {
fInitCheck = fPlayFile->InitCheck();
if (fInitCheck != B_OK)
return;
}
memset(&fPlayFormat, 0, sizeof(fPlayFormat));
for (int i=0; i < fPlayFile->CountTracks(); i++) {
BMediaTrack *track = fPlayFile->TrackAt(i);
BMediaTrack* track = fPlayFile->TrackAt(i);
if (track == NULL)
continue;
fPlayFormat.type = B_MEDIA_RAW_AUDIO;
@ -88,11 +94,12 @@ MediaFilePlayer::MediaFilePlayer(const char *media_type,
return;
}
fSoundPlayer = new BSoundPlayer(&fPlayFormat.u.raw_audio, media_name, PlayFunction,
NULL, this);
if ((fInitCheck = fSoundPlayer->InitCheck()) != B_OK) {
fSoundPlayer = new BSoundPlayer(&fPlayFormat.u.raw_audio,
media_name, PlayFunction, NULL, this);
fInitCheck = fSoundPlayer->InitCheck();
if (fInitCheck != B_OK)
return;
}
fSoundPlayer->SetVolume(1.0f);
fSoundPlayer->SetHasData(true);
@ -115,10 +122,24 @@ MediaFilePlayer::InitCheck()
}
const char*
MediaFilePlayer::Name()
{
return fName;
}
const entry_ref*
MediaFilePlayer::Ref()
{
return &fRef;
}
bool
MediaFilePlayer::IsPlaying()
{
return (fSoundPlayer && fSoundPlayer->HasData());
return (fSoundPlayer != NULL && fSoundPlayer->HasData());
}
void
@ -140,13 +161,13 @@ MediaFilePlayer::Stop()
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;
player->fPlayTrack->ReadFrames(buffer, &frames);
if (frames <=0) {
if (frames <= 0)
player->fSoundPlayer->SetHasData(false);
}
}

View File

@ -5,41 +5,46 @@
#ifndef _MEDIA_FILE_PLAYER_H
#define _MEDIA_FILE_PLAYER_H
#include <Entry.h>
#include <SoundPlayer.h>
#include <MediaDefs.h>
#include <MediaFile.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
{
public:
MediaFilePlayer(const char *media_type, const char *media_name,
entry_ref *ref);
~MediaFilePlayer();
MediaFilePlayer(const char* media_type,
const char* media_name,
entry_ref* ref);
~MediaFilePlayer();
bool IsPlaying();
void Restart();
void Stop();
status_t InitCheck();
const char *Name() { return fName; };
const entry_ref *Ref() { return &fRef; };
static void PlayFunction(void *cookie, void * buffer,
size_t size, const media_raw_audio_format & format);
status_t InitCheck();
bool IsPlaying();
void Restart();
void Stop();
const char* Name();
const entry_ref* Ref();
static void PlayFunction(void* cookie, void* buffer,
size_t size,
const media_raw_audio_format& format);
private:
char *fName;
status_t fInitCheck;
entry_ref fRef;
BSoundPlayer *fSoundPlayer;
BMediaFile *fPlayFile;
BMediaTrack *fPlayTrack;
media_format fPlayFormat;
char* fName;
status_t fInitCheck;
entry_ref fRef;
BSoundPlayer* fSoundPlayer;
BMediaFile* fPlayFile;
BMediaTrack* fPlayTrack;
media_format fPlayFormat;
};
#endif // _MEDIA_FILE_PLAYER_H