Adopt absl::string_view in modules/audio_device/

Bug: webrtc:13579
Change-Id: I6e8a90281a9d70a40364b6df5fee4f0a55b4a797
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/269060
Commit-Queue: Ali Tofigh <alito@webrtc.org>
Reviewed-by: Henrik Andreassson <henrika@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#37607}
This commit is contained in:
Ali Tofigh 2022-07-20 21:08:06 +02:00 committed by WebRTC LUCI CQ
parent 3fada3563a
commit 82c29716c0
16 changed files with 85 additions and 54 deletions

View File

@ -101,6 +101,7 @@ rtc_library("audio_device_name") {
"audio_device_name.cc",
"audio_device_name.h",
]
absl_deps = [ "//third_party/abseil-cpp/absl/strings" ]
}
rtc_source_set("windows_core_audio_utility") {
@ -121,6 +122,7 @@ rtc_source_set("windows_core_audio_utility") {
"../../rtc_base:stringutils",
"../../rtc_base/win:windows_version",
]
absl_deps = [ "//third_party/abseil-cpp/absl/strings:strings" ]
libs = [ "oleaut32.lib" ]
}
@ -166,7 +168,10 @@ rtc_source_set("audio_device_module_from_input_and_output") {
"../../rtc_base/win:scoped_com_initializer",
"../../rtc_base/win:windows_version",
]
absl_deps = [ "//third_party/abseil-cpp/absl/types:optional" ]
absl_deps = [
"//third_party/abseil-cpp/absl/strings:strings",
"//third_party/abseil-cpp/absl/types:optional",
]
}
}
@ -434,7 +439,10 @@ if (rtc_include_tests && !build_with_chromium) {
"../../test:fileutils",
"../../test:test_support",
]
absl_deps = [ "//third_party/abseil-cpp/absl/types:optional" ]
absl_deps = [
"//third_party/abseil-cpp/absl/strings",
"//third_party/abseil-cpp/absl/types:optional",
]
if (is_linux || is_chromeos || is_mac || is_win) {
sources += [ "audio_device_unittest.cc" ]
}

View File

@ -18,6 +18,7 @@
#include <string>
#include <vector>
#include "absl/strings/string_view.h"
#include "api/scoped_refptr.h"
#include "api/task_queue/default_task_queue_factory.h"
#include "api/task_queue/task_queue_factory.h"
@ -104,7 +105,7 @@ class AudioStreamInterface {
class FileAudioStream : public AudioStreamInterface {
public:
FileAudioStream(size_t num_callbacks,
const std::string& file_name,
absl::string_view file_name,
int sample_rate)
: file_size_in_bytes_(0), sample_rate_(sample_rate), file_pos_(0) {
file_size_in_bytes_ = test::GetFileSize(file_name);
@ -114,7 +115,7 @@ class FileAudioStream : public AudioStreamInterface {
const size_t num_16bit_samples =
test::GetFileSize(file_name) / kBytesPerSample;
file_.reset(new int16_t[num_16bit_samples]);
FILE* audio_file = fopen(file_name.c_str(), "rb");
FILE* audio_file = fopen(std::string(file_name).c_str(), "rb");
EXPECT_NE(audio_file, nullptr);
size_t num_samples_read =
fread(file_.get(), sizeof(int16_t), num_16bit_samples, audio_file);
@ -567,7 +568,7 @@ class AudioDeviceTest : public ::testing::Test {
return active;
}
bool DisableTestForThisDevice(const std::string& model) {
bool DisableTestForThisDevice(absl::string_view model) {
return (build_info_->GetDeviceModel() == model);
}

View File

@ -10,14 +10,15 @@
#include "modules/audio_device/audio_device_name.h"
#include <utility>
#include "absl/strings/string_view.h"
namespace webrtc {
const char AudioDeviceName::kDefaultDeviceId[] = "default";
AudioDeviceName::AudioDeviceName(std::string device_name, std::string unique_id)
: device_name(std::move(device_name)), unique_id(std::move(unique_id)) {}
AudioDeviceName::AudioDeviceName(absl::string_view device_name,
absl::string_view unique_id)
: device_name(device_name), unique_id(unique_id) {}
bool AudioDeviceName::IsValid() {
return !device_name.empty() && !unique_id.empty();

View File

@ -14,6 +14,8 @@
#include <deque>
#include <string>
#include "absl/strings/string_view.h"
namespace webrtc {
struct AudioDeviceName {
@ -25,7 +27,7 @@ struct AudioDeviceName {
static const char kDefaultDeviceId[];
AudioDeviceName() = default;
AudioDeviceName(std::string device_name, std::string unique_id);
AudioDeviceName(absl::string_view device_name, absl::string_view unique_id);
~AudioDeviceName() = default;

View File

@ -12,6 +12,7 @@
#include <string.h>
#include "absl/strings/string_view.h"
#include "rtc_base/checks.h"
#include "rtc_base/logging.h"
#include "rtc_base/platform_thread.h"
@ -29,8 +30,8 @@ const size_t kPlayoutBufferSize =
const size_t kRecordingBufferSize =
kRecordingFixedSampleRate / 100 * kRecordingNumChannels * 2;
FileAudioDevice::FileAudioDevice(const char* inputFilename,
const char* outputFilename)
FileAudioDevice::FileAudioDevice(absl::string_view inputFilename,
absl::string_view outputFilename)
: _ptrAudioBuffer(NULL),
_recordingBuffer(NULL),
_playoutBuffer(NULL),

View File

@ -16,6 +16,7 @@
#include <memory>
#include <string>
#include "absl/strings/string_view.h"
#include "modules/audio_device/audio_device_generic.h"
#include "rtc_base/platform_thread.h"
#include "rtc_base/synchronization/mutex.h"
@ -34,7 +35,8 @@ class FileAudioDevice : public AudioDeviceGeneric {
// The input file should be a readable 48k stereo raw file, and the output
// file should point to a writable location. The output format will also be
// 48k stereo raw audio.
FileAudioDevice(const char* inputFilename, const char* outputFilename);
FileAudioDevice(absl::string_view inputFilename,
absl::string_view outputFilename);
virtual ~FileAudioDevice();
// Retrieve the currently utilized audio layer

View File

@ -14,8 +14,10 @@
#include <cstdlib>
#include "absl/strings/string_view.h"
#include "modules/audio_device/dummy/file_audio_device.h"
#include "rtc_base/logging.h"
#include "rtc_base/string_utils.h"
namespace webrtc {
@ -38,15 +40,15 @@ FileAudioDevice* FileAudioDeviceFactory::CreateFileAudioDevice() {
}
void FileAudioDeviceFactory::SetFilenamesToUse(
const char* inputAudioFilename,
const char* outputAudioFilename) {
absl::string_view inputAudioFilename,
absl::string_view outputAudioFilename) {
#ifdef WEBRTC_DUMMY_FILE_DEVICES
RTC_DCHECK_LT(strlen(inputAudioFilename), MAX_FILENAME_LEN);
RTC_DCHECK_LT(strlen(outputAudioFilename), MAX_FILENAME_LEN);
RTC_DCHECK_LT(inputAudioFilename.size(), MAX_FILENAME_LEN);
RTC_DCHECK_LT(outputAudioFilename.size(), MAX_FILENAME_LEN);
// Copy the strings since we don't know the lifetime of the input pointers.
strncpy(_inputAudioFilename, inputAudioFilename, MAX_FILENAME_LEN);
strncpy(_outputAudioFilename, outputAudioFilename, MAX_FILENAME_LEN);
rtc::strcpyn(_inputAudioFilename, MAX_FILENAME_LEN, inputAudioFilename);
rtc::strcpyn(_outputAudioFilename, MAX_FILENAME_LEN, outputAudioFilename);
_isConfigured = true;
#else
// Sanity: must be compiled with the right define to run this.

View File

@ -13,6 +13,8 @@
#include <stdint.h>
#include "absl/strings/string_view.h"
namespace webrtc {
class FileAudioDevice;
@ -27,8 +29,8 @@ class FileAudioDeviceFactory {
// The input file must be a readable 48k stereo raw file. The output
// file must be writable. The strings will be copied.
static void SetFilenamesToUse(const char* inputAudioFilename,
const char* outputAudioFilename);
static void SetFilenamesToUse(absl::string_view inputAudioFilename,
absl::string_view outputAudioFilename);
private:
enum : uint32_t { MAX_FILENAME_LEN = 512 };

View File

@ -18,6 +18,7 @@
#include <utility>
#include <vector>
#include "absl/strings/string_view.h"
#include "api/array_view.h"
#include "api/make_ref_counted.h"
#include "common_audio/wav_file.h"
@ -266,7 +267,7 @@ class PulsedNoiseCapturerImpl final
class WavFileReader final : public TestAudioDeviceModule::Capturer {
public:
WavFileReader(std::string filename,
WavFileReader(absl::string_view filename,
int sampling_frequency_in_hz,
int num_channels,
bool repeat)
@ -320,7 +321,7 @@ class WavFileReader final : public TestAudioDeviceModule::Capturer {
class WavFileWriter final : public TestAudioDeviceModule::Renderer {
public:
WavFileWriter(std::string filename,
WavFileWriter(absl::string_view filename,
int sampling_frequency_in_hz,
int num_channels)
: WavFileWriter(std::make_unique<WavWriter>(filename,
@ -353,7 +354,7 @@ class WavFileWriter final : public TestAudioDeviceModule::Renderer {
class BoundedWavFileWriter : public TestAudioDeviceModule::Renderer {
public:
BoundedWavFileWriter(std::string filename,
BoundedWavFileWriter(absl::string_view filename,
int sampling_frequency_in_hz,
int num_channels)
: sampling_frequency_in_hz_(sampling_frequency_in_hz),
@ -467,7 +468,7 @@ TestAudioDeviceModule::CreateDiscardRenderer(int sampling_frequency_in_hz,
}
std::unique_ptr<TestAudioDeviceModule::Capturer>
TestAudioDeviceModule::CreateWavFileReader(std::string filename,
TestAudioDeviceModule::CreateWavFileReader(absl::string_view filename,
int sampling_frequency_in_hz,
int num_channels) {
return std::make_unique<WavFileReader>(filename, sampling_frequency_in_hz,
@ -475,7 +476,8 @@ TestAudioDeviceModule::CreateWavFileReader(std::string filename,
}
std::unique_ptr<TestAudioDeviceModule::Capturer>
TestAudioDeviceModule::CreateWavFileReader(std::string filename, bool repeat) {
TestAudioDeviceModule::CreateWavFileReader(absl::string_view filename,
bool repeat) {
WavReader reader(filename);
int sampling_frequency_in_hz = reader.sample_rate();
int num_channels = rtc::checked_cast<int>(reader.num_channels());
@ -484,7 +486,7 @@ TestAudioDeviceModule::CreateWavFileReader(std::string filename, bool repeat) {
}
std::unique_ptr<TestAudioDeviceModule::Renderer>
TestAudioDeviceModule::CreateWavFileWriter(std::string filename,
TestAudioDeviceModule::CreateWavFileWriter(absl::string_view filename,
int sampling_frequency_in_hz,
int num_channels) {
return std::make_unique<WavFileWriter>(filename, sampling_frequency_in_hz,
@ -492,7 +494,7 @@ TestAudioDeviceModule::CreateWavFileWriter(std::string filename,
}
std::unique_ptr<TestAudioDeviceModule::Renderer>
TestAudioDeviceModule::CreateBoundedWavFileWriter(std::string filename,
TestAudioDeviceModule::CreateBoundedWavFileWriter(absl::string_view filename,
int sampling_frequency_in_hz,
int num_channels) {
return std::make_unique<BoundedWavFileWriter>(

View File

@ -16,6 +16,7 @@
#include <memory>
#include <string>
#include "absl/strings/string_view.h"
#include "api/array_view.h"
#include "api/scoped_refptr.h"
#include "api/task_queue/task_queue_factory.h"
@ -103,7 +104,7 @@ class TestAudioDeviceModule : public AudioDeviceModule {
// Returns a Capturer instance that gets its data from a file. The sample rate
// and channels will be checked against the Wav file.
static std::unique_ptr<Capturer> CreateWavFileReader(
std::string filename,
absl::string_view filename,
int sampling_frequency_in_hz,
int num_channels = 1);
@ -111,12 +112,13 @@ class TestAudioDeviceModule : public AudioDeviceModule {
// Automatically detects sample rate and num of channels.
// `repeat` - if true, the file will be replayed from the start when we reach
// the end of file.
static std::unique_ptr<Capturer> CreateWavFileReader(std::string filename,
bool repeat = false);
static std::unique_ptr<Capturer> CreateWavFileReader(
absl::string_view filename,
bool repeat = false);
// Returns a Renderer instance that writes its data to a file.
static std::unique_ptr<Renderer> CreateWavFileWriter(
std::string filename,
absl::string_view filename,
int sampling_frequency_in_hz,
int num_channels = 1);
@ -124,7 +126,7 @@ class TestAudioDeviceModule : public AudioDeviceModule {
// off silence at the beginning (not necessarily perfect silence, see
// kAmplitudeThreshold) and at the end (only actual 0 samples in this case).
static std::unique_ptr<Renderer> CreateBoundedWavFileWriter(
std::string filename,
absl::string_view filename,
int sampling_frequency_in_hz,
int num_channels = 1);

View File

@ -10,6 +10,7 @@
#include "modules/audio_device/linux/latebindingsymboltable_linux.h"
#include "absl/strings/string_view.h"
#include "rtc_base/logging.h"
#ifdef WEBRTC_LINUX
@ -32,9 +33,9 @@ inline static const char* GetDllError() {
#endif
}
DllHandle InternalLoadDll(const char dll_name[]) {
DllHandle InternalLoadDll(absl::string_view dll_name) {
#ifdef WEBRTC_LINUX
DllHandle handle = dlopen(dll_name, RTLD_NOW);
DllHandle handle = dlopen(std::string(dll_name).c_str(), RTLD_NOW);
#else
#error Not implemented
#endif
@ -64,10 +65,10 @@ void InternalUnloadDll(DllHandle handle) {
}
static bool LoadSymbol(DllHandle handle,
const char* symbol_name,
absl::string_view symbol_name,
void** symbol) {
#ifdef WEBRTC_LINUX
*symbol = dlsym(handle, symbol_name);
*symbol = dlsym(handle, std::string(symbol_name).c_str());
char* err = dlerror();
if (err) {
RTC_LOG(LS_ERROR) << "Error loading symbol " << symbol_name << " : " << err;

View File

@ -14,6 +14,7 @@
#include <stddef.h> // for NULL
#include <string.h>
#include "absl/strings/string_view.h"
#include "rtc_base/checks.h"
// This file provides macros for creating "symbol table" classes to simplify the
@ -33,7 +34,7 @@ const DllHandle kInvalidDllHandle = NULL;
#endif
// These are helpers for use only by the class below.
DllHandle InternalLoadDll(const char dll_name[]);
DllHandle InternalLoadDll(absl::string_view);
void InternalUnloadDll(DllHandle handle);

View File

@ -13,6 +13,7 @@
#include <memory>
#include <string>
#include "absl/strings/string_view.h"
#include "modules/audio_device/audio_device_buffer.h"
#include "rtc_base/arraysize.h"
#include "rtc_base/checks.h"
@ -230,7 +231,7 @@ bool CoreAudioBase::IsDefaultCommunicationsDevice(int index) const {
return index == kDefaultCommunications;
}
bool CoreAudioBase::IsDefaultDeviceId(const std::string& device_id) const {
bool CoreAudioBase::IsDefaultDeviceId(absl::string_view device_id) const {
// Returns true if `device_id` corresponds to the id of the default
// device. Note that, if only one device is available (or if the user has not
// explicitly set a default device), `device_id` will also math
@ -242,7 +243,7 @@ bool CoreAudioBase::IsDefaultDeviceId(const std::string& device_id) const {
}
bool CoreAudioBase::IsDefaultCommunicationsDeviceId(
const std::string& device_id) const {
absl::string_view device_id) const {
// Returns true if `device_id` corresponds to the id of the default
// communication device. Note that, if only one device is available (or if
// the user has not explicitly set a communication device), `device_id` will

View File

@ -16,6 +16,7 @@
#include <memory>
#include <string>
#include "absl/strings/string_view.h"
#include "absl/types/optional.h"
#include "api/sequence_checker.h"
#include "modules/audio_device/win/core_audio_utility_win.h"
@ -117,8 +118,8 @@ class CoreAudioBase : public IAudioSessionEvents {
bool IsOutput() const;
bool IsDefaultDevice(int index) const;
bool IsDefaultCommunicationsDevice(int index) const;
bool IsDefaultDeviceId(const std::string& device_id) const;
bool IsDefaultCommunicationsDeviceId(const std::string& device_id) const;
bool IsDefaultDeviceId(absl::string_view device_id) const;
bool IsDefaultCommunicationsDeviceId(absl::string_view device_id) const;
EDataFlow GetDataFlow() const;
bool IsRestarting() const;
int64_t TimeSinceStart() const;

View File

@ -18,6 +18,7 @@
#include <string>
#include <utility>
#include "absl/strings/string_view.h"
#include "rtc_base/arraysize.h"
#include "rtc_base/logging.h"
#include "rtc_base/platform_thread_types.h"
@ -280,7 +281,7 @@ bool IsDeviceActive(IMMDevice* device) {
// Retrieve an audio device specified by `device_id` or a default device
// specified by data-flow direction and role if `device_id` is default.
ComPtr<IMMDevice> CreateDeviceInternal(const std::string& device_id,
ComPtr<IMMDevice> CreateDeviceInternal(absl::string_view device_id,
EDataFlow data_flow,
ERole role) {
RTC_DLOG(LS_INFO) << "CreateDeviceInternal: "
@ -745,7 +746,7 @@ std::string GetCommunicationsOutputDeviceID() {
return device.Get() ? GetDeviceIdInternal(device.Get()) : std::string();
}
ComPtr<IMMDevice> CreateDevice(const std::string& device_id,
ComPtr<IMMDevice> CreateDevice(absl::string_view device_id,
EDataFlow data_flow,
ERole role) {
RTC_DLOG(LS_INFO) << "CreateDevice";
@ -762,7 +763,7 @@ AudioDeviceName GetDeviceName(IMMDevice* device) {
return device_name;
}
std::string GetFriendlyName(const std::string& device_id,
std::string GetFriendlyName(absl::string_view device_id,
EDataFlow data_flow,
ERole role) {
RTC_DLOG(LS_INFO) << "GetFriendlyName";
@ -871,7 +872,7 @@ int NumberOfActiveSessions(IMMDevice* device) {
return num_active;
}
ComPtr<IAudioClient> CreateClient(const std::string& device_id,
ComPtr<IAudioClient> CreateClient(absl::string_view device_id,
EDataFlow data_flow,
ERole role) {
RTC_DLOG(LS_INFO) << "CreateClient";
@ -879,7 +880,7 @@ ComPtr<IAudioClient> CreateClient(const std::string& device_id,
return CreateClientInternal(device.Get());
}
ComPtr<IAudioClient2> CreateClient2(const std::string& device_id,
ComPtr<IAudioClient2> CreateClient2(absl::string_view device_id,
EDataFlow data_flow,
ERole role) {
RTC_DLOG(LS_INFO) << "CreateClient2";
@ -887,7 +888,7 @@ ComPtr<IAudioClient2> CreateClient2(const std::string& device_id,
return CreateClient2Internal(device.Get());
}
ComPtr<IAudioClient3> CreateClient3(const std::string& device_id,
ComPtr<IAudioClient3> CreateClient3(absl::string_view device_id,
EDataFlow data_flow,
ERole role) {
RTC_DLOG(LS_INFO) << "CreateClient3";

View File

@ -22,6 +22,7 @@
#include <string>
#include "absl/strings/string_view.h"
#include "api/units/time_delta.h"
#include "modules/audio_device/audio_device_name.h"
#include "modules/audio_device/include/audio_device_defines.h"
@ -329,7 +330,7 @@ std::string GetCommunicationsOutputDeviceID();
// Creates an IMMDevice interface corresponding to the unique device id in
// `device_id`, or by data-flow direction and role if `device_id` is set to
// AudioDeviceName::kDefaultDeviceId.
Microsoft::WRL::ComPtr<IMMDevice> CreateDevice(const std::string& device_id,
Microsoft::WRL::ComPtr<IMMDevice> CreateDevice(absl::string_view device_id,
EDataFlow data_flow,
ERole role);
@ -341,7 +342,7 @@ webrtc::AudioDeviceName GetDeviceName(IMMDevice* device);
// Gets the user-friendly name of the endpoint device which is represented
// by a unique id in `device_id`, or by data-flow direction and role if
// `device_id` is set to AudioDeviceName::kDefaultDeviceId.
std::string GetFriendlyName(const std::string& device_id,
std::string GetFriendlyName(absl::string_view device_id,
EDataFlow data_flow,
ERole role);
@ -378,13 +379,15 @@ int NumberOfActiveSessions(IMMDevice* device);
// Creates an IAudioClient instance for a specific device or the default
// device specified by data-flow direction and role.
Microsoft::WRL::ComPtr<IAudioClient> CreateClient(const std::string& device_id,
Microsoft::WRL::ComPtr<IAudioClient> CreateClient(absl::string_view device_id,
EDataFlow data_flow,
ERole role);
Microsoft::WRL::ComPtr<IAudioClient2>
CreateClient2(const std::string& device_id, EDataFlow data_flow, ERole role);
Microsoft::WRL::ComPtr<IAudioClient3>
CreateClient3(const std::string& device_id, EDataFlow data_flow, ERole role);
Microsoft::WRL::ComPtr<IAudioClient2> CreateClient2(absl::string_view device_id,
EDataFlow data_flow,
ERole role);
Microsoft::WRL::ComPtr<IAudioClient3> CreateClient3(absl::string_view device_id,
EDataFlow data_flow,
ERole role);
// Sets the AudioCategory_Communications category. Should be called before
// GetSharedModeMixFormat() and IsFormatSupported(). The `client` argument must