1 /* ------------------------------------------------------------------
2 * Copyright (C) 1998-2009 PacketVideo
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
13 * express or implied.
14 * See the License for the specific language governing permissions
15 * and limitations under the License.
16 * -------------------------------------------------------------------
17 */
18 /**
19 * @file pvmf_jb_session_duration_timer.cpp
20 * @brief Session Duration timer to Jitter Buffer
21 */
22 #ifndef PVMF_JB_SESSION_DURATION_TIMER_H_INCLUDED
23 #include "pvmf_jb_session_duration_timer.h"
24 #endif
25
26 #ifndef PVMF_JITTER_BUFFER_COMMON_INTERNAL_H
27 #include "pvmf_jitter_buffer_common_internal.h"
28 #endif
29
30 ////////////////////////////////////////////////////////////////////////////
PvmfJBSessionDurationTimer(PvmfJBSessionDurationTimerObserver * aObserver)31 PvmfJBSessionDurationTimer::PvmfJBSessionDurationTimer(PvmfJBSessionDurationTimerObserver* aObserver)
32 : OsclTimerObject(OsclActiveObject::EPriorityNominal, "PvmfJBSessionDurationTimer"),
33 iCurrentMonitoringIntervalInMS(0),
34 iSessionDurationInMS(0),
35 iElapsedSessionDurationInMS(0),
36 iObserver(aObserver),
37 iStarted(false),
38 iTimerStartTimeInMS(0),
39 iMonitoringIntervalElapsed(0),
40 iEstimatedServerClock(NULL),
41 iEstimatedServClockValAtLastCancel(0),
42 iExpectedEstimatedServClockValAtSessionEnd(0)
43 {
44 ipLogger = PVLogger::GetLoggerObject("PvmfJBSessionDurationTimer");
45 ipClockLoggerSessionDuration = PVLogger::GetLoggerObject("clock.streaming_manager.sessionduration");
46 AddToScheduler();
47 iClock.SetClockTimebase(iClockTimeBase);
48 }
49
50
51 ////////////////////////////////////////////////////////////////////////////
~PvmfJBSessionDurationTimer()52 PvmfJBSessionDurationTimer::~PvmfJBSessionDurationTimer()
53 {
54 Stop();
55 iEstimatedServerClock = NULL;
56 }
57
58 ////////////////////////////////////////////////////////////////////////////
Start()59 PVMFStatus PvmfJBSessionDurationTimer::Start()
60 {
61 PVMF_JB_LOGINFO((0, "PvmfJBSessionDurationTimer::Start"));
62 PVMF_JB_LOGCLOCK_SESSION_DURATION((0, "PvmfJBSessionDurationTimer::Start"));
63 if ((iSessionDurationInMS > 0) && (iCurrentMonitoringIntervalInMS > 0))
64 {
65 PVMF_JB_LOGCLOCK_SESSION_DURATION((0, "PvmfJBSessionDurationTimer::Start - SessionDurationInMS = %d", iSessionDurationInMS));
66 iClock.Start();
67 uint32 timebase32 = 0;
68 iTimerStartTimeInMS = 0;
69 iMonitoringIntervalElapsed = 0;
70 bool overflowFlag = false;
71 iClock.GetCurrentTime32(iTimerStartTimeInMS, overflowFlag, PVMF_MEDIA_CLOCK_MSEC, timebase32);
72 /* Compute expected estimated serv clock value when duration expires */
73 if (iEstimatedServerClock != NULL)
74 {
75 iExpectedEstimatedServClockValAtSessionEnd = iEstimatedServClockValAtLastCancel;
76 uint32 currEstServClk32;
77 currEstServClk32 = iExpectedEstimatedServClockValAtSessionEnd;
78 PVMF_JB_LOGCLOCK_SESSION_DURATION((0, "PvmfJBSessionDurationTimer::Start - CurrEstServClock = %d", currEstServClk32));
79 uint32 remainingSessionDuration32 = 0;
80 remainingSessionDuration32 = iSessionDurationInMS - iElapsedSessionDurationInMS;
81 iExpectedEstimatedServClockValAtSessionEnd += remainingSessionDuration32;
82
83 PVMF_JB_LOGCLOCK_SESSION_DURATION((0, "PvmfJBSessionDurationTimer::Start - ExpectedEstimatedServClockValAtSessionEnd = %d", iExpectedEstimatedServClockValAtSessionEnd));
84 }
85 RunIfNotReady(iCurrentMonitoringIntervalInMS*1000);
86 iStarted = true;
87 return PVMFSuccess;
88 }
89 return PVMFFailure;
90 }
91
92 ////////////////////////////////////////////////////////////////////////////
setSessionDurationInMS(uint32 duration)93 PVMFStatus PvmfJBSessionDurationTimer::setSessionDurationInMS(uint32 duration)
94 {
95 PVMF_JB_LOGINFO((0, "PvmfJBSessionDurationTimer::setMaxInactivityDurationInMS"));
96 PVMF_JB_LOGCLOCK_SESSION_DURATION((0, "PvmfJBSessionDurationTimer::setMaxInactivityDurationInMS"));
97 iSessionDurationInMS = duration;
98 iElapsedSessionDurationInMS = 0;
99 return PVMFSuccess;
100 }
101
102 ////////////////////////////////////////////////////////////////////////////
Stop()103 PVMFStatus PvmfJBSessionDurationTimer::Stop()
104 {
105 PVMF_JB_LOGINFO((0, "PvmfJBSessionDurationTimer::Stop"));
106 PVMF_JB_LOGCLOCK_SESSION_DURATION((0, "PvmfJBSessionDurationTimer::Stop"));
107 OsclTimerObject::Cancel();
108 iStarted = false;
109 iSessionDurationInMS = 0;
110 iClock.Stop();
111 iTimerStartTimeInMS = 0;
112 iMonitoringIntervalElapsed = 0;
113 iExpectedEstimatedServClockValAtSessionEnd = 0;
114 iEstimatedServClockValAtLastCancel = 0;
115 iElapsedSessionDurationInMS = 0;
116 return PVMFSuccess;
117 }
118
119 ////////////////////////////////////////////////////////////////////////////
Cancel()120 PVMFStatus PvmfJBSessionDurationTimer::Cancel()
121 {
122 PVMF_JB_LOGINFO((0, "PvmfJBSessionDurationTimer::Cancel"));
123 PVMF_JB_LOGCLOCK_SESSION_DURATION((0, "PvmfJBSessionDurationTimer::Cancel"));
124 iStarted = false;
125 uint32 timebase32 = 0;
126 uint32 cancelTime = 0;
127 bool overflowFlag = false;
128 iClock.GetCurrentTime32(cancelTime, overflowFlag, PVMF_MEDIA_CLOCK_MSEC, timebase32);
129 iMonitoringIntervalElapsed = (cancelTime - iTimerStartTimeInMS);
130 iEstimatedServClockValAtLastCancel = 0;
131 if (iEstimatedServerClock != NULL)
132 {
133 uint32 timebase32 = 0;
134 iEstimatedServerClock->GetCurrentTime32(iEstimatedServClockValAtLastCancel, overflowFlag,
135 PVMF_MEDIA_CLOCK_MSEC, timebase32);
136 }
137
138 PVMF_JB_LOGCLOCK_SESSION_DURATION((0, "PvmfJBSessionDurationTimer::Cancel - EstimatedServClockValAtLastCancel = %d", iEstimatedServClockValAtLastCancel));
139 iClock.Stop();
140 iTimerStartTimeInMS = 0;
141 OsclTimerObject::Cancel();
142 return PVMFSuccess;
143 }
144
145
EstimatedServerClockUpdated()146 void PvmfJBSessionDurationTimer::EstimatedServerClockUpdated()
147 {
148 if (iEstimatedServerClock != NULL)
149 {
150 uint32 timebase32 = 0;
151 uint32 estServClock = 0;
152 bool overflowFlag = false;
153
154 iEstimatedServerClock->GetCurrentTime32(estServClock, overflowFlag,
155 PVMF_MEDIA_CLOCK_MSEC, timebase32);
156 PVMF_JB_LOGCLOCK_SESSION_DURATION((0, "PvmfJBSessionDurationTimer::EstimatedServerClockUpdated - CurrEstServClock = %2d", estServClock));
157 PVMF_JB_LOGCLOCK_SESSION_DURATION((0, "PvmfJBSessionDurationTimer::EstimatedServerClockUpdated - ExpectedEstServClock = %2d", iExpectedEstimatedServClockValAtSessionEnd));
158 if (estServClock >= iExpectedEstimatedServClockValAtSessionEnd)
159 {
160 this->Cancel();
161 iObserver->PVMFJBSessionDurationTimerEvent();
162 }
163 }
164 }
165
166 ////////////////////////////////////////////////////////////////////////////
Run()167 void PvmfJBSessionDurationTimer::Run()
168 {
169 PVMF_JB_LOGINFO((0, "PvmfJBSessionDurationTimer::Run"));
170 PVMF_JB_LOGCLOCK_SESSION_DURATION((0, "PvmfJBSessionDurationTimer::Run"));
171
172 if (!iStarted)
173 return;
174
175 if (!iObserver)
176 {
177 PVMF_JB_LOGERROR((0, "PvmfJBSessionDurationTimer::Run: Error - Observer not set"));
178 return;
179 }
180
181 uint32 timebase32 = 0;
182 uint32 cancelTime = 0;
183 bool overflowFlag = false;
184
185 iClock.GetCurrentTime32(cancelTime, overflowFlag, PVMF_MEDIA_CLOCK_MSEC, timebase32);
186 iMonitoringIntervalElapsed = (cancelTime - iTimerStartTimeInMS);
187 iClock.Stop();
188 iTimerStartTimeInMS = 0;
189 iObserver->PVMFJBSessionDurationTimerEvent();
190 /*
191 * Do not reschudule the AO here. Observer would reschedule this AO
192 * once it is done processing the timer event.
193 */
194 }
195
196