source: svn/trunk/newcon3bcm2_21bu/magnum/syslib/astmlib/7552/bastmlib_presentation_task.c

Last change on this file was 2, checked in by phkim, 11 years ago

1.phkim

  1. revision copy newcon3sk r27
  • Property svn:executable set to *
File size: 13.9 KB
Line 
1/***************************************************************************
2*     Copyright (c) 2004-2011, 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: bastmlib_presentation_task.c $
11* $brcm_Revision: Hydra_Software_Devel/2 $
12* $brcm_Date: 3/1/11 10:11p $
13*
14* Revision History:
15*
16* $brcm_Log: /magnum/syslib/astmlib/noarch/bastmlib_presentation_task.c $
17*
18* Hydra_Software_Devel/2   3/1/11 10:11p bandrews
19* SW7550-691: only handle timer expiry if enabled and started
20*
21* Hydra_Software_Devel/1   7/17/09 6:56p bandrews
22* PR49215: playback support
23***************************************************************************/
24
25#include "bstd.h"
26#include "bkni.h"
27#include "bsyslib.h"
28#include "bastmlib.h"
29#include "bastmlib_priv.h"
30#include "bastmlib_presentation_task.h"
31#include "bastmlib_presenter_priv.h" /* TODO: remove dependency on presenter internals */
32
33BDBG_MODULE(astmlib);
34
35#define BASTMLIB_DEBUG_QUEUE 0
36#define BASTMLIB_DEBUG_SIGNALS 0
37
38void BASTMlib_P_Presentation_GetDefaultConfig(
39        BASTMlib_Config * psConfig
40)
41{
42        BDBG_ENTER(BASTMlib_P_Presentation_GetDefaultConfig);
43
44        BDBG_ASSERT(psConfig);
45
46        psConfig->sPresentation.ePreferredStcSource = BASTMlib_StcSource_eClockReference;
47        psConfig->sPresentation.hPreferredPresenter = NULL;
48        psConfig->sPresentation.uiInitialAcquisitionTime = BASTMLIB_PRESENTATION_DEFAULT_INITIAL_ACQUISITION_TIME;
49        psConfig->sPresentation.uiProcessingFrequency = BASTMLIB_PRESENTATION_DEFAULT_PROCESSING_FREQUENCY;
50        psConfig->sPresentation.uiSettlingTime = BASTMLIB_PRESENTATION_DEFAULT_SETTLING_TIME;
51        psConfig->sPresentation.uiTsmDisabledWatchdogTimeout = BASTMLIB_PRESENTATION_DEFAULT_TSM_DISABLED_WATCHDOG_TIMEOUT;
52        psConfig->sPresentation.ePreferredPresentationRateControl = BASTMlib_PresentationRateControl_eTimeStamp;
53
54        BDBG_LEAVE(BASTMlib_P_Presentation_GetDefaultConfig);
55}
56
57void BASTMlib_P_Presentation_GetMaxAcquisitionTime(
58        BASTMlib_Handle hAstm,
59        unsigned int * puiMaxAcquisitionTime
60)
61{
62        BDBG_ENTER(BASTMlib_P_Presentation_GetMaxAcquisitionTime);
63
64        BDBG_ASSERT(hAstm);
65        BDBG_ASSERT(puiMaxAcquisitionTime);
66
67        *puiMaxAcquisitionTime = hAstm->sConfig.sPresentation.uiInitialAcquisitionTime
68                > hAstm->sConfig.sPresentation.uiProcessingFrequency
69                ? hAstm->sConfig.sPresentation.uiInitialAcquisitionTime
70                : hAstm->sConfig.sPresentation.uiProcessingFrequency;
71
72        BDBG_LEAVE(BASTMlib_P_Presentation_GetMaxAcquisitionTime);
73}
74
75BERR_Code BASTMlib_P_Presentation_WatchdogTimerExpired(
76        void * pvParm1, /* first user context parameter [in] */ 
77        int iParm2, /* second user context parameter [in] */ 
78        BSYSlib_Timer_Handle hTimer /* the handle of the timer that expired [in] */ 
79)
80{
81        BERR_Code rc = BERR_SUCCESS;
82        BASTMlib_Handle hAstm = pvParm1;
83
84        BDBG_ENTER(BASTMlib_P_Presentation_WatchdogTimerExpired);
85
86        BDBG_ASSERT(hAstm);
87
88        BSTD_UNUSED(iParm2);
89        BSTD_UNUSED(hTimer);
90
91        if (hAstm->sConfig.bEnabled && hAstm->bStarted)
92        {
93                /* watchdog only happens when we have been in output master mode for a long time.
94                we will try to go back to the preferred presentation master mode */
95                BDBG_WRN(("Presentation TSM-disabled watchdog timer expired"));
96                BASTMlib_P_Presentation_RateControlStateMachine_SendSignal(hAstm, 
97                        BASTMlib_Presentation_StateMachineSignal_ePresenterPass);
98        }
99        else
100        {
101                BDBG_MSG(("Presentation TSM-disabled watchdog timer expired, but ASTM was not started or enabled"));
102        }
103
104        BDBG_LEAVE(BASTMlib_P_Presentation_WatchdogTimerExpired);
105        return rc;
106}
107
108BERR_Code BASTMlib_P_Presentation_TimerExpired(
109        void * pvParm1, /* first user context parameter [in] */ 
110        int iParm2, /* second user context parameter [in] */ 
111        BSYSlib_Timer_Handle hTimer /* the handle of the timer that expired [in] */ 
112)
113{
114        BERR_Code rc = BERR_SUCCESS;
115        BASTMlib_Handle hAstm = pvParm1;
116
117        BDBG_ENTER(BASTMlib_P_Presentation_TimerExpired);
118
119        BDBG_ASSERT(hAstm);
120
121        BSTD_UNUSED(iParm2);
122        BSTD_UNUSED(hTimer);
123
124        if (hAstm->sConfig.bEnabled && hAstm->bStarted)
125        {
126                if (hAstm->sPresentation.bAcquire)
127                {
128                        BDBG_MSG(("Presentation processing timer expired"));
129                        /* if we are acquiring, then this is a processing timer expiry event */
130                        rc = BASTMlib_P_Presentation_Process(hAstm);
131                        if (rc) goto error;
132                }
133                else
134                {
135                        BDBG_MSG(("Presentation settling timer expired"));
136                        /* if we are NOT acquiring, then this is a settling timer expiry event
137                        and we need to re-enable acquisition of data */
138                        BKNI_EnterCriticalSection();
139                        hAstm->sPresentation.bAcquire = true;
140                        BKNI_LeaveCriticalSection();
141                }
142
143                /* reschedule for later */
144                BDBG_MSG(("Scheduling presentation processing timer"));
145                BKNI_EnterCriticalSection();
146                rc = BASTMlib_P_StartTimer_isr(hAstm, hAstm->sPresentation.hTimer, 
147                        hAstm->sConfig.sPresentation.uiProcessingFrequency, 
148                        &BASTMlib_P_Presentation_TimerExpired, hAstm, 0);
149                BKNI_LeaveCriticalSection();
150                if (rc) goto error;
151        }
152        else
153        {
154                BDBG_MSG(("Presentation timer expired, but ASTM was not started or enabled"));
155        }
156
157        goto end;
158
159error:
160
161end:
162        BDBG_LEAVE(BASTMlib_P_Presentation_TimerExpired);
163        return rc;
164}
165
166BERR_Code BASTMlib_P_Presentation_Process(
167        BASTMlib_Handle hAstm
168)
169{
170        BERR_Code rc = BERR_SUCCESS;
171        uint32_t uiPassEventCount;
172        uint32_t uiFailEventCount;
173        uint32_t uiPassEventThreshold;
174        uint32_t uiFailEventThreshold;
175        uint32_t uiFailingPresenterCount;
176        uint32_t uiPassingPresenterCount;
177        uint32_t uiSize;
178        BSYSlib_List_IteratorHandle hIterator;
179        BASTMlib_Presenter_Handle hPresenter;
180
181        BDBG_ENTER(BASTMlib_P_Presentation_Process);
182
183        BDBG_ASSERT(hAstm);
184
185        uiFailingPresenterCount = 0;
186        uiPassingPresenterCount = 0;
187
188        hIterator = BSYSlib_List_AcquireIterator(hAstm->sPresentation.hPresenters);
189       
190        while (BSYSlib_List_HasNext(hIterator))
191        {
192                hPresenter = (BASTMlib_Presenter_Handle)BSYSlib_List_Next(hIterator);
193
194                uiPassEventCount = 0;
195                uiPassEventThreshold = hPresenter->sConfig.uiPassEventCountThreshold;
196
197                uiFailEventCount = 0;
198                uiFailEventThreshold = hPresenter->sConfig.uiFailEventCountThreshold;
199
200                BKNI_EnterCriticalSection();
201                uiSize = hPresenter->sEventQueue.uiSize;
202                BKNI_LeaveCriticalSection();
203               
204                while (uiSize)
205                {
206                        BASTMlib_Presenter_Event sEvent;
207
208#if BASTMLIB_DEBUG_QUEUE
209                        BDBG_MSG(("Removing event from presenter %p queue at %u", hPresenter, hPresenter->sEventQueue.uiRead + 1));
210#endif
211                        BKNI_EnterCriticalSection();
212                        hPresenter->sEventQueue.uiRead++;
213                        hPresenter->sEventQueue.uiRead %= hPresenter->sEventQueue.uiCapacity;
214                        sEvent = hPresenter->sEventQueue.asEvents[hPresenter->sEventQueue.uiRead];
215                        hPresenter->sEventQueue.uiSize--;
216                        BKNI_LeaveCriticalSection();
217
218                        BDBG_MSG(("Presenter %s TSM %s PTS %#x STC %#x delta %+ld", hPresenter->pcName, sEvent.bPass ? "pass" : "failure", sEvent.uiPts, sEvent.uiStc, (long)sEvent.uiPts - (long)sEvent.uiStc));
219                       
220                        if (sEvent.bPass)
221                        {
222                                uiPassEventCount++;
223                        }
224                        else
225                        {
226                                /* don't count the current STC master presenter as failing, since it will seed the STC */
227                                if (!(hAstm->sStatus.eStcSource == BASTMlib_StcSource_ePresenter && hAstm->sStatus.hStcMaster == hPresenter))
228                                {
229                                        uiFailEventCount++;
230                                }
231                        }
232                       
233                        BKNI_EnterCriticalSection();
234                        uiSize = hPresenter->sEventQueue.uiSize;
235                        BKNI_LeaveCriticalSection();
236                }
237
238                if (uiPassEventCount > uiPassEventThreshold)
239                {
240                        uiPassingPresenterCount++;
241                }
242
243                if (uiFailEventCount > uiFailEventThreshold)
244                {
245                        uiFailingPresenterCount++;
246                }
247        }
248
249        BSYSlib_List_ReleaseIterator(hIterator);
250
251        BDBG_MSG(("Passing presenters: %u", uiPassingPresenterCount));
252        BDBG_MSG(("Failing presenters: %u", uiFailingPresenterCount));
253
254        if (uiFailingPresenterCount > 0)
255        {
256                switch (hAstm->sStatus.eStcSource)
257                {
258                        case BASTMlib_StcSource_eClockReference:
259                                /* don't signal presentation master presenter failure if not all presenters fail */
260                                if (uiFailingPresenterCount != hAstm->sPresentation.uiPresenterCount)
261                                {
262                                        BASTMlib_P_Presentation_RateControlStateMachine_SendSignal(hAstm, 
263                                                BASTMlib_Presentation_StateMachineSignal_ePresenterFailure);
264                                }
265                                else /* instead signal stc master presenter failure */
266                                {
267                                        BASTMlib_P_Presentation_StcSourceStateMachine_SendSignal(hAstm, 
268                                                BASTMlib_Presentation_StateMachineSignal_ePresenterFailure);
269                                }
270                                break;
271                        case BASTMlib_StcSource_ePresenter:
272                                BASTMlib_P_Presentation_RateControlStateMachine_SendSignal(hAstm, 
273                                        BASTMlib_Presentation_StateMachineSignal_ePresenterFailure);
274                                break;
275                        default:
276                                break;
277                }
278        }
279        else if (uiPassingPresenterCount > 0)
280        {
281                BASTMlib_P_Presentation_RateControlStateMachine_SendSignal(hAstm,
282                        BASTMlib_Presentation_StateMachineSignal_ePresenterPass);
283        }
284
285        BDBG_LEAVE(BASTMlib_P_Presentation_Process);
286        return rc;
287}
288
289void BASTMlib_P_Presentation_RateControlStateMachine_SendSignal(
290        BASTMlib_Handle hAstm,
291        BASTMlib_Presentation_StateMachineSignal eSignal
292)
293{
294        BASTMlib_PresentationRateControl eNewState;
295
296        BDBG_ENTER(BASTMlib_P_Presentation_RateControlStateMachine_SendSignal);
297       
298        BDBG_ASSERT(hAstm);
299
300#if BASTMLIB_DEBUG_SIGNALS
301        BDBG_MSG(("Presentation rate control signal received: %u", eSignal));
302#endif
303
304        /*
305        Presentation rate control
306
307        signal (s)
308        0 reset
309        1 presenter pass
310        2 presenter fail
311        3 clock reference pass
312        4 clock reference fail
313
314        preference (p), before (t-1), now (t)
315        0 time stamp
316        1 output clock
317
318        p       s       t-1     t
319        0       0       x       0
320        0       1       x       0
321        0       2       x       1
322        0       3       0       0
323        0       3       1       1
324        0       4       0       0
325        0       4       1       1
326        1       x       x       1
327        */
328        switch (eSignal)
329        {
330                case BASTMlib_Presentation_StateMachineSignal_eReset:
331                case BASTMlib_Presentation_StateMachineSignal_ePresenterPass:
332                        eNewState = hAstm->sConfig.sPresentation.ePreferredPresentationRateControl; /* never go above preference */
333                        break;
334                case BASTMlib_Presentation_StateMachineSignal_ePresenterFailure:
335                        eNewState = BASTMlib_PresentationRateControl_eOutputClock;
336                        break;
337                default:
338                        eNewState = hAstm->sStatus.ePresentationRateControl; /* copy old state */
339                        break;
340        }
341
342        if (eNewState != hAstm->sStatus.ePresentationRateControl)
343        {
344                hAstm->sStatus.ePresentationRateControl = eNewState;
345                BASTMlib_P_Presentation_StateChangeHandler(hAstm, true);
346        }
347       
348        BDBG_LEAVE(BASTMlib_P_Presentation_RateControlStateMachine_SendSignal);
349}
350
351void BASTMlib_P_Presentation_StcSourceStateMachine_SendSignal(
352        BASTMlib_Handle hAstm,
353        BASTMlib_Presentation_StateMachineSignal eSignal
354)
355{
356        BASTMlib_StcSource eNewState;
357
358        BDBG_ENTER(BASTMlib_P_Presentation_StcSourceStateMachine_SendSignal);
359       
360        BDBG_ASSERT(hAstm);
361
362#if BASTMLIB_DEBUG_SIGNALS
363        BDBG_MSG(("STC source signal received: %u", eSignal));
364#endif
365
366        /*
367        STC source
368
369        signal (s)
370        0 reset
371        1 presenter pass
372        2 presenter fail
373        3 clock reference pass
374        4 clock reference fail
375
376        preference (p), before (t-1), now (t)
377        0 clock reference
378        1 presenter
379
380        p       s       t-1     t
381        0       0       x       0
382        0       1       0       0
383        0       1       1       1
384        0       2       x       1
385        0       3       x       0
386        0       4       x       1
387        1       x       x       1
388        */
389        switch (eSignal)
390        {
391                case BASTMlib_Presentation_StateMachineSignal_eReset:
392                case BASTMlib_Presentation_StateMachineSignal_eClockReferencePass:
393                        eNewState = hAstm->sConfig.sPresentation.ePreferredStcSource; /* never go above preference */
394                        break;
395                case BASTMlib_Presentation_StateMachineSignal_ePresenterFailure: /* means all presenters are failing */
396                case BASTMlib_Presentation_StateMachineSignal_eClockReferenceFailure:
397                        eNewState = BASTMlib_StcSource_ePresenter;
398                        break;
399                default:
400                        eNewState = hAstm->sStatus.eStcSource; /* copy old state */
401                        break;
402        }
403
404        if (eNewState != hAstm->sStatus.eStcSource)
405        {
406                hAstm->sStatus.eStcSource = eNewState;
407                if (eNewState == BASTMlib_StcSource_ePresenter)
408                {
409                        hAstm->sStatus.hStcMaster = hAstm->sConfig.sPresentation.hPreferredPresenter;
410                }
411                else
412                {
413                        hAstm->sStatus.hStcMaster = NULL;
414                }
415
416                BASTMlib_P_Presentation_StateChangeHandler(hAstm, false);
417        }
418       
419        BDBG_LEAVE(BASTMlib_P_Presentation_StcSourceStateMachine_SendSignal);
420}
421
422BERR_Code BASTMlib_P_Presentation_StateChangeHandler(
423        BASTMlib_Handle hAstm,
424        bool bPresentationMasterChanged
425)
426{
427        BERR_Code rc = BERR_SUCCESS;
428
429        BDBG_ENTER(BASTMlib_P_Presentation_StateChangeHandler);
430
431        BDBG_ASSERT(hAstm);
432
433        /* state changed, so stop acquiring for a bit */
434        BKNI_EnterCriticalSection();
435        hAstm->sPresentation.bAcquire = false;
436        BKNI_LeaveCriticalSection();
437
438        if (bPresentationMasterChanged)
439        {
440                /* Presentation mastership changed, do watchdog processing */
441                switch (hAstm->sStatus.ePresentationRateControl)
442                {
443                        case BASTMlib_PresentationRateControl_eTimeStamp:
444                                /* cancel any pending watchdog timer */
445                                BKNI_EnterCriticalSection();
446                                if (hAstm->sSettings.cbTimer.pfCancel_isr)
447                                {
448                                        rc = hAstm->sSettings.cbTimer.pfCancel_isr(hAstm->sSettings.cbTimer.pvParm1,
449                                                hAstm->sSettings.cbTimer.iParm2, hAstm->sPresentation.hWatchdogTimer);
450                                }
451                                BKNI_LeaveCriticalSection();
452                                if (rc) goto error;
453                                break;
454                        case BASTMlib_PresentationRateControl_eOutputClock:
455                                /* zero-length watchdog => disabled, do not schedule */
456                                if (hAstm->sConfig.sPresentation.uiTsmDisabledWatchdogTimeout)
457                                {
458                                        /* schedule watchdog timer */
459                                        BDBG_MSG(("Scheduling TSM-disabled watchdog timer"));
460                                        BKNI_EnterCriticalSection();
461                                        rc = BASTMlib_P_StartTimer_isr(hAstm, hAstm->sPresentation.hWatchdogTimer, 
462                                                hAstm->sConfig.sPresentation.uiTsmDisabledWatchdogTimeout, 
463                                                &BASTMlib_P_Presentation_WatchdogTimerExpired, hAstm, 0);
464                                        BKNI_LeaveCriticalSection();
465                                        if (rc) goto error;
466                                }
467                                break;
468                        default:
469                                break;
470                }
471        }
472
473        /* tell external sw that the state changed */
474        rc = BSYSlib_Callback_Invoke(&hAstm->sSettings.sPresentation.cbStateChange);
475        if (rc) goto error;
476
477        /* cancel processing timer and schedule settling timer -- if started */
478        if (hAstm->bStarted)
479        {
480                BDBG_MSG(("Scheduling presentation settling timer"));
481                BKNI_EnterCriticalSection();
482                rc = BASTMlib_P_StartTimer_isr(hAstm, hAstm->sPresentation.hTimer, 
483                        hAstm->sConfig.sPresentation.uiSettlingTime, 
484                        &BASTMlib_P_Presentation_TimerExpired, hAstm, 0);
485                BKNI_LeaveCriticalSection();
486                if (rc) goto error;
487        }
488
489        goto end;
490
491error:
492
493end:
494        BDBG_LEAVE(BASTMlib_P_Presentation_StateChangeHandler);
495        return rc;
496}
497
Note: See TracBrowser for help on using the repository browser.