aboutsummaryrefslogtreecommitdiff
path: root/Src/Plugins/Input/in_mp3/giofile.h
blob: 121d77c962d58825a1f9b0070e7e74e51caa0abd (plain) (blame)
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306

/***************************************************************************\
 *
 *               (C) copyright Fraunhofer - IIS (1998)
 *                        All Rights Reserved
 *
 *   filename: giofile.h
 *   project : MPEG Decoder
 *   author  : Martin Sieler
 *   date    : 1998-02-11
 *   contents/description: HEADER - file I/O class for MPEG Decoder
 *
 *
\***************************************************************************/

#ifndef _GIOFILE_H
#define _GIOFILE_H

/* ------------------------ includes --------------------------------------*/
#include <windows.h>

#include "CVbriHeader.h"
#include "jnetlib/jnetlib.h"

#include "../vlb/dataio.h"
#include "ID3v2.h"
#include "LAMEInfo.h"
#include "../nu/RingBuffer.h"
#include "../apev2/tag.h"
#include "ifc_mpeg_stream_reader.h"

/*-------------------------- defines --------------------------------------*/

/*-------------------------------------------------------------------------*/

class CGioFile : public DataIOControl, public ifc_mpeg_stream_reader
{
public:
	CGioFile();
	virtual ~CGioFile();

	int  Open(const wchar_t *pszName, int maxbufsizek);
	int  Close();
	int  Read(void *pBuffer, int cbToRead, int *pcbRead);
	int  Peek(void *pBuffer, int cbToRead, int *pcbRead);

	//dataiocontrol interface
	int IO(void *buf, int size, int count)
	{
		int l=0;
		Read(buf,count,&l);
		return l;
	}
	int Seek(long offset, int origin)
	{
		return 0;
	}
	//int Close() { return 0; }
	int EndOf(void)
	{
		return IsEof();
	}
	int DICGetLastError()
	{
		return DATA_IO_ERROR_NONE;
	}
	int DICGetDirection()
	{
		return DATA_IO_READ;
	}

	unsigned int GetAvgVBRBitrate(void)
	{
		if (m_vbr_ms && m_vbr_frames)
		{
			if (m_vbr_bytes && encodingMethod != ENCODING_METHOD_CBR)
				return (unsigned int)(m_vbr_bytes * 8 / m_vbr_ms);
			else
				return (unsigned int)(mpeg_length * 8 / m_vbr_ms);
		}
		return 0;
	}
	bool IsEof() const;
	bool lengthVerified;
	bool isSeekReset()
	{

		if (m_is_stream && m_seek_reset && m_is_stream_seek)
		{
			m_seek_reset = false;
			m_is_stream_seek = false;
			return true;
		}
		else
			return false;
	}

	bool IsStreamSeekable()
	{
		return m_is_stream_seekable;
	}

	int GetHeaderOffset();

	int PercentAvailable()
	{
		if (!m_is_stream) return 0;
		if (!recvbuffersize) return 0;
		if (!m_connection) return 0;
		if (constate != 5) return 0;
		uint64_t bytes_100 = jnl_connection_receive_bytes_available(m_connection)*100;
		bytes_100 /= recvbuffersize;
		return (int)bytes_100;
	}

	int RunStream()
	{
		if (m_is_stream && m_connection)
		{
			if (constate != 5) Read(NULL,0,NULL);
			else jnl_connection_run(m_connection, -1, -1, NULL, NULL);
			int p=jnl_connection_get_state(m_connection);
			if (p==JNL_CONNECTION_STATE_ERROR||
			    p==JNL_CONNECTION_STATE_CLOSED)
				return 2;
			return 1;
		}
		return 0;
	}
	int IsStream()
	{
		return m_is_stream;
	}
	void GetStreamInfo(wchar_t *, size_t len);

	DWORD GetContentLength(void)   const;
	DWORD GetCurrentPosition(void) const;
	void SetCurrentPosition(long dwPos, int How);

	enum { GIO_FILE_BEGIN, GIO_FILE_CURRENT, GIO_FILE_END };

	uint64_t mpeg_length; /* length of valid audio data */
	uint64_t mpeg_position; /* starting position of first valid decodable non-header MPEG frame */
	uint64_t file_position; /* position within the MPEG data that we've read so far */

	uint64_t m_vbr_bytes;
	int m_vbr_frames, m_vbr_ms;
	int encodingMethod;
	uint64_t m_vbr_samples;
	int prepad, postpad;
	void Seek(int posms, int br);
	bool IsSeekable();

	unsigned char id3v1_data[128];

	char stream_url[256];
	char stream_name[256];
	char stream_genre[256];
	char stream_current_title[256];

	char *m_content_type;
	int uvox_last_message;
	char *uvox_3901;
	char *uvox_3902;

	typedef struct {
		char *uvox_stream_artwork;
		int uvox_stream_artwork_len;
		int uvox_stream_artwork_type;
		char *uvox_playing_artwork;
		int uvox_playing_artwork_len;
		int uvox_playing_artwork_type;
	} UVOX_ARTWORK;
	UVOX_ARTWORK uvox_artwork;

	ID3v2 info;
	float GetGain();
	unsigned char m_vbr_toc[100];
	int m_vbr_frame_len;
	int m_vbr_flag;
	CVbriHeader *m_vbr_hdr;

	FILETIME last_write_time;

	void *GetID3v1()
	{
		if (m_id3v1_len == 128)
			return id3v1_data;
		else
			return 0;
	}

	void *GetID3v2(uint32_t *len)
	{
		if (stream_id3v2_buf)
		{
			*len = stream_id3v2_read;
			return stream_id3v2_buf;
		}
		else
			return 0;
	}

	void *GetLyrics3(uint32_t *len)
	{
		if (lyrics3_data)
		{
			*len = lyrics3_size;
			return lyrics3_data;
		}
		else
			return 0;
	}

	void *GetAPEv2(uint32_t *len)
	{
		if (apev2_data)
		{
			*len = m_apev2_len;
			return apev2_data;
		}
		return 0;
	}

protected:
	void ReadiTunesGaps();
private:
	/* ID3v2 */
	int m_id3v1_len;

	/* ID3v2 */
	uint32_t stream_id3v2_read;
	char *stream_id3v2_buf;
	int m_id3v2_len;

	/* Lyrics3 */
	uint32_t lyrics3_size;
	char *lyrics3_data;

	/* APEv2 */
	uint32_t m_apev2_len;
	char *apev2_data;
	APEv2::Tag apev2;

	int m_is_stream;
	jnl_dns_t m_dns;
	jnl_connection_t m_connection;
	char last_full_url[4096];
	int is_stream_seek;
	char *host;
	char *proxy_host;
	char *req;
	unsigned short port;
	char *lpinfo;
	char *proxy_lp;
	char *request;
	int constate;
	char save_filename[256];
	char server_name[128];
	int recvbuffersize;
	
	int64_t stream_bytes_read;
	int64_t stream_metabytes_read;
	unsigned int timeout_start;
	char force_lpinfo[256];
	unsigned char *m_full_buffer;
	int is_uvox;
	int uvox_stream_data_len;
	int uvox_message_cnt, uvox_desyncs;
	int uvox_sid,uvox_maxbr, uvox_avgbr,uvox_maxmsg;

	unsigned char *uvox_stream_data;

	char *uvox_meta[2][32];
	int meta_interval,meta_pos;
	uint64_t m_full_buffer_pos/*,m_full_buffer_len*/;
	char stream_title_save[580];
	char last_title_sent[256];

	RingBuffer peekBuffer;
	//unsigned char m_peekbuf[8192];
	//int m_peekbuf_used;
	bool no_peek_hack;

	int doConnect(const char *str, int start_offset);
	void processMetaData(char *data, int lent, int msgId = 0);

	int m_redircnt;
	int m_auth_tries;

	HANDLE hFile;
	bool   fEof;
	

	char dlg_realm[256];
	static INT_PTR CALLBACK httpDlgProc(HWND hwndDlg, UINT uMsg, WPARAM wParam,LPARAM lParam);
	int m_http_response;
	bool m_seek_reset;
	bool m_is_stream_seek;
	bool m_is_stream_seekable;
	bool m_useaproxy;
	RECVS_DISPATCH;
};

/*-------------------------------------------------------------------------*/
#endif