VirtualBox

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

Last change on this file since 88037 was 88028, checked in by vboxsync, 4 years ago

Audio: Moving some of the DrvAudio.h stuff into PDM - VBox/vmm/pdmaudioinline.h. bugref:9890

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 13.9 KB
Line 
1/* $Id: HDAStreamPeriod.cpp 88028 2021-03-08 19:31:22Z vboxsync $ */
2/** @file
3 * HDAStreamPeriod.cpp - Stream period functions for HD Audio.
4 *
5 * Utility functions for handling HDA audio stream periods. Stream period
6 * 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
10 * processed at certain points in time, these functions can be used to estimate
11 * and schedule the wall clock (WALCLK) for all streams accordingly.
12 */
13
14/*
15 * Copyright (C) 2017-2020 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#include <VBox/vmm/pdmaudioinline.h>
38
39#include "DrvAudio.h"
40#include "HDAStreamPeriod.h"
41
42
43#ifdef IN_RING3 /* entire file currently */
44
45/**
46 * Creates a stream period.
47 *
48 * @return IPRT status code.
49 * @param pPeriod Stream period to initialize.
50 */
51int hdaR3StreamPeriodCreate(PHDASTREAMPERIOD pPeriod)
52{
53 Assert(!(pPeriod->fStatus & HDASTREAMPERIOD_F_VALID));
54
55 pPeriod->fStatus = HDASTREAMPERIOD_F_VALID;
56
57 return VINF_SUCCESS;
58}
59
60/**
61 * Destroys a formerly created stream period.
62 *
63 * @param pPeriod Stream period to destroy.
64 */
65void hdaR3StreamPeriodDestroy(PHDASTREAMPERIOD pPeriod)
66{
67 if (pPeriod->fStatus & HDASTREAMPERIOD_F_VALID)
68 pPeriod->fStatus = HDASTREAMPERIOD_F_NONE;
69}
70
71/**
72 * Initializes a given stream period with needed parameters.
73 *
74 * @return VBox status code.
75 * @param pPeriod Stream period to (re-)initialize. Must be created with hdaR3StreamPeriodCreate() 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 */
81int hdaR3StreamPeriodInit(PHDASTREAMPERIOD pPeriod,
82 uint8_t u8SD, uint16_t u16LVI, uint32_t u32CBL, PPDMAUDIOSTREAMCFG pStreamCfg)
83{
84 if ( !u16LVI
85 || !u32CBL
86 || !DrvAudioHlpPcmPropsAreValid(&pStreamCfg->Props))
87 {
88 return VERR_INVALID_PARAMETER;
89 }
90
91 /*
92 * Linux guests (at least Ubuntu):
93 * 17632 bytes (CBL) / 4 (frame size) = 4408 frames / 4 (LVI) = 1102 frames per period
94 *
95 * Windows guests (Win10 AU):
96 * 3584 bytes (CBL) / 4 (frame size) = 896 frames / 2 (LVI) = 448 frames per period
97 */
98 unsigned cTotalPeriods = u16LVI + 1;
99
100 if (cTotalPeriods <= 1)
101 cTotalPeriods = 2; /* At least two periods *must* be present (LVI >= 1). */
102
103 uint32_t cFramesToTransfer =
104 (u32CBL / (pStreamCfg->Props.cbSample * pStreamCfg->Props.cChannels /* Frame size */)) / cTotalPeriods;
105
106 pPeriod->u8SD = u8SD;
107 pPeriod->u64StartWalClk = 0;
108 pPeriod->u32Hz = pStreamCfg->Props.uHz;
109 pPeriod->u64DurationWalClk = hdaR3StreamPeriodFramesToWalClk(pPeriod, cFramesToTransfer);
110 pPeriod->u64ElapsedWalClk = 0;
111 pPeriod->i64DelayWalClk = 0;
112 pPeriod->cFramesToTransfer = cFramesToTransfer;
113 pPeriod->cFramesTransferred = 0;
114 pPeriod->cIntPending = 0;
115
116 Log3Func(("[SD%RU8] %RU64 long, Hz=%RU32, CBL=%RU32, LVI=%RU16 -> %u periods, %RU32 frames each\n",
117 pPeriod->u8SD, pPeriod->u64DurationWalClk, pPeriod->u32Hz, u32CBL, u16LVI,
118 cTotalPeriods, pPeriod->cFramesToTransfer));
119
120 return VINF_SUCCESS;
121}
122
123/**
124 * Resets a stream period to its initial state.
125 *
126 * @param pPeriod Stream period to reset.
127 */
128void hdaR3StreamPeriodReset(PHDASTREAMPERIOD pPeriod)
129{
130 Log3Func(("[SD%RU8]\n", pPeriod->u8SD));
131
132 if (pPeriod->cIntPending)
133 LogRelMax(50, ("HDA: Warning: %RU8 interrupts for stream #%RU8 still pending -- so a period reset might trigger audio hangs\n",
134 pPeriod->cIntPending, pPeriod->u8SD));
135
136 pPeriod->fStatus &= ~HDASTREAMPERIOD_F_ACTIVE;
137 pPeriod->u64StartWalClk = 0;
138 pPeriod->u64ElapsedWalClk = 0;
139 pPeriod->cFramesTransferred = 0;
140 pPeriod->cIntPending = 0;
141# ifdef LOG_ENABLED
142 pPeriod->Dbg.tsStartNs = 0;
143# endif
144}
145
146/**
147 * Begins a new period life span of a given period.
148 *
149 * @return IPRT status code.
150 * @param pPeriod Stream period to begin new life span for.
151 * @param u64WalClk Wall clock (WALCLK) value to set for the period's starting point.
152 */
153int hdaR3StreamPeriodBegin(PHDASTREAMPERIOD pPeriod, uint64_t u64WalClk)
154{
155 Assert(!(pPeriod->fStatus & HDASTREAMPERIOD_F_ACTIVE)); /* No nested calls. */
156
157 pPeriod->fStatus |= HDASTREAMPERIOD_F_ACTIVE;
158 pPeriod->u64StartWalClk = u64WalClk;
159 pPeriod->u64ElapsedWalClk = 0;
160 pPeriod->cFramesTransferred = 0;
161 pPeriod->cIntPending = 0;
162# ifdef LOG_ENABLED
163 pPeriod->Dbg.tsStartNs = RTTimeNanoTS();
164# endif
165
166 Log3Func(("[SD%RU8] Starting @ %RU64 (%RU64 long)\n", pPeriod->u8SD, pPeriod->u64StartWalClk, pPeriod->u64DurationWalClk));
167 return VINF_SUCCESS;
168}
169
170/**
171 * Ends a formerly begun period life span.
172 *
173 * @param pPeriod Stream period to end life span for.
174 */
175void hdaR3StreamPeriodEnd(PHDASTREAMPERIOD pPeriod)
176{
177 Log3Func(("[SD%RU8] Took %zuus\n", pPeriod->u8SD, (RTTimeNanoTS() - pPeriod->Dbg.tsStartNs) / 1000));
178
179 if (!(pPeriod->fStatus & HDASTREAMPERIOD_F_ACTIVE))
180 return;
181
182 /* Sanity. */
183 AssertMsg(pPeriod->cIntPending == 0,
184 ("%RU8 interrupts for stream #%RU8 still pending -- so ending a period might trigger audio hangs\n",
185 pPeriod->cIntPending, pPeriod->u8SD));
186 Assert(hdaR3StreamPeriodIsComplete(pPeriod));
187
188 pPeriod->fStatus &= ~HDASTREAMPERIOD_F_ACTIVE;
189}
190
191/**
192 * Pauses a period. All values remain intact.
193 *
194 * @param pPeriod Stream period to pause.
195 */
196void hdaR3StreamPeriodPause(PHDASTREAMPERIOD pPeriod)
197{
198 AssertMsg((pPeriod->fStatus & HDASTREAMPERIOD_F_ACTIVE), ("Period %p already in inactive state\n", pPeriod));
199
200 pPeriod->fStatus &= ~HDASTREAMPERIOD_F_ACTIVE;
201
202 Log3Func(("[SD%RU8]\n", pPeriod->u8SD));
203}
204
205/**
206 * Resumes a formerly paused period.
207 *
208 * @param pPeriod Stream period to resume.
209 */
210void hdaR3StreamPeriodResume(PHDASTREAMPERIOD pPeriod)
211{
212 AssertMsg(!(pPeriod->fStatus & HDASTREAMPERIOD_F_ACTIVE), ("Period %p already in active state\n", pPeriod));
213
214 pPeriod->fStatus |= HDASTREAMPERIOD_F_ACTIVE;
215
216 Log3Func(("[SD%RU8]\n", pPeriod->u8SD));
217}
218
219/**
220 * Returns the wall clock (WALCLK) value for a given amount of stream period audio frames.
221 *
222 * @return Calculated wall clock value.
223 * @param pPeriod Stream period to calculate wall clock value for.
224 * @param uFrames Number of audio frames to calculate wall clock value for.
225 *
226 * @remark Calculation depends on the given stream period and assumes a 24 MHz wall clock counter (WALCLK).
227 */
228uint64_t hdaR3StreamPeriodFramesToWalClk(PHDASTREAMPERIOD pPeriod, uint32_t uFrames)
229{
230 /* Prevent division by zero. */
231 const uint32_t uHz = pPeriod->u32Hz ? pPeriod->u32Hz : 1;
232
233 /* 24 MHz wall clock (WALCLK): 42ns resolution. */
234 return ASMMultU64ByU32DivByU32(uFrames, 24000000, uHz);
235}
236
237/**
238 * Returns the absolute wall clock (WALCLK) value for the already elapsed time of
239 * a given stream period.
240 *
241 * @return Absolute elapsed time as wall clock (WALCLK) value.
242 * @param pPeriod Stream period to use.
243 */
244uint64_t hdaR3StreamPeriodGetAbsElapsedWalClk(PHDASTREAMPERIOD pPeriod)
245{
246 return pPeriod->u64StartWalClk
247 + pPeriod->u64ElapsedWalClk
248 + pPeriod->i64DelayWalClk;
249}
250
251/**
252 * Returns the absolute wall clock (WALCLK) value for the calculated end time of
253 * a given stream period.
254 *
255 * @return Absolute end time as wall clock (WALCLK) value.
256 * @param pPeriod Stream period to use.
257 */
258uint64_t hdaR3StreamPeriodGetAbsEndWalClk(PHDASTREAMPERIOD pPeriod)
259{
260 return pPeriod->u64StartWalClk + pPeriod->u64DurationWalClk;
261}
262
263/**
264 * Returns the remaining audio frames to process for a given stream period.
265 *
266 * @return Number of remaining audio frames to process. 0 if all were processed.
267 * @param pPeriod Stream period to return value for.
268 */
269uint32_t hdaR3StreamPeriodGetRemainingFrames(PHDASTREAMPERIOD pPeriod)
270{
271 Assert(pPeriod->cFramesToTransfer >= pPeriod->cFramesTransferred);
272 return pPeriod->cFramesToTransfer - pPeriod->cFramesTransferred;
273}
274
275/**
276 * Tells whether a given stream period has elapsed (time-wise) or not.
277 *
278 * @return true if the stream period has elapsed, false if not.
279 * @param pPeriod Stream period to get status for.
280 */
281bool hdaR3StreamPeriodHasElapsed(PHDASTREAMPERIOD pPeriod)
282{
283 return (pPeriod->u64ElapsedWalClk >= pPeriod->u64DurationWalClk);
284}
285
286/**
287 * Tells whether a given stream period has passed the given absolute wall clock (WALCLK)
288 * time or not
289 *
290 * @return true if the stream period has passed the given time, false if not.
291 * @param pPeriod Stream period to get status for.
292 * @param u64WalClk Absolute wall clock (WALCLK) time to check for.
293 */
294bool hdaR3StreamPeriodHasPassedAbsWalClk(PHDASTREAMPERIOD pPeriod, uint64_t u64WalClk)
295{
296 /* Period not in use? */
297 if (!(pPeriod->fStatus & HDASTREAMPERIOD_F_ACTIVE))
298 return true; /* ... implies that it has passed. */
299
300 if (hdaR3StreamPeriodHasElapsed(pPeriod))
301 return true; /* Period already has elapsed. */
302
303 return (pPeriod->u64StartWalClk + pPeriod->u64ElapsedWalClk) >= u64WalClk;
304}
305
306/**
307 * Tells whether a given stream period has some required interrupts pending or not.
308 *
309 * @return true if period has interrupts pending, false if not.
310 * @param pPeriod Stream period to get status for.
311 */
312bool hdaR3StreamPeriodNeedsInterrupt(PHDASTREAMPERIOD pPeriod)
313{
314 return pPeriod->cIntPending > 0;
315}
316
317/**
318 * Acquires (references) an (pending) interrupt for a given stream period.
319 *
320 * @param pPeriod Stream period to acquire interrupt for.
321 *
322 * @remark This routine does not do any actual interrupt processing; it only
323 * keeps track of the required (pending) interrupts for a stream period.
324 */
325void hdaR3StreamPeriodAcquireInterrupt(PHDASTREAMPERIOD pPeriod)
326{
327 uint32_t cIntPending = pPeriod->cIntPending;
328 if (cIntPending)
329 {
330 Log3Func(("[SD%RU8] Already pending\n", pPeriod->u8SD));
331 return;
332 }
333
334 pPeriod->cIntPending++;
335
336 Log3Func(("[SD%RU8] %RU32\n", pPeriod->u8SD, pPeriod->cIntPending));
337}
338
339/**
340 * Releases (dereferences) a pending interrupt.
341 *
342 * @param pPeriod Stream period to release pending interrupt for.
343 */
344void hdaR3StreamPeriodReleaseInterrupt(PHDASTREAMPERIOD pPeriod)
345{
346 Assert(pPeriod->cIntPending);
347 pPeriod->cIntPending--;
348
349 Log3Func(("[SD%RU8] %RU32\n", pPeriod->u8SD, pPeriod->cIntPending));
350}
351
352/**
353 * Adds an amount of (processed) audio frames to a given stream period.
354 *
355 * @return IPRT status code.
356 * @param pPeriod Stream period to add audio frames to.
357 * @param framesInc Audio frames to add.
358 */
359void hdaR3StreamPeriodInc(PHDASTREAMPERIOD pPeriod, uint32_t framesInc)
360{
361 pPeriod->cFramesTransferred += framesInc;
362 Assert(pPeriod->cFramesTransferred <= pPeriod->cFramesToTransfer);
363
364 pPeriod->u64ElapsedWalClk = hdaR3StreamPeriodFramesToWalClk(pPeriod, pPeriod->cFramesTransferred);
365 Assert(pPeriod->u64ElapsedWalClk <= pPeriod->u64DurationWalClk);
366
367 Log3Func(("[SD%RU8] cbTransferred=%RU32, u64ElapsedWalClk=%RU64\n",
368 pPeriod->u8SD, pPeriod->cFramesTransferred, pPeriod->u64ElapsedWalClk));
369}
370
371/**
372 * Tells whether a given stream period is considered as complete or not.
373 *
374 * @return true if stream period is complete, false if not.
375 * @param pPeriod Stream period to report status for.
376 *
377 * @remark A stream period is considered complete if it has 1) passed (elapsed) its calculated period time
378 * and 2) processed all required audio frames.
379 */
380bool hdaR3StreamPeriodIsComplete(PHDASTREAMPERIOD pPeriod)
381{
382 const bool fIsComplete = /* Has the period elapsed time-wise? */
383 hdaR3StreamPeriodHasElapsed(pPeriod)
384 /* All frames transferred? */
385 && pPeriod->cFramesTransferred >= pPeriod->cFramesToTransfer;
386# ifdef VBOX_STRICT
387 if (fIsComplete)
388 {
389 Assert(pPeriod->cFramesTransferred == pPeriod->cFramesToTransfer);
390 Assert(pPeriod->u64ElapsedWalClk == pPeriod->u64DurationWalClk);
391 }
392# endif
393
394 Log3Func(("[SD%RU8] Period %s - runtime %RU64 / %RU64 (abs @ %RU64, starts @ %RU64, ends @ %RU64), %RU8 IRQs pending\n",
395 pPeriod->u8SD,
396 fIsComplete ? "COMPLETE" : "NOT COMPLETE YET",
397 pPeriod->u64ElapsedWalClk, pPeriod->u64DurationWalClk,
398 hdaR3StreamPeriodGetAbsElapsedWalClk(pPeriod), pPeriod->u64StartWalClk,
399 hdaR3StreamPeriodGetAbsEndWalClk(pPeriod), pPeriod->cIntPending));
400
401 return fIsComplete;
402}
403
404#endif /* IN_RING3 */
405
Note: See TracBrowser for help on using the repository browser.

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