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
|
|
4 | 4 | * Distributed under the terms of the MIT License. |
5 | 5 | */ |
6 | 6 | |
7 | | #include <MediaFiles.h> |
| 7 | |
8 | 8 | #include "MediaFilePlayer.h" |
9 | | #include "ObjectList.h" |
10 | 9 | |
11 | | #include <stdio.h> |
| 10 | #include <MediaFiles.h> |
| 11 | #include <ObjectList.h> |
| 12 | |
12 | 13 | #include <stdlib.h> |
13 | 14 | |
| 15 | |
14 | 16 | BObjectList<MediaFilePlayer> list; |
15 | 17 | |
16 | | MediaFilePlayer * |
17 | | FindMediaFilePlayer(MediaFilePlayer *player, void *media_name) |
| 18 | |
| 19 | MediaFilePlayer* |
| 20 | FindMediaFilePlayer(MediaFilePlayer* player, void* media_name) |
18 | 21 | { |
19 | | if (!strcmp(player->Name(), (const char *)media_name)) |
| 22 | if (strcmp(player->Name(), (const char*)media_name) == 0) |
20 | 23 | return player; |
21 | 24 | return NULL; |
22 | 25 | } |
23 | 26 | |
24 | 27 | |
25 | 28 | void |
26 | | PlayMediaFile(const char *media_type, const char *media_name) |
| 29 | PlayMediaFile(const char* media_type, const char* media_name) |
27 | 30 | { |
28 | 31 | 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 |
30 | 33 | || !BEntry(&ref).Exists()) |
31 | 34 | return; |
32 | 35 | |
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) { |
35 | 40 | if (*(player->Ref()) == ref) { |
36 | 41 | player->Restart(); |
37 | 42 | return; |
… |
… |
PlayMediaFile(const char *media_type, const char *media_name)
|
42 | 47 | player = NULL; |
43 | 48 | } |
44 | 49 | |
45 | | if (!player) { |
| 50 | if (player == NULL) { |
46 | 51 | player = new MediaFilePlayer(media_type, media_name, &ref); |
47 | 52 | if (player->InitCheck() == B_OK) |
48 | 53 | list.AddItem(player); |
… |
… |
PlayMediaFile(const char *media_type, const char *media_name)
|
53 | 58 | |
54 | 59 | |
55 | 60 | |
56 | | MediaFilePlayer::MediaFilePlayer(const char *media_type, |
57 | | const char *media_name, entry_ref *ref) : |
| 61 | MediaFilePlayer::MediaFilePlayer(const char* media_type, |
| 62 | const char* media_name, entry_ref* ref) |
| 63 | : |
58 | 64 | fInitCheck(B_ERROR), |
59 | 65 | fRef(*ref), |
60 | 66 | fSoundPlayer(NULL), |
… |
… |
MediaFilePlayer::MediaFilePlayer(const char *media_type,
|
63 | 69 | fName = strdup(media_name); |
64 | 70 | |
65 | 71 | fPlayFile = new BMediaFile(&fRef); |
66 | | if ((fInitCheck = fPlayFile->InitCheck()) <B_OK) { |
| 72 | fInitCheck = fPlayFile->InitCheck(); |
| 73 | if (fInitCheck != B_OK) |
67 | 74 | return; |
68 | | } |
69 | 75 | |
70 | 76 | memset(&fPlayFormat, 0, sizeof(fPlayFormat)); |
71 | 77 | |
72 | 78 | for (int i=0; i < fPlayFile->CountTracks(); i++) { |
73 | | BMediaTrack *track = fPlayFile->TrackAt(i); |
| 79 | BMediaTrack* track = fPlayFile->TrackAt(i); |
74 | 80 | if (track == NULL) |
75 | 81 | continue; |
76 | 82 | fPlayFormat.type = B_MEDIA_RAW_AUDIO; |
… |
… |
MediaFilePlayer::MediaFilePlayer(const char *media_type,
|
88 | 94 | return; |
89 | 95 | } |
90 | 96 | |
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) |
94 | 102 | return; |
95 | | } |
96 | 103 | |
97 | 104 | fSoundPlayer->SetVolume(1.0f); |
98 | 105 | fSoundPlayer->SetHasData(true); |
… |
… |
MediaFilePlayer::InitCheck()
|
115 | 122 | } |
116 | 123 | |
117 | 124 | |
| 125 | const char* |
| 126 | MediaFilePlayer::Name() |
| 127 | { |
| 128 | return fName; |
| 129 | } |
| 130 | |
| 131 | |
| 132 | const entry_ref* |
| 133 | MediaFilePlayer::Ref() |
| 134 | { |
| 135 | return &fRef; |
| 136 | } |
| 137 | |
| 138 | |
118 | 139 | bool |
119 | 140 | MediaFilePlayer::IsPlaying() |
120 | 141 | { |
121 | | return (fSoundPlayer && fSoundPlayer->HasData()); |
| 142 | return (fSoundPlayer != NULL && fSoundPlayer->HasData()); |
122 | 143 | } |
123 | 144 | |
124 | 145 | void |
… |
… |
MediaFilePlayer::Stop()
|
140 | 161 | |
141 | 162 | |
142 | 163 | void |
143 | | MediaFilePlayer::PlayFunction(void *cookie, void * buffer, size_t size, const media_raw_audio_format & format) |
| 164 | MediaFilePlayer::PlayFunction(void* cookie, void* buffer, |
| 165 | size_t size, const media_raw_audio_format& format) |
144 | 166 | { |
145 | | MediaFilePlayer *player = (MediaFilePlayer *)cookie; |
| 167 | MediaFilePlayer* player = (MediaFilePlayer*)cookie; |
146 | 168 | int64 frames = 0; |
147 | 169 | player->fPlayTrack->ReadFrames(buffer, &frames); |
148 | 170 | |
149 | | if (frames <=0) { |
| 171 | if (frames <= 0) |
150 | 172 | player->fSoundPlayer->SetHasData(false); |
151 | | } |
152 | 173 | } |
diff --git a/src/servers/media_addon/MediaFilePlayer.h b/src/servers/media_addon/MediaFilePlayer.h
index 4c85458..508be95 100644
a
|
b
|
|
5 | 5 | #ifndef _MEDIA_FILE_PLAYER_H |
6 | 6 | #define _MEDIA_FILE_PLAYER_H |
7 | 7 | |
| 8 | |
8 | 9 | #include <Entry.h> |
9 | | #include <SoundPlayer.h> |
| 10 | #include <MediaDefs.h> |
10 | 11 | #include <MediaFile.h> |
11 | 12 | #include <MediaTrack.h> |
12 | | #include <MediaDefs.h> |
| 13 | #include <SoundPlayer.h> |
13 | 14 | |
14 | 15 | |
15 | | void PlayMediaFile(const char *media_type, const char *media_name); |
| 16 | void PlayMediaFile(const char* media_type, const char* media_name); |
16 | 17 | |
17 | 18 | |
18 | 19 | class MediaFilePlayer |
19 | 20 | { |
20 | 21 | public: |
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); |
34 | 39 | |
35 | 40 | private: |
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; |
43 | 48 | }; |
44 | 49 | |
45 | 50 | #endif // _MEDIA_FILE_PLAYER_H |