1/*
2 * Copyright 1991-1999, Be Incorporated.
3 * Copyright (c) 1999-2000, Eric Moon.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions, and the following disclaimer.
12 *
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions, and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 *
17 * 3. The name of the author may not be used to endorse or promote products
18 *    derived from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
24 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
28 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32
33// LoggingConsumer.h
34
35#ifndef LoggingConsumer_H
36#define LoggingConsumer_H 1
37
38#include <media/BufferConsumer.h>
39#include <media/Controllable.h>
40#include <media/MediaEventLooper.h>
41#include "LogWriter.h"
42
43// forward declarations
44struct entry_ref;
45class BTimeSource;
46class BMediaAddOn;
47class BParameterWeb;
48class LogWriter;
49struct input_record;
50
51// --------------------
52// LoggingConsumer node class
53
54class LoggingConsumer :
55	public BBufferConsumer,
56	public BControllable,
57	public BMediaEventLooper
58{
59public:
60	// add-on-friendly ctor
61	// e.moon [11jun99]
62	LoggingConsumer(
63		const entry_ref& logFile, 	// points to an *existing* file
64		BMediaAddOn* pAddOn=0);
65	~LoggingConsumer();
66
67// Our own logging-control methods
68	void SetEnabled(log_what what, bool enable);
69	void EnableAllMessages();
70	void DisableAllMessages();
71
72// A little bit of instrumentation
73	long LateBuffers() const { return mLateBuffers; }
74	void ResetLateBufferCount() { mLateBuffers = 0; }
75
76// Methods from BMediaNode
77	BMediaAddOn* AddOn(int32*) const;
78	void SetRunMode(run_mode);
79	void Preroll();
80	void SetTimeSource(BTimeSource* time_source);
81	status_t RequestCompleted(const media_request_info & info);
82
83	// e.moon [11jun99; testing add-on]	+++++no longer needed
84	status_t DeleteHook(BMediaNode* pNode);
85
86// Methods from BControllable
87	status_t GetParameterValue(
88		int32 id,
89		bigtime_t* last_change,
90		void* value,
91		size_t* ioSize);
92
93	void SetParameterValue(
94		int32 id,
95		bigtime_t when,
96		const void* value,
97		size_t size);
98
99// Methods from BBufferConsumer
100	status_t HandleMessage(
101		int32 message,
102		const void* data,
103		size_t size );
104
105// all of these are pure virtual in BBufferConsumer
106	status_t AcceptFormat(
107		const media_destination& dest,
108		media_format* format);
109
110	status_t GetNextInput(
111		int32* cookie,
112		media_input* out_input);
113
114	void DisposeInputCookie( int32 cookie );
115
116	void BufferReceived( BBuffer* buffer );
117
118	void ProducerDataStatus(
119		const media_destination& for_whom,
120		int32 status,
121		bigtime_t at_performance_time);
122
123	status_t GetLatencyFor(
124		const media_destination& for_whom,
125		bigtime_t* out_latency,
126		media_node_id* out_timesource);
127
128	status_t Connected(
129		const media_source& producer,	/* here's a good place to request buffer group usage */
130		const media_destination& where,
131		const media_format& with_format,
132		media_input* out_input);
133
134	void Disconnected(
135		const media_source& producer,
136		const media_destination& where);
137
138	/* The notification comes from the upstream producer, so he's already cool with */
139	/* the format; you should not ask him about it in here. */
140	status_t FormatChanged(
141		const media_source& producer,
142		const media_destination& consumer,
143		int32 change_tag,
144		const media_format& format);
145
146	/* Given a performance time of some previous buffer, retrieve the remembered tag */
147	/* of the closest (previous or exact) performance time. Set *out_flags to 0; the */
148	/* idea being that flags can be added later, and the understood flags returned in */
149	/* *out_flags. */
150	status_t SeekTagRequested(
151		const media_destination& destination,
152		bigtime_t in_target_time,
153		uint32 in_flags,
154		media_seek_tag* out_seek_tag,
155		bigtime_t* out_tagged_time,
156		uint32* out_flags);
157
158// Methods from BMediaEventLooper
159
160	void NodeRegistered();
161	void Start( bigtime_t performance_time );
162	void Stop( bigtime_t performance_time, bool immediate );
163	void Seek( bigtime_t media_time, bigtime_t performance_time );
164	void TimeWarp( bigtime_t at_real_time, bigtime_t to_performance_time );
165
166	// The primary event processing method
167	void HandleEvent(const media_timed_event *event, bigtime_t lateness, bool realTimeEvent = false);
168
169// Private stuff -- various data we need for the logging implementation and parameter handling
170private:
171	entry_ref mLogRef;								// file that we're logging to
172	media_input mInput;								// descriptor of our single input
173	BParameterWeb* mWeb;						// description of our controllable parameters
174	LogWriter* mLogger;								// the actual logging object that we use
175	bigtime_t mSchedulingLatency;			// our scheduling latency (estimated at run time)
176	long mLateBuffers;									// track how many late buffers we've gotten
177
178	// controllable parameters and their change history
179	bigtime_t mLatency;								// our internal latency
180	float mSpinPercentage;							// how much of our latency time to spin the CPU
181	int32 mPriority;										// our control thread's priority
182	bigtime_t mLastLatencyChange;			// when did we last change our latency?
183	bigtime_t mLastSpinChange;					// when did we last change our CPU usage?
184	bigtime_t mLastPrioChange;					// when did we last change thread priority?
185
186	// host addon
187	// [11jun99] e.moon
188	BMediaAddOn*		m_pAddOn;
189};
190
191#endif
192