xref: /haiku/headers/os/media/MediaEventLooper.h (revision 51978af14a173e7fae0563b562be5603bc652aeb)
1 /*******************************************************************************
2 /
3 /	File:			MediaEventLooper.h
4 /
5 /	Description:	BMediaEventLooper spawns a thread which calls WaitForMessage,
6 /					pushes BMediaNode messages onto its BTimedEventQueues.
7 /					informs you when it is time to handle an event.
8 /					Report your event latency, push other events onto the queue
9 /					and override HandleEvent to do your work.
10 /
11 /	Copyright 1999, Be Incorporated, All Rights Reserved
12 /
13 *******************************************************************************/
14 
15 #if !defined(_MEDIA_EVENT_LOOPER_H)
16 #define _MEDIA_EVENT_LOOPER_H
17 
18 #include <MediaNode.h>
19 #include <TimedEventQueue.h>
20 
21 class BMediaEventLooper :
22 	public virtual BMediaNode
23 {
24 	protected:
25 		enum run_state {
26 			B_IN_DISTRESS = -1,
27 			B_UNREGISTERED,
28 			B_STOPPED,
29 			B_STARTED,
30 			B_QUITTING,
31 			B_TERMINATED,
32 			B_USER_RUN_STATES = 0x4000
33 		};
34 
35 	protected:
36 		/* this has to be on top rather than bottom to force a vtable in mwcc */
37 		virtual				~BMediaEventLooper();
38 		explicit			BMediaEventLooper(uint32 apiVersion = B_BEOS_VERSION);
39 
40 	/* from BMediaNode */
41 	protected:
42 		virtual void		NodeRegistered();
43 		virtual void		Start(bigtime_t performance_time);
44 		virtual void		Stop(bigtime_t performance_time, bool immediate);
45 		virtual void		Seek(bigtime_t media_time, bigtime_t performance_time);
46 		virtual void		TimeWarp(bigtime_t at_real_time, bigtime_t to_performance_time);
47 		virtual status_t	AddTimer(bigtime_t at_performance_time, int32 cookie);
48 		virtual	void 		SetRunMode(run_mode mode);
49 
50 	/* BMediaEventLooper Hook functions */
51 	protected:
52 		/* you must override to handle your events! */
53 		/* you should not call HandleEvent directly */
54 		virtual void		HandleEvent(	const media_timed_event *event,
55 											bigtime_t lateness,
56 											bool realTimeEvent = false) = 0;
57 
58 		/* override to clean up custom events you have added to your queue */
59 		virtual void		CleanUpEvent(const media_timed_event *event);
60 
61 		/* called from Offline mode to determine the current time of the node */
62 		/* update your internal information whenever it changes */
63 		virtual	bigtime_t	OfflineTime();
64 
65 		/* override only if you know what you are doing! */
66 		/* otherwise much badness could occur */
67 		/* the actual control loop function: */
68 		/* 	waits for messages, Pops events off the queue and calls DispatchEvent */
69 		virtual void		ControlLoop();
70 
71 		thread_id			ControlThread();
72 
73 	protected:
74 		BTimedEventQueue * 	EventQueue();
75 		BTimedEventQueue *	RealTimeQueue();
76 
77 		int32				Priority() const;
78 		int32				RunState() const;
79 		bigtime_t			EventLatency() const;
80 		bigtime_t			BufferDuration() const;
81 		bigtime_t			SchedulingLatency() const;
82 
83 		/* use the priority constants from OS.h */
84 		/* or suggest_thread_priority from scheduler.h */
85 		/* will clamp priorities to be inbetween 5 and 120 */
86 		status_t			SetPriority(int32 priority);
87 
88 		/* set the run state */
89 		void				SetRunState(run_state state);
90 
91 		/* clamps to 0 if latency < 0 */
92 		void				SetEventLatency(bigtime_t latency);
93 
94 		/* clamps to 0 if duration is < 0 */
95 		void				SetBufferDuration(bigtime_t duration);
96 
97 		/* set the offline time returned in OfflineTime */
98 		void				SetOfflineTime(bigtime_t offTime);
99 
100 		/* spawn and resume the thread - must be called from NodeRegistered */
101 		void				Run();
102 
103 		/* close down the thread - must be called from your destructor */
104 		void				Quit();
105 
106 		/* calls HandleEvent and does BMediaEventLooper event work */
107 		void				DispatchEvent(	const media_timed_event *event,
108 											bigtime_t lateness,
109 											bool realTimeEvent = false);
110 
111 	private:
112 		static int32		_ControlThreadStart(void *arg);
113 		static void			_CleanUpEntry(const media_timed_event *event, void *context);
114 		void				_DispatchCleanUp(const media_timed_event *event);
115 
116 		BTimedEventQueue	fEventQueue;
117 		BTimedEventQueue	fRealTimeQueue;
118 		thread_id			fControlThread;
119 		int32				fCurrentPriority;
120 		int32				fSetPriority;
121 		volatile int32		fRunState;
122 		bigtime_t			fEventLatency;
123 		bigtime_t			fSchedulingLatency;
124 		bigtime_t			fBufferDuration;
125 		bigtime_t			fOfflineTime;
126 		uint32				fApiVersion;
127 
128 	private:
129 	/* unimplemented for your protection */
130 							BMediaEventLooper(const BMediaEventLooper&);
131 							BMediaEventLooper& operator=(const BMediaEventLooper&);
132 
133 	protected:
134 		virtual	status_t 	DeleteHook(BMediaNode * node);
135 
136 	/* fragile base class stuffing */
137 	private:
138 		/* it must be thanksgiving!! lots of stuffing! */
139 		virtual	status_t 	_Reserved_BMediaEventLooper_0(int32 arg, ...);
140 		virtual	status_t 	_Reserved_BMediaEventLooper_1(int32 arg, ...);
141 		virtual	status_t 	_Reserved_BMediaEventLooper_2(int32 arg, ...);
142 		virtual	status_t 	_Reserved_BMediaEventLooper_3(int32 arg, ...);
143 		virtual	status_t 	_Reserved_BMediaEventLooper_4(int32 arg, ...);
144 		virtual	status_t 	_Reserved_BMediaEventLooper_5(int32 arg, ...);
145 		virtual	status_t 	_Reserved_BMediaEventLooper_6(int32 arg, ...);
146 		virtual	status_t 	_Reserved_BMediaEventLooper_7(int32 arg, ...);
147 		virtual	status_t 	_Reserved_BMediaEventLooper_8(int32 arg, ...);
148 		virtual	status_t 	_Reserved_BMediaEventLooper_9(int32 arg, ...);
149 		virtual	status_t 	_Reserved_BMediaEventLooper_10(int32 arg, ...);
150 		virtual	status_t 	_Reserved_BMediaEventLooper_11(int32 arg, ...);
151 		virtual	status_t 	_Reserved_BMediaEventLooper_12(int32 arg, ...);
152 		virtual	status_t 	_Reserved_BMediaEventLooper_13(int32 arg, ...);
153 		virtual	status_t 	_Reserved_BMediaEventLooper_14(int32 arg, ...);
154 		virtual	status_t 	_Reserved_BMediaEventLooper_15(int32 arg, ...);
155 		virtual	status_t 	_Reserved_BMediaEventLooper_16(int32 arg, ...);
156 		virtual	status_t 	_Reserved_BMediaEventLooper_17(int32 arg, ...);
157 		virtual	status_t 	_Reserved_BMediaEventLooper_18(int32 arg, ...);
158 		virtual	status_t 	_Reserved_BMediaEventLooper_19(int32 arg, ...);
159 		virtual	status_t 	_Reserved_BMediaEventLooper_20(int32 arg, ...);
160 		virtual	status_t 	_Reserved_BMediaEventLooper_21(int32 arg, ...);
161 		virtual	status_t 	_Reserved_BMediaEventLooper_22(int32 arg, ...);
162 		virtual	status_t 	_Reserved_BMediaEventLooper_23(int32 arg, ...);
163 
164 		/* turkey for weeks! */
165 		bool				_reserved_bool_[4];
166 		uint32				_reserved_BMediaEventLooper_[12];
167 };
168 
169 #endif
170