summaryrefslogtreecommitdiffstats
path: root/xbmc/addons/kodi-dev-kit/include/kodi/addon-instance/AudioEncoder.h
diff options
context:
space:
mode:
authormanuel <manuel@mausz.at>2020-10-19 00:52:24 +0200
committermanuel <manuel@mausz.at>2020-10-19 00:52:24 +0200
commitbe933ef2241d79558f91796cc5b3a161f72ebf9c (patch)
treefe3ab2f130e20c99001f2d7a81d610c78c96a3f4 /xbmc/addons/kodi-dev-kit/include/kodi/addon-instance/AudioEncoder.h
parent5f8335c1e49ce108ef3481863833c98efa00411b (diff)
downloadkodi-pvr-build-be933ef2241d79558f91796cc5b3a161f72ebf9c.tar.gz
kodi-pvr-build-be933ef2241d79558f91796cc5b3a161f72ebf9c.tar.bz2
kodi-pvr-build-be933ef2241d79558f91796cc5b3a161f72ebf9c.zip
sync with upstream
Diffstat (limited to 'xbmc/addons/kodi-dev-kit/include/kodi/addon-instance/AudioEncoder.h')
-rw-r--r--xbmc/addons/kodi-dev-kit/include/kodi/addon-instance/AudioEncoder.h353
1 files changed, 353 insertions, 0 deletions
diff --git a/xbmc/addons/kodi-dev-kit/include/kodi/addon-instance/AudioEncoder.h b/xbmc/addons/kodi-dev-kit/include/kodi/addon-instance/AudioEncoder.h
new file mode 100644
index 0000000..9a869c1
--- /dev/null
+++ b/xbmc/addons/kodi-dev-kit/include/kodi/addon-instance/AudioEncoder.h
@@ -0,0 +1,353 @@
1/*
2 * Copyright (C) 2005-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
4 *
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
7 */
8
9#pragma once
10
11#include "../AddonBase.h"
12#include "../c-api/addon-instance/audio_encoder.h"
13
14#ifdef __cplusplus
15namespace kodi
16{
17namespace addon
18{
19
20//==============================================================================
21/// @addtogroup cpp_kodi_addon_audioencoder
22/// @brief \cpp_class{ kodi::addon::CInstanceAudioEncoder }
23/// **Audio encoder add-on instance.**\n
24/// For audio encoders as binary add-ons. This class implements a way to handle
25/// the encode of given stream to a new format.
26///
27/// The addon.xml defines the capabilities of this add-on.
28///
29///
30/// ----------------------------------------------------------------------------
31///
32/// **Here's an example on addon.xml:**
33/// ~~~~~~~~~~~~~{.xml}
34/// <extension
35/// point="kodi.audioencoder"
36/// extension=".flac"
37/// library_@PLATFORM@="@LIBRARY_FILENAME@"/>
38/// ~~~~~~~~~~~~~
39///
40/// Description to audio encoder related addon.xml values:
41/// | Name | Description
42/// |:------------------------------|----------------------------------------
43/// | <b>`point`</b> | Addon type specification<br>At all addon types and for this kind always <b>"kodi.audioencoder"</b>.
44/// | <b>`library_@PLATFORM@`</b> | Sets the used library name, which is automatically set by cmake at addon build.
45/// | <b>`extension`</b> | The file extensions / styles supported by this addon.
46///
47/// --------------------------------------------------------------------------
48///
49/// --------------------------------------------------------------------------
50///
51/// **Here is a code example how this addon is used:**
52///
53/// ~~~~~~~~~~~~~{.cpp}
54/// #include <kodi/addon-instance/AudioEncoder.h>
55///
56/// class ATTRIBUTE_HIDDEN CMyAudioEncoder : public kodi::addon::CInstanceAudioEncoder
57/// {
58/// public:
59/// CMyAudioEncoder(KODI_HANDLE instance, const std::string& kodiVersion)
60/// : kodi::addon::CInstanceAudioEncoder(instance, kodiVersion)
61///
62/// bool Init(const std::string& filename, unsigned int filecache,
63/// int& channels, int& samplerate,
64/// int& bitspersample, int64_t& totaltime,
65/// int& bitrate, AEDataFormat& format,
66/// std::vector<AEChannel>& channellist) override;
67/// int Encode(int numBytesRead, const uint8_t* pbtStream) override;
68/// bool Finish() override; // Optional
69/// };
70///
71/// CMyAudioEncoder::CMyAudioEncoder(KODI_HANDLE instance)
72/// : kodi::addon::CInstanceAudioEncoder(instance)
73/// {
74/// ...
75/// }
76///
77/// bool CMyAudioEncoder::Start(int inChannels,
78/// int inRate,
79/// int inBits,
80/// const std::string& title,
81/// const std::string& artist,
82/// const std::string& albumartist,
83/// const std::string& album,
84/// const std::string& year,
85/// const std::string& track,
86/// const std::string& genre,
87/// const std::string& comment,
88/// int trackLength)
89/// {
90/// ...
91/// return true;
92/// }
93///
94/// int CMyAudioEncoder::Encode(int numBytesRead, const uint8_t* pbtStream)
95/// {
96/// uint8_t* data = nullptr;
97/// int length = 0;
98/// ...
99/// kodi::addon::CInstanceAudioEncoder::Write(data, length);
100///
101/// return 0;
102/// }
103///
104///
105/// bool CMyAudioEncoder::Finish()
106/// {
107/// ...
108/// return true;
109/// }
110///
111/// //----------------------------------------------------------------------
112///
113/// class CMyAddon : public kodi::addon::CAddonBase
114/// {
115/// public:
116/// CMyAddon() = default;
117/// ADDON_STATUS CreateInstance(int instanceType,
118/// const std::string& instanceID,
119/// KODI_HANDLE instance,
120/// const std::string& version,
121/// KODI_HANDLE& addonInstance) override;
122/// };
123///
124/// // If you use only one instance in your add-on, can be instanceType and
125/// // instanceID ignored
126/// ADDON_STATUS CMyAddon::CreateInstance(int instanceType,
127/// const std::string& instanceID,
128/// KODI_HANDLE instance,
129/// const std::string& version,
130/// KODI_HANDLE& addonInstance)
131/// {
132/// if (instanceType == ADDON_INSTANCE_AUDIOENCODER)
133/// {
134/// kodi::Log(ADDON_LOG_INFO, "Creating my audio encoder instance");
135/// addonInstance = new CMyAudioEncoder(instance, version);
136/// return ADDON_STATUS_OK;
137/// }
138/// else if (...)
139/// {
140/// ...
141/// }
142/// return ADDON_STATUS_UNKNOWN;
143/// }
144///
145/// ADDONCREATOR(CMyAddon)
146/// ~~~~~~~~~~~~~
147///
148/// The destruction of the example class `CMyAudioEncoder` is called from
149/// Kodi's header. Manually deleting the add-on instance is not required.
150///
151class ATTRIBUTE_HIDDEN CInstanceAudioEncoder : public IAddonInstance
152{
153public:
154 //============================================================================
155 /// @ingroup cpp_kodi_addon_audioencoder
156 /// @brief Audio encoder class constructor used to support multiple instances.
157 ///
158 /// @param[in] instance The instance value given to
159 /// <b>`kodi::addon::CAddonBase::CreateInstance(...)`</b>.
160 /// @param[in] kodiVersion [opt] Version used in Kodi for this instance, to
161 /// allow compatibility to older Kodi versions.
162 ///
163 /// @note Recommended to set <b>`kodiVersion`</b>.
164 ///
165 ///
166 /// --------------------------------------------------------------------------
167 ///
168 /// **Here's example about the use of this:**
169 /// ~~~~~~~~~~~~~{.cpp}
170 /// class CMyAudioEncoder : public kodi::addon::CInstanceAudioEncoder
171 /// {
172 /// public:
173 /// CMyAudioEncoder(KODI_HANDLE instance, const std::string& kodiVersion)
174 /// : kodi::addon::CInstanceAudioEncoder(instance, kodiVersion)
175 /// {
176 /// ...
177 /// }
178 ///
179 /// ...
180 /// };
181 ///
182 /// ADDON_STATUS CMyAddon::CreateInstance(int instanceType,
183 /// const std::string& instanceID,
184 /// KODI_HANDLE instance,
185 /// const std::string& version,
186 /// KODI_HANDLE& addonInstance)
187 /// {
188 /// kodi::Log(ADDON_LOG_INFO, "Creating my audio encoder instance");
189 /// addonInstance = new CMyAudioEncoder(instance, version);
190 /// return ADDON_STATUS_OK;
191 /// }
192 /// ~~~~~~~~~~~~~
193 ///
194 explicit CInstanceAudioEncoder(KODI_HANDLE instance, const std::string& kodiVersion = "")
195 : IAddonInstance(ADDON_INSTANCE_AUDIOENCODER,
196 !kodiVersion.empty() ? kodiVersion
197 : GetKodiTypeVersion(ADDON_INSTANCE_AUDIOENCODER))
198 {
199 if (CAddonBase::m_interface->globalSingleInstance != nullptr)
200 throw std::logic_error("kodi::addon::CInstanceAudioEncoder: Creation of multiple together "
201 "with single instance way is not allowed!");
202
203 SetAddonStruct(instance);
204 }
205 //----------------------------------------------------------------------------
206
207 //============================================================================
208 /// @ingroup cpp_kodi_addon_audioencoder
209 /// @brief Start encoder (**required**)
210 ///
211 /// @param[in] inChannels Number of channels
212 /// @param[in] inRate Sample rate of input data
213 /// @param[in] inBits Bits per sample in input data
214 /// @param[in] title The title of the song
215 /// @param[in] artist The artist of the song
216 /// @param[in] albumartist The albumartist of the song
217 /// @param[in] year The year of the song
218 /// @param[in] track The track number of the song
219 /// @param[in] genre The genre of the song
220 /// @param[in] comment A comment to attach to the song
221 /// @param[in] trackLength Total track length in seconds
222 /// @return True on success, false on failure.
223 ///
224 virtual bool Start(int inChannels,
225 int inRate,
226 int inBits,
227 const std::string& title,
228 const std::string& artist,
229 const std::string& albumartist,
230 const std::string& album,
231 const std::string& year,
232 const std::string& track,
233 const std::string& genre,
234 const std::string& comment,
235 int trackLength) = 0;
236 //----------------------------------------------------------------------------
237
238 //============================================================================
239 /// @ingroup cpp_kodi_addon_audioencoder
240 /// @brief Encode a chunk of audio (**required**)
241 ///
242 /// @param[in] numBytesRead Number of bytes in input buffer
243 /// @param[in] pbtStream The input buffer
244 /// @return Number of bytes consumed
245 ///
246 virtual int Encode(int numBytesRead, const uint8_t* pbtStream) = 0;
247 //----------------------------------------------------------------------------
248
249 //============================================================================
250 /// @ingroup cpp_kodi_addon_audioencoder
251 /// @brief Finalize encoding (**optional**)
252 ///
253 /// @return True on success, false on failure.
254 ///
255 virtual bool Finish() { return true; }
256 //----------------------------------------------------------------------------
257
258 //============================================================================
259 /// @ingroup cpp_kodi_addon_audioencoder
260 /// @brief Write block of data
261 ///
262 /// @param[in] data Pointer to the array of elements to be written
263 /// @param[in] length Size in bytes to be written.
264 /// @return The total number of bytes successfully written is returned.
265 ///
266 /// @remarks Only called from addon itself.
267 ///
268 int Write(const uint8_t* data, int length)
269 {
270 return m_instanceData->toKodi->write(m_instanceData->toKodi->kodiInstance, data, length);
271 }
272 //----------------------------------------------------------------------------
273
274 //============================================================================
275 /// @ingroup cpp_kodi_addon_audioencoder
276 /// @brief Set the file's current position.
277 ///
278 /// The whence argument is optional and defaults to SEEK_SET (0)
279 ///
280 /// @param[in] position The position that you want to seek to
281 /// @param[in] whence [optional] offset relative to\n
282 /// You can set the value of whence to one
283 /// of three things:
284 /// | Value | int | Description |
285 /// |:--------:|:---:|:---------------------------------------------------|
286 /// | SEEK_SET | 0 | position is relative to the beginning of the file. This is probably what you had in mind anyway, and is the most commonly used value for whence.
287 /// | SEEK_CUR | 1 | position is relative to the current file pointer position. So, in effect, you can say, "Move to my current position plus 30 bytes," or, "move to my current position minus 20 bytes."
288 /// | SEEK_END | 2 | position is relative to the end of the file. Just like SEEK_SET except from the other end of the file. Be sure to use negative values for offset if you want to back up from the end of the file, instead of going past the end into oblivion.
289 ///
290 /// @return Returns the resulting offset location as measured in bytes from
291 /// the beginning of the file. On error, the value -1 is returned.
292 ///
293 /// @remarks Only called from addon itself.
294 ///
295 int64_t Seek(int64_t position, int whence = SEEK_SET)
296 {
297 return m_instanceData->toKodi->seek(m_instanceData->toKodi->kodiInstance, position, whence);
298 }
299 //----------------------------------------------------------------------------
300
301private:
302 void SetAddonStruct(KODI_HANDLE instance)
303 {
304 if (instance == nullptr)
305 throw std::logic_error("kodi::addon::CInstanceAudioEncoder: Creation with empty addon "
306 "structure not allowed, table must be given from Kodi!");
307
308 m_instanceData = static_cast<AddonInstance_AudioEncoder*>(instance);
309 m_instanceData->toAddon->addonInstance = this;
310 m_instanceData->toAddon->start = ADDON_Start;
311 m_instanceData->toAddon->encode = ADDON_Encode;
312 m_instanceData->toAddon->finish = ADDON_Finish;
313 }
314
315 inline static bool ADDON_Start(const AddonInstance_AudioEncoder* instance,
316 int inChannels,
317 int inRate,
318 int inBits,
319 const char* title,
320 const char* artist,
321 const char* albumartist,
322 const char* album,
323 const char* year,
324 const char* track,
325 const char* genre,
326 const char* comment,
327 int trackLength)
328 {
329 return static_cast<CInstanceAudioEncoder*>(instance->toAddon->addonInstance)
330 ->Start(inChannels, inRate, inBits, title, artist, albumartist, album, year, track, genre,
331 comment, trackLength);
332 }
333
334 inline static int ADDON_Encode(const AddonInstance_AudioEncoder* instance,
335 int numBytesRead,
336 const uint8_t* pbtStream)
337 {
338 return static_cast<CInstanceAudioEncoder*>(instance->toAddon->addonInstance)
339 ->Encode(numBytesRead, pbtStream);
340 }
341
342 inline static bool ADDON_Finish(const AddonInstance_AudioEncoder* instance)
343 {
344 return static_cast<CInstanceAudioEncoder*>(instance->toAddon->addonInstance)->Finish();
345 }
346
347 AddonInstance_AudioEncoder* m_instanceData;
348};
349
350} /* namespace addon */
351} /* namespace kodi */
352
353#endif /* __cplusplus */