2012-07-12 15:53:08 +04: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"
|
2014-04-02 21:58:19 +04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <algorithm>
|
|
|
|
#include "mozilla/Assertions.h"
|
2014-04-18 23:15:10 +04:00
|
|
|
#include "MediaTrackConstraints.h"
|
2014-07-08 23:02:40 +04:00
|
|
|
#include "mtransport/runnable_utils.h"
|
2014-04-02 21:58:19 +04:00
|
|
|
|
|
|
|
// scoped_ptr.h uses FF
|
|
|
|
#ifdef FF
|
|
|
|
#undef FF
|
|
|
|
#endif
|
|
|
|
#include "webrtc/modules/audio_device/opensl/single_rw_fifo.h"
|
2012-07-12 15:53:08 +04:00
|
|
|
|
|
|
|
#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)
|
|
|
|
|
2014-04-02 21:58:19 +04:00
|
|
|
// These are restrictions from the webrtc.org code
|
|
|
|
#define MAX_CHANNELS 2
|
|
|
|
#define MAX_SAMPLING_FREQ 48000 // Hz - multiple of 100
|
|
|
|
|
|
|
|
#define MAX_AEC_FIFO_DEPTH 200 // ms - multiple of 10
|
|
|
|
static_assert(!(MAX_AEC_FIFO_DEPTH % 10), "Invalid MAX_AEC_FIFO_DEPTH");
|
|
|
|
|
2012-07-12 15:53:08 +04:00
|
|
|
namespace mozilla {
|
|
|
|
|
2013-11-18 06:31:46 +04:00
|
|
|
#ifdef LOG
|
|
|
|
#undef LOG
|
|
|
|
#endif
|
|
|
|
|
2012-10-30 03:32:10 +04:00
|
|
|
extern PRLogModuleInfo* GetMediaManagerLog();
|
2015-06-04 01:25:57 +03:00
|
|
|
#define LOG(msg) MOZ_LOG(GetMediaManagerLog(), mozilla::LogLevel::Debug, msg)
|
|
|
|
#define LOG_FRAMES(msg) MOZ_LOG(GetMediaManagerLog(), mozilla::LogLevel::Verbose, msg)
|
2012-10-16 00:41:46 +04:00
|
|
|
|
2012-07-12 15:53:08 +04:00
|
|
|
/**
|
2015-07-24 15:28:16 +03:00
|
|
|
* Webrtc microphone source source.
|
2012-07-12 15:53:08 +04:00
|
|
|
*/
|
2015-07-24 15:28:16 +03:00
|
|
|
NS_IMPL_ISUPPORTS0(MediaEngineWebRTCMicrophoneSource)
|
2015-07-24 15:28:16 +03:00
|
|
|
NS_IMPL_ISUPPORTS0(MediaEngineWebRTCAudioCaptureSource)
|
2012-07-12 15:53:08 +04:00
|
|
|
|
2014-04-02 21:58:19 +04:00
|
|
|
// XXX temp until MSG supports registration
|
2014-08-26 19:02:31 +04:00
|
|
|
StaticRefPtr<AudioOutputObserver> gFarendObserver;
|
2014-04-02 21:58:19 +04:00
|
|
|
|
|
|
|
AudioOutputObserver::AudioOutputObserver()
|
|
|
|
: mPlayoutFreq(0)
|
|
|
|
, mPlayoutChannels(0)
|
|
|
|
, mChunkSize(0)
|
2014-06-07 08:11:42 +04:00
|
|
|
, mSaved(nullptr)
|
2014-04-02 21:58:19 +04:00
|
|
|
, mSamplesSaved(0)
|
|
|
|
{
|
|
|
|
// Buffers of 10ms chunks
|
|
|
|
mPlayoutFifo = new webrtc::SingleRwFifo(MAX_AEC_FIFO_DEPTH/10);
|
|
|
|
}
|
|
|
|
|
|
|
|
AudioOutputObserver::~AudioOutputObserver()
|
|
|
|
{
|
2014-05-12 17:25:01 +04:00
|
|
|
Clear();
|
2015-02-19 07:51:06 +03:00
|
|
|
free(mSaved);
|
2014-07-08 16:46:28 +04:00
|
|
|
mSaved = nullptr;
|
2014-04-02 21:58:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AudioOutputObserver::Clear()
|
|
|
|
{
|
|
|
|
while (mPlayoutFifo->size() > 0) {
|
2015-02-19 07:51:06 +03:00
|
|
|
free(mPlayoutFifo->Pop());
|
2014-04-02 21:58:19 +04:00
|
|
|
}
|
2014-07-08 16:46:28 +04:00
|
|
|
// we'd like to touch mSaved here, but we can't if we might still be getting callbacks
|
2014-04-02 21:58:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
FarEndAudioChunk *
|
|
|
|
AudioOutputObserver::Pop()
|
|
|
|
{
|
|
|
|
return (FarEndAudioChunk *) mPlayoutFifo->Pop();
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t
|
|
|
|
AudioOutputObserver::Size()
|
|
|
|
{
|
|
|
|
return mPlayoutFifo->size();
|
|
|
|
}
|
|
|
|
|
2014-08-25 16:13:08 +04:00
|
|
|
void
|
|
|
|
AudioOutputObserver::MixerCallback(AudioDataValue* aMixedBuffer,
|
|
|
|
AudioSampleFormat aFormat,
|
|
|
|
uint32_t aChannels,
|
|
|
|
uint32_t aFrames,
|
|
|
|
uint32_t aSampleRate)
|
|
|
|
{
|
2014-08-26 19:02:31 +04:00
|
|
|
if (gFarendObserver) {
|
|
|
|
gFarendObserver->InsertFarEnd(aMixedBuffer, aFrames, false,
|
|
|
|
aSampleRate, aChannels, aFormat);
|
|
|
|
}
|
2014-08-25 16:13:08 +04:00
|
|
|
}
|
|
|
|
|
2014-04-02 21:58:19 +04:00
|
|
|
// static
|
|
|
|
void
|
2014-07-23 18:02:34 +04:00
|
|
|
AudioOutputObserver::InsertFarEnd(const AudioDataValue *aBuffer, uint32_t aFrames, bool aOverran,
|
2014-04-02 21:58:19 +04:00
|
|
|
int aFreq, int aChannels, AudioSampleFormat aFormat)
|
|
|
|
{
|
|
|
|
if (mPlayoutChannels != 0) {
|
|
|
|
if (mPlayoutChannels != static_cast<uint32_t>(aChannels)) {
|
|
|
|
MOZ_CRASH();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
MOZ_ASSERT(aChannels <= MAX_CHANNELS);
|
|
|
|
mPlayoutChannels = static_cast<uint32_t>(aChannels);
|
|
|
|
}
|
|
|
|
if (mPlayoutFreq != 0) {
|
|
|
|
if (mPlayoutFreq != static_cast<uint32_t>(aFreq)) {
|
|
|
|
MOZ_CRASH();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
MOZ_ASSERT(aFreq <= MAX_SAMPLING_FREQ);
|
|
|
|
MOZ_ASSERT(!(aFreq % 100), "Sampling rate for far end data should be multiple of 100.");
|
|
|
|
mPlayoutFreq = aFreq;
|
|
|
|
mChunkSize = aFreq/100; // 10ms
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef LOG_FAREND_INSERTION
|
|
|
|
static FILE *fp = fopen("insertfarend.pcm","wb");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (mSaved) {
|
|
|
|
// flag overrun as soon as possible, and only once
|
|
|
|
mSaved->mOverrun = aOverran;
|
|
|
|
aOverran = false;
|
|
|
|
}
|
|
|
|
// Rechunk to 10ms.
|
|
|
|
// The AnalyzeReverseStream() and WebRtcAec_BufferFarend() functions insist on 10ms
|
|
|
|
// samples per call. Annoying...
|
2014-07-23 18:02:34 +04:00
|
|
|
while (aFrames) {
|
2014-04-02 21:58:19 +04:00
|
|
|
if (!mSaved) {
|
|
|
|
mSaved = (FarEndAudioChunk *) moz_xmalloc(sizeof(FarEndAudioChunk) +
|
|
|
|
(mChunkSize * aChannels - 1)*sizeof(int16_t));
|
|
|
|
mSaved->mSamples = mChunkSize;
|
|
|
|
mSaved->mOverrun = aOverran;
|
|
|
|
aOverran = false;
|
|
|
|
}
|
|
|
|
uint32_t to_copy = mChunkSize - mSamplesSaved;
|
2014-07-23 18:02:34 +04:00
|
|
|
if (to_copy > aFrames) {
|
|
|
|
to_copy = aFrames;
|
2014-04-02 21:58:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int16_t *dest = &(mSaved->mData[mSamplesSaved * aChannels]);
|
|
|
|
ConvertAudioSamples(aBuffer, dest, to_copy * aChannels);
|
|
|
|
|
|
|
|
#ifdef LOG_FAREND_INSERTION
|
|
|
|
if (fp) {
|
|
|
|
fwrite(&(mSaved->mData[mSamplesSaved * aChannels]), to_copy * aChannels, sizeof(int16_t), fp);
|
|
|
|
}
|
|
|
|
#endif
|
2014-07-23 18:02:34 +04:00
|
|
|
aFrames -= to_copy;
|
2014-04-02 21:58:19 +04:00
|
|
|
mSamplesSaved += to_copy;
|
2014-04-25 16:10:38 +04:00
|
|
|
aBuffer += to_copy * aChannels;
|
2014-04-02 21:58:19 +04:00
|
|
|
|
|
|
|
if (mSamplesSaved >= mChunkSize) {
|
|
|
|
int free_slots = mPlayoutFifo->capacity() - mPlayoutFifo->size();
|
|
|
|
if (free_slots <= 0) {
|
|
|
|
// XXX We should flag an overrun for the reader. We can't drop data from it due to
|
|
|
|
// thread safety issues.
|
|
|
|
break;
|
|
|
|
} else {
|
2014-06-07 08:11:42 +04:00
|
|
|
mPlayoutFifo->Push((int8_t *) mSaved); // takes ownership
|
|
|
|
mSaved = nullptr;
|
2014-04-02 21:58:19 +04:00
|
|
|
mSamplesSaved = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-12 15:53:08 +04:00
|
|
|
void
|
2015-07-24 15:28:16 +03:00
|
|
|
MediaEngineWebRTCMicrophoneSource::GetName(nsAString& aName)
|
2012-07-12 15:53:08 +04:00
|
|
|
{
|
|
|
|
if (mInitDone) {
|
|
|
|
aName.Assign(mDeviceName);
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-07-24 15:28:16 +03:00
|
|
|
MediaEngineWebRTCMicrophoneSource::GetUUID(nsACString& aUUID)
|
2012-07-12 15:53:08 +04:00
|
|
|
{
|
|
|
|
if (mInitDone) {
|
|
|
|
aUUID.Assign(mDeviceUUID);
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-01-29 20:55:09 +04:00
|
|
|
nsresult
|
2015-07-24 15:28:16 +03:00
|
|
|
MediaEngineWebRTCMicrophoneSource::Config(bool aEchoOn, uint32_t aEcho,
|
|
|
|
bool aAgcOn, uint32_t aAGC,
|
|
|
|
bool aNoiseOn, uint32_t aNoise,
|
|
|
|
int32_t aPlayoutDelay)
|
2013-01-29 20:55:09 +04:00
|
|
|
{
|
|
|
|
LOG(("Audio config: aec: %d, agc: %d, noise: %d",
|
|
|
|
aEchoOn ? aEcho : -1,
|
|
|
|
aAgcOn ? aAGC : -1,
|
|
|
|
aNoiseOn ? aNoise : -1));
|
|
|
|
|
2014-04-02 21:58:19 +04:00
|
|
|
bool update_echo = (mEchoOn != aEchoOn);
|
|
|
|
bool update_agc = (mAgcOn != aAgcOn);
|
|
|
|
bool update_noise = (mNoiseOn != aNoiseOn);
|
|
|
|
mEchoOn = aEchoOn;
|
2013-01-29 20:55:09 +04:00
|
|
|
mAgcOn = aAgcOn;
|
|
|
|
mNoiseOn = aNoiseOn;
|
|
|
|
|
2014-04-02 21:58:19 +04:00
|
|
|
if ((webrtc::EcModes) aEcho != webrtc::kEcUnchanged) {
|
|
|
|
if (mEchoCancel != (webrtc::EcModes) aEcho) {
|
|
|
|
update_echo = true;
|
|
|
|
mEchoCancel = (webrtc::EcModes) aEcho;
|
|
|
|
}
|
|
|
|
}
|
2013-01-29 20:55:09 +04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2014-04-02 21:58:19 +04:00
|
|
|
mPlayoutDelay = aPlayoutDelay;
|
2013-01-29 20:55:09 +04:00
|
|
|
|
|
|
|
if (mInitDone) {
|
|
|
|
int error;
|
2014-04-02 21:58:19 +04:00
|
|
|
|
|
|
|
if (update_echo &&
|
|
|
|
0 != (error = mVoEProcessing->SetEcStatus(mEchoOn, (webrtc::EcModes) aEcho))) {
|
|
|
|
LOG(("%s Error setting Echo Status: %d ",__FUNCTION__, error));
|
2014-04-02 21:58:20 +04:00
|
|
|
// Overhead of capturing all the time is very low (<0.1% of an audio only call)
|
|
|
|
if (mEchoOn) {
|
|
|
|
if (0 != (error = mVoEProcessing->SetEcMetricsStatus(true))) {
|
|
|
|
LOG(("%s Error setting Echo Metrics: %d ",__FUNCTION__, error));
|
|
|
|
}
|
|
|
|
}
|
2014-04-02 21:58:19 +04:00
|
|
|
}
|
2013-01-29 20:55:09 +04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-07-03 01:01:52 +03:00
|
|
|
// GetBestFitnessDistance returns the best distance the capture device can offer
|
|
|
|
// as a whole, given an accumulated number of ConstraintSets.
|
|
|
|
// Ideal values are considered in the first ConstraintSet only.
|
|
|
|
// Plain values are treated as Ideal in the first ConstraintSet.
|
|
|
|
// Plain values are treated as Exact in subsequent ConstraintSets.
|
|
|
|
// Infinity = UINT32_MAX e.g. device cannot satisfy accumulated ConstraintSets.
|
|
|
|
// A finite result may be used to calculate this device's ranking as a choice.
|
|
|
|
|
2015-07-24 23:15:57 +03:00
|
|
|
uint32_t MediaEngineWebRTCAudioSource::GetBestFitnessDistance(
|
2015-07-03 01:01:52 +03:00
|
|
|
const nsTArray<const dom::MediaTrackConstraintSet*>& aConstraintSets,
|
|
|
|
const nsString& aDeviceId)
|
|
|
|
{
|
|
|
|
uint32_t distance = 0;
|
|
|
|
|
|
|
|
for (const MediaTrackConstraintSet* cs : aConstraintSets) {
|
|
|
|
distance = GetMinimumFitnessDistance(*cs, false, aDeviceId);
|
|
|
|
break; // distance is read from first entry only
|
|
|
|
}
|
|
|
|
return distance;
|
|
|
|
}
|
|
|
|
|
2012-07-12 15:53:08 +04:00
|
|
|
nsresult
|
2015-07-24 15:28:16 +03:00
|
|
|
MediaEngineWebRTCMicrophoneSource::Allocate(const dom::MediaTrackConstraints &aConstraints,
|
|
|
|
const MediaEnginePrefs &aPrefs,
|
|
|
|
const nsString& aDeviceId)
|
2012-07-12 15:53:08 +04:00
|
|
|
{
|
2013-10-01 16:06:57 +04:00
|
|
|
if (mState == kReleased) {
|
|
|
|
if (mInitDone) {
|
2014-03-09 09:18:50 +04:00
|
|
|
ScopedCustomReleasePtr<webrtc::VoEHardware> ptrVoEHw(webrtc::VoEHardware::GetInterface(mVoiceEngine));
|
|
|
|
if (!ptrVoEHw || ptrVoEHw->SetRecordingDevice(mCapIndex)) {
|
2013-10-01 16:06:57 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
mState = kAllocated;
|
|
|
|
LOG(("Audio device %d allocated", mCapIndex));
|
|
|
|
} else {
|
|
|
|
LOG(("Audio device is not initalized"));
|
2013-01-01 03:12:12 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2015-06-04 01:25:57 +03:00
|
|
|
} else if (MOZ_LOG_TEST(GetMediaManagerLog(), LogLevel::Debug)) {
|
2015-01-17 00:27:56 +03:00
|
|
|
MonitorAutoLock lock(mMonitor);
|
|
|
|
if (mSources.IsEmpty()) {
|
|
|
|
LOG(("Audio device %d reallocated", mCapIndex));
|
|
|
|
} else {
|
|
|
|
LOG(("Audio device %d allocated shared", mCapIndex));
|
|
|
|
}
|
2012-11-16 03:23:39 +04:00
|
|
|
}
|
2012-07-12 15:53:08 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2015-07-24 15:28:16 +03:00
|
|
|
MediaEngineWebRTCMicrophoneSource::Deallocate()
|
2012-07-12 15:53:08 +04:00
|
|
|
{
|
2015-01-17 00:27:56 +03:00
|
|
|
bool empty;
|
|
|
|
{
|
|
|
|
MonitorAutoLock lock(mMonitor);
|
|
|
|
empty = mSources.IsEmpty();
|
|
|
|
}
|
|
|
|
if (empty) {
|
|
|
|
// If empty, no callbacks to deliver data should be occuring
|
2013-01-01 03:12:12 +04:00
|
|
|
if (mState != kStopped && mState != kAllocated) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2012-07-12 15:53:08 +04:00
|
|
|
|
2013-01-01 03:12:12 +04:00
|
|
|
mState = kReleased;
|
|
|
|
LOG(("Audio device %d deallocated", mCapIndex));
|
|
|
|
} else {
|
|
|
|
LOG(("Audio device %d deallocated but still in use", mCapIndex));
|
|
|
|
}
|
2012-07-12 15:53:08 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2015-07-24 15:28:16 +03:00
|
|
|
MediaEngineWebRTCMicrophoneSource::Start(SourceMediaStream *aStream,
|
|
|
|
TrackID aID)
|
2012-07-12 15:53:08 +04:00
|
|
|
{
|
2013-01-01 03:12:12 +04:00
|
|
|
if (!mInitDone || !aStream) {
|
2012-07-12 15:53:08 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2013-01-10 20:52:53 +04:00
|
|
|
{
|
2013-03-13 19:42:18 +04:00
|
|
|
MonitorAutoLock lock(mMonitor);
|
2013-01-10 20:52:53 +04:00
|
|
|
mSources.AppendElement(aStream);
|
|
|
|
}
|
2012-07-12 15:53:08 +04:00
|
|
|
|
|
|
|
AudioSegment* segment = new AudioSegment();
|
2015-02-19 20:04:26 +03:00
|
|
|
aStream->AddAudioTrack(aID, SAMPLE_FREQUENCY, 0, segment, SourceMediaStream::ADDTRACK_QUEUED);
|
|
|
|
|
2014-03-24 14:06:06 +04:00
|
|
|
// XXX Make this based on the pref.
|
|
|
|
aStream->RegisterForAudioMixing();
|
2013-10-26 02:13:42 +04:00
|
|
|
LOG(("Start audio for stream %p", aStream));
|
2012-07-12 15:53:08 +04:00
|
|
|
|
2013-01-01 03:12:12 +04:00
|
|
|
if (mState == kStarted) {
|
2013-10-26 02:13:42 +04:00
|
|
|
MOZ_ASSERT(aID == mTrackID);
|
2013-01-01 03:12:12 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
mState = kStarted;
|
2013-10-26 02:13:42 +04:00
|
|
|
mTrackID = aID;
|
|
|
|
|
|
|
|
// Make sure logger starts before capture
|
|
|
|
AsyncLatencyLogger::Get(true);
|
2013-01-01 03:12:12 +04:00
|
|
|
|
2014-04-02 21:58:19 +04:00
|
|
|
// Register output observer
|
|
|
|
// XXX
|
|
|
|
MOZ_ASSERT(gFarendObserver);
|
|
|
|
gFarendObserver->Clear();
|
|
|
|
|
2013-01-29 20:55:09 +04:00
|
|
|
// Configure audio processing in webrtc code
|
|
|
|
Config(mEchoOn, webrtc::kEcUnchanged,
|
|
|
|
mAgcOn, webrtc::kAgcUnchanged,
|
2014-04-02 21:58:19 +04:00
|
|
|
mNoiseOn, webrtc::kNsUnchanged,
|
|
|
|
mPlayoutDelay);
|
2013-01-29 20:55:09 +04:00
|
|
|
|
2012-07-12 15:53:08 +04: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
|
2015-07-24 15:28:16 +03:00
|
|
|
MediaEngineWebRTCMicrophoneSource::Stop(SourceMediaStream *aSource, TrackID aID)
|
2012-07-12 15:53:08 +04:00
|
|
|
{
|
2013-01-01 03:12:12 +04:00
|
|
|
{
|
2013-03-13 19:42:18 +04:00
|
|
|
MonitorAutoLock lock(mMonitor);
|
2013-01-10 20:52:53 +04:00
|
|
|
|
|
|
|
if (!mSources.RemoveElement(aSource)) {
|
|
|
|
// Already stopped - this is allowed
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2014-12-12 07:52:00 +03:00
|
|
|
|
|
|
|
aSource->EndTrack(aID);
|
|
|
|
|
2013-01-10 20:52:53 +04:00
|
|
|
if (!mSources.IsEmpty()) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
if (mState != kStarted) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
if (!mVoEBase) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2013-01-01 03:12:12 +04:00
|
|
|
mState = kStopped;
|
|
|
|
}
|
|
|
|
|
2012-07-12 15:53:08 +04: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 13:46:40 +04:00
|
|
|
void
|
2015-07-24 15:28:16 +03:00
|
|
|
MediaEngineWebRTCMicrophoneSource::NotifyPull(MediaStreamGraph *aGraph,
|
|
|
|
SourceMediaStream *aSource,
|
|
|
|
TrackID aID,
|
|
|
|
StreamTime aDesiredTime)
|
2012-10-17 13:46:40 +04:00
|
|
|
{
|
|
|
|
// Ignore - we push audio data
|
2014-12-30 04:54:03 +03:00
|
|
|
LOG_FRAMES(("NotifyPull, desired = %ld", (int64_t) aDesiredTime));
|
2012-10-17 13:46:40 +04:00
|
|
|
}
|
|
|
|
|
2012-10-17 04:53:55 +04:00
|
|
|
void
|
2015-07-24 15:28:16 +03:00
|
|
|
MediaEngineWebRTCMicrophoneSource::Init()
|
2012-10-17 04:53:55 +04:00
|
|
|
{
|
|
|
|
mVoEBase = webrtc::VoEBase::GetInterface(mVoiceEngine);
|
|
|
|
|
|
|
|
mVoEBase->Init();
|
|
|
|
|
|
|
|
mVoERender = webrtc::VoEExternalMedia::GetInterface(mVoiceEngine);
|
|
|
|
if (!mVoERender) {
|
|
|
|
return;
|
|
|
|
}
|
2012-11-16 02:58:40 +04:00
|
|
|
mVoENetwork = webrtc::VoENetwork::GetInterface(mVoiceEngine);
|
|
|
|
if (!mVoENetwork) {
|
|
|
|
return;
|
|
|
|
}
|
2012-10-17 04:53:55 +04:00
|
|
|
|
2013-01-29 20:55:09 +04:00
|
|
|
mVoEProcessing = webrtc::VoEAudioProcessing::GetInterface(mVoiceEngine);
|
|
|
|
if (!mVoEProcessing) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-10-17 04:53:55 +04:00
|
|
|
mChannel = mVoEBase->CreateChannel();
|
|
|
|
if (mChannel < 0) {
|
|
|
|
return;
|
|
|
|
}
|
2012-11-16 02:58:40 +04:00
|
|
|
mNullTransport = new NullTransport();
|
|
|
|
if (mVoENetwork->RegisterExternalTransport(mChannel, *mNullTransport)) {
|
|
|
|
return;
|
|
|
|
}
|
2012-10-17 04:53:55 +04:00
|
|
|
|
|
|
|
// Check for availability.
|
2014-03-09 09:18:50 +04:00
|
|
|
ScopedCustomReleasePtr<webrtc::VoEHardware> ptrVoEHw(webrtc::VoEHardware::GetInterface(mVoiceEngine));
|
|
|
|
if (!ptrVoEHw || ptrVoEHw->SetRecordingDevice(mCapIndex)) {
|
2012-10-17 04:53:55 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-12-05 18:29:07 +04:00
|
|
|
#ifndef MOZ_B2G
|
|
|
|
// Because of the permission mechanism of B2G, we need to skip the status
|
|
|
|
// check here.
|
2012-10-17 04:53:55 +04:00
|
|
|
bool avail = false;
|
|
|
|
ptrVoEHw->GetRecordingDeviceStatus(avail);
|
|
|
|
if (!avail) {
|
|
|
|
return;
|
|
|
|
}
|
2013-12-05 18:29:07 +04:00
|
|
|
#endif // MOZ_B2G
|
2014-03-09 09:18:50 +04:00
|
|
|
|
2012-10-17 04:53:55 +04:00
|
|
|
// Set "codec" to PCM, 32kHz on 1 channel
|
2014-03-09 09:18:50 +04:00
|
|
|
ScopedCustomReleasePtr<webrtc::VoECodec> ptrVoECodec(webrtc::VoECodec::GetInterface(mVoiceEngine));
|
2012-10-17 04:53:55 +04:00
|
|
|
if (!ptrVoECodec) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-03-09 09:18:50 +04:00
|
|
|
webrtc::CodecInst codec;
|
2012-10-17 04:53:55 +04:00
|
|
|
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
|
|
|
|
|
2014-03-09 09:18:50 +04:00
|
|
|
if (!ptrVoECodec->SetSendCodec(mChannel, codec)) {
|
|
|
|
mInitDone = true;
|
2012-10-17 04:53:55 +04:00
|
|
|
}
|
|
|
|
}
|
2012-07-12 15:53:08 +04:00
|
|
|
|
|
|
|
void
|
2015-07-24 15:28:16 +03:00
|
|
|
MediaEngineWebRTCMicrophoneSource::Shutdown()
|
2012-07-12 15:53:08 +04:00
|
|
|
{
|
|
|
|
if (!mInitDone) {
|
2012-11-16 02:58:40 +04:00
|
|
|
// duplicate these here in case we failed during Init()
|
2015-05-29 21:28:03 +03:00
|
|
|
if (mChannel != -1 && mVoENetwork) {
|
2012-11-16 02:58:40 +04:00
|
|
|
mVoENetwork->DeRegisterExternalTransport(mChannel);
|
|
|
|
}
|
|
|
|
|
2014-04-17 17:23:16 +04:00
|
|
|
delete mNullTransport;
|
2015-05-29 21:28:03 +03:00
|
|
|
mNullTransport = nullptr;
|
2012-07-12 15:53:08 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mState == kStarted) {
|
2015-01-17 00:27:56 +03:00
|
|
|
SourceMediaStream *source;
|
|
|
|
bool empty;
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
{
|
|
|
|
MonitorAutoLock lock(mMonitor);
|
|
|
|
empty = mSources.IsEmpty();
|
|
|
|
if (empty) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
source = mSources[0];
|
|
|
|
}
|
|
|
|
Stop(source, kAudioTrack); // XXX change to support multiple tracks
|
2013-01-01 03:12:12 +04:00
|
|
|
}
|
|
|
|
MOZ_ASSERT(mState == kStopped);
|
2012-07-12 15:53:08 +04:00
|
|
|
}
|
|
|
|
|
2013-01-01 03:12:12 +04:00
|
|
|
if (mState == kAllocated || mState == kStopped) {
|
2012-07-12 15:53:08 +04:00
|
|
|
Deallocate();
|
|
|
|
}
|
|
|
|
|
2012-10-17 04:53:55 +04:00
|
|
|
mVoEBase->Terminate();
|
2012-11-16 02:58:40 +04:00
|
|
|
if (mChannel != -1) {
|
|
|
|
mVoENetwork->DeRegisterExternalTransport(mChannel);
|
|
|
|
}
|
|
|
|
|
2014-04-17 17:23:16 +04:00
|
|
|
delete mNullTransport;
|
2015-05-29 21:28:03 +03:00
|
|
|
mNullTransport = nullptr;
|
2012-11-16 02:58:40 +04:00
|
|
|
|
2014-03-09 09:18:50 +04:00
|
|
|
mVoEProcessing = nullptr;
|
|
|
|
mVoENetwork = nullptr;
|
|
|
|
mVoERender = nullptr;
|
|
|
|
mVoEBase = nullptr;
|
2012-07-12 15:53:08 +04:00
|
|
|
|
|
|
|
mState = kReleased;
|
|
|
|
mInitDone = false;
|
|
|
|
}
|
|
|
|
|
2013-08-30 10:08:57 +04:00
|
|
|
typedef int16_t sample;
|
2012-07-12 15:53:08 +04:00
|
|
|
|
|
|
|
void
|
2015-07-24 15:28:16 +03:00
|
|
|
MediaEngineWebRTCMicrophoneSource::Process(int channel,
|
|
|
|
webrtc::ProcessingTypes type,
|
|
|
|
sample *audio10ms, int length,
|
|
|
|
int samplingFreq, bool isStereo)
|
2012-07-12 15:53:08 +04:00
|
|
|
{
|
2014-04-02 21:58:19 +04:00
|
|
|
// On initial capture, throw away all far-end data except the most recent sample
|
|
|
|
// since it's already irrelevant and we want to keep avoid confusing the AEC far-end
|
|
|
|
// input code with "old" audio.
|
|
|
|
if (!mStarted) {
|
|
|
|
mStarted = true;
|
|
|
|
while (gFarendObserver->Size() > 1) {
|
2015-02-19 07:51:06 +03:00
|
|
|
free(gFarendObserver->Pop()); // only call if size() > 0
|
2014-04-02 21:58:19 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
while (gFarendObserver->Size() > 0) {
|
|
|
|
FarEndAudioChunk *buffer = gFarendObserver->Pop(); // only call if size() > 0
|
|
|
|
if (buffer) {
|
|
|
|
int length = buffer->mSamples;
|
2014-06-07 08:11:42 +04:00
|
|
|
int res = mVoERender->ExternalPlayoutData(buffer->mData,
|
|
|
|
gFarendObserver->PlayoutFrequency(),
|
|
|
|
gFarendObserver->PlayoutChannels(),
|
|
|
|
mPlayoutDelay,
|
|
|
|
length);
|
2015-02-19 07:51:06 +03:00
|
|
|
free(buffer);
|
2014-06-07 08:11:42 +04:00
|
|
|
if (res == -1) {
|
2014-04-02 21:58:19 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-13 19:42:18 +04:00
|
|
|
MonitorAutoLock lock(mMonitor);
|
2012-10-18 01:40:14 +04:00
|
|
|
if (mState != kStarted)
|
|
|
|
return;
|
2012-07-12 15:53:08 +04:00
|
|
|
|
2013-01-01 03:12:12 +04: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 15:53:08 +04:00
|
|
|
|
2014-07-08 23:02:40 +04:00
|
|
|
nsAutoPtr<AudioSegment> segment(new AudioSegment());
|
2012-11-22 09:04:27 +04:00
|
|
|
nsAutoTArray<const sample*,1> channels;
|
|
|
|
channels.AppendElement(dest);
|
2014-07-08 23:02:40 +04:00
|
|
|
segment->AppendFrames(buffer.forget(), channels, length);
|
2013-10-26 02:13:42 +04:00
|
|
|
TimeStamp insertTime;
|
2014-07-08 23:02:40 +04:00
|
|
|
segment->GetStartTime(insertTime);
|
2012-07-12 15:53:08 +04:00
|
|
|
|
2014-07-08 23:02:40 +04:00
|
|
|
if (mSources[i]) {
|
2013-10-26 02:13:42 +04:00
|
|
|
// Make sure we include the stream and the track.
|
|
|
|
// The 0:1 is a flag to note when we've done the final insert for a given input block.
|
2015-03-10 08:08:03 +03:00
|
|
|
LogTime(AsyncLatencyLogger::AudioTrackInsertion, LATENCY_STREAM_ID(mSources[i].get(), mTrackID),
|
2013-10-26 02:13:42 +04:00
|
|
|
(i+1 < len) ? 0 : 1, insertTime);
|
|
|
|
|
2014-07-08 23:02:40 +04:00
|
|
|
// This is safe from any thread, and is safe if the track is Finished
|
|
|
|
// or Destroyed.
|
|
|
|
// Note: due to evil magic, the nsAutoPtr<AudioSegment>'s ownership transfers to
|
|
|
|
// the Runnable (AutoPtr<> = AutoPtr<>)
|
|
|
|
RUN_ON_THREAD(mThread, WrapRunnable(mSources[i], &SourceMediaStream::AppendToTrack,
|
2014-07-09 00:05:39 +04:00
|
|
|
mTrackID, segment, (AudioSegment *) nullptr),
|
2014-07-08 23:02:40 +04:00
|
|
|
NS_DISPATCH_NORMAL);
|
2013-01-01 03:12:12 +04:00
|
|
|
}
|
|
|
|
}
|
2012-07-12 15:53:08 +04:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-07-24 15:28:16 +03:00
|
|
|
void
|
|
|
|
MediaEngineWebRTCAudioCaptureSource::GetName(nsAString &aName)
|
|
|
|
{
|
|
|
|
aName.AssignLiteral("AudioCapture");
|
|
|
|
}
|
|
|
|
void
|
|
|
|
MediaEngineWebRTCAudioCaptureSource::GetUUID(nsACString &aUUID)
|
|
|
|
{
|
|
|
|
nsID uuid;
|
|
|
|
char uuidBuffer[NSID_LENGTH];
|
|
|
|
nsCString asciiString;
|
|
|
|
ErrorResult rv;
|
|
|
|
|
|
|
|
rv = nsContentUtils::GenerateUUIDInPlace(uuid);
|
|
|
|
if (rv.Failed()) {
|
|
|
|
aUUID.AssignLiteral("");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
uuid.ToProvidedString(uuidBuffer);
|
|
|
|
asciiString.AssignASCII(uuidBuffer);
|
|
|
|
|
|
|
|
// Remove {} and the null terminator
|
|
|
|
aUUID.Assign(Substring(asciiString, 1, NSID_LENGTH - 3));
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
MediaEngineWebRTCAudioCaptureSource::Start(SourceMediaStream *aMediaStream,
|
|
|
|
TrackID aId)
|
|
|
|
{
|
|
|
|
aMediaStream->AddTrack(aId, 0, new AudioSegment());
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
MediaEngineWebRTCAudioCaptureSource::Stop(SourceMediaStream *aMediaStream,
|
|
|
|
TrackID aId)
|
|
|
|
{
|
|
|
|
aMediaStream->EndAllTrackAndFinish();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t
|
|
|
|
MediaEngineWebRTCAudioCaptureSource::GetBestFitnessDistance(
|
|
|
|
const nsTArray<const dom::MediaTrackConstraintSet*>& aConstraintSets,
|
|
|
|
const nsString& aDeviceId)
|
|
|
|
{
|
|
|
|
// There is only one way of capturing audio for now, and it's always adequate.
|
|
|
|
return 0;
|
|
|
|
}
|
2012-07-12 15:53:08 +04:00
|
|
|
}
|