gecko/dom/ipc/AudioParent.cpp
Chris Pearce 9abb830db0 Bug 811381 - Remove ns prefix from media code. r=roc
--HG--
rename : content/media/nsAudioAvailableEventManager.cpp => content/media/AudioAvailableEventManager.cpp
rename : content/media/nsAudioAvailableEventManager.h => content/media/AudioAvailableEventManager.h
rename : content/media/nsAudioStream.cpp => content/media/AudioStream.cpp
rename : content/media/nsAudioStream.h => content/media/AudioStream.h
rename : content/media/nsMediaCache.cpp => content/media/MediaCache.cpp
rename : content/media/nsMediaCache.h => content/media/MediaCache.h
rename : content/media/nsBuiltinDecoder.cpp => content/media/MediaDecoder.cpp
rename : content/media/nsBuiltinDecoder.h => content/media/MediaDecoder.h
rename : content/media/nsBuiltinDecoderReader.cpp => content/media/MediaDecoderReader.cpp
rename : content/media/nsBuiltinDecoderReader.h => content/media/MediaDecoderReader.h
rename : content/media/nsBuiltinDecoderStateMachine.cpp => content/media/MediaDecoderStateMachine.cpp
rename : content/media/nsBuiltinDecoderStateMachine.h => content/media/MediaDecoderStateMachine.h
rename : content/media/dash/nsDASHDecoder.cpp => content/media/dash/DASHDecoder.cpp
rename : content/media/dash/nsDASHDecoder.h => content/media/dash/DASHDecoder.h
rename : content/media/dash/nsDASHReader.cpp => content/media/dash/DASHReader.cpp
rename : content/media/dash/nsDASHReader.h => content/media/dash/DASHReader.h
rename : content/media/dash/nsDASHRepDecoder.cpp => content/media/dash/DASHRepDecoder.cpp
rename : content/media/dash/nsDASHRepDecoder.h => content/media/dash/DASHRepDecoder.h
rename : content/media/gstreamer/nsGStreamerDecoder.cpp => content/media/gstreamer/GStreamerDecoder.cpp
rename : content/media/gstreamer/nsGStreamerDecoder.h => content/media/gstreamer/GStreamerDecoder.h
rename : content/media/gstreamer/nsGStreamerReader.cpp => content/media/gstreamer/GStreamerReader.cpp
rename : content/media/gstreamer/nsGStreamerReader.h => content/media/gstreamer/GStreamerReader.h
rename : content/media/ogg/nsOggCodecState.cpp => content/media/ogg/OggCodecState.cpp
rename : content/media/ogg/nsOggCodecState.h => content/media/ogg/OggCodecState.h
rename : content/media/ogg/nsOggDecoder.cpp => content/media/ogg/OggDecoder.cpp
rename : content/media/ogg/nsOggDecoder.h => content/media/ogg/OggDecoder.h
rename : content/media/ogg/nsOggReader.cpp => content/media/ogg/OggReader.cpp
rename : content/media/ogg/nsOggReader.h => content/media/ogg/OggReader.h
rename : content/media/omx/nsMediaOmxDecoder.cpp => content/media/omx/MediaOmxDecoder.cpp
rename : content/media/omx/nsMediaOmxDecoder.h => content/media/omx/MediaOmxDecoder.h
rename : content/media/omx/nsMediaOmxReader.cpp => content/media/omx/MediaOmxReader.cpp
rename : content/media/omx/nsMediaOmxReader.h => content/media/omx/MediaOmxReader.h
rename : content/media/plugins/nsMediaPluginDecoder.cpp => content/media/plugins/MediaPluginDecoder.cpp
rename : content/media/plugins/nsMediaPluginDecoder.h => content/media/plugins/MediaPluginDecoder.h
rename : content/media/plugins/nsMediaPluginHost.cpp => content/media/plugins/MediaPluginHost.cpp
rename : content/media/plugins/nsMediaPluginHost.h => content/media/plugins/MediaPluginHost.h
rename : content/media/plugins/nsMediaPluginReader.cpp => content/media/plugins/MediaPluginReader.cpp
rename : content/media/plugins/nsMediaPluginReader.h => content/media/plugins/MediaPluginReader.h
rename : content/media/raw/nsRawDecoder.cpp => content/media/raw/RawDecoder.cpp
rename : content/media/raw/nsRawDecoder.h => content/media/raw/RawDecoder.h
rename : content/media/raw/nsRawReader.cpp => content/media/raw/RawReader.cpp
rename : content/media/raw/nsRawReader.h => content/media/raw/RawReader.h
rename : content/media/raw/nsRawStructs.h => content/media/raw/RawStructs.h
rename : content/media/wave/nsWaveDecoder.cpp => content/media/wave/WaveDecoder.cpp
rename : content/media/wave/nsWaveDecoder.h => content/media/wave/WaveDecoder.h
rename : content/media/wave/nsWaveReader.cpp => content/media/wave/WaveReader.cpp
rename : content/media/wave/nsWaveReader.h => content/media/wave/WaveReader.h
rename : content/media/webm/nsWebMBufferedParser.cpp => content/media/webm/WebMBufferedParser.cpp
rename : content/media/webm/nsWebMBufferedParser.h => content/media/webm/WebMBufferedParser.h
rename : content/media/webm/nsWebMDecoder.cpp => content/media/webm/WebMDecoder.cpp
rename : content/media/webm/nsWebMDecoder.h => content/media/webm/WebMDecoder.h
rename : content/media/webm/nsWebMReader.cpp => content/media/webm/WebMReader.cpp
rename : content/media/webm/nsWebMReader.h => content/media/webm/WebMReader.h
2012-11-14 11:46:40 -08:00

340 lines
6.6 KiB
C++

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set sw=2 ts=8 et tw=80 : */
/* 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 "mozilla/dom/AudioParent.h"
#include "mozilla/unused.h"
#include "nsThreadUtils.h"
// C++ file contents
namespace mozilla {
namespace dom {
class AudioWriteDoneEvent : public nsRunnable
{
public:
AudioWriteDoneEvent(AudioParent* owner)
{
mOwner = owner;
}
NS_IMETHOD Run()
{
mOwner->SendWriteDone();
return NS_OK;
}
private:
nsRefPtr<AudioParent> mOwner;
};
class AudioWriteEvent : public nsRunnable
{
public:
AudioWriteEvent(AudioParent* parent, AudioStream* owner, nsCString data, uint32_t frames)
{
mParent = parent;
mOwner = owner;
mData = data;
mFrames = frames;
}
NS_IMETHOD Run()
{
mOwner->Write(reinterpret_cast<const AudioDataValue*>(mData.get()), mFrames);
nsCOMPtr<nsIRunnable> event = new AudioWriteDoneEvent(mParent);
NS_DispatchToMainThread(event);
return NS_OK;
}
private:
nsRefPtr<AudioParent> mParent;
nsRefPtr<AudioStream> mOwner;
nsCString mData;
uint32_t mFrames;
};
class AudioPauseEvent : public nsRunnable
{
public:
AudioPauseEvent(AudioStream* owner, bool aPause)
{
mOwner = owner;
mPause = aPause;
}
NS_IMETHOD Run()
{
if (mPause)
mOwner->Pause();
else
mOwner->Resume();
return NS_OK;
}
private:
nsRefPtr<AudioStream> mOwner;
bool mPause;
};
class AudioStreamShutdownEvent : public nsRunnable
{
public:
AudioStreamShutdownEvent(AudioStream* owner)
{
mOwner = owner;
}
NS_IMETHOD Run()
{
mOwner->Shutdown();
return NS_OK;
}
private:
nsRefPtr<AudioStream> mOwner;
};
class AudioMinWriteSizeDone : public nsRunnable
{
public:
AudioMinWriteSizeDone(AudioParent* owner, int32_t minFrames)
{
mOwner = owner;
mMinFrames = minFrames;
}
NS_IMETHOD Run()
{
mOwner->SendMinWriteSizeDone(mMinFrames);
return NS_OK;
}
private:
nsRefPtr<AudioParent> mOwner;
int32_t mMinFrames;
};
class AudioMinWriteSizeEvent : public nsRunnable
{
public:
AudioMinWriteSizeEvent(AudioParent* parent, AudioStream* owner)
{
mParent = parent;
mOwner = owner;
}
NS_IMETHOD Run()
{
int32_t minFrames = mOwner->GetMinWriteSize();
nsCOMPtr<nsIRunnable> event = new AudioMinWriteSizeDone(mParent, minFrames);
NS_DispatchToMainThread(event);
return NS_OK;
}
private:
nsRefPtr<AudioStream> mOwner;
nsRefPtr<AudioParent> mParent;
};
class AudioDrainDoneEvent : public nsRunnable
{
public:
AudioDrainDoneEvent(AudioParent* owner)
{
mOwner = owner;
}
NS_IMETHOD Run()
{
mOwner->SendDrainDone();
return NS_OK;
}
private:
nsRefPtr<AudioParent> mOwner;
};
class AudioDrainEvent : public nsRunnable
{
public:
AudioDrainEvent(AudioParent* parent, AudioStream* owner)
{
mParent = parent;
mOwner = owner;
}
NS_IMETHOD Run()
{
mOwner->Drain();
nsCOMPtr<nsIRunnable> event = new AudioDrainDoneEvent(mParent);
NS_DispatchToMainThread(event);
return NS_OK;
}
private:
nsRefPtr<AudioStream> mOwner;
nsRefPtr<AudioParent> mParent;
};
NS_IMPL_THREADSAFE_ISUPPORTS1(AudioParent, nsITimerCallback)
nsresult
AudioParent::Notify(nsITimer* timer)
{
if (!mIPCOpen) {
timer->Cancel();
return NS_ERROR_FAILURE;
}
NS_ASSERTION(mStream, "AudioStream not initialized.");
int64_t position = mStream->GetPositionInFrames();
unused << SendPositionInFramesUpdate(position, PR_IntervalNow());
return NS_OK;
}
bool
AudioParent::RecvWrite(const nsCString& data, const uint32_t& frames)
{
if (!mStream)
return false;
nsCOMPtr<nsIRunnable> event = new AudioWriteEvent(this, mStream, data, frames);
nsCOMPtr<nsIThread> thread = mStream->GetThread();
thread->Dispatch(event, nsIEventTarget::DISPATCH_NORMAL);
return true;
}
bool
AudioParent::RecvSetVolume(const float& aVolume)
{
if (!mStream)
return false;
mStream->SetVolume(aVolume);
return true;
}
bool
AudioParent::RecvMinWriteSize()
{
if (!mStream)
return false;
nsCOMPtr<nsIRunnable> event = new AudioMinWriteSizeEvent(this, mStream);
nsCOMPtr<nsIThread> thread = mStream->GetThread();
thread->Dispatch(event, nsIEventTarget::DISPATCH_NORMAL);
return true;
}
bool
AudioParent::RecvDrain()
{
if (!mStream)
return false;
nsCOMPtr<nsIRunnable> event = new AudioDrainEvent(this, mStream);
nsCOMPtr<nsIThread> thread = mStream->GetThread();
thread->Dispatch(event, nsIEventTarget::DISPATCH_NORMAL);
return true;
}
bool
AudioParent::RecvPause()
{
if (!mStream)
return false;
nsCOMPtr<nsIRunnable> event = new AudioPauseEvent(mStream, true);
nsCOMPtr<nsIThread> thread = mStream->GetThread();
thread->Dispatch(event, nsIEventTarget::DISPATCH_NORMAL);
return true;
}
bool
AudioParent::RecvResume()
{
if (!mStream)
return false;
nsCOMPtr<nsIRunnable> event = new AudioPauseEvent(mStream, false);
nsCOMPtr<nsIThread> thread = mStream->GetThread();
thread->Dispatch(event, nsIEventTarget::DISPATCH_NORMAL);
return true;
}
bool
AudioParent::RecvShutdown()
{
Shutdown();
unused << PAudioParent::Send__delete__(this);
return true;
}
bool
AudioParent::SendMinWriteSizeDone(int32_t minFrames)
{
if (mIPCOpen)
return PAudioParent::SendMinWriteSizeDone(minFrames);
return true;
}
bool
AudioParent::SendDrainDone()
{
if (mIPCOpen)
return PAudioParent::SendDrainDone();
return true;
}
bool
AudioParent::SendWriteDone()
{
if (mIPCOpen)
return PAudioParent::SendWriteDone();
return true;
}
AudioParent::AudioParent(int32_t aNumChannels, int32_t aRate)
: mIPCOpen(true)
{
mStream = AudioStream::AllocateStream();
NS_ASSERTION(mStream, "AudioStream allocation failed.");
if (NS_FAILED(mStream->Init(aNumChannels, aRate))) {
NS_WARNING("AudioStream initialization failed.");
mStream = nullptr;
return;
}
mTimer = do_CreateInstance("@mozilla.org/timer;1");
mTimer->InitWithCallback(this, 1000, nsITimer::TYPE_REPEATING_SLACK);
}
AudioParent::~AudioParent()
{
}
void
AudioParent::ActorDestroy(ActorDestroyReason aWhy)
{
mIPCOpen = false;
Shutdown();
}
void
AudioParent::Shutdown()
{
if (mTimer) {
mTimer->Cancel();
mTimer = nullptr;
}
if (mStream) {
nsCOMPtr<nsIRunnable> event = new AudioStreamShutdownEvent(mStream);
nsCOMPtr<nsIThread> thread = mStream->GetThread();
thread->Dispatch(event, nsIEventTarget::DISPATCH_NORMAL);
mStream = nullptr;
}
}
} // namespace dom
} // namespace mozilla