| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/cast/test/fake_media_source.h" | 5 #include "media/cast/test/fake_media_source.h" |
| 6 | 6 |
| 7 #include "base/files/memory_mapped_file.h" | 7 #include "base/files/memory_mapped_file.h" |
| 8 #include "base/files/scoped_file.h" | 8 #include "base/files/scoped_file.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/rand_util.h" | |
| 11 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
| 12 #include "media/audio/audio_parameters.h" | 11 #include "media/audio/audio_parameters.h" |
| 13 #include "media/base/audio_buffer.h" | 12 #include "media/base/audio_buffer.h" |
| 14 #include "media/base/audio_bus.h" | 13 #include "media/base/audio_bus.h" |
| 15 #include "media/base/audio_fifo.h" | 14 #include "media/base/audio_fifo.h" |
| 16 #include "media/base/audio_timestamp_helper.h" | 15 #include "media/base/audio_timestamp_helper.h" |
| 17 #include "media/base/media.h" | 16 #include "media/base/media.h" |
| 18 #include "media/base/multi_channel_resampler.h" | 17 #include "media/base/multi_channel_resampler.h" |
| 19 #include "media/base/video_frame.h" | 18 #include "media/base/video_frame.h" |
| 20 #include "media/base/video_util.h" | 19 #include "media/base/video_util.h" |
| 21 #include "media/cast/cast_sender.h" | 20 #include "media/cast/cast_sender.h" |
| 22 #include "media/cast/test/utility/audio_utility.h" | 21 #include "media/cast/test/utility/audio_utility.h" |
| 23 #include "media/cast/test/utility/video_utility.h" | 22 #include "media/cast/test/utility/video_utility.h" |
| 24 #include "media/ffmpeg/ffmpeg_common.h" | 23 #include "media/ffmpeg/ffmpeg_common.h" |
| 25 #include "media/ffmpeg/ffmpeg_deleters.h" | 24 #include "media/ffmpeg/ffmpeg_deleters.h" |
| 26 #include "media/filters/audio_renderer_algorithm.h" | 25 #include "media/filters/audio_renderer_algorithm.h" |
| 27 #include "media/filters/ffmpeg_demuxer.h" | 26 #include "media/filters/ffmpeg_demuxer.h" |
| 28 #include "media/filters/ffmpeg_glue.h" | 27 #include "media/filters/ffmpeg_glue.h" |
| 29 #include "media/filters/in_memory_url_protocol.h" | 28 #include "media/filters/in_memory_url_protocol.h" |
| 30 #include "ui/gfx/geometry/size.h" | 29 #include "ui/gfx/geometry/size.h" |
| 31 | 30 |
| 32 namespace { | 31 namespace { |
| 33 | 32 |
| 34 static const int kAudioChannels = 2; | 33 static const int kAudioChannels = 2; |
| 35 static const int kAudioSamplingFrequency = 48000; | 34 static const int kAudioSamplingFrequency = 48000; |
| 36 static const int kSoundFrequency = 440; // Frequency of sinusoid wave. | 35 static const int kSoundFrequency = 1234; // Frequency of sinusoid wave. |
| 37 static const float kSoundVolume = 0.10f; | 36 static const float kSoundVolume = 0.5f; |
| 38 static const int kAudioFrameMs = 10; // Each audio frame is exactly 10ms. | 37 static const int kAudioFrameMs = 10; // Each audio frame is exactly 10ms. |
| 39 static const int kAudioPacketsPerSecond = 1000 / kAudioFrameMs; | 38 static const int kAudioPacketsPerSecond = 1000 / kAudioFrameMs; |
| 40 | 39 |
| 41 // Bounds for variable frame size mode. | |
| 42 static const int kMinFakeFrameWidth = 60; | |
| 43 static const int kMinFakeFrameHeight = 34; | |
| 44 static const int kStartingFakeFrameWidth = 854; | |
| 45 static const int kStartingFakeFrameHeight = 480; | |
| 46 static const int kMaxFakeFrameWidth = 1280; | |
| 47 static const int kMaxFakeFrameHeight = 720; | |
| 48 static const int kMaxFrameSizeChangeMillis = 5000; | |
| 49 | |
| 50 void AVFreeFrame(AVFrame* frame) { | 40 void AVFreeFrame(AVFrame* frame) { |
| 51 av_frame_free(&frame); | 41 av_frame_free(&frame); |
| 52 } | 42 } |
| 53 | 43 |
| 54 base::TimeDelta PtsToTimeDelta(int64 pts, const AVRational& time_base) { | 44 base::TimeDelta PtsToTimeDelta(int64 pts, const AVRational& time_base) { |
| 55 return pts * base::TimeDelta::FromSeconds(1) * time_base.num / time_base.den; | 45 return pts * base::TimeDelta::FromSeconds(1) * time_base.num / time_base.den; |
| 56 } | 46 } |
| 57 | 47 |
| 58 int64 TimeDeltaToPts(base::TimeDelta delta, const AVRational& time_base) { | 48 int64 TimeDeltaToPts(base::TimeDelta delta, const AVRational& time_base) { |
| 59 return static_cast<int64>( | 49 return static_cast<int64>( |
| 60 delta.InSecondsF() * time_base.den / time_base.num + | 50 delta.InSecondsF() * time_base.den / time_base.num + |
| 61 0.5 /* rounding */); | 51 0.5 /* rounding */); |
| 62 } | 52 } |
| 63 | 53 |
| 64 } // namespace | 54 } // namespace |
| 65 | 55 |
| 66 namespace media { | 56 namespace media { |
| 67 namespace cast { | 57 namespace cast { |
| 68 | 58 |
| 69 FakeMediaSource::FakeMediaSource( | 59 FakeMediaSource::FakeMediaSource( |
| 70 scoped_refptr<base::SingleThreadTaskRunner> task_runner, | 60 scoped_refptr<base::SingleThreadTaskRunner> task_runner, |
| 71 base::TickClock* clock, | 61 base::TickClock* clock, |
| 72 const VideoSenderConfig& video_config, | 62 const VideoSenderConfig& video_config, |
| 73 bool keep_frames) | 63 bool keep_frames) |
| 74 : task_runner_(task_runner), | 64 : task_runner_(task_runner), |
| 75 video_config_(video_config), | 65 video_config_(video_config), |
| 76 keep_frames_(keep_frames), | 66 keep_frames_(keep_frames), |
| 77 variable_frame_size_mode_(false), | |
| 78 synthetic_count_(0), | 67 synthetic_count_(0), |
| 79 clock_(clock), | 68 clock_(clock), |
| 80 audio_frame_count_(0), | 69 audio_frame_count_(0), |
| 81 video_frame_count_(0), | 70 video_frame_count_(0), |
| 82 av_format_context_(NULL), | 71 av_format_context_(NULL), |
| 83 audio_stream_index_(-1), | 72 audio_stream_index_(-1), |
| 84 playback_rate_(1.0), | 73 playback_rate_(1.0), |
| 85 video_stream_index_(-1), | 74 video_stream_index_(-1), |
| 86 video_frame_rate_numerator_(video_config.max_frame_rate), | 75 video_frame_rate_numerator_(video_config.max_frame_rate), |
| 87 video_frame_rate_denominator_(1), | 76 video_frame_rate_denominator_(1), |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 193 } | 182 } |
| 194 LOG(INFO) << "Source file has video."; | 183 LOG(INFO) << "Source file has video."; |
| 195 } else { | 184 } else { |
| 196 LOG(ERROR) << "Unknown stream type; ignore."; | 185 LOG(ERROR) << "Unknown stream type; ignore."; |
| 197 } | 186 } |
| 198 } | 187 } |
| 199 | 188 |
| 200 Rewind(); | 189 Rewind(); |
| 201 } | 190 } |
| 202 | 191 |
| 203 void FakeMediaSource::SetVariableFrameSizeMode(bool enabled) { | |
| 204 variable_frame_size_mode_ = enabled; | |
| 205 } | |
| 206 | |
| 207 void FakeMediaSource::Start(scoped_refptr<AudioFrameInput> audio_frame_input, | 192 void FakeMediaSource::Start(scoped_refptr<AudioFrameInput> audio_frame_input, |
| 208 scoped_refptr<VideoFrameInput> video_frame_input) { | 193 scoped_refptr<VideoFrameInput> video_frame_input) { |
| 209 audio_frame_input_ = audio_frame_input; | 194 audio_frame_input_ = audio_frame_input; |
| 210 video_frame_input_ = video_frame_input; | 195 video_frame_input_ = video_frame_input; |
| 211 | 196 |
| 212 LOG(INFO) << "Max Frame rate: " << video_config_.max_frame_rate; | 197 LOG(INFO) << "Max Frame rate: " << video_config_.max_frame_rate; |
| 213 LOG(INFO) << "Source Frame rate: " | 198 LOG(INFO) << "Source Frame rate: " |
| 214 << video_frame_rate_numerator_ << "/" | 199 << video_frame_rate_numerator_ << "/" |
| 215 << video_frame_rate_denominator_ << " fps."; | 200 << video_frame_rate_denominator_ << " fps."; |
| 216 LOG(INFO) << "Audio playback rate: " << playback_rate_; | 201 LOG(INFO) << "Audio playback rate: " << playback_rate_; |
| 217 | 202 |
| 218 if (start_time_.is_null()) | 203 if (start_time_.is_null()) |
| 219 start_time_ = clock_->NowTicks(); | 204 start_time_ = clock_->NowTicks(); |
| 220 | 205 |
| 221 if (!is_transcoding_audio() && !is_transcoding_video()) { | 206 if (!is_transcoding_audio() && !is_transcoding_video()) { |
| 222 // Send fake patterns. | 207 // Send fake patterns. |
| 223 task_runner_->PostTask( | 208 task_runner_->PostTask( |
| 224 FROM_HERE, | 209 FROM_HERE, |
| 225 base::Bind(&FakeMediaSource::SendNextFakeFrame, | 210 base::Bind( |
| 226 weak_factory_.GetWeakPtr())); | 211 &FakeMediaSource::SendNextFakeFrame, |
| 212 base::Unretained(this))); |
| 227 return; | 213 return; |
| 228 } | 214 } |
| 229 | 215 |
| 230 // Send transcoding streams. | 216 // Send transcoding streams. |
| 231 audio_algo_.Initialize(audio_params_); | 217 audio_algo_.Initialize(audio_params_); |
| 232 audio_algo_.FlushBuffers(); | 218 audio_algo_.FlushBuffers(); |
| 233 audio_fifo_input_bus_ = | 219 audio_fifo_input_bus_ = |
| 234 AudioBus::Create( | 220 AudioBus::Create( |
| 235 audio_params_.channels(), audio_params_.frames_per_buffer()); | 221 audio_params_.channels(), audio_params_.frames_per_buffer()); |
| 236 // Audio FIFO can carry all data fron AudioRendererAlgorithm. | 222 // Audio FIFO can carry all data fron AudioRendererAlgorithm. |
| 237 audio_fifo_.reset( | 223 audio_fifo_.reset( |
| 238 new AudioFifo(audio_params_.channels(), | 224 new AudioFifo(audio_params_.channels(), |
| 239 audio_algo_.QueueCapacity())); | 225 audio_algo_.QueueCapacity())); |
| 240 audio_resampler_.reset(new media::MultiChannelResampler( | 226 audio_resampler_.reset(new media::MultiChannelResampler( |
| 241 audio_params_.channels(), | 227 audio_params_.channels(), |
| 242 static_cast<double>(audio_params_.sample_rate()) / | 228 static_cast<double>(audio_params_.sample_rate()) / |
| 243 kAudioSamplingFrequency, | 229 kAudioSamplingFrequency, |
| 244 audio_params_.frames_per_buffer(), | 230 audio_params_.frames_per_buffer(), |
| 245 base::Bind(&FakeMediaSource::ProvideData, weak_factory_.GetWeakPtr()))); | 231 base::Bind(&FakeMediaSource::ProvideData, base::Unretained(this)))); |
| 246 task_runner_->PostTask( | 232 task_runner_->PostTask( |
| 247 FROM_HERE, | 233 FROM_HERE, |
| 248 base::Bind(&FakeMediaSource::SendNextFrame, weak_factory_.GetWeakPtr())); | 234 base::Bind( |
| 235 &FakeMediaSource::SendNextFrame, |
| 236 base::Unretained(this))); |
| 249 } | 237 } |
| 250 | 238 |
| 251 void FakeMediaSource::SendNextFakeFrame() { | 239 void FakeMediaSource::SendNextFakeFrame() { |
| 252 UpdateNextFrameSize(); | 240 gfx::Size size(video_config_.width, video_config_.height); |
| 253 scoped_refptr<VideoFrame> video_frame = | 241 scoped_refptr<VideoFrame> video_frame = |
| 254 VideoFrame::CreateBlackFrame(current_frame_size_); | 242 VideoFrame::CreateBlackFrame(size); |
| 255 PopulateVideoFrame(video_frame.get(), synthetic_count_); | 243 PopulateVideoFrame(video_frame.get(), synthetic_count_); |
| 256 ++synthetic_count_; | 244 ++synthetic_count_; |
| 257 | 245 |
| 258 const base::TimeTicks now = clock_->NowTicks(); | 246 const base::TimeTicks now = clock_->NowTicks(); |
| 259 | 247 |
| 260 base::TimeDelta video_time = VideoFrameTime(++video_frame_count_); | 248 base::TimeDelta video_time = VideoFrameTime(++video_frame_count_); |
| 261 video_frame->set_timestamp(video_time); | 249 video_frame->set_timestamp(video_time); |
| 262 if (keep_frames_) | 250 if (keep_frames_) |
| 263 inserted_video_frame_queue_.push(video_frame); | 251 inserted_video_frame_queue_.push(video_frame); |
| 264 video_frame_input_->InsertRawVideoFrame(video_frame, | 252 video_frame_input_->InsertRawVideoFrame(video_frame, |
| (...skipping 28 matching lines...) Expand all Loading... |
| 293 video_time = VideoFrameTime(++video_frame_count_); | 281 video_time = VideoFrameTime(++video_frame_count_); |
| 294 } | 282 } |
| 295 | 283 |
| 296 task_runner_->PostDelayedTask( | 284 task_runner_->PostDelayedTask( |
| 297 FROM_HERE, | 285 FROM_HERE, |
| 298 base::Bind(&FakeMediaSource::SendNextFakeFrame, | 286 base::Bind(&FakeMediaSource::SendNextFakeFrame, |
| 299 weak_factory_.GetWeakPtr()), | 287 weak_factory_.GetWeakPtr()), |
| 300 video_time - elapsed_time); | 288 video_time - elapsed_time); |
| 301 } | 289 } |
| 302 | 290 |
| 303 void FakeMediaSource::UpdateNextFrameSize() { | |
| 304 if (variable_frame_size_mode_) { | |
| 305 bool update_size_change_time = false; | |
| 306 if (current_frame_size_.IsEmpty()) { | |
| 307 current_frame_size_ = gfx::Size(kStartingFakeFrameWidth, | |
| 308 kStartingFakeFrameHeight); | |
| 309 update_size_change_time = true; | |
| 310 } else if (clock_->NowTicks() >= next_frame_size_change_time_) { | |
| 311 current_frame_size_ = gfx::Size( | |
| 312 base::RandInt(kMinFakeFrameWidth, kMaxFakeFrameWidth), | |
| 313 base::RandInt(kMinFakeFrameHeight, kMaxFakeFrameHeight)); | |
| 314 update_size_change_time = true; | |
| 315 } | |
| 316 | |
| 317 if (update_size_change_time) { | |
| 318 next_frame_size_change_time_ = clock_->NowTicks() + | |
| 319 base::TimeDelta::FromMillisecondsD( | |
| 320 base::RandDouble() * kMaxFrameSizeChangeMillis); | |
| 321 } | |
| 322 } else { | |
| 323 current_frame_size_ = gfx::Size(kStartingFakeFrameWidth, | |
| 324 kStartingFakeFrameHeight); | |
| 325 next_frame_size_change_time_ = base::TimeTicks(); | |
| 326 } | |
| 327 } | |
| 328 | |
| 329 bool FakeMediaSource::SendNextTranscodedVideo(base::TimeDelta elapsed_time) { | 291 bool FakeMediaSource::SendNextTranscodedVideo(base::TimeDelta elapsed_time) { |
| 330 if (!is_transcoding_video()) | 292 if (!is_transcoding_video()) |
| 331 return false; | 293 return false; |
| 332 | 294 |
| 333 Decode(false); | 295 Decode(false); |
| 334 if (video_frame_queue_.empty()) | 296 if (video_frame_queue_.empty()) |
| 335 return false; | 297 return false; |
| 336 | 298 |
| 337 const scoped_refptr<VideoFrame> video_frame = video_frame_queue_.front(); | 299 scoped_refptr<VideoFrame> decoded_frame = |
| 338 if (elapsed_time < video_frame->timestamp()) | 300 video_frame_queue_.front(); |
| 301 if (elapsed_time < decoded_frame->timestamp()) |
| 339 return false; | 302 return false; |
| 303 |
| 304 gfx::Size size(video_config_.width, video_config_.height); |
| 305 scoped_refptr<VideoFrame> video_frame = |
| 306 VideoFrame::CreateBlackFrame(size); |
| 340 video_frame_queue_.pop(); | 307 video_frame_queue_.pop(); |
| 308 media::CopyPlane(VideoFrame::kYPlane, |
| 309 decoded_frame->data(VideoFrame::kYPlane), |
| 310 decoded_frame->stride(VideoFrame::kYPlane), |
| 311 decoded_frame->rows(VideoFrame::kYPlane), |
| 312 video_frame.get()); |
| 313 media::CopyPlane(VideoFrame::kUPlane, |
| 314 decoded_frame->data(VideoFrame::kUPlane), |
| 315 decoded_frame->stride(VideoFrame::kUPlane), |
| 316 decoded_frame->rows(VideoFrame::kUPlane), |
| 317 video_frame.get()); |
| 318 media::CopyPlane(VideoFrame::kVPlane, |
| 319 decoded_frame->data(VideoFrame::kVPlane), |
| 320 decoded_frame->stride(VideoFrame::kVPlane), |
| 321 decoded_frame->rows(VideoFrame::kVPlane), |
| 322 video_frame.get()); |
| 341 | 323 |
| 342 // Use the timestamp from the file if we're transcoding. | 324 // Use the timestamp from the file if we're transcoding. |
| 343 video_frame->set_timestamp(ScaleTimestamp(video_frame->timestamp())); | 325 video_frame->set_timestamp(ScaleTimestamp(decoded_frame->timestamp())); |
| 344 if (keep_frames_) | 326 if (keep_frames_) |
| 345 inserted_video_frame_queue_.push(video_frame); | 327 inserted_video_frame_queue_.push(video_frame); |
| 346 video_frame_input_->InsertRawVideoFrame( | 328 video_frame_input_->InsertRawVideoFrame( |
| 347 video_frame, start_time_ + video_frame->timestamp()); | 329 video_frame, start_time_ + video_frame->timestamp()); |
| 348 | 330 |
| 349 // Make sure queue is not empty. | 331 // Make sure queue is not empty. |
| 350 Decode(false); | 332 Decode(false); |
| 351 return true; | 333 return true; |
| 352 } | 334 } |
| 353 | 335 |
| (...skipping 30 matching lines...) Expand all Loading... |
| 384 if (audio_bus_queue_.empty() && video_frame_queue_.empty()) { | 366 if (audio_bus_queue_.empty() && video_frame_queue_.empty()) { |
| 385 // Both queues are empty can only mean that we have reached | 367 // Both queues are empty can only mean that we have reached |
| 386 // the end of the stream. | 368 // the end of the stream. |
| 387 LOG(INFO) << "Rewind."; | 369 LOG(INFO) << "Rewind."; |
| 388 Rewind(); | 370 Rewind(); |
| 389 } | 371 } |
| 390 | 372 |
| 391 // Send next send. | 373 // Send next send. |
| 392 task_runner_->PostDelayedTask( | 374 task_runner_->PostDelayedTask( |
| 393 FROM_HERE, | 375 FROM_HERE, |
| 394 base::Bind(&FakeMediaSource::SendNextFrame, weak_factory_.GetWeakPtr()), | 376 base::Bind( |
| 377 &FakeMediaSource::SendNextFrame, |
| 378 base::Unretained(this)), |
| 395 base::TimeDelta::FromMilliseconds(kAudioFrameMs)); | 379 base::TimeDelta::FromMilliseconds(kAudioFrameMs)); |
| 396 } | 380 } |
| 397 | 381 |
| 398 base::TimeDelta FakeMediaSource::VideoFrameTime(int frame_number) { | 382 base::TimeDelta FakeMediaSource::VideoFrameTime(int frame_number) { |
| 399 return frame_number * base::TimeDelta::FromSeconds(1) * | 383 return frame_number * base::TimeDelta::FromSeconds(1) * |
| 400 video_frame_rate_denominator_ / video_frame_rate_numerator_; | 384 video_frame_rate_denominator_ / video_frame_rate_numerator_; |
| 401 } | 385 } |
| 402 | 386 |
| 403 base::TimeDelta FakeMediaSource::ScaleTimestamp(base::TimeDelta timestamp) { | 387 base::TimeDelta FakeMediaSource::ScaleTimestamp(base::TimeDelta timestamp) { |
| 404 return base::TimeDelta::FromSecondsD(timestamp.InSecondsF() / playback_rate_); | 388 return base::TimeDelta::FromSecondsD(timestamp.InSecondsF() / playback_rate_); |
| (...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 618 AVCodecContext* FakeMediaSource::av_audio_context() { | 602 AVCodecContext* FakeMediaSource::av_audio_context() { |
| 619 return av_audio_stream()->codec; | 603 return av_audio_stream()->codec; |
| 620 } | 604 } |
| 621 | 605 |
| 622 AVCodecContext* FakeMediaSource::av_video_context() { | 606 AVCodecContext* FakeMediaSource::av_video_context() { |
| 623 return av_video_stream()->codec; | 607 return av_video_stream()->codec; |
| 624 } | 608 } |
| 625 | 609 |
| 626 } // namespace cast | 610 } // namespace cast |
| 627 } // namespace media | 611 } // namespace media |
| OLD | NEW |