add vad code.

This commit is contained in:
luocai
2024-09-06 18:26:45 +08:00
parent 35bf68338f
commit 2bed1dacf2
93 changed files with 12362 additions and 2 deletions

View File

@ -0,0 +1,86 @@
/*
* Copyright 2019 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include "rtc_base/synchronization/sequence_checker_internal.h"
#include <string>
#include "rtc_base/checks.h"
#include "rtc_base/strings/string_builder.h"
namespace webrtc {
namespace webrtc_sequence_checker_internal {
SequenceCheckerImpl::SequenceCheckerImpl(bool attach_to_current_thread)
: attached_(attach_to_current_thread),
valid_thread_(rtc::CurrentThreadRef()),
valid_queue_(TaskQueueBase::Current()) {}
SequenceCheckerImpl::SequenceCheckerImpl(TaskQueueBase* attached_queue)
: attached_(attached_queue != nullptr),
valid_thread_(rtc::PlatformThreadRef()),
valid_queue_(attached_queue) {}
bool SequenceCheckerImpl::IsCurrent() const {
const TaskQueueBase* const current_queue = TaskQueueBase::Current();
const rtc::PlatformThreadRef current_thread = rtc::CurrentThreadRef();
MutexLock scoped_lock(&lock_);
if (!attached_) { // Previously detached.
attached_ = true;
valid_thread_ = current_thread;
valid_queue_ = current_queue;
return true;
}
if (valid_queue_) {
return valid_queue_ == current_queue;
}
return rtc::IsThreadRefEqual(valid_thread_, current_thread);
}
void SequenceCheckerImpl::Detach() {
MutexLock scoped_lock(&lock_);
attached_ = false;
// We don't need to touch the other members here, they will be
// reset on the next call to IsCurrent().
}
#if RTC_DCHECK_IS_ON
std::string SequenceCheckerImpl::ExpectationToString() const {
const TaskQueueBase* const current_queue = TaskQueueBase::Current();
const rtc::PlatformThreadRef current_thread = rtc::CurrentThreadRef();
MutexLock scoped_lock(&lock_);
if (!attached_)
return "Checker currently not attached.";
// The format of the string is meant to compliment the one we have inside of
// FatalLog() (checks.cc). Example:
//
// # Expected: TQ: 0x0 SysQ: 0x7fff69541330 Thread: 0x11dcf6dc0
// # Actual: TQ: 0x7fa8f0604190 SysQ: 0x7fa8f0604a30 Thread: 0x700006f1a000
// TaskQueue doesn't match
rtc::StringBuilder message;
message.AppendFormat(
"# Expected: TQ: %p Thread: %p\n"
"# Actual: TQ: %p Thread: %p\n",
valid_queue_, reinterpret_cast<const void*>(valid_thread_), current_queue,
reinterpret_cast<const void*>(current_thread));
if ((valid_queue_ || current_queue) && valid_queue_ != current_queue) {
message << "TaskQueue doesn't match\n";
} else if (!rtc::IsThreadRefEqual(valid_thread_, current_thread)) {
message << "Threads don't match\n";
}
return message.Release();
}
#endif // RTC_DCHECK_IS_ON
} // namespace webrtc_sequence_checker_internal
} // namespace webrtc

View File

@ -0,0 +1,90 @@
/*
* Copyright 2020 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#ifndef RTC_BASE_SYNCHRONIZATION_SEQUENCE_CHECKER_INTERNAL_H_
#define RTC_BASE_SYNCHRONIZATION_SEQUENCE_CHECKER_INTERNAL_H_
#include <string>
#include <type_traits>
#include "api/task_queue/task_queue_base.h"
#include "rtc_base/platform_thread_types.h"
#include "rtc_base/synchronization/mutex.h"
#include "rtc_base/system/rtc_export.h"
#include "rtc_base/thread_annotations.h"
namespace webrtc {
namespace webrtc_sequence_checker_internal {
// Real implementation of SequenceChecker, for use in debug mode, or
// for temporary use in release mode (e.g. to RTC_CHECK on a threading issue
// seen only in the wild).
//
// Note: You should almost always use the SequenceChecker class to get the
// right version for your build configuration.
class RTC_EXPORT SequenceCheckerImpl {
public:
explicit SequenceCheckerImpl(bool attach_to_current_thread);
explicit SequenceCheckerImpl(TaskQueueBase* attached_queue);
~SequenceCheckerImpl() = default;
bool IsCurrent() const;
// Changes the task queue or thread that is checked for in IsCurrent. This can
// be useful when an object may be created on one task queue / thread and then
// used exclusively on another thread.
void Detach();
// Returns a string that is formatted to match with the error string printed
// by RTC_CHECK() when a condition is not met.
// This is used in conjunction with the RTC_DCHECK_RUN_ON() macro.
std::string ExpectationToString() const;
private:
mutable Mutex lock_;
// These are mutable so that IsCurrent can set them.
mutable bool attached_ RTC_GUARDED_BY(lock_);
mutable rtc::PlatformThreadRef valid_thread_ RTC_GUARDED_BY(lock_);
mutable const TaskQueueBase* valid_queue_ RTC_GUARDED_BY(lock_);
};
// Do nothing implementation, for use in release mode.
//
// Note: You should almost always use the SequenceChecker class to get the
// right version for your build configuration.
class SequenceCheckerDoNothing {
public:
explicit SequenceCheckerDoNothing(bool attach_to_current_thread) {}
explicit SequenceCheckerDoNothing(TaskQueueBase* attached_queue) {}
bool IsCurrent() const { return true; }
void Detach() {}
};
template <typename ThreadLikeObject>
std::enable_if_t<std::is_base_of_v<SequenceCheckerImpl, ThreadLikeObject>,
std::string>
ExpectationToString(const ThreadLikeObject* checker) {
#if RTC_DCHECK_IS_ON
return checker->ExpectationToString();
#else
return std::string();
#endif
}
// Catch-all implementation for types other than explicitly supported above.
template <typename ThreadLikeObject>
std::enable_if_t<!std::is_base_of_v<SequenceCheckerImpl, ThreadLikeObject>,
std::string>
ExpectationToString(const ThreadLikeObject*) {
return std::string();
}
} // namespace webrtc_sequence_checker_internal
} // namespace webrtc
#endif // RTC_BASE_SYNCHRONIZATION_SEQUENCE_CHECKER_INTERNAL_H_

View File

@ -0,0 +1,82 @@
/*
* Copyright 2019 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include "rtc_base/synchronization/yield_policy.h"
#include "absl/base/attributes.h"
#include "absl/base/config.h"
#include "rtc_base/checks.h"
#if !defined(ABSL_HAVE_THREAD_LOCAL) && defined(WEBRTC_POSIX)
#include <pthread.h>
#endif
namespace rtc {
namespace {
#if defined(ABSL_HAVE_THREAD_LOCAL)
ABSL_CONST_INIT thread_local YieldInterface* current_yield_policy = nullptr;
YieldInterface* GetCurrentYieldPolicy() {
return current_yield_policy;
}
void SetCurrentYieldPolicy(YieldInterface* ptr) {
current_yield_policy = ptr;
}
#elif defined(WEBRTC_POSIX)
// Emscripten does not support the C++11 thread_local keyword but does support
// the pthread thread-local storage API.
// https://github.com/emscripten-core/emscripten/issues/3502
ABSL_CONST_INIT pthread_key_t g_current_yield_policy_tls = 0;
void InitializeTls() {
RTC_CHECK_EQ(pthread_key_create(&g_current_yield_policy_tls, nullptr), 0);
}
pthread_key_t GetCurrentYieldPolicyTls() {
static pthread_once_t init_once = PTHREAD_ONCE_INIT;
RTC_CHECK_EQ(pthread_once(&init_once, &InitializeTls), 0);
return g_current_yield_policy_tls;
}
YieldInterface* GetCurrentYieldPolicy() {
return static_cast<YieldInterface*>(
pthread_getspecific(GetCurrentYieldPolicyTls()));
}
void SetCurrentYieldPolicy(YieldInterface* ptr) {
pthread_setspecific(GetCurrentYieldPolicyTls(), ptr);
}
#else
#error Unsupported platform
#endif
} // namespace
ScopedYieldPolicy::ScopedYieldPolicy(YieldInterface* policy)
: previous_(GetCurrentYieldPolicy()) {
SetCurrentYieldPolicy(policy);
}
ScopedYieldPolicy::~ScopedYieldPolicy() {
SetCurrentYieldPolicy(previous_);
}
void ScopedYieldPolicy::YieldExecution() {
YieldInterface* current = GetCurrentYieldPolicy();
if (current)
current->YieldExecution();
}
} // namespace rtc

View File

@ -0,0 +1,38 @@
/*
* Copyright 2019 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#ifndef RTC_BASE_SYNCHRONIZATION_YIELD_POLICY_H_
#define RTC_BASE_SYNCHRONIZATION_YIELD_POLICY_H_
namespace rtc {
class YieldInterface {
public:
virtual ~YieldInterface() = default;
virtual void YieldExecution() = 0;
};
// Sets the current thread-local yield policy while it's in scope and reverts
// to the previous policy when it leaves the scope.
class ScopedYieldPolicy final {
public:
explicit ScopedYieldPolicy(YieldInterface* policy);
ScopedYieldPolicy(const ScopedYieldPolicy&) = delete;
ScopedYieldPolicy& operator=(const ScopedYieldPolicy&) = delete;
~ScopedYieldPolicy();
// Will yield as specified by the currently active thread-local yield policy
// (which by default is a no-op).
static void YieldExecution();
private:
YieldInterface* const previous_;
};
} // namespace rtc
#endif // RTC_BASE_SYNCHRONIZATION_YIELD_POLICY_H_