2014-04-01 01:34:59 +00:00
|
|
|
// Copyright 2014 The Chromium Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2014-10-01 22:10:21 +00:00
|
|
|
#include "packager/media/formats/mp2t/mp2t_media_parser.h"
|
|
|
|
|
2016-08-17 17:41:40 +00:00
|
|
|
#include <memory>
|
2020-10-16 21:18:35 +00:00
|
|
|
|
2014-10-01 22:10:21 +00:00
|
|
|
#include "packager/base/bind.h"
|
|
|
|
#include "packager/media/base/media_sample.h"
|
|
|
|
#include "packager/media/base/stream_info.h"
|
2020-10-16 21:18:35 +00:00
|
|
|
#include "packager/media/base/text_sample.h"
|
2014-10-01 22:10:21 +00:00
|
|
|
#include "packager/media/formats/mp2t/es_parser.h"
|
2017-10-23 05:58:52 +00:00
|
|
|
#include "packager/media/formats/mp2t/es_parser_audio.h"
|
2020-10-05 22:39:59 +00:00
|
|
|
#include "packager/media/formats/mp2t/es_parser_dvb.h"
|
2014-10-01 22:10:21 +00:00
|
|
|
#include "packager/media/formats/mp2t/es_parser_h264.h"
|
2016-03-31 19:48:16 +00:00
|
|
|
#include "packager/media/formats/mp2t/es_parser_h265.h"
|
2014-10-01 22:10:21 +00:00
|
|
|
#include "packager/media/formats/mp2t/mp2t_common.h"
|
|
|
|
#include "packager/media/formats/mp2t/ts_packet.h"
|
|
|
|
#include "packager/media/formats/mp2t/ts_section.h"
|
|
|
|
#include "packager/media/formats/mp2t/ts_section_pat.h"
|
|
|
|
#include "packager/media/formats/mp2t/ts_section_pes.h"
|
|
|
|
#include "packager/media/formats/mp2t/ts_section_pmt.h"
|
2017-10-23 05:38:55 +00:00
|
|
|
#include "packager/media/formats/mp2t/ts_stream_type.h"
|
2014-04-01 01:34:59 +00:00
|
|
|
|
2016-05-20 21:19:33 +00:00
|
|
|
namespace shaka {
|
2014-04-01 01:34:59 +00:00
|
|
|
namespace media {
|
|
|
|
namespace mp2t {
|
|
|
|
|
|
|
|
class PidState {
|
|
|
|
public:
|
|
|
|
enum PidType {
|
|
|
|
kPidPat,
|
|
|
|
kPidPmt,
|
|
|
|
kPidAudioPes,
|
|
|
|
kPidVideoPes,
|
2020-10-05 22:39:59 +00:00
|
|
|
kPidTextPes,
|
2014-04-01 01:34:59 +00:00
|
|
|
};
|
|
|
|
|
2016-08-17 17:41:40 +00:00
|
|
|
PidState(int pid,
|
|
|
|
PidType pid_type,
|
|
|
|
std::unique_ptr<TsSection> section_parser);
|
2014-04-01 01:34:59 +00:00
|
|
|
|
|
|
|
// Extract the content of the TS packet and parse it.
|
|
|
|
// Return true if successful.
|
|
|
|
bool PushTsPacket(const TsPacket& ts_packet);
|
|
|
|
|
|
|
|
// Flush the PID state (possibly emitting some pending frames)
|
|
|
|
// and reset its state.
|
2020-12-10 23:03:41 +00:00
|
|
|
bool Flush();
|
2014-04-01 01:34:59 +00:00
|
|
|
|
|
|
|
// Enable/disable the PID.
|
|
|
|
// Disabling a PID will reset its state and ignore any further incoming TS
|
|
|
|
// packets.
|
|
|
|
void Enable();
|
|
|
|
void Disable();
|
|
|
|
bool IsEnabled() const;
|
|
|
|
|
|
|
|
PidType pid_type() const { return pid_type_; }
|
|
|
|
|
2017-01-24 00:55:02 +00:00
|
|
|
std::shared_ptr<StreamInfo>& config() { return config_; }
|
|
|
|
void set_config(const std::shared_ptr<StreamInfo>& config) {
|
|
|
|
config_ = config;
|
|
|
|
}
|
2014-04-10 19:57:10 +00:00
|
|
|
|
2014-04-01 01:34:59 +00:00
|
|
|
private:
|
2020-10-16 21:18:35 +00:00
|
|
|
friend Mp2tMediaParser;
|
2014-04-01 01:34:59 +00:00
|
|
|
void ResetState();
|
|
|
|
|
|
|
|
int pid_;
|
|
|
|
PidType pid_type_;
|
2016-08-17 17:41:40 +00:00
|
|
|
std::unique_ptr<TsSection> section_parser_;
|
2014-04-01 01:34:59 +00:00
|
|
|
|
2020-10-16 21:18:35 +00:00
|
|
|
std::deque<std::shared_ptr<MediaSample>> media_sample_queue_;
|
|
|
|
std::deque<std::shared_ptr<TextSample>> text_sample_queue_;
|
|
|
|
|
2014-04-01 01:34:59 +00:00
|
|
|
bool enable_;
|
|
|
|
int continuity_counter_;
|
2017-01-24 00:55:02 +00:00
|
|
|
std::shared_ptr<StreamInfo> config_;
|
2014-04-01 01:34:59 +00:00
|
|
|
};
|
|
|
|
|
2016-08-17 17:41:40 +00:00
|
|
|
PidState::PidState(int pid,
|
|
|
|
PidType pid_type,
|
|
|
|
std::unique_ptr<TsSection> section_parser)
|
2014-04-10 19:57:10 +00:00
|
|
|
: pid_(pid),
|
|
|
|
pid_type_(pid_type),
|
2016-08-17 17:41:40 +00:00
|
|
|
section_parser_(std::move(section_parser)),
|
2014-04-10 19:57:10 +00:00
|
|
|
enable_(false),
|
|
|
|
continuity_counter_(-1) {
|
2014-04-01 01:34:59 +00:00
|
|
|
DCHECK(section_parser_);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PidState::PushTsPacket(const TsPacket& ts_packet) {
|
|
|
|
DCHECK_EQ(ts_packet.pid(), pid_);
|
|
|
|
|
|
|
|
// The current PID is not part of the PID filter,
|
|
|
|
// just discard the incoming TS packet.
|
|
|
|
if (!enable_)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
int expected_continuity_counter = (continuity_counter_ + 1) % 16;
|
|
|
|
if (continuity_counter_ >= 0 &&
|
|
|
|
ts_packet.continuity_counter() != expected_continuity_counter) {
|
2021-01-25 20:16:32 +00:00
|
|
|
LOG(ERROR) << "TS discontinuity detected for pid: " << pid_;
|
2014-04-10 19:57:10 +00:00
|
|
|
// TODO(tinskip): Handle discontinuity better.
|
2014-04-01 01:34:59 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool status = section_parser_->Parse(
|
|
|
|
ts_packet.payload_unit_start_indicator(),
|
|
|
|
ts_packet.payload(),
|
|
|
|
ts_packet.payload_size());
|
|
|
|
|
|
|
|
// At the minimum, when parsing failed, auto reset the section parser.
|
2014-04-16 23:22:31 +00:00
|
|
|
// Components that use the Mp2tMediaParser can take further action if needed.
|
2014-04-01 01:34:59 +00:00
|
|
|
if (!status) {
|
2021-01-25 20:16:32 +00:00
|
|
|
LOG(ERROR) << "Parsing failed for pid = " << pid_ << ", type=" << pid_type_;
|
2014-04-01 01:34:59 +00:00
|
|
|
ResetState();
|
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2020-12-10 23:03:41 +00:00
|
|
|
bool PidState::Flush() {
|
|
|
|
RCHECK(section_parser_->Flush());
|
2014-04-01 01:34:59 +00:00
|
|
|
ResetState();
|
2020-12-10 23:03:41 +00:00
|
|
|
return true;
|
2014-04-01 01:34:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PidState::Enable() {
|
|
|
|
enable_ = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PidState::Disable() {
|
|
|
|
if (!enable_)
|
|
|
|
return;
|
|
|
|
|
|
|
|
ResetState();
|
|
|
|
enable_ = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PidState::IsEnabled() const {
|
|
|
|
return enable_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PidState::ResetState() {
|
|
|
|
section_parser_->Reset();
|
|
|
|
continuity_counter_ = -1;
|
|
|
|
}
|
|
|
|
|
2014-04-16 23:22:31 +00:00
|
|
|
Mp2tMediaParser::Mp2tMediaParser()
|
2014-04-10 19:57:10 +00:00
|
|
|
: sbr_in_mimetype_(false),
|
|
|
|
is_initialized_(false) {
|
2014-04-01 01:34:59 +00:00
|
|
|
}
|
|
|
|
|
2016-08-30 23:01:19 +00:00
|
|
|
Mp2tMediaParser::~Mp2tMediaParser() {}
|
2014-04-01 01:34:59 +00:00
|
|
|
|
2020-07-01 22:43:44 +00:00
|
|
|
void Mp2tMediaParser::Init(const InitCB& init_cb,
|
|
|
|
const NewMediaSampleCB& new_media_sample_cb,
|
|
|
|
const NewTextSampleCB& new_text_sample_cb,
|
|
|
|
KeySource* decryption_key_source) {
|
2014-04-01 01:34:59 +00:00
|
|
|
DCHECK(!is_initialized_);
|
|
|
|
DCHECK(init_cb_.is_null());
|
|
|
|
DCHECK(!init_cb.is_null());
|
2020-07-01 22:43:44 +00:00
|
|
|
DCHECK(!new_media_sample_cb.is_null());
|
2020-10-16 21:18:35 +00:00
|
|
|
DCHECK(!new_text_sample_cb.is_null());
|
2014-04-01 01:34:59 +00:00
|
|
|
|
|
|
|
init_cb_ = init_cb;
|
2020-10-16 21:18:35 +00:00
|
|
|
new_media_sample_cb_ = new_media_sample_cb;
|
|
|
|
new_text_sample_cb_ = new_text_sample_cb;
|
2014-04-01 01:34:59 +00:00
|
|
|
}
|
|
|
|
|
2016-01-21 18:30:29 +00:00
|
|
|
bool Mp2tMediaParser::Flush() {
|
2014-04-16 23:22:31 +00:00
|
|
|
DVLOG(1) << "Mp2tMediaParser::Flush";
|
2014-04-01 01:34:59 +00:00
|
|
|
|
|
|
|
// Flush the buffers and reset the pids.
|
2016-08-30 23:01:19 +00:00
|
|
|
for (const auto& pair : pids_) {
|
|
|
|
DVLOG(1) << "Flushing PID: " << pair.first;
|
|
|
|
PidState* pid_state = pair.second.get();
|
2020-12-10 23:03:41 +00:00
|
|
|
RCHECK(pid_state->Flush());
|
2014-04-01 01:34:59 +00:00
|
|
|
}
|
2016-01-21 18:30:29 +00:00
|
|
|
bool result = EmitRemainingSamples();
|
2016-08-30 23:01:19 +00:00
|
|
|
pids_.clear();
|
2014-04-01 01:34:59 +00:00
|
|
|
|
|
|
|
// Remove any bytes left in the TS buffer.
|
|
|
|
// (i.e. any partial TS packet => less than 188 bytes).
|
|
|
|
ts_byte_queue_.Reset();
|
2016-01-21 18:30:29 +00:00
|
|
|
return result;
|
2014-04-01 01:34:59 +00:00
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
bool Mp2tMediaParser::Parse(const uint8_t* buf, int size) {
|
2020-10-06 23:07:51 +00:00
|
|
|
DVLOG(2) << "Mp2tMediaParser::Parse size=" << size;
|
2014-04-01 01:34:59 +00:00
|
|
|
|
|
|
|
// Add the data to the parser state.
|
|
|
|
ts_byte_queue_.Push(buf, size);
|
|
|
|
|
|
|
|
while (true) {
|
2014-09-30 21:52:21 +00:00
|
|
|
const uint8_t* ts_buffer;
|
2014-04-01 01:34:59 +00:00
|
|
|
int ts_buffer_size;
|
|
|
|
ts_byte_queue_.Peek(&ts_buffer, &ts_buffer_size);
|
|
|
|
if (ts_buffer_size < TsPacket::kPacketSize)
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Synchronization.
|
|
|
|
int skipped_bytes = TsPacket::Sync(ts_buffer, ts_buffer_size);
|
|
|
|
if (skipped_bytes > 0) {
|
|
|
|
DVLOG(1) << "Packet not aligned on a TS syncword:"
|
|
|
|
<< " skipped_bytes=" << skipped_bytes;
|
|
|
|
ts_byte_queue_.Pop(skipped_bytes);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse the TS header, skipping 1 byte if the header is invalid.
|
2016-08-17 17:41:40 +00:00
|
|
|
std::unique_ptr<TsPacket> ts_packet(
|
|
|
|
TsPacket::Parse(ts_buffer, ts_buffer_size));
|
2014-04-01 01:34:59 +00:00
|
|
|
if (!ts_packet) {
|
|
|
|
DVLOG(1) << "Error: invalid TS packet";
|
|
|
|
ts_byte_queue_.Pop(1);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
DVLOG(LOG_LEVEL_TS)
|
|
|
|
<< "Processing PID=" << ts_packet->pid()
|
|
|
|
<< " start_unit=" << ts_packet->payload_unit_start_indicator();
|
|
|
|
|
|
|
|
// Parse the section.
|
2020-10-16 21:18:35 +00:00
|
|
|
auto it = pids_.find(ts_packet->pid());
|
2014-04-01 01:34:59 +00:00
|
|
|
if (it == pids_.end() &&
|
|
|
|
ts_packet->pid() == TsSection::kPidPat) {
|
|
|
|
// Create the PAT state here if needed.
|
2016-08-17 17:41:40 +00:00
|
|
|
std::unique_ptr<TsSection> pat_section_parser(new TsSectionPat(
|
|
|
|
base::Bind(&Mp2tMediaParser::RegisterPmt, base::Unretained(this))));
|
|
|
|
std::unique_ptr<PidState> pat_pid_state(new PidState(
|
|
|
|
ts_packet->pid(), PidState::kPidPat, std::move(pat_section_parser)));
|
2014-04-01 01:34:59 +00:00
|
|
|
pat_pid_state->Enable();
|
2020-10-16 21:18:35 +00:00
|
|
|
it = pids_.emplace(ts_packet->pid(), std::move(pat_pid_state)).first;
|
2014-04-01 01:34:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (it != pids_.end()) {
|
2021-01-25 20:16:32 +00:00
|
|
|
RCHECK(it->second->PushTsPacket(*ts_packet));
|
2014-04-01 01:34:59 +00:00
|
|
|
} else {
|
|
|
|
DVLOG(LOG_LEVEL_TS) << "Ignoring TS packet for pid: " << ts_packet->pid();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Go to the next packet.
|
|
|
|
ts_byte_queue_.Pop(TsPacket::kPacketSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Emit the A/V buffers that kept accumulating during TS parsing.
|
2014-04-10 19:57:10 +00:00
|
|
|
return EmitRemainingSamples();
|
2014-04-01 01:34:59 +00:00
|
|
|
}
|
|
|
|
|
2014-04-16 23:22:31 +00:00
|
|
|
void Mp2tMediaParser::RegisterPmt(int program_number, int pmt_pid) {
|
2014-04-01 01:34:59 +00:00
|
|
|
DVLOG(1) << "RegisterPmt:"
|
|
|
|
<< " program_number=" << program_number
|
|
|
|
<< " pmt_pid=" << pmt_pid;
|
|
|
|
|
|
|
|
// Only one TS program is allowed. Ignore the incoming program map table,
|
|
|
|
// if there is already one registered.
|
2016-08-30 23:01:19 +00:00
|
|
|
for (const auto& pair : pids_) {
|
|
|
|
if (pair.second->pid_type() == PidState::kPidPmt) {
|
|
|
|
DVLOG_IF(1, pmt_pid != pair.first) << "More than one program is defined";
|
2014-04-01 01:34:59 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the PMT state here if needed.
|
|
|
|
DVLOG(1) << "Create a new PMT parser";
|
2016-08-17 17:41:40 +00:00
|
|
|
std::unique_ptr<TsSection> pmt_section_parser(new TsSectionPmt(base::Bind(
|
|
|
|
&Mp2tMediaParser::RegisterPes, base::Unretained(this), pmt_pid)));
|
|
|
|
std::unique_ptr<PidState> pmt_pid_state(
|
|
|
|
new PidState(pmt_pid, PidState::kPidPmt, std::move(pmt_section_parser)));
|
2014-04-01 01:34:59 +00:00
|
|
|
pmt_pid_state->Enable();
|
2020-10-16 21:18:35 +00:00
|
|
|
pids_.emplace(pmt_pid, std::move(pmt_pid_state));
|
2014-04-01 01:34:59 +00:00
|
|
|
}
|
|
|
|
|
2014-04-16 23:22:31 +00:00
|
|
|
void Mp2tMediaParser::RegisterPes(int pmt_pid,
|
2014-04-29 18:51:28 +00:00
|
|
|
int pes_pid,
|
2020-12-11 20:58:26 +00:00
|
|
|
TsStreamType stream_type,
|
|
|
|
const uint8_t* descriptor,
|
|
|
|
size_t descriptor_length) {
|
2020-10-16 21:18:35 +00:00
|
|
|
if (pids_.count(pes_pid) != 0)
|
2014-04-01 01:34:59 +00:00
|
|
|
return;
|
2020-10-06 23:07:51 +00:00
|
|
|
DVLOG(1) << "RegisterPes:"
|
|
|
|
<< " pes_pid=" << pes_pid << " stream_type=" << std::hex
|
2021-01-25 20:16:32 +00:00
|
|
|
<< static_cast<int>(stream_type) << std::dec;
|
2014-04-01 01:34:59 +00:00
|
|
|
|
|
|
|
// Create a stream parser corresponding to the stream type.
|
2020-10-05 22:39:59 +00:00
|
|
|
PidState::PidType pid_type = PidState::kPidVideoPes;
|
2016-08-17 17:41:40 +00:00
|
|
|
std::unique_ptr<EsParser> es_parser;
|
2020-10-06 23:07:51 +00:00
|
|
|
auto on_new_stream = base::Bind(&Mp2tMediaParser::OnNewStreamInfo,
|
|
|
|
base::Unretained(this), pes_pid);
|
2020-10-16 21:18:35 +00:00
|
|
|
auto on_emit_media = base::Bind(&Mp2tMediaParser::OnEmitMediaSample,
|
|
|
|
base::Unretained(this), pes_pid);
|
2020-10-05 22:39:59 +00:00
|
|
|
auto on_emit_text = base::Bind(&Mp2tMediaParser::OnEmitTextSample,
|
|
|
|
base::Unretained(this), pes_pid);
|
2021-01-25 20:16:32 +00:00
|
|
|
switch (stream_type) {
|
2017-10-23 05:38:55 +00:00
|
|
|
case TsStreamType::kAvc:
|
2020-10-16 21:18:35 +00:00
|
|
|
es_parser.reset(new EsParserH264(pes_pid, on_new_stream, on_emit_media));
|
2017-10-23 05:38:55 +00:00
|
|
|
break;
|
|
|
|
case TsStreamType::kHevc:
|
2020-10-16 21:18:35 +00:00
|
|
|
es_parser.reset(new EsParserH265(pes_pid, on_new_stream, on_emit_media));
|
2017-10-23 05:38:55 +00:00
|
|
|
break;
|
|
|
|
case TsStreamType::kAdtsAac:
|
2020-06-03 00:32:19 +00:00
|
|
|
case TsStreamType::kMpeg1Audio:
|
2017-10-23 05:38:55 +00:00
|
|
|
case TsStreamType::kAc3:
|
2020-10-06 23:07:51 +00:00
|
|
|
es_parser.reset(
|
|
|
|
new EsParserAudio(pes_pid, static_cast<TsStreamType>(stream_type),
|
2020-10-16 21:18:35 +00:00
|
|
|
on_new_stream, on_emit_media, sbr_in_mimetype_));
|
2020-10-05 22:39:59 +00:00
|
|
|
pid_type = PidState::kPidAudioPes;
|
|
|
|
break;
|
|
|
|
case TsStreamType::kDvbSubtitles:
|
2020-12-11 20:58:26 +00:00
|
|
|
es_parser.reset(new EsParserDvb(pes_pid, on_new_stream, on_emit_text,
|
|
|
|
descriptor, descriptor_length));
|
2020-10-05 22:39:59 +00:00
|
|
|
pid_type = PidState::kPidTextPes;
|
2017-10-23 05:38:55 +00:00
|
|
|
break;
|
2017-12-01 22:53:20 +00:00
|
|
|
default: {
|
2021-01-25 20:16:32 +00:00
|
|
|
auto type = static_cast<int>(stream_type);
|
2020-10-05 22:39:59 +00:00
|
|
|
DCHECK(type <= 0xff);
|
2021-01-25 20:16:32 +00:00
|
|
|
LOG_IF(ERROR, !stream_type_logged_once_[type])
|
|
|
|
<< "Ignore unsupported MPEG2TS stream type 0x" << std::hex << type
|
|
|
|
<< std::dec;
|
|
|
|
stream_type_logged_once_[type] = true;
|
2017-10-23 05:38:55 +00:00
|
|
|
return;
|
2017-12-01 22:53:20 +00:00
|
|
|
}
|
2014-04-01 01:34:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create the PES state here.
|
|
|
|
DVLOG(1) << "Create a new PES state";
|
2016-08-17 17:41:40 +00:00
|
|
|
std::unique_ptr<TsSection> pes_section_parser(
|
|
|
|
new TsSectionPes(std::move(es_parser)));
|
|
|
|
std::unique_ptr<PidState> pes_pid_state(
|
|
|
|
new PidState(pes_pid, pid_type, std::move(pes_section_parser)));
|
2014-04-10 19:57:10 +00:00
|
|
|
pes_pid_state->Enable();
|
2020-10-16 21:18:35 +00:00
|
|
|
pids_.emplace(pes_pid, std::move(pes_pid_state));
|
2014-04-01 01:34:59 +00:00
|
|
|
}
|
|
|
|
|
2014-04-16 23:22:31 +00:00
|
|
|
void Mp2tMediaParser::OnNewStreamInfo(
|
2020-10-06 23:07:51 +00:00
|
|
|
uint32_t pes_pid,
|
2020-10-16 21:18:35 +00:00
|
|
|
std::shared_ptr<StreamInfo> new_stream_info) {
|
2020-10-06 23:07:51 +00:00
|
|
|
DCHECK(!new_stream_info || new_stream_info->track_id() == pes_pid);
|
|
|
|
DVLOG(1) << "OnVideoConfigChanged for pid=" << pes_pid
|
|
|
|
<< ", has_info=" << (new_stream_info ? "true" : "false");
|
2014-04-01 01:34:59 +00:00
|
|
|
|
2020-10-16 21:18:35 +00:00
|
|
|
auto pid_state = pids_.find(pes_pid);
|
2014-04-10 19:57:10 +00:00
|
|
|
if (pid_state == pids_.end()) {
|
|
|
|
LOG(ERROR) << "PID State for new stream not found (pid = "
|
|
|
|
<< new_stream_info->track_id() << ").";
|
|
|
|
return;
|
2014-04-01 01:34:59 +00:00
|
|
|
}
|
|
|
|
|
2020-10-06 23:07:51 +00:00
|
|
|
if (new_stream_info) {
|
|
|
|
// Set the stream configuration information for the PID.
|
|
|
|
pid_state->second->set_config(new_stream_info);
|
|
|
|
} else {
|
|
|
|
LOG(WARNING) << "Ignoring unsupported stream with pid=" << pes_pid;
|
|
|
|
pid_state->second->Disable();
|
|
|
|
}
|
2014-04-01 01:34:59 +00:00
|
|
|
|
2014-04-10 19:57:10 +00:00
|
|
|
// Finish initialization if all streams have configs.
|
|
|
|
FinishInitializationIfNeeded();
|
2014-04-01 01:34:59 +00:00
|
|
|
}
|
|
|
|
|
2014-04-16 23:22:31 +00:00
|
|
|
bool Mp2tMediaParser::FinishInitializationIfNeeded() {
|
2014-04-01 01:34:59 +00:00
|
|
|
// Nothing to be done if already initialized.
|
|
|
|
if (is_initialized_)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Wait for more data to come to finish initialization.
|
2014-04-10 19:57:10 +00:00
|
|
|
if (pids_.empty())
|
2014-04-01 01:34:59 +00:00
|
|
|
return true;
|
|
|
|
|
2017-01-24 00:55:02 +00:00
|
|
|
std::vector<std::shared_ptr<StreamInfo>> all_stream_info;
|
2014-09-30 21:52:21 +00:00
|
|
|
uint32_t num_es(0);
|
2020-10-16 21:18:35 +00:00
|
|
|
for (const auto& pair : pids_) {
|
|
|
|
if ((pair.second->pid_type() == PidState::kPidAudioPes ||
|
2020-10-05 22:39:59 +00:00
|
|
|
pair.second->pid_type() == PidState::kPidVideoPes ||
|
|
|
|
pair.second->pid_type() == PidState::kPidTextPes) &&
|
2020-10-16 21:18:35 +00:00
|
|
|
pair.second->IsEnabled()) {
|
2014-04-10 19:57:10 +00:00
|
|
|
++num_es;
|
2020-10-16 21:18:35 +00:00
|
|
|
if (pair.second->config())
|
|
|
|
all_stream_info.push_back(pair.second->config());
|
2014-04-10 19:57:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (num_es && (all_stream_info.size() == num_es)) {
|
|
|
|
// All stream configurations have been received. Initialization can
|
|
|
|
// be completed.
|
|
|
|
init_cb_.Run(all_stream_info);
|
|
|
|
DVLOG(1) << "Mpeg2TS stream parser initialization done";
|
|
|
|
is_initialized_ = true;
|
|
|
|
}
|
2014-04-01 01:34:59 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-10-16 21:18:35 +00:00
|
|
|
void Mp2tMediaParser::OnEmitMediaSample(
|
2015-07-22 23:40:45 +00:00
|
|
|
uint32_t pes_pid,
|
2020-10-16 21:18:35 +00:00
|
|
|
std::shared_ptr<MediaSample> new_sample) {
|
|
|
|
DCHECK(new_sample);
|
|
|
|
DVLOG(LOG_LEVEL_ES) << "OnEmitMediaSample: "
|
|
|
|
<< " pid=" << pes_pid
|
|
|
|
<< " size=" << new_sample->data_size()
|
|
|
|
<< " dts=" << new_sample->dts()
|
|
|
|
<< " pts=" << new_sample->pts();
|
|
|
|
|
|
|
|
// Add the sample to the appropriate PID sample queue.
|
|
|
|
auto pid_state = pids_.find(pes_pid);
|
|
|
|
if (pid_state == pids_.end()) {
|
|
|
|
LOG(ERROR) << "PID State for new sample not found (pid = " << pes_pid
|
|
|
|
<< ").";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
pid_state->second->media_sample_queue_.push_back(std::move(new_sample));
|
|
|
|
}
|
|
|
|
|
|
|
|
void Mp2tMediaParser::OnEmitTextSample(uint32_t pes_pid,
|
|
|
|
std::shared_ptr<TextSample> new_sample) {
|
2014-04-10 19:57:10 +00:00
|
|
|
DCHECK(new_sample);
|
2020-10-16 21:18:35 +00:00
|
|
|
DVLOG(LOG_LEVEL_ES) << "OnEmitTextSample: "
|
|
|
|
<< " pid=" << pes_pid
|
|
|
|
<< " start=" << new_sample->start_time();
|
2014-04-01 01:34:59 +00:00
|
|
|
|
2014-04-10 19:57:10 +00:00
|
|
|
// Add the sample to the appropriate PID sample queue.
|
2020-10-16 21:18:35 +00:00
|
|
|
auto pid_state = pids_.find(pes_pid);
|
2014-04-10 19:57:10 +00:00
|
|
|
if (pid_state == pids_.end()) {
|
|
|
|
LOG(ERROR) << "PID State for new sample not found (pid = "
|
|
|
|
<< pes_pid << ").";
|
2014-04-01 01:34:59 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-10-16 21:18:35 +00:00
|
|
|
pid_state->second->text_sample_queue_.push_back(std::move(new_sample));
|
2014-04-01 01:34:59 +00:00
|
|
|
}
|
|
|
|
|
2014-04-16 23:22:31 +00:00
|
|
|
bool Mp2tMediaParser::EmitRemainingSamples() {
|
|
|
|
DVLOG(LOG_LEVEL_ES) << "Mp2tMediaParser::EmitRemainingBuffers";
|
2014-04-01 01:34:59 +00:00
|
|
|
|
|
|
|
// No buffer should be sent until fully initialized.
|
|
|
|
if (!is_initialized_)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Buffer emission.
|
2020-10-16 21:18:35 +00:00
|
|
|
for (const auto& pid_pair : pids_) {
|
|
|
|
for (auto sample : pid_pair.second->media_sample_queue_) {
|
2021-01-25 20:16:32 +00:00
|
|
|
RCHECK(new_media_sample_cb_.Run(pid_pair.first, sample));
|
2020-10-16 21:18:35 +00:00
|
|
|
}
|
|
|
|
pid_pair.second->media_sample_queue_.clear();
|
|
|
|
|
|
|
|
for (auto sample : pid_pair.second->text_sample_queue_) {
|
2021-01-25 20:16:32 +00:00
|
|
|
RCHECK(new_text_sample_cb_.Run(pid_pair.first, sample));
|
2014-04-01 01:34:59 +00:00
|
|
|
}
|
2020-10-16 21:18:35 +00:00
|
|
|
pid_pair.second->text_sample_queue_.clear();
|
2014-04-01 01:34:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace mp2t
|
|
|
|
} // namespace media
|
2016-05-20 21:19:33 +00:00
|
|
|
} // namespace shaka
|