audio wave icons, viewer wave image, 7 lib updates, tweak file size
[goodguy/history.git] / cinelerra-5.1 / guicast / vicon.C
1 #include "vicon.h"
2
3 #include "bctimer.h"
4 #include "bcwindow.h"
5 #include "bccolors.h"
6 #include "keys.h"
7 #include "mutex.h"
8 #include "condition.h"
9
10 VIcon::
11 VIcon(int vw, int vh, double rate)
12 {
13         this->vw = vw;
14         this->vh = vh;
15         this->frame_rate = rate;
16
17         cycle_start = 0;
18         age = 0;
19         seq_no = 0;
20         in_use = 1;
21         hidden = 0;
22         audio_data = 0;
23         audio_size = 0;
24         playing_audio = 0;
25 }
26
27 VIcon::
28 ~VIcon()
29 {
30         clear_images();
31         delete [] audio_data;
32 }
33
34 void VIcon::
35 add_image(VFrame *frm, int ww, int hh, int vcmdl)
36 {
37         VIFrame *vifrm = new VIFrame(ww, hh, vcmdl);
38         VFrame *img = *vifrm;
39         img->transfer_from(frm);
40         images.append(vifrm);
41 }
42
43 void VIcon::
44 draw_vframe(BC_WindowBase *wdw, int x, int y)
45 {
46         wdw->draw_vframe(frame(), x,y, vw,vh);
47 }
48
49 VIcon *VIconThread::low_vicon()
50 {
51         if( !t_heap.size() ) return 0;
52         VIcon *vip = t_heap[0];
53         remove_vicon(0);
54         return vip;
55 }
56
57 void VIconThread::remove_vicon(int i)
58 {
59         int sz = t_heap.size();
60         for( int k; (k=2*(i+1)) < sz; i=k ) {
61                 if( t_heap[k]->age > t_heap[k-1]->age ) --k;
62                 t_heap[i] = t_heap[k];
63         }
64         VIcon *last = t_heap[--sz];
65         t_heap.remove_number(sz);
66         double age = last->age;
67         for( int k; i>0 && age<t_heap[k=(i-1)/2]->age; i=k )
68                 t_heap[i] = t_heap[k];
69         t_heap[i] = last;
70 }
71
72
73 VIconThread::
74 VIconThread(BC_WindowBase *wdw, int vw, int vh)
75  : Thread(1, 0, 0)
76 {
77         this->wdw = wdw;
78         this->view_win = 0;  this->vicon = 0;
79         this->view_w = vw;   this->view_h = vh;
80         this->viewing = 0;
81         draw_lock = new Condition(0, "VIconThread::draw_lock", 1);
82         timer = new Timer();
83         this->refresh_rate = VICON_RATE;
84         done = 0;
85         interrupted = -1;
86 }
87
88 VIconThread::
89 ~VIconThread()
90 {
91         stop_drawing();
92         done = 1;
93         draw_lock->unlock();
94         if( Thread::running() ) {
95                 Thread::cancel();
96         }
97         Thread::join();
98         t_heap.remove_all_objects();
99         delete timer;
100         delete draw_lock;
101 }
102
103 void VIconThread::
104 start_drawing()
105 {
106         wdw->lock_window("VIconThread::start_drawing");
107         if( view_win )
108                 wdw->set_active_subwindow(view_win);
109         if( interrupted < 0 )
110                 draw_lock->unlock();
111         interrupted = 0;
112         wdw->unlock_window();
113 }
114
115 void VIconThread::
116 stop_drawing()
117 {
118         wdw->lock_window("VIconThread::stop_drawing");
119         set_view_popup(0);
120         if( !interrupted )
121                 interrupted = 1;
122         wdw->unlock_window();
123 }
124
125 int VIconThread::keypress_event(int key)
126 {
127         if( key != ESC ) return 0;
128         set_view_popup(0);
129         return 1;
130 }
131
132 int ViewPopup::button_press_event()
133 {
134         vt->set_view_popup(0);
135         return 1;
136 }
137
138 bool VIconThread::
139 visible(VIcon *vicon, int x, int y)
140 {
141         if( vicon->hidden ) return false;
142         int y0 = 0;
143         int my = y + vicon->vh;
144         if( my <= y0 ) return false;
145         int y1 = y0 + wdw->get_h();
146         if( y >= y1 ) return false;
147         int x0 = 0;
148         int mx = x + vicon->vw;
149         if( mx <= x0 ) return false;
150         int x1 = x0 + wdw->get_w();
151         if( x >= x1 ) return false;
152         return true;
153 }
154
155 int ViewPopup::keypress_event()
156 {
157         int key = get_keypress();
158         return vt->keypress_event(key);
159 }
160 ViewPopup::ViewPopup(VIconThread *vt, VFrame *frame, int x, int y, int w, int h)
161  : BC_Popup(vt->wdw, x, y, w, h, BLACK)
162 {
163         this->vt = vt;
164 }
165
166 ViewPopup::~ViewPopup()
167 {
168         vt->wdw->set_active_subwindow(0);
169 }
170
171 void ViewPopup::draw_vframe(VFrame *frame)
172 {
173         BC_WindowBase::draw_vframe(frame, 0,0, get_w(),get_h());
174 }
175
176 ViewPopup *VIconThread::new_view_window(VFrame *frame)
177 {
178         int wx = viewing->get_vx() - view_w, rx = 0;
179         int wy = viewing->get_vy() - view_h, ry = 0;
180         wdw->get_root_coordinates(wx, wy, &rx, &ry);
181         ViewPopup *vwin = new ViewPopup(this, frame, rx, ry, view_w, view_h);
182         wdw->set_active_subwindow(vwin);
183         return vwin;
184 }
185
186 void VIconThread::
187 reset_images()
188 {
189         for( int i=t_heap.size(); --i>=0; ) t_heap[i]->reset();
190         timer->update();
191         img_dirty = win_dirty = 0;
192 }
193
194 void VIconThread::add_vicon(VIcon *vip)
195 {
196         double age = vip->age;
197         int i = t_heap.size();  t_heap.append(vip);
198         for( int k; i>0 && age<t_heap[(k=(i-1)/2)]->age; i=k )
199                 t_heap[i] = t_heap[k];
200         t_heap[i] = vip;
201 }
202
203 int VIconThread::del_vicon(VIcon *&vicon)
204 {
205         int i = t_heap.size();
206         while( --i >= 0 && t_heap[i] != vicon );
207         if( i < 0 ) return 0;
208         remove_vicon(i);
209         delete vicon;  vicon = 0;
210         return 1;
211 }
212
213 void VIconThread::set_view_popup(VIcon *vicon)
214 {
215         if( !vicon && this->vicon )
216                 this->vicon->stop_audio();
217         this->vicon = vicon;
218 }
219
220 int VIconThread::
221 update_view()
222 {
223         delete view_win;  view_win = 0;
224         if( (viewing=vicon) != 0 ) {
225                 VFrame *frame = viewing->frame();
226                 view_win = new_view_window(frame);
227                 view_win->show_window();
228                 vicon->start_audio();
229         }
230         wdw->set_active_subwindow(view_win);
231         return 1;
232 }
233
234
235 void VIconThread::
236 draw_images()
237 {
238         for( int i=0; i<t_heap.size(); ++i )
239                 draw(t_heap[i]);
240 }
241
242 void VIconThread::
243 flash()
244 {
245         if( !img_dirty && !win_dirty ) return;
246         if( img_dirty ) wdw->flash();
247         if( win_dirty && view_win ) view_win->flash();
248         win_dirty = img_dirty = 0;
249 }
250
251 int VIconThread::
252 draw(VIcon *vicon)
253 {
254         int x = vicon->get_vx(), y = vicon->get_vy();
255         int draw_img = visible(vicon, x, y);
256         int draw_win = view_win && viewing == vicon ? 1 : 0;
257         if( !draw_img && !draw_win ) return 0;
258         if( !vicon->frame() ) return 0;
259         if( draw_img ) {
260                 vicon->draw_vframe(wdw, x, y);
261                 img_dirty = 1;
262         }
263         if( draw_win ) {
264                 view_win->draw_vframe(vicon->frame());
265                 win_dirty = 1;
266         }
267         return 1;
268 }
269
270 void VIconThread::
271 run()
272 {
273         while(!done) {
274                 draw_lock->lock("VIconThread::run 0");
275                 if( done ) break;
276                 wdw->lock_window("BC_WindowBase::run 1");
277                 drawing_started();
278                 reset_images();
279                 int64_t seq_no = 0, now = 0;
280                 int64_t draw_flash = 1000 / refresh_rate;
281                 while( !interrupted ) {
282                         if( viewing != vicon )
283                                 update_view();
284                         VIcon *next = low_vicon();
285                         while( next && next->age < draw_flash ) {
286                                 now = timer->get_difference();
287                                 if( now >= draw_flash ) break;
288                                 draw(next);
289                                 if( !next->seq_no ) {
290                                         next->cycle_start = now;
291                                         if( next->playing_audio )
292                                                 next->start_audio();
293                                 }
294                                 int64_t ref_no = (now - next->cycle_start) / 1000. * refresh_rate;
295                                 int count = ref_no - next->seq_no;
296                                 if( count < 1 ) count = 1;
297                                 ref_no = next->seq_no + count;
298                                 next->age =  next->cycle_start + 1000. * ref_no / refresh_rate;
299                                 if( !next->set_seq_no(ref_no) )
300                                         next->age = now + 1000.;
301                                 add_vicon(next);
302                                 next = low_vicon();
303                         }
304                         if( !next ) break;
305                         add_vicon(next);
306                         if( draw_flash < now+1 )
307                                 draw_flash = now+1;
308                         wdw->unlock_window();
309                         while( !interrupted ) {
310                                 now = timer->get_difference();
311                                 int64_t ms = draw_flash - now;
312                                 if( ms <= 0 ) break;
313                                 if( ms > 100 ) ms = 100;
314                                 Timer::delay(ms);
315                         }
316                         wdw->lock_window("BC_WindowBase::run 2");
317                         now = timer->get_difference();
318                         int64_t late = now - draw_flash;
319                         if( late < 1000 ) flash();
320                         int64_t ref_no = now / 1000. * refresh_rate;
321                         int64_t count = ref_no - seq_no;
322                         if( count < 1 ) count = 1;
323                         seq_no += count;
324                         draw_flash = seq_no * 1000. / refresh_rate;
325                 }
326                 if( viewing != vicon )
327                         update_view();
328                 drawing_stopped();
329                 interrupted = -1;
330                 wdw->unlock_window();
331         }
332 }
333
334
335 void VIcon::init_audio(int audio_size)
336 {
337         this->audio_size = audio_size;
338         audio_data = new uint8_t[audio_size];
339         memset(audio_data, 0, audio_size);
340 }
341
342 void VIcon::dump(const char *dir)
343 {
344         mkdir(dir,0777);
345         for( int i=0; i<images.size(); ++i ) {
346                 char fn[1024];  sprintf(fn,"%s/img%05d.png",dir,i);
347                 printf("\r%s",fn);
348                 VFrame *img = *images[i];
349                 img->write_png(fn);
350         }
351         printf("\n");
352 }
353