Skip to content

Commit

Permalink
Bug 1617915 - Create unit tests to verify that MTG sets the expected …
Browse files Browse the repository at this point in the history
…device id. r=padenot

Create gtests to verify that the device id provided to the constructor of MTG will be the one used by cubeb as output device id. In addition to that, verify that using a different device id will result in creating/getting a different MTG instance.

Differential Revision: https://phabricator.services.mozilla.com/D64136

--HG--
extra : moz-landing-system : lando
  • Loading branch information
achronop committed Mar 16, 2020
1 parent 32d51dd commit d4de5d5
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 0 deletions.
108 changes: 108 additions & 0 deletions dom/media/gtest/TestAudioTrackGraph.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-*/
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "MediaTrackGraphImpl.h"

#include "gmock/gmock.h"
#include "gtest/gtest-printers.h"
#include "gtest/gtest.h"

#include "GMPTestMonitor.h"
#include "MockCubeb.h"

TEST(TestAudioTrackGraph, DifferentDeviceIDs)
{
MockCubeb* cubeb = new MockCubeb();
CubebUtils::ForceSetCubebContext(cubeb->AsCubebContext());

MediaTrackGraph* g1 = MediaTrackGraph::GetInstance(
MediaTrackGraph::AUDIO_THREAD_DRIVER, /*window*/ nullptr,
MediaTrackGraph::REQUEST_DEFAULT_SAMPLE_RATE,
/*OutputDeviceID*/ nullptr);

MediaTrackGraph* g2 = MediaTrackGraph::GetInstance(
MediaTrackGraph::AUDIO_THREAD_DRIVER, /*window*/ nullptr,
MediaTrackGraph::REQUEST_DEFAULT_SAMPLE_RATE,
/*OutputDeviceID*/ reinterpret_cast<cubeb_devid>(1));

MediaTrackGraph* g1_2 = MediaTrackGraph::GetInstance(
MediaTrackGraph::AUDIO_THREAD_DRIVER, /*window*/ nullptr,
MediaTrackGraph::REQUEST_DEFAULT_SAMPLE_RATE,
/*OutputDeviceID*/ nullptr);

MediaTrackGraph* g2_2 = MediaTrackGraph::GetInstance(
MediaTrackGraph::AUDIO_THREAD_DRIVER, /*window*/ nullptr,
MediaTrackGraph::REQUEST_DEFAULT_SAMPLE_RATE,
/*OutputDeviceID*/ reinterpret_cast<cubeb_devid>(1));

EXPECT_NE(g1, g2) << "Different graphs have due to different device ids";
EXPECT_EQ(g1, g1_2) << "Same graphs for same device ids";
EXPECT_EQ(g2, g2_2) << "Same graphs for same device ids";

// Dummy track to make graph rolling. Add it and remove it to remove the
// graph from the global hash table and let it shutdown.
RefPtr<SourceMediaTrack> dummySource1 =
g1->CreateSourceTrack(MediaSegment::AUDIO);
RefPtr<SourceMediaTrack> dummySource2 =
g2->CreateSourceTrack(MediaSegment::AUDIO);
dummySource1->Destroy();
dummySource2->Destroy();
}

TEST(TestAudioTrackGraph, SetOutputDeviceID)
{
MockCubeb* cubeb = new MockCubeb();
CubebUtils::ForceSetCubebContext(cubeb->AsCubebContext());

// Set the output device id in GetInstance method confirm that it is the one
// used in cubeb_stream_init.
MediaTrackGraph* graph = MediaTrackGraph::GetInstance(
MediaTrackGraph::AUDIO_THREAD_DRIVER, /*window*/ nullptr,
MediaTrackGraph::REQUEST_DEFAULT_SAMPLE_RATE,
/*OutputDeviceID*/ reinterpret_cast<cubeb_devid>(1));

// Cubeb stream has not been init yet, confirm invalid device id `-1`
EXPECT_EQ(cubeb->GetCurrentOutputDeviceID(),
reinterpret_cast<cubeb_devid>(-1))
<< "Initial state, invalid output device id";

// Dummy track to make graph rolling. Add it and remove it to remove the
// graph from the global hash table and let it shutdown.
RefPtr<SourceMediaTrack> dummySource =
graph->CreateSourceTrack(MediaSegment::AUDIO);

/* Use a ControlMessage to signal that the AudioCallbackDriver has started. */
class Message : public ControlMessage {
public:
explicit Message(MediaTrack* aTrack) : ControlMessage(aTrack) {}
void Run() override {
MOZ_ASSERT(mTrack->GraphImpl()->CurrentDriver()->AsAudioCallbackDriver());
mGraphStartedPromise.Resolve(true, __func__);
}
void RunDuringShutdown() override {
// During shutdown we still want the listener's NotifyRemoved to be
// called, since not doing that might block shutdown of other modules.
Run();
}
MozPromiseHolder<GenericPromise> mGraphStartedPromise;
};

GMPTestMonitor mon;
UniquePtr<Message> message = MakeUnique<Message>(dummySource);
RefPtr<GenericPromise> p = message->mGraphStartedPromise.Ensure(__func__);
p->Then(GetMainThreadSerialEventTarget(), __func__,
[&mon, cubeb, dummySource]() {
EXPECT_EQ(cubeb->GetCurrentOutputDeviceID(),
reinterpret_cast<cubeb_devid>(1))
<< "After init confirm the expected output device id";
// Test has finished, destroy the track to shutdown the MTG.
dummySource->Destroy();
mon.SetFinished();
});

dummySource->GraphImpl()->AppendMessage(std::move(message));

mon.AwaitFinished();
}
1 change: 1 addition & 0 deletions dom/media/gtest/moz.build
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ UNIFIED_SOURCES += [
'TestAudioPacketizer.cpp',
'TestAudioSegment.cpp',
'TestAudioTrackEncoder.cpp',
'TestAudioTrackGraph.cpp',
'TestBenchmarkStorage.cpp',
'TestBitWriter.cpp',
'TestBlankVideoDataCreator.cpp',
Expand Down

0 comments on commit d4de5d5

Please sign in to comment.