Kodi Development 22.0
for Binary and Script based Add-Ons
 
Loading...
Searching...
No Matches
AudioDecoder.h
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 "../AudioEngine.h"
13#include "../c-api/addon-instance/audiodecoder.h"
14
15#ifdef __cplusplus
16
17#include <cstring>
18#include <stdexcept>
19
20namespace kodi
21{
22namespace addon
23{
24
25class CInstanceAudioDecoder;
26
27//==============================================================================
41class ATTR_DLL_LOCAL AudioDecoderInfoTag
42{
43public:
45 AudioDecoderInfoTag() = default;
74
77
79 void SetTitle(const std::string& title) { m_title = title; }
80
82 const std::string& GetTitle() const { return m_title; }
83
85 void SetArtist(const std::string& artist) { m_artist = artist; }
86
88 const std::string& GetArtist() const { return m_artist; }
89
91 void SetAlbum(const std::string& album) { m_album = album; }
92
94 const std::string& GetAlbum() const { return m_album; }
95
97 void SetAlbumArtist(const std::string& albumArtist) { m_album_artist = albumArtist; }
98
100 const std::string& GetAlbumArtist() const { return m_album_artist; }
101
112 void SetMediaType(const std::string& mediaType) { m_media_type = mediaType; }
113
115 const std::string& GetMediaType() const { return m_media_type; }
116
118 void SetGenre(const std::string& genre) { m_genre = genre; }
119
121 const std::string& GetGenre() const { return m_genre; }
122
124 void SetDuration(int duration) { m_duration = duration; }
125
127 int GetDuration() const { return m_duration; }
128
130 void SetTrack(int track) { m_track = track; }
131
133 int GetTrack() const { return m_track; }
134
136 void SetDisc(int disc) { m_disc = disc; }
137
139 int GetDisc() const { return m_disc; }
140
142 void SetDiscSubtitle(const std::string& discSubtitle) { m_disc_subtitle = discSubtitle; }
143
145 const std::string& GetDiscSubtitle() const { return m_disc_subtitle; }
146
148 void SetDiscTotal(int discTotal) { m_disc_total = discTotal; }
149
151 int GetDiscTotal() const { return m_disc_total; }
152
155 void SetReleaseDate(const std::string& releaseDate) { m_release_date = releaseDate; }
156
158 const std::string& GetReleaseDate() const { return m_release_date; }
159
161 void SetLyrics(const std::string& lyrics) { m_lyrics = lyrics; }
162
164 const std::string& GetLyrics() const { return m_lyrics; }
165
167 void SetSamplerate(int samplerate) { m_samplerate = samplerate; }
168
170 int GetSamplerate() const { return m_samplerate; }
171
173 void SetChannels(int channels) { m_channels = channels; }
174
176 int GetChannels() const { return m_channels; }
177
179 void SetBitrate(int bitrate) { m_bitrate = bitrate; }
180
182 int GetBitrate() const { return m_bitrate; }
183
185 void SetComment(const std::string& comment) { m_comment = comment; }
186
188 const std::string& GetComment() const { return m_comment; }
189
195 void SetCoverArtByPath(const std::string& path) { m_cover_art_path = path; }
196
204 const std::string& GetCoverArtByPath() const { return m_cover_art_path; }
205
217 void SetCoverArtByMem(const uint8_t* data, size_t size, const std::string& mimetype)
218 {
219 if (size > 0)
220 {
221 m_cover_art_mem_mimetype = mimetype;
222 m_cover_art_mem.resize(size);
223 m_cover_art_mem.assign(data, data + size);
224 }
225 }
226
237 const uint8_t* GetCoverArtByMem(size_t& size, std::string& mimetype) const
238 {
239 if (!m_cover_art_mem.empty())
240 {
241 mimetype = m_cover_art_mem_mimetype;
242 size = m_cover_art_mem.size();
243 return m_cover_art_mem.data();
244 }
245 else
246 {
247 size = 0;
248 return nullptr;
249 }
250 }
251
253
254private:
255 std::string m_title;
256 std::string m_artist;
257 std::string m_album;
258 std::string m_album_artist;
259 std::string m_media_type;
260 std::string m_genre;
261 int m_duration{0};
262 int m_track{0};
263 int m_disc{0};
264 std::string m_disc_subtitle;
265 int m_disc_total{0};
266 std::string m_release_date;
267 std::string m_lyrics;
268 int m_samplerate{0};
269 int m_channels{0};
270 int m_bitrate{0};
271 std::string m_comment;
272 std::string m_cover_art_path;
273 std::string m_cover_art_mem_mimetype;
274 std::vector<uint8_t> m_cover_art_mem;
275};
277//------------------------------------------------------------------------------
278
279//==============================================================================
289
290//==============================================================================
430class ATTR_DLL_LOCAL CInstanceAudioDecoder : public IAddonInstance
431{
432public:
433 //==========================================================================
468 : IAddonInstance(instance)
469 {
470 if (CPrivateBase::m_interface->globalSingleInstance != nullptr)
471 throw std::logic_error("kodi::addon::CInstanceAudioDecoder: Creation of multiple together "
472 "with single instance way is not allowed!");
473
474 SetAddonStruct(instance);
475 }
476 //--------------------------------------------------------------------------
477
478 //==========================================================================
487 virtual bool SupportsFile(const std::string& filename) { return true; }
488 //--------------------------------------------------------------------------
489
490 //==========================================================================
509 virtual bool Init(const std::string& filename,
510 unsigned int filecache,
511 int& channels,
512 int& samplerate,
513 int& bitspersample,
514 int64_t& totaltime,
515 int& bitrate,
516 AudioEngineDataFormat& format,
517 std::vector<AudioEngineChannel>& channellist) = 0;
518 //--------------------------------------------------------------------------
519
520 //==========================================================================
529 virtual int ReadPCM(uint8_t* buffer, size_t size, size_t& actualsize) = 0;
530 //--------------------------------------------------------------------------
531
532 //==========================================================================
539 virtual int64_t Seek(int64_t time) { return time; }
540 //--------------------------------------------------------------------------
541
542 //==========================================================================
554 virtual bool ReadTag(const std::string& file, kodi::addon::AudioDecoderInfoTag& tag)
555 {
556 return false;
557 }
558 //--------------------------------------------------------------------------
559
560 //==========================================================================
567 virtual int TrackCount(const std::string& file) { return 1; }
568 //--------------------------------------------------------------------------
569
570 //==========================================================================
584 inline static std::string GetTrack(const std::string& name,
585 const std::string& trackPath,
586 int& track)
587 {
588 /*
589 * get the track name from path
590 */
591 track = 0;
592 std::string toLoad(trackPath);
593 const std::string ext = "." + name + KODI_ADDON_AUDIODECODER_TRACK_EXT;
594 if (toLoad.find(ext) != std::string::npos)
595 {
596 size_t iStart = toLoad.rfind('-') + 1;
597 track = atoi(toLoad.substr(iStart, toLoad.size() - iStart - ext.size()).c_str());
598 // The directory we are in, is the file
599 // that contains the bitstream to play,
600 // so extract it
601 size_t slash = trackPath.rfind('\\');
602 if (slash == std::string::npos)
603 slash = trackPath.rfind('/');
604 toLoad = trackPath.substr(0, slash);
605 }
606
607 return toLoad;
608 }
609 //--------------------------------------------------------------------------
610
611private:
612 void SetAddonStruct(KODI_ADDON_INSTANCE_STRUCT* instance)
613 {
614 instance->hdl = this;
615 instance->audiodecoder->toAddon->supports_file = ADDON_supports_file;
616 instance->audiodecoder->toAddon->init = ADDON_init;
617 instance->audiodecoder->toAddon->read_pcm = ADDON_read_pcm;
618 instance->audiodecoder->toAddon->seek = ADDON_seek;
619 instance->audiodecoder->toAddon->read_tag = ADDON_read_tag;
620 instance->audiodecoder->toAddon->track_count = ADDON_track_count;
621 }
622
623 inline static bool ADDON_supports_file(const KODI_ADDON_AUDIODECODER_HDL hdl, const char* file)
624 {
625 return static_cast<CInstanceAudioDecoder*>(hdl)->SupportsFile(file);
626 }
627
628 inline static bool ADDON_init(const KODI_ADDON_AUDIODECODER_HDL hdl,
629 const char* file,
630 unsigned int filecache,
631 int* channels,
632 int* samplerate,
633 int* bitspersample,
634 int64_t* totaltime,
635 int* bitrate,
636 AudioEngineDataFormat* format,
638 {
639 CInstanceAudioDecoder* thisClass = static_cast<CInstanceAudioDecoder*>(hdl);
640
641 std::vector<AudioEngineChannel> channelList;
642
643 bool ret = thisClass->Init(file, filecache, *channels, *samplerate, *bitspersample, *totaltime,
644 *bitrate, *format, channelList);
645 if (!channelList.empty())
646 {
647 if (channelList.back() != AUDIOENGINE_CH_NULL)
648 channelList.push_back(AUDIOENGINE_CH_NULL);
649
650 for (unsigned int i = 0; i < channelList.size(); ++i)
651 {
652 info[i] = channelList[i];
653 }
654 }
655 return ret;
656 }
657
658 inline static int ADDON_read_pcm(const KODI_ADDON_AUDIODECODER_HDL hdl,
659 uint8_t* buffer,
660 size_t size,
661 size_t* actualsize)
662 {
663 return static_cast<CInstanceAudioDecoder*>(hdl)->ReadPCM(buffer, size, *actualsize);
664 }
665
666 inline static int64_t ADDON_seek(const KODI_ADDON_AUDIODECODER_HDL hdl, int64_t time)
667 {
668 return static_cast<CInstanceAudioDecoder*>(hdl)->Seek(time);
669 }
670
671 inline static bool ADDON_read_tag(const KODI_ADDON_AUDIODECODER_HDL hdl,
672 const char* file,
674 {
675#ifdef _WIN32
676#pragma warning(push)
677#pragma warning(disable : 4996)
678#endif // _WIN32
680 bool ret = static_cast<CInstanceAudioDecoder*>(hdl)->ReadTag(file, cppTag);
681 if (ret)
682 {
683 tag->title = strdup(cppTag.GetTitle().c_str());
684 tag->artist = strdup(cppTag.GetArtist().c_str());
685 tag->album = strdup(cppTag.GetAlbum().c_str());
686 tag->album_artist = strdup(cppTag.GetAlbumArtist().c_str());
687 tag->media_type = strdup(cppTag.GetMediaType().c_str());
688 tag->genre = strdup(cppTag.GetGenre().c_str());
689 tag->duration = cppTag.GetDuration();
690 tag->track = cppTag.GetTrack();
691 tag->disc = cppTag.GetDisc();
692 tag->disc_subtitle = strdup(cppTag.GetDiscSubtitle().c_str());
693 tag->disc_total = cppTag.GetDiscTotal();
694 tag->release_date = strdup(cppTag.GetReleaseDate().c_str());
695 tag->lyrics = strdup(cppTag.GetLyrics().c_str());
696 tag->samplerate = cppTag.GetSamplerate();
697 tag->channels = cppTag.GetChannels();
698 tag->bitrate = cppTag.GetBitrate();
699 tag->comment = strdup(cppTag.GetComment().c_str());
700 std::string mimetype;
701 size_t size = 0;
702 const uint8_t* mem = cppTag.GetCoverArtByMem(size, mimetype);
703 if (mem && size > 0)
704 {
705 tag->cover_art_mem_mimetype = strdup(mimetype.c_str());
706 tag->cover_art_mem_size = size;
707 tag->cover_art_mem = static_cast<uint8_t*>(malloc(size));
708 memcpy(tag->cover_art_mem, mem, size);
709 }
710 else
711 {
712 tag->cover_art_path = strdup(cppTag.GetCoverArtByPath().c_str());
713 }
714 }
715 return ret;
716#ifdef _WIN32
717#pragma warning(pop)
718#endif // _WIN32
719 }
720
721 inline static int ADDON_track_count(const KODI_ADDON_AUDIODECODER_HDL hdl, const char* file)
722 {
723 return static_cast<CInstanceAudioDecoder*>(hdl)->TrackCount(file);
724 }
725};
726
727} /* namespace addon */
728} /* namespace kodi */
729#endif /* __cplusplus */
Definition AudioDecoder.h:42
Definition AudioDecoder.h:431
Definition AddonBase.h:565
Definition AddonBase.h:498
int GetDisc() const
Get disk number (if present)
Definition AudioDecoder.h:139
const std::string & GetAlbumArtist() const
Get album artist name.
Definition AudioDecoder.h:100
void SetDiscSubtitle(const std::string &discSubtitle)
Set disk subtitle name (if present) from music info.
Definition AudioDecoder.h:142
void SetAlbum(const std::string &album)
Set album name.
Definition AudioDecoder.h:91
void SetComment(const std::string &comment)
Set additional information comment (if present).
Definition AudioDecoder.h:185
const uint8_t * GetCoverArtByMem(size_t &size, std::string &mimetype) const
Get cover art data by memory.
Definition AudioDecoder.h:237
const std::string & GetCoverArtByPath() const
Get cover art image path.
Definition AudioDecoder.h:204
void SetMediaType(const std::string &mediaType)
Set the media type of the music item.
Definition AudioDecoder.h:112
void SetDuration(int duration)
Set the duration of music as integer from info.
Definition AudioDecoder.h:124
void SetLyrics(const std::string &lyrics)
Set string from lyrics.
Definition AudioDecoder.h:161
const std::string & GetDiscSubtitle() const
Get disk subtitle name (if present) from music info.
Definition AudioDecoder.h:145
const std::string & GetAlbum() const
Set album name.
Definition AudioDecoder.h:94
void SetDiscTotal(int discTotal)
Set disks amount quantity (if present) from music info as integer.
Definition AudioDecoder.h:148
void SetCoverArtByMem(const uint8_t *data, size_t size, const std::string &mimetype)
Set cover art image by memory.
Definition AudioDecoder.h:217
void SetBitrate(int bitrate)
Set related stream bitrate.
Definition AudioDecoder.h:179
void SetTitle(const std::string &title)
Set the title from music as string on info tag.
Definition AudioDecoder.h:79
const std::string & GetMediaType() const
Get the media type of the music item.
Definition AudioDecoder.h:115
const std::string & GetReleaseDate() const
Get release date as string from music info (if present).
Definition AudioDecoder.h:158
void SetDisc(int disc)
Set disk number (if present) from music info as integer.
Definition AudioDecoder.h:136
void SetArtist(const std::string &artist)
Set artist name.
Definition AudioDecoder.h:85
void SetAlbumArtist(const std::string &albumArtist)
Set album artist name.
Definition AudioDecoder.h:97
void SetChannels(int channels)
Set related stream channels amount.
Definition AudioDecoder.h:173
const std::string & GetComment() const
Get additional information comment (if present).
Definition AudioDecoder.h:188
int GetDuration() const
Get the duration of music as integer from info.
Definition AudioDecoder.h:127
void SetTrack(int track)
Set track number (if present) from music info as integer.
Definition AudioDecoder.h:130
const std::string & GetGenre() const
Get genre name from music as string if present.
Definition AudioDecoder.h:121
int GetBitrate() const
Get related stream bitrate.
Definition AudioDecoder.h:182
void SetCoverArtByPath(const std::string &path)
Set cover art image by path.
Definition AudioDecoder.h:195
void SetReleaseDate(const std::string &releaseDate)
Set release date as string from music info (if present). ISO8601 date YYYY, YYYY-MM or YYYY-MM-DD.
Definition AudioDecoder.h:155
const std::string & GetLyrics() const
Get string from lyrics.
Definition AudioDecoder.h:164
const std::string & GetTitle() const
Get title name.
Definition AudioDecoder.h:82
void SetSamplerate(int samplerate)
Set related stream samplerate.
Definition AudioDecoder.h:167
int GetDiscTotal() const
Get disks amount quantity (if present)
Definition AudioDecoder.h:151
int GetTrack() const
Get track number (if present).
Definition AudioDecoder.h:133
void SetGenre(const std::string &genre)
Set genre name from music as string if present.
Definition AudioDecoder.h:118
int GetChannels() const
Get related stream channels amount.
Definition AudioDecoder.h:176
const std::string & GetArtist() const
Get artist name.
Definition AudioDecoder.h:88
int GetSamplerate() const
Get related stream samplerate.
Definition AudioDecoder.h:170
#define KODI_ADDON_AUDIODECODER_TRACK_EXT
Identifier which is attached to stream files and with defined names in addon.xml (name="?...
Definition audiodecoder.h:26
static std::string GetTrack(const std::string &name, const std::string &trackPath, int &track)
Static auxiliary function to read the track number used from the given path.
Definition AudioDecoder.h:584
virtual bool SupportsFile(const std::string &filename)
Checks addon support given file path.
Definition AudioDecoder.h:487
virtual bool ReadTag(const std::string &file, kodi::addon::AudioDecoderInfoTag &tag)
Read tag of a file.
Definition AudioDecoder.h:554
virtual int64_t Seek(int64_t time)
Seek in output stream.
Definition AudioDecoder.h:539
virtual bool Init(const std::string &filename, unsigned int filecache, int &channels, int &samplerate, int &bitspersample, int64_t &totaltime, int &bitrate, AudioEngineDataFormat &format, std::vector< AudioEngineChannel > &channellist)=0
Initialize a decoder.
CInstanceAudioDecoder(const kodi::addon::IInstanceInfo &instance)
Audio decoder class constructor used to support multiple instance types.
Definition AudioDecoder.h:467
virtual int TrackCount(const std::string &file)
Get number of tracks in a file.
Definition AudioDecoder.h:567
virtual int ReadPCM(uint8_t *buffer, size_t size, size_t &actualsize)=0
Produce some noise.
AudioEngineChannel
Definition audio_engine.h:70
@ 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
Definition audiodecoder.h:78
Definition addon_base.h:268
Definition visualization.h:22