From 1bd5fe4a2bf71b563183c48bed129f72beaae854 Mon Sep 17 00:00:00 2001 From: Suhas Nandakumar Date: Wed, 5 Dec 2012 13:37:32 -0800 Subject: [PATCH] Bug 817488 Audio Send and Recv support for PC tests r=ekr --- .../webrtc/signaling/test/FakeMediaStreams.h | 52 +++++++++++++++++-- .../signaling/test/FakeMediaStreamsImpl.h | 31 +++++++++-- .../signaling/test/signaling_unittests.cpp | 22 ++++++++ 3 files changed, 98 insertions(+), 7 deletions(-) diff --git a/media/webrtc/signaling/test/FakeMediaStreams.h b/media/webrtc/signaling/test/FakeMediaStreams.h index f15656a1d876..2f1ec88be035 100644 --- a/media/webrtc/signaling/test/FakeMediaStreams.h +++ b/media/webrtc/signaling/test/FakeMediaStreams.h @@ -95,13 +95,46 @@ class Fake_SourceMediaStream : public Fake_MediaStream { public: Fake_SourceMediaStream() : mSegmentsAdded(0), mPullEnabled(false), + mStop(false), mPeriodic(new Fake_MediaPeriodic(this)) {} void AddTrack(mozilla::TrackID aID, mozilla::TrackRate aRate, mozilla::TrackTicks aStart, mozilla::MediaSegment* aSegment) {} void AppendToTrack(mozilla::TrackID aID, mozilla::MediaSegment* aSegment) { - ++mSegmentsAdded; + bool nonZeroSample = false; + MOZ_ASSERT(aSegment); + if(aSegment->GetType() == mozilla::MediaSegment::AUDIO) { + //On audio segment append, we verify for validity + //of the audio samples. + mozilla::AudioSegment* audio = + static_cast(aSegment); + mozilla::AudioSegment::ChunkIterator iter(*audio); + while(!iter.IsEnded()) { + mozilla::AudioChunk& chunk = *(iter); + MOZ_ASSERT(chunk.mBuffer); + const int16_t* buf = + static_cast(chunk.mBuffer->Data()); + for(int i=0; i mPeriodic; nsCOMPtr mTimer; }; @@ -202,9 +240,17 @@ class Fake_MediaStreamBase : public Fake_MediaStream { class Fake_AudioStreamSource : public Fake_MediaStreamBase { public: - Fake_AudioStreamSource() : Fake_MediaStreamBase() {} - + Fake_AudioStreamSource() : Fake_MediaStreamBase(), + mCount(0), + mStop(false) {} + //Signaling Agent indicates us to stop generating + //further audio. + void StopStream() { + mStop = true; + } virtual void Periodic(); + int mCount; + bool mStop; }; class Fake_VideoStreamSource : public Fake_MediaStreamBase { diff --git a/media/webrtc/signaling/test/FakeMediaStreamsImpl.h b/media/webrtc/signaling/test/FakeMediaStreamsImpl.h index e48047c6b6cb..a6187ee0849c 100644 --- a/media/webrtc/signaling/test/FakeMediaStreamsImpl.h +++ b/media/webrtc/signaling/test/FakeMediaStreamsImpl.h @@ -10,6 +10,9 @@ #include "nspr.h" #include "nsError.h" +static const int AUDIO_BUFFER_SIZE = 1600; +static const int NUM_CHANNELS = 2; + NS_IMPL_THREADSAFE_ISUPPORTS1(Fake_nsDOMMediaStream, nsIDOMMediaStream) // DOM Media stream @@ -42,7 +45,10 @@ nsresult Fake_SourceMediaStream::Stop() { } void Fake_SourceMediaStream::Periodic() { - if (mPullEnabled) { + // Pull more audio-samples iff pulling is enabled + // and we are not asked by the signaling agent to stop + //pulling data. + if (mPullEnabled && !mStop) { for (std::set::iterator it = mListeners.begin(); it != mListeners.end(); ++it) { (*it)->NotifyPull(NULL, mozilla::MillisecondsToMediaTime(10)); @@ -50,7 +56,6 @@ void Fake_SourceMediaStream::Periodic() { } } - // Fake_MediaStreamBase nsresult Fake_MediaStreamBase::Start() { mTimer = do_CreateInstance(NS_TIMER_CONTRACTID); @@ -71,11 +76,29 @@ nsresult Fake_MediaStreamBase::Stop() { // Fake_AudioStreamSource void Fake_AudioStreamSource::Periodic() { + //Are we asked to stop pumping audio samples ? + if(mStop) { + return; + } + //Generate Signed 16 Bit Audio samples + nsRefPtr samples = + mozilla::SharedBuffer::Create(AUDIO_BUFFER_SIZE * NUM_CHANNELS * sizeof(int16_t)); + for(int i=0; i<(1600*2); i++) { + //saw tooth audio sample + reinterpret_cast(samples->Data())[i] = + ((mCount % 8) * 4000) - (7*4000)/2; + mCount++; + } + mozilla::AudioSegment segment; segment.Init(1); - segment.InsertNullDataAtStart(160); + segment.AppendFrames(samples.forget(), + AUDIO_BUFFER_SIZE, + 0, + AUDIO_BUFFER_SIZE, + mozilla::AUDIO_FORMAT_S16); - for (std::set::iterator it = mListeners.begin(); + for(std::set::iterator it = mListeners.begin(); it != mListeners.end(); ++it) { (*it)->NotifyQueuedTrackChanges(NULL, // Graph 0, // TrackID diff --git a/media/webrtc/signaling/test/signaling_unittests.cpp b/media/webrtc/signaling/test/signaling_unittests.cpp index d053a184388f..234815853773 100644 --- a/media/webrtc/signaling/test/signaling_unittests.cpp +++ b/media/webrtc/signaling/test/signaling_unittests.cpp @@ -677,6 +677,22 @@ void CreateAnswer(sipcc::MediaConstraints& constraints, std::string offer, domMediaStream_->GetStream())->GetSegmentsAdded(); } + //Stops generating new audio data for transmission. + //Should be called before Cleanup of the peer connection. + void CloseSendStreams() { + static_cast( + domMediaStream_->GetStream())->StopStream(); + } + + //Stops pulling audio data off the receivers. + //Should be called before Cleanup of the peer connection. + void CloseReceiveStreams() { + std::vector streams = + pObserver->GetStreams(); + for(int i=0; i < streams.size(); i++) { + streams[i]->GetStream()->AsSourceStream()->StopStream(); + } + } public: mozilla::RefPtr pc; @@ -1189,6 +1205,8 @@ TEST_F(SignalingTest, OfferModifiedAnswer) OfferModifiedAnswer(constraints, constraints, SHOULD_SENDRECV_AV, SHOULD_SENDRECV_AV); PR_Sleep(kDefaultTimeout * 2); // Wait for completion + a1_.CloseSendStreams(); + a2_.CloseReceiveStreams(); } TEST_F(SignalingTest, FullCall) @@ -1199,6 +1217,8 @@ TEST_F(SignalingTest, FullCall) PR_Sleep(kDefaultTimeout * 2); // Wait for some data to get written + a1_.CloseSendStreams(); + a2_.CloseReceiveStreams(); // Check that we wrote a bunch of data ASSERT_GE(a1_.GetPacketsSent(0), 40); //ASSERT_GE(a2_.GetPacketsSent(0), 40); @@ -1214,6 +1234,8 @@ TEST_F(SignalingTest, FullCallTrickle) PR_Sleep(kDefaultTimeout * 2); // Wait for some data to get written + a1_.CloseSendStreams(); + a2_.CloseReceiveStreams(); ASSERT_GE(a1_.GetPacketsSent(0), 40); ASSERT_GE(a2_.GetPacketsReceived(0), 40); }