Skip to content

Commit

Permalink
MOT: use observer pattern for update
Browse files Browse the repository at this point in the history
  • Loading branch information
basicmaster committed Aug 15, 2022
1 parent c4b1356 commit 2675f14
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 42 deletions.
19 changes: 8 additions & 11 deletions src/mot_manager.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
DABlin - capital DAB experience
Copyright (C) 2016-2018 Stefan Pöschel
Copyright (C) 2016-2022 Stefan Pöschel
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -210,10 +210,6 @@ bool MOTObject::IsToBeShown() {


// --- MOTManager -----------------------------------------------------------------
MOTManager::MOTManager() {
Reset();
}

void MOTManager::Reset() {
object = MOTObject();
current_transport_id = -1;
Expand Down Expand Up @@ -284,7 +280,7 @@ bool MOTManager::ParseCheckSegmentationHeader(const std::vector<uint8_t>& dg, si
return true;
}

bool MOTManager::HandleMOTDataGroup(const std::vector<uint8_t>& dg) {
void MOTManager::HandleMOTDataGroup(const std::vector<uint8_t>& dg) {
size_t offset = 0;

// parse/check headers
Expand All @@ -295,11 +291,11 @@ bool MOTManager::HandleMOTDataGroup(const std::vector<uint8_t>& dg) {
size_t seg_size;

if(!ParseCheckDataGroupHeader(dg, offset, dg_type))
return false;
return;
if(!ParseCheckSessionHeader(dg, offset, last_seg, seg_number, transport_id))
return false;
return;
if(!ParseCheckSegmentationHeader(dg, offset, seg_size))
return false;
return;


// add segment to MOT object (reset if necessary)
Expand All @@ -314,6 +310,7 @@ bool MOTManager::HandleMOTDataGroup(const std::vector<uint8_t>& dg) {
// fprintf(stderr, "dg_type: %d, seg_number: %2d%s, transport_id: %5d, size: %4zu; display: %s\n",
// dg_type, seg_number, last_seg ? " (LAST)" : "", transport_id, seg_size, display ? "true" : "false");

// if object shall be shown, update it
return display;
// if object shall be shown, forward it
if(display)
observer->MOTFileCompleted(object.GetFile());
}
18 changes: 14 additions & 4 deletions src/mot_manager.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
DABlin - capital DAB experience
Copyright (C) 2016-2018 Stefan Pöschel
Copyright (C) 2016-2022 Stefan Pöschel
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -104,21 +104,31 @@ class MOTObject {
};


// --- MOTManagerObserver -----------------------------------------------------------------
class MOTManagerObserver {
public:
virtual ~MOTManagerObserver() {}

virtual void MOTFileCompleted(const MOT_FILE& /*file*/) {}
};


// --- MOTManager -----------------------------------------------------------------
class MOTManager {
private:
MOTManagerObserver *observer;

MOTObject object;
int current_transport_id;

bool ParseCheckDataGroupHeader(const std::vector<uint8_t>& dg, size_t& offset, int& dg_type);
bool ParseCheckSessionHeader(const std::vector<uint8_t>& dg, size_t& offset, bool& last_seg, int& seg_number, int& transport_id);
bool ParseCheckSegmentationHeader(const std::vector<uint8_t>& dg, size_t& offset, size_t& seg_size);
public:
MOTManager();
MOTManager(MOTManagerObserver *observer) : observer(observer) {Reset();}

void Reset();
bool HandleMOTDataGroup(const std::vector<uint8_t>& dg);
MOT_FILE GetFile() {return object.GetFile();}
void HandleMOTDataGroup(const std::vector<uint8_t>& dg);
};

#endif /* MOT_MANAGER_H_ */
56 changes: 33 additions & 23 deletions src/pad_decoder.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
DABlin - capital DAB experience
Copyright (C) 2015-2021 Stefan Pöschel
Copyright (C) 2015-2022 Stefan Pöschel
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand All @@ -24,6 +24,18 @@ const size_t XPAD_CI::lens[] = {4, 6, 8, 12, 16, 24, 32, 48};


// --- PADDecoder -----------------------------------------------------------------
PADDecoder::PADDecoder(PADDecoderObserver *observer, bool loose) {
this->observer = observer;
this->loose = loose;
mot_app_type = -1;

mot_manager = new MOTManager(this);
}

PADDecoder::~PADDecoder() {
delete mot_manager;
}

void PADDecoder::Reset() {
mot_app_type = -1;

Expand All @@ -32,7 +44,7 @@ void PADDecoder::Reset() {
dl_decoder.Reset();
dgli_decoder.Reset();
mot_decoder.Reset();
mot_manager.Reset();
mot_manager->Reset();
}

void PADDecoder::Process(const uint8_t *xpad_data, size_t xpad_len, bool exact_xpad_len, const uint8_t* fpad_data) {
Expand Down Expand Up @@ -163,27 +175,8 @@ void PADDecoder::Process(const uint8_t *xpad_data, size_t xpad_len, bool exact_x
mot_decoder.SetLen(dgli_len);

// if new Data Group available, append it
if(mot_decoder.ProcessDataSubfield(start, xpad + xpad_offset, xpad_ci.len)) {
// if new slide available, show it
if(mot_manager.HandleMOTDataGroup(mot_decoder.GetMOTDataGroup())) {
const MOT_FILE new_slide = mot_manager.GetFile();

// check file type
bool show_slide = true;
if(new_slide.content_type != MOT_FILE::CONTENT_TYPE_IMAGE)
show_slide = false;
switch(new_slide.content_sub_type) {
case MOT_FILE::CONTENT_SUB_TYPE_JFIF:
case MOT_FILE::CONTENT_SUB_TYPE_PNG:
break;
default:
show_slide = false;
}

if(show_slide)
observer->PADChangeSlide(new_slide);
}
}
if(mot_decoder.ProcessDataSubfield(start, xpad + xpad_offset, xpad_ci.len))
mot_manager->HandleMOTDataGroup(mot_decoder.GetMOTDataGroup());

xpad_ci_type_continued = mot_app_type + 1;
}
Expand All @@ -200,6 +193,23 @@ void PADDecoder::Process(const uint8_t *xpad_data, size_t xpad_len, bool exact_x
last_xpad_ci.type = xpad_ci_type_continued;
}

void PADDecoder::MOTFileCompleted(const MOT_FILE& file) {
// check file type
bool show_slide = true;
if(file.content_type != MOT_FILE::CONTENT_TYPE_IMAGE)
show_slide = false;
switch(file.content_sub_type) {
case MOT_FILE::CONTENT_SUB_TYPE_JFIF:
case MOT_FILE::CONTENT_SUB_TYPE_PNG:
break;
default:
show_slide = false;
}

if(show_slide)
observer->PADChangeSlide(file);
}


// --- DataGroup -----------------------------------------------------------------
DataGroup::DataGroup(size_t dg_size_max) {
Expand Down
11 changes: 7 additions & 4 deletions src/pad_decoder.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
DABlin - capital DAB experience
Copyright (C) 2015-2021 Stefan Pöschel
Copyright (C) 2015-2022 Stefan Pöschel
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -211,7 +211,7 @@ class PADDecoderObserver {


// --- PADDecoder -----------------------------------------------------------------
class PADDecoder {
class PADDecoder : public MOTManagerObserver {
private:
PADDecoderObserver *observer;
bool loose;
Expand All @@ -223,9 +223,12 @@ class PADDecoder {
DynamicLabelDecoder dl_decoder;
DGLIDecoder dgli_decoder;
MOTDecoder mot_decoder;
MOTManager mot_manager;
MOTManager *mot_manager;

void MOTFileCompleted(const MOT_FILE& file);
public:
PADDecoder(PADDecoderObserver *observer, bool loose) : observer(observer), loose(loose), mot_app_type(-1) {}
PADDecoder(PADDecoderObserver *observer, bool loose);
~PADDecoder();

void SetMOTAppType(int mot_app_type) {this-> mot_app_type = mot_app_type;}
void Process(const uint8_t *xpad_data, size_t xpad_len, bool exact_xpad_len, const uint8_t* fpad_data);
Expand Down

0 comments on commit 2675f14

Please sign in to comment.