Repository: xbmc/xbmc
File: xbmc/interfaces/json-rpc/PlayerOperations.cpp (C++)

Code before:
    return {};

  const std::shared_ptr<const CPVRChannel> currentChannel = state->GetPlayingChannel();
  if (!currentChannel)
    return {};

  return currentChannel->GetEPGNow();
}

JSONRPC_STATUS CPlayerOperations::GetChapters(const std::string& method, ITransportLayer* transport, IClient* client, const CVariant& parameterObject, CVariant& result)
{
  // Return the chapters list of the running video
  switch (GetPlayer(parameterObject["playerid"]))
  {
    case Video:
      break;
    default: return InvalidParams;
  }
  auto& components = CServiceBroker::GetAppComponents();
  const auto appPlayer = components.GetComponent<CApplicationPlayer>();

  if (!appPlayer->IsPlayingVideo())
    return FailedToExecute; // No running video

  // Extract chapters from CApplicationPlayer
  int chapterCount = appPlayer->GetChapterCount();
  CVariant chapters(CVariant::VariantTypeArray);

  for (int i = 0; i < chapterCount; ++i)
  {
    CVariant chapter(CVariant::VariantTypeObject);
    chapter["index"] = i;
    // Chapter name
    std::string name;
    appPlayer->GetChapterName(name, i + 1);
    if (!name.empty())
      chapter["name"] = name;
    // Chapter position in seconds
    int64_t position = appPlayer->GetChapterPos(i + 1);
    if (position < 0) continue;
    chapter["time"] = position;
    chapters.push_back(chapter);
  }

  result["chapters"] = chapters;
  return OK;
}


Review comment (suggestion):
```suggestion
  const int chapterCount = appPlayer->GetChapterCount();
```

Code after:
    return {};

  const std::shared_ptr<const CPVRChannel> currentChannel = state->GetPlayingChannel();
  if (!currentChannel)
    return {};

  return currentChannel->GetEPGNow();
}

JSONRPC_STATUS CPlayerOperations::GetChapters(const std::string& method,
                                              ITransportLayer* transport,
                                              IClient* client,
                                              const CVariant& parameterObject,
                                              CVariant& result)
{
  // Return the chapters list of the running video or empty list if none
  switch (GetPlayer(parameterObject["playerid"]))
  {
    case Video:
      break;
    default:
      return InvalidParams;
  }
  auto& components = CServiceBroker::GetAppComponents();
  const auto appPlayer = components.GetComponent<CApplicationPlayer>();

  if (!appPlayer->IsPlayingVideo())
    return FailedToExecute; // No running video

  // Extract chapters from CApplicationPlayer
  const int chapterCount = appPlayer->GetChapterCount();
  CVariant chapters(CVariant::VariantTypeArray);

  for (int i = 1; i <= chapterCount; ++i)
  {
    CVariant chapter(CVariant::VariantTypeObject);
    chapter["index"] = i;
    // Chapter name
    std::string name;
    appPlayer->GetChapterName(name, i);
    if (!name.empty())
      chapter["name"] = name;
    // Chapter position in seconds
    int64_t position = appPlayer->GetChapterPos(i);
    if (position < 0)
      continue;
    chapter["time"] = position;
    chapters.push_back(chapter);
  }

  result["chapters"] = std::move(chapters);