1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
|
/*
Copyright (C) 2009 Adam Charrett
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 the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
patprocessor.c
Process Program Association Tables and update the services information.
*/
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdint.h>
#include <assert.h>
#include "multiplexes.h"
#include "services.h"
#include "dvbadapter.h"
#include "ts.h"
#include "standard/mpeg2.h"
#include "patprocessor.h"
#include "cache.h"
#include "logging.h"
#include "main.h"
#include "list.h"
/*******************************************************************************
* Typedefs *
*******************************************************************************/
struct PATProcessor_s
{
TSFilterGroup_t *tsgroup;
Multiplex_t *multiplex;
dvbpsi_handle pathandle;
};
/*******************************************************************************
* Prototypes *
*******************************************************************************/
static void PATProcessorFilterEventCallback(void *userArg, struct TSFilterGroup_t *group, TSFilterEventType_e event, void *details);
static void PATHandler(void* arg, dvbpsi_pat_t* newpat);
/*******************************************************************************
* Global variables *
*******************************************************************************/
static char PATPROCESSOR[] = "PATProcessor";
static Event_t patEvent = NULL;
/*******************************************************************************
* Global functions *
*******************************************************************************/
PATProcessor_t PATProcessorCreate(TSReader_t *reader)
{
PATProcessor_t state;
if (patEvent == NULL)
{
patEvent = EventsRegisterEvent(MPEG2EventSource, "PAT", NULL);
}
ObjectRegisterClass("PATProcessor_t", sizeof(struct PATProcessor_s), NULL);
state = ObjectCreateType(PATProcessor_t);
if (state)
{
state->tsgroup = TSReaderCreateFilterGroup(reader, PATPROCESSOR, MPEG2FilterType, PATProcessorFilterEventCallback, state);
}
return state;
}
void PATProcessorDestroy(PATProcessor_t processor)
{
TSFilterGroupDestroy(processor->tsgroup);
if (processor->multiplex)
{
dvbpsi_DetachPAT(processor->pathandle);
MultiplexRefDec(processor->multiplex);
}
ObjectRefDec(processor);
}
/*******************************************************************************
* Local Functions *
*******************************************************************************/
static void PATProcessorFilterEventCallback(void *userArg, struct TSFilterGroup_t *group, TSFilterEventType_e event, void *details)
{
PATProcessor_t state= (PATProcessor_t)userArg;
if (event == TSFilterEventType_MuxChanged)
{
if (state->multiplex)
{
MultiplexRefDec(state->multiplex);
TSFilterGroupRemoveSectionFilter(state->tsgroup, 0);
dvbpsi_DetachPAT(state->pathandle);
}
state->multiplex = details;
if (details)
{
MultiplexRefInc(state->multiplex);
state->pathandle = dvbpsi_AttachPAT(PATHandler, (void*)state);
TSFilterGroupAddSectionFilter(state->tsgroup, 0, -1, state->pathandle);
}
}
}
static void PATHandler(void* arg, dvbpsi_pat_t* newpat)
{
PATProcessor_t state = (PATProcessor_t)arg;
Multiplex_t *multiplex = state->multiplex;
int count,i;
Service_t **services;
dvbpsi_pat_program_t *patentry;
LogModule(LOG_DEBUG, PATPROCESSOR, "PAT recieved, version %d (old version %d)\n", newpat->i_version, multiplex->patVersion);
if (multiplex->patVersion == -1)
{
/* Cause a TS Structure change call back*/
state->tsgroup->tsReader->tsStructureChanged = TRUE;
}
/* Version has changed update the services */
for (patentry = newpat->p_first_program; patentry; patentry = patentry->p_next)
{
LogModule(LOG_DEBUG, PATPROCESSOR, "Service 0x%04x PMT PID 0x%04x\n", patentry->i_number, patentry->i_pid);
if (patentry->i_number != 0x0000)
{
Service_t *service = CacheServiceFindId(patentry->i_number);
if (!service)
{
LogModule(LOG_DEBUG, PATPROCESSOR, "Service not found in cache while processing PAT, adding 0x%04x\n", patentry->i_number);
service = CacheServiceAdd(patentry->i_number, patentry->i_number);
/* Cause a TS Structure change call back*/
state->tsgroup->tsReader->tsStructureChanged = TRUE;
}
else
{
CacheServiceSeen(service, TRUE, TRUE);
}
if (service && (service->pmtPID != patentry->i_pid))
{
CacheUpdateServicePMTPID(service, patentry->i_pid);
}
if (service)
{
ServiceRefDec(service);
}
}
}
/* Delete any services that no longer exist */
services = CacheServicesGet(&count);
for (i = 0; i < count; i ++)
{
bool found = FALSE;
for (patentry = newpat->p_first_program; patentry; patentry = patentry->p_next)
{
if (services[i]->id == patentry->i_number)
{
found = TRUE;
break;
}
}
if (!found)
{
LogModule(LOG_DEBUG, PATPROCESSOR, "Service not found in PAT while checking cache, deleting 0x%04x (%s)\n",
services[i]->id, services[i]->name);
if (!CacheServiceSeen(services[i], FALSE, TRUE))
{
CacheServicesRelease();
CacheServiceDelete(services[i]);
services = CacheServicesGet(&count);
i --;
/* Cause a TS Structure change call back*/
state->tsgroup->tsReader->tsStructureChanged = TRUE;
}
}
}
CacheServicesRelease();
CacheUpdateMultiplex(multiplex, newpat->i_version, newpat->i_ts_id);
EventsFireEventListeners(patEvent, newpat);
ObjectRefDec(newpat);
}
|