summaryrefslogtreecommitdiffstats
path: root/xbmc/addons/kodi-addon-dev-kit/include/kodi/xbmc_stream_utils.hpp
blob: 099776a039914537fcc0961f72326e72114e817b (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
#pragma once
/*
 *      Copyright (C) 2005-2013 Team XBMC
 *      http://xbmc.org
 *
 *  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 XBMC; see the file COPYING.  If not, see
 *  <http://www.gnu.org/licenses/>.
 *
 */

#include "xbmc_pvr_types.h"
#include <algorithm>
#include <map>

namespace ADDON
{
  /**
   * Represents a single stream. It extends the PODS to provide some operators 
   * overloads.
   */
  class XbmcPvrStream : public PVR_STREAM_PROPERTIES::PVR_STREAM
  {
  public:
    XbmcPvrStream()
    {
      Clear();
    }
    
    XbmcPvrStream(const XbmcPvrStream &other)
    {
      memcpy(this, &other, sizeof(PVR_STREAM_PROPERTIES::PVR_STREAM));
    }
    
    XbmcPvrStream& operator=(const XbmcPvrStream &other)
    {
      memcpy(this, &other, sizeof(PVR_STREAM_PROPERTIES::PVR_STREAM));
      return *this;
    }
    
    /**
     * Compares this stream based on another stream
     * @param other
     * @return
     */
    inline bool operator==(const XbmcPvrStream &other) const
    {
      return iPhysicalId == other.iPhysicalId && iCodecId == other.iCodecId;
    }

    /**
     * Compares this stream with another one so that video streams are sorted
     * before any other streams and the others are sorted by the physical ID
     * @param other
     * @return
     */
    bool operator<(const XbmcPvrStream &other) const
    {
      if (iCodecType == XBMC_CODEC_TYPE_VIDEO)
        return true;
      else if (other.iCodecType != XBMC_CODEC_TYPE_VIDEO)
        return iPhysicalId < other.iPhysicalId;
      else
        return false;
    }
    
    /**
     * Clears the stream
     */
    void Clear()
    {
      memset(this, 0, sizeof(PVR_STREAM_PROPERTIES::PVR_STREAM));
      iCodecId = XBMC_INVALID_CODEC_ID;
      iCodecType = XBMC_CODEC_TYPE_UNKNOWN;
    }
    
    /**
     * Checks whether the stream has been cleared
     * @return 
     */
    inline bool IsCleared() const
    {
      return iCodecId   == XBMC_INVALID_CODEC_ID &&
             iCodecType == XBMC_CODEC_TYPE_UNKNOWN;
    }
  };
  
  class XbmcStreamProperties
  {
  public:
    typedef std::vector<XbmcPvrStream> stream_vector;
    
    XbmcStreamProperties(void)
    {
      // make sure the vector won't have to resize itself later
      m_streamVector = new stream_vector();
      m_streamVector->reserve(PVR_STREAM_MAX_STREAMS);
    }

    virtual ~XbmcStreamProperties(void)
    {
      delete m_streamVector;
    }
    
    /**
     * Resets the streams
     */
    void Clear(void)
    {
      m_streamVector->clear();
      m_streamIndex.clear();
    }

    /**
     * Returns the index of the stream with the specified physical ID, or -1 if 
     * there no stream is found. This method is called very often which is why 
     * we keep a separate map for this.
     * @param iPhysicalId
     * @return
     */
    int GetStreamId(unsigned int iPhysicalId) const
    {
      std::map<unsigned int, int>::const_iterator it = m_streamIndex.find(iPhysicalId);
      if (it != m_streamIndex.end())
        return it->second;

      return -1;
    }
    
    /**
     * Returns the stream with the specified physical ID, or null if no such 
     * stream exists
     * @param iPhysicalId
     * @return
     */
    XbmcPvrStream* GetStreamById(unsigned int iPhysicalId) const
    {
      int position = GetStreamId(iPhysicalId);
      return position != -1 ? &m_streamVector->at(position) : NULL;
    }

    /**
     * Populates the specified stream with the stream having the specified 
     * physical ID. If the stream is not found only target stream's physical ID 
     * will be populated.
     * @param iPhysicalId
     * @param stream
     */
    void GetStreamData(unsigned int iPhysicalId, XbmcPvrStream* stream)
    {
      XbmcPvrStream *foundStream = GetStreamById(iPhysicalId);
      if (foundStream)
        *stream = *foundStream;
      else
      {
        stream->iIdentifier = -1;
        stream->iPhysicalId = iPhysicalId;
      }
    }

    /**
     * Populates props with the current streams and returns whether there are 
     * any streams at the moment or not.
     * @param props
     * @return
     */
    bool GetProperties(PVR_STREAM_PROPERTIES* props)
    {
      unsigned int i = 0;
      for (stream_vector::const_iterator it = m_streamVector->begin(); 
           it != m_streamVector->end(); ++it, ++i)
      {
        memcpy(&props->stream[i], &(*it), sizeof(PVR_STREAM_PROPERTIES::PVR_STREAM));
      }
      
      props->iStreamCount = m_streamVector->size();
      return (props->iStreamCount > 0);
    }

    /**
     * Merges new streams into the current list of streams. Identical streams 
     * will retain their respective indexes and new streams will replace unused 
     * indexes or be appended.
     * @param newStreams
     */
    void UpdateStreams(stream_vector &newStreams)
    {
      // sort the new streams
      std::sort(newStreams.begin(), newStreams.end());
      
      // ensure we never have more than PVR_STREAMS_MAX_STREAMS streams
      if (newStreams.size() > PVR_STREAM_MAX_STREAMS)
      {
        while (newStreams.size() > PVR_STREAM_MAX_STREAMS)
          newStreams.pop_back();
        
        XBMC->Log(LOG_ERROR, "%s - max amount of streams reached", __FUNCTION__);
      }
      
      stream_vector::iterator newStreamPosition;
      for (stream_vector::iterator it = m_streamVector->begin(); it != m_streamVector->end(); ++it)
      {
        newStreamPosition = std::find(newStreams.begin(), newStreams.end(), *it);

        // if the current stream no longer exists we clear it, otherwise we 
        // copy it and remove it from newStreams
        if (newStreamPosition == newStreams.end())
          it->Clear();
        else
        {
          *it = *newStreamPosition;
          newStreams.erase(newStreamPosition);
        }
      }

      // replace cleared streams with new streams
      for (stream_vector::iterator it = m_streamVector->begin();
           it != m_streamVector->end() && !newStreams.empty(); ++it)
      {
        if (it->IsCleared())
        {
          *it = newStreams.front();
          newStreams.erase(newStreams.begin());
        }
      }

      // append any remaining new streams
      m_streamVector->insert(m_streamVector->end(), newStreams.begin(), newStreams.end());
      
      // remove trailing cleared streams
      while (m_streamVector->back().IsCleared())
        m_streamVector->pop_back();
      
      // update the index
      UpdateIndex();
    }

  private:
    stream_vector               *m_streamVector;
    std::map<unsigned int, int> m_streamIndex;
    
    /**
     * Updates the stream index
     */
    void UpdateIndex()
    {
      m_streamIndex.clear();
      
      int i = 0;
      for (stream_vector::const_iterator it = m_streamVector->begin(); it != m_streamVector->end(); ++it, ++i)
        m_streamIndex[it->iPhysicalId] = i;
    }
  };
}