| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "media/muxers/webm_muxer.h" |
| 6 |
| 5 #include <stddef.h> | 7 #include <stddef.h> |
| 6 #include <stdint.h> | 8 #include <stdint.h> |
| 7 | 9 |
| 8 #include "base/bind.h" | 10 #include "base/bind.h" |
| 9 #include "base/location.h" | 11 #include "base/location.h" |
| 10 #include "base/macros.h" | 12 #include "base/macros.h" |
| 13 #include "base/memory/ptr_util.h" |
| 11 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
| 12 #include "base/memory/scoped_ptr.h" | |
| 13 #include "media/base/audio_parameters.h" | 15 #include "media/base/audio_parameters.h" |
| 14 #include "media/base/channel_layout.h" | 16 #include "media/base/channel_layout.h" |
| 15 #include "media/base/video_frame.h" | 17 #include "media/base/video_frame.h" |
| 16 #include "media/muxers/webm_muxer.h" | |
| 17 #include "testing/gmock/include/gmock/gmock.h" | 18 #include "testing/gmock/include/gmock/gmock.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
| 19 | 20 |
| 20 using ::testing::_; | 21 using ::testing::_; |
| 21 using ::testing::AllOf; | 22 using ::testing::AllOf; |
| 22 using ::testing::AnyNumber; | 23 using ::testing::AnyNumber; |
| 23 using ::testing::AtLeast; | 24 using ::testing::AtLeast; |
| 24 using ::testing::Eq; | 25 using ::testing::Eq; |
| 25 using ::testing::InSequence; | 26 using ::testing::InSequence; |
| 26 using ::testing::Mock; | 27 using ::testing::Mock; |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 102 const gfx::Size frame_size(160, 80); | 103 const gfx::Size frame_size(160, 80); |
| 103 const scoped_refptr<VideoFrame> video_frame = | 104 const scoped_refptr<VideoFrame> video_frame = |
| 104 VideoFrame::CreateBlackFrame(frame_size); | 105 VideoFrame::CreateBlackFrame(frame_size); |
| 105 const std::string encoded_data("abcdefghijklmnopqrstuvwxyz"); | 106 const std::string encoded_data("abcdefghijklmnopqrstuvwxyz"); |
| 106 | 107 |
| 107 EXPECT_CALL(*this, WriteCallback(_)) | 108 EXPECT_CALL(*this, WriteCallback(_)) |
| 108 .Times(AtLeast(1)) | 109 .Times(AtLeast(1)) |
| 109 .WillRepeatedly( | 110 .WillRepeatedly( |
| 110 WithArgs<0>(Invoke(this, &WebmMuxerTest::SaveEncodedDataLen))); | 111 WithArgs<0>(Invoke(this, &WebmMuxerTest::SaveEncodedDataLen))); |
| 111 webm_muxer_.OnEncodedVideo(video_frame, | 112 webm_muxer_.OnEncodedVideo(video_frame, |
| 112 make_scoped_ptr(new std::string(encoded_data)), | 113 base::WrapUnique(new std::string(encoded_data)), |
| 113 base::TimeTicks::Now(), false /* keyframe */); | 114 base::TimeTicks::Now(), false /* keyframe */); |
| 114 | 115 |
| 115 // First time around WriteCallback() is pinged a number of times to write the | 116 // First time around WriteCallback() is pinged a number of times to write the |
| 116 // Matroska header, but at the end it dumps |encoded_data|. | 117 // Matroska header, but at the end it dumps |encoded_data|. |
| 117 EXPECT_EQ(last_encoded_length_, encoded_data.size()); | 118 EXPECT_EQ(last_encoded_length_, encoded_data.size()); |
| 118 EXPECT_EQ(GetWebmMuxerPosition(), accumulated_position_); | 119 EXPECT_EQ(GetWebmMuxerPosition(), accumulated_position_); |
| 119 EXPECT_GE(GetWebmMuxerPosition(), static_cast<int64_t>(last_encoded_length_)); | 120 EXPECT_GE(GetWebmMuxerPosition(), static_cast<int64_t>(last_encoded_length_)); |
| 120 EXPECT_EQ(GetWebmSegmentMode(), mkvmuxer::Segment::kLive); | 121 EXPECT_EQ(GetWebmSegmentMode(), mkvmuxer::Segment::kLive); |
| 121 | 122 |
| 122 const int64_t begin_of_second_block = accumulated_position_; | 123 const int64_t begin_of_second_block = accumulated_position_; |
| 123 EXPECT_CALL(*this, WriteCallback(_)) | 124 EXPECT_CALL(*this, WriteCallback(_)) |
| 124 .Times(AtLeast(1)) | 125 .Times(AtLeast(1)) |
| 125 .WillRepeatedly( | 126 .WillRepeatedly( |
| 126 WithArgs<0>(Invoke(this, &WebmMuxerTest::SaveEncodedDataLen))); | 127 WithArgs<0>(Invoke(this, &WebmMuxerTest::SaveEncodedDataLen))); |
| 127 webm_muxer_.OnEncodedVideo(video_frame, | 128 webm_muxer_.OnEncodedVideo(video_frame, |
| 128 make_scoped_ptr(new std::string(encoded_data)), | 129 base::WrapUnique(new std::string(encoded_data)), |
| 129 base::TimeTicks::Now(), false /* keyframe */); | 130 base::TimeTicks::Now(), false /* keyframe */); |
| 130 | 131 |
| 131 // The second time around the callbacks should include a SimpleBlock header, | 132 // The second time around the callbacks should include a SimpleBlock header, |
| 132 // namely the track index, a timestamp and a flags byte, for a total of 6B. | 133 // namely the track index, a timestamp and a flags byte, for a total of 6B. |
| 133 EXPECT_EQ(last_encoded_length_, encoded_data.size()); | 134 EXPECT_EQ(last_encoded_length_, encoded_data.size()); |
| 134 EXPECT_EQ(GetWebmMuxerPosition(), accumulated_position_); | 135 EXPECT_EQ(GetWebmMuxerPosition(), accumulated_position_); |
| 135 const uint32_t kSimpleBlockSize = 6u; | 136 const uint32_t kSimpleBlockSize = 6u; |
| 136 EXPECT_EQ(static_cast<int64_t>(begin_of_second_block + kSimpleBlockSize + | 137 EXPECT_EQ(static_cast<int64_t>(begin_of_second_block + kSimpleBlockSize + |
| 137 encoded_data.size()), | 138 encoded_data.size()), |
| 138 accumulated_position_); | 139 accumulated_position_); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 150 media::CHANNEL_LAYOUT_MONO, sample_rate, bits_per_sample, | 151 media::CHANNEL_LAYOUT_MONO, sample_rate, bits_per_sample, |
| 151 frames_per_buffer); | 152 frames_per_buffer); |
| 152 | 153 |
| 153 const std::string encoded_data("abcdefghijklmnopqrstuvwxyz"); | 154 const std::string encoded_data("abcdefghijklmnopqrstuvwxyz"); |
| 154 | 155 |
| 155 EXPECT_CALL(*this, WriteCallback(_)) | 156 EXPECT_CALL(*this, WriteCallback(_)) |
| 156 .Times(AtLeast(1)) | 157 .Times(AtLeast(1)) |
| 157 .WillRepeatedly( | 158 .WillRepeatedly( |
| 158 WithArgs<0>(Invoke(this, &WebmMuxerTest::SaveEncodedDataLen))); | 159 WithArgs<0>(Invoke(this, &WebmMuxerTest::SaveEncodedDataLen))); |
| 159 webm_muxer_.OnEncodedAudio(audio_params, | 160 webm_muxer_.OnEncodedAudio(audio_params, |
| 160 make_scoped_ptr(new std::string(encoded_data)), | 161 base::WrapUnique(new std::string(encoded_data)), |
| 161 base::TimeTicks::Now()); | 162 base::TimeTicks::Now()); |
| 162 | 163 |
| 163 // First time around WriteCallback() is pinged a number of times to write the | 164 // First time around WriteCallback() is pinged a number of times to write the |
| 164 // Matroska header, but at the end it dumps |encoded_data|. | 165 // Matroska header, but at the end it dumps |encoded_data|. |
| 165 EXPECT_EQ(last_encoded_length_, encoded_data.size()); | 166 EXPECT_EQ(last_encoded_length_, encoded_data.size()); |
| 166 EXPECT_EQ(GetWebmMuxerPosition(), accumulated_position_); | 167 EXPECT_EQ(GetWebmMuxerPosition(), accumulated_position_); |
| 167 EXPECT_GE(GetWebmMuxerPosition(), static_cast<int64_t>(last_encoded_length_)); | 168 EXPECT_GE(GetWebmMuxerPosition(), static_cast<int64_t>(last_encoded_length_)); |
| 168 EXPECT_EQ(GetWebmSegmentMode(), mkvmuxer::Segment::kLive); | 169 EXPECT_EQ(GetWebmSegmentMode(), mkvmuxer::Segment::kLive); |
| 169 | 170 |
| 170 const int64_t begin_of_second_block = accumulated_position_; | 171 const int64_t begin_of_second_block = accumulated_position_; |
| 171 EXPECT_CALL(*this, WriteCallback(_)) | 172 EXPECT_CALL(*this, WriteCallback(_)) |
| 172 .Times(AtLeast(1)) | 173 .Times(AtLeast(1)) |
| 173 .WillRepeatedly( | 174 .WillRepeatedly( |
| 174 WithArgs<0>(Invoke(this, &WebmMuxerTest::SaveEncodedDataLen))); | 175 WithArgs<0>(Invoke(this, &WebmMuxerTest::SaveEncodedDataLen))); |
| 175 webm_muxer_.OnEncodedAudio(audio_params, | 176 webm_muxer_.OnEncodedAudio(audio_params, |
| 176 make_scoped_ptr(new std::string(encoded_data)), | 177 base::WrapUnique(new std::string(encoded_data)), |
| 177 base::TimeTicks::Now()); | 178 base::TimeTicks::Now()); |
| 178 | 179 |
| 179 // The second time around the callbacks should include a SimpleBlock header, | 180 // The second time around the callbacks should include a SimpleBlock header, |
| 180 // namely the track index, a timestamp and a flags byte, for a total of 6B. | 181 // namely the track index, a timestamp and a flags byte, for a total of 6B. |
| 181 EXPECT_EQ(last_encoded_length_, encoded_data.size()); | 182 EXPECT_EQ(last_encoded_length_, encoded_data.size()); |
| 182 EXPECT_EQ(GetWebmMuxerPosition(), accumulated_position_); | 183 EXPECT_EQ(GetWebmMuxerPosition(), accumulated_position_); |
| 183 const uint32_t kSimpleBlockSize = 6u; | 184 const uint32_t kSimpleBlockSize = 6u; |
| 184 EXPECT_EQ(static_cast<int64_t>(begin_of_second_block + kSimpleBlockSize + | 185 EXPECT_EQ(static_cast<int64_t>(begin_of_second_block + kSimpleBlockSize + |
| 185 encoded_data.size()), | 186 encoded_data.size()), |
| 186 accumulated_position_); | 187 accumulated_position_); |
| 187 } | 188 } |
| 188 | 189 |
| 189 // This test verifies that when video data comes before audio data, we save the | 190 // This test verifies that when video data comes before audio data, we save the |
| 190 // encoded video frames and add it to the video track when audio data arrives. | 191 // encoded video frames and add it to the video track when audio data arrives. |
| 191 TEST_P(WebmMuxerTest, VideoIsStoredWhileWaitingForAudio) { | 192 TEST_P(WebmMuxerTest, VideoIsStoredWhileWaitingForAudio) { |
| 192 // This test is only relevant if we have both kinds of tracks. | 193 // This test is only relevant if we have both kinds of tracks. |
| 193 if (GetParam().num_video_tracks == 0 || GetParam().num_audio_tracks == 0) | 194 if (GetParam().num_video_tracks == 0 || GetParam().num_audio_tracks == 0) |
| 194 return; | 195 return; |
| 195 | 196 |
| 196 // First send a video keyframe | 197 // First send a video keyframe |
| 197 const gfx::Size frame_size(160, 80); | 198 const gfx::Size frame_size(160, 80); |
| 198 const scoped_refptr<VideoFrame> video_frame = | 199 const scoped_refptr<VideoFrame> video_frame = |
| 199 VideoFrame::CreateBlackFrame(frame_size); | 200 VideoFrame::CreateBlackFrame(frame_size); |
| 200 const std::string encoded_video("thisisanencodedvideopacket"); | 201 const std::string encoded_video("thisisanencodedvideopacket"); |
| 201 webm_muxer_.OnEncodedVideo(video_frame, | 202 webm_muxer_.OnEncodedVideo(video_frame, |
| 202 make_scoped_ptr(new std::string(encoded_video)), | 203 base::WrapUnique(new std::string(encoded_video)), |
| 203 base::TimeTicks::Now(), true /* keyframe */); | 204 base::TimeTicks::Now(), true /* keyframe */); |
| 204 // A few encoded non key frames. | 205 // A few encoded non key frames. |
| 205 const int kNumNonKeyFrames = 2; | 206 const int kNumNonKeyFrames = 2; |
| 206 for (int i = 0; i < kNumNonKeyFrames; ++i) { | 207 for (int i = 0; i < kNumNonKeyFrames; ++i) { |
| 207 webm_muxer_.OnEncodedVideo(video_frame, | 208 webm_muxer_.OnEncodedVideo(video_frame, |
| 208 make_scoped_ptr(new std::string(encoded_video)), | 209 base::WrapUnique(new std::string(encoded_video)), |
| 209 base::TimeTicks::Now(), false /* keyframe */); | 210 base::TimeTicks::Now(), false /* keyframe */); |
| 210 } | 211 } |
| 211 | 212 |
| 212 // Send some audio. The header will be written and muxing will proceed | 213 // Send some audio. The header will be written and muxing will proceed |
| 213 // normally. | 214 // normally. |
| 214 const int sample_rate = 48000; | 215 const int sample_rate = 48000; |
| 215 const int bits_per_sample = 16; | 216 const int bits_per_sample = 16; |
| 216 const int frames_per_buffer = 480; | 217 const int frames_per_buffer = 480; |
| 217 media::AudioParameters audio_params( | 218 media::AudioParameters audio_params( |
| 218 media::AudioParameters::Format::AUDIO_PCM_LOW_LATENCY, | 219 media::AudioParameters::Format::AUDIO_PCM_LOW_LATENCY, |
| 219 media::CHANNEL_LAYOUT_MONO, sample_rate, bits_per_sample, | 220 media::CHANNEL_LAYOUT_MONO, sample_rate, bits_per_sample, |
| 220 frames_per_buffer); | 221 frames_per_buffer); |
| 221 const std::string encoded_audio("thisisanencodedaudiopacket"); | 222 const std::string encoded_audio("thisisanencodedaudiopacket"); |
| 222 | 223 |
| 223 // We should first get the encoded video frames, then the encoded audio frame. | 224 // We should first get the encoded video frames, then the encoded audio frame. |
| 224 Sequence s; | 225 Sequence s; |
| 225 EXPECT_CALL(*this, WriteCallback(Eq(encoded_video))) | 226 EXPECT_CALL(*this, WriteCallback(Eq(encoded_video))) |
| 226 .Times(1 + kNumNonKeyFrames) | 227 .Times(1 + kNumNonKeyFrames) |
| 227 .InSequence(s); | 228 .InSequence(s); |
| 228 EXPECT_CALL(*this, WriteCallback(Eq(encoded_audio))).Times(1).InSequence(s); | 229 EXPECT_CALL(*this, WriteCallback(Eq(encoded_audio))).Times(1).InSequence(s); |
| 229 // We'll also get lots of other header-related stuff. | 230 // We'll also get lots of other header-related stuff. |
| 230 EXPECT_CALL(*this, WriteCallback( | 231 EXPECT_CALL(*this, WriteCallback( |
| 231 AllOf(Not(Eq(encoded_video)), Not(Eq(encoded_audio))))) | 232 AllOf(Not(Eq(encoded_video)), Not(Eq(encoded_audio))))) |
| 232 .Times(AnyNumber()); | 233 .Times(AnyNumber()); |
| 233 webm_muxer_.OnEncodedAudio(audio_params, | 234 webm_muxer_.OnEncodedAudio(audio_params, |
| 234 make_scoped_ptr(new std::string(encoded_audio)), | 235 base::WrapUnique(new std::string(encoded_audio)), |
| 235 base::TimeTicks::Now()); | 236 base::TimeTicks::Now()); |
| 236 } | 237 } |
| 237 | 238 |
| 238 const kTestParams kTestCases[] = { | 239 const kTestParams kTestCases[] = { |
| 239 // TODO: consider not enumerating every combination by hand. | 240 // TODO: consider not enumerating every combination by hand. |
| 240 {kCodecVP8, 1 /* num_video_tracks */, 0 /*num_audio_tracks*/}, | 241 {kCodecVP8, 1 /* num_video_tracks */, 0 /*num_audio_tracks*/}, |
| 241 {kCodecVP8, 0, 1}, | 242 {kCodecVP8, 0, 1}, |
| 242 {kCodecVP8, 1, 1}, | 243 {kCodecVP8, 1, 1}, |
| 243 {kCodecVP9, 1, 0}, | 244 {kCodecVP9, 1, 0}, |
| 244 {kCodecVP9, 0, 1}, | 245 {kCodecVP9, 0, 1}, |
| 245 {kCodecVP9, 1, 1}, | 246 {kCodecVP9, 1, 1}, |
| 246 }; | 247 }; |
| 247 | 248 |
| 248 INSTANTIATE_TEST_CASE_P(, WebmMuxerTest, ValuesIn(kTestCases)); | 249 INSTANTIATE_TEST_CASE_P(, WebmMuxerTest, ValuesIn(kTestCases)); |
| 249 | 250 |
| 250 } // namespace media | 251 } // namespace media |
| OLD | NEW |