| 1 | /*************************************************************************** |
|---|
| 2 | * Copyright (c) 2003-2008, Broadcom Corporation |
|---|
| 3 | * All Rights Reserved |
|---|
| 4 | * Confidential Property of Broadcom Corporation |
|---|
| 5 | * |
|---|
| 6 | * THIS SOFTWARE MAY ONLY BE USED SUBJECT TO AN EXECUTED SOFTWARE LICENSE |
|---|
| 7 | * AGREEMENT BETWEEN THE USER AND BROADCOM. YOU HAVE NO RIGHT TO USE OR |
|---|
| 8 | * EXPLOIT THIS MATERIAL EXCEPT SUBJECT TO THE TERMS OF SUCH AN AGREEMENT. |
|---|
| 9 | * |
|---|
| 10 | * $brcm_Workfile: $ |
|---|
| 11 | * $brcm_Revision: $ |
|---|
| 12 | * $brcm_Date: $ |
|---|
| 13 | * |
|---|
| 14 | * Module Description: stream api implementation, utility module used to manage |
|---|
| 15 | * decode structures for FCC. API assumes all functions are called from the same thread. |
|---|
| 16 | * |
|---|
| 17 | * Revision History: |
|---|
| 18 | * |
|---|
| 19 | * $brcm_Log: $ |
|---|
| 20 | * |
|---|
| 21 | * |
|---|
| 22 | ***************************************************************************/ |
|---|
| 23 | #include "bsettop_stream.h" |
|---|
| 24 | #include "bsettop_p_stream.h" |
|---|
| 25 | #include "ts_psi.h" |
|---|
| 26 | |
|---|
| 27 | BDBG_MODULE(bstream); |
|---|
| 28 | |
|---|
| 29 | #define INVALID_PID 0xFFFF |
|---|
| 30 | |
|---|
| 31 | struct bstream |
|---|
| 32 | { |
|---|
| 33 | bool in_use; /* flag indicating the context is in use */ |
|---|
| 34 | int index; /* stream index */ |
|---|
| 35 | bband_t band; /* Band with possibly playback flag */ |
|---|
| 36 | bstream_mpeg mpeg; /* mpeg program pids */ |
|---|
| 37 | bsettop_p_stream_t stream_info; /* private stream parameters */ |
|---|
| 38 | }; |
|---|
| 39 | |
|---|
| 40 | static struct bstream s_streams[BSETTOP_MAX_STREAMS] = |
|---|
| 41 | { |
|---|
| 42 | { false, 0, DEF_PARSER_BAND, { { {INVALID_PID}},{{INVALID_PID, TS_PSI_ST_ATSC_AC3}},INVALID_PID}}, |
|---|
| 43 | { false, 1, DEF_PARSER_BAND, { { {INVALID_PID}},{{INVALID_PID, TS_PSI_ST_ATSC_AC3}},INVALID_PID}}, |
|---|
| 44 | { false, 2, DEF_PARSER_BAND, { { {INVALID_PID}},{{INVALID_PID, TS_PSI_ST_ATSC_AC3}},INVALID_PID}} |
|---|
| 45 | }; |
|---|
| 46 | static bstream_mpeg s_def_stream_mpeg = { {{INVALID_PID}}, {{ INVALID_PID,TS_PSI_ST_ATSC_AC3}},INVALID_PID}; |
|---|
| 47 | static bool s_bstream_init = false; |
|---|
| 48 | |
|---|
| 49 | /* |
|---|
| 50 | Summary: |
|---|
| 51 | Reset mpeg program structure to the default. |
|---|
| 52 | */ |
|---|
| 53 | |
|---|
| 54 | void bstream_mpeg_init(bstream_mpeg *mpeg) |
|---|
| 55 | { |
|---|
| 56 | |
|---|
| 57 | *mpeg = s_def_stream_mpeg; |
|---|
| 58 | return; |
|---|
| 59 | } |
|---|
| 60 | /* |
|---|
| 61 | Summary: |
|---|
| 62 | Returns the current global configuration settings. |
|---|
| 63 | */ |
|---|
| 64 | |
|---|
| 65 | bresult bstream_get_config(bstream_config *config) |
|---|
| 66 | { |
|---|
| 67 | BSTD_UNUSED(config); |
|---|
| 68 | BKNI_Memset(config,0,sizeof(bstream_config)); |
|---|
| 69 | return b_ok; |
|---|
| 70 | } |
|---|
| 71 | /* |
|---|
| 72 | Summary: |
|---|
| 73 | Sets the current global configuration settings. |
|---|
| 74 | */ |
|---|
| 75 | |
|---|
| 76 | bresult bstream_set_config(bstream_config *config) |
|---|
| 77 | { |
|---|
| 78 | BSTD_UNUSED(config); |
|---|
| 79 | return b_ok; |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | /* |
|---|
| 83 | Summary: |
|---|
| 84 | Find available stream. |
|---|
| 85 | */ |
|---|
| 86 | |
|---|
| 87 | static bstream_t bstream_p_find_stream(void) |
|---|
| 88 | { |
|---|
| 89 | int i; |
|---|
| 90 | |
|---|
| 91 | for (i = 0; i < BSETTOP_MAX_STREAMS; ++i) |
|---|
| 92 | { |
|---|
| 93 | if (!s_streams[i].in_use) |
|---|
| 94 | return(bstream_t)&s_streams[i]; |
|---|
| 95 | } |
|---|
| 96 | return NULL; |
|---|
| 97 | } |
|---|
| 98 | /* |
|---|
| 99 | Summary: |
|---|
| 100 | Module initialization function. |
|---|
| 101 | */ |
|---|
| 102 | |
|---|
| 103 | void bstream_init(NEXUS_VideoDecoderHandle videoDecodeHandle) |
|---|
| 104 | { |
|---|
| 105 | int i; |
|---|
| 106 | |
|---|
| 107 | if (s_bstream_init) |
|---|
| 108 | return; |
|---|
| 109 | |
|---|
| 110 | for (i = 0; i < BSETTOP_MAX_STREAMS; ++i) |
|---|
| 111 | { |
|---|
| 112 | BKNI_Memset(&(s_streams[i].stream_info),0,sizeof(s_streams[i].stream_info)); |
|---|
| 113 | s_streams[i].stream_info.videoDecoder = videoDecodeHandle; |
|---|
| 114 | #if 0 |
|---|
| 115 | s_streams[i].stream_info.primerHandle = NEXUS_VideoDecoder_OpenPrimer(videoDecodeHandle); |
|---|
| 116 | BDBG_ASSERT(s_streams[i].stream_info.primerHandle); |
|---|
| 117 | #endif |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | s_bstream_init = true; |
|---|
| 121 | |
|---|
| 122 | return; |
|---|
| 123 | } |
|---|
| 124 | /* |
|---|
| 125 | Summary: |
|---|
| 126 | Module deinitialization function. |
|---|
| 127 | */ |
|---|
| 128 | |
|---|
| 129 | void bstream_destroy(void) |
|---|
| 130 | { |
|---|
| 131 | int i; |
|---|
| 132 | |
|---|
| 133 | if (!s_bstream_init) |
|---|
| 134 | return; |
|---|
| 135 | |
|---|
| 136 | #if 0 |
|---|
| 137 | for (i = 0; i < BSETTOP_MAX_STREAMS; ++i) |
|---|
| 138 | { |
|---|
| 139 | if (s_streams[i].stream_info.primerHandle) |
|---|
| 140 | NEXUS_VideoDecoder_ClosePrimer(s_streams[i].stream_info.videoDecoder,s_streams[i].stream_info.primerHandle); |
|---|
| 141 | } |
|---|
| 142 | #endif |
|---|
| 143 | s_bstream_init = true; |
|---|
| 144 | |
|---|
| 145 | return; |
|---|
| 146 | } |
|---|
| 147 | |
|---|
| 148 | /* |
|---|
| 149 | Summary: |
|---|
| 150 | Open stream and allocate transport channels. |
|---|
| 151 | */ |
|---|
| 152 | |
|---|
| 153 | bstream_t bstream_open(bband_t band, const bstream_mpeg *mpeg) |
|---|
| 154 | { |
|---|
| 155 | bstream_t stream; |
|---|
| 156 | NEXUS_StcChannelSettings stcSettings; |
|---|
| 157 | |
|---|
| 158 | BDBG_ASSERT(mpeg); |
|---|
| 159 | BDBG_WRN(("%s:%d\n",__FUNCTION__,__LINE__)); |
|---|
| 160 | |
|---|
| 161 | stream = bstream_p_find_stream(); |
|---|
| 162 | |
|---|
| 163 | if (!stream) |
|---|
| 164 | { |
|---|
| 165 | BDBG_WRN(("%s no more streams available.\n",__FUNCTION__)); |
|---|
| 166 | goto ExitError; |
|---|
| 167 | } |
|---|
| 168 | |
|---|
| 169 | stream->band = band; |
|---|
| 170 | stream->mpeg = *mpeg; |
|---|
| 171 | |
|---|
| 172 | stream->stream_info.pcrPidChannel = NULL; |
|---|
| 173 | if (stream->mpeg.video[0].pid != 0) |
|---|
| 174 | { |
|---|
| 175 | stream->stream_info.videoPidChannel = NEXUS_PidChannel_Open(stream->band,stream->mpeg.video[0].pid,NULL); |
|---|
| 176 | if (!stream->stream_info.videoPidChannel) |
|---|
| 177 | { |
|---|
| 178 | BDBG_WRN(("NEXUS_PidChannel_Open Video PID = 0x%04x\n",stream->mpeg.video[0].pid)); |
|---|
| 179 | goto ExitError; |
|---|
| 180 | } |
|---|
| 181 | |
|---|
| 182 | if (stream->mpeg.pcr_pid == stream->mpeg.video[0].pid) |
|---|
| 183 | { |
|---|
| 184 | stream->stream_info.pcrPidChannel = stream->stream_info.videoPidChannel; |
|---|
| 185 | } |
|---|
| 186 | } |
|---|
| 187 | |
|---|
| 188 | if (stream->mpeg.audio[0].pid != 0) |
|---|
| 189 | { |
|---|
| 190 | stream->stream_info.audioPidChannel = NEXUS_PidChannel_Open(stream->band,stream->mpeg.audio[0].pid,NULL); |
|---|
| 191 | if (!stream->stream_info.audioPidChannel) |
|---|
| 192 | { |
|---|
| 193 | BDBG_WRN(("NEXUS_PidChannel_Open Audio PID = 0x%04x\n",stream->mpeg.audio[0].pid)); |
|---|
| 194 | goto ExitError; |
|---|
| 195 | } |
|---|
| 196 | if (stream->mpeg.pcr_pid == stream->mpeg.audio[0].pid) |
|---|
| 197 | { |
|---|
| 198 | stream->stream_info.pcrPidChannel = stream->stream_info.audioPidChannel; |
|---|
| 199 | } |
|---|
| 200 | } |
|---|
| 201 | |
|---|
| 202 | if ((stream->mpeg.pcr_pid != 0) && (stream->stream_info.pcrPidChannel == NULL)) |
|---|
| 203 | { |
|---|
| 204 | stream->stream_info.pcrPidChannel = NEXUS_PidChannel_Open(stream->band,stream->mpeg.pcr_pid,NULL); |
|---|
| 205 | if (!stream->stream_info.pcrPidChannel) |
|---|
| 206 | { |
|---|
| 207 | BDBG_WRN(("NEXUS_PidChannel_Open PCR PID = 0x%04x\n",stream->mpeg.pcr_pid)); |
|---|
| 208 | goto ExitError; |
|---|
| 209 | } |
|---|
| 210 | } |
|---|
| 211 | BDBG_ASSERT(stream->stream_info.pcrPidChannel); |
|---|
| 212 | |
|---|
| 213 | NEXUS_StcChannel_GetDefaultSettings(0, &stcSettings); |
|---|
| 214 | stcSettings.autoConfigTimebase = false; /* must do it manually */ |
|---|
| 215 | stcSettings.timebase = NEXUS_Timebase_e0; |
|---|
| 216 | stcSettings.mode = NEXUS_StcChannelMode_ePcr; /* live */ |
|---|
| 217 | stcSettings.modeSettings.pcr.pidChannel = stream->stream_info.pcrPidChannel; |
|---|
| 218 | stcSettings.modeSettings.pcr.offsetThreshold = 0; |
|---|
| 219 | stcSettings.stcIndex = 0; |
|---|
| 220 | stcSettings.modeSettings.Auto.behavior = NEXUS_StcChannelAutoModeBehavior_eFirstAvailable; |
|---|
| 221 | stcSettings.modeSettings.Auto.transportType = NEXUS_TransportType_eTs; |
|---|
| 222 | stream->stream_info.stcChannel = NEXUS_StcChannel_Open(stream->index, &stcSettings); |
|---|
| 223 | if (!stream->stream_info.stcChannel) |
|---|
| 224 | { |
|---|
| 225 | BDBG_WRN(("NEXUS_StcChannel_Open %d\n",stream->index)); |
|---|
| 226 | goto ExitError; |
|---|
| 227 | } |
|---|
| 228 | |
|---|
| 229 | NEXUS_VideoDecoder_GetDefaultStartSettings(&(stream->stream_info.decodeSettings)); |
|---|
| 230 | stream->stream_info.decodeSettings.codec = stream->mpeg.video[0].format; |
|---|
| 231 | stream->stream_info.decodeSettings.pidChannel = stream->stream_info.videoPidChannel; |
|---|
| 232 | stream->stream_info.decodeSettings.stcChannel = stream->stream_info.stcChannel; |
|---|
| 233 | stream->stream_info.decodeSettings.prerollRate = 1; |
|---|
| 234 | stream->stream_info.primerHandle = NEXUS_VideoDecoder_OpenPrimer(stream->stream_info.videoDecoder); |
|---|
| 235 | if (!stream->stream_info.primerHandle) |
|---|
| 236 | { |
|---|
| 237 | BDBG_WRN(("NEXUS_VideoDecoder_OpenPrimer failed %d\n",stream->index)); |
|---|
| 238 | goto ExitError; |
|---|
| 239 | } |
|---|
| 240 | |
|---|
| 241 | BDBG_ASSERT(stream->stream_info.decodeSettings.stcChannel); |
|---|
| 242 | BDBG_ERR(("NEXUS_VideoDecoder_StartPrimer(0x%08x,0x%08x)\n",stream->stream_info.primerHandle)); |
|---|
| 243 | NEXUS_VideoDecoder_StartPrimer(stream->stream_info.videoDecoder, |
|---|
| 244 | stream->stream_info.primerHandle, &(stream->stream_info.decodeSettings)); |
|---|
| 245 | |
|---|
| 246 | stream->in_use = true; |
|---|
| 247 | BDBG_WRN(("%s:%d\n",__FUNCTION__,__LINE__)); |
|---|
| 248 | |
|---|
| 249 | return stream; |
|---|
| 250 | |
|---|
| 251 | ExitError: |
|---|
| 252 | if (stream) |
|---|
| 253 | bstream_close(stream); |
|---|
| 254 | return NULL; |
|---|
| 255 | } |
|---|
| 256 | void bstream_close(bstream_t stream) |
|---|
| 257 | { |
|---|
| 258 | BDBG_ASSERT(stream); |
|---|
| 259 | BDBG_ASSERT(stream->stream_info.videoDecoder); |
|---|
| 260 | BDBG_ASSERT(stream->stream_info.primerHandle); |
|---|
| 261 | BDBG_WRN(("%s:%d\n",__FUNCTION__,__LINE__)); |
|---|
| 262 | BDBG_ERR(("NEXUS_VideoDecoder_StopPrimer(0x%08x)\n",stream->stream_info.primerHandle)); |
|---|
| 263 | NEXUS_VideoDecoder_StopPrimer(stream->stream_info.videoDecoder, stream->stream_info.primerHandle); |
|---|
| 264 | NEXUS_VideoDecoder_ClosePrimer(stream->stream_info.videoDecoder,stream->stream_info.primerHandle); |
|---|
| 265 | |
|---|
| 266 | if (stream->stream_info.stcChannel) |
|---|
| 267 | { |
|---|
| 268 | NEXUS_StcChannel_Close(stream->stream_info.stcChannel); |
|---|
| 269 | stream->stream_info.stcChannel = NULL; |
|---|
| 270 | } |
|---|
| 271 | |
|---|
| 272 | if ((stream->stream_info.pcrPidChannel != stream->stream_info.videoPidChannel) && |
|---|
| 273 | (stream->stream_info.pcrPidChannel != stream->stream_info.audioPidChannel) && |
|---|
| 274 | stream->stream_info.pcrPidChannel) |
|---|
| 275 | { |
|---|
| 276 | NEXUS_PidChannel_Close(stream->stream_info.pcrPidChannel); |
|---|
| 277 | stream->stream_info.pcrPidChannel = NULL; |
|---|
| 278 | } |
|---|
| 279 | |
|---|
| 280 | if (stream->stream_info.videoPidChannel) |
|---|
| 281 | { |
|---|
| 282 | NEXUS_PidChannel_Close(stream->stream_info.videoPidChannel); |
|---|
| 283 | stream->stream_info.videoPidChannel = NULL; |
|---|
| 284 | } |
|---|
| 285 | |
|---|
| 286 | if (stream->stream_info.audioPidChannel) |
|---|
| 287 | { |
|---|
| 288 | NEXUS_PidChannel_Close(stream->stream_info.audioPidChannel); |
|---|
| 289 | stream->stream_info.audioPidChannel = NULL; |
|---|
| 290 | } |
|---|
| 291 | stream->in_use = false; |
|---|
| 292 | BDBG_WRN(("%s:%d\n",__FUNCTION__,__LINE__)); |
|---|
| 293 | } |
|---|
| 294 | |
|---|
| 295 | static uint32_t ts_clock_diff_avg = 0; |
|---|
| 296 | |
|---|
| 297 | bresult bstream_get_status(bstream_t stream, bstream_status *status ) |
|---|
| 298 | { |
|---|
| 299 | BDBG_ASSERT(stream); |
|---|
| 300 | BDBG_ASSERT(status); |
|---|
| 301 | |
|---|
| 302 | BKNI_Memset(status,0,sizeof(bstream_status)); |
|---|
| 303 | status->mpeg = stream->mpeg; |
|---|
| 304 | status->band = stream->band; |
|---|
| 305 | |
|---|
| 306 | return b_ok; |
|---|
| 307 | } |
|---|
| 308 | |
|---|
| 309 | /** |
|---|
| 310 | Summary: |
|---|
| 311 | Return private stream structure. |
|---|
| 312 | */ |
|---|
| 313 | bsettop_p_stream_t *bstream_get_info(bstream_t stream) |
|---|
| 314 | { |
|---|
| 315 | return &(stream->stream_info); |
|---|
| 316 | } |
|---|
| 317 | |
|---|