Annotation of multiplexer/splice.c, revision 1.4

1.1       oskar       1: /*
                      2:  * ISO 13818 stream multiplexer
                      3:  * Copyright (C) 2001 Convergence Integrated Media GmbH Berlin
                      4:  * Author: Oskar Schirmer (oskar@convergence.de)
                      5:  */
                      6: 
                      7: /*
                      8:  * Module:  Splice
                      9:  * Purpose: Service functions for specific Splice* modules
                     10:  *
                     11:  * This module provides functions needed for splicing
                     12:  * which are independent from the splice type.
                     13:  */
                     14: 
                     15: #include "global.h"
                     16: #include "error.h"
                     17: #include "splice.h"
                     18: #include "input.h"
                     19: #include "pes.h"
                     20: 
1.2       oskar      21: /* Connect a stream with a target program.
                     22:  * programnb denotes the program to connect,
                     23:  * stream is the stream to connect,
                     24:  * all further parameters are as with input_openstream.
                     25:  * If stream is NULL, open a stream first.
                     26:  * Add the stream to the programs list of streams and vice versa.
                     27:  * Precondition: f!=NULL
                     28:  * Return: the changed stream on success, the unchanged "stream" otherwise
                     29:  */
1.1       oskar      30: stream_descr *connect_streamprog (file_descr *f,
                     31:     int programnb,
                     32:     int sourceid,
                     33:     int streamid,
                     34:     int streamtype,
                     35:     stream_descr *stream,
                     36:     stream_descr *mapstream,
                     37:     boolean mention)
                     38: {
                     39:   stream_descr *s;
                     40:   prog_descr *p;
                     41:   if (stream == NULL) {
                     42:     s = input_openstream (f,sourceid,streamid<0?-streamid:streamid,
1.3       oskar      43:             streamtype,sd_data,mapstream);
1.1       oskar      44:   } else {
                     45:     if (streamid < 0) {
                     46:       streamid = -streamid;
1.4     ! oskar      47:       warn (LWAR,"Cannot refind sid",ESPC,1,1,streamid);
1.1       oskar      48:     }
                     49:     s = stream;
                     50:   }
                     51:   if (s != NULL) {
                     52:     p = splice_openprog (programnb);
                     53:     if (p != NULL) {
                     54:       if (input_addprog (s,p)) {
                     55:         if (splice_addstream (p,s,streamid>=0) > 0) {
                     56: /*
                     57:           if (p->pcr_pid < 0) {
                     58:             if (xxx) {
                     59:               p->pcr_pid = s->u.d.pid;
                     60:               s->u.d.has_clockref = TRUE;
                     61:               s->u.d.next_clockref = msec_now () - MAX_MSEC_PCRDIST;
                     62:             }
                     63:           }
                     64: */
                     65:           s->endaction = ENDSTR_WAIT;
                     66:           s->u.d.mention = mention;
                     67:           return (s);
                     68:         }
                     69:         input_delprog (s,p);
                     70:       }
                     71:       if (p->streams <= 0) {
                     72:         splice_closeprog (p);
                     73:       }
                     74:     }
                     75:     if (stream == NULL) {
                     76:       input_closestream (s);
                     77:     }
                     78:   }
                     79:   return (stream);
                     80: }
1.2       oskar      81: 
                     82: /* Unlink a stream from a target program.
                     83:  * If the stream comes out to be in no program then, close it.
                     84:  *   This function may be used only, if the program in question will either
                     85:  *   be non-empty after the call, or will be closed by the calling function.
                     86:  * Precondition: s!=NULL, p!=NULL
                     87:  */
1.1       oskar      88: void unlink_streamprog (stream_descr *s,
                     89:     prog_descr *p)
                     90: {
                     91:   splice_delstream (p,s);
                     92:   input_delprog (s,p);
                     93:   if (s->u.d.progs <= 0) {
                     94:     file_descr *f;
                     95:     f = s->fdescr;
                     96:     input_closestream (s);
                     97:     input_closefileifunused (f);
                     98:   }
                     99: }
1.2       oskar     100: 
                    101: /* Remove a stream from a target program.
                    102:  * Close stream and/or program, if not contained in another program or stream.
                    103:  * Precondition: s!=NULL, p!=NULL, s is stream in target program p
                    104:  */
1.1       oskar     105: void remove_streamprog (stream_descr *s,
                    106:     prog_descr *p)
                    107: {
1.2       oskar     108:   if (p->streams > 1) {
                    109:     unlink_streamprog (s,p);
                    110:   } else {
                    111:     splice_closeprog (p);
1.1       oskar     112:   }
                    113: }
                    114: 
1.2       oskar     115: /* Find the right stream in a program
                    116:  * Precondition: p!=NULL
                    117:  * Return: stream, if found, NULL otherwise
                    118:  */
1.1       oskar     119: stream_descr *get_streamprog (prog_descr *p,
                    120:     int streamid)
                    121: {
                    122:   int i;
                    123:   i = p->streams;
                    124:   while (--i >= 0) {
                    125:     stream_descr *s;
                    126:     s = p->stream[i];
                    127:     if (s->stream_id == streamid) {
                    128:       return (s);
                    129:     }
                    130:   }
                    131:   return (NULL);
                    132: }
                    133: 
1.2       oskar     134: /* Find a free stream ID in a program that is equivalent to the given stream id
                    135:  * Precondition: p!=NULL
                    136:  * Return: Free ID, if found; given sid otherwise.
                    137:  */
1.1       oskar     138: int splice_findfreestreamid (prog_descr *p,
                    139:     int sid)
                    140: {
1.2       oskar     141:   int s0, s, n;
                    142:   s0 = sid;
1.1       oskar     143:   if ((sid >= PES_CODE_AUDIO)
                    144:    && (sid < (PES_CODE_AUDIO+PES_NUMB_AUDIO))) {
                    145:     s = PES_CODE_AUDIO;
1.2       oskar     146:     n = PES_NUMB_AUDIO;
1.1       oskar     147:   } else if ((sid >= PES_CODE_VIDEO)
                    148:    && (sid < (PES_CODE_VIDEO+PES_NUMB_VIDEO))) {
                    149:     s = PES_CODE_VIDEO;
1.2       oskar     150:     n = PES_NUMB_VIDEO;
1.1       oskar     151:   } else {
                    152:     s = sid;
1.2       oskar     153:     n = 1;
1.1       oskar     154:   }
                    155:   while (--n >= 0) {
                    156:     int i;
                    157:     i = p->streams;
                    158:     while ((--i >= 0)
1.2       oskar     159:         && (p->stream[i]->stream_id != s0)) {
1.1       oskar     160:     }
                    161:     if (i < 0) {
1.4     ! oskar     162:       warn (LIMP,"Found SID free",ESPC,2,sid,s0);
1.2       oskar     163:       return (s0);
1.1       oskar     164:     }
1.2       oskar     165:     s0 = s;
1.1       oskar     166:     s += 1;
                    167:   }
1.4     ! oskar     168:   warn (LIMP,"Found SID",ESPC,2,sid,sid);
1.2       oskar     169:   return (sid);
1.1       oskar     170: }
                    171: 
1.2       oskar     172: /* Check if there is a source pcr stream in a target program
                    173:  * Precondition: p!=NULL
                    174:  * Return: pcr-stream, if found; NULL otherwise.
                    175:  */
1.1       oskar     176: stream_descr *splice_findpcrstream (prog_descr *p)
                    177: {
                    178:   int i;
                    179:   pmt_descr *pmt;
1.4     ! oskar     180:   warn (LIMP,"Find PCR Stream",ESPC,3,0,p->program_number);
1.1       oskar     181:   i = p->streams;
                    182:   while (--i >= 0) {
                    183:     if (p->stream[i]->fdescr->content == ct_transport) {
                    184:       pmt = p->stream[i]->fdescr->u.ts.pat;
                    185:       while (pmt != NULL) {
                    186:         if (pmt->pcr_pid == p->stream[i]->sourceid) {
1.4     ! oskar     187:           warn (LIMP,"Found PCR Stream",ESPC,3,1,p->stream[i]->sourceid);
1.1       oskar     188:           return (p->stream[i]);
                    189:         }
                    190:         pmt = pmt->next;
                    191:       }
                    192:     }
                    193:   }
                    194:   return (NULL);
                    195: }
                    196: 

LinuxTV legacy CVS <linuxtv.org/cvs>