1 /*
2 * ieee1394io.cc -- asynchronously grabbing DV data
3 * Copyright (C) 2000 Arne Schirmacher <arne@schirmacher.de>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software Foundation,
17 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
19 
20 #ifdef HAS_AVC_SUPPORT
21 
22 #ifndef _IEEE1394IO_H
23 #define _IEEE1394IO_H 1
24 
25 #include <libraw1394/raw1394.h>
26 #include <libraw1394/csr.h>
27 #include <libiec61883/iec61883.h>
28 
29 
30 #include <string>
31 using std::string;
32 #include <deque>
33 using std::deque;
34 
35 namespace avcap
36 {
37 
38 class Frame;
39 
40 class IEEE1394Reader
41 {
42 protected:
43 	/// the number of frames that had to be thrown away because
44 	/// our inFrames queue did not contain available frames
45 	int droppedFrames;
46 
47 	/// the number of frames that are tainted because they are incomplete -
48 	/// a packet was dropped
49 	int incompleteFrames;
50 
51 	/// a pointer to the frame which is currently been transmitted
52 	Frame	*currentFrame;
53 
54 	/// a list of empty frames
55 	deque < Frame* > inFrames;
56 
57 	/// a list of already received frames
58 	deque < Frame* > outFrames;
59 
60 public:
61 
62 	IEEE1394Reader( int channel = 63, int frames = 5 );
63 	virtual ~IEEE1394Reader();
64 
65 	// Mutex protected public methods
66 	virtual bool StartThread( void ) = 0;
67 	virtual void StopThread( void ) = 0;
68 	Frame* GetFrame( void );
69 
70 	void DoneWithFrame( Frame* );
71 	int GetDroppedFrames( void );
72 	int GetIncompleteFrames( void );
GetOutQueueSize(void)73 	int GetOutQueueSize( void )
74 	{
75 		return outFrames.size();
76 	}
GetInQueueSize(void)77 	int GetInQueueSize( void )
78 	{
79 		return inFrames.size();
80 	}
81 
82 	// These two public methods are not mutex protected
83 	virtual bool Open( void ) = 0;
84 	virtual void Close( void ) = 0;
85 
86 	bool WaitForAction( int seconds = 0 );
87 	virtual void TriggerAction( );
88 
89 	virtual bool StartReceive( void ) = 0;
90 	virtual void StopReceive( void ) = 0;
91 
92 protected:
93 	/// the iso channel we listen to (typically == 63)
94 	int	channel;
95 
96 	/// contains information about our thread after calling StartThread
97 	pthread_t thread;
98 
99 	/// this mutex protects capture related variables that could possibly
100 	/// accessed from two threads at the same time
101 	pthread_mutex_t mutex;
102 
103 	// This condition and mutex are used to indicate when new frames are
104 	// received
105 	pthread_mutex_t condition_mutex;
106 	pthread_cond_t condition;
107 
108 	/// A state variable for starting and stopping thread
109 	bool isRunning;
110 
111 	void Flush( void );
112 };
113 
114 
115 typedef void (*BusResetHandler)( void* );
116 typedef void* BusResetHandlerData;
117 
118 class CaptureHandler;
119 
120 class iec61883Reader: public IEEE1394Reader
121 {
122 private:
123 
124 	/// the interface card to use (typically == 0)
125 	int	m_port;
126 
127 	/// the handle to libraw1394
128 	raw1394handle_t m_handle;
129 
130 	/// the handle to libiec61883
131 	iec61883_dv_fb_t m_iec61883dv;
132 
133 	BusResetHandler m_resetHandler;
134 	const void* m_resetHandlerData;
135 
136 	CaptureHandler* m_captureHandler;
137 
138 public:
139 	iec61883Reader( int port = 0, int channel = 63, int buffers = 5,
140 		BusResetHandler = 0, BusResetHandlerData = 0 );
141 	~iec61883Reader();
142 
143 	bool Open( void );
144 	void Close( void );
145 	bool StartReceive( void );
146 	void StopReceive( void );
147 	bool StartThread( void );
148 	void StopThread( void );
149 	int Handler( int length, int complete, unsigned char *data );
150 	void *Thread();
151 	void ResetHandler( void );
152 
153 
154 private:
155 	static int ResetHandlerProxy( raw1394handle_t handle, unsigned int generation );
156 	static int HandlerProxy( unsigned char *data, int length, int complete,
157 		void *callback_data );
158 	static void* ThreadProxy( void *arg );
159 };
160 
161 
162 class iec61883Connection
163 {
164 private:
165 	raw1394handle_t m_handle;
166 	nodeid_t m_node;
167 	int m_channel;
168 	int m_bandwidth;
169 	int m_outputPort;
170 	int m_inputPort;
171 
172 public:
173 
174 	iec61883Connection( int port, int node );
175 	~iec61883Connection();
176 
177 	static void CheckConsistency( int port, int node );
GetChannel(void)178 	int GetChannel( void ) const
179 	{
180 		return m_channel;
181 	}
182 	int Reconnect( void );
183 };
184 
185 }
186 
187 #endif
188 #endif
189