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..f15c01c5901cd4f336ebe6988ccc34aef254d535 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,26 +34,42 @@ 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"; |
+static const int kTestAudioChannels = 2; |
+static const int kTestAudioBitsPerSample = 16; |
+static const int kTestAudioSampleRate = 48000; |
+static const int kTestAudioBufferDurationMS = 60; |
struct MediaRecorderTestParams { |
+ const bool has_video; |
+ const bool has_audio; |
const char* const mime_type; |
- const size_t first_encoded_frame_size; |
- const size_t second_encoded_frame_size; |
+ const size_t first_encoded_video_frame_size; |
+ const size_t second_encoded_video_frame_size; |
+ const size_t first_encoded_audio_frame_size; |
+ const size_t second_encoded_audio_frame_size; |
}; |
static const MediaRecorderTestParams kMediaRecorderTestParams[] = { |
- {"video/vp8", 52, 32}, |
- {"video/vp9", 33, 18}}; |
+ {true, false, "video/vp8", 52, 32, 0, 0}, |
+ {true, false, "video/vp9", 33, 18, 0, 0}, |
+ {false, true, "video/vp8", 0, 0, 990, 706}}; |
class MediaRecorderHandlerTest : public TestWithParam<MediaRecorderTestParams>, |
public blink::WebMediaRecorderHandlerClient { |
public: |
MediaRecorderHandlerTest() |
- : media_recorder_handler_(new MediaRecorderHandler()) { |
+ : media_recorder_handler_(new MediaRecorderHandler()), |
+ audio_source_(kTestAudioChannels, |
+ 440 /* freq */, |
+ kTestAudioSampleRate) { |
EXPECT_FALSE(media_recorder_handler_->recording_); |
registry_.Init(kTestStreamUrl); |
- registry_.AddVideoTrack(kTestVideoTrackId); |
+ if (GetParam().has_video) |
+ registry_.AddVideoTrack(kTestVideoTrackId); |
+ if (GetParam().has_audio) |
+ registry_.AddAudioTrack(kTestAudioTrackId); |
} |
~MediaRecorderHandlerTest() { |
@@ -68,11 +86,29 @@ 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()); |
+ } |
+ void SetAudioFormatForTesting(const media::AudioParameters& params) { |
+ media_recorder_handler_->SetAudioFormatForTesting(params); |
+ } |
+ |
+ scoped_ptr<media::AudioBus> NextAudioBus() { |
+ scoped_ptr<media::AudioBus> bus(media::AudioBus::Create( |
+ kTestAudioChannels, |
+ kTestAudioSampleRate * kTestAudioBufferDurationMS / 1000)); |
+ 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,19 +119,25 @@ 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); |
}; |
// Checks that canSupportMimeType() works as expected. |
// TODO(mcasas): revisit this when canSupportMimeType() is fully implemented. |
-TEST_F(MediaRecorderHandlerTest, CanSupportMimeType) { |
+TEST_P(MediaRecorderHandlerTest, CanSupportMimeType) { |
mcasas
2015/11/19 00:49:52
Hmm this should not be parameterised, right?
Keep
ajose
2015/11/19 22:17:19
Done.
|
const WebString good_mime_type_vp8(base::UTF8ToUTF16("video/vp8")); |
EXPECT_TRUE(media_recorder_handler_->canSupportMimeType(good_mime_type_vp8)); |
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 +153,25 @@ 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()); |
+ |
+ if (GetParam().has_video) |
+ EXPECT_TRUE(hasVideoRecorders()); |
+ else |
+ EXPECT_FALSE(hasVideoRecorders()); |
mcasas
2015/11/19 00:49:52
EXPECT_TRUE(hasVideoRecorders() || !GetParam().has
ajose
2015/11/19 22:17:19
Done.
|
+ |
+ if (GetParam().has_audio) |
+ EXPECT_TRUE(hasAudioRecorders()); |
+ else |
+ EXPECT_FALSE(hasAudioRecorders()); |
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); |
@@ -127,6 +180,11 @@ TEST_P(MediaRecorderHandlerTest, InitializeStartStop) { |
// Sends 2 frames and expect them as WebM contained encoded data in writeData(). |
TEST_P(MediaRecorderHandlerTest, EncodeVideoFrames) { |
+ // Video-only test. |
+ if (GetParam().has_audio) { |
mcasas
2015/11/19 00:49:52
no {}
ajose
2015/11/19 22:17:19
Done.
|
+ return; |
+ } |
+ |
const WebString mime_type(base::UTF8ToUTF16(GetParam().mime_type)); |
EXPECT_TRUE(media_recorder_handler_->initialize(this, registry_.test_stream(), |
mime_type)); |
@@ -141,7 +199,7 @@ TEST_P(MediaRecorderHandlerTest, EncodeVideoFrames) { |
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 encoded_data_size = GetParam().first_encoded_frame_size; |
+ const size_t encoded_data_size = GetParam().first_encoded_video_frame_size; |
EXPECT_CALL(*this, writeData(_, Lt(encoded_data_size), _)) |
.Times(AtLeast(1)); |
EXPECT_CALL(*this, writeData(_, encoded_data_size, _)) |
@@ -157,7 +215,7 @@ TEST_P(MediaRecorderHandlerTest, EncodeVideoFrames) { |
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 encoded_data_size = GetParam().second_encoded_frame_size; |
+ const size_t encoded_data_size = GetParam().second_encoded_video_frame_size; |
EXPECT_CALL(*this, writeData(_, Lt(encoded_data_size), _)) |
.Times(AtLeast(1)); |
EXPECT_CALL(*this, writeData(_, encoded_data_size, _)) |
@@ -179,4 +237,65 @@ INSTANTIATE_TEST_CASE_P(, |
MediaRecorderHandlerTest, |
ValuesIn(kMediaRecorderTestParams)); |
+// Sends 2 frames and expect them as WebM contained encoded data in writeData(). |
+TEST_P(MediaRecorderHandlerTest, EncodeAudioFrames) { |
+ // Audio-only test. |
+ if (GetParam().has_video) { |
+ return; |
+ } |
mcasas
2015/11/19 00:49:52
no {}
ajose
2015/11/19 22:17:19
Done.
|
+ |
+ 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_ptr<media::AudioBus> audio_bus1 = NextAudioBus(); |
+ const scoped_ptr<media::AudioBus> audio_bus2 = NextAudioBus(); |
+ |
+ media::AudioParameters params( |
+ media::AudioParameters::AUDIO_PCM_LINEAR, media::CHANNEL_LAYOUT_STEREO, |
+ kTestAudioSampleRate, kTestAudioBitsPerSample, |
+ kTestAudioSampleRate * kTestAudioBufferDurationMS / 1000); |
+ SetAudioFormatForTesting(params); |
+ |
+ { |
+ 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 = GetParam().first_encoded_audio_frame_size; |
+ EXPECT_CALL(*this, writeData(_, Lt(kEncodedDataSize), _)).Times(AtLeast(1)); |
+ EXPECT_CALL(*this, writeData(_, kEncodedDataSize, _)) |
+ .Times(1) |
+ .WillOnce(RunClosure(quit_closure)); |
+ |
+ 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 = |
+ GetParam().second_encoded_audio_frame_size; |
+ EXPECT_CALL(*this, writeData(_, Lt(kSecondEncodedDataSize), _)) |
+ .Times(AtLeast(1)); |
+ EXPECT_CALL(*this, writeData(_, kSecondEncodedDataSize, _)) |
+ .Times(1) |
+ .WillOnce(RunClosure(quit_closure)); |
+ |
+ 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 |