improve delays created by vicon drawing locks, reset_cache segv fix, gang track toolt...
[goodguy/cinelerra.git] / cinelerra-5.1 / cinelerra / filethread.h
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2009 Adam Williams <broadcast at earthling dot net>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  */
21
22 #ifndef FILETHREAD_H
23 #define FILETHREAD_H
24
25 #include "condition.inc"
26 #include "file.inc"
27 #include "mutex.inc"
28 #include "samples.inc"
29 #include "thread.h"
30 #include "vframe.inc"
31
32
33 // This allows the file hander to write in the background without
34 // blocking the write commands.
35 // Used for recording.
36
37
38 // Container for read frames
39 class FileThreadFrame
40 {
41 public:
42         FileThreadFrame();
43         ~FileThreadFrame();
44
45 // Frame position in native framerate
46         int64_t position;
47         int layer;
48         VFrame *frame;
49         int valid;
50 };
51
52 class FileThread : public Thread
53 {
54 public:
55         FileThread(File *file, int do_audio, int do_video);
56         ~FileThread();
57
58         void create_objects(File *file,
59                 int do_audio,
60                 int do_video);
61         void delete_objects();
62         void reset();
63
64
65 // ============================== writing section ==============================
66         int start_writing();
67 // Allocate the buffers and start loop for writing.
68 // compressed - if 1 write_compressed_frames is called in the file
69 //            - if 0 write_frames is called
70         int start_writing(long buffer_size,
71                         int color_model,
72                         int ring_buffers,
73                         int compressed);
74         int stop_writing();
75
76
77
78
79 // ================================ reading section ============================
80 // Allocate buffers and start loop for reading
81         int start_reading();
82         int stop_reading();
83
84         int read_frame(VFrame *frame);
85 // Set native framerate.
86 // Called by File::set_video_position.
87         int set_video_position(int64_t position);
88         int set_layer(int layer);
89         int read_buffer();
90         int64_t get_memory_usage();
91
92 // write data into next available buffer
93         int write_buffer(long size);
94 // get pointer to next buffer to be written and lock it
95         Samples** get_audio_buffer();
96 // get pointer to next frame to be written and lock it
97         VFrame*** get_video_buffer();
98 // get pointer to last video buffer returned without advancing it
99         VFrame*** get_last_video_buffer();
100
101         void run();
102         void swap_buffer();
103
104 // [ring_buffer][channels][Samples*]
105         Samples ***audio_buffer;
106 // [ring buffer](Track *)(VFrame array *)(VFrame*)
107         VFrame ****video_buffer;
108         long *output_size;  // Number of frames or samples to write
109 // Not used
110         int *is_compressed; // Whether to use the compressed data in the frame
111         Condition **output_lock, **input_lock;
112 // Lock access to the file to allow it to be changed without stopping the loop
113         Mutex *file_lock;
114         int current_buffer;
115         int local_buffer;
116         int *last_buffer;  // last_buffer[ring buffer]
117         int return_value;
118         int do_audio;
119         int do_video;
120         File *file;
121         int ring_buffers;
122         int buffer_size;    // Frames or samples per ring buffer
123 // Color model of frames
124         int color_model;
125 // Whether to use the compressed data in the frame
126         int compressed;
127
128 // Mode of operation
129         int is_reading;
130         int is_writing;
131         int done;
132
133 // For the reading mode, the thread reads continuously from the given
134 // point until stopped.
135 // Maximum frames to preload
136 #define MAX_READ_FRAMES 4
137 // Total number of frames preloaded
138         int total_frames;
139 // Allocated frames
140         FileThreadFrame *read_frames[MAX_READ_FRAMES];
141 // If the seeking pattern isn't optimal for asynchronous reading, this is
142 // set to 1 to stop reading.
143         int disable_read;
144 // Thread waits on this if the maximum frames have been read.
145         Condition *read_wait_lock;
146 // read_frame waits on this if the thread is running.
147         Condition *user_wait_lock;
148 // Lock access to read_frames
149         Mutex *frame_lock;
150 // Position of first frame in read_frames.
151 // Set by set_video_position and read_frame only.
152 // Position is in native framerate.
153         int64_t start_position;
154 // Position to read next frame from
155         int64_t read_position;
156 // Last layer a frame was read from
157         int layer;
158 };
159
160
161
162 #endif