VirtualBox

source: vbox/trunk/src/VBox/Devices/Audio/HDAStreamPeriod.cpp@ 67683

Last change on this file since 67683 was 67649, checked in by vboxsync, 7 years ago

Audio/HDAStreamPeriod.cpp: LogRel nit.

  • Property svn:executable set to *
File size: 14.1 KB
Line 
1/* $Id$ */
2/** @file
3 * HDAStreamPeriod.cpp - Stream period functions for HD Audio.
4 *
5 * Utility functions for handling HDA audio stream periods.
6 * Stream period handling is needed in order to keep track of a stream's timing
7 * and processed audio data.
8 *
9 * As the HDA device only has one bit clock (WALCLK) but audio streams can be processed
10 * at certain points in time, these functions can be used to estimate and schedule the
11 * wall clock (WALCLK) for all streams accordingly.
12 */
13
14/*
15 * Copyright (C) 2017 Oracle Corporation
16 *
17 * This file is part of VirtualBox Open Source Edition (OSE), as
18 * available from http://www.virtualbox.org. This file is free software;
19 * you can redistribute it and/or modify it under the terms of the GNU
20 * General Public License (GPL) as published by the Free Software
21 * Foundation, in version 2 as it comes in the "COPYING" file of the
22 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
23 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
24 */
25
26/*********************************************************************************************************************************
27* Header Files *
28*********************************************************************************************************************************/
29#define LOG_GROUP LOG_GROUP_DEV_HDA
30#include <VBox/log.h>
31
32#include <VBox/vmm/pdmdev.h>
33#include <VBox/vmm/pdmaudioifs.h>
34
35#include "HDAStreamPeriod.h"
36
37#ifdef IN_RING3
38/**
39 * Creates a stream period.
40 *
41 * @return IPRT status code.
42 * @param pPeriod Stream period to initialize.
43 */
44int hdaStreamPeriodCreate(PHDASTREAMPERIOD pPeriod)
45{
46 Assert(!(pPeriod->fStatus & HDASTREAMPERIOD_FLAG_VALID));
47
48 int rc = RTCritSectInit(&pPeriod->CritSect);
49 if (RT_SUCCESS(rc))
50 {
51 pPeriod->fStatus = HDASTREAMPERIOD_FLAG_VALID;
52 }
53
54 return VINF_SUCCESS;
55}
56
57/**
58 * Destroys a formerly created stream period.
59 *
60 * @param pPeriod Stream period to destroy.
61 */
62void hdaStreamPeriodDestroy(PHDASTREAMPERIOD pPeriod)
63{
64 if (pPeriod->fStatus & HDASTREAMPERIOD_FLAG_VALID)
65 {
66 RTCritSectDelete(&pPeriod->CritSect);
67
68 pPeriod->fStatus = HDASTREAMPERIOD_FLAG_NONE;
69 }
70}
71
72/**
73 * Initializes a given stream period with needed parameters.
74 *
75 * @param pPeriod Stream period to (re-)initialize. Must be created with hdaStreamPeriodCreate() first.
76 * @param u8SD Stream descriptor (serial data #) number to assign this stream period to.
77 * @param u16LVI The HDA stream's LVI value to use for the period calculation.
78 * @param u32CBL The HDA stream's CBL value to use for the period calculation.
79 * @param pStreamCfg Audio stream configuration to use for this period.
80 */
81void hdaStreamPeriodInit(PHDASTREAMPERIOD pPeriod,
82 uint8_t u8SD, uint16_t u16LVI, uint32_t u32CBL, PPDMAUDIOSTREAMCFG pStreamCfg)
83{
84 /*
85 * Linux guests (at least Ubuntu):
86 * 17632 bytes (CBL) / 4 (frame size) = 4408 frames / 4 (LVI) = 1102 frames per period
87 *
88 * Windows guests (Win10 AU):
89 * 3584 bytes (CBL) / 4 (frame size) = 896 frames / 2 (LVI) = 448 frames per period
90 */
91 unsigned cTotalPeriods = u16LVI;
92
93 if (cTotalPeriods <= 1)
94 cTotalPeriods = 2; /* At least two periods *must* be present (LVI >= 1). */
95
96 uint32_t framesToTransfer = (u32CBL / 4 /** @todo Define frame size? */) / cTotalPeriods;
97
98 pPeriod->u8SD = u8SD;
99 pPeriod->u64StartWalClk = 0;
100 pPeriod->u32Hz = pStreamCfg->Props.uHz;
101 pPeriod->u64DurationWalClk = hdaStreamPeriodFramesToWalClk(pPeriod, framesToTransfer);
102 pPeriod->u64ElapsedWalClk = 0;
103 pPeriod->i64DelayWalClk = 0;
104 pPeriod->framesToTransfer = framesToTransfer;
105 pPeriod->framesTransferred = 0;
106 pPeriod->cIntPending = 0;
107
108 Log3Func(("[SD%RU8] %RU64 long, Hz=%RU32, CBL=%RU32, LVI=%RU16 -> %u periods, %RU32 frames each\n",
109 pPeriod->u8SD, pPeriod->u64DurationWalClk, pPeriod->u32Hz, u32CBL, u16LVI,
110 cTotalPeriods, pPeriod->framesToTransfer));
111}
112
113/**
114 * Resets a stream period to its initial state.
115 *
116 * @param pPeriod Stream period to reset.
117 */
118void hdaStreamPeriodReset(PHDASTREAMPERIOD pPeriod)
119{
120 Log3Func(("[SD%RU8]\n", pPeriod->u8SD));
121
122 if (pPeriod->cIntPending)
123 LogRelMax(50, ("HDA: Warning: %RU8 interrupts for stream #%RU8 still pending -- so a period reset might trigger audio hangs\n",
124 pPeriod->cIntPending, pPeriod->u8SD));
125
126 pPeriod->fStatus &= ~HDASTREAMPERIOD_FLAG_ACTIVE;
127 pPeriod->u64StartWalClk = 0;
128 pPeriod->u64ElapsedWalClk = 0;
129 pPeriod->framesTransferred = 0;
130 pPeriod->cIntPending = 0;
131#ifdef DEBUG
132 pPeriod->Dbg.tsStartNs = 0;
133#endif
134}
135
136/**
137 * Begins a new period life span of a given period.
138 *
139 * @return IPRT status code.
140 * @param pPeriod Stream period to begin new life span for.
141 * @param u64WalClk Wall clock (WALCLK) value to set for the period's starting point.
142 */
143int hdaStreamPeriodBegin(PHDASTREAMPERIOD pPeriod, uint64_t u64WalClk)
144{
145 Assert(!(pPeriod->fStatus & HDASTREAMPERIOD_FLAG_ACTIVE)); /* No nested calls. */
146
147 pPeriod->fStatus |= HDASTREAMPERIOD_FLAG_ACTIVE;
148 pPeriod->u64StartWalClk = u64WalClk;
149 pPeriod->u64ElapsedWalClk = 0;
150 pPeriod->framesTransferred = 0;
151 pPeriod->cIntPending = 0;
152#ifdef DEBUG
153 pPeriod->Dbg.tsStartNs = RTTimeNanoTS();
154#endif
155
156 Log3Func(("[SD%RU8] Starting @ %RU64 (%RU64 long)\n",
157 pPeriod->u8SD, pPeriod->u64StartWalClk, pPeriod->u64DurationWalClk));
158
159 return VINF_SUCCESS;
160}
161
162/**
163 * Ends a formerly begun period life span.
164 *
165 * @param pPeriod Stream period to end life span for.
166 */
167void hdaStreamPeriodEnd(PHDASTREAMPERIOD pPeriod)
168{
169 Log3Func(("[SD%RU8] Took %zuus\n", pPeriod->u8SD, (RTTimeNanoTS() - pPeriod->Dbg.tsStartNs) / 1000));
170
171 if (!(pPeriod->fStatus & HDASTREAMPERIOD_FLAG_ACTIVE))
172 return;
173
174 /* Sanity. */
175 AssertMsg(pPeriod->cIntPending == 0,
176 ("%RU8 interrupts for stream #%RU8 still pending -- so ending a period might trigger audio hangs\n",
177 pPeriod->cIntPending, pPeriod->u8SD));
178 Assert(hdaStreamPeriodIsComplete(pPeriod));
179
180 pPeriod->fStatus &= ~HDASTREAMPERIOD_FLAG_ACTIVE;
181}
182
183/**
184 * Pauses a period. All values remain intact.
185 *
186 * @param pPeriod Stream period to pause.
187 */
188void hdaStreamPeriodPause(PHDASTREAMPERIOD pPeriod)
189{
190 AssertMsg((pPeriod->fStatus & HDASTREAMPERIOD_FLAG_ACTIVE), ("Period %p already in inactive state\n", pPeriod));
191
192 pPeriod->fStatus &= ~HDASTREAMPERIOD_FLAG_ACTIVE;
193
194 Log3Func(("[SD%RU8]\n", pPeriod->u8SD));
195}
196
197/**
198 * Resumes a formerly paused period.
199 *
200 * @param pPeriod Stream period to resume.
201 */
202void hdaStreamPeriodResume(PHDASTREAMPERIOD pPeriod)
203{
204 AssertMsg(!(pPeriod->fStatus & HDASTREAMPERIOD_FLAG_ACTIVE), ("Period %p already in active state\n", pPeriod));
205
206 pPeriod->fStatus |= HDASTREAMPERIOD_FLAG_ACTIVE;
207
208 Log3Func(("[SD%RU8]\n", pPeriod->u8SD));
209}
210
211/**
212 * Locks a stream period for serializing access.
213 *
214 * @return true if locking was successful, false if not.
215 * @param pPeriod Stream period to lock.
216 */
217bool hdaStreamPeriodLock(PHDASTREAMPERIOD pPeriod)
218{
219 return RT_SUCCESS(RTCritSectEnter(&pPeriod->CritSect));
220}
221
222/**
223 * Unlocks a formerly locked stream period.
224 *
225 * @param pPeriod Stream period to unlock.
226 */
227void hdaStreamPeriodUnlock(PHDASTREAMPERIOD pPeriod)
228{
229 int rc2 = RTCritSectLeave(&pPeriod->CritSect);
230 AssertRC(rc2);
231}
232
233/**
234 * Returns the wall clock (WALCLK) value for a given amount of stream period audio frames.
235 *
236 * @return Calculated wall clock value.
237 * @param pPeriod Stream period to calculate wall clock value for.
238 * @param uFrames Number of audio frames to calculate wall clock value for.
239 *
240 * @remark Calculation depends on the given stream period and assumes a 24 MHz wall clock counter (WALCLK).
241 */
242uint64_t hdaStreamPeriodFramesToWalClk(PHDASTREAMPERIOD pPeriod, uint32_t uFrames)
243{
244 /* Prevent division by zero. */
245 const uint32_t uHz = (pPeriod->u32Hz ? pPeriod->u32Hz : 1);
246
247 /* 24 MHz WallClock (WALCLK): 42ns resolution. */
248 return ((uFrames * 24000 /* 24 MHz */) / uHz) * 1000;
249}
250
251/**
252 * Returns the absolute wall clock (WALCLK) value for the already elapsed time of
253 * a given stream period.
254 *
255 * @return Absolute elapsed time as wall clock (WALCLK) value.
256 * @param pPeriod Stream period to use.
257 */
258uint64_t hdaStreamPeriodGetAbsElapsedWalClk(PHDASTREAMPERIOD pPeriod)
259{
260 return pPeriod->u64StartWalClk
261 + pPeriod->u64ElapsedWalClk
262 + pPeriod->i64DelayWalClk;
263}
264
265/**
266 * Returns the absolute wall clock (WALCLK) value for the calculated end time of
267 * a given stream period.
268 *
269 * @return Absolute end time as wall clock (WALCLK) value.
270 * @param pPeriod Stream period to use.
271 */
272uint64_t hdaStreamPeriodGetAbsEndWalClk(PHDASTREAMPERIOD pPeriod)
273{
274 return pPeriod->u64StartWalClk + pPeriod->u64DurationWalClk;
275}
276
277/**
278 * Returns the remaining audio frames to process for a given stream period.
279 *
280 * @return Number of remaining audio frames to process. 0 if all were processed.
281 * @param pPeriod Stream period to return value for.
282 */
283uint32_t hdaStreamPeriodGetRemainingFrames(PHDASTREAMPERIOD pPeriod)
284{
285 Assert(pPeriod->framesToTransfer >= pPeriod->framesTransferred);
286 return pPeriod->framesToTransfer - pPeriod->framesTransferred;
287}
288
289/**
290 * Tells whether a given stream period has elapsed (time-wise) or not.
291 *
292 * @return true if the stream period has elapsed, false if not.
293 * @param pPeriod Stream period to get status for.
294 */
295bool hdaStreamPeriodHasElapsed(PHDASTREAMPERIOD pPeriod)
296{
297 return (pPeriod->u64ElapsedWalClk >= pPeriod->u64DurationWalClk);
298}
299
300/**
301 * Tells whether a given stream period has passed the given absolute wall clock (WALCLK)
302 * time or not
303 *
304 * @return true if the stream period has passed the given time, false if not.
305 * @param pPeriod Stream period to get status for.
306 * @param u64WalClk Absolute wall clock (WALCLK) time to check for.
307 */
308bool hdaStreamPeriodHasPassedAbsWalClk(PHDASTREAMPERIOD pPeriod, uint64_t u64WalClk)
309{
310 /* Period not in use? */
311 if (!(pPeriod->fStatus & HDASTREAMPERIOD_FLAG_ACTIVE))
312 return true; /* ... implies that it has passed. */
313
314 if (hdaStreamPeriodHasElapsed(pPeriod))
315 return true; /* Period already has elapsed. */
316
317 return (pPeriod->u64StartWalClk + pPeriod->u64ElapsedWalClk) >= u64WalClk;
318}
319
320/**
321 * Tells whether a given stream period has some required interrupts pending or not.
322 *
323 * @return true if period has interrupts pending, false if not.
324 * @param pPeriod Stream period to get status for.
325 */
326bool hdaStreamPeriodNeedsInterrupt(PHDASTREAMPERIOD pPeriod)
327{
328 return pPeriod->cIntPending > 0;
329}
330
331/**
332 * Acquires (references) an (pending) interrupt for a given stream period.
333 *
334 * @param pPeriod Stream period to acquire interrupt for.
335 *
336 * @remark This routine does not do any actual interrupt processing; it only
337 * keeps track of the required (pending) interrupts for a stream period.
338 */
339void hdaStreamPeriodAcquireInterrupt(PHDASTREAMPERIOD pPeriod)
340{
341 uint32_t cIntPending = pPeriod->cIntPending;
342 if (cIntPending)
343 {
344 Log3Func(("[SD%RU8] Already pending\n", pPeriod->u8SD));
345 return;
346 }
347
348 pPeriod->cIntPending++;
349
350 Log3Func(("[SD%RU8] %RU32\n", pPeriod->u8SD, pPeriod->cIntPending));
351}
352
353/**
354 * Releases (dereferences) a pending interrupt.
355 *
356 * @param pPeriod Stream period to release pending interrupt for.
357 */
358void hdaStreamPeriodReleaseInterrupt(PHDASTREAMPERIOD pPeriod)
359{
360 Assert(pPeriod->cIntPending);
361 pPeriod->cIntPending--;
362
363 Log3Func(("[SD%RU8] %RU32\n", pPeriod->u8SD, pPeriod->cIntPending));
364}
365
366/**
367 * Adds an amount of (processed) audio frames to a given stream period.
368 *
369 * @return IPRT status code.
370 * @param pPeriod Stream period to add audio frames to.
371 * @param framesInc Audio frames to add.
372 */
373void hdaStreamPeriodInc(PHDASTREAMPERIOD pPeriod, uint32_t framesInc)
374{
375 pPeriod->framesTransferred += framesInc;
376 Assert(pPeriod->framesTransferred <= pPeriod->framesToTransfer);
377
378 pPeriod->u64ElapsedWalClk = hdaStreamPeriodFramesToWalClk(pPeriod, pPeriod->framesTransferred);
379 Assert(pPeriod->u64ElapsedWalClk <= pPeriod->u64DurationWalClk);
380
381 Log3Func(("[SD%RU8] cbTransferred=%RU32, u64ElapsedWalClk=%RU64\n",
382 pPeriod->u8SD, pPeriod->framesTransferred, pPeriod->u64ElapsedWalClk));
383}
384
385/**
386 * Tells whether a given stream period is considered as complete or not.
387 *
388 * @return true if stream period is complete, false if not.
389 * @param pPeriod Stream period to report status for.
390 *
391 * @remark A stream period is considered complete if it has 1) passed (elapsed) its calculated period time
392 * and 2) processed all required audio frames.
393 */
394bool hdaStreamPeriodIsComplete(PHDASTREAMPERIOD pPeriod)
395{
396 const bool fIsComplete = /* Has the period elapsed time-wise? */
397 hdaStreamPeriodHasElapsed(pPeriod)
398 /* All frames transferred? */
399 && pPeriod->framesTransferred >= pPeriod->framesToTransfer;
400#ifdef VBOX_STRICT
401 if (fIsComplete)
402 {
403 Assert(pPeriod->framesTransferred == pPeriod->framesToTransfer);
404 Assert(pPeriod->u64ElapsedWalClk == pPeriod->u64DurationWalClk);
405 }
406#endif
407
408 Log3Func(("[SD%RU8] Period %s - runtime %RU64 / %RU64 (abs @ %RU64, starts @ %RU64, ends @ %RU64), %RU8 IRQs pending\n",
409 pPeriod->u8SD,
410 fIsComplete ? "COMPLETE" : "NOT COMPLETE YET",
411 pPeriod->u64ElapsedWalClk, pPeriod->u64DurationWalClk,
412 hdaStreamPeriodGetAbsElapsedWalClk(pPeriod), pPeriod->u64StartWalClk,
413 hdaStreamPeriodGetAbsEndWalClk(pPeriod), pPeriod->cIntPending));
414
415 return fIsComplete;
416}
417#endif /* IN_RING3 */
418
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette