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