2012-07-12 04:53:08 -07:00
|
|
|
/* 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 "MediaEngineWebRTC.h"
|
|
|
|
|
|
|
|
#define CHANNELS 1
|
|
|
|
#define ENCODING "L16"
|
|
|
|
#define DEFAULT_PORT 5555
|
|
|
|
|
|
|
|
#define SAMPLE_RATE 256000
|
|
|
|
#define SAMPLE_FREQUENCY 16000
|
|
|
|
#define SAMPLE_LENGTH ((SAMPLE_FREQUENCY*10)/1000)
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
2012-10-15 13:41:46 -07:00
|
|
|
#ifdef PR_LOGGING
|
2012-10-29 16:32:10 -07:00
|
|
|
extern PRLogModuleInfo* GetMediaManagerLog();
|
|
|
|
#define LOG(msg) PR_LOG(GetMediaManagerLog(), PR_LOG_DEBUG, msg)
|
2012-10-15 13:41:46 -07:00
|
|
|
#else
|
|
|
|
#define LOG(msg)
|
|
|
|
#endif
|
|
|
|
|
2012-07-12 04:53:08 -07:00
|
|
|
/**
|
|
|
|
* Webrtc audio source.
|
|
|
|
*/
|
|
|
|
NS_IMPL_THREADSAFE_ISUPPORTS0(MediaEngineWebRTCAudioSource)
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaEngineWebRTCAudioSource::GetName(nsAString& aName)
|
|
|
|
{
|
|
|
|
if (mInitDone) {
|
|
|
|
aName.Assign(mDeviceName);
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaEngineWebRTCAudioSource::GetUUID(nsAString& aUUID)
|
|
|
|
{
|
|
|
|
if (mInitDone) {
|
|
|
|
aUUID.Assign(mDeviceUUID);
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-01-29 08:55:09 -08:00
|
|
|
nsresult
|
|
|
|
MediaEngineWebRTCAudioSource::Config(bool aEchoOn, uint32_t aEcho,
|
|
|
|
bool aAgcOn, uint32_t aAGC,
|
|
|
|
bool aNoiseOn, uint32_t aNoise)
|
|
|
|
{
|
|
|
|
LOG(("Audio config: aec: %d, agc: %d, noise: %d",
|
|
|
|
aEchoOn ? aEcho : -1,
|
|
|
|
aAgcOn ? aAGC : -1,
|
|
|
|
aNoiseOn ? aNoise : -1));
|
|
|
|
|
|
|
|
bool update_agc = (mAgcOn == aAgcOn);
|
|
|
|
bool update_noise = (mNoiseOn == aNoiseOn);
|
|
|
|
mAgcOn = aAgcOn;
|
|
|
|
mNoiseOn = aNoiseOn;
|
|
|
|
|
|
|
|
if ((webrtc::AgcModes) aAGC != webrtc::kAgcUnchanged) {
|
|
|
|
if (mAGC != (webrtc::AgcModes) aAGC) {
|
|
|
|
update_agc = true;
|
|
|
|
mAGC = (webrtc::AgcModes) aAGC;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ((webrtc::NsModes) aNoise != webrtc::kNsUnchanged) {
|
|
|
|
if (mNoiseSuppress != (webrtc::NsModes) aNoise) {
|
|
|
|
update_noise = true;
|
|
|
|
mNoiseSuppress = (webrtc::NsModes) aNoise;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mInitDone) {
|
|
|
|
int error;
|
|
|
|
#if 0
|
|
|
|
// Until we can support feeding our full output audio from the browser
|
|
|
|
// through the MediaStream, this won't work. Or we need to move AEC to
|
|
|
|
// below audio input and output, perhaps invoked from here.
|
|
|
|
mEchoOn = aEchoOn;
|
|
|
|
if ((webrtc::EcModes) aEcho != webrtc::kEcUnchanged)
|
|
|
|
mEchoCancel = (webrtc::EcModes) aEcho;
|
|
|
|
mVoEProcessing->SetEcStatus(mEchoOn, aEcho);
|
|
|
|
#else
|
2013-02-22 03:59:29 -08:00
|
|
|
(void) aEcho; (void) aEchoOn; (void) mEchoCancel; // suppress warnings
|
2013-01-29 08:55:09 -08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
if (update_agc &&
|
|
|
|
0 != (error = mVoEProcessing->SetAgcStatus(mAgcOn, (webrtc::AgcModes) aAGC))) {
|
|
|
|
LOG(("%s Error setting AGC Status: %d ",__FUNCTION__, error));
|
|
|
|
}
|
|
|
|
if (update_noise &&
|
|
|
|
0 != (error = mVoEProcessing->SetNsStatus(mNoiseOn, (webrtc::NsModes) aNoise))) {
|
|
|
|
LOG(("%s Error setting NoiseSuppression Status: %d ",__FUNCTION__, error));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-07-12 04:53:08 -07:00
|
|
|
nsresult
|
2013-03-04 13:02:17 -08:00
|
|
|
MediaEngineWebRTCAudioSource::Allocate(const MediaEnginePrefs &aPrefs)
|
2012-07-12 04:53:08 -07:00
|
|
|
{
|
2012-12-31 15:12:12 -08:00
|
|
|
if (mState == kReleased && mInitDone) {
|
|
|
|
webrtc::VoEHardware* ptrVoEHw = webrtc::VoEHardware::GetInterface(mVoiceEngine);
|
|
|
|
int res = ptrVoEHw->SetRecordingDevice(mCapIndex);
|
|
|
|
ptrVoEHw->Release();
|
|
|
|
if (res) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
mState = kAllocated;
|
|
|
|
LOG(("Audio device %d allocated", mCapIndex));
|
|
|
|
} else if (mSources.IsEmpty()) {
|
|
|
|
LOG(("Audio device %d reallocated", mCapIndex));
|
|
|
|
} else {
|
|
|
|
LOG(("Audio device %d allocated shared", mCapIndex));
|
2012-11-15 15:23:39 -08:00
|
|
|
}
|
2012-07-12 04:53:08 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
MediaEngineWebRTCAudioSource::Deallocate()
|
|
|
|
{
|
2012-12-31 15:12:12 -08:00
|
|
|
if (mSources.IsEmpty()) {
|
|
|
|
if (mState != kStopped && mState != kAllocated) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2012-07-12 04:53:08 -07:00
|
|
|
|
2012-12-31 15:12:12 -08:00
|
|
|
mState = kReleased;
|
|
|
|
LOG(("Audio device %d deallocated", mCapIndex));
|
|
|
|
} else {
|
|
|
|
LOG(("Audio device %d deallocated but still in use", mCapIndex));
|
|
|
|
}
|
2012-07-12 04:53:08 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
MediaEngineWebRTCAudioSource::Start(SourceMediaStream* aStream, TrackID aID)
|
|
|
|
{
|
2012-12-31 15:12:12 -08:00
|
|
|
if (!mInitDone || !aStream) {
|
2012-07-12 04:53:08 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2013-01-10 08:52:53 -08:00
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter enter(mMonitor);
|
|
|
|
mSources.AppendElement(aStream);
|
|
|
|
}
|
2012-07-12 04:53:08 -07:00
|
|
|
|
|
|
|
AudioSegment* segment = new AudioSegment();
|
2012-12-31 15:12:12 -08:00
|
|
|
aStream->AddTrack(aID, SAMPLE_FREQUENCY, 0, segment);
|
|
|
|
aStream->AdvanceKnownTracksTime(STREAM_TIME_MAX);
|
2012-10-17 02:46:40 -07:00
|
|
|
LOG(("Initial audio"));
|
2012-07-12 04:53:08 -07:00
|
|
|
mTrackID = aID;
|
|
|
|
|
2012-12-31 15:12:12 -08:00
|
|
|
if (mState == kStarted) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
mState = kStarted;
|
|
|
|
|
2013-01-29 08:55:09 -08:00
|
|
|
// Configure audio processing in webrtc code
|
|
|
|
Config(mEchoOn, webrtc::kEcUnchanged,
|
|
|
|
mAgcOn, webrtc::kAgcUnchanged,
|
|
|
|
mNoiseOn, webrtc::kNsUnchanged);
|
|
|
|
|
2012-07-12 04:53:08 -07:00
|
|
|
if (mVoEBase->StartReceive(mChannel)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
if (mVoEBase->StartSend(mChannel)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attach external media processor, so this::Process will be called.
|
|
|
|
mVoERender->RegisterExternalMediaProcessing(mChannel, webrtc::kRecordingPerChannel, *this);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-12-31 15:12:12 -08:00
|
|
|
MediaEngineWebRTCAudioSource::Stop(SourceMediaStream *aSource, TrackID aID)
|
2012-07-12 04:53:08 -07:00
|
|
|
{
|
2012-12-31 15:12:12 -08:00
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter enter(mMonitor);
|
2013-01-10 08:52:53 -08:00
|
|
|
|
|
|
|
if (!mSources.RemoveElement(aSource)) {
|
|
|
|
// Already stopped - this is allowed
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
if (!mSources.IsEmpty()) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
if (mState != kStarted) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
if (!mVoEBase) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2012-12-31 15:12:12 -08:00
|
|
|
mState = kStopped;
|
|
|
|
aSource->EndTrack(aID);
|
|
|
|
}
|
|
|
|
|
2012-07-12 04:53:08 -07:00
|
|
|
mVoERender->DeRegisterExternalMediaProcessing(mChannel, webrtc::kRecordingPerChannel);
|
|
|
|
|
|
|
|
if (mVoEBase->StopSend(mChannel)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
if (mVoEBase->StopReceive(mChannel)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-10-17 02:46:40 -07:00
|
|
|
void
|
|
|
|
MediaEngineWebRTCAudioSource::NotifyPull(MediaStreamGraph* aGraph,
|
2012-12-31 15:12:12 -08:00
|
|
|
SourceMediaStream *aSource,
|
|
|
|
TrackID aID,
|
|
|
|
StreamTime aDesiredTime,
|
|
|
|
TrackTicks &aLastEndTime)
|
2012-10-17 02:46:40 -07:00
|
|
|
{
|
|
|
|
// Ignore - we push audio data
|
2012-10-24 16:21:15 -07:00
|
|
|
#ifdef DEBUG
|
|
|
|
TrackTicks target = TimeToTicksRoundUp(SAMPLE_FREQUENCY, aDesiredTime);
|
2012-12-31 15:12:12 -08:00
|
|
|
TrackTicks delta = target - aLastEndTime;
|
2012-11-30 00:08:17 -08:00
|
|
|
LOG(("Audio: NotifyPull: aDesiredTime %ld, target %ld, delta %ld",(int64_t) aDesiredTime, (int64_t) target, (int64_t) delta));
|
2012-12-31 15:12:12 -08:00
|
|
|
aLastEndTime = target;
|
2012-10-24 16:21:15 -07:00
|
|
|
#endif
|
2012-10-17 02:46:40 -07:00
|
|
|
}
|
|
|
|
|
2012-07-12 04:53:08 -07:00
|
|
|
nsresult
|
2012-08-22 08:56:38 -07:00
|
|
|
MediaEngineWebRTCAudioSource::Snapshot(uint32_t aDuration, nsIDOMFile** aFile)
|
2012-07-12 04:53:08 -07:00
|
|
|
{
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
2012-10-16 17:53:55 -07:00
|
|
|
void
|
|
|
|
MediaEngineWebRTCAudioSource::Init()
|
|
|
|
{
|
|
|
|
mVoEBase = webrtc::VoEBase::GetInterface(mVoiceEngine);
|
|
|
|
|
|
|
|
mVoEBase->Init();
|
|
|
|
|
|
|
|
mVoERender = webrtc::VoEExternalMedia::GetInterface(mVoiceEngine);
|
|
|
|
if (!mVoERender) {
|
|
|
|
return;
|
|
|
|
}
|
2012-11-15 14:58:40 -08:00
|
|
|
mVoENetwork = webrtc::VoENetwork::GetInterface(mVoiceEngine);
|
|
|
|
if (!mVoENetwork) {
|
|
|
|
return;
|
|
|
|
}
|
2012-10-16 17:53:55 -07:00
|
|
|
|
2013-01-29 08:55:09 -08:00
|
|
|
mVoEProcessing = webrtc::VoEAudioProcessing::GetInterface(mVoiceEngine);
|
|
|
|
if (!mVoEProcessing) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-10-16 17:53:55 -07:00
|
|
|
mChannel = mVoEBase->CreateChannel();
|
|
|
|
if (mChannel < 0) {
|
|
|
|
return;
|
|
|
|
}
|
2012-11-15 14:58:40 -08:00
|
|
|
mNullTransport = new NullTransport();
|
|
|
|
if (mVoENetwork->RegisterExternalTransport(mChannel, *mNullTransport)) {
|
|
|
|
return;
|
|
|
|
}
|
2012-10-16 17:53:55 -07:00
|
|
|
|
|
|
|
// Check for availability.
|
|
|
|
webrtc::VoEHardware* ptrVoEHw = webrtc::VoEHardware::GetInterface(mVoiceEngine);
|
|
|
|
if (ptrVoEHw->SetRecordingDevice(mCapIndex)) {
|
2012-11-15 15:23:39 -08:00
|
|
|
ptrVoEHw->Release();
|
2012-10-16 17:53:55 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool avail = false;
|
|
|
|
ptrVoEHw->GetRecordingDeviceStatus(avail);
|
2012-11-15 15:23:39 -08:00
|
|
|
ptrVoEHw->Release();
|
2012-10-16 17:53:55 -07:00
|
|
|
if (!avail) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set "codec" to PCM, 32kHz on 1 channel
|
|
|
|
webrtc::VoECodec* ptrVoECodec;
|
|
|
|
webrtc::CodecInst codec;
|
|
|
|
ptrVoECodec = webrtc::VoECodec::GetInterface(mVoiceEngine);
|
|
|
|
if (!ptrVoECodec) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
strcpy(codec.plname, ENCODING);
|
|
|
|
codec.channels = CHANNELS;
|
|
|
|
codec.rate = SAMPLE_RATE;
|
|
|
|
codec.plfreq = SAMPLE_FREQUENCY;
|
|
|
|
codec.pacsize = SAMPLE_LENGTH;
|
|
|
|
codec.pltype = 0; // Default payload type
|
|
|
|
|
|
|
|
if (ptrVoECodec->SetSendCodec(mChannel, codec)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mInitDone = true;
|
|
|
|
}
|
2012-07-12 04:53:08 -07:00
|
|
|
|
|
|
|
void
|
|
|
|
MediaEngineWebRTCAudioSource::Shutdown()
|
|
|
|
{
|
|
|
|
if (!mInitDone) {
|
2012-11-15 14:58:40 -08:00
|
|
|
// duplicate these here in case we failed during Init()
|
|
|
|
if (mChannel != -1) {
|
|
|
|
mVoENetwork->DeRegisterExternalTransport(mChannel);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mNullTransport) {
|
|
|
|
delete mNullTransport;
|
|
|
|
}
|
|
|
|
|
2012-07-12 04:53:08 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mState == kStarted) {
|
2012-12-31 15:12:12 -08:00
|
|
|
while (!mSources.IsEmpty()) {
|
|
|
|
Stop(mSources[0], kAudioTrack); // XXX change to support multiple tracks
|
|
|
|
}
|
|
|
|
MOZ_ASSERT(mState == kStopped);
|
2012-07-12 04:53:08 -07:00
|
|
|
}
|
|
|
|
|
2012-12-31 15:12:12 -08:00
|
|
|
if (mState == kAllocated || mState == kStopped) {
|
2012-07-12 04:53:08 -07:00
|
|
|
Deallocate();
|
|
|
|
}
|
|
|
|
|
2012-10-16 17:53:55 -07:00
|
|
|
mVoEBase->Terminate();
|
2012-11-15 14:58:40 -08:00
|
|
|
if (mChannel != -1) {
|
|
|
|
mVoENetwork->DeRegisterExternalTransport(mChannel);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mNullTransport) {
|
|
|
|
delete mNullTransport;
|
|
|
|
}
|
|
|
|
|
2012-10-16 17:53:55 -07:00
|
|
|
mVoERender->Release();
|
2012-07-12 04:53:08 -07:00
|
|
|
mVoEBase->Release();
|
|
|
|
|
|
|
|
mState = kReleased;
|
|
|
|
mInitDone = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef WebRtc_Word16 sample;
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaEngineWebRTCAudioSource::Process(const int channel,
|
|
|
|
const webrtc::ProcessingTypes type, sample* audio10ms,
|
|
|
|
const int length, const int samplingFreq, const bool isStereo)
|
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter enter(mMonitor);
|
2012-10-17 14:40:14 -07:00
|
|
|
if (mState != kStarted)
|
|
|
|
return;
|
2012-07-12 04:53:08 -07:00
|
|
|
|
2012-12-31 15:12:12 -08:00
|
|
|
uint32_t len = mSources.Length();
|
|
|
|
for (uint32_t i = 0; i < len; i++) {
|
|
|
|
nsRefPtr<SharedBuffer> buffer = SharedBuffer::Create(length * sizeof(sample));
|
|
|
|
|
|
|
|
sample* dest = static_cast<sample*>(buffer->Data());
|
|
|
|
memcpy(dest, audio10ms, length * sizeof(sample));
|
2012-07-12 04:53:08 -07:00
|
|
|
|
2012-12-31 15:12:12 -08:00
|
|
|
AudioSegment segment;
|
2012-11-21 21:04:27 -08:00
|
|
|
nsAutoTArray<const sample*,1> channels;
|
|
|
|
channels.AppendElement(dest);
|
|
|
|
segment.AppendFrames(buffer.forget(), channels, length);
|
2012-07-12 04:53:08 -07:00
|
|
|
|
2012-12-31 15:12:12 -08:00
|
|
|
SourceMediaStream *source = mSources[i];
|
|
|
|
if (source) {
|
2013-01-16 23:38:21 -08:00
|
|
|
// This is safe from any thread, and is safe if the track is Finished
|
|
|
|
// or Destroyed
|
2012-12-31 15:12:12 -08:00
|
|
|
source->AppendToTrack(mTrackID, &segment);
|
|
|
|
}
|
|
|
|
}
|
2012-07-12 04:53:08 -07:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|