summaryrefslogtreecommitdiffstats
path: root/xbmc/addons/kodi-dev-kit/include/kodi/addon-instance/Inputstream.h
blob: 396b92ee478137601bf34639522f7ef8d5306ec6 (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
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
/*
 *  Copyright (C) 2005-2018 Team Kodi
 *  This file is part of Kodi - https://kodi.tv
 *
 *  SPDX-License-Identifier: GPL-2.0-or-later
 *  See LICENSES/README.md for more information.
 */

#pragma once

/*
 * Parts with a comment named "internal" are only used inside header and not
 * used or accessed direct during add-on development!
 */

#include "../AddonBase.h"
#include "../StreamCodec.h"
#include "../StreamCrypto.h"

#ifdef BUILD_KODI_ADDON
#include "../DemuxPacket.h"
#include "../InputStreamConstants.h"
#else
#include "cores/VideoPlayer/Interface/Addon/DemuxPacket.h"
#include "cores/VideoPlayer/Interface/Addon/InputStreamConstants.h"
#endif

//Increment this level always if you add features which can lead to compile failures in the addon
#define INPUTSTREAM_VERSION_LEVEL 2

#define INPUTSTREAM_MAX_STREAM_COUNT 256
#define INPUTSTREAM_MAX_STRING_NAME_SIZE 256
#define INPUTSTREAM_MAX_STRING_CODEC_SIZE 32
#define INPUTSTREAM_MAX_STRING_LANGUAGE_SIZE 64

#ifdef __cplusplus
extern "C"
{
#endif /* __cplusplus */

  /*!
   * @brief InputStream add-on capabilities. All capabilities are set to "false" as default.
   */
  struct INPUTSTREAM_CAPABILITIES
  {
    enum MASKTYPE : uint32_t
    {
      /// supports interface IDemux
      SUPPORTS_IDEMUX = (1 << 0),

      /// supports interface IPosTime
      SUPPORTS_IPOSTIME = (1 << 1),

      /// supports interface IDisplayTime
      SUPPORTS_IDISPLAYTIME = (1 << 2),

      /// supports seek
      SUPPORTS_SEEK = (1 << 3),

      /// supports pause
      SUPPORTS_PAUSE = (1 << 4),

      /// supports interface ITime
      SUPPORTS_ITIME = (1 << 5),

      /// supports interface IChapter
      SUPPORTS_ICHAPTER = (1 << 6),
    };

    /// set of supported capabilities
    uint32_t m_mask;
  };

  /*!
   * @brief structure of key/value pairs passed to addon on Open()
   */
  struct INPUTSTREAM
  {
    const char* m_strURL;
    const char* m_mimeType;

    unsigned int m_nCountInfoValues;
    struct LISTITEMPROPERTY
    {
      const char* m_strKey;
      const char* m_strValue;
    } m_ListItemProperties[STREAM_MAX_PROPERTY_COUNT];

    const char* m_libFolder;
    const char* m_profileFolder;
  };

  /*!
   * @brief Array of stream IDs
   */
  struct INPUTSTREAM_IDS
  {
    unsigned int m_streamCount;
    unsigned int m_streamIds[INPUTSTREAM_MAX_STREAM_COUNT];
  };

  /*!
   * @brief MASTERING Metadata
   */
  struct INPUTSTREAM_MASTERING_METADATA
  {
    double primary_r_chromaticity_x;
    double primary_r_chromaticity_y;
    double primary_g_chromaticity_x;
    double primary_g_chromaticity_y;
    double primary_b_chromaticity_x;
    double primary_b_chromaticity_y;
    double white_point_chromaticity_x;
    double white_point_chromaticity_y;
    double luminance_max;
    double luminance_min;
  };

  /*!
  * @brief CONTENTLIGHT Metadata
  */
  struct INPUTSTREAM_CONTENTLIGHT_METADATA
  {
    uint64_t max_cll;
    uint64_t max_fall;
  };

  /*!
   * @brief stream properties
   */
  struct INPUTSTREAM_INFO
  {
    enum STREAM_TYPE
    {
      TYPE_NONE = 0,
      TYPE_VIDEO,
      TYPE_AUDIO,
      TYPE_SUBTITLE,
      TYPE_TELETEXT,
      TYPE_RDS,
    } m_streamType;

    enum Codec_FEATURES : uint32_t
    {
      FEATURE_DECODE = 1
    };
    uint32_t m_features;

    enum STREAM_FLAGS : uint32_t
    {
      FLAG_NONE = 0x0000,
      FLAG_DEFAULT = 0x0001,
      FLAG_DUB = 0x0002,
      FLAG_ORIGINAL = 0x0004,
      FLAG_COMMENT = 0x0008,
      FLAG_LYRICS = 0x0010,
      FLAG_KARAOKE = 0x0020,
      FLAG_FORCED = 0x0040,
      FLAG_HEARING_IMPAIRED = 0x0080,
      FLAG_VISUAL_IMPAIRED = 0x0100,
    };

    // Keep in sync with AVColorSpace
    enum COLORSPACE
    {
      COLORSPACE_RGB = 0,
      COLORSPACE_BT709 = 1,
      COLORSPACE_UNSPECIFIED = 2,
      COLORSPACE_UNKNOWN = COLORSPACE_UNSPECIFIED, // compatibility
      COLORSPACE_RESERVED = 3,
      COLORSPACE_FCC = 4,
      COLORSPACE_BT470BG = 5,
      COLORSPACE_SMPTE170M = 6,
      COLORSPACE_SMPTE240M = 7,
      COLORSPACE_YCGCO = 8,
      COLORSPACE_YCOCG = COLORSPACE_YCGCO,
      COLORSPACE_BT2020_NCL = 9,
      COLORSPACE_BT2020_CL = 10,
      COLORSPACE_SMPTE2085 = 11,
      COLORSPACE_CHROMA_DERIVED_NCL = 12,
      COLORSPACE_CHROMA_DERIVED_CL = 13,
      COLORSPACE_ICTCP = 14,
      COLORSPACE_MAX
    };

    // Keep in sync with AVColorPrimaries
    enum COLORPRIMARIES : int32_t
    {
      COLORPRIMARY_RESERVED0 = 0,
      COLORPRIMARY_BT709 = 1,
      COLORPRIMARY_UNSPECIFIED = 2,
      COLORPRIMARY_RESERVED = 3,
      COLORPRIMARY_BT470M = 4,
      COLORPRIMARY_BT470BG = 5,
      COLORPRIMARY_SMPTE170M = 6,
      COLORPRIMARY_SMPTE240M = 7,
      COLORPRIMARY_FILM = 8,
      COLORPRIMARY_BT2020 = 9,
      COLORPRIMARY_SMPTE428 = 10,
      COLORPRIMARY_SMPTEST428_1 = COLORPRIMARY_SMPTE428,
      COLORPRIMARY_SMPTE431 = 11,
      COLORPRIMARY_SMPTE432 = 12,
      COLORPRIMARY_JEDEC_P22 = 22,
      COLORPRIMARY_MAX
    };

    // Keep in sync with AVColorRange
    enum COLORRANGE
    {
      COLORRANGE_UNKNOWN = 0,
      COLORRANGE_LIMITED,
      COLORRANGE_FULLRANGE,
      COLORRANGE_MAX
    };

    // keep in sync with AVColorTransferCharacteristic
    enum COLORTRC : int32_t
    {
      COLORTRC_RESERVED0 = 0,
      COLORTRC_BT709 = 1,
      COLORTRC_UNSPECIFIED = 2,
      COLORTRC_RESERVED = 3,
      COLORTRC_GAMMA22 = 4,
      COLORTRC_GAMMA28 = 5,
      COLORTRC_SMPTE170M = 6,
      COLORTRC_SMPTE240M = 7,
      COLORTRC_LINEAR = 8,
      COLORTRC_LOG = 9,
      COLORTRC_LOG_SQRT = 10,
      COLORTRC_IEC61966_2_4 = 11,
      COLORTRC_BT1361_ECG = 12,
      COLORTRC_IEC61966_2_1 = 13,
      COLORTRC_BT2020_10 = 14,
      COLORTRC_BT2020_12 = 15,
      COLORTRC_SMPTE2084 = 16,
      COLORTRC_SMPTEST2084 = COLORTRC_SMPTE2084,
      COLORTRC_SMPTE428 = 17,
      COLORTRC_SMPTEST428_1 = COLORTRC_SMPTE428,
      COLORTRC_ARIB_STD_B67 = 18,
      COLORTRC_MAX
    };

    uint32_t m_flags;

    //! @brief (optional) name of the stream, \0 for default handling
    char m_name[INPUTSTREAM_MAX_STRING_NAME_SIZE];

    //! @brief (required) name of codec according to ffmpeg
    char m_codecName[INPUTSTREAM_MAX_STRING_CODEC_SIZE];

    //! @brief (optional) internal name of codec (selectionstream info)
    char m_codecInternalName[INPUTSTREAM_MAX_STRING_CODEC_SIZE];

    //! @brief (optional) the profile of the codec
    STREAMCODEC_PROFILE m_codecProfile;

    //! @brief (required) physical index
    unsigned int m_pID;

    const uint8_t* m_ExtraData;
    unsigned int m_ExtraSize;

    //! @brief RFC 5646 language code (empty string if undefined)
    char m_language[INPUTSTREAM_MAX_STRING_LANGUAGE_SIZE];

    //! Video stream related data
    //@{

    //! @brief Scale of 1000 and a rate of 29970 will result in 29.97 fps
    unsigned int m_FpsScale;

    unsigned int m_FpsRate;

    //! @brief height of the stream reported by the demuxer
    unsigned int m_Height;

    //! @brief width of the stream reported by the demuxer
    unsigned int m_Width;

    //! @brief display aspect of stream
    float m_Aspect;

    //@}

    //! Audio stream related data
    //@{

    //! @brief (required) amount of channels
    unsigned int m_Channels;

    //! @brief (required) sample rate
    unsigned int m_SampleRate;

    //! @brief (required) bit rate
    unsigned int m_BitRate;

    //! @brief (required) bits per sample
    unsigned int m_BitsPerSample;

    unsigned int m_BlockAlign;

    //@}

    CRYPTO_INFO m_cryptoInfo;

    // new in API version 2.0.8
    //@{
    //! @brief Codec If available, the fourcc code codec
    unsigned int m_codecFourCC;

    //! @brief definition of colorspace
    COLORSPACE m_colorSpace;

    //! @brief color range if available
    COLORRANGE m_colorRange;
    //@}

    //new in API 2.0.9 / INPUTSTREAM_VERSION_LEVEL 1
    //@{
    COLORPRIMARIES m_colorPrimaries;
    COLORTRC m_colorTransferCharacteristic;
    //@}

    //! @brief mastering static Metadata
    INPUTSTREAM_MASTERING_METADATA* m_masteringMetadata;

    //! @brief content light static Metadata
    INPUTSTREAM_CONTENTLIGHT_METADATA* m_contentLightMetadata;
  };

  struct INPUTSTREAM_TIMES
  {
    time_t startTime;
    double ptsStart;
    double ptsBegin;
    double ptsEnd;
  };

  /*!
   * @brief "C" ABI Structures to transfer the methods from this to Kodi
   */

  // this are properties given to the addon on create
  // at this time we have no parameters for the addon
  typedef struct AddonProps_InputStream /* internal */
  {
    int dummy;
  } AddonProps_InputStream;

  typedef struct AddonToKodiFuncTable_InputStream /* internal */
  {
    KODI_HANDLE kodiInstance;
    DemuxPacket* (*allocate_demux_packet)(void* kodiInstance, int data_size);
    DemuxPacket* (*allocate_encrypted_demux_packet)(void* kodiInstance,
                                                    unsigned int data_size,
                                                    unsigned int encrypted_subsample_count);
    void (*free_demux_packet)(void* kodiInstance, DemuxPacket* packet);
  } AddonToKodiFuncTable_InputStream;

  struct AddonInstance_InputStream;
  typedef struct KodiToAddonFuncTable_InputStream /* internal */
  {
    KODI_HANDLE addonInstance;

    bool(__cdecl* open)(const AddonInstance_InputStream* instance, INPUTSTREAM* props);
    void(__cdecl* close)(const AddonInstance_InputStream* instance);
    const char*(__cdecl* get_path_list)(const AddonInstance_InputStream* instance);
    void(__cdecl* get_capabilities)(const AddonInstance_InputStream* instance,
                                    INPUTSTREAM_CAPABILITIES* capabilities);

    // IDemux
    struct INPUTSTREAM_IDS(__cdecl* get_stream_ids)(const AddonInstance_InputStream* instance);
    struct INPUTSTREAM_INFO(__cdecl* get_stream)(const AddonInstance_InputStream* instance,
                                                 int streamid);
    void(__cdecl* enable_stream)(const AddonInstance_InputStream* instance,
                                 int streamid,
                                 bool enable);
    bool(__cdecl* open_stream)(const AddonInstance_InputStream* instance, int streamid);
    void(__cdecl* demux_reset)(const AddonInstance_InputStream* instance);
    void(__cdecl* demux_abort)(const AddonInstance_InputStream* instance);
    void(__cdecl* demux_flush)(const AddonInstance_InputStream* instance);
    DemuxPacket*(__cdecl* demux_read)(const AddonInstance_InputStream* instance);
    bool(__cdecl* demux_seek_time)(const AddonInstance_InputStream* instance,
                                   double time,
                                   bool backwards,
                                   double* startpts);
    void(__cdecl* demux_set_speed)(const AddonInstance_InputStream* instance, int speed);
    void(__cdecl* set_video_resolution)(const AddonInstance_InputStream* instance,
                                        int width,
                                        int height);

    // IDisplayTime
    int(__cdecl* get_total_time)(const AddonInstance_InputStream* instance);
    int(__cdecl* get_time)(const AddonInstance_InputStream* instance);

    // ITime
    bool(__cdecl* get_times)(const AddonInstance_InputStream* instance, INPUTSTREAM_TIMES* times);

    // IPosTime
    bool(__cdecl* pos_time)(const AddonInstance_InputStream* instance, int ms);

    int(__cdecl* read_stream)(const AddonInstance_InputStream* instance,
                              uint8_t* buffer,
                              unsigned int bufferSize);
    int64_t(__cdecl* seek_stream)(const AddonInstance_InputStream* instance,
                                  int64_t position,
                                  int whence);
    int64_t(__cdecl* position_stream)(const AddonInstance_InputStream* instance);
    int64_t(__cdecl* length_stream)(const AddonInstance_InputStream* instance);
    bool(__cdecl* is_real_time_stream)(const AddonInstance_InputStream* instance);

    // IChapter
    int(__cdecl* get_chapter)(const AddonInstance_InputStream* instance);
    int(__cdecl* get_chapter_count)(const AddonInstance_InputStream* instance);
    const char*(__cdecl* get_chapter_name)(const AddonInstance_InputStream* instance, int ch);
    int64_t(__cdecl* get_chapter_pos)(const AddonInstance_InputStream* instance, int ch);
    bool(__cdecl* seek_chapter)(const AddonInstance_InputStream* instance, int ch);

    int(__cdecl* block_size_stream)(const AddonInstance_InputStream* instance);
  } KodiToAddonFuncTable_InputStream;

  typedef struct AddonInstance_InputStream /* internal */
  {
    AddonProps_InputStream* props;
    AddonToKodiFuncTable_InputStream* toKodi;
    KodiToAddonFuncTable_InputStream* toAddon;
  } AddonInstance_InputStream;

#ifdef __cplusplus
} /* extern "C" */

namespace kodi
{
namespace addon
{

class ATTRIBUTE_HIDDEN CInstanceInputStream : public IAddonInstance
{
public:
  explicit CInstanceInputStream(KODI_HANDLE instance, const std::string& kodiVersion = "")
    : IAddonInstance(ADDON_INSTANCE_INPUTSTREAM,
                     !kodiVersion.empty() ? kodiVersion
                                          : GetKodiTypeVersion(ADDON_INSTANCE_INPUTSTREAM))
  {
    if (CAddonBase::m_interface->globalSingleInstance != nullptr)
      throw std::logic_error("kodi::addon::CInstanceInputStream: Creation of multiple together "
                             "with single instance way is not allowed!");

    SetAddonStruct(instance, m_kodiVersion);
  }

  ~CInstanceInputStream() override = default;

  /*!
     * Open a stream.
     * @param props
     * @return True if the stream has been opened successfully, false otherwise.
     * @remarks
     */
  virtual bool Open(INPUTSTREAM& props) = 0;

  /*!
     * Close an open stream.
     * @remarks
     */
  virtual void Close() = 0;

  /*!
     * Get Capabilities of this addon.
     * @param capabilities The add-on's capabilities.
     * @remarks
     */
  virtual void GetCapabilities(INPUTSTREAM_CAPABILITIES& capabilities) = 0;

  /*!
     * Get IDs of available streams
     * @remarks
     */
  virtual INPUTSTREAM_IDS GetStreamIds() = 0;

  /*!
     * Get stream properties of a stream.
     * @param streamid unique id of stream
     * @return struc of stream properties
     * @remarks
     */
  virtual INPUTSTREAM_INFO GetStream(int streamid) = 0;

  /*!
     * Enable or disable a stream.
     * A disabled stream does not send demux packets
     * @param streamid unique id of stream
     * @param enable true for enable, false for disable
     * @remarks
     */
  virtual void EnableStream(int streamid, bool enable) = 0;

  /*!
    * Opens a stream for playback.
    * @param streamid unique id of stream
    * @remarks
    */
  virtual bool OpenStream(int streamid) = 0;

  /*!
     * Reset the demultiplexer in the add-on.
     * @remarks Required if bHandlesDemuxing is set to true.
     */
  virtual void DemuxReset() {}

  /*!
     * Abort the demultiplexer thread in the add-on.
     * @remarks Required if bHandlesDemuxing is set to true.
     */
  virtual void DemuxAbort() {}

  /*!
     * Flush all data that's currently in the demultiplexer buffer in the add-on.
     * @remarks Required if bHandlesDemuxing is set to true.
     */
  virtual void DemuxFlush() {}

  /*!
     * Read the next packet from the demultiplexer, if there is one.
     * @return The next packet.
     *         If there is no next packet, then the add-on should return the
     *         packet created by calling AllocateDemuxPacket(0) on the callback.
     *         If the stream changed and Kodi's player needs to be reinitialised,
     *         then, the add-on should call AllocateDemuxPacket(0) on the
     *         callback, and set the streamid to DMX_SPECIALID_STREAMCHANGE and
     *         return the value.
     *         The add-on should return NULL if an error occurred.
     * @remarks Return NULL if this add-on won't provide this function.
     */
  virtual DemuxPacket* DemuxRead() { return nullptr; }

  /*!
     * Notify the InputStream addon/demuxer that Kodi wishes to seek the stream by time
     * Demuxer is required to set stream to an IDR frame
     * @param time The absolute time since stream start
     * @param backwards True to seek to keyframe BEFORE time, else AFTER
     * @param startpts can be updated to point to where display should start
     * @return True if the seek operation was possible
     * @remarks Optional, and only used if addon has its own demuxer.
     */
  virtual bool DemuxSeekTime(double time, bool backwards, double& startpts) { return false; }

  /*!
     * Notify the InputStream addon/demuxer that Kodi wishes to change playback speed
     * @param speed The requested playback speed
     * @remarks Optional, and only used if addon has its own demuxer.
     */
  virtual void DemuxSetSpeed(int speed) {}

  /*!
     * Sets desired width / height
     * @param width / hight
     */
  virtual void SetVideoResolution(int width, int height) {}

  /*!
     * Totel time in ms
     * @remarks
     */
  virtual int GetTotalTime() { return -1; }

  /*!
     * Playing time in ms
     * @remarks
     */
  virtual int GetTime() { return -1; }

  /*!
    * Get current timing values in PTS scale
    * @remarks
    */
  virtual bool GetTimes(INPUTSTREAM_TIMES& times) { return false; }

  /*!
     * Positions inputstream to playing time given in ms
     * @remarks
     */
  virtual bool PosTime(int ms) { return false; }

  /*!
  * Return currently selected chapter
  * @remarks
  */
  virtual int GetChapter() { return -1; };

  /*!
  * Return number of available chapters
  * @remarks
  */
  virtual int GetChapterCount() { return 0; };

  /*!
  * Return name of chapter # ch
  * @remarks
  */
  virtual const char* GetChapterName(int ch) { return nullptr; };

  /*!
  * Return position if chapter # ch in milliseconds
  * @remarks
  */
  virtual int64_t GetChapterPos(int ch) { return 0; };

  /*!
  * Seek to the beginning of chapter # ch
  * @remarks
  */
  virtual bool SeekChapter(int ch) { return false; };

  /*!
     * Read from an open stream.
     * @param buffer The buffer to store the data in.
     * @param bufferSize The amount of bytes to read.
     * @return The amount of bytes that were actually read from the stream.
     * @remarks Return -1 if this add-on won't provide this function.
     */
  virtual int ReadStream(uint8_t* buffer, unsigned int bufferSize) { return -1; }

  /*!
     * Seek in a stream.
     * @param position The position to seek to.
     * @param whence ?
     * @return The new position.
     * @remarks Return -1 if this add-on won't provide this function.
     */
  virtual int64_t SeekStream(int64_t position, int whence = SEEK_SET) { return -1; }

  /*!
     * @return The position in the stream that's currently being read.
     * @remarks Return -1 if this add-on won't provide this function.
     */
  virtual int64_t PositionStream() { return -1; }

  /*!
     * @return The total length of the stream that's currently being read.
     * @remarks Return -1 if this add-on won't provide this function.
     */
  virtual int64_t LengthStream() { return -1; }

  /*!
     * @return Obtain the chunk size to use when reading streams.
     * @remarks Return 0 if this add-on won't provide this function.
     */
  virtual int GetBlockSize() { return 0; }

  /*!
     *  Check for real-time streaming
     *  @return true if current stream is real-time
     */
  virtual bool IsRealTimeStream() { return true; }

  /*!
     * @brief Allocate a demux packet. Free with FreeDemuxPacket
     * @param dataSize The size of the data that will go into the packet
     * @return The allocated packet
     */
  DemuxPacket* AllocateDemuxPacket(int dataSize)
  {
    return m_instanceData->toKodi->allocate_demux_packet(m_instanceData->toKodi->kodiInstance,
                                                         dataSize);
  }

  /*!
     * @brief Allocate a demux packet. Free with FreeDemuxPacket
     * @param dataSize The size of the data that will go into the packet
     * @return The allocated packet
     */
  DemuxPacket* AllocateEncryptedDemuxPacket(int dataSize, unsigned int encryptedSubsampleCount)
  {
    return m_instanceData->toKodi->allocate_encrypted_demux_packet(
        m_instanceData->toKodi->kodiInstance, dataSize, encryptedSubsampleCount);
  }

  /*!
     * @brief Free a packet that was allocated with AllocateDemuxPacket
     * @param packet The packet to free
     */
  void FreeDemuxPacket(DemuxPacket* packet)
  {
    return m_instanceData->toKodi->free_demux_packet(m_instanceData->toKodi->kodiInstance, packet);
  }

private:
  static int compareVersion(const int v1[3], const int v2[3])
  {
    for (unsigned i(0); i < 3; ++i)
      if (v1[i] != v2[i])
        return v1[i] - v2[i];
    return 0;
  }

  void SetAddonStruct(KODI_HANDLE instance, const std::string& kodiVersion)
  {
    if (instance == nullptr)
      throw std::logic_error("kodi::addon::CInstanceInputStream: Creation with empty addon "
                             "structure not allowed, table must be given from Kodi!");
    int api[3] = { 0, 0, 0 };
    sscanf(kodiVersion.c_str(), "%d.%d.%d", &api[0], &api[1], &api[2]);

    m_instanceData = static_cast<AddonInstance_InputStream*>(instance);
    m_instanceData->toAddon->addonInstance = this;
    m_instanceData->toAddon->open = ADDON_Open;
    m_instanceData->toAddon->close = ADDON_Close;
    m_instanceData->toAddon->get_capabilities = ADDON_GetCapabilities;

    m_instanceData->toAddon->get_stream_ids = ADDON_GetStreamIds;
    m_instanceData->toAddon->get_stream = ADDON_GetStream;
    m_instanceData->toAddon->enable_stream = ADDON_EnableStream;
    m_instanceData->toAddon->open_stream = ADDON_OpenStream;
    m_instanceData->toAddon->demux_reset = ADDON_DemuxReset;
    m_instanceData->toAddon->demux_abort = ADDON_DemuxAbort;
    m_instanceData->toAddon->demux_flush = ADDON_DemuxFlush;
    m_instanceData->toAddon->demux_read = ADDON_DemuxRead;
    m_instanceData->toAddon->demux_seek_time = ADDON_DemuxSeekTime;
    m_instanceData->toAddon->demux_set_speed = ADDON_DemuxSetSpeed;
    m_instanceData->toAddon->set_video_resolution = ADDON_SetVideoResolution;

    m_instanceData->toAddon->get_total_time = ADDON_GetTotalTime;
    m_instanceData->toAddon->get_time = ADDON_GetTime;

    m_instanceData->toAddon->get_times = ADDON_GetTimes;
    m_instanceData->toAddon->pos_time = ADDON_PosTime;

    m_instanceData->toAddon->read_stream = ADDON_ReadStream;
    m_instanceData->toAddon->seek_stream = ADDON_SeekStream;
    m_instanceData->toAddon->position_stream = ADDON_PositionStream;
    m_instanceData->toAddon->length_stream = ADDON_LengthStream;
    m_instanceData->toAddon->is_real_time_stream = ADDON_IsRealTimeStream;

    // Added on 2.0.10
    m_instanceData->toAddon->get_chapter = ADDON_GetChapter;
    m_instanceData->toAddon->get_chapter_count = ADDON_GetChapterCount;
    m_instanceData->toAddon->get_chapter_name = ADDON_GetChapterName;
    m_instanceData->toAddon->get_chapter_pos = ADDON_GetChapterPos;
    m_instanceData->toAddon->seek_chapter = ADDON_SeekChapter;

    // Added on 2.0.12
    m_instanceData->toAddon->block_size_stream = ADDON_GetBlockSize;

    /*
    // Way to include part on new API version
    int minPartVersion[3] = { 3, 0, 0 };
    if (compareVersion(api, minPartVersion) >= 0)
    {

    }
    */
  }

  inline static bool ADDON_Open(const AddonInstance_InputStream* instance, INPUTSTREAM* props)
  {
    return static_cast<CInstanceInputStream*>(instance->toAddon->addonInstance)->Open(*props);
  }

  inline static void ADDON_Close(const AddonInstance_InputStream* instance)
  {
    static_cast<CInstanceInputStream*>(instance->toAddon->addonInstance)->Close();
  }

  inline static void ADDON_GetCapabilities(const AddonInstance_InputStream* instance,
                                           INPUTSTREAM_CAPABILITIES* capabilities)
  {
    static_cast<CInstanceInputStream*>(instance->toAddon->addonInstance)
        ->GetCapabilities(*capabilities);
  }


  // IDemux
  inline static struct INPUTSTREAM_IDS ADDON_GetStreamIds(const AddonInstance_InputStream* instance)
  {
    return static_cast<CInstanceInputStream*>(instance->toAddon->addonInstance)->GetStreamIds();
  }

  inline static struct INPUTSTREAM_INFO ADDON_GetStream(const AddonInstance_InputStream* instance,
                                                        int streamid)
  {
    return static_cast<CInstanceInputStream*>(instance->toAddon->addonInstance)
        ->GetStream(streamid);
  }

  inline static void ADDON_EnableStream(const AddonInstance_InputStream* instance,
                                        int streamid,
                                        bool enable)
  {
    static_cast<CInstanceInputStream*>(instance->toAddon->addonInstance)
        ->EnableStream(streamid, enable);
  }

  inline static bool ADDON_OpenStream(const AddonInstance_InputStream* instance, int streamid)
  {
    return static_cast<CInstanceInputStream*>(instance->toAddon->addonInstance)
        ->OpenStream(streamid);
  }

  inline static void ADDON_DemuxReset(const AddonInstance_InputStream* instance)
  {
    static_cast<CInstanceInputStream*>(instance->toAddon->addonInstance)->DemuxReset();
  }

  inline static void ADDON_DemuxAbort(const AddonInstance_InputStream* instance)
  {
    static_cast<CInstanceInputStream*>(instance->toAddon->addonInstance)->DemuxAbort();
  }

  inline static void ADDON_DemuxFlush(const AddonInstance_InputStream* instance)
  {
    static_cast<CInstanceInputStream*>(instance->toAddon->addonInstance)->DemuxFlush();
  }

  inline static DemuxPacket* ADDON_DemuxRead(const AddonInstance_InputStream* instance)
  {
    return static_cast<CInstanceInputStream*>(instance->toAddon->addonInstance)->DemuxRead();
  }

  inline static bool ADDON_DemuxSeekTime(const AddonInstance_InputStream* instance,
                                         double time,
                                         bool backwards,
                                         double* startpts)
  {
    return static_cast<CInstanceInputStream*>(instance->toAddon->addonInstance)
        ->DemuxSeekTime(time, backwards, *startpts);
  }

  inline static void ADDON_DemuxSetSpeed(const AddonInstance_InputStream* instance, int speed)
  {
    static_cast<CInstanceInputStream*>(instance->toAddon->addonInstance)->DemuxSetSpeed(speed);
  }

  inline static void ADDON_SetVideoResolution(const AddonInstance_InputStream* instance,
                                              int width,
                                              int height)
  {
    static_cast<CInstanceInputStream*>(instance->toAddon->addonInstance)
        ->SetVideoResolution(width, height);
  }


  // IDisplayTime
  inline static int ADDON_GetTotalTime(const AddonInstance_InputStream* instance)
  {
    return static_cast<CInstanceInputStream*>(instance->toAddon->addonInstance)->GetTotalTime();
  }

  inline static int ADDON_GetTime(const AddonInstance_InputStream* instance)
  {
    return static_cast<CInstanceInputStream*>(instance->toAddon->addonInstance)->GetTime();
  }

  // ITime
  inline static bool ADDON_GetTimes(const AddonInstance_InputStream* instance,
                                    INPUTSTREAM_TIMES* times)
  {
    return static_cast<CInstanceInputStream*>(instance->toAddon->addonInstance)->GetTimes(*times);
  }

  // IPosTime
  inline static bool ADDON_PosTime(const AddonInstance_InputStream* instance, int ms)
  {
    return static_cast<CInstanceInputStream*>(instance->toAddon->addonInstance)->PosTime(ms);
  }

  inline static int ADDON_GetChapter(const AddonInstance_InputStream* instance)
  {
    return static_cast<CInstanceInputStream*>(instance->toAddon->addonInstance)->GetChapter();
  }

  inline static int ADDON_GetChapterCount(const AddonInstance_InputStream* instance)
  {
    return static_cast<CInstanceInputStream*>(instance->toAddon->addonInstance)->GetChapterCount();
  }

  inline static const char* ADDON_GetChapterName(const AddonInstance_InputStream* instance, int ch)
  {
    return static_cast<CInstanceInputStream*>(instance->toAddon->addonInstance)->GetChapterName(ch);
  }

  inline static int64_t ADDON_GetChapterPos(const AddonInstance_InputStream* instance, int ch)
  {
    return static_cast<CInstanceInputStream*>(instance->toAddon->addonInstance)->GetChapterPos(ch);
  }

  inline static bool ADDON_SeekChapter(const AddonInstance_InputStream* instance, int ch)
  {
    return static_cast<CInstanceInputStream*>(instance->toAddon->addonInstance)->SeekChapter(ch);
  }

  inline static int ADDON_ReadStream(const AddonInstance_InputStream* instance,
                                     uint8_t* buffer,
                                     unsigned int bufferSize)
  {
    return static_cast<CInstanceInputStream*>(instance->toAddon->addonInstance)
        ->ReadStream(buffer, bufferSize);
  }

  inline static int64_t ADDON_SeekStream(const AddonInstance_InputStream* instance,
                                         int64_t position,
                                         int whence)
  {
    return static_cast<CInstanceInputStream*>(instance->toAddon->addonInstance)
        ->SeekStream(position, whence);
  }

  inline static int64_t ADDON_PositionStream(const AddonInstance_InputStream* instance)
  {
    return static_cast<CInstanceInputStream*>(instance->toAddon->addonInstance)->PositionStream();
  }

  inline static int64_t ADDON_LengthStream(const AddonInstance_InputStream* instance)
  {
    return static_cast<CInstanceInputStream*>(instance->toAddon->addonInstance)->LengthStream();
  }

  inline static int ADDON_GetBlockSize(const AddonInstance_InputStream* instance)
  {
    return static_cast<CInstanceInputStream*>(instance->toAddon->addonInstance)->GetBlockSize();
  }

  inline static bool ADDON_IsRealTimeStream(const AddonInstance_InputStream* instance)
  {
    return static_cast<CInstanceInputStream*>(instance->toAddon->addonInstance)->IsRealTimeStream();
  }

  AddonInstance_InputStream* m_instanceData;
};

} /* namespace addon */
} /* namespace kodi */

#endif /* __cplusplus */