aboutsummaryrefslogtreecommitdiff
path: root/xbmc/application/ApplicationStackHelper.cpp
blob: 515c52b5aa863d744e36fc8805c14109e6321f65 (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
/*
 *  Copyright (C) 2017-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.
 */

#include "ApplicationStackHelper.h"

#include "FileItem.h"
#include "FileItemList.h"
#include "URL.h"
#include "Util.h"
#include "cores/VideoPlayer/DVDFileInfo.h"
#include "filesystem/StackDirectory.h"
#include "utils/URIUtils.h"
#include "utils/log.h"
#include "video/VideoDatabase.h"

#include <utility>

using namespace XFILE;

CApplicationStackHelper::CApplicationStackHelper(void)
  : m_currentStack(new CFileItemList)
{
}

void CApplicationStackHelper::Clear()
{
  m_currentStackPosition = 0;
  m_currentStack->Clear();
}

void CApplicationStackHelper::OnPlayBackStarted(const CFileItem& item)
{
  std::unique_lock<CCriticalSection> lock(m_critSection);

  // time to clean up stack map
  if (!HasRegisteredStack(item))
    m_stackmap.clear();
  else
  {
    auto stack = GetRegisteredStack(item);
    Stackmap::iterator itr = m_stackmap.begin();
    while (itr != m_stackmap.end())
    {
      if (itr->second->m_pStack != stack)
      {
        itr = m_stackmap.erase(itr);
      }
      else
      {
        ++itr;
      }
    }
  }
}

bool CApplicationStackHelper::InitializeStack(const CFileItem & item)
{
  if (!item.IsStack())
    return false;

  auto stack = std::make_shared<CFileItem>(item);

  Clear();
  // read and determine kind of stack
  CStackDirectory dir;
  CURL path{item.GetDynPath()};
  if (!dir.GetDirectory(path, *m_currentStack) || m_currentStack->IsEmpty())
    return false;
  for (int i = 0; i < m_currentStack->Size(); i++)
  {
    // keep cross-references between stack parts and the stack
    SetRegisteredStack(GetStackPartFileItem(i), stack);
    SetRegisteredStackPartNumber(GetStackPartFileItem(i), i);
  }
  m_currentStackIsDiscImageStack = URIUtils::IsDiscImageStack(item.GetDynPath());

  return true;
}

std::optional<int64_t> CApplicationStackHelper::InitializeStackStartPartAndOffset(
    const CFileItem& item)
{
  CVideoDatabase dbs;
  int64_t startoffset = 0;

  // case 1: stacked ISOs
  if (m_currentStackIsDiscImageStack)
  {
    // first assume values passed to the stack
    int selectedFile = item.m_lStartPartNumber;
    startoffset = item.GetStartOffset();

    // check if we instructed the stack to resume from default
    if (startoffset == STARTOFFSET_RESUME) // selected file is not specified, pick the 'last' resume point
    {
      if (dbs.Open())
      {
        CBookmark bookmark;
        std::string path = item.GetDynPath();
        if (item.HasProperty("original_listitem_url") && URIUtils::IsPlugin(item.GetProperty("original_listitem_url").asString()))
          path = item.GetProperty("original_listitem_url").asString();
        if (dbs.GetResumeBookMark(path, bookmark))
        {
          startoffset = CUtil::ConvertSecsToMilliSecs(bookmark.timeInSeconds);
          selectedFile = bookmark.partNumber;
        }
        dbs.Close();
      }
      else
        CLog::LogF(LOGERROR, "Cannot open VideoDatabase");
    }

    // make sure that the selected part is within the boundaries
    if (selectedFile <= 0)
    {
      CLog::LogF(LOGWARNING, "Selected part {} out of range, playing part 1", selectedFile);
      selectedFile = 1;
    }
    else if (selectedFile > m_currentStack->Size())
    {
      CLog::LogF(LOGWARNING, "Selected part {} out of range, playing part {}", selectedFile,
                 m_currentStack->Size());
      selectedFile = m_currentStack->Size();
    }

    // set startoffset in selected item, track stack item for updating purposes, and finally play disc part
    m_currentStackPosition = selectedFile - 1;
    startoffset = startoffset > 0 ? STARTOFFSET_RESUME : 0;
  }
  // case 2: all other stacks
  else
  {
    // see if we have the info in the database
    //! @todo If user changes the time speed (FPS via framerate conversion stuff)
    //!       then these times will be wrong.
    //!       Also, this is really just a hack for the slow load up times we have
    //!       A much better solution is a fast reader of FPS and fileLength
    //!       that we can use on a file to get it's time.
    std::vector<uint64_t> times;
    bool haveTimes(false);

    if (dbs.Open())
    {
      haveTimes = dbs.GetStackTimes(item.GetDynPath(), times);
      dbs.Close();
    }

    // calculate the total time of the stack
    uint64_t totalTimeMs = 0;
    for (int i = 0; i < m_currentStack->Size(); i++)
    {
      if (haveTimes)
      {
        // set end time in every part
        GetStackPartFileItem(i).SetEndOffset(times[i]);
      }
      else
      {
        int duration;
        if (!CDVDFileInfo::GetFileDuration(GetStackPartFileItem(i).GetDynPath(), duration))
        {
          m_currentStack->Clear();
          return std::nullopt;
        }
        totalTimeMs += duration;
        // set end time in every part
        GetStackPartFileItem(i).SetEndOffset(totalTimeMs);
        times.push_back(totalTimeMs);
      }
      // set start time in every part
      SetRegisteredStackPartStartTimeMs(GetStackPartFileItem(i), GetStackPartStartTimeMs(i));
    }
    // set total time in every part
    totalTimeMs = GetStackTotalTimeMs();
    for (int i = 0; i < m_currentStack->Size(); i++)
      SetRegisteredStackTotalTimeMs(GetStackPartFileItem(i), totalTimeMs);

    uint64_t msecs = item.GetStartOffset();

    if (!haveTimes || item.GetStartOffset() == STARTOFFSET_RESUME)
    {
      if (dbs.Open())
      {
        // have our times now, so update the dB
        if (!haveTimes && !times.empty())
          dbs.SetStackTimes(item.GetDynPath(), times);

        if (item.GetStartOffset() == STARTOFFSET_RESUME)
        {
          // can only resume seek here, not dvdstate
          CBookmark bookmark;
          std::string path = item.GetDynPath();
          if (item.HasProperty("original_listitem_url") && URIUtils::IsPlugin(item.GetProperty("original_listitem_url").asString()))
            path = item.GetProperty("original_listitem_url").asString();
          if (dbs.GetResumeBookMark(path, bookmark))
            msecs = static_cast<uint64_t>(bookmark.timeInSeconds * 1000);
          else
            msecs = 0;
        }
        dbs.Close();
      }
    }

    m_currentStackPosition = GetStackPartNumberAtTimeMs(msecs);
    startoffset = msecs - GetStackPartStartTimeMs(m_currentStackPosition);
  }
  return startoffset;
}

bool CApplicationStackHelper::IsPlayingISOStack() const
{
  return m_currentStack->Size() > 0 && m_currentStackIsDiscImageStack;
}

bool CApplicationStackHelper::IsPlayingRegularStack() const
{
  return m_currentStack->Size() > 0 && !m_currentStackIsDiscImageStack;
}

bool CApplicationStackHelper::HasNextStackPartFileItem() const
{
  return m_currentStackPosition < m_currentStack->Size() - 1;
}

uint64_t CApplicationStackHelper::GetStackPartEndTimeMs(int partNumber) const
{
  return GetStackPartFileItem(partNumber).GetEndOffset();
}

uint64_t CApplicationStackHelper::GetStackTotalTimeMs() const
{
  return GetStackPartEndTimeMs(m_currentStack->Size() - 1);
}

int CApplicationStackHelper::GetStackPartNumberAtTimeMs(uint64_t msecs)
{
  if (msecs > 0)
  {
    // work out where to seek to
    for (int partNumber = 0; partNumber < m_currentStack->Size(); partNumber++)
    {
      if (msecs < GetStackPartEndTimeMs(partNumber))
        return partNumber;
    }
  }
  return 0;
}

void CApplicationStackHelper::ClearAllRegisteredStackInformation()
{
  m_stackmap.clear();
}

std::shared_ptr<const CFileItem> CApplicationStackHelper::GetRegisteredStack(
    const CFileItem& item) const
{
  return GetStackPartInformation(item.GetDynPath())->m_pStack;
}

bool CApplicationStackHelper::HasRegisteredStack(const CFileItem& item) const
{
  const auto it = m_stackmap.find(item.GetDynPath());
  return it != m_stackmap.end() && it->second != nullptr;
}

void CApplicationStackHelper::SetRegisteredStack(const CFileItem& item,
                                                 std::shared_ptr<CFileItem> stackItem)
{
  GetStackPartInformation(item.GetDynPath())->m_pStack = std::move(stackItem);
}

CFileItem& CApplicationStackHelper::GetStackPartFileItem(int partNumber)
{
  return *(*m_currentStack)[partNumber];
}

const CFileItem& CApplicationStackHelper::GetStackPartFileItem(int partNumber) const
{
  return *(*m_currentStack)[partNumber];
}

int CApplicationStackHelper::GetRegisteredStackPartNumber(const CFileItem& item)
{
  return GetStackPartInformation(item.GetDynPath())->m_lStackPartNumber;
}

void CApplicationStackHelper::SetRegisteredStackPartNumber(const CFileItem& item, int partNumber)
{
  GetStackPartInformation(item.GetDynPath())->m_lStackPartNumber = partNumber;
}

uint64_t CApplicationStackHelper::GetRegisteredStackPartStartTimeMs(const CFileItem& item) const
{
  return GetStackPartInformation(item.GetDynPath())->m_lStackPartStartTimeMs;
}

void CApplicationStackHelper::SetRegisteredStackPartStartTimeMs(const CFileItem& item, uint64_t startTime)
{
  GetStackPartInformation(item.GetDynPath())->m_lStackPartStartTimeMs = startTime;
}

uint64_t CApplicationStackHelper::GetRegisteredStackTotalTimeMs(const CFileItem& item) const
{
  return GetStackPartInformation(item.GetDynPath())->m_lStackTotalTimeMs;
}

void CApplicationStackHelper::SetRegisteredStackTotalTimeMs(const CFileItem& item, uint64_t totalTime)
{
  GetStackPartInformation(item.GetDynPath())->m_lStackTotalTimeMs = totalTime;
}

CApplicationStackHelper::StackPartInformationPtr CApplicationStackHelper::GetStackPartInformation(
    const std::string& key)
{
  if (m_stackmap.count(key) == 0)
  {
    StackPartInformationPtr value(new StackPartInformation());
    m_stackmap[key] = value;
  }
  return m_stackmap[key];
}

CApplicationStackHelper::StackPartInformationPtr CApplicationStackHelper::GetStackPartInformation(
    const std::string& key) const
{
  const auto it = m_stackmap.find(key);
  if (it == m_stackmap.end())
    return std::make_shared<StackPartInformation>();
  return it->second;
}