Skip to content

Commit

Permalink
Refacoring, get rid of Date and Calendar
Browse files Browse the repository at this point in the history
  • Loading branch information
EricBerendsen committed Aug 23, 2024
1 parent c961838 commit 24692ed
Show file tree
Hide file tree
Showing 8 changed files with 614 additions and 647 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@
import static nl.digitalekabeltelevisie.util.Utils.*;

import java.io.*;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
Expand Down Expand Up @@ -143,7 +147,7 @@ public class TransportStream implements TreeNode{
/**
* time at which this transportStream started. Calculated by calculating backwards from first TDT, using bitrate. null if no TDT found
*/
private Calendar zeroTime;
private LocalDateTime zeroTime;

private final long len;

Expand Down Expand Up @@ -904,11 +908,11 @@ private void calculateBitrateTDT() {
TDTsection first = tdtSectionList.getFirst();
TDTsection last = tdtSectionList.getLast();
long diffPacket = (long)last.getPacket_no() - first.getPacket_no();
Calendar utcCalenderLast = getUTCCalender(last.getUTC_time());
Calendar utcCalenderFirst = getUTCCalender(first.getUTC_time());
LocalDateTime utcCalenderLast = getUTCLocalDateTime(last.getUTC_time());
LocalDateTime utcCalenderFirst = getUTCLocalDateTime(first.getUTC_time());
// getUTCCalender might fail if not correct BCD, then will return null.
if((utcCalenderLast!=null)&&(utcCalenderFirst!=null)){
long timeDiffMills = utcCalenderLast.getTimeInMillis()- utcCalenderFirst.getTimeInMillis();
long timeDiffMills = utcCalenderFirst.until(utcCalenderLast, ChronoUnit.MILLIS);
if(timeDiffMills> 0L){ // shit happens... capture.guangdong has 10 with same timestamp....
bitRateTDT = (diffPacket * packetLength * 8 * 1000)/timeDiffMills;
}
Expand All @@ -922,11 +926,10 @@ private void calculateZeroTime() {
List<TDTsection> tdtSectionList = psi.getTdt().getTdtSectionList();
if (!tdtSectionList.isEmpty()) {
TDTsection first = tdtSectionList.getFirst();
Calendar firstTime = getUTCCalender(first.getUTC_time());
LocalDateTime firstTime = getUTCLocalDateTime(first.getUTC_time());
if (firstTime != null) {
long millsIntoStream = ((long) first.getPacket_no() * packetLength * 8 * 1000) / getBitRate();
firstTime.add(Calendar.MILLISECOND, (int) -millsIntoStream);
zeroTime = firstTime;
zeroTime = firstTime.minus (millsIntoStream, ChronoUnit.MILLIS);
}
}
}
Expand Down Expand Up @@ -1013,33 +1016,34 @@ public String getPacketTime(int packetNo){
return packetNo + " (packetNo)";
}
if (zeroTime == null) {
Calendar calendar = new GregorianCalendar();
calendar.setTimeZone(TimeZone.getTimeZone("GMT"));
calendar.setTimeInMillis(0L);
calendar.add(Calendar.MILLISECOND, getTimeFromStartInMilliSecs(packetNo));
// return only the hours/min,secs and millisecs. Not TS recording will last days
return getFormattedTime(calendar);

Instant instant = Instant.ofEpochMilli(getTimeFromStartInMilliSecs(packetNo));
LocalDateTime ldt = instant.atZone(ZoneId.of("Z")).toLocalDateTime();
return getFormattedTime(ldt);
}
Calendar calendar = (Calendar) zeroTime.clone();
// calculation in long, intermediate results can be > Integer.MAX_VALUE
LocalDateTime packetTime = zeroTime.plusNanos(1_000_000L * getTimeFromStartInMilliSecs( packetNo));

calendar.add(Calendar.MILLISECOND, getTimeFromStartInMilliSecs( packetNo));
return getFormattedDateTime(calendar);
return getFormattedDateTime(packetTime);
}

private int getTimeFromStartInMilliSecs(int packetNo) {
return (int) (((long) packetNo * packetLength * 8 * 1000) / getBitRate());
// calculation in long, intermediate results can be > Integer.MAX_VALUE
return (int) ((((long) packetNo) * packetLength * 8 * 1000L) / getBitRate());
}

private static String getFormattedDateTime(Calendar calendar) {

private static String getFormattedDateTime(LocalDateTime calendar) {
return(String.format("%1$tY/%1$tm/%1$td %1$tHh%1$tMm%1$tS:%1$tL", calendar));
}

private static String getFormattedTime(Calendar calendar) {
private static String getFormattedTime(LocalDateTime calendar) {

return(String.format("%1$tHh%1$tMm%1$tS:%1$tL", calendar));
}


/**
* TODO the parameter packetNoOrPCR has two different meaning, because BitRateChat and TimeStampChart use different X-axis for aVCHD/DVB Full stream
* This should be fixed somewhere else ???
Expand All @@ -1054,18 +1058,12 @@ public String getShortPacketTime(long packetNoOrPCR){
}

if(getBitRate()!=-1){ //can't calculate time without a bitrate
Calendar calendar;
if(zeroTime==null){
calendar = new GregorianCalendar();
calendar.setTimeZone(TimeZone.getTimeZone("GMT"));
calendar.setTimeInMillis(0);
}else{
calendar = (Calendar)zeroTime.clone();
Instant instant = Instant.ofEpochMilli(getTimeFromStartInMilliSecs((int)packetNoOrPCR));
return getFormattedTime(instant.atZone(ZoneId.of("Z")).toLocalDateTime());
}
calendar.add(Calendar.MILLISECOND, getTimeFromStartInMilliSecs((int)packetNoOrPCR));
// return only the hours/min,secs and millisecs. No TS recording will last days

return getFormattedTime(calendar);
return getFormattedTime(zeroTime.plusNanos(1_000_000L * getTimeFromStartInMilliSecs((int) packetNoOrPCR)));

} // no bitrate
return packetNoOrPCR +" (packetNo)";
Expand Down
101 changes: 47 additions & 54 deletions src/main/java/nl/digitalekabeltelevisie/data/mpeg/psi/EIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*
* http://www.digitalekabeltelevisie.nl/dvb_inspector
*
* This code is Copyright 2009-2022 by Eric Berendsen ([email protected])
* This code is Copyright 2009-2024 by Eric Berendsen ([email protected])
*
* This file is part of DVB Inspector.
*
Expand All @@ -28,20 +28,13 @@

import static nl.digitalekabeltelevisie.data.mpeg.descriptors.Descriptor.findDescriptorApplyListFunc;
import static nl.digitalekabeltelevisie.util.Utils.addListJTree;
import static nl.digitalekabeltelevisie.util.Utils.getDurationMillis;
import static nl.digitalekabeltelevisie.util.Utils.getUTCDate;
import static nl.digitalekabeltelevisie.util.Utils.getDurationSeconds;
import static nl.digitalekabeltelevisie.util.Utils.isUndefined;
import static nl.digitalekabeltelevisie.util.Utils.simpleModus;

import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.time.LocalDateTime;
import java.util.*;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.Set;
import java.util.TreeMap;
import java.util.function.IntPredicate;
import java.util.logging.Level;
import java.util.logging.Logger;
Expand Down Expand Up @@ -84,16 +77,16 @@ public class EIT extends AbstractPSITabel{
private static final Logger logger = Logger.getLogger(EIT.class.getName());


public EIT(final PSI parent){
public EIT(PSI parent){
super(parent);
}

public void update(final EITsection section){
public void update(EITsection section){

final int original_network_id = section.getOriginalNetworkID();
final int streamId = section.getTransportStreamID();
final int serviceId = section.getServiceID();
final int tableId = section.getTableId();
int original_network_id = section.getOriginalNetworkID();
int streamId = section.getTransportStreamID();
int serviceId = section.getServiceID();
int tableId = section.getTableId();

TreeMap<Integer, TreeMap<Integer, TreeMap<Integer, EITsection[]>>> networkSections = newEit.computeIfAbsent(original_network_id,k -> new TreeMap<>());
TreeMap<Integer, TreeMap<Integer, EITsection[]>> programStreamSections = networkSections.computeIfAbsent(streamId,k -> new TreeMap<>());
Expand All @@ -108,51 +101,51 @@ public void update(final EITsection section){
if(tableSectionArray[section.getSectionNumber()]==null){
tableSectionArray[section.getSectionNumber()] = section;
}else{
final TableSection last = tableSectionArray[section.getSectionNumber()];
TableSection last = tableSectionArray[section.getSectionNumber()];
updateSectionVersion(section, last);
}
}

@Override
public DefaultMutableTreeNode getJTreeNode(final int modus) {
public DefaultMutableTreeNode getJTreeNode(int modus) {

// need this KVP at end of loop to set ImageSource
final KVP eitKVP = new KVP("EIT");
final DefaultMutableTreeNode t = new DefaultMutableTreeNode(eitKVP);
KVP eitKVP = new KVP("EIT");
DefaultMutableTreeNode t = new DefaultMutableTreeNode(eitKVP);

Map<ServiceIdentification, EITsection[]> allEitImageMap = new TreeMap<>();

for(Entry<Integer, TreeMap<Integer, TreeMap<Integer, TreeMap<Integer, EITsection[]>>>> network:newEit.entrySet()) {
final Integer orgNetworkId= network.getKey();
TreeMap<Integer, TreeMap<Integer, TreeMap<Integer, EITsection[]>>> networkSections = network.getValue();
Integer orgNetworkId= network.getKey();
SortedMap<Integer, TreeMap<Integer, TreeMap<Integer, EITsection[]>>> networkSections = network.getValue();

// need this KVP at end of loop to set ImageSource
final KVP networkNodeKVP = new KVP("original_network_id", orgNetworkId,Utils.getOriginalNetworkIDString(orgNetworkId));
final DefaultMutableTreeNode networkNode = new DefaultMutableTreeNode(networkNodeKVP);
KVP networkNodeKVP = new KVP("original_network_id", orgNetworkId,Utils.getOriginalNetworkIDString(orgNetworkId));
DefaultMutableTreeNode networkNode = new DefaultMutableTreeNode(networkNodeKVP);
t.add(networkNode);

Map<ServiceIdentification, EITsection[]> networkImageMap = new TreeMap<>();

for(Entry<Integer, TreeMap<Integer, TreeMap<Integer, EITsection[]>>> netWorkSection:networkSections.entrySet()) {
final Integer transport_stream_id = netWorkSection.getKey();
TreeMap<Integer, TreeMap<Integer, EITsection[]>> streams = netWorkSection.getValue();
Integer transport_stream_id = netWorkSection.getKey();
SortedMap<Integer, TreeMap<Integer, EITsection[]>> streams = netWorkSection.getValue();

// need this KVP at end of loop to set ImageSource
final KVP streamNodeKVP = new KVP("transport_stream_id", transport_stream_id,null);
final DefaultMutableTreeNode streamNode = new DefaultMutableTreeNode(streamNodeKVP);
KVP streamNodeKVP = new KVP("transport_stream_id", transport_stream_id,null);
DefaultMutableTreeNode streamNode = new DefaultMutableTreeNode(streamNodeKVP);
networkNode.add(streamNode);

TreeMap<ServiceIdentification, EITsection[]> streamImageMap = new TreeMap<>();

for(Entry<Integer, TreeMap<Integer, EITsection[]>> streamEntry: streams.entrySet()) {
final Integer serviceId = streamEntry.getKey();
TreeMap<Integer, EITsection[]> service = streamEntry.getValue();
Integer serviceId = streamEntry.getKey();
SortedMap<Integer, EITsection[]> service = streamEntry.getValue();

// for EITImage, sections of this service with tableID >80
EITsection[] serviceSections = new EITsection[0];

final KVP serviceNodeKVP = new KVP("service_id", serviceId,getParentPSI().getSdt().getServiceName(orgNetworkId, transport_stream_id, serviceId));
final DefaultMutableTreeNode serviceNode = new DefaultMutableTreeNode(serviceNodeKVP);
KVP serviceNodeKVP = new KVP("service_id", serviceId,getParentPSI().getSdt().getServiceName(orgNetworkId, transport_stream_id, serviceId));
DefaultMutableTreeNode serviceNode = new DefaultMutableTreeNode(serviceNodeKVP);

serviceNodeKVP.addHTMLSource(() -> service.entrySet().
stream().
Expand All @@ -172,7 +165,7 @@ public DefaultMutableTreeNode getJTreeNode(final int modus) {
Integer tableId = serviceEntry.getKey();
EITsection[] sections = serviceEntry.getValue();

final KVP tableNodeKVP = new KVP("tableid", tableId,TableSection.getTableType(tableId));
KVP tableNodeKVP = new KVP("tableid", tableId,TableSection.getTableType(tableId));
tableNodeKVP.addHTMLSource(() -> Arrays.stream(sections).
filter(Objects::nonNull).
map(e -> e.getHtmlForEit(modus)).
Expand All @@ -181,7 +174,7 @@ public DefaultMutableTreeNode getJTreeNode(final int modus) {
);

tableNodeKVP.addTableSource(()->getTableModel(sections), "Events");
final DefaultMutableTreeNode tableNode = new DefaultMutableTreeNode(tableNodeKVP);
DefaultMutableTreeNode tableNode = new DefaultMutableTreeNode(tableNodeKVP);
serviceNode.add(tableNode);

if(tableId>=80) {
Expand All @@ -191,11 +184,11 @@ public DefaultMutableTreeNode getJTreeNode(final int modus) {

for (EITsection section : sections) {
if(section!= null){
if(!simpleModus(modus)){
addSectionVersionsToJTree(tableNode, section, modus);
}else{
addListJTree(tableNode,section.getEventList(),modus,"events");
}
if (simpleModus(modus)) {
addListJTree(tableNode, section.getEventList(), modus, "events");
} else {
addSectionVersionsToJTree(tableNode, section, modus);
}
}
}
}
Expand Down Expand Up @@ -299,28 +292,28 @@ private static EITsection[] appendSections(EITsection[] serviceSections, EITsect
* @param eitTable map of service IDs to EITSection[] Can contain sections from different Table IDs, like 0x50 and 0x51, etc... (for very long EPGs)
* @return Interval that covers all events in eitTable
*/
public static Interval getSpanningInterval(final Set<ServiceIdentification> serviceSet, Map<ServiceIdentification, EITsection[]> eitTable) {
Date startDate = null;
Date endDate = null;
public static Interval getSpanningInterval(Set<ServiceIdentification> serviceSet, Map<ServiceIdentification, EITsection[]> eitTable) {
LocalDateTime startDate = null;
LocalDateTime endDate = null;
// services to be displayed

for(final ServiceIdentification serviceNo : serviceSet){
for(final EITsection section :eitTable.get(serviceNo)){
for(ServiceIdentification serviceNo : serviceSet){
for(EITsection section :eitTable.get(serviceNo)){
if(section!= null){
List<Event> eventList = section.getEventList();
for(Event event:eventList){
final byte[] startTime = event.getStartTime();
byte[] startTime = event.getStartTime();
if(isUndefined(startTime)){ // undefined start time
continue;
}
Date eventStart = getUTCDate( startTime);
if((startDate==null)||(startDate.after(eventStart))){
LocalDateTime eventStart = Utils.getUTCLocalDateTime(startTime);
if((startDate==null)||(startDate.isAfter(eventStart))){
startDate = eventStart;
}
if(eventStart!=null){
try{
Date eventEnd = new Date(eventStart.getTime()+ getDurationMillis(event.getDuration()));
if((endDate==null)||(endDate.before(eventEnd))){
LocalDateTime eventEnd =eventStart.plusSeconds(getDurationSeconds(event.getDuration()));
if((endDate==null)||(endDate.isBefore(eventEnd))){
endDate = eventEnd;
}
}catch(NumberFormatException nfe){
Expand Down Expand Up @@ -350,15 +343,15 @@ public Map<ServiceIdentification, EITsection[]> getFlatEit(IntPredicate schedule

for (Entry<Integer, TreeMap<Integer, TreeMap<Integer, TreeMap<Integer, EITsection[]>>>> networkEntry : newEit.entrySet()) {
int orgNetworkId = networkEntry.getKey();
TreeMap<Integer, TreeMap<Integer, TreeMap<Integer, EITsection[]>>> network = networkEntry.getValue();
SortedMap<Integer, TreeMap<Integer, TreeMap<Integer, EITsection[]>>> network = networkEntry.getValue();

for (Entry<Integer, TreeMap<Integer, TreeMap<Integer, EITsection[]>>> streamEntry : network.entrySet()) {
int streamId = streamEntry.getKey();
TreeMap<Integer, TreeMap<Integer, EITsection[]>> stream = streamEntry.getValue();
SortedMap<Integer, TreeMap<Integer, EITsection[]>> stream = streamEntry.getValue();

for (Entry<Integer, TreeMap<Integer, EITsection[]>> serviceEntry : stream.entrySet()) {
int serviceId = serviceEntry.getKey();
TreeMap<Integer, EITsection[]> service = serviceEntry.getValue();
SortedMap<Integer, EITsection[]> service = serviceEntry.getValue();

for (Entry<Integer, EITsection[]> tableEntry : service.entrySet()) {
int tableId = tableEntry.getKey();
Expand Down Expand Up @@ -391,7 +384,7 @@ static TableHeader<EITsection, Event> buildEitTableHeader() {

.addOptionalRepeatingRowColumn("rating ", component -> findDescriptorApplyListFunc(
component.getDescriptorList(), ParentalRatingDescriptor.class,
ratingDescriptor -> ratingDescriptor.getRatingList().stream().collect(Collectors.toList())),
ratingDescriptor -> new ArrayList<>(ratingDescriptor.getRatingList())),
ParentalRatingDescriptor.Rating.class)
.build();
}
Expand Down
Loading

0 comments on commit 24692ed

Please sign in to comment.