-
-
Notifications
You must be signed in to change notification settings - Fork 338
/
MediaEventLooper.cpp
597 lines (485 loc) · 16.8 KB
/
MediaEventLooper.cpp
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
/*
* Copyright (c) 2015 Dario Casalinuovo <b.vitruvio@gmail.com>
* Copyright (c) 2002, 2003 Marcus Overhagen <Marcus@Overhagen.de>
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files or portions
* thereof (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge,
* publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so, subject
* to the following conditions:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice
* in the binary, as well as this list of conditions and the following
* disclaimer in the documentation and/or other materials provided with
* the distribution.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/
#include <MediaEventLooper.h>
#include <TimeSource.h>
#include <scheduler.h>
#include <Buffer.h>
#include <ServerInterface.h>
#include "debug.h"
/*************************************************************
* protected BMediaEventLooper
*************************************************************/
/* virtual */
BMediaEventLooper::~BMediaEventLooper()
{
CALLED();
// don't call Quit(); here, except if the user was stupid
if (fControlThread != -1) {
printf("You MUST call BMediaEventLooper::Quit() in your destructor!\n");
Quit();
}
}
/* explicit */
BMediaEventLooper::BMediaEventLooper(uint32 apiVersion) :
BMediaNode("called by BMediaEventLooper"),
fControlThread(-1),
fCurrentPriority(B_URGENT_PRIORITY),
fSetPriority(B_URGENT_PRIORITY),
fRunState(B_UNREGISTERED),
fEventLatency(0),
fSchedulingLatency(0),
fBufferDuration(0),
fOfflineTime(0),
fApiVersion(apiVersion)
{
CALLED();
fEventQueue.SetCleanupHook(BMediaEventLooper::_CleanUpEntry, this);
fRealTimeQueue.SetCleanupHook(BMediaEventLooper::_CleanUpEntry, this);
}
/* virtual */ void
BMediaEventLooper::NodeRegistered()
{
CALLED();
// Calling Run() should be done by the derived class,
// at least that's how it is documented by the BeBook.
// It appears that BeOS R5 called it here. Calling Run()
// twice doesn't hurt, and some nodes need it to be called here.
Run();
}
/* virtual */ void
BMediaEventLooper::Start(bigtime_t performance_time)
{
CALLED();
// This hook function is called when a node is started
// by a call to the BMediaRoster. The specified
// performanceTime, the time at which the node
// should start running, may be in the future.
fEventQueue.AddEvent(media_timed_event(performance_time, BTimedEventQueue::B_START));
}
/* virtual */ void
BMediaEventLooper::Stop(bigtime_t performance_time,
bool immediate)
{
CALLED();
// This hook function is called when a node is stopped
// by a call to the BMediaRoster. The specified performanceTime,
// the time at which the node should stop, may be in the future.
// If immediate is true, your node should ignore the performanceTime
// value and synchronously stop performance. When Stop() returns,
// you're promising not to write into any BBuffers you may have
// received from your downstream consumers, and you promise not
// to send any more buffers until Start() is called again.
if (immediate) {
// always be sure to add to the front of the queue so we can make sure it is
// handled before any buffers are sent!
performance_time = 0;
}
fEventQueue.AddEvent(media_timed_event(performance_time, BTimedEventQueue::B_STOP));
}
/* virtual */ void
BMediaEventLooper::Seek(bigtime_t media_time,
bigtime_t performance_time)
{
CALLED();
// This hook function is called when a node is asked to seek to
// the specified mediaTime by a call to the BMediaRoster.
// The specified performanceTime, the time at which the node
// should begin the seek operation, may be in the future.
fEventQueue.AddEvent(media_timed_event(performance_time, BTimedEventQueue::B_SEEK, NULL,
BTimedEventQueue::B_NO_CLEANUP, 0, media_time, NULL));
}
/* virtual */ void
BMediaEventLooper::TimeWarp(bigtime_t at_real_time,
bigtime_t to_performance_time)
{
CALLED();
// This hook function is called when the time source to which the
// node is slaved is repositioned (via a seek operation) such that
// there will be a sudden jump in the performance time progression
// as seen by the node. The to_performance_time argument indicates
// the new performance time; the change should occur at the real
// time specified by the at_real_time argument.
// place in the realtime queue
fRealTimeQueue.AddEvent(media_timed_event(at_real_time, BTimedEventQueue::B_WARP,
NULL, BTimedEventQueue::B_NO_CLEANUP, 0, to_performance_time, NULL));
// BeBook: Your implementation of TimeWarp() should call through to BMediaNode::TimeWarp()
// BeBook: as well as all other inherited forms of TimeWarp()
// XXX should we do this here?
BMediaNode::TimeWarp(at_real_time, to_performance_time);
}
/* virtual */ status_t
BMediaEventLooper::AddTimer(bigtime_t at_performance_time,
int32 cookie)
{
CALLED();
media_timed_event event(at_performance_time,
BTimedEventQueue::B_TIMER, NULL,
BTimedEventQueue::B_EXPIRE_TIMER);
event.data = cookie;
return EventQueue()->AddEvent(event);
}
/* virtual */ void
BMediaEventLooper::SetRunMode(run_mode mode)
{
CALLED();
// The SetRunMode() hook function is called when someone requests that your node's run mode be changed.
// bump or reduce priority when switching from/to offline run mode
int32 priority;
priority = (mode == B_OFFLINE) ? min_c(B_NORMAL_PRIORITY, fSetPriority) : fSetPriority;
if (priority != fCurrentPriority) {
fCurrentPriority = priority;
if (fControlThread > 0) {
set_thread_priority(fControlThread, fCurrentPriority);
fSchedulingLatency = estimate_max_scheduling_latency(fControlThread);
printf("BMediaEventLooper: SchedulingLatency is %" B_PRId64 "\n",
fSchedulingLatency);
}
}
BMediaNode::SetRunMode(mode);
}
/* virtual */ void
BMediaEventLooper::CleanUpEvent(const media_timed_event *event)
{
CALLED();
// Implement this function to clean up after custom events you've created
// and added to your queue. It's called when a custom event is removed from
// the queue, to let you handle any special tidying-up that the event might require.
}
/* virtual */ bigtime_t
BMediaEventLooper::OfflineTime()
{
CALLED();
return fOfflineTime;
}
/* virtual */ void
BMediaEventLooper::ControlLoop()
{
CALLED();
status_t err = B_OK;
bigtime_t waitUntil = B_INFINITE_TIMEOUT;
bool hasRealtime = false;
bool hasEvent = false;
// While there are no events or it is not time for the earliest event,
// process messages using WaitForMessages. Whenever this funtion times out,
// we need to handle the next event
fSchedulingLatency = estimate_max_scheduling_latency(fControlThread);
while (RunState() != B_QUITTING) {
if (err == B_TIMED_OUT
|| err == B_WOULD_BLOCK) {
// NOTE: The reference for doing the lateness calculus this way can
// be found in the BeBook article "A BMediaEventLooper Example".
// The value which we are going to calculate, is referred there as
// 'lateness'.
media_timed_event event;
if (hasEvent)
err = fEventQueue.RemoveFirstEvent(&event);
else if (hasRealtime)
err = fRealTimeQueue.RemoveFirstEvent(&event);
if (err == B_OK) {
// The general idea of lateness is to allow
// the client code to detect when the buffer
// is handled late or early. What we add is
// that the code log the time at which the
// current event is added to the queue. This
// allow us to detect cyclic/stagnant latency
// in the meantime, so that the client can
// notify to the producer only the portion
// that might be attributable.
bigtime_t lateness = waitUntil - TimeSource()->RealTime();
if (lateness < 0) {
if (event.enqueue_time > waitUntil) {
lateness = event.enqueue_time
- TimeSource()->RealTime();
}
}
DispatchEvent(&event, -lateness, hasRealtime);
}
} else if (err != B_OK)
return;
// BMediaEventLooper compensates your performance time by adding
// the event latency (see SetEventLatency()) and the scheduling
// latency (or, for real-time events, only the scheduling latency).
hasRealtime = fRealTimeQueue.HasEvents();
hasEvent = fEventQueue.HasEvents();
if (hasEvent) {
waitUntil = TimeSource()->RealTimeFor(
fEventQueue.FirstEventTime(),
fEventLatency + fSchedulingLatency);
} else if (!hasRealtime)
waitUntil = B_INFINITE_TIMEOUT;
if (hasRealtime) {
bigtime_t realtimeWait = fRealTimeQueue.FirstEventTime()
- fSchedulingLatency;
if (!hasEvent || realtimeWait <= waitUntil) {
waitUntil = realtimeWait;
hasEvent = false;
} else
hasRealtime = false;
}
if (waitUntil != B_INFINITE_TIMEOUT
&& TimeSource()->RealTime() >= waitUntil) {
err = WaitForMessage(0);
} else
err = WaitForMessage(waitUntil);
}
}
thread_id
BMediaEventLooper::ControlThread()
{
CALLED();
return fControlThread;
}
/*************************************************************
* protected BMediaEventLooper
*************************************************************/
BTimedEventQueue *
BMediaEventLooper::EventQueue()
{
CALLED();
return &fEventQueue;
}
BTimedEventQueue *
BMediaEventLooper::RealTimeQueue()
{
CALLED();
return &fRealTimeQueue;
}
int32
BMediaEventLooper::Priority() const
{
CALLED();
return fCurrentPriority;
}
int32
BMediaEventLooper::RunState() const
{
PRINT(6, "CALLED BMediaEventLooper::RunState()\n");
return fRunState;
}
bigtime_t
BMediaEventLooper::EventLatency() const
{
CALLED();
return fEventLatency;
}
bigtime_t
BMediaEventLooper::BufferDuration() const
{
CALLED();
return fBufferDuration;
}
bigtime_t
BMediaEventLooper::SchedulingLatency() const
{
CALLED();
return fSchedulingLatency;
}
status_t
BMediaEventLooper::SetPriority(int32 priority)
{
CALLED();
// clamp to a valid value
if (priority < 5)
priority = 5;
if (priority > 120)
priority = 120;
fSetPriority = priority;
fCurrentPriority = (RunMode() == B_OFFLINE) ? min_c(B_NORMAL_PRIORITY, fSetPriority) : fSetPriority;
if (fControlThread > 0) {
set_thread_priority(fControlThread, fCurrentPriority);
fSchedulingLatency = estimate_max_scheduling_latency(fControlThread);
printf("BMediaEventLooper: SchedulingLatency is %" B_PRId64 "\n",
fSchedulingLatency);
}
return B_OK;
}
void
BMediaEventLooper::SetRunState(run_state state)
{
CALLED();
// don't allow run state changes while quitting,
// also needed for correct terminating of the ControlLoop()
if (fRunState == B_QUITTING && state != B_TERMINATED)
return;
fRunState = state;
}
void
BMediaEventLooper::SetEventLatency(bigtime_t latency)
{
CALLED();
// clamp to a valid value
if (latency < 0)
latency = 0;
fEventLatency = latency;
write_port_etc(ControlPort(), GENERAL_PURPOSE_WAKEUP, 0, 0, B_TIMEOUT, 0);
}
void
BMediaEventLooper::SetBufferDuration(bigtime_t duration)
{
CALLED();
if (duration < 0)
duration = 0;
fBufferDuration = duration;
}
void
BMediaEventLooper::SetOfflineTime(bigtime_t offTime)
{
CALLED();
fOfflineTime = offTime;
}
void
BMediaEventLooper::Run()
{
CALLED();
if (fControlThread != -1)
return; // thread already running
// until now, the run state is B_UNREGISTERED, but we need to start in B_STOPPED state.
SetRunState(B_STOPPED);
char threadName[32];
sprintf(threadName, "%.20s control", Name());
fControlThread = spawn_thread(_ControlThreadStart, threadName, fCurrentPriority, this);
resume_thread(fControlThread);
// get latency information
fSchedulingLatency = estimate_max_scheduling_latency(fControlThread);
}
void
BMediaEventLooper::Quit()
{
CALLED();
if (fRunState == B_TERMINATED)
return;
SetRunState(B_QUITTING);
close_port(ControlPort());
if (fControlThread != -1) {
status_t err;
wait_for_thread(fControlThread, &err);
fControlThread = -1;
}
SetRunState(B_TERMINATED);
}
void
BMediaEventLooper::DispatchEvent(const media_timed_event *event,
bigtime_t lateness,
bool realTimeEvent)
{
PRINT(6, "CALLED BMediaEventLooper::DispatchEvent()\n");
HandleEvent(event, lateness, realTimeEvent);
switch (event->type) {
case BTimedEventQueue::B_START:
SetRunState(B_STARTED);
break;
case BTimedEventQueue::B_STOP:
SetRunState(B_STOPPED);
break;
case BTimedEventQueue::B_SEEK:
/* nothing */
break;
case BTimedEventQueue::B_WARP:
/* nothing */
break;
case BTimedEventQueue::B_TIMER:
TimerExpired(event->event_time, event->data);
break;
default:
break;
}
_DispatchCleanUp(event);
}
/*************************************************************
* private BMediaEventLooper
*************************************************************/
/* static */ int32
BMediaEventLooper::_ControlThreadStart(void *arg)
{
CALLED();
((BMediaEventLooper *)arg)->SetRunState(B_STOPPED);
((BMediaEventLooper *)arg)->ControlLoop();
((BMediaEventLooper *)arg)->SetRunState(B_QUITTING);
return 0;
}
/* static */ void
BMediaEventLooper::_CleanUpEntry(const media_timed_event *event,
void *context)
{
PRINT(6, "CALLED BMediaEventLooper::_CleanUpEntry()\n");
((BMediaEventLooper *)context)->_DispatchCleanUp(event);
}
void
BMediaEventLooper::_DispatchCleanUp(const media_timed_event *event)
{
PRINT(6, "CALLED BMediaEventLooper::_DispatchCleanUp()\n");
// this function to clean up after custom events you've created
if (event->cleanup >= BTimedEventQueue::B_USER_CLEANUP)
CleanUpEvent(event);
}
/*
// unimplemented
BMediaEventLooper::BMediaEventLooper(const BMediaEventLooper &)
BMediaEventLooper &BMediaEventLooper::operator=(const BMediaEventLooper &)
*/
/*************************************************************
* protected BMediaEventLooper
*************************************************************/
status_t
BMediaEventLooper::DeleteHook(BMediaNode *node)
{
CALLED();
// this is the DeleteHook that gets called by the media server
// before the media node is deleted
Quit();
return BMediaNode::DeleteHook(node);
}
/*************************************************************
* private BMediaEventLooper
*************************************************************/
status_t BMediaEventLooper::_Reserved_BMediaEventLooper_0(int32 arg,...) { return B_ERROR; }
status_t BMediaEventLooper::_Reserved_BMediaEventLooper_1(int32 arg,...) { return B_ERROR; }
status_t BMediaEventLooper::_Reserved_BMediaEventLooper_2(int32 arg,...) { return B_ERROR; }
status_t BMediaEventLooper::_Reserved_BMediaEventLooper_3(int32 arg,...) { return B_ERROR; }
status_t BMediaEventLooper::_Reserved_BMediaEventLooper_4(int32 arg,...) { return B_ERROR; }
status_t BMediaEventLooper::_Reserved_BMediaEventLooper_5(int32 arg,...) { return B_ERROR; }
status_t BMediaEventLooper::_Reserved_BMediaEventLooper_6(int32 arg,...) { return B_ERROR; }
status_t BMediaEventLooper::_Reserved_BMediaEventLooper_7(int32 arg,...) { return B_ERROR; }
status_t BMediaEventLooper::_Reserved_BMediaEventLooper_8(int32 arg,...) { return B_ERROR; }
status_t BMediaEventLooper::_Reserved_BMediaEventLooper_9(int32 arg,...) { return B_ERROR; }
status_t BMediaEventLooper::_Reserved_BMediaEventLooper_10(int32 arg,...) { return B_ERROR; }
status_t BMediaEventLooper::_Reserved_BMediaEventLooper_11(int32 arg,...) { return B_ERROR; }
status_t BMediaEventLooper::_Reserved_BMediaEventLooper_12(int32 arg,...) { return B_ERROR; }
status_t BMediaEventLooper::_Reserved_BMediaEventLooper_13(int32 arg,...) { return B_ERROR; }
status_t BMediaEventLooper::_Reserved_BMediaEventLooper_14(int32 arg,...) { return B_ERROR; }
status_t BMediaEventLooper::_Reserved_BMediaEventLooper_15(int32 arg,...) { return B_ERROR; }
status_t BMediaEventLooper::_Reserved_BMediaEventLooper_16(int32 arg,...) { return B_ERROR; }
status_t BMediaEventLooper::_Reserved_BMediaEventLooper_17(int32 arg,...) { return B_ERROR; }
status_t BMediaEventLooper::_Reserved_BMediaEventLooper_18(int32 arg,...) { return B_ERROR; }
status_t BMediaEventLooper::_Reserved_BMediaEventLooper_19(int32 arg,...) { return B_ERROR; }
status_t BMediaEventLooper::_Reserved_BMediaEventLooper_20(int32 arg,...) { return B_ERROR; }
status_t BMediaEventLooper::_Reserved_BMediaEventLooper_21(int32 arg,...) { return B_ERROR; }
status_t BMediaEventLooper::_Reserved_BMediaEventLooper_22(int32 arg,...) { return B_ERROR; }
status_t BMediaEventLooper::_Reserved_BMediaEventLooper_23(int32 arg,...) { return B_ERROR; }