This source file includes following definitions.
- Start
- Stop
- IsStopped
- synchronized_buffers_
- Start
- Stop
- ThreadMain
- Run
- total_segments_
- InitializeOnAudioThread
#include "media/audio/audio_device_thread.h"
#include <algorithm>
#include "base/bind.h"
#include "base/logging.h"
#include "base/memory/aligned_memory.h"
#include "base/message_loop/message_loop.h"
#include "base/threading/platform_thread.h"
#include "base/threading/thread_restrictions.h"
#include "media/base/audio_bus.h"
using base::PlatformThread;
namespace media {
class AudioDeviceThread::Thread
: public PlatformThread::Delegate,
public base::RefCountedThreadSafe<AudioDeviceThread::Thread> {
public:
Thread(AudioDeviceThread::Callback* callback,
base::SyncSocket::Handle socket,
const char* thread_name,
bool synchronized_buffers);
void Start();
void Stop(base::MessageLoop* loop_for_join);
private:
friend class base::RefCountedThreadSafe<AudioDeviceThread::Thread>;
virtual ~Thread();
virtual void ThreadMain() OVERRIDE;
void Run();
private:
base::PlatformThreadHandle thread_;
AudioDeviceThread::Callback* callback_;
base::CancelableSyncSocket socket_;
base::Lock callback_lock_;
const char* thread_name_;
const bool synchronized_buffers_;
DISALLOW_COPY_AND_ASSIGN(Thread);
};
AudioDeviceThread::AudioDeviceThread() {
}
AudioDeviceThread::~AudioDeviceThread() { DCHECK(!thread_.get()); }
void AudioDeviceThread::Start(AudioDeviceThread::Callback* callback,
base::SyncSocket::Handle socket,
const char* thread_name,
bool synchronized_buffers) {
base::AutoLock auto_lock(thread_lock_);
CHECK(!thread_);
thread_ = new AudioDeviceThread::Thread(
callback, socket, thread_name, synchronized_buffers);
thread_->Start();
}
void AudioDeviceThread::Stop(base::MessageLoop* loop_for_join) {
base::AutoLock auto_lock(thread_lock_);
if (thread_.get()) {
thread_->Stop(loop_for_join);
thread_ = NULL;
}
}
bool AudioDeviceThread::IsStopped() {
base::AutoLock auto_lock(thread_lock_);
return !thread_;
}
AudioDeviceThread::Thread::Thread(AudioDeviceThread::Callback* callback,
base::SyncSocket::Handle socket,
const char* thread_name,
bool synchronized_buffers)
: thread_(),
callback_(callback),
socket_(socket),
thread_name_(thread_name),
synchronized_buffers_(synchronized_buffers) {
}
AudioDeviceThread::Thread::~Thread() {
DCHECK(thread_.is_null());
}
void AudioDeviceThread::Thread::Start() {
base::AutoLock auto_lock(callback_lock_);
DCHECK(thread_.is_null());
AddRef();
PlatformThread::CreateWithPriority(0, this, &thread_,
base::kThreadPriority_RealtimeAudio);
CHECK(!thread_.is_null());
}
void AudioDeviceThread::Thread::Stop(base::MessageLoop* loop_for_join) {
socket_.Shutdown();
base::PlatformThreadHandle thread = base::PlatformThreadHandle();
{
base::AutoLock auto_lock(callback_lock_);
callback_ = NULL;
std::swap(thread, thread_);
}
if (!thread.is_null()) {
if (loop_for_join) {
loop_for_join->PostTask(FROM_HERE,
base::Bind(&base::PlatformThread::Join, thread));
} else {
base::PlatformThread::Join(thread);
}
}
}
void AudioDeviceThread::Thread::ThreadMain() {
PlatformThread::SetName(thread_name_);
base::ThreadRestrictions::SetSingletonAllowed(true);
{
base::AutoLock auto_lock(callback_lock_);
if (callback_)
callback_->InitializeOnAudioThread();
}
Run();
Release();
}
void AudioDeviceThread::Thread::Run() {
uint32 buffer_index = 0;
while (true) {
int pending_data = 0;
size_t bytes_read = socket_.Receive(&pending_data, sizeof(pending_data));
if (bytes_read != sizeof(pending_data)) {
DCHECK_EQ(bytes_read, 0U);
break;
}
{
base::AutoLock auto_lock(callback_lock_);
if (callback_)
callback_->Process(pending_data);
}
if (synchronized_buffers_) {
++buffer_index;
size_t bytes_sent = socket_.Send(&buffer_index, sizeof(buffer_index));
if (bytes_sent != sizeof(buffer_index))
break;
}
}
}
AudioDeviceThread::Callback::Callback(
const AudioParameters& audio_parameters,
base::SharedMemoryHandle memory,
int memory_length,
int total_segments)
: audio_parameters_(audio_parameters),
samples_per_ms_(audio_parameters.sample_rate() / 1000),
bytes_per_ms_(audio_parameters.channels() *
(audio_parameters_.bits_per_sample() / 8) *
samples_per_ms_),
shared_memory_(memory, false),
memory_length_(memory_length),
total_segments_(total_segments) {
CHECK_NE(bytes_per_ms_, 0);
CHECK_NE(samples_per_ms_, 0);
CHECK_GT(total_segments_, 0);
CHECK_EQ(memory_length_ % total_segments_, 0);
segment_length_ = memory_length_ / total_segments_;
}
AudioDeviceThread::Callback::~Callback() {}
void AudioDeviceThread::Callback::InitializeOnAudioThread() {
MapSharedMemory();
CHECK(shared_memory_.memory());
}
}