VirtualBox

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

Last change on this file since 71710 was 70125, checked in by vboxsync, 7 years ago

Audio/HDA: Use LVI + 1 foor the stream periods and stream transfer fragments (as LVI is zero-based, 2 entries minimum).

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