aboutsummaryrefslogtreecommitdiffstats
path: root/mediaplugin/src/mediaplugins/include/core/util.h
blob: 6b402774a12a003806e9d53abd4791c92415a0e9 (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
/* UltraStar Deluxe - Karaoke Game
 *
 * UltraStar Deluxe is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * 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; see the file COPYING. If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 *
 * $URL$
 * $Id$
 */
#ifndef _UTIL_H_
#define _UTIL_H_

#include <string>
#include "plugin_core.h"

class AudioFormatInfo {
private:
    double _sampleRate;
    uint8_t _channels;
    audioSampleFormat_t _format;
    int _frameSize;

    void updateFrameSize() {
    	_frameSize = g_audioSampleSize[_format] * _channels;
    }

public:
	double getSampleRate() const { return _sampleRate; }
	void setSampleRate(double sampleRate) { _sampleRate = sampleRate; }

	uint8_t getChannels() const { return _channels; }
	void setChannels(uint8_t channels) {
		_channels = channels;
		updateFrameSize();
	}

	audioSampleFormat_t getFormat() const { return _format; }
    void setFormat(audioSampleFormat_t sampleFormat) {
    	_format = sampleFormat;
    	updateFrameSize();
    }

	long getFrameSize() const { return _frameSize; }
	double getBytesPerSec() const { return _frameSize * _sampleRate; }

public:
	AudioFormatInfo() :
		_sampleRate(0),
		_channels(0),
		_format(AUDIO_SAMPLE_FORMAT_UNKNOWN),
		_frameSize(0)
	{}

	AudioFormatInfo(int channels, int sampleRate, audioSampleFormat_t sampleFormat) :
		_sampleRate(sampleRate), _channels(channels), _format(sampleFormat)
	{
		updateFrameSize();
	}

	AudioFormatInfo(audioFormatInfo_t *info) :
		_sampleRate(info->sampleRate),
		_channels(info->channels),
		_format(info->format)
	{
		updateFrameSize();
	}

    /**
     * Returns the inverse ratio of the size of data in this format to its
     * size in a given target format.
     * Example: SrcSize*SrcInfo.GetRatio(TgtInfo) = TgtSize
     */
    double getRatio(const AudioFormatInfo &targetInfo) const {
    	return (targetInfo.getFrameSize() / this->_frameSize) *
    	       (targetInfo.getSampleRate() / this->_sampleRate);
    }

    void toCStruct(audioFormatInfo_t *info) const {
    	info->channels = _channels;
    	info->format = _format;
    	info->sampleRate = _sampleRate;
    }

    //AudioFormatInfo copy();
};

class Path {
private:
	std::string _filename;
public:
	Path(const char *filename) :
		_filename(filename) {}

	Path(std::string filename) :
		_filename(filename) {}

	std::string toNative() const {
		const char* cstr = pluginCore->pathToNative(_filename.c_str());
		std::string result(cstr);
		pluginCore->memFree((void*)cstr);
		return result;
	}

	std::string toUTF8(bool useNativeDelim = true) const {
		const char* cstr = pluginCore->pathToUTF8(_filename.c_str(),
				useNativeDelim ? TRUE : FALSE);
		std::string result(cstr);
		pluginCore->memFree((void*)cstr);
		return result;
	}

// TODO: wchar_t sizes differ on Windows (2 byte)/Linux (4 byte).
// USDX uses the Pascal WideChar type which is 2 bytes in size.
#ifdef _WIN32
	std::wstring toWide(bool useNativeDelim = true) const {
		const wchar_t* cstr = pluginCore->pathToWide(_filename.c_str(),
				useNativeDelim ? TRUE : FALSE);
		std::wstring result(cstr);
		pluginCore->memFree((void*)cstr);
		return result;
	}
#endif

	bool isFile() const {
		return pluginCore->pathIsFile(_filename.c_str());
	}
};

extern "C" {
PLUGIN_CALL int threadMainRoutine(void *data);
}

class Thread {
private:
	thread_t *_thread;
public:
	Thread() : _thread(0) {}
	virtual ~Thread() {}

	virtual int run() = 0;

	void start() {
		_thread = pluginCore->threadCreate(threadMainRoutine, this);
	}

	/**
	 * Get the 32-bit thread identifier for the current thread.
	 */
	static uint32_t getCurrentThreadID() {
		return pluginCore->threadCurrentID();
	}

	/**
	 * Get the 32-bit thread identifier for the specified thread,
	 * equivalent to SDL_ThreadID() if the specified thread is NULL.
	 */
	uint32_t getThreadID() {
		return pluginCore->threadGetID(_thread);
	}

	/**
	 * Wait a specified number of milliseconds before returning.
	 */
	static void sleep(uint32_t ms) {
		pluginCore->threadSleep(ms);
	}

	/**
	 * Wait for a thread to finish.
	 * The return code for the thread function is placed in the area
	 * pointed to by 'status', if 'status' is not NULL.
	 */
	void wait(int *status) {
		if (_thread) {
			pluginCore->threadWait(_thread, status);
		}
	}

	void wait() {
		int status;
		if (_thread) {
			pluginCore->threadWait(_thread, &status);
		}
	}
};

class Condition;

class Mutex {
private:
	friend class Condition;
	mutex_t *_mutex;
public:
	Mutex() {
		_mutex = pluginCore->mutexCreate();
	}

	~Mutex() {
		pluginCore->mutexDestroy(_mutex);
	}

	/**
	 * Lock the mutex
	 * Returns 0, or -1 on error
	 */
	int lock() {
		return pluginCore->mutexLock(_mutex);
	}

	/**
	 * Unlock the mutex
	 * It is an error to unlock a mutex that has not been locked by
	 * the current thread, and doing so results in undefined behavior.
	 *
	 * Returns 0, or -1 on error
	 */
	int unlock() {
		return pluginCore->mutexUnlock(_mutex);
	}

	class RegionLock {
	private:
		Mutex *_mutex;
	public:
		RegionLock(Mutex &mutex) :
			_mutex(&mutex)
		{
			_mutex->lock();
		}

		~RegionLock() {
			_mutex->unlock();
		}
	};
};

class Condition {
private:
	cond_t *_cond;
public:
	Condition() {
		_cond = pluginCore->condCreate();
	}

	~Condition() {
		pluginCore->condDestroy(_cond);
	}

	/**
	 * Wait on the condition variable, unlocking the provided mutex.
	 * The mutex must be locked before entering this function!
	 * The mutex is re-locked once the condition variable is signaled.
	 * Returns 0 when it is signaled, or -1 on error.
	 */
	int wait(const Mutex &mutex) {
		return pluginCore->condWait(_cond, mutex._mutex);
	}

	/*
	 * Waits for at most 'ms' milliseconds, and returns 0 if the condition
	 * variable is signaled, SDL_MUTEX_TIMEDOUT if the condition is not
	 * signaled in the allotted time, and -1 on error.
	 * On some platforms this function is implemented by looping with a delay
	 * of 1 ms, and so should be avoided if possible.
	*/
	int waitTimeout(const Mutex &mutex, uint32_t ms) {
		return pluginCore->condWaitTimeout(_cond, mutex._mutex, ms);
	}

	/**
	 * Restart one of the threads that are waiting on the condition variable.
	 * Returns 0 or -1 on error.
	 */
	int signal() {
		return pluginCore->condSignal(_cond);
	}

	/**
	 * Restart all threads that are waiting on the condition variable.
	 * Returns 0 or -1 on error.
	 */
	int broadcast() {
		return pluginCore->condBroadcast(_cond);
	}
};

#endif /* _UTIL_H_ */