xref: /haiku/src/add-ons/media/media-add-ons/usb_webcam/Producer.h (revision 17889a8c70dbb3d59c1412f6431968753c767bab)
1 /*
2  * Copyright 2004-2008, François Revol, <revol@free.fr>.
3  * Distributed under the terms of the MIT License.
4  */
5 #ifndef _VIDEO_PRODUCER_H
6 #define _VIDEO_PRODUCER_H
7 
8 #include <kernel/OS.h>
9 #include <media/BufferProducer.h>
10 #include <media/Controllable.h>
11 #include <media/MediaDefs.h>
12 #include <media/MediaEventLooper.h>
13 #include <media/MediaNode.h>
14 #include <support/Locker.h>
15 #include <support/String.h>
16 
17 class CamDevice;
18 class BParameter;
19 class BTextParameter;
20 
21 class VideoProducer :
22 	public virtual BMediaEventLooper,
23 	public virtual BBufferProducer,
24 	public virtual BControllable {
25 public:
26 						VideoProducer(BMediaAddOn *addon, CamDevice *dev,
27 								const char *name, int32 internal_id);
28 virtual					~VideoProducer();
29 
30 virtual	status_t		InitCheck() const { return fInitStatus; }
31 
32 
33 /* BMediaNode */
34 public:
35 virtual port_id		ControlPort() const;
36 virtual	BMediaAddOn	*AddOn(int32 * internal_id) const;
37 virtual	status_t 	HandleMessage(int32 message, const void *data,
38 							size_t size);
39 protected:
40 virtual	void 		Preroll();
41 virtual void		SetTimeSource(BTimeSource * time_source);
42 virtual status_t	RequestCompleted(const media_request_info & info);
43 
44 /* BMediaEventLooper */
45 protected:
46 virtual	void 		NodeRegistered();
47 virtual void		Start(bigtime_t performance_time);
48 virtual void		Stop(bigtime_t performance_time, bool immediate);
49 virtual void		Seek(bigtime_t media_time, bigtime_t performance_time);
50 virtual void		TimeWarp(bigtime_t at_real_time,
51 							bigtime_t to_performance_time);
52 virtual status_t	AddTimer(bigtime_t at_performance_time, int32 cookie);
53 virtual void		SetRunMode(run_mode mode);
54 virtual void		HandleEvent(const media_timed_event *event,
55 							bigtime_t lateness, bool realTimeEvent = false);
56 virtual void		CleanUpEvent(const media_timed_event *event);
57 virtual bigtime_t	OfflineTime();
58 virtual void		ControlLoop();
59 virtual status_t	DeleteHook(BMediaNode * node);
60 
61 /* BBufferProducer */
62 protected:
63 virtual	status_t	FormatSuggestionRequested(media_type type, int32 quality,
64 							media_format * format);
65 virtual	status_t 	FormatProposal(const media_source &output,
66 							media_format *format);
67 virtual	status_t	FormatChangeRequested(const media_source &source,
68 							const media_destination &destination,
69 							media_format *io_format, int32 *_deprecated_);
70 virtual	status_t 	GetNextOutput(int32 * cookie, media_output * out_output);
71 virtual	status_t	DisposeOutputCookie(int32 cookie);
72 virtual	status_t	SetBufferGroup(const media_source &for_source,
73 							BBufferGroup * group);
74 virtual	status_t 	VideoClippingChanged(const media_source &for_source,
75 							int16 num_shorts, int16 *clip_data,
76 							const media_video_display_info &display,
77 							int32 * _deprecated_);
78 virtual	status_t	GetLatency(bigtime_t * out_latency);
79 virtual	status_t	PrepareToConnect(const media_source &what,
80 							const media_destination &where,
81 							media_format *format,
82 							media_source *out_source, char *out_name);
83 virtual	void		Connect(status_t error, const media_source &source,
84 							const media_destination &destination,
85 							const media_format & format, char *io_name);
86 virtual	void 		Disconnect(const media_source & what,
87 							const media_destination & where);
88 virtual	void 		LateNoticeReceived(const media_source & what,
89 							bigtime_t how_much, bigtime_t performance_time);
90 virtual	void 		EnableOutput(const media_source & what, bool enabled,
91 							int32 * _deprecated_);
92 virtual	status_t	SetPlayRate(int32 numer,int32 denom);
93 virtual	void 		AdditionalBufferRequested(const media_source & source,
94 							media_buffer_id prev_buffer, bigtime_t prev_time,
95 							const media_seek_tag * prev_tag);
96 virtual	void		LatencyChanged(const media_source & source,
97 							const media_destination & destination,
98 							bigtime_t new_latency, uint32 flags);
99 
100 /* BControllable */
101 protected:
102 virtual status_t	GetParameterValue(int32 id, bigtime_t *last_change,
103 							void *value, size_t *size);
104 virtual void		SetParameterValue(int32 id, bigtime_t when,
105 							const void *value, size_t size);
106 virtual status_t	StartControlPanel(BMessenger *out_messenger);
107 
108 /* state */
109 private:
110 		void				HandleStart(bigtime_t performance_time);
111 		void				HandleStop();
112 		void				HandleTimeWarp(bigtime_t performance_time);
113 		void				HandleSeek(bigtime_t performance_time);
114 
115 		void				_UpdateStats();
116 
117 static	int32				fInstances;
118 
119 		status_t			fInitStatus;
120 
121 		int32				fInternalID;
122 		BMediaAddOn			*fAddOn;
123 		CamDevice			*fCamDevice;
124 
125 		BLocker				fLock;
126 			BBufferGroup	*fBufferGroup;
127 
128 		thread_id			fThread;
129 		sem_id				fFrameSync;
130 static	int32				_frame_generator_(void *data);
131 		int32				FrameGenerator();
132 
133 		/* The remaining variables should be declared volatile, but they
134 		 * are not here to improve the legibility of the sample code. */
135 		uint32				fFrame;
136 		uint32				fFrameBase;
137 		bigtime_t			fPerformanceTimeBase;
138 		bigtime_t			fProcessingLatency;
139 		media_output		fOutput;
140 		media_raw_video_format	fConnectedFormat;
141 		bool				fRunning;
142 		bool				fConnected;
143 		bool				fEnabled;
144 
145 		enum {
146 			 P_COLOR,
147 			 P_INFO,
148 			 P_LAST			// first available for addons
149 		};
150 		uint32				fColor;
151 		BString				fInfoString;
152 		bigtime_t			fLastColorChange;	// TODO: rename
153 
154 		struct {
155 			uint32 frames;
156 			uint32 actual;
157 			uint32 missed;
158 			bigtime_t stamp;
159 		}					fStats[2];
160 };
161 
162 #endif
163