aboutsummaryrefslogtreecommitdiff
path: root/src/ReadMedia.h
blob: 6499f7344e428820231a563f6a8d282d2f445b89 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*
 * Copyright (C) 2003-2010 August Black
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

#ifndef _READMEDIA_H_
#define _READMEDIA_H_

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include "FifoAudioFrames.h"
#include "FifoVideoFrames.h"


#ifndef _AVDEC_H_
#define _AVDEC_H_
extern "C" {
#include <avdec.h>
}
#endif


#define STATE_EMPTY              0
#define STATE_OPENING           1
#define STATE_READY            2

#define SRC_MAX                 256.0
#define SRC_MIN                 1/256.0

#define CMD_START	3
#define CMD_NULL	0
#define CMD_OPEN 1
#define CMD_QUIT 2

#define SEEK_NOTHING -1
#define SEEK_REWIND -2

class ReadMedia  {

	public:
		ReadMedia();
		~ReadMedia();

		void openFile( char * filename, int vfifosize, int afifosize, int samples_per_frame);

		int decodeAudio( gavl_audio_frame_t *af);
		int decodeVideo( gavl_video_frame_t *vf);
	
		bool isReady(); // see if file is loaded or not
		bool getEOF();

		bool rewind();
		bool rewindNoFlush();
		bool frameSeek(int64_t frames);
		bool pcmSeek(int64_t samples);
		bool timeSeek(double seconds);

		int getAudioSamplerate();
		int getAudioChannelCount();
		int getVideoTimescale();

		double getLengthInSeconds(); 
		gavl_time_t getLengthInGavlTime(); 
		int64_t getLengthInAudioSamples();
		int64_t getLengthInVideoFrames();

		int getAudioStreamCount();
		int getVideoStreamCount();

		// this is used to sync AV frames
		void pealOffVideoFrames(int howmany);

		void dump() { lockState(); if (m_file != NULL) bgav_dump(m_file); unlockState(); }; 
	
		double getATimeInSeconds();
		double getVTimeInSeconds();
		//float getTimeInSeconds();
		float getAudioFifoSizePercentage();

		void copyAudioFormat(gavl_audio_format_t * dst ); 
		void copyVideoFormat(gavl_video_format_t * dst ); 
		void setLoop( bool b); 
		bool getLoop(); 


		//|||||||||||||||||||||||||||||||||||||||||||||||||||||||||
		// NON-PUBLIC public functions
		//|||||||||||||||||||||||||||||||||||||||||||||||||||||||||

		int64_t pcmSeek();
		int64_t frameSeek();
		//bool seek(gavl_time_t gt);

		void killAVThreads();
		bool startAVThreads();
		void setAudioStreamCount(int s);
		void setVideoStreamCount(int s);

		FifoAudioFrames * getAudioFifo(); 
		FifoVideoFrames * getVideoFifo(); 

		bgav_t * getFile();
		char * getFilename();

		gavl_audio_frame_t * getAudioFrame();
		gavl_video_frame_t * getVideoFrame();
		
		void closeFile();
		void clearFile();

		void setState(int b);
		int getState();
		gavl_audio_format_t * getAudioFormat() { return &m_audio_format;}; 
		gavl_video_format_t * getVideoFormat() { return &m_video_format;}; 

		void setCommand( int c);
		int getCommand();

		void setAEOF(bool b);
		void setVEOF(bool b);
		bool getAEOF();
		bool getVEOF();

		int getSamplesPerFrame();

		int lockAV();
		int unlockAV();
		//int lockAV(const char *s);
		//int unlockAV(const char *s);
		int lockState();
		int unlockState();
		//int lockState(const char *s);
		//int unlockState(const char *s);


		void waitA();
		void waitV();
		void signalAV();
		void signalA();
		void signalV();
		void waitDispatch();
		void signalDispatcher();
	
		bool initFormat();

		void setOpenCallback(void (*oc)(void *), void *v );
		void callOpenCallback();

		void setBufferCallback( bgav_buffer_callback bc, void *v );
		
		bool quitAVThreads();

	private:
		bool quit_av_threads;
		// callbacks for open, input buffer etc.	
		void * m_open_callback_data;	
		void (* m_open_callback)(void * v);
		bgav_buffer_callback m_buffer_callback;
		void * m_buffer_callback_data;			

		// end of file for audio and video
		bool m_aeof;
		bool m_veof;
	
		// seek vars
		int64_t m_pcm_seek;
		int64_t m_frame_seek;
		
		double m_length_in_seconds;
		gavl_time_t m_length_in_gavltime;
		int64_t m_num_samples;
		int64_t m_num_frames;

		// command for dispatcher thread
		int m_cmd;

		// internal state of media reader and 
		// current filename
		int m_state;
		char m_filename[1024];
		bool m_loop;

	
		// current time of audio in seconds
		//double m_atime;
		//double m_vtime;

		int m_afifosize;
		int m_vfifosize;
		int m_audio_stream_count;
		int m_video_stream_count;
		
		bgav_t * m_file;
		bgav_options_t * m_opt;

		// audio stuff
		gavl_audio_frame_t * m_audio_frame;
		gavl_audio_format_t m_audio_format;
	
		//video stuff 
		gavl_video_frame_t * m_video_frame;
		gavl_video_format_t m_video_format;

		FifoAudioFrames  *m_fifoaudio;
		FifoVideoFrames  *m_fifovideo;

		int m_audio_thread_ret;
		int m_video_thread_ret;
		int m_dispatcher_thread_ret;

		pthread_t m_thread_fillaudiofifo;
		pthread_t m_thread_fillvideofifo;
		pthread_t m_thread_dispatch;

		pthread_mutex_t m_condmut_a;
		pthread_mutex_t m_condmut_v;
		pthread_mutex_t m_condmut_dispatch;

		pthread_mutex_t m_state_mut;
		pthread_mutex_t m_av_mut;

		pthread_cond_t m_cond_a;
		pthread_cond_t m_cond_v;
		pthread_cond_t m_cond_dispatch;
		
};

#endif