xref: /aosp_15_r20/external/webrtc/modules/audio_device/dummy/file_audio_device_factory.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #include "modules/audio_device/dummy/file_audio_device_factory.h"
12 
13 #include <stdio.h>
14 
15 #include <cstdlib>
16 
17 #include "absl/strings/string_view.h"
18 #include "modules/audio_device/dummy/file_audio_device.h"
19 #include "rtc_base/logging.h"
20 #include "rtc_base/string_utils.h"
21 
22 namespace webrtc {
23 
24 bool FileAudioDeviceFactory::_isConfigured = false;
25 char FileAudioDeviceFactory::_inputAudioFilename[MAX_FILENAME_LEN] = "";
26 char FileAudioDeviceFactory::_outputAudioFilename[MAX_FILENAME_LEN] = "";
27 
CreateFileAudioDevice()28 FileAudioDevice* FileAudioDeviceFactory::CreateFileAudioDevice() {
29   // Bail out here if the files haven't been set explicitly.
30   // audio_device_impl.cc should then fall back to dummy audio.
31   if (!_isConfigured) {
32     RTC_LOG(LS_WARNING)
33         << "WebRTC configured with WEBRTC_DUMMY_FILE_DEVICES but "
34            "no device files supplied. Will fall back to dummy "
35            "audio.";
36 
37     return nullptr;
38   }
39   return new FileAudioDevice(_inputAudioFilename, _outputAudioFilename);
40 }
41 
SetFilenamesToUse(absl::string_view inputAudioFilename,absl::string_view outputAudioFilename)42 void FileAudioDeviceFactory::SetFilenamesToUse(
43     absl::string_view inputAudioFilename,
44     absl::string_view outputAudioFilename) {
45 #ifdef WEBRTC_DUMMY_FILE_DEVICES
46   RTC_DCHECK_LT(inputAudioFilename.size(), MAX_FILENAME_LEN);
47   RTC_DCHECK_LT(outputAudioFilename.size(), MAX_FILENAME_LEN);
48 
49   // Copy the strings since we don't know the lifetime of the input pointers.
50   rtc::strcpyn(_inputAudioFilename, MAX_FILENAME_LEN, inputAudioFilename);
51   rtc::strcpyn(_outputAudioFilename, MAX_FILENAME_LEN, outputAudioFilename);
52   _isConfigured = true;
53 #else
54   // Sanity: must be compiled with the right define to run this.
55   printf(
56       "Trying to use dummy file devices, but is not compiled "
57       "with WEBRTC_DUMMY_FILE_DEVICES. Bailing out.\n");
58   std::exit(1);
59 #endif
60 }
61 
62 }  // namespace webrtc
63