summaryrefslogtreecommitdiffstats
path: root/xbmc/addons/kodi-addon-dev-kit/include/kodi/AudioEngine.h
blob: 1265dcd0f73507e75da2b06cb57fb6ba1d71c78c (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
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
#pragma once
/*
 *      Copyright (C) 2005-2017 Team KODI
 *      http://kodi.tv
 *
 *  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, 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 Kodi; see the file COPYING.  If not, see
 *  <http://www.gnu.org/licenses/>.
 *
 */

#include "AddonBase.h"

#ifdef BUILD_KODI_ADDON
#include "AEChannelData.h"
#else
#include "cores/AudioEngine/Utils/AEChannelData.h"
#endif

//==============================================================================
///
/// \defgroup cpp_kodi_audioengine  Interface - kodi::audioengine
/// \ingroup cpp
/// @brief **Audio engine functions**
///
///
/// It has the header \ref AudioEngine.h "#include <kodi/AudioEngine.h>" be included
/// to enjoy it.
///
//------------------------------------------------------------------------------

//==============================================================================
///
/// \defgroup cpp_kodi_audioengine_Defs Definitions, structures and enumerators
/// \ingroup cpp_kodi_audioengine
/// @brief **Library definition values**
///
//------------------------------------------------------------------------------

extern "C"
{

  //============================================================================
  /// \ingroup cpp_kodi_audioengine_Defs
  /// @brief Bit options to pass to CAddonAEStream
  ///
  typedef enum AudioEngineStreamOptions
  {
    /// force resample even if rates match
    AUDIO_STREAM_FORCE_RESAMPLE = 1 << 0,
    /// create the stream paused
    AUDIO_STREAM_PAUSED         = 1 << 1,
    /// autostart the stream when enough data is buffered
    AUDIO_STREAM_AUTOSTART      = 1 << 2,
    /// if this option is set the ADSP-System is bypassed and the raw stream
    /// will be passed through IAESink
    AUDIO_STREAM_BYPASS_ADSP    = 1 << 3
  } AudioEngineStreamOptions;
  //----------------------------------------------------------------------------

  //============================================================================
  /// \defgroup cpp_kodi_audioengine_Defs_AudioEngineFormat struct AudioEngineFormat
  /// \ingroup cpp_kodi_audioengine_Defs
  /// @brief The audio format structure that fully defines a stream's audio
  /// information
  ///
  //@{
  struct AudioEngineFormat
  {
    /// The stream's data format (eg, AE_FMT_S16LE)
    enum AEDataFormat m_dataFormat;

    /// The stream's sample rate (eg, 48000)
    unsigned int m_sampleRate;

    /// The encoded streams sample rate if a bitstream, otherwise undefined
    unsigned int m_encodedRate;

    /// The amount of used speaker channels
    unsigned int m_channelCount;

    /// The stream's channel layout
    enum AEChannel m_channels[AE_CH_MAX];

    /// The number of frames per period
    unsigned int m_frames;

    /// The size of one frame in bytes
    unsigned int m_frameSize;

    AudioEngineFormat()
    {
      m_dataFormat = AE_FMT_INVALID;
      m_sampleRate = 0;
      m_encodedRate = 0;
      m_frames = 0;
      m_frameSize = 0;
      m_channelCount = 0;

      for (unsigned int ch = 0; ch < AE_CH_MAX; ++ch)
      {
        m_channels[ch] = AE_CH_MAX;
      }
    }

    /// Function to compare the format structure with another
    bool compareFormat(const AudioEngineFormat *fmt)
    {
      if (!fmt)
      {
        return false;
      }

      if (m_dataFormat    != fmt->m_dataFormat    ||
          m_sampleRate    != fmt->m_sampleRate    ||
          m_encodedRate   != fmt->m_encodedRate   ||
          m_frames        != fmt->m_frames        ||
          m_frameSize     != fmt->m_frameSize     ||
          m_channelCount  != fmt->m_channelCount)
      {
        return false;
      }

      for (unsigned int ch = 0; ch < AE_CH_MAX; ++ch)
      {
        if (fmt->m_channels[ch] != m_channels[ch])
        {
          return false;
        }
      }

      return  true;
    }
  };
  //@}
  //----------------------------------------------------------------------------

  /* A stream handle pointer, which is only used internally by the addon stream handle */
  typedef void AEStreamHandle;

  /*
   * Function address structure, not need to visible on dev kit doxygen
   * documentation
   */
  typedef struct AddonToKodiFuncTable_kodi_audioengine
  {
    AEStreamHandle* (*MakeStream) (void *kodiBase, AudioEngineFormat format, unsigned int options);
    void (*FreeStream) (void *kodiBase, AEStreamHandle *stream);
    bool (*GetCurrentSinkFormat) (void *kodiBase, AudioEngineFormat *SinkFormat);

    // Audio Engine Stream definitions
    unsigned int (*AEStream_GetSpace) (void *kodiBase, AEStreamHandle *handle);
    unsigned int (*AEStream_AddData) (void *kodiBase, AEStreamHandle *handle, uint8_t* const *Data, unsigned int Offset, unsigned int Frames);
    double (*AEStream_GetDelay)(void *kodiBase, AEStreamHandle *handle);
    bool (*AEStream_IsBuffering)(void *kodiBase, AEStreamHandle *handle);
    double (*AEStream_GetCacheTime)(void *kodiBase, AEStreamHandle *handle);
    double (*AEStream_GetCacheTotal)(void *kodiBase, AEStreamHandle *handle);
    void (*AEStream_Pause)(void *kodiBase, AEStreamHandle *handle);
    void (*AEStream_Resume)(void *kodiBase, AEStreamHandle *handle);
    void (*AEStream_Drain)(void *kodiBase, AEStreamHandle *handle, bool Wait);
    bool (*AEStream_IsDraining)(void *kodiBase, AEStreamHandle *handle);
    bool (*AEStream_IsDrained)(void *kodiBase, AEStreamHandle *handle);
    void (*AEStream_Flush)(void *kodiBase, AEStreamHandle *handle);
    float (*AEStream_GetVolume)(void *kodiBase, AEStreamHandle *handle);
    void (*AEStream_SetVolume)(void *kodiBase, AEStreamHandle *handle, float Volume);
    float (*AEStream_GetAmplification)(void *kodiBase, AEStreamHandle *handle);
    void (*AEStream_SetAmplification)(void *kodiBase, AEStreamHandle *handle, float Amplify);
    unsigned int (*AEStream_GetFrameSize)(void *kodiBase, AEStreamHandle *handle);
    unsigned int (*AEStream_GetChannelCount)(void *kodiBase, AEStreamHandle *handle);
    unsigned int (*AEStream_GetSampleRate)(void *kodiBase, AEStreamHandle *handle);
    AEDataFormat (*AEStream_GetDataFormat)(void *kodiBase, AEStreamHandle *handle);
    double (*AEStream_GetResampleRatio)(void *kodiBase, AEStreamHandle *handle);
    void (*AEStream_SetResampleRatio)(void *kodiBase, AEStreamHandle *handle, double Ratio);
  } AddonToKodiFuncTable_kodi_audioengine;

} /* extern "C" */

namespace kodi
{
namespace audioengine
{

  //============================================================================
  ///
  /// \defgroup cpp_kodi_audioengine_CAddonAEStream class CAddonAEStream
  /// \ingroup cpp_kodi_audioengine
  /// @brief **Audio Engine Stream Class**
  ///
  ///
  /// It has the header \ref AudioEngine.h "#include <kodi/AudioEngine.h>" be
  /// included to enjoy it.
  ///
  //----------------------------------------------------------------------------
  class CAddonAEStream
  {
  public:
    //==========================================================================
    /// @ingroup cpp_kodi_audioengine_CAddonAEStream
    /// @brief Contructs new class to an Kodi IAEStream in the format specified.
    ///
    /// @param[in] format       The data format the incoming audio will be in
    ///                         (e.g. \ref AE_FMT_S16LE)
    /// @param[in] options      [opt] A bit field of stream options (see: enum \ref AudioEngineStreamOptions)
    ///
    ///
    /// ------------------------------------------------------------------------
    ///
    /// **Audio engine format information:**
    /// @code
    /// /*
    ///  * Audio engine format information
    ///  *
    ///  * Only as example shown here! See always the original structure on related header.
    ///  */
    /// typedef struct AudioEngineFormat
    /// {
    ///   enum AEDataFormat    m_dataFormat;             /* The stream's data format (eg, AE_FMT_S16LE) */
    ///   unsigned int         m_sampleRate;             /* The stream's sample rate (eg, 48000) */
    ///   unsigned int         m_encodedRate;            /* The encoded streams sample rate if a bitstream, otherwise undefined */
    ///   unsigned int         m_channelCount;           /* The amount of used speaker channels */
    ///   enum AEChannel       m_channels[AE_CH_MAX];    /* The stream's channel layout */
    ///   unsigned int         m_frames;                 /* The number of frames per period */
    ///   unsigned int         m_frameSamples;           /* The number of samples in one frame */
    ///   unsigned int         m_frameSize;              /* The size of one frame in bytes */
    ///
    ///   /* Function to compare the format structure with another */
    ///   bool compareFormat(const AudioEngineFormat *fmt);
    /// } AudioEngineFormat;
    /// @endcode
    ///
    /// ------------------------------------------------------------------------
    ///
    /// **Bit options to pass to CAELib_Stream (on Kodi by <c>IAE::MakeStream</c>)**
    ///
    /// | enum AEStreamOptions     | Value: | Description:
    /// |-------------------------:|:------:|:-----------------------------------
    /// | AE_STREAM_FORCE_RESAMPLE | 1 << 0 | Force resample even if rates match
    /// | AE_STREAM_PAUSED         | 1 << 1 | Create the stream paused
    /// | AE_STREAM_AUTOSTART      | 1 << 2 | Autostart the stream when enough data is buffered
    /// | AE_STREAM_BYPASS_ADSP    | 1 << 3 | if this option is set the ADSP-System is bypassed and the raw stream will be passed through IAESink.
    ///
    ///
    /// ------------------------------------------------------------------------
    ///
    /// **Example:**
    /// ~~~~~~~~~~~~~{.cpp}
    ///
    /// #include <kodi/AudioEngine.h>
    ///
    /// using namespace kodi::audioengine;
    ///
    /// ...
    ///
    /// CAddonAEStream* stream = new CAddonAEStream(AE_FMT_S16LE, AE_STREAM_AUTOSTART | AE_STREAM_BYPASS_ADSP);
    ///
    /// ~~~~~~~~~~~~~
    ///
    CAddonAEStream(AudioEngineFormat format, unsigned int options = 0)
      : m_kodiBase(::kodi::addon::CAddonBase::m_interface->toKodi->kodiBase),
        m_cb(::kodi::addon::CAddonBase::m_interface->toKodi->kodi_audioengine)
    {
      m_StreamHandle = m_cb->MakeStream(m_kodiBase, format, options);
      if (m_StreamHandle == nullptr)
      {
        kodi::Log(ADDON_LOG_FATAL, "CAddonAEStream: MakeStream failed!");
      }
    }
    //--------------------------------------------------------------------------

    ~CAddonAEStream()
    {
      if (m_StreamHandle)
      {
        m_cb->FreeStream(m_kodiBase, m_StreamHandle);
        m_StreamHandle = nullptr;
      }
    }

    //==========================================================================
    /// @ingroup cpp_kodi_audioengine_CAddonAEStream
    /// @brief Returns the amount of space available in the stream
    ///
    /// @return                 The number of bytes AddData will consume
    ///
    unsigned int GetSpace()
    {
      return m_cb->AEStream_GetSpace(m_kodiBase, m_StreamHandle);
    }
    //--------------------------------------------------------------------------

    //==========================================================================
    /// @ingroup cpp_kodi_audioengine_CAddonAEStream
    /// @brief Add planar or interleaved PCM data to the stream
    ///
    /// @param data             array of pointers to the planes
    /// @param offset           to frame in frames
    /// @param frames           number of frames
    /// @return                 The number of frames consumed
    ///
    unsigned int AddData(uint8_t* const *data, unsigned int offset, unsigned int frames)
    {
      return m_cb->AEStream_AddData(m_kodiBase, m_StreamHandle, data, offset, frames);
    }
    //--------------------------------------------------------------------------

    //==========================================================================
    /// @ingroup cpp_kodi_audioengine_CAddonAEStream
    /// @brief Returns the time in seconds that it will take for the next added
    /// packet to be heard from the speakers.
    ///
    /// @return seconds
    ///
    double GetDelay()
    {
      return m_cb->AEStream_GetDelay(m_kodiBase, m_StreamHandle);
    }
    //--------------------------------------------------------------------------

    //==========================================================================
    /// @ingroup cpp_kodi_audioengine_CAddonAEStream
    /// @brief Returns if the stream is buffering
    ///
    /// @return True if the stream is buffering
    ///
    bool IsBuffering()
    {
      return m_cb->AEStream_IsBuffering(m_kodiBase, m_StreamHandle);
    }
    //--------------------------------------------------------------------------

    //==========================================================================
    /// @ingroup cpp_kodi_audioengine_CAddonAEStream
    /// @brief Returns the time in seconds of the stream's cached audio samples.
    /// Engine buffers excluded.
    ///
    /// @return seconds
    ///
    double GetCacheTime()
    {
      return m_cb->AEStream_GetCacheTime(m_kodiBase, m_StreamHandle);
    }
    //--------------------------------------------------------------------------

    //==========================================================================
    /// @ingroup cpp_kodi_audioengine_CAddonAEStream
    /// @brief Returns the total time in seconds of the cache
    ///
    /// @return seconds
    ///
    double GetCacheTotal()
    {
      return m_cb->AEStream_GetCacheTotal(m_kodiBase, m_StreamHandle);
    }
    //--------------------------------------------------------------------------

    //==========================================================================
    /// @ingroup cpp_kodi_audioengine_CAddonAEStream
    /// @brief Pauses the stream playback
    ///
    void Pause()
    {
      return m_cb->AEStream_Pause(m_kodiBase, m_StreamHandle);
    }
    //--------------------------------------------------------------------------

    //==========================================================================
    /// @ingroup cpp_kodi_audioengine_CAddonAEStream
    /// @brief Resumes the stream after pausing
    ///
    void Resume()
    {
      return m_cb->AEStream_Resume(m_kodiBase, m_StreamHandle);
    }
    //--------------------------------------------------------------------------

    //==========================================================================
    /// @ingroup cpp_kodi_audioengine_CAddonAEStream
    /// @brief Start draining the stream
    ///
    /// @param[in] wait         [opt] Wait until drain is finished if set to
    ///                         true, otherwise it returns direct
    ///
    /// @note Once called AddData will not consume more data.
    ///
    void Drain(bool wait)
    {
      return m_cb->AEStream_Drain(m_kodiBase, m_StreamHandle, wait=true);
    }
    //--------------------------------------------------------------------------

    //==========================================================================
    /// @ingroup cpp_kodi_audioengine_CAddonAEStream
    /// @brief Returns true if the is stream draining
    ///
    bool IsDraining()
    {
      return m_cb->AEStream_IsDraining(m_kodiBase, m_StreamHandle);
    }
    //--------------------------------------------------------------------------

    //==========================================================================
    /// @ingroup cpp_kodi_audioengine_CAddonAEStream
    /// @brief Returns true if the is stream has finished draining
    ///
    bool IsDrained()
    {
      return m_cb->AEStream_IsDrained(m_kodiBase, m_StreamHandle);
    }
    //--------------------------------------------------------------------------

    //==========================================================================
    /// @ingroup cpp_kodi_audioengine_CAddonAEStream
    /// @brief Flush all buffers dropping the audio data
    ///
    void Flush()
    {
      return m_cb->AEStream_Flush(m_kodiBase, m_StreamHandle);
    }
    //--------------------------------------------------------------------------

    //==========================================================================
    /// @ingroup cpp_kodi_audioengine_CAddonAEStream
    /// @brief Return the stream's current volume level
    ///
    /// @return The volume level between 0.0 and 1.0
    ///
    float GetVolume()
    {
      return m_cb->AEStream_GetVolume(m_kodiBase, m_StreamHandle);
    }
    //--------------------------------------------------------------------------

    //==========================================================================
    /// @ingroup cpp_kodi_audioengine_CAddonAEStream
    /// @brief Set the stream's volume level
    ///
    /// @param[in] volume               The new volume level between 0.0 and 1.0
    ///
    void SetVolume(float volume)
    {
      return m_cb->AEStream_SetVolume(m_kodiBase, m_StreamHandle, volume);
    }
    //--------------------------------------------------------------------------

    //==========================================================================
    /// @ingroup cpp_kodi_audioengine_CAddonAEStream
    /// @brief Gets the stream's volume amplification in linear units.
    ///
    /// @return The volume amplification factor between 1.0 and 1000.0
    ///
    float GetAmplification()
    {
      return m_cb->AEStream_GetAmplification(m_kodiBase, m_StreamHandle);
    }
    //--------------------------------------------------------------------------

    //==========================================================================
    /// @ingroup cpp_kodi_audioengine_CAddonAEStream
    /// @brief Sets the stream's volume amplification in linear units.
    ///
    /// @param[in] amplify              The volume amplification factor between
    ///                                 1.0 and 1000.0
    ///
    void SetAmplification(float amplify)
    {
      return m_cb->AEStream_SetAmplification(m_kodiBase, m_StreamHandle, amplify);
    }
    //--------------------------------------------------------------------------

    //==========================================================================
    /// @ingroup cpp_kodi_audioengine_CAddonAEStream
    /// @brief Returns the size of one audio frame in bytes (channelCount * resolution)
    ///
    /// @return The size in bytes of one frame
    ///
    unsigned int GetFrameSize() const
    {
      return m_cb->AEStream_GetFrameSize(m_kodiBase, m_StreamHandle);
    }
    //--------------------------------------------------------------------------

    //==========================================================================
    /// @ingroup cpp_kodi_audioengine_CAddonAEStream
    /// @brief Returns the number of channels the stream is configured to accept
    ///
    /// @return The channel count
    ///
    unsigned int GetChannelCount() const
    {
      return m_cb->AEStream_GetChannelCount(m_kodiBase, m_StreamHandle);
    }
    //--------------------------------------------------------------------------

    //==========================================================================
    /// @ingroup cpp_kodi_audioengine_CAddonAEStream
    /// @brief Returns the stream's sample rate, if the stream is using a dynamic
    /// sample rate, this value will NOT reflect any changes made by calls to
    /// SetResampleRatio()
    ///
    /// @return The stream's sample rate (eg, 48000)
    ///
    unsigned int GetSampleRate() const
    {
      return m_cb->AEStream_GetSampleRate(m_kodiBase, m_StreamHandle);
    }
    //--------------------------------------------------------------------------

    //==========================================================================
    /// @ingroup cpp_kodi_audioengine_CAddonAEStream
    /// @brief Return the data format the stream has been configured with
    ///
    /// @return The stream's data format (eg, AUDIOENGINE_FMT_S16LE)
    ///
    AEDataFormat GetDataFormat() const
    {
      return m_cb->AEStream_GetDataFormat(m_kodiBase, m_StreamHandle);
    }
    //--------------------------------------------------------------------------

    //==========================================================================
    /// @ingroup cpp_kodi_audioengine_CAddonAEStream
    /// @brief Return the resample ratio
    ///
    /// @note This will return an undefined value if the stream is not resampling
    ///
    /// @return the current resample ratio or undefined if the stream is not resampling
    ///
    double GetResampleRatio()
    {
      return m_cb->AEStream_GetResampleRatio(m_kodiBase, m_StreamHandle);
    }
    //--------------------------------------------------------------------------

    //==========================================================================
    /// @ingroup cpp_kodi_audioengine_CAddonAEStream
    /// @brief Sets the resample ratio
    ///
    /// @note This function may return false if the stream is not resampling, if
    /// you wish to use this be sure to set the AESTREAM_FORCE_RESAMPLE option
    ///
    /// @param[in] ratio         the new sample rate ratio, calculated by
    ///                          ((double)desiredRate / (double)GetSampleRate())
    ///
    void SetResampleRatio(double ratio)
    {
      m_cb->AEStream_SetResampleRatio(m_kodiBase, m_StreamHandle, ratio);
    }
    //--------------------------------------------------------------------------

  private:
    void* m_kodiBase;
    AddonToKodiFuncTable_kodi_audioengine* m_cb;
    AEStreamHandle  *m_StreamHandle;
  };

  //============================================================================
  /// @ingroup cpp_kodi_audioengine
  /// @brief Get the current sink data format
  ///
  /// @param[in] format Current sink data format. For more details see AudioEngineFormat.
  /// @return Returns true on success, else false.
  ///
  inline bool GetCurrentSinkFormat(AudioEngineFormat &format)
  {
    return ::kodi::addon::CAddonBase::m_interface->toKodi->kodi_audioengine->GetCurrentSinkFormat(::kodi::addon::CAddonBase::m_interface->toKodi->kodiBase, &format);
  }
  //----------------------------------------------------------------------------

} /* audioengine */
} /* kodi */