Kodi Development 22.0
for Binary and Script based Add-Ons
 
Loading...
Searching...
No Matches
AudioEncoder.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 "../c-api/addon-instance/audioencoder.h"
13
14#ifdef __cplusplus
15
16#include <stdexcept>
17
18namespace kodi
19{
20namespace addon
21{
22
23class CInstanceAudioEncoder;
24
25//==============================================================================
39class ATTR_DLL_LOCAL AudioEncoderInfoTag
40{
41public:
43 AudioEncoderInfoTag() = default;
73
75 void SetTitle(const std::string& title) { m_title = title; }
76
78 const std::string& GetTitle() const { return m_title; }
79
81 void SetArtist(const std::string& artist) { m_artist = artist; }
82
84 const std::string& GetArtist() const { return m_artist; }
85
87 void SetAlbum(const std::string& album) { m_album = album; }
88
90 const std::string& GetAlbum() const { return m_album; }
91
93 void SetAlbumArtist(const std::string& albumArtist) { m_album_artist = albumArtist; }
94
96 const std::string& GetAlbumArtist() const { return m_album_artist; }
97
108 void SetMediaType(const std::string& mediaType) { m_media_type = mediaType; }
109
111 const std::string& GetMediaType() const { return m_media_type; }
112
114 void SetGenre(const std::string& genre) { m_genre = genre; }
115
117 const std::string& GetGenre() const { return m_genre; }
118
120 void SetDuration(int duration) { m_duration = duration; }
121
123 int GetDuration() const { return m_duration; }
124
126 void SetTrack(int track) { m_track = track; }
127
129 int GetTrack() const { return m_track; }
130
132 void SetDisc(int disc) { m_disc = disc; }
133
135 int GetDisc() const { return m_disc; }
136
138 void SetDiscSubtitle(const std::string& discSubtitle) { m_disc_subtitle = discSubtitle; }
139
141 const std::string& GetDiscSubtitle() const { return m_disc_subtitle; }
142
144 void SetDiscTotal(int discTotal) { m_disc_total = discTotal; }
145
147 int GetDiscTotal() const { return m_disc_total; }
148
151 void SetReleaseDate(const std::string& releaseDate) { m_release_date = releaseDate; }
152
154 const std::string& GetReleaseDate() const { return m_release_date; }
155
157 void SetLyrics(const std::string& lyrics) { m_lyrics = lyrics; }
158
160 const std::string& GetLyrics() const { return m_lyrics; }
161
163 void SetSamplerate(int samplerate) { m_samplerate = samplerate; }
164
166 int GetSamplerate() const { return m_samplerate; }
167
169 void SetChannels(int channels) { m_channels = channels; }
170
172 int GetChannels() const { return m_channels; }
173
175 void SetBitsPerSample(int bits_per_sample) { m_bits_per_sample = bits_per_sample; }
176
178 int GetBitsPerSample() const { return m_bits_per_sample; }
179
181 void SetTrackLength(int track_length) { m_track_length = track_length; }
182
184 int GetTrackLength() const { return m_track_length; }
185
187 void SetComment(const std::string& comment) { m_comment = comment; }
188
190 const std::string& GetComment() const { return m_comment; }
191
193
194private:
195 friend class CInstanceAudioEncoder;
196
198 {
199 if (tag->title)
200 m_title = tag->title;
201 if (tag->artist)
202 m_artist = tag->artist;
203 if (tag->album)
204 m_album = tag->album;
205 if (tag->album_artist)
206 m_album_artist = tag->album_artist;
207 if (tag->media_type)
208 m_media_type = tag->media_type;
209 if (tag->genre)
210 m_genre = tag->genre;
211 m_duration = tag->duration;
212 m_track = tag->track;
213 m_disc = tag->disc;
214 if (tag->artist)
215 m_disc_subtitle = tag->artist;
216 m_disc_total = tag->disc_total;
217 if (tag->release_date)
218 m_release_date = tag->release_date;
219 if (tag->lyrics)
220 m_lyrics = tag->lyrics;
221 m_samplerate = tag->samplerate;
222 m_channels = tag->channels;
223 m_bits_per_sample = tag->bits_per_sample;
224 m_track_length = tag->track_length;
225 if (tag->comment)
226 m_comment = tag->comment;
227 }
228
229 std::string m_title;
230 std::string m_artist;
231 std::string m_album;
232 std::string m_album_artist;
233 std::string m_media_type;
234 std::string m_genre;
235 int m_duration{0};
236 int m_track{0};
237 int m_disc{0};
238 std::string m_disc_subtitle;
239 int m_disc_total{0};
240 std::string m_release_date;
241 std::string m_lyrics;
242 int m_samplerate{0};
243 int m_channels{0};
244 int m_bits_per_sample{0};
245 int m_track_length{0};
246 std::string m_comment;
247};
249//------------------------------------------------------------------------------
250
251//==============================================================================
360class ATTR_DLL_LOCAL CInstanceAudioEncoder : public IAddonInstance
361{
362public:
363 //============================================================================
397 : IAddonInstance(instance)
398 {
399 if (CPrivateBase::m_interface->globalSingleInstance != nullptr)
400 throw std::logic_error("kodi::addon::CInstanceAudioEncoder: Creation of multiple together "
401 "with single instance way is not allowed!");
402
403 SetAddonStruct(instance);
404 }
405 //----------------------------------------------------------------------------
406
407 //============================================================================
414 virtual bool Start(const kodi::addon::AudioEncoderInfoTag& tag) = 0;
415 //----------------------------------------------------------------------------
416
417 //============================================================================
425 virtual ssize_t Encode(const uint8_t* pbtStream, size_t numBytesRead) = 0;
426 //----------------------------------------------------------------------------
427
428 //============================================================================
434 virtual bool Finish() { return true; }
435 //----------------------------------------------------------------------------
436
437 //============================================================================
447 ssize_t Write(const uint8_t* data, size_t length)
448 {
449 return m_kodi->write(m_kodi->kodiInstance, data, length);
450 }
451 //----------------------------------------------------------------------------
452
453 //============================================================================
474 ssize_t Seek(ssize_t position, int whence = SEEK_SET)
475 {
476 return m_kodi->seek(m_kodi->kodiInstance, position, whence);
477 }
478 //----------------------------------------------------------------------------
479
480private:
481 void SetAddonStruct(KODI_ADDON_INSTANCE_STRUCT* instance)
482 {
483 instance->hdl = this;
484 instance->audioencoder->toAddon->start = ADDON_start;
485 instance->audioencoder->toAddon->encode = ADDON_encode;
486 instance->audioencoder->toAddon->finish = ADDON_finish;
487 m_kodi = instance->audioencoder->toKodi;
488 }
489
490 inline static bool ADDON_start(const KODI_ADDON_AUDIOENCODER_HDL hdl,
491 const struct KODI_ADDON_AUDIOENCODER_INFO_TAG* tag)
492 {
493 return static_cast<CInstanceAudioEncoder*>(hdl)->Start(tag);
494 }
495
496 inline static ssize_t ADDON_encode(const KODI_ADDON_AUDIOENCODER_HDL hdl,
497 const uint8_t* pbtStream,
498 size_t num_bytes_read)
499 {
500 return static_cast<CInstanceAudioEncoder*>(hdl)->Encode(pbtStream, num_bytes_read);
501 }
502
503 inline static bool ADDON_finish(const KODI_ADDON_AUDIOENCODER_HDL hdl)
504 {
505 return static_cast<CInstanceAudioEncoder*>(hdl)->Finish();
506 }
507
508 AddonToKodiFuncTable_AudioEncoder* m_kodi{nullptr};
509};
510
511} /* namespace addon */
512} /* namespace kodi */
513
514#endif /* __cplusplus */
Definition AudioEncoder.h:40
Definition AudioEncoder.h:361
Definition AddonBase.h:565
Definition AddonBase.h:498
int GetDisc() const
Get disk number (if present)
Definition AudioEncoder.h:135
const std::string & GetAlbumArtist() const
Get album artist name.
Definition AudioEncoder.h:96
void SetDiscSubtitle(const std::string &discSubtitle)
Set disk subtitle name (if present) from music info.
Definition AudioEncoder.h:138
void SetAlbum(const std::string &album)
Set album name.
Definition AudioEncoder.h:87
void SetComment(const std::string &comment)
Set additional information comment (if present).
Definition AudioEncoder.h:187
void SetMediaType(const std::string &mediaType)
Set the media type of the music item.
Definition AudioEncoder.h:108
void SetDuration(int duration)
Set the duration of music as integer from info.
Definition AudioEncoder.h:120
void SetLyrics(const std::string &lyrics)
Set string from lyrics.
Definition AudioEncoder.h:157
const std::string & GetDiscSubtitle() const
Get disk subtitle name (if present) from music info.
Definition AudioEncoder.h:141
const std::string & GetAlbum() const
Get album name.
Definition AudioEncoder.h:90
void SetDiscTotal(int discTotal)
Set disks amount quantity (if present) from music info as integer.
Definition AudioEncoder.h:144
void SetTitle(const std::string &title)
Set the title from music as string on info tag.
Definition AudioEncoder.h:75
const std::string & GetMediaType() const
Get the media type of the music item.
Definition AudioEncoder.h:111
const std::string & GetReleaseDate() const
Get release date as string from music info (if present).
Definition AudioEncoder.h:154
void SetDisc(int disc)
Set disk number (if present) from music info as integer.
Definition AudioEncoder.h:132
void SetTrackLength(int track_length)
Set related stream track length.
Definition AudioEncoder.h:181
void SetArtist(const std::string &artist)
Set artist name.
Definition AudioEncoder.h:81
void SetAlbumArtist(const std::string &albumArtist)
Set album artist name.
Definition AudioEncoder.h:93
void SetChannels(int channels)
Set related stream channels amount.
Definition AudioEncoder.h:169
const std::string & GetComment() const
Get additional information comment (if present).
Definition AudioEncoder.h:190
int GetBitsPerSample() const
Get related stream bits per sample.
Definition AudioEncoder.h:178
int GetDuration() const
Get the duration of music as integer from info.
Definition AudioEncoder.h:123
void SetTrack(int track)
Set track number (if present) from music info as integer.
Definition AudioEncoder.h:126
const std::string & GetGenre() const
Get genre name from music as string if present.
Definition AudioEncoder.h:117
int GetTrackLength() const
Get related stream track length.
Definition AudioEncoder.h:184
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 AudioEncoder.h:151
const std::string & GetLyrics() const
Get string from lyrics.
Definition AudioEncoder.h:160
void SetBitsPerSample(int bits_per_sample)
Set related stream bits per sample.
Definition AudioEncoder.h:175
const std::string & GetTitle() const
Get title name.
Definition AudioEncoder.h:78
void SetSamplerate(int samplerate)
Set related stream samplerate.
Definition AudioEncoder.h:163
int GetDiscTotal() const
Get disks amount quantity (if present)
Definition AudioEncoder.h:147
int GetTrack() const
Get track number (if present).
Definition AudioEncoder.h:129
void SetGenre(const std::string &genre)
Set genre name from music as string if present.
Definition AudioEncoder.h:114
int GetChannels() const
Get related stream channels amount.
Definition AudioEncoder.h:172
const std::string & GetArtist() const
Get artist name.
Definition AudioEncoder.h:84
int GetSamplerate() const
Get related stream samplerate.
Definition AudioEncoder.h:166
virtual bool Finish()
Finalize encoding (optional)
Definition AudioEncoder.h:434
ssize_t Write(const uint8_t *data, size_t length)
Write block of data.
Definition AudioEncoder.h:447
virtual bool Start(const kodi::addon::AudioEncoderInfoTag &tag)=0
Start encoder (required)
ssize_t Seek(ssize_t position, int whence=SEEK_SET)
Set the file's current position.
Definition AudioEncoder.h:474
virtual ssize_t Encode(const uint8_t *pbtStream, size_t numBytesRead)=0
Encode a chunk of audio (required)
CInstanceAudioEncoder(const kodi::addon::IInstanceInfo &instance)
Audio encoder class constructor used to support multiple instances.
Definition AudioEncoder.h:396
Definition audioencoder.h:51
Definition audioencoder.h:22
Definition addon_base.h:268
Definition visualization.h:22