Index: content/renderer/media/media_recorder_handler_unittest.cc |
diff --git a/content/renderer/media/media_recorder_handler_unittest.cc b/content/renderer/media/media_recorder_handler_unittest.cc |
index 208542ff0b39186b5481cbf2641d5b5b92927819..6d9d8689e16230a2d0f6ad66343e4a02da5bbc32 100644 |
--- a/content/renderer/media/media_recorder_handler_unittest.cc |
+++ b/content/renderer/media/media_recorder_handler_unittest.cc |
@@ -7,6 +7,8 @@ |
#include "content/child/child_process.h" |
#include "content/renderer/media/media_recorder_handler.h" |
#include "content/renderer/media/mock_media_stream_registry.h" |
+#include "media/audio/simple_sources.h" |
+#include "media/base/audio_bus.h" |
#include "media/base/video_frame.h" |
#include "testing/gmock/include/gmock/gmock.h" |
#include "testing/gtest/include/gtest/gtest.h" |
@@ -32,6 +34,7 @@ ACTION_P(RunClosure, closure) { |
static const std::string kTestStreamUrl = "stream_url"; |
static const std::string kTestVideoTrackId = "video_track_id"; |
+static const std::string kTestAudioTrackId = "audio_track_id"; |
struct MediaRecorderTestParams { |
const char* const mime_type; |
@@ -47,11 +50,15 @@ class MediaRecorderHandlerTest : public TestWithParam<MediaRecorderTestParams>, |
public blink::WebMediaRecorderHandlerClient { |
public: |
MediaRecorderHandlerTest() |
- : media_recorder_handler_(new MediaRecorderHandler()) { |
+ : media_recorder_handler_(new MediaRecorderHandler()), |
+ audio_source_(2 /* channels */, |
+ 440 /* freq */, |
+ 48000 /* sampling rate */) { |
EXPECT_FALSE(media_recorder_handler_->recording_); |
registry_.Init(kTestStreamUrl); |
registry_.AddVideoTrack(kTestVideoTrackId); |
+ registry_.AddAudioTrack(kTestAudioTrackId); |
} |
~MediaRecorderHandlerTest() { |
@@ -68,11 +75,26 @@ class MediaRecorderHandlerTest : public TestWithParam<MediaRecorderTestParams>, |
bool hasVideoRecorders() const { |
return !media_recorder_handler_->video_recorders_.empty(); |
} |
+ bool hasAudioRecorders() const { |
+ return !media_recorder_handler_->audio_recorders_.empty(); |
+ } |
void OnVideoFrameForTesting(const scoped_refptr<media::VideoFrame>& frame) { |
media_recorder_handler_->OnVideoFrameForTesting(frame, |
base::TimeTicks::Now()); |
} |
+ void OnAudioBusForTesting(const media::AudioBus& audio_bus) { |
+ media_recorder_handler_->OnAudioBusForTesting(audio_bus, |
+ base::TimeTicks::Now()); |
+ } |
+ |
+ scoped_ptr<media::AudioBus> NextAudioBus(const base::TimeDelta& duration) { |
+ const int num_samples = |
+ static_cast<int>((48000 * duration) / base::TimeDelta::FromSeconds(1)); |
+ scoped_ptr<media::AudioBus> bus(media::AudioBus::Create(2, num_samples)); |
+ audio_source_.OnMoreData(bus.get(), 0); |
+ return bus.Pass(); |
+ } |
// A ChildProcess and a MessageLoopForUI are both needed to fool the Tracks |
// and Sources in |registry_| into believing they are on the right threads. |
@@ -83,6 +105,9 @@ class MediaRecorderHandlerTest : public TestWithParam<MediaRecorderTestParams>, |
// The Class under test. Needs to be scoped_ptr to force its destruction. |
scoped_ptr<MediaRecorderHandler> media_recorder_handler_; |
+ // For generating test AudioBuses |
+ media::SineWaveAudioSource audio_source_; |
+ |
private: |
DISALLOW_COPY_AND_ASSIGN(MediaRecorderHandlerTest); |
}; |
@@ -96,6 +121,9 @@ TEST_F(MediaRecorderHandlerTest, CanSupportMimeType) { |
const WebString good_mime_type_vp9(base::UTF8ToUTF16("video/vp9")); |
EXPECT_TRUE(media_recorder_handler_->canSupportMimeType(good_mime_type_vp9)); |
+ const WebString audio_mime_type(base::UTF8ToUTF16("audio/opus")); |
+ EXPECT_TRUE(media_recorder_handler_->canSupportMimeType(audio_mime_type)); |
+ |
const WebString bad_mime_type(base::UTF8ToUTF16("video/unsupportedcodec")); |
EXPECT_FALSE(media_recorder_handler_->canSupportMimeType(bad_mime_type)); |
@@ -111,14 +139,17 @@ TEST_P(MediaRecorderHandlerTest, InitializeStartStop) { |
mime_type)); |
EXPECT_FALSE(recording()); |
EXPECT_FALSE(hasVideoRecorders()); |
+ EXPECT_FALSE(hasAudioRecorders()); |
EXPECT_TRUE(media_recorder_handler_->start()); |
EXPECT_TRUE(recording()); |
EXPECT_TRUE(hasVideoRecorders()); |
+ EXPECT_FALSE(hasAudioRecorders()); |
mcasas
2015/11/10 18:18:59
Why not EXPECT_TRUE()?
Oh I see, the |kMediaRecord
ajose
2015/11/18 00:11:52
Done.
|
media_recorder_handler_->stop(); |
EXPECT_FALSE(recording()); |
EXPECT_FALSE(hasVideoRecorders()); |
+ EXPECT_FALSE(hasAudioRecorders()); |
// Expect a last call on destruction. |
EXPECT_CALL(*this, writeData(_, _, true)).Times(1); |
@@ -179,4 +210,60 @@ INSTANTIATE_TEST_CASE_P(, |
MediaRecorderHandlerTest, |
ValuesIn(kMediaRecorderTestParams)); |
+// Sends 2 frames and expect them as WebM contained encoded data in writeData(). |
+TEST_F(MediaRecorderHandlerTest, EncodeAudioFrames) { |
+ const WebString mime_type(base::UTF8ToUTF16("audio/opus")); |
+ EXPECT_TRUE(media_recorder_handler_->initialize(this, registry_.test_stream(), |
+ mime_type)); |
+ EXPECT_TRUE(media_recorder_handler_->start()); |
+ |
+ InSequence s; |
+ // const scoped_refptr<media::VideoFrame> video_frame = |
+ // media::VideoFrame::CreateBlackFrame(gfx::Size(160, 80)); |
mcasas
2015/11/10 18:18:58
?
ajose
2015/11/18 00:11:52
Done.
|
+ const scoped_ptr<media::AudioBus> audio_bus1 = |
+ NextAudioBus(base::TimeDelta::FromMilliseconds(10)); |
+ const scoped_ptr<media::AudioBus> audio_bus2 = |
+ NextAudioBus(base::TimeDelta::FromMilliseconds(10)); |
+ |
+ { |
+ base::RunLoop run_loop; |
+ base::Closure quit_closure = run_loop.QuitClosure(); |
+ // writeData() is pinged a number of times as the WebM header is written; |
+ // the last time it is called it has the encoded data. |
+ const size_t kEncodedDataSize = 52; |
+ EXPECT_CALL(*this, writeData(_, Lt(kEncodedDataSize), false)) |
+ .Times(AtLeast(1)); |
+ EXPECT_CALL(*this, writeData(_, kEncodedDataSize, false)) |
+ .Times(1) |
+ .WillOnce(RunClosure(quit_closure)); |
+ |
+ // OnVideoFrameForTesting(video_frame); |
+ OnAudioBusForTesting(*audio_bus1); |
+ run_loop.Run(); |
+ } |
+ |
+ { |
+ base::RunLoop run_loop; |
+ base::Closure quit_closure = run_loop.QuitClosure(); |
+ // The second time around writeData() is called a number of times to write |
+ // the WebM frame header, and then is pinged with the encoded data. |
+ const size_t kSecondEncodedDataSize = 32; |
+ EXPECT_CALL(*this, writeData(_, Lt(kSecondEncodedDataSize), false)) |
+ .Times(AtLeast(1)); |
+ EXPECT_CALL(*this, writeData(_, kSecondEncodedDataSize, false)) |
+ .Times(1) |
+ .WillOnce(RunClosure(quit_closure)); |
+ |
+ // OnVideoFrameForTesting(video_frame); |
+ OnAudioBusForTesting(*audio_bus2); |
+ run_loop.Run(); |
+ } |
+ |
+ media_recorder_handler_->stop(); |
+ |
+ // Expect a last call on destruction, with size 0 and |lastInSlice| true. |
+ EXPECT_CALL(*this, writeData(nullptr, 0, true)).Times(1); |
+ media_recorder_handler_.reset(); |
+} |
+ |
} // namespace content |