Kodi Development 22.0
for Binary and Script based Add-Ons
 
Loading...
Searching...
No Matches
AudioEngine.h
1/*
2 * Copyright (C) 2005-2019 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/audio_engine.h"
13
14#ifdef __cplusplus
15
16namespace kodi
17{
18namespace audioengine
19{
20
21//¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
22// Main page text for audio engine group by Doxygen.
23//{{{
24
25//==============================================================================
74//------------------------------------------------------------------------------
75
76//==============================================================================
83//------------------------------------------------------------------------------
84
85//}}}
86
87//¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
88// "C++" related audio engine definitions
89//{{{
90
91//==============================================================================
102class ATTR_DLL_LOCAL AudioEngineFormat
103 : public addon::CStructHdl<AudioEngineFormat, AUDIO_ENGINE_FORMAT>
104{
105public:
108 {
109 m_cStructure->m_dataFormat = AUDIOENGINE_FMT_INVALID;
110 m_cStructure->m_sampleRate = 0;
111 m_cStructure->m_encodedRate = 0;
112 m_cStructure->m_frames = 0;
113 m_cStructure->m_frameSize = 0;
114 m_cStructure->m_channelCount = 0;
115
116 for (size_t ch = 0; ch < AUDIOENGINE_CH_MAX; ++ch)
117 m_cStructure->m_channels[ch] = AUDIOENGINE_CH_NULL;
118 }
119 AudioEngineFormat(const AudioEngineFormat& channel) : CStructHdl(channel) {}
120 AudioEngineFormat(const AUDIO_ENGINE_FORMAT* channel) : CStructHdl(channel) {}
121 AudioEngineFormat(AUDIO_ENGINE_FORMAT* channel) : CStructHdl(channel) {}
139
143
145 void SetDataFormat(enum AudioEngineDataFormat format) { m_cStructure->m_dataFormat = format; }
146
148 enum AudioEngineDataFormat GetDataFormat() const { return m_cStructure->m_dataFormat; }
149
151 void SetSampleRate(unsigned int rate) { m_cStructure->m_sampleRate = rate; }
152
154 unsigned int GetSampleRate() const { return m_cStructure->m_sampleRate; }
155
157 void SetEncodedRate(unsigned int rate) { m_cStructure->m_encodedRate = rate; }
158
160 unsigned int GetEncodedRate() const { return m_cStructure->m_encodedRate; }
161
163 void SetChannelLayout(const std::vector<enum AudioEngineChannel>& layout)
164 {
165 // Reset first all to empty values to AUDIOENGINE_CH_NULL, in case given list is empty
166 m_cStructure->m_channelCount = 0;
167 for (size_t ch = 0; ch < AUDIOENGINE_CH_MAX; ++ch)
168 m_cStructure->m_channels[ch] = AUDIOENGINE_CH_NULL;
169
170 for (size_t ch = 0; ch < layout.size() && ch < AUDIOENGINE_CH_MAX; ++ch)
171 {
172 m_cStructure->m_channels[ch] = layout[ch];
173 m_cStructure->m_channelCount++;
174 }
175 }
176
178 std::vector<enum AudioEngineChannel> GetChannelLayout() const
179 {
180 std::vector<enum AudioEngineChannel> channels;
181 for (size_t ch = 0; ch < AUDIOENGINE_CH_MAX; ++ch)
182 {
183 if (m_cStructure->m_channels[ch] == AUDIOENGINE_CH_NULL)
184 break;
185
186 channels.push_back(m_cStructure->m_channels[ch]);
187 }
188 return channels;
189 }
190
192 void SetFramesAmount(unsigned int frames) { m_cStructure->m_frames = frames; }
193
195 unsigned int GetFramesAmount() const { return m_cStructure->m_frames; }
196
198 void SetFrameSize(unsigned int frameSize) { m_cStructure->m_frameSize = frameSize; }
199
201 unsigned int GetFrameSize() const { return m_cStructure->m_frameSize; }
202
205 {
206 if (!fmt)
207 {
208 return false;
209 }
210
211 if (m_cStructure->m_dataFormat != fmt->m_cStructure->m_dataFormat ||
212 m_cStructure->m_sampleRate != fmt->m_cStructure->m_sampleRate ||
213 m_cStructure->m_encodedRate != fmt->m_cStructure->m_encodedRate ||
214 m_cStructure->m_frames != fmt->m_cStructure->m_frames ||
215 m_cStructure->m_frameSize != fmt->m_cStructure->m_frameSize ||
216 m_cStructure->m_channelCount != fmt->m_cStructure->m_channelCount)
217 {
218 return false;
219 }
220
221 for (unsigned int ch = 0; ch < AUDIOENGINE_CH_MAX; ++ch)
222 {
223 if (fmt->m_cStructure->m_channels[ch] != m_cStructure->m_channels[ch])
224 {
225 return false;
226 }
227 }
228
229 return true;
230 }
232};
234//----------------------------------------------------------------------------
235
236//}}}
237
238//¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
239// "C++" AudioEngine addon interface
240//{{{
241
242//============================================================================
256//----------------------------------------------------------------------------
257class ATTR_DLL_LOCAL CAEStream
258{
259public:
260 //==========================================================================
305 CAEStream(AudioEngineFormat& format, unsigned int options = 0)
306 : m_kodiBase(::kodi::addon::CPrivateBase::m_interface->toKodi->kodiBase),
307 m_cb(::kodi::addon::CPrivateBase::m_interface->toKodi->kodi_audioengine)
308 {
309 m_StreamHandle = m_cb->make_stream(m_kodiBase, format, options);
310 if (m_StreamHandle == nullptr)
311 {
312 kodi::Log(ADDON_LOG_FATAL, "CAEStream: make_stream failed!");
313 }
314 }
315 //--------------------------------------------------------------------------
316
317 //==========================================================================
322 {
323 if (m_StreamHandle)
324 {
325 m_cb->free_stream(m_kodiBase, m_StreamHandle);
326 m_StreamHandle = nullptr;
327 }
328 }
329 //--------------------------------------------------------------------------
330
331 //==========================================================================
337 unsigned int GetSpace() { return m_cb->aestream_get_space(m_kodiBase, m_StreamHandle); }
338 //--------------------------------------------------------------------------
339
340 //==========================================================================
352 unsigned int AddData(uint8_t* const* data,
353 unsigned int offset,
354 unsigned int frames,
355 double pts = 0,
356 bool hasDownmix = false,
357 double centerMixLevel = 1.0)
358 {
359 return m_cb->aestream_add_data(m_kodiBase, m_StreamHandle, data, offset, frames, pts,
360 hasDownmix, centerMixLevel);
361 }
362 //--------------------------------------------------------------------------
363
364 //==========================================================================
371 double GetDelay() { return m_cb->aestream_get_delay(m_kodiBase, m_StreamHandle); }
372 //--------------------------------------------------------------------------
373
374 //==========================================================================
380 bool IsBuffering() { return m_cb->aestream_is_buffering(m_kodiBase, m_StreamHandle); }
381 //--------------------------------------------------------------------------
382
383 //==========================================================================
390 double GetCacheTime() { return m_cb->aestream_get_cache_time(m_kodiBase, m_StreamHandle); }
391 //--------------------------------------------------------------------------
392
393 //==========================================================================
399 double GetCacheTotal() { return m_cb->aestream_get_cache_total(m_kodiBase, m_StreamHandle); }
400 //--------------------------------------------------------------------------
401
402 //==========================================================================
406 void Pause() { return m_cb->aestream_pause(m_kodiBase, m_StreamHandle); }
407 //--------------------------------------------------------------------------
408
409 //==========================================================================
413 void Resume() { return m_cb->aestream_resume(m_kodiBase, m_StreamHandle); }
414 //--------------------------------------------------------------------------
415
416 //==========================================================================
425 void Drain(bool wait = true) { return m_cb->aestream_drain(m_kodiBase, m_StreamHandle, wait); }
426 //--------------------------------------------------------------------------
427
428 //==========================================================================
432 bool IsDraining() { return m_cb->aestream_is_draining(m_kodiBase, m_StreamHandle); }
433 //--------------------------------------------------------------------------
434
435 //==========================================================================
439 bool IsDrained() { return m_cb->aestream_is_drained(m_kodiBase, m_StreamHandle); }
440 //--------------------------------------------------------------------------
441
442 //==========================================================================
446 void Flush() { return m_cb->aestream_flush(m_kodiBase, m_StreamHandle); }
447 //--------------------------------------------------------------------------
448
449 //==========================================================================
455 float GetVolume() { return m_cb->aestream_get_volume(m_kodiBase, m_StreamHandle); }
456 //--------------------------------------------------------------------------
457
458 //==========================================================================
464 void SetVolume(float volume)
465 {
466 return m_cb->aestream_set_volume(m_kodiBase, m_StreamHandle, volume);
467 }
468 //--------------------------------------------------------------------------
469
470 //==========================================================================
476 float GetAmplification() { return m_cb->aestream_get_amplification(m_kodiBase, m_StreamHandle); }
477 //--------------------------------------------------------------------------
478
479 //==========================================================================
485 void SetAmplification(float amplify)
486 {
487 return m_cb->aestream_set_amplification(m_kodiBase, m_StreamHandle, amplify);
488 }
489 //--------------------------------------------------------------------------
490
491 //==========================================================================
497 unsigned int GetFrameSize() const
498 {
499 return m_cb->aestream_get_frame_size(m_kodiBase, m_StreamHandle);
500 }
501 //--------------------------------------------------------------------------
502
503 //==========================================================================
509 unsigned int GetChannelCount() const
510 {
511 return m_cb->aestream_get_channel_count(m_kodiBase, m_StreamHandle);
512 }
513 //--------------------------------------------------------------------------
514
515 //==========================================================================
523 unsigned int GetSampleRate() const
524 {
525 return m_cb->aestream_get_sample_rate(m_kodiBase, m_StreamHandle);
526 }
527 //--------------------------------------------------------------------------
528
529 //==========================================================================
536 {
537 return m_cb->aestream_get_data_format(m_kodiBase, m_StreamHandle);
538 }
539 //--------------------------------------------------------------------------
540
541 //==========================================================================
550 {
551 return m_cb->aestream_get_resample_ratio(m_kodiBase, m_StreamHandle);
552 }
553 //--------------------------------------------------------------------------
554
555 //==========================================================================
565 void SetResampleRatio(double ratio)
566 {
567 m_cb->aestream_set_resample_ratio(m_kodiBase, m_StreamHandle, ratio);
568 }
569 //--------------------------------------------------------------------------
570
571private:
572 void* m_kodiBase;
574 AEStreamHandle* m_StreamHandle;
575};
576//----------------------------------------------------------------------------
577
578//============================================================================
606inline bool ATTR_DLL_LOCAL GetCurrentSinkFormat(AudioEngineFormat& format)
607{
608 using namespace kodi::addon;
609 return CPrivateBase::m_interface->toKodi->kodi_audioengine->get_current_sink_format(
610 CPrivateBase::m_interface->toKodi->kodiBase, format);
611}
612//----------------------------------------------------------------------------
613
614//}}}
615
616} // namespace audioengine
617} // namespace kodi
618
619#endif /* __cplusplus */
Definition AddonBase.h:206
Definition AudioEngine.h:104
Definition AudioEngine.h:258
@ ADDON_LOG_FATAL
4 : To notify fatal unrecoverable errors, which can may also indicate upcoming crashes.
Definition addon_base.h:197
bool IsBuffering()
Returns if the stream is buffering.
Definition AudioEngine.h:380
void Flush()
Flush all buffers dropping the audio data.
Definition AudioEngine.h:446
float GetAmplification()
Gets the stream's volume amplification in linear units.
Definition AudioEngine.h:476
bool IsDraining()
Returns true if the is stream draining.
Definition AudioEngine.h:432
unsigned int GetFrameSize() const
Returns the size of one audio frame in bytes (channelCount * resolution).
Definition AudioEngine.h:497
~CAEStream()
Class destructor.
Definition AudioEngine.h:321
void SetAmplification(float amplify)
Sets the stream's volume amplification in linear units.
Definition AudioEngine.h:485
void SetVolume(float volume)
Set the stream's volume level.
Definition AudioEngine.h:464
double GetResampleRatio()
Return the resample ratio.
Definition AudioEngine.h:549
void Pause()
Pauses the stream playback.
Definition AudioEngine.h:406
AudioEngineDataFormat GetDataFormat() const
Return the data format the stream has been configured with.
Definition AudioEngine.h:535
float GetVolume()
Return the stream's current volume level.
Definition AudioEngine.h:455
void Resume()
Resumes the stream after pausing.
Definition AudioEngine.h:413
double GetCacheTotal()
Returns the total time in seconds of the cache.
Definition AudioEngine.h:399
unsigned int GetSpace()
Returns the amount of space available in the stream.
Definition AudioEngine.h:337
unsigned int AddData(uint8_t *const *data, unsigned int offset, unsigned int frames, double pts=0, bool hasDownmix=false, double centerMixLevel=1.0)
Add planar or interleaved PCM data to the stream.
Definition AudioEngine.h:352
unsigned int GetSampleRate() const
Returns the stream's sample rate, if the stream is using a dynamic sample rate, this value will NOT r...
Definition AudioEngine.h:523
double GetDelay()
Returns the time in seconds that it will take for the next added packet to be heard from the speakers...
Definition AudioEngine.h:371
void SetResampleRatio(double ratio)
Sets the resample ratio.
Definition AudioEngine.h:565
double GetCacheTime()
Returns the time in seconds of the stream's cached audio samples. Engine buffers excluded.
Definition AudioEngine.h:390
CAEStream(AudioEngineFormat &format, unsigned int options=0)
Constructs new class to a Kodi IAEStream in the format specified.
Definition AudioEngine.h:305
unsigned int GetChannelCount() const
Returns the number of channels the stream is configured to accept.
Definition AudioEngine.h:509
bool IsDrained()
Returns true if the is stream has finished draining.
Definition AudioEngine.h:439
void Drain(bool wait=true)
Start draining the stream.
Definition AudioEngine.h:425
@ AUDIOENGINE_CH_NULL
Used inside to indicate the end of a list and not for addon use directly.
Definition audio_engine.h:72
@ AUDIOENGINE_CH_MAX
Maximum possible value, to use e.g. as size inside list.
Definition audio_engine.h:116
AudioEngineDataFormat
Definition audio_engine.h:148
@ AUDIOENGINE_FMT_INVALID
To define format as invalid.
Definition audio_engine.h:150
void SetFrameSize(unsigned int frameSize)
The size of one frame in bytes.
Definition AudioEngine.h:198
unsigned int GetFramesAmount() const
To get with SetFramesAmount changed values.
Definition AudioEngine.h:195
void SetSampleRate(unsigned int rate)
The stream's sample rate (eg, 48000)
Definition AudioEngine.h:151
std::vector< enum AudioEngineChannel > GetChannelLayout() const
To get with SetChannelLayout changed values.
Definition AudioEngine.h:178
unsigned int GetFrameSize() const
To get with SetFrameSize changed values.
Definition AudioEngine.h:201
bool CompareFormat(const AudioEngineFormat *fmt)
Function to compare the format structure with another.
Definition AudioEngine.h:204
void SetEncodedRate(unsigned int rate)
The encoded streams sample rate if a bitstream, otherwise undefined.
Definition AudioEngine.h:157
enum AudioEngineDataFormat GetDataFormat() const
To get with SetDataFormat changed values.
Definition AudioEngine.h:148
unsigned int GetSampleRate() const
To get with SetSampleRate changed values.
Definition AudioEngine.h:154
void SetDataFormat(enum AudioEngineDataFormat format)
The stream's data format (eg, AUDIOENGINE_FMT_S16LE)
Definition AudioEngine.h:145
void SetChannelLayout(const std::vector< enum AudioEngineChannel > &layout)
The stream's channel layout.
Definition AudioEngine.h:163
void SetFramesAmount(unsigned int frames)
The number of frames per period.
Definition AudioEngine.h:192
unsigned int GetEncodedRate() const
To get with SetEncodedRate changed values.
Definition AudioEngine.h:160
bool ATTR_DLL_LOCAL GetCurrentSinkFormat(AudioEngineFormat &format)
Get the current sink data format.
Definition AudioEngine.h:606
void ATTR_DLL_LOCAL Log(const ADDON_LOG loglevel, const char *format,...)
Add a message to Kodi's log.
Definition AddonBase.h:1938
Internal API structure which are used for data exchange between Kodi and addon.
Definition audio_engine.h:230
unsigned int m_sampleRate
Definition audio_engine.h:235
enum AudioEngineChannel m_channels[AUDIOENGINE_CH_MAX]
Definition audio_engine.h:244
unsigned int m_frames
Definition audio_engine.h:247
unsigned int m_encodedRate
Definition audio_engine.h:238
enum AudioEngineDataFormat m_dataFormat
Definition audio_engine.h:232
unsigned int m_frameSize
Definition audio_engine.h:250
unsigned int m_channelCount
Definition audio_engine.h:241
Definition audio_engine.h:267