VirtualBox

Ignore:
Timestamp:
Jan 19, 2018 12:20:33 PM (7 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
120374
Message:

Audio/Main: More code needed for attaching / detaching host backends at runtime.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Main/src-client/AudioDriver.cpp

    r70626 r70644  
    3535    : mpConsole(pConsole)
    3636    , mfAttached(false)
    37     , muLUN(UINT8_MAX)
    3837{
    3938}
     
    4342}
    4443
    45 /**
    46  * Returns the next free LUN of the audio device driver
    47  * chain.
    48  *
    49  * @return unsigned             Next free LUN in audio device driver chain.
    50  */
    51 unsigned AudioDriver::getFreeLUN(void)
    52 {
    53     Console::SafeVMPtrQuiet ptrVM(mpConsole);
    54     Assert(ptrVM.isOk());
    55 
    56     PUVM pUVM = ptrVM.rawUVM();
    57     AssertPtr(pUVM);
    58 
    59     unsigned uLUN = 0;
    60 
    61     PCFGMNODE pDevLUN;
    62     for (;;)
    63     {
    64         pDevLUN = CFGMR3GetChildF(CFGMR3GetRootU(pUVM), "Devices/%s/%u/LUN#%u/", mCfg.strDev.c_str(), mCfg.uInst, uLUN);
    65         if (!pDevLUN)
    66             break;
    67         uLUN++;
    68     }
    69 
    70     return uLUN;
    71 }
    72 
    7344
    7445/**
    7546 * Initializes the audio driver with a certain (device) configuration.
    76  *
    77  * @note The driver's LUN will be determined on runtime when attaching the
    78  *       driver to the audio driver chain.
    7947 *
    8048 * @returns VBox status code.
     
    8452{
    8553    AssertPtrReturn(pCfg, VERR_INVALID_POINTER);
     54
     55    /* Sanity. */
     56    AssertReturn(pCfg->strDev.isNotEmpty(),  VERR_INVALID_PARAMETER);
     57    AssertReturn(pCfg->uLUN != UINT8_MAX,    VERR_INVALID_PARAMETER);
     58    AssertReturn(pCfg->strName.isNotEmpty(), VERR_INVALID_PARAMETER);
    8659
    8760    /* Apply configuration. */
     
    142115    Assert(ptrVM.isOk());
    143116
    144 
    145117    if (pThis->mfAttached) /* Already attached? Bail out. */
    146118    {
     
    151123    AudioDriverCfg *pCfg = &pThis->mCfg;
    152124
    153     unsigned uLUN = pThis->muLUN;
    154     if (uLUN == UINT8_MAX) /* No LUN assigned / configured yet? Retrieve it. */
    155         uLUN = pThis->getFreeLUN();
    156 
    157125    LogFunc(("strName=%s, strDevice=%s, uInst=%u, uLUN=%u\n",
    158              pCfg->strName.c_str(), pCfg->strDev.c_str(), pCfg->uInst, uLUN));
    159 
    160     int vrc = pThis->configure(uLUN, true /* Attach */);
    161     if (RT_SUCCESS(vrc))
    162         vrc = PDMR3DeviceAttach(ptrVM.rawUVM(), pCfg->strDev.c_str(), pCfg->uInst, uLUN, 0 /* fFlags */,
    163                                 NULL /* ppBase */);
    164     if (RT_SUCCESS(vrc))
    165     {
    166         pThis->muLUN      = uLUN;
     126             pCfg->strName.c_str(), pCfg->strDev.c_str(), pCfg->uInst, pCfg->uLUN));
     127
     128    int rc = pThis->configure(pCfg->uLUN, true /* Attach */);
     129    if (RT_SUCCESS(rc))
     130        rc = PDMR3DriverAttach(ptrVM.rawUVM(), pCfg->strDev.c_str(), pCfg->uInst, pCfg->uLUN, 0 /* fFlags */,
     131                               NULL /* ppBase */);
     132    if (RT_SUCCESS(rc))
     133    {
    167134        pThis->mfAttached = true;
    168 
    169         LogRel2(("%s: Driver attached (LUN #%u)\n", pCfg->strName.c_str(), pThis->muLUN));
     135        LogRel2(("%s: Driver attached (LUN #%u)\n", pCfg->strName.c_str(), pCfg->uLUN));
    170136    }
    171137    else
    172         LogRel(("%s: Failed to attach audio driver, rc=%Rrc\n", pCfg->strName.c_str(), vrc));
    173 
    174     return vrc;
     138        LogRel(("%s: Failed to attach audio driver, rc=%Rrc\n", pCfg->strName.c_str(), rc));
     139
     140    LogFunc(("Returning %Rrc\n", rc));
     141    return rc;
    175142}
    176143
     
    232199    Assert(ptrVM.isOk());
    233200
    234     Assert(pThis->muLUN != UINT8_MAX);
    235 
    236201    AudioDriverCfg *pCfg = &pThis->mCfg;
    237202
     203    Assert(pCfg->uLUN != UINT8_MAX);
     204
    238205    LogFunc(("strName=%s, strDevice=%s, uInst=%u, uLUN=%u\n",
    239              pCfg->strName.c_str(), pCfg->strDev.c_str(), pCfg->uInst, pThis->muLUN));
    240 
    241     int vrc = PDMR3DriverDetach(ptrVM.rawUVM(), pCfg->strDev.c_str(), pCfg->uInst, pThis->muLUN, "AUDIO",
    242                             0 /* iOccurrence */, 0 /* fFlags */);
    243     if (RT_SUCCESS(vrc))
    244         vrc = pThis->configure(pThis->muLUN, false /* Detach */);
    245 
    246     if (RT_SUCCESS(vrc))
    247     {
    248         pThis->muLUN      = UINT8_MAX;
     206             pCfg->strName.c_str(), pCfg->strDev.c_str(), pCfg->uInst, pCfg->uLUN));
     207
     208    int rc = PDMR3DeviceDetach(ptrVM.rawUVM(), pCfg->strDev.c_str(), pCfg->uInst, pCfg->uLUN, 0 /* fFlags */);
     209    if (RT_SUCCESS(rc))
     210        rc = pThis->configure(pCfg->uLUN, false /* Detach */);
     211
     212    if (RT_SUCCESS(rc))
     213    {
    249214        pThis->mfAttached = false;
    250 
    251215        LogRel2(("%s: Driver detached\n", pCfg->strName.c_str()));
    252216    }
    253217    else
    254         LogRel(("%s: Failed to detach audio driver, rc=%Rrc\n", pCfg->strName.c_str(), vrc));
    255 
    256     return vrc;
     218        LogRel(("%s: Failed to detach audio driver, rc=%Rrc\n", pCfg->strName.c_str(), rc));
     219
     220    LogFunc(("Returning %Rrc\n", rc));
     221    return rc;
    257222}
    258223
     
    268233int AudioDriver::configure(unsigned uLUN, bool fAttach)
    269234{
    270     int rc = VINF_SUCCESS;
    271 
    272235    Console::SafeVMPtrQuiet ptrVM(mpConsole);
    273236    Assert(ptrVM.isOk());
     
    276239    AssertPtr(pUVM);
    277240
    278     PCFGMNODE pRoot   = CFGMR3GetRootU(pUVM);
     241    PCFGMNODE pRoot = CFGMR3GetRootU(pUVM);
    279242    AssertPtr(pRoot);
    280     PCFGMNODE pDev0   = CFGMR3GetChildF(pRoot, "Devices/%s/%u/", mCfg.strDev.c_str(), mCfg.uInst);
     243    PCFGMNODE pDev0 = CFGMR3GetChildF(pRoot, "Devices/%s/%u/", mCfg.strDev.c_str(), mCfg.uInst);
    281244
    282245    if (!pDev0) /* No audio device configured? Bail out. */
     
    286249    }
    287250
     251    int rc = VINF_SUCCESS;
     252
    288253    PCFGMNODE pDevLun = CFGMR3GetChildF(pDev0, "LUN#%u/", uLUN);
    289254
    290255    if (fAttach)
    291256    {
    292         AssertMsg(uLUN != UINT8_MAX, ("%s: LUN is undefined when it must not\n", mCfg.strName.c_str()));
    293 
    294         if (!pDevLun)
     257        do
    295258        {
    296             LogRel2(("%s: Configuring audio driver (to LUN #%RU8)\n", mCfg.strName.c_str(), uLUN));
    297 
    298             PCFGMNODE pLunL0;
    299             CFGMR3InsertNodeF(pDev0, &pLunL0, "LUN#%RU8", uLUN);
    300             CFGMR3InsertString(pLunL0, "Driver", "AUDIO");
    301 
    302             PCFGMNODE pLunCfg;
    303             CFGMR3InsertNode(pLunL0,   "Config", &pLunCfg);
    304                 CFGMR3InsertStringF(pLunCfg, "DriverName",    "%s", mCfg.strName.c_str());
    305                 CFGMR3InsertInteger(pLunCfg, "InputEnabled",  0); /* Play safe by default. */
    306                 CFGMR3InsertInteger(pLunCfg, "OutputEnabled", 1);
    307 
    308             PCFGMNODE pLunL1;
    309             CFGMR3InsertNode(pLunL0, "AttachedDriver", &pLunL1);
    310                 CFGMR3InsertStringF(pLunL1, "Driver", "%s", mCfg.strName.c_str());
    311 
    312                 CFGMR3InsertNode(pLunL1, "Config", &pLunCfg);
     259            AssertMsgBreakStmt(pDevLun, ("%s: Device LUN #%u not found\n", mCfg.strName.c_str(), uLUN), rc = VERR_NOT_FOUND);
     260
     261            LogRel2(("%s: Configuring audio driver (to LUN #%u)\n", mCfg.strName.c_str(), uLUN));
     262
     263            PCFGMNODE pLunCfg = CFGMR3GetChild(pDevLun, "Config");
     264            AssertBreakStmt(pLunCfg, rc = VERR_NOT_FOUND);
     265
     266                rc = CFGMR3InsertStringF(pLunCfg, "DriverName",    "%s", mCfg.strName.c_str());    AssertRCBreak(rc);
     267
     268                rc = CFGMR3InsertInteger(pLunCfg, "InputEnabled",  0); /* Play safe by default. */ AssertRCBreak(rc);
     269                rc = CFGMR3InsertInteger(pLunCfg, "OutputEnabled", 1);                             AssertRCBreak(rc);
     270
     271            PCFGMNODE pAttachedDriver, pAttachedDriverCfg;
     272            rc = CFGMR3InsertNode(pDevLun, "AttachedDriver", &pAttachedDriver);                    AssertRCBreak(rc);
     273                rc = CFGMR3InsertStringF(pAttachedDriver, "Driver", "%s", mCfg.strName.c_str());   AssertRCBreak(rc);
     274                rc = CFGMR3InsertNode(pAttachedDriver, "Config", &pAttachedDriverCfg);             AssertRCBreak(rc);
    313275
    314276                /* Call the (virtual) method for driver-specific configuration. */
    315                 configureDriver(pLunCfg);
    316         }
    317         else
    318             rc = VERR_ALREADY_EXISTS;
     277                rc = configureDriver(pAttachedDriverCfg);                                          AssertRCBreak(rc);
     278
     279        } while (0);
    319280    }
    320281    else /* Detach */
     
    323284        {
    324285            LogRel2(("%s: Unconfiguring audio driver\n", mCfg.strName.c_str()));
    325             CFGMR3RemoveNode(pDevLun);
     286
     287            PCFGMNODE pLunCfg = CFGMR3GetChild(pDevLun, "Config");
     288            if (pLunCfg)
     289                CFGMR3RemoveNode(pLunCfg);
     290
     291            rc = CFGMR3InsertNode(pDevLun, "Config", &pLunCfg);
     292
     293            PCFGMNODE pLunAttachedDriver = CFGMR3GetChild(pDevLun, "AttachedDriver");
     294            if (pLunAttachedDriver)
     295                CFGMR3RemoveNode(pLunAttachedDriver);
    326296        }
    327         else
    328             rc = VERR_NOT_FOUND;
    329     }
    330 
    331 #ifdef DEBUG_andy
    332     CFGMR3Dump(pDev0);
    333 #endif
     297    }
    334298
    335299    if (RT_FAILURE(rc))
     
    337301                mCfg.strName.c_str(), fAttach ? "Configuring" : "Unconfiguring", rc));
    338302
     303    LogFunc(("Returning %Rrc\n", rc));
    339304    return rc;
    340305}
Note: See TracChangeset for help on using the changeset viewer.

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