| 1 |
/* |
| 2 |
* Copyright (C) 2012-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 |
#include "DVDDemuxClient.h" |
| 10 |
|
| 11 |
#include "DVDDemuxUtils.h" |
| 12 |
#include "DVDInputStreams/DVDInputStream.h" |
| 13 |
#include "cores/FFmpeg.h" |
| 14 |
#include "cores/VideoPlayer/Interface/TimingConstants.h" |
| 15 |
#include "utils/log.h" |
| 16 |
|
| 17 |
#include <memory> |
| 18 |
#include <tuple> |
| 19 |
#include <type_traits> |
| 20 |
#include <utility> |
| 21 |
|
| •22 |
extern "C" |
| 23 |
{ |
| 24 |
#include <libavcodec/defs.h> |
| 25 |
} |
| 26 |
|
| 27 |
class CDemuxStreamClientInternal |
| 28 |
{ |
| 29 |
public: |
| 30 |
~CDemuxStreamClientInternal() |
| 31 |
{ |
| 32 |
DisposeParser(); |
| 33 |
} |
| 34 |
|
| 35 |
void DisposeParser() |
| 36 |
{ |
| 37 |
if (m_parser) |
| 38 |
{ |
| 39 |
av_parser_close(m_parser); |
| 40 |
m_parser = nullptr; |
| 41 |
} |
| 42 |
if (m_context) |
| 43 |
{ |
| 44 |
avcodec_free_context(&m_context); |
| 45 |
m_context = nullptr; |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
AVCodecParserContext *m_parser = nullptr; |
| 50 |
AVCodecContext *m_context = nullptr; |
| 51 |
bool m_parser_split = false; |
| 52 |
}; |
| 53 |
|
| 54 |
template <class T> |
| 55 |
class CDemuxStreamClientInternalTpl : public CDemuxStreamClientInternal, public T |
| 56 |
{ |
| 57 |
}; |
| 58 |
|
| 59 |
CDVDDemuxClient::CDVDDemuxClient() : CDVDDemux() |
| 60 |
{ |
| 61 |
m_streams.clear(); |
| 62 |
} |
| 63 |
|
| 64 |
CDVDDemuxClient::~CDVDDemuxClient() |
| 65 |
{ |
| 66 |
Dispose(); |
| 67 |
} |
| 68 |
|
| 69 |
bool CDVDDemuxClient::Open(std::shared_ptr<CDVDInputStream> pInput) |
| 70 |
{ |
| 71 |
Abort(); |
| 72 |
|
| 73 |
m_pInput = std::move(pInput); |
| 74 |
m_IDemux = std::dynamic_pointer_cast<CDVDInputStream::IDemux>(m_pInput); |
| 75 |
if (!m_IDemux) |
| 76 |
return false; |
| 77 |
|
| 78 |
if (!m_IDemux->OpenDemux()) |
| 79 |
return false; |
| 80 |
|
| 81 |
RequestStreams(); |
| 82 |
|
| 83 |
m_displayTime = 0; |
| 84 |
m_dtsAtDisplayTime = DVD_NOPTS_VALUE; |
| 85 |
return true; |
| 86 |
} |
| 87 |
|
| 88 |
void CDVDDemuxClient::Dispose() |
| 89 |
{ |
| 90 |
DisposeStreams(); |
| 91 |
|
| 92 |
m_pInput = nullptr; |
| 93 |
m_IDemux = nullptr; |
| 94 |
} |
| 95 |
|
| 96 |
void CDVDDemuxClient::DisposeStreams() |
| 97 |
{ |
| 98 |
m_streams.clear(); |
| 99 |
m_videoStreamPlaying = -1; |
| 100 |
} |
| 101 |
|
| 102 |
bool CDVDDemuxClient::Reset() |
| 103 |
{ |
| 104 |
std::shared_ptr<CDVDInputStream> pInputStream = m_pInput; |
| 105 |
Dispose(); |
| 106 |
return Open(pInputStream); |
| 107 |
} |
| 108 |
|
| 109 |
void CDVDDemuxClient::Abort() |
| 110 |
{ |
| 111 |
if (m_IDemux) |
| 112 |
m_IDemux->AbortDemux(); |
| 113 |
} |
| 114 |
|
| 115 |
void CDVDDemuxClient::Flush() |
| 116 |
{ |
| 117 |
if (m_IDemux) |
| 118 |
m_IDemux->FlushDemux(); |
| 119 |
|
| 120 |
m_displayTime = 0; |
| 121 |
m_dtsAtDisplayTime = DVD_NOPTS_VALUE; |
| 122 |
} |
| 123 |
|
| 124 |
bool CDVDDemuxClient::ParsePacket(DemuxPacket* pkt) |
| 125 |
{ |
| 126 |
bool change = false; |
| 127 |
|
| 128 |
CDemuxStream* st = GetStream(pkt->iStreamId); |
| 129 |
if (st == nullptr || st->changes < 0 || st->extraData || !CodecHasExtraData(st->codec)) |
| 130 |
return change; |
| 131 |
|
| 132 |
CDemuxStreamClientInternal* stream = dynamic_cast<CDemuxStreamClientInternal*>(st); |
| 133 |
|
| 134 |
if (stream == nullptr || |
| 135 |
stream->m_parser == nullptr) |
| 136 |
return change; |
| 137 |
|
| 138 |
if (stream->m_context == nullptr) |
| 139 |
{ |
| 140 |
const AVCodec* codec = avcodec_find_decoder(st->codec); |
| 141 |
if (codec == nullptr) |
| 142 |
{ |
| 143 |
CLog::Log(LOGERROR, "{} - can't find decoder", __FUNCTION__); |
| 144 |
stream->DisposeParser(); |
| 145 |
return change; |
| 146 |
} |
| 147 |
|
| 148 |
stream->m_context = avcodec_alloc_context3(codec); |
| 149 |
if (stream->m_context == nullptr) |
| 150 |
{ |
| 151 |
CLog::Log(LOGERROR, "{} - can't allocate context", __FUNCTION__); |
| 152 |
stream->DisposeParser(); |
| 153 |
return change; |
| 154 |
} |
| 155 |
stream->m_context->time_base.num = 1; |
| 156 |
stream->m_context->time_base.den = DVD_TIME_BASE; |
| 157 |
} |
| 158 |
|
| 159 |
if (stream->m_parser_split && stream->m_parser && stream->m_parser->parser) |
| 160 |
{ |
| 161 |
AVPacket* avpkt = av_packet_alloc(); |
| 162 |
if (!avpkt) |
| 163 |
{ |
| 164 |
CLog::LogF(LOGERROR, "av_packet_alloc failed: {}", strerror(errno)); |
| 165 |
return false; |
| 166 |
} |
| 167 |
|
| 168 |
avpkt->data = pkt->pData; |
| 169 |
avpkt->size = pkt->iSize; |
| 170 |
avpkt->dts = avpkt->pts = AV_NOPTS_VALUE; |
| 171 |
|
| 172 |
constexpr auto codecParDeleter = [](AVCodecParameters* p) { avcodec_parameters_free(&p); }; |
| 173 |
auto codecPar = std::unique_ptr<AVCodecParameters, decltype(codecParDeleter)>( |
| 174 |
avcodec_parameters_alloc(), codecParDeleter); |
| 175 |
int ret = avcodec_parameters_from_context(codecPar.get(), stream->m_context); |
| 176 |
if (ret < 0) |
| 177 |
{ |
| 178 |
CLog::LogF(LOGERROR, "avcodec_parameters_from_context failed"); |
| 179 |
return false; |
| 180 |
} |
| 181 |
|
| 182 |
FFmpegExtraData retExtraData = GetPacketExtradata(avpkt, codecPar.get()); |
| 183 |
if (retExtraData) |
| 184 |
{ |
| 185 |
st->changes++; |
| 186 |
st->disabled = false; |
| 187 |
st->extraData = std::move(retExtraData); |
| 188 |
stream->m_parser_split = false; |
| 189 |
change = true; |
| 190 |
CLog::Log(LOGDEBUG, "CDVDDemuxClient::ParsePacket - split extradata"); |
| 191 |
|
| 192 |
// Allow ffmpeg to transport codec information to stream->m_context |
| 193 |
if (!avcodec_open2(stream->m_context, stream->m_context->codec, nullptr)) |
| 194 |
{ |
| 195 |
avcodec_send_packet(stream->m_context, avpkt); |
| 196 |
} |
| 197 |
} |
| 198 |
av_packet_free(&avpkt); |
| 199 |
} |
| 200 |
|
| 201 |
uint8_t *outbuf = nullptr; |
| 202 |
int outbuf_size = 0; |
| 203 |
int len = av_parser_parse2(stream->m_parser, |
| 204 |
stream->m_context, &outbuf, &outbuf_size, |
| 205 |
pkt->pData, pkt->iSize, |
| 206 |
(int64_t)(pkt->pts * DVD_TIME_BASE), |
| 207 |
(int64_t)(pkt->dts * DVD_TIME_BASE), |
| 208 |
0); |
| 209 |
|
| 210 |
// our parse is setup to parse complete frames, so we don't care about outbufs |
| 211 |
if (len >= 0) |
| 212 |
{ |
| 213 |
if (stream->m_context->profile != st->profile && |
| •214 |
stream->m_context->profile != AV_PROFILE_UNKNOWN) |
| 215 |
{ |
| 216 |
CLog::Log(LOGDEBUG, "CDVDDemuxClient::ParsePacket - ({}) profile changed from {} to {}", st->uniqueId, st->profile, stream->m_context->profile); |
| 217 |
st->profile = stream->m_context->profile; |
| 218 |
st->changes++; |
| 219 |
st->disabled = false; |
| 220 |
} |
| 221 |
|
| •222 |
if (stream->m_context->level != st->level && stream->m_context->level != AV_LEVEL_UNKNOWN) |
| 223 |
{ |
| 224 |
CLog::Log(LOGDEBUG, "CDVDDemuxClient::ParsePacket - ({}) level changed from {} to {}", st->uniqueId, st->level, stream->m_context->level); |
| 225 |
st->level = stream->m_context->level; |
| 226 |
st->changes++; |
| 227 |
st->disabled = false; |
| 228 |
} |
| 229 |
|
| 230 |
switch (st->type) |
| 231 |
{ |
| 232 |
case STREAM_AUDIO: |
| 233 |
{ |
| 234 |
CDemuxStreamClientInternalTpl<CDemuxStreamAudio>* sta = static_cast<CDemuxStreamClientInternalTpl<CDemuxStreamAudio>*>(st); |
| 235 |
int streamChannels = stream->m_context->ch_layout.nb_channels; |
| 236 |
if (streamChannels != sta->iChannels && streamChannels != 0) |
| 237 |
{ |
| 238 |
CLog::Log(LOGDEBUG, "CDVDDemuxClient::ParsePacket - ({}) channels changed from {} to {}", |
| 239 |
st->uniqueId, sta->iChannels, streamChannels); |
| 240 |
sta->iChannels = streamChannels; |
| 241 |
sta->changes++; |
| 242 |
sta->disabled = false; |
| 243 |
} |
| 244 |
if (stream->m_context->sample_rate != sta->iSampleRate && |
| 245 |
stream->m_context->sample_rate != 0) |
| 246 |
{ |
| 247 |
CLog::Log(LOGDEBUG, "CDVDDemuxClient::ParsePacket - ({}) samplerate changed from {} to {}", st->uniqueId, sta->iSampleRate, stream->m_context->sample_rate); |
| 248 |
sta->iSampleRate = stream->m_context->sample_rate; |
| 249 |
sta->changes++; |
| 250 |
sta->disabled = false; |
| 251 |
} |
| 252 |
if (streamChannels) |
| 253 |
st->changes = -1; // stop parsing |
| 254 |
break; |
| 255 |
} |
| 256 |
case STREAM_VIDEO: |
| 257 |
{ |
| 258 |
CDemuxStreamClientInternalTpl<CDemuxStreamVideo>* stv = static_cast<CDemuxStreamClientInternalTpl<CDemuxStreamVideo>*>(st); |
| 259 |
if (stream->m_parser->width != stv->iWidth && stream->m_parser->width != 0) |
| 260 |
{ |
| 261 |
CLog::Log(LOGDEBUG, "CDVDDemuxClient::ParsePacket - ({}) width changed from {} to {}", |
| 262 |
st->uniqueId, stv->iWidth, stream->m_parser->width); |
| 263 |
stv->iWidth = stream->m_parser->width; |
| 264 |
stv->changes++; |
| 265 |
stv->disabled = false; |
| 266 |
} |
| 267 |
if (stream->m_parser->height != stv->iHeight && stream->m_parser->height != 0) |
| 268 |
{ |
| 269 |
CLog::Log(LOGDEBUG, "CDVDDemuxClient::ParsePacket - ({}) height changed from {} to {}", |
| 270 |
st->uniqueId, stv->iHeight, stream->m_parser->height); |
| 271 |
stv->iHeight = stream->m_parser->height; |
| 272 |
stv->changes++; |
| 273 |
stv->disabled = false; |
| 274 |
} |
| 275 |
if (stream->m_context->sample_aspect_ratio.num && stream->m_context->height) |
| 276 |
{ |
| 277 |
double fAspect = |
| 278 |
(av_q2d(stream->m_context->sample_aspect_ratio) * stream->m_context->width) / |
| 279 |
stream->m_context->height; |
| 280 |
if (abs(fAspect - stv->fAspect) > 0.001 && fAspect >= 0.001) |
| 281 |
{ |
| 282 |
CLog::Log(LOGDEBUG, "CDVDDemuxClient::ParsePacket - ({}) aspect changed from {} to {}", |
| 283 |
st->uniqueId, stv->fAspect, fAspect); |
| 284 |
stv->fAspect = fAspect; |
| 285 |
stv->changes++; |
| 286 |
stv->disabled = false; |
| 287 |
} |
| 288 |
} |
| 289 |
if (stream->m_context->framerate.num && |
| 290 |
(stv->iFpsRate != stream->m_context->framerate.num || |
| 291 |
stv->iFpsScale != stream->m_context->framerate.den)) |
| 292 |
{ |
| 293 |
CLog::Log(LOGDEBUG, "CDVDDemuxClient::ParsePacket - ({}) fps changed from {}/{} to {}/{}", |
| 294 |
st->uniqueId, stv->iFpsRate, stv->iFpsScale, stream->m_context->framerate.num, |
| 295 |
stream->m_context->framerate.den); |
| 296 |
stv->iFpsRate = stream->m_context->framerate.num; |
| 297 |
stv->iFpsScale = stream->m_context->framerate.den; |
| 298 |
stv->changes++; |
| 299 |
stv->disabled = false; |
| 300 |
} |
| 301 |
|
| 302 |
break; |
| 303 |
} |
| 304 |
|
| 305 |
default: |
| 306 |
break; |
| 307 |
} |
| 308 |
} |
| 309 |
else |
| 310 |
CLog::Log(LOGDEBUG, "{} - parser returned error {}", __FUNCTION__, len); |
| 311 |
|
| 312 |
return change; |
| 313 |
} |
| 314 |
|
| 315 |
DemuxPacket* CDVDDemuxClient::Read() |
| 316 |
{ |
| 317 |
if (!m_IDemux) |
| 318 |
return nullptr; |
| 319 |
|
| 320 |
if (m_packet) |
| 321 |
return m_packet.release(); |
| 322 |
|
| 323 |
m_packet.reset(m_IDemux->ReadDemux()); |
| 324 |
if (!m_packet) |
| 325 |
{ |
| 326 |
return nullptr; |
| 327 |
} |
| 328 |
|
| 329 |
if (m_packet->iStreamId == DMX_SPECIALID_STREAMINFO) |
| 330 |
{ |
| 331 |
RequestStreams(); |
| 332 |
CDVDDemuxUtils::FreeDemuxPacket(m_packet.release()); |
| 333 |
return CDVDDemuxUtils::AllocateDemuxPacket(0); |
| 334 |
} |
| 335 |
else if (m_packet->iStreamId == DMX_SPECIALID_STREAMCHANGE) |
| 336 |
{ |
| 337 |
RequestStreams(); |
| 338 |
} |
| 339 |
else if (m_packet->iStreamId >= 0 && m_streams.count(m_packet->iStreamId) > 0) |
| 340 |
{ |
| 341 |
if (ParsePacket(m_packet.get())) |
| 342 |
{ |
| 343 |
RequestStreams(); |
| 344 |
DemuxPacket *pPacket = CDVDDemuxUtils::AllocateDemuxPacket(0); |
| 345 |
pPacket->iStreamId = DMX_SPECIALID_STREAMCHANGE; |
| 346 |
pPacket->demuxerId = m_demuxerId; |
| 347 |
return pPacket; |
| 348 |
} |
| 349 |
} |
| 350 |
|
| 351 |
if (!IsVideoReady()) |
| 352 |
{ |
| 353 |
CDVDDemuxUtils::FreeDemuxPacket(m_packet.release()); |
| 354 |
DemuxPacket *pPacket = CDVDDemuxUtils::AllocateDemuxPacket(0); |
| 355 |
pPacket->demuxerId = m_demuxerId; |
| 356 |
return pPacket; |
| 357 |
} |
| 358 |
|
| 359 |
//! @todo drop this block |
| 360 |
CDVDInputStream::IDisplayTime *inputStream = m_pInput->GetIDisplayTime(); |
| 361 |
if (inputStream) |
| 362 |
{ |
| 363 |
int dispTime = inputStream->GetTime(); |
| 364 |
if (m_displayTime != dispTime) |
| 365 |
{ |
| 366 |
m_displayTime = dispTime; |
| 367 |
if (m_packet->dts != DVD_NOPTS_VALUE) |
| 368 |
{ |
| 369 |
m_dtsAtDisplayTime = m_packet->dts; |
| 370 |
} |
| 371 |
} |
| 372 |
if (m_dtsAtDisplayTime != DVD_NOPTS_VALUE && m_packet->dts != DVD_NOPTS_VALUE) |
| 373 |
{ |
| 374 |
m_packet->dispTime = m_displayTime; |
| 375 |
m_packet->dispTime += DVD_TIME_TO_MSEC(m_packet->dts - m_dtsAtDisplayTime); |
| 376 |
} |
| 377 |
} |
| 378 |
|
| 379 |
return m_packet.release(); |
| 380 |
} |
| 381 |
|
| 382 |
CDemuxStream* CDVDDemuxClient::GetStream(int iStreamId) const |
| 383 |
{ |
| 384 |
auto stream = m_streams.find(iStreamId); |
| 385 |
if (stream == m_streams.end()) |
| 386 |
return nullptr; |
| 387 |
|
| 388 |
return stream->second.get(); |
| 389 |
} |
| 390 |
|
| 391 |
std::vector<CDemuxStream*> CDVDDemuxClient::GetStreams() const |
| 392 |
{ |
| 393 |
std::vector<CDemuxStream*> streams; |
| 394 |
|
| 395 |
streams.reserve(m_streams.size()); |
| 396 |
for (auto &st : m_streams) |
| 397 |
streams.push_back(st.second.get()); |
| 398 |
|
| 399 |
return streams; |
| 400 |
} |
| 401 |
|
| 402 |
void CDVDDemuxClient::RequestStreams() |
| 403 |
{ |
| 404 |
std::map<int, std::shared_ptr<CDemuxStream>> newStreamMap; |
| 405 |
for (auto stream : m_IDemux->GetStreams()) |
| 406 |
SetStreamProps(stream, newStreamMap, false); |
| 407 |
m_streams = newStreamMap; |
| 408 |
} |
| 409 |
|
| 410 |
void CDVDDemuxClient::SetStreamProps(CDemuxStream *stream, std::map<int, std::shared_ptr<CDemuxStream>> &map, bool forceInit) |
| 411 |
{ |
| 412 |
if (!stream) |
| 413 |
{ |
| 414 |
CLog::Log(LOGERROR, "CDVDDemuxClient::RequestStream - invalid stream"); |
| 415 |
DisposeStreams(); |
| 416 |
return; |
| 417 |
} |
| 418 |
|
| 419 |
std::shared_ptr<CDemuxStream> currentStream(GetStreamInternal(stream->uniqueId)); |
| 420 |
std::shared_ptr<CDemuxStream> toStream; |
| 421 |
|
| 422 |
if (stream->type == STREAM_AUDIO) |
| 423 |
{ |
| 424 |
CDemuxStreamAudio *source = dynamic_cast<CDemuxStreamAudio*>(stream); |
| 425 |
if (!source) |
| 426 |
{ |
| 427 |
CLog::Log(LOGERROR, "CDVDDemuxClient::RequestStream - invalid audio stream with id {}", |
| 428 |
stream->uniqueId); |
| 429 |
DisposeStreams(); |
| 430 |
return; |
| 431 |
} |
| 432 |
|
| 433 |
std::shared_ptr<CDemuxStreamClientInternalTpl<CDemuxStreamAudio>> streamAudio; |
| 434 |
if (currentStream) |
| 435 |
streamAudio = std::dynamic_pointer_cast<CDemuxStreamClientInternalTpl<CDemuxStreamAudio>>(currentStream); |
| 436 |
if (forceInit || !streamAudio || streamAudio->codec != source->codec) |
| 437 |
{ |
| 438 |
streamAudio = std::make_shared<CDemuxStreamClientInternalTpl<CDemuxStreamAudio>>(); |
| 439 |
streamAudio->m_parser = av_parser_init(source->codec); |
| 440 |
if (streamAudio->m_parser) |
| 441 |
streamAudio->m_parser->flags |= PARSER_FLAG_COMPLETE_FRAMES; |
| 442 |
streamAudio->iSampleRate = source->iSampleRate; |
| 443 |
streamAudio->iChannels = source->iChannels; |
| 444 |
} |
| 445 |
|
| 446 |
streamAudio->iBlockAlign = source->iBlockAlign; |
| 447 |
streamAudio->iBitRate = source->iBitRate; |
| 448 |
streamAudio->iBitsPerSample = source->iBitsPerSample; |
| 449 |
if (source->extraData) |
| 450 |
{ |
| 451 |
streamAudio->extraData = source->extraData; |
| 452 |
} |
| 453 |
streamAudio->m_parser_split = true; |
| 454 |
streamAudio->changes++; |
| 455 |
map[stream->uniqueId] = streamAudio; |
| 456 |
toStream = streamAudio; |
| 457 |
} |
| 458 |
else if (stream->type == STREAM_VIDEO) |
| 459 |
{ |
| 460 |
CDemuxStreamVideo *source = dynamic_cast<CDemuxStreamVideo*>(stream); |
| 461 |
|
| 462 |
if (!source) |
| 463 |
{ |
| 464 |
CLog::Log(LOGERROR, "CDVDDemuxClient::RequestStream - invalid video stream with id {}", |
| 465 |
stream->uniqueId); |
| 466 |
DisposeStreams(); |
| 467 |
return; |
| 468 |
} |
| 469 |
|
| 470 |
std::shared_ptr<CDemuxStreamClientInternalTpl<CDemuxStreamVideo>> streamVideo; |
| 471 |
if (currentStream) |
| 472 |
streamVideo = std::dynamic_pointer_cast<CDemuxStreamClientInternalTpl<CDemuxStreamVideo>>(currentStream); |
| 473 |
if (forceInit || !streamVideo || streamVideo->codec != source->codec) |
| 474 |
{ |
| 475 |
streamVideo = std::make_shared<CDemuxStreamClientInternalTpl<CDemuxStreamVideo>>(); |
| 476 |
streamVideo->m_parser = av_parser_init(source->codec); |
| 477 |
if (streamVideo->m_parser) |
| 478 |
streamVideo->m_parser->flags |= PARSER_FLAG_COMPLETE_FRAMES; |
| 479 |
streamVideo->iHeight = source->iHeight; |
| 480 |
streamVideo->iWidth = source->iWidth; |
| 481 |
streamVideo->fAspect = source->fAspect; |
| 482 |
streamVideo->iFpsScale = source->iFpsScale; |
| 483 |
streamVideo->iFpsRate = source->iFpsRate; |
| 484 |
} |
| 485 |
streamVideo->iBitRate = source->iBitRate; |
| 486 |
if (source->extraData) |
| 487 |
{ |
| 488 |
streamVideo->extraData = source->extraData; |
| 489 |
} |
| 490 |
streamVideo->colorPrimaries = source->colorPrimaries; |
| 491 |
streamVideo->colorRange = source->colorRange; |
| 492 |
streamVideo->colorSpace = source->colorSpace; |
| 493 |
streamVideo->colorTransferCharacteristic = source->colorTransferCharacteristic; |
| 494 |
streamVideo->masteringMetaData = source->masteringMetaData; |
| 495 |
streamVideo->contentLightMetaData = source->contentLightMetaData; |
| 496 |
|
| 497 |
streamVideo->m_parser_split = true; |
| 498 |
streamVideo->changes++; |
| 499 |
map[stream->uniqueId] = streamVideo; |
| 500 |
toStream = streamVideo; |
| 501 |
} |
| 502 |
else if (stream->type == STREAM_SUBTITLE) |
| 503 |
{ |
| 504 |
CDemuxStreamSubtitle *source = dynamic_cast<CDemuxStreamSubtitle*>(stream); |
| 505 |
|
| 506 |
if (!source) |
| 507 |
{ |
| 508 |
CLog::Log(LOGERROR, "CDVDDemuxClient::RequestStream - invalid subtitle stream with id {}", |
| 509 |
stream->uniqueId); |
| 510 |
DisposeStreams(); |
| 511 |
return; |
| 512 |
} |
| 513 |
|
| 514 |
std::shared_ptr<CDemuxStreamClientInternalTpl<CDemuxStreamSubtitle>> streamSubtitle; |
| 515 |
if (currentStream) |
| 516 |
streamSubtitle = std::dynamic_pointer_cast<CDemuxStreamClientInternalTpl<CDemuxStreamSubtitle>>(currentStream); |
| 517 |
if (!streamSubtitle || streamSubtitle->codec != source->codec) |
| 518 |
{ |
| 519 |
streamSubtitle = std::make_shared<CDemuxStreamClientInternalTpl<CDemuxStreamSubtitle>>(); |
| 520 |
streamSubtitle->m_parser = av_parser_init(source->codec); |
| 521 |
if (streamSubtitle->m_parser) |
| 522 |
streamSubtitle->m_parser->flags |= PARSER_FLAG_COMPLETE_FRAMES; |
| 523 |
} |
| 524 |
|
| 525 |
if (source->extraData.GetSize() == 4) |
| 526 |
{ |
| 527 |
streamSubtitle->extraData = source->extraData; |
| 528 |
} |
| 529 |
map[stream->uniqueId] = streamSubtitle; |
| 530 |
toStream = streamSubtitle; |
| 531 |
} |
| 532 |
else if (stream->type == STREAM_TELETEXT) |
| 533 |
{ |
| 534 |
CDemuxStreamTeletext *source = dynamic_cast<CDemuxStreamTeletext*>(stream); |
| 535 |
|
| 536 |
if (!source) |
| 537 |
{ |
| 538 |
CLog::Log(LOGERROR, "CDVDDemuxClient::RequestStream - invalid teletext stream with id {}", |
| 539 |
stream->uniqueId); |
| 540 |
DisposeStreams(); |
| 541 |
return; |
| 542 |
} |
| 543 |
|
| 544 |
std::shared_ptr<CDemuxStreamClientInternalTpl<CDemuxStreamTeletext>> streamTeletext; |
| 545 |
if (currentStream) |
| 546 |
streamTeletext = std::dynamic_pointer_cast<CDemuxStreamClientInternalTpl<CDemuxStreamTeletext>>(currentStream); |
| 547 |
if (!streamTeletext || streamTeletext->codec != source->codec) |
| 548 |
{ |
| 549 |
streamTeletext = std::make_shared<CDemuxStreamClientInternalTpl<CDemuxStreamTeletext>>(); |
| 550 |
} |
| 551 |
|
| 552 |
map[stream->uniqueId] = streamTeletext; |
| 553 |
toStream = streamTeletext; |
| 554 |
} |
| 555 |
else if (stream->type == STREAM_RADIO_RDS) |
| 556 |
{ |
| 557 |
CDemuxStreamRadioRDS *source = dynamic_cast<CDemuxStreamRadioRDS*>(stream); |
| 558 |
|
| 559 |
if (!source) |
| 560 |
{ |
| 561 |
CLog::Log(LOGERROR, "CDVDDemuxClient::RequestStream - invalid radio-rds stream with id {}", |
| 562 |
stream->uniqueId); |
| 563 |
DisposeStreams(); |
| 564 |
return; |
| 565 |
} |
| 566 |
|
| 567 |
std::shared_ptr<CDemuxStreamClientInternalTpl<CDemuxStreamRadioRDS>> streamRDS; |
| 568 |
if (currentStream) |
| 569 |
streamRDS = std::dynamic_pointer_cast<CDemuxStreamClientInternalTpl<CDemuxStreamRadioRDS>>(currentStream); |
| 570 |
if (!streamRDS || streamRDS->codec != source->codec) |
| 571 |
{ |
| 572 |
streamRDS = std::make_shared<CDemuxStreamClientInternalTpl<CDemuxStreamRadioRDS>>(); |
| 573 |
} |
| 574 |
|
| 575 |
map[stream->uniqueId] = streamRDS; |
| 576 |
toStream = streamRDS; |
| 577 |
} |
| 578 |
else if (stream->type == STREAM_AUDIO_ID3) |
| 579 |
{ |
| 580 |
CDemuxStreamAudioID3* source = dynamic_cast<CDemuxStreamAudioID3*>(stream); |
| 581 |
|
| 582 |
if (!source) |
| 583 |
{ |
| 584 |
CLog::Log(LOGERROR, "CDVDDemuxClient::RequestStream - invalid audio ID3 stream with id {}", |
| 585 |
stream->uniqueId); |
| 586 |
DisposeStreams(); |
| 587 |
return; |
| 588 |
} |
| 589 |
|
| 590 |
std::shared_ptr<CDemuxStreamClientInternalTpl<CDemuxStreamAudioID3>> streamID3; |
| 591 |
if (currentStream) |
| 592 |
streamID3 = std::dynamic_pointer_cast<CDemuxStreamClientInternalTpl<CDemuxStreamAudioID3>>( |
| 593 |
currentStream); |
| 594 |
if (!streamID3 || streamID3->codec != source->codec) |
| 595 |
{ |
| 596 |
streamID3 = std::make_shared<CDemuxStreamClientInternalTpl<CDemuxStreamAudioID3>>(); |
| 597 |
} |
| 598 |
|
| 599 |
map[stream->uniqueId] = streamID3; |
| 600 |
toStream = streamID3; |
| 601 |
} |
| 602 |
else |
| 603 |
{ |
| 604 |
std::shared_ptr<CDemuxStreamClientInternalTpl<CDemuxStream>> streamGen; |
| 605 |
streamGen = std::make_shared<CDemuxStreamClientInternalTpl<CDemuxStream>>(); |
| 606 |
map[stream->uniqueId] = streamGen; |
| 607 |
toStream = streamGen; |
| 608 |
} |
| 609 |
|
| 610 |
// only update profile / level if we create a new stream or has been reset stream properties |
| 611 |
// existing streams may be corrected by ParsePacket |
| 612 |
if (!currentStream || forceInit) |
| 613 |
{ |
| 614 |
toStream->profile = stream->profile; |
| 615 |
toStream->level = stream->level; |
| 616 |
} |
| 617 |
|
| 618 |
toStream->uniqueId = stream->uniqueId; |
| 619 |
toStream->codec = stream->codec; |
| 620 |
toStream->codecName = stream->codecName; |
| 621 |
toStream->codec_fourcc = stream->codec_fourcc; |
| 622 |
toStream->flags = stream->flags; |
| 623 |
toStream->cryptoSession = stream->cryptoSession; |
| 624 |
toStream->externalInterfaces = stream->externalInterfaces; |
| 625 |
toStream->language = stream->language; |
| 626 |
toStream->name = stream->name; |
| 627 |
|
| 628 |
CLog::Log(LOGDEBUG, "CDVDDemuxClient::RequestStream(): added/updated stream {} with codec_id {}", |
| 629 |
toStream->uniqueId, toStream->codec); |
| 630 |
} |
| 631 |
|
| 632 |
std::shared_ptr<CDemuxStream> CDVDDemuxClient::GetStreamInternal(int iStreamId) |
| 633 |
{ |
| 634 |
auto stream = m_streams.find(iStreamId); |
| 635 |
if (stream != m_streams.end()) |
| 636 |
{ |
| 637 |
return stream->second; |
| 638 |
} |
| 639 |
else |
| 640 |
return nullptr; |
| 641 |
} |
| 642 |
|
| 643 |
int CDVDDemuxClient::GetNrOfStreams() const |
| 644 |
{ |
| 645 |
return m_streams.size(); |
| 646 |
} |
| 647 |
|
| 648 |
bool CDVDDemuxClient::IsVideoReady() |
| 649 |
{ |
| 650 |
for (const auto& stream : m_streams) |
| 651 |
{ |
| 652 |
if (stream.first == m_videoStreamPlaying && stream.second->type == STREAM_VIDEO && |
| 653 |
CodecHasExtraData(stream.second->codec) && !stream.second->extraData) |
| 654 |
return false; |
| 655 |
} |
| 656 |
return true; |
| 657 |
} |
| 658 |
|
| 659 |
std::string CDVDDemuxClient::GetFileName() |
| 660 |
{ |
| 661 |
if (m_pInput) |
| 662 |
return m_pInput->GetFileName(); |
| 663 |
else |
| 664 |
return ""; |
| 665 |
} |
| 666 |
|
| 667 |
std::string CDVDDemuxClient::GetStreamCodecName(int iStreamId) |
| 668 |
{ |
| 669 |
CDemuxStream *stream = GetStream(iStreamId); |
| 670 |
std::string strName; |
| 671 |
if (stream) |
| 672 |
{ |
| 673 |
if (stream->codec == AV_CODEC_ID_AC3) |
| 674 |
strName = "ac3"; |
| 675 |
else if (stream->codec == AV_CODEC_ID_MP2) |
| 676 |
strName = "mp2"; |
| 677 |
else if (stream->codec == AV_CODEC_ID_AAC) |
| 678 |
strName = "aac"; |
| 679 |
else if (stream->codec == AV_CODEC_ID_DTS) |
| 680 |
strName = "dca"; |
| 681 |
else if (stream->codec == AV_CODEC_ID_MPEG2VIDEO) |
| 682 |
strName = "mpeg2video"; |
| 683 |
else if (stream->codec == AV_CODEC_ID_H264) |
| 684 |
strName = "h264"; |
| 685 |
else if (stream->codec == AV_CODEC_ID_EAC3) |
| 686 |
strName = "eac3"; |
| 687 |
else if (stream->codec == AV_CODEC_ID_VP8) |
| 688 |
strName = "vp8"; |
| 689 |
else if (stream->codec == AV_CODEC_ID_VP9) |
| 690 |
strName = "vp9"; |
| 691 |
else if (stream->codec == AV_CODEC_ID_HEVC) |
| 692 |
strName = "hevc"; |
| 693 |
else if (stream->codec == AV_CODEC_ID_AV1) |
| 694 |
strName = "av1"; |
| 695 |
} |
| 696 |
return strName; |
| 697 |
} |
| 698 |
|
| 699 |
bool CDVDDemuxClient::SeekTime(double timems, bool backwards, double *startpts) |
| 700 |
{ |
| 701 |
if (m_IDemux) |
| 702 |
{ |
| 703 |
m_displayTime = 0; |
| 704 |
m_dtsAtDisplayTime = DVD_NOPTS_VALUE; |
| 705 |
return m_IDemux->SeekTime(timems, backwards, startpts); |
| 706 |
} |
| 707 |
return false; |
| 708 |
} |
| 709 |
|
| 710 |
void CDVDDemuxClient::SetSpeed (int speed) |
| 711 |
{ |
| 712 |
if (m_IDemux) |
| 713 |
{ |
| 714 |
m_IDemux->SetSpeed(speed); |
| 715 |
} |
| 716 |
} |
| 717 |
|
| 718 |
void CDVDDemuxClient::FillBuffer(bool mode) |
| 719 |
{ |
| 720 |
if (m_IDemux) |
| 721 |
{ |
| 722 |
m_IDemux->FillBuffer(mode); |
| 723 |
} |
| 724 |
} |
| 725 |
|
| 726 |
void CDVDDemuxClient::EnableStream(int id, bool enable) |
| 727 |
{ |
| 728 |
if (m_IDemux) |
| 729 |
{ |
| 730 |
m_IDemux->EnableStream(id, enable); |
| 731 |
} |
| 732 |
} |
| 733 |
|
| 734 |
void CDVDDemuxClient::OpenStream(int id) |
| 735 |
{ |
| 736 |
// OpenStream may change some parameters |
| 737 |
// in this case we need to reset our stream properties |
| 738 |
if (m_IDemux) |
| 739 |
{ |
| 740 |
bool bOpenStream = m_IDemux->OpenStream(id); |
| 741 |
|
| 742 |
CDemuxStream *stream(m_IDemux->GetStream(id)); |
| 743 |
if (stream && stream->type == STREAM_VIDEO) |
| 744 |
m_videoStreamPlaying = id; |
| 745 |
|
| 746 |
if (bOpenStream) |
| 747 |
SetStreamProps(stream, m_streams, true); |
| 748 |
} |
| 749 |
} |
| 750 |
|
| 751 |
void CDVDDemuxClient::SetVideoResolution(unsigned int width, unsigned int height) |
| 752 |
{ |
| 753 |
if (m_IDemux) |
| 754 |
{ |
| 755 |
m_IDemux->SetVideoResolution(width, height, width, height); |
| 756 |
} |
| 757 |
} |
| 758 |
|
| 759 |
bool CDVDDemuxClient::CodecHasExtraData(AVCodecID id) |
| 760 |
{ |
| 761 |
switch (id) |
| 762 |
{ |
| 763 |
case AV_CODEC_ID_VP9: |
| 764 |
return false; |
| 765 |
default: |
| 766 |
return true; |
| 767 |
} |
| 768 |
} |