Skip to content

Commit

Permalink
HLS Draft
Browse files Browse the repository at this point in the history
  • Loading branch information
pschatzmann committed Sep 16, 2023
1 parent 921684c commit 9f8263a
Show file tree
Hide file tree
Showing 13 changed files with 625 additions and 230 deletions.
4 changes: 4 additions & 0 deletions src/AudioBasic/Collections/Vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,10 @@ class Vector {
return p_data!=nullptr;
}

void set_capacity(int size){
resize_internal(size, false, false);
}

protected:
int bufferLen=0;
int len = 0;
Expand Down
18 changes: 17 additions & 1 deletion src/AudioCodecs/AudioEncoded.h
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@ class EncodedAudioOutput : public AudioStream {

/// Starts the processing - sets the status to active
bool begin() override {
custom_log_level.set();
TRACED();
if (!active) {
TRACED();
Expand All @@ -338,6 +339,7 @@ class EncodedAudioOutput : public AudioStream {
LOGW("no decoder or encoder defined");
}
}
custom_log_level.reset();
return active;
}

Expand All @@ -349,14 +351,17 @@ class EncodedAudioOutput : public AudioStream {

/// Ends the processing
void end() override {
custom_log_level.set();
TRACEI();
decoder_ptr->end();
encoder_ptr->end();
active = false;
custom_log_level.reset();
}

/// encodeor decode the data
virtual size_t write(const uint8_t *data, size_t len) override {
custom_log_level.set();
LOGD("EncodedAudioOutput::write: %d", (int)len);
if (len == 0) {
LOGI("write: %d", 0);
Expand All @@ -370,6 +375,8 @@ class EncodedAudioOutput : public AudioStream {

size_t result = writer_ptr->write(data, len);
LOGD("EncodedAudioOutput::write: %d -> %d", (int)len, (int)result);
custom_log_level.reset();

return result;
}

Expand All @@ -384,13 +391,19 @@ class EncodedAudioOutput : public AudioStream {
/// Provides the initialized encoder
AudioEncoder &encoder() { return *encoder_ptr; }

/// Defines the class specific custom log level
void setLogLevel(AudioLogger::LogLevel level){
custom_log_level.set(level);
}

protected:
AudioInfo info;
AudioDecoder *decoder_ptr = CodecNOP::instance(); // decoder
AudioEncoder *encoder_ptr = CodecNOP::instance(); // decoder
AudioWriter *writer_ptr = nullptr;
Print *ptr_out = nullptr;
bool active;
CustomLogLevel custom_log_level;
};

// legacy name
Expand Down Expand Up @@ -470,13 +483,16 @@ class EncodedAudioStream : public EncodedAudioOutput {
}

size_t readBytes(uint8_t *buffer, size_t length) override {
custom_log_level.set();
LOGD("EncodedAudioStream::readBytes: %d", (int)length);
if (p_stream == nullptr) {
TRACEE();
return 0;
}
decode(reqested_bytes);
return decoded_buffer.readArray(buffer, length);
size_t result = decoded_buffer.readArray(buffer, length);
custom_log_level.reset();
return result;
}

protected:
Expand Down
103 changes: 64 additions & 39 deletions src/AudioCodecs/CodecMTS.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,13 @@ class MTSDecoder : public AudioDecoder {
stream_types.push_back(type);
}

bool isStreamTypeActive(TSDPMTStreamType type){
for (int j=0;j<stream_types.size();j++){
if (stream_types[j]==type) return true;
}
return false;
}

/// Set a new write buffer size (default is 2000)
void resizeBuffer(int size){
buffer.resize(size);
Expand All @@ -128,12 +135,18 @@ class MTSDecoder : public AudioDecoder {
static MTSDecoder *self;
int underflowLimit = MTS_UNDERFLOW_LIMIT;
bool is_active = false;
bool is_write_active = false;
bool is_alloc_active = false;
TSDemuxContext ctx;
uint16_t print_pids[MTS_PRINT_PIDS_LEN] = {0};
SingleBuffer<uint8_t> buffer{MTS_WRITE_BUFFER_SIZE};
Vector<TSDPMTStreamType> stream_types;

void set_write_active(bool flag){
//LOGD("is_write_active: %s", flag ? "true":"false");
is_write_active = flag;
}

void demux(int limit){
TRACED();
TSDCode res = TSD_OK;
Expand All @@ -152,7 +165,7 @@ class MTSDecoder : public AudioDecoder {
void logResult(TSDCode code) {
switch (code) {
case TSD_OK:
LOGI("TSD_OK");
LOGD("TSD_OK");
break;
case TSD_INVALID_SYNC_BYTE:
LOGW("TSD_INVALID_SYNC_BYTE");
Expand Down Expand Up @@ -202,7 +215,7 @@ class MTSDecoder : public AudioDecoder {
// event callback
static void event_cb(TSDemuxContext *ctx, uint16_t pid, TSDEventId event_id,
void *data) {
TRACEI();
TRACED();
if (MTSDecoder::self != nullptr) {
MTSDecoder::self->event_cb_local(ctx, pid, event_id, data);
}
Expand All @@ -211,27 +224,26 @@ class MTSDecoder : public AudioDecoder {
void event_cb_local(TSDemuxContext *ctx, uint16_t pid, TSDEventId event_id,
void *data) {
if (event_id == TSD_EVENT_PAT) {
set_write_active(false);
print_pat(ctx, data);
} else if (event_id == TSD_EVENT_PMT) {
set_write_active(false);
print_pmt(ctx, data);
} else if (event_id == TSD_EVENT_PES) {
TSDPESPacket *pes = (TSDPESPacket *)data;
// This is where we would write the PES data into our buffer.
LOGI("====================");
LOGI("PID %d PES Packet, Size: %ld, stream_id=%u, pts=%lu, dts=%lu", pid,
// This is where we write the PES data into our buffer.
LOGD("====================");
LOGD("PID %d PES Packet, Size: %ld, stream_id=%u, pts=%lu, dts=%lu", pid,
pes->data_bytes_length, pes->stream_id, pes->pts, pes->dts);
// print out the PES Packet data if it's in our print list
int i;
AudioLogger logger = AudioLogger::instance();
for (i = 0; i < MTS_PRINT_PIDS_LEN; ++i) {
if (print_pids[i] == pid) {
// output data
if (p_print != nullptr) {
p_print->write(pes->data_bytes, pes->data_bytes_length);
}
// log data
if (logger.isLogging(AudioLogger::Debug)) {
LOGI(" PES data: ");
logger.print(" PES data ");
logger.print(is_write_active? "active:":"inactive:");
int j = 0;
while (j < pes->data_bytes_length) {
char n = pes->data_bytes[j];
Expand All @@ -240,10 +252,19 @@ class MTSDecoder : public AudioDecoder {
}
logger.printChar('\n');
}
// output data
if (p_print != nullptr) {
size_t eff = p_print->write(pes->data_bytes, pes->data_bytes_length);
if(eff!=pes->data_bytes_length){
// we should not get here
TRACEE();
}
}
}
}

} else if (event_id == TSD_EVENT_ADAP_FIELD_PRV_DATA) {
set_write_active(false);
// we're only watching for SCTE Adaptions Field Private Data,
// so we know that we must parse it as a list of descritors.
TSDAdaptationField *adap_field = (TSDAdaptationField *)data;
Expand All @@ -253,61 +274,65 @@ class MTSDecoder : public AudioDecoder {
adap_field->transport_private_data_length,
&descriptors, &descriptors_length);

LOGI("====================");
LOGI("Descriptors - Adaptation Fields");
LOGD("====================");
LOGD("Descriptors - Adaptation Fields");
int i = 0;
for (; i < descriptors_length; ++i) {
TSDDescriptor *des = &descriptors[i];
LOGI(" %d) tag: (0x%04X) %s", i, des->tag,
LOGD(" %d) tag: (0x%04X) %s", i, des->tag,
descriptor_tag_to_str(des->tag));
LOGI(" length: %d", des->length);
LOGD(" length: %d", des->length);
print_descriptor_info(des);
}
}
}

void print_pat(TSDemuxContext *ctx, void *data) {
LOGI("====================");
LOGD("====================");
TSDPATData *pat = (TSDPATData *)data;
size_t len = pat->length;
size_t i;
LOGI("PAT, Length %d", (int)pat->length);
LOGD("PAT, Length %d", (int)pat->length);

if (len > 1) {
LOGI("number of progs: %d", (int)len);
LOGD("number of progs: %d", (int)len);
}
for (i = 0; i < len; ++i) {
LOGI(" %d) prog num: 0x%X, pid: 0x%X", (int)i, pat->program_number[i],
LOGD(" %d) prog num: 0x%X, pid: 0x%X", (int)i, pat->program_number[i],
pat->pid[i]);
}
}

void print_pmt(TSDemuxContext *ctx, void *data) {
LOGI("====================");
LOGI("PMT");
LOGD("====================");
LOGD("PMT");
TSDPMTData *pmt = (TSDPMTData *)data;
LOGI("PCR PID: 0x%04X", pmt->pcr_pid);
LOGI("program info length: %d", (int)pmt->program_info_length);
LOGI("descriptors length: %d", (int)pmt->descriptors_length);
LOGD("PCR PID: 0x%04X", pmt->pcr_pid);
LOGD("program info length: %d", (int)pmt->program_info_length);
LOGD("descriptors length: %d", (int)pmt->descriptors_length);
size_t i;

for (i = 0; i < pmt->descriptors_length; ++i) {
TSDDescriptor *des = &pmt->descriptors[i];
LOGI(" %d) tag: (0x%04X) %s", (int)i, des->tag,
LOGD(" %d) tag: (0x%04X) %s", (int)i, des->tag,
descriptor_tag_to_str(des->tag));
LOGI(" length: %d", des->length);
LOGD(" length: %d", des->length);
print_descriptor_info(des);
}

LOGI("program elements length: %d", (int)pmt->program_elements_length);
LOGD("program elements length: %d", (int)pmt->program_elements_length);
for (i = 0; i < pmt->program_elements_length; ++i) {
TSDProgramElement *prog = &pmt->program_elements[i];
LOGI(" -----Program #%d", (int)i);
LOGI(" stream type: (0x%04X) %s", prog->stream_type,
LOGD(" -----Program #%d", (int)i);
LOGD(" stream type: (0x%04X) %s", prog->stream_type,
stream_type_to_str((TSDPESStreamId)(prog->stream_type)));
LOGI(" elementary pid: 0x%04X", prog->elementary_pid);
LOGI(" es info length: %d", prog->es_info_length);
LOGI(" descriptors length: %d", (int)prog->descriptors_length);
LOGD(" elementary pid: 0x%04X", prog->elementary_pid);
LOGD(" es info length: %d", prog->es_info_length);
LOGD(" descriptors length: %d", (int)prog->descriptors_length);

if (isStreamTypeActive((TSDPMTStreamType)prog->stream_type)){
set_write_active(true);
}

// keep track of metadata pids, we'll print the data for these
for(int j=0;j<stream_types.size();j++){
Expand All @@ -320,9 +345,9 @@ class MTSDecoder : public AudioDecoder {
size_t j;
for (j = 0; j < prog->descriptors_length; ++j) {
TSDDescriptor *des = &prog->descriptors[j];
LOGI(" %d) tag: (0x%04X) %s", (int)j, des->tag,
LOGD(" %d) tag: (0x%04X) %s", (int)j, des->tag,
descriptor_tag_to_str(des->tag));
LOGI(" length: %d", des->length);
LOGD(" length: %d", des->length);
print_descriptor_info(des);

// if this tag is the SCTE Adaption field private data descriptor,
Expand Down Expand Up @@ -784,29 +809,29 @@ class MTSDecoder : public AudioDecoder {
TSDDescriptorRegistration res;
if (TSD_OK == tsd_parse_descriptor_registration(
desc->data, desc->data_length, &res)) {
LOGI("\n format identififer: 0x%08X", res.format_identifier);
LOGD("\n format identififer: 0x%08X", res.format_identifier);
}
} break;
case 0x0A: // ISO 639 Language descriptor
{
TSDDescriptorISO639Language res;
if (TSD_OK == tsd_parse_descriptor_iso639_language(
desc->data, desc->data_length, &res)) {
LOGI("\n");
LOGD("\n");
int i = 0;
for (; i < res.language_length; ++i) {
LOGI(" ISO Language Code: 0x%08X, audio type: 0x%02x",
LOGD(" ISO Language Code: 0x%08X, audio type: 0x%02x",
res.iso_language_code[i], res.audio_type[i]);
}
LOGI("\n");
LOGD("\n");
}
} break;
case 0x0E: // Maximum bitrate descriptor
{
TSDDescriptorMaxBitrate res;
if (TSD_OK == tsd_parse_descriptor_max_bitrate(
desc->data, desc->data_length, &res)) {
LOGI(" Maximum Bitrate: %d x 50 bytes/second", res.max_bitrate);
LOGD(" Maximum Bitrate: %d x 50 bytes/second", res.max_bitrate);
}
} break;
default: {
Expand Down Expand Up @@ -834,7 +859,7 @@ class MTSDecoder : public AudioDecoder {
}

static void log_free (void *mem){
LOGI("free(%p)\n", mem);
LOGD("free(%p)\n", mem);
free(mem);
}

Expand Down
Loading

0 comments on commit 9f8263a

Please sign in to comment.