-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathMediaPlaybackController.cpp
More file actions
365 lines (309 loc) · 9.03 KB
/
MediaPlaybackController.cpp
File metadata and controls
365 lines (309 loc) · 9.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
#include "MediaPlaybackController.h"
#include "Debug.h"
#include <Entry.h>
#include <Message.h>
#include <OS.h>
#include <Path.h>
#include <cstring>
#include <stdio.h>
MediaPlaybackController::MediaPlaybackController() {}
MediaPlaybackController::~MediaPlaybackController() { Stop(); }
/**
* @brief Sets the messenger for notifying the UI about playback events.
*
* @param target The messenger (usually the MainWindow).
*/
void MediaPlaybackController::SetTarget(BMessenger target) { fTarget = target; }
/**
* @brief Starts the BMessageRunner that sends periodic time updates to the UI.
*/
void MediaPlaybackController::_StartTimeUpdates() {
if (fUpdateRunner == nullptr && fTarget.IsValid()) {
fUpdateRunner =
new BMessageRunner(fTarget, new BMessage(MSG_TIME_UPDATE), 500000);
}
}
/**
* @brief Stops the periodic time updates.
*/
void MediaPlaybackController::_StopTimeUpdates() {
if (fUpdateRunner) {
delete fUpdateRunner;
fUpdateRunner = nullptr;
}
}
/**
* @brief Cleans up media resources (BSoundPlayer, BMediaTrack, BMediaFile).
*
* Ensures thread safety and proper resource deallocation order.
*/
void MediaPlaybackController::_CleanupMedia() {
if (fPlayer) {
// Wait briefly to allow the sound player thread to exit its callback
snooze(20000);
delete fPlayer;
fPlayer = nullptr;
}
if (fTrack) {
fMediaFile->ReleaseTrack(fTrack);
fTrack = nullptr;
}
if (fMediaFile) {
delete fMediaFile;
fMediaFile = nullptr;
}
}
/**
* @brief Sets the playback volume.
*
* @param percent Volume level between 0.0 and 1.0.
*/
void MediaPlaybackController::SetVolume(float percent) {
if (percent < 0.0f)
percent = 0.0f;
if (percent > 1.0f)
percent = 1.0f;
fVolume = percent;
if (fPlayer) {
fPlayer->SetVolume(fVolume);
}
}
/**
* @brief Plays the track at the specified index in the queue.
*
* Stops current playback, initializes BMediaFile and BMediaTrack,
* sets up audio format, and starts the BSoundPlayer.
*
* @param trackIndex Index of the track in fQueue to play.
*/
void MediaPlaybackController::Play(size_t trackIndex) {
DEBUG_PRINT("[Controller] Play(%zu) called\n", trackIndex);
Stop();
snooze(10000);
if (trackIndex >= fQueue.size()) {
DEBUG_PRINT("[Play2] index %zu out of range (queue size %zu)\n", trackIndex,
fQueue.size());
return;
}
fCurrentIdx = trackIndex;
const char *path = fQueue[trackIndex].c_str();
DEBUG_PRINT("[Play2] opening: %s\n", path);
entry_ref ref;
status_t st = get_ref_for_path(path, &ref);
if (st != B_OK) {
DEBUG_PRINT("[Play2] get_ref_for_path failed: %s (%ld)\n", strerror(st),
(long)st);
return;
}
fMediaFile = new BMediaFile(&ref);
st = fMediaFile->InitCheck();
if (st != B_OK) {
DEBUG_PRINT("[Play2] BMediaFile::InitCheck failed: %s (%ld)\n",
strerror(st), (long)st);
_CleanupMedia();
return;
}
fTrack = fMediaFile->TrackAt(0);
if (!fTrack) {
DEBUG_PRINT("[Play2] TrackAt(0) returned nullptr\n");
_CleanupMedia();
return;
}
fDuration = fTrack->Duration();
DEBUG_PRINT("[Play2] duration: %lld us (%.2f s)\n", (long long)fDuration,
fDuration / 1e6);
media_format mf{};
st = fTrack->DecodedFormat(&mf);
if (st != B_OK) {
DEBUG_PRINT("[Play2] DecodedFormat failed: %s (%ld)\n", strerror(st),
(long)st);
_CleanupMedia();
return;
}
const media_raw_audio_format &raf = mf.u.raw_audio;
DEBUG_PRINT("[Play2] decoded: rate=%.0f Hz, channels=%d, format=0x%lx, "
"byte_order=%s, buffer=%ld\n",
raf.frame_rate, (int)raf.channel_count, (unsigned long)raf.format,
raf.byte_order == B_MEDIA_BIG_ENDIAN ? "BE" : "LE",
(long)raf.buffer_size);
fPlayer = new BSoundPlayer(&raf, "Orchester", &_PlayBuffer, NULL, this);
if (!fPlayer) {
DEBUG_PRINT("[Play2] BSoundPlayer new failed\n");
_CleanupMedia();
return;
}
fPlayer->SetVolume(fVolume);
fPlayer->Start();
fPlayer->SetHasData(true);
if (fTarget.IsValid()) {
BMessage m(MSG_NOW_PLAYING);
m.AddInt32("index", (int32)trackIndex);
m.AddString("path", path);
fTarget.SendMessage(&m);
}
fAtEnd = false;
fPlaying = true;
fPaused = false;
fCurrentPos = 0;
_StartTimeUpdates();
DEBUG_PRINT("[Play2] started OK\n");
}
/**
* @brief Pauses playback.
*/
void MediaPlaybackController::Pause() {
if (fPlayer && fPlaying) {
fPlayer->Stop();
fPaused = true;
fPlaying = false;
}
}
/**
* @brief Resumes paused playback.
*/
void MediaPlaybackController::Resume() {
if (fPlayer && fPaused) {
fPlayer->Start();
fPlayer->SetHasData(true);
fPaused = false;
fPlaying = true;
}
}
/**
* @brief Stops playback completely and resets state.
*/
void MediaPlaybackController::Stop() {
DEBUG_PRINT("[Controller] Stop() called\n");
_StopTimeUpdates();
fAtEnd = true;
if (fPlayer) {
DEBUG_PRINT("[Controller] stopping BSoundPlayer...\n");
fPlayer->SetHasData(false);
fPlayer->Stop();
snooze(20000);
}
_CleanupMedia();
fPlaying = false;
fPaused = false;
fCurrentPos = 0;
fDuration = 0;
fCurrentIdx = 0;
DEBUG_PRINT("[Controller] Stop() finished\n");
}
/**
* @brief Plays the next track in the queue, if available.
*/
void MediaPlaybackController::PlayNext() {
if (!fQueue.empty()) {
if (fCurrentIdx + 1 < fQueue.size()) {
fCurrentIdx++;
Play(fCurrentIdx);
} else {
Stop();
}
}
}
/**
* @brief Plays the previous track in the queue, if available.
*/
void MediaPlaybackController::PlayPrev() {
if (!fQueue.empty()) {
if (fCurrentIdx > 0) {
fCurrentIdx--;
Play(fCurrentIdx);
} else {
Stop();
}
}
}
/**
* @brief Seeks to a specific position in the current track.
*
* @param pos Position in microseconds.
*/
void MediaPlaybackController::SeekTo(bigtime_t pos) {
if (!fTrack)
return;
bigtime_t newTime = pos;
status_t ret = fTrack->SeekToTime(&newTime, B_MEDIA_SEEK_CLOSEST_BACKWARD);
if (ret == B_OK) {
fCurrentPos = newTime;
}
}
bool MediaPlaybackController::IsPlaying() const { return fPlaying && !fPaused; }
/**
* @brief Shuts down the controller, stopping playback and cleaning up.
*/
void MediaPlaybackController::Shutdown() {
fShuttingDown = true;
fAtEnd = true;
_StopTimeUpdates();
if (fPlayer) {
fPlayer->SetHasData(false);
fPlayer->Stop();
}
_CleanupMedia();
fTarget = BMessenger();
fPlaying = false;
fPaused = false;
}
bool MediaPlaybackController::IsPaused() const { return fPaused; }
int32 MediaPlaybackController::CurrentIndex() const { return fCurrentIdx; }
void MediaPlaybackController::SetQueue(const std::vector<std::string> &queue) {
fQueue = queue;
fCurrentIdx = 0;
}
bigtime_t MediaPlaybackController::CurrentPosition() const {
return fCurrentPos;
}
bigtime_t MediaPlaybackController::Duration() const { return fDuration; }
/**
* @brief Static audio buffer callback for BSoundPlayer.
*
* Reads decoded frames from BMediaTrack and fills the audio buffer.
* Handles end-of-track detection and notification.
*/
void MediaPlaybackController::_PlayBuffer(
void *cookie, void *buffer, size_t size,
const media_raw_audio_format &format) {
auto *self = static_cast<MediaPlaybackController *>(cookie);
if (!self) {
memset(buffer, 0, size);
return;
}
self->fInCallback.store(true, std::memory_order_relaxed);
if (self->fShuttingDown.load(std::memory_order_relaxed) ||
self->fAtEnd.load(std::memory_order_relaxed) || self->fTrack == nullptr) {
memset(buffer, 0, size);
self->fInCallback.store(false, std::memory_order_relaxed);
return;
}
const int bytesPerSample = (format.format & 0xF);
const int frameSize = bytesPerSample * format.channel_count;
int64 frames = frameSize > 0 ? (int64)(size / frameSize) : 0;
status_t ret = B_ERROR;
if (self->fTrack && frames > 0)
ret = self->fTrack->ReadFrames(buffer, &frames);
if (ret == B_OK && frames > 0) {
self->fCurrentPos +=
(bigtime_t)((frames * 1000000LL) / (int)format.frame_rate);
size_t produced = (size_t)frames * frameSize;
if (produced < size)
memset((uint8 *)buffer + produced, 0, size - produced);
} else {
// End of stream or error
bool expected = false;
if (!self->fShuttingDown.load(std::memory_order_relaxed) &&
!self->fStopping.load(std::memory_order_relaxed) &&
self->fAtEnd.compare_exchange_strong(expected, true)) {
memset(buffer, 0, size);
if (self->fTarget.IsValid()) {
BMessage m(MSG_TRACK_ENDED);
self->fTarget.SendMessage(&m);
}
} else {
memset(buffer, 0, size);
}
}
self->fInCallback.store(false, std::memory_order_relaxed);
}