Chromium Code Reviews| 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_video_encode_accelerator.h" | 5 #include "media/video/fake_video_encode_accelerator.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/location.h" | 8 #include "base/location.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/message_loop/message_loop_proxy.h" | |
| 10 #include "base/single_thread_task_runner.h" | 11 #include "base/single_thread_task_runner.h" |
| 11 | 12 |
| 12 namespace media { | 13 namespace media { |
| 13 namespace cast { | |
| 14 namespace test { | |
| 15 | 14 |
| 16 static const unsigned int kMinimumInputCount = 1; | 15 static const unsigned int kMinimumInputCount = 1; |
| 17 static const size_t kMinimumOutputBufferSize = 123456; | 16 static const size_t kMinimumOutputBufferSize = 123456; |
| 18 | 17 |
| 18 struct FakeVideoEncodeAccelerator::BitstreamBufferRef { | |
| 19 BitstreamBufferRef(int id, scoped_ptr<base::SharedMemory> shm, size_t size) | |
| 20 : id(id), shm(shm.Pass()), size(size) {} | |
| 21 const int id; | |
| 22 const scoped_ptr<base::SharedMemory> shm; | |
| 23 const size_t size; | |
| 24 }; | |
| 25 | |
| 19 FakeVideoEncodeAccelerator::FakeVideoEncodeAccelerator( | 26 FakeVideoEncodeAccelerator::FakeVideoEncodeAccelerator( |
| 20 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, | 27 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner) |
| 21 std::vector<uint32>* stored_bitrates) | |
| 22 : task_runner_(task_runner), | 28 : task_runner_(task_runner), |
| 23 stored_bitrates_(stored_bitrates), | 29 will_initialization_succeed_(true), |
| 24 client_(NULL), | 30 client_(NULL), |
| 25 first_(true), | 31 first_(true), |
| 26 will_initialization_succeed_(true), | 32 weak_this_factory_(this) {} |
| 27 weak_this_factory_(this) { | |
| 28 DCHECK(stored_bitrates_); | |
| 29 } | |
| 30 | 33 |
| 31 FakeVideoEncodeAccelerator::~FakeVideoEncodeAccelerator() { | 34 FakeVideoEncodeAccelerator::~FakeVideoEncodeAccelerator() { |
| 32 weak_this_factory_.InvalidateWeakPtrs(); | 35 weak_this_factory_.InvalidateWeakPtrs(); |
| 33 } | 36 } |
| 34 | 37 |
| 35 std::vector<VideoEncodeAccelerator::SupportedProfile> | 38 std::vector<VideoEncodeAccelerator::SupportedProfile> |
| 36 FakeVideoEncodeAccelerator::GetSupportedProfiles() { | 39 FakeVideoEncodeAccelerator::GetSupportedProfiles() { |
| 37 return std::vector<VideoEncodeAccelerator::SupportedProfile>(); | 40 return std::vector<VideoEncodeAccelerator::SupportedProfile>(); |
| 38 } | 41 } |
| 39 | 42 |
| 40 bool FakeVideoEncodeAccelerator::Initialize( | 43 bool FakeVideoEncodeAccelerator::Initialize( |
| 41 media::VideoFrame::Format input_format, | 44 VideoFrame::Format input_format, |
| 42 const gfx::Size& input_visible_size, | 45 const gfx::Size& input_visible_size, |
| 43 VideoCodecProfile output_profile, | 46 VideoCodecProfile output_profile, |
| 44 uint32 initial_bitrate, | 47 uint32 initial_bitrate, |
| 45 Client* client) { | 48 Client* client) { |
| 46 if (!will_initialization_succeed_) | 49 if (!will_initialization_succeed_) { |
| 47 return false; | |
| 48 client_ = client; | |
| 49 if (output_profile != media::VP8PROFILE_ANY && | |
| 50 output_profile != media::H264PROFILE_MAIN) { | |
| 51 return false; | 50 return false; |
| 52 } | 51 } |
| 52 if (output_profile == VIDEO_CODEC_PROFILE_UNKNOWN || | |
| 53 output_profile > VIDEO_CODEC_PROFILE_MAX) { | |
| 54 return false; | |
| 55 } | |
| 56 client_ = client; | |
| 53 task_runner_->PostTask( | 57 task_runner_->PostTask( |
| 54 FROM_HERE, | 58 FROM_HERE, |
| 55 base::Bind(&FakeVideoEncodeAccelerator::DoRequireBitstreamBuffers, | 59 base::Bind(&FakeVideoEncodeAccelerator::DoRequireBitstreamBuffers, |
| 56 weak_this_factory_.GetWeakPtr(), | 60 weak_this_factory_.GetWeakPtr(), |
| 57 kMinimumInputCount, | 61 kMinimumInputCount, |
| 58 input_visible_size, | 62 input_visible_size, |
| 59 kMinimumOutputBufferSize)); | 63 kMinimumOutputBufferSize)); |
| 60 return true; | 64 return true; |
| 61 } | 65 } |
| 62 | 66 |
| 63 void FakeVideoEncodeAccelerator::Encode(const scoped_refptr<VideoFrame>& frame, | 67 void FakeVideoEncodeAccelerator::Encode( |
| 64 bool force_keyframe) { | 68 const scoped_refptr<VideoFrame>& frame, |
| 69 bool force_keyframe) { | |
| 65 DCHECK(client_); | 70 DCHECK(client_); |
| 66 DCHECK(!available_buffer_ids_.empty()); | 71 DCHECK(!available_buffers_.empty()); |
| 67 | 72 |
| 68 // Fake that we have encoded the frame; resulting in using the full output | 73 // Create fake encoded frame by just signaling that the buffer contains |
| 69 // buffer. | 74 // data. Note that the only thing written to the buffer is whether it is |
| 70 int32 id = available_buffer_ids_.front(); | 75 // a key frame or not. |
| 71 available_buffer_ids_.pop_front(); | 76 BitstreamBufferRef* buffer = available_buffers_.front().get(); |
| 77 int32 id = buffer->id; | |
| 78 bool is_key_frame = first_ || force_keyframe; | |
| 79 first_ = false; | |
| 72 | 80 |
| 73 bool is_key_fame = force_keyframe; | 81 uint8* stream = static_cast<uint8*>(buffer->shm->memory()); |
| 74 if (first_) { | 82 stream[0] = is_key_frame ? 0x00 : 0x01; // Key frame scheme = VP8 |
|
wuchengli
2014/12/11 02:14:31
Can remove this because is_key_frame is communicat
hellner1
2014/12/11 19:34:19
Indeed. I forgot to remove it. Thanks for the ping
| |
| 75 is_key_fame = true; | 83 DoBitstreamBufferReady(id, |
| 76 first_ = false; | 84 kMinimumOutputBufferSize, |
| 77 } | 85 is_key_frame); |
| 78 task_runner_->PostTask( | 86 available_buffers_.pop_front(); |
| 79 FROM_HERE, | |
| 80 base::Bind(&FakeVideoEncodeAccelerator::DoBitstreamBufferReady, | |
| 81 weak_this_factory_.GetWeakPtr(), | |
| 82 id, | |
| 83 kMinimumOutputBufferSize, | |
| 84 is_key_fame)); | |
| 85 } | 87 } |
| 86 | 88 |
| 87 void FakeVideoEncodeAccelerator::UseOutputBitstreamBuffer( | 89 void FakeVideoEncodeAccelerator::UseOutputBitstreamBuffer( |
| 88 const BitstreamBuffer& buffer) { | 90 const BitstreamBuffer& buffer) { |
| 89 available_buffer_ids_.push_back(buffer.id()); | 91 scoped_ptr<base::SharedMemory> shm( |
| 92 new base::SharedMemory(buffer.handle(), false)); | |
| 93 if (!shm->Map(buffer.size())) { | |
| 94 LOG(ERROR) << "Shared memory failure"; | |
| 95 return; | |
| 96 } | |
| 97 available_buffers_.push_back( | |
| 98 scoped_ptr<BitstreamBufferRef>( | |
| 99 new BitstreamBufferRef(buffer.id(), shm.Pass(), buffer.size()))); | |
| 90 } | 100 } |
| 91 | 101 |
| 92 void FakeVideoEncodeAccelerator::RequestEncodingParametersChange( | 102 void FakeVideoEncodeAccelerator::RequestEncodingParametersChange( |
| 93 uint32 bitrate, | 103 uint32 bitrate, |
| 94 uint32 framerate) { | 104 uint32 framerate) { |
| 95 stored_bitrates_->push_back(bitrate); | 105 stored_bitrates_.push_back(bitrate); |
| 96 } | 106 } |
| 97 | 107 |
| 98 void FakeVideoEncodeAccelerator::Destroy() { delete this; } | 108 void FakeVideoEncodeAccelerator::Destroy() { delete this; } |
| 99 | 109 |
| 100 void FakeVideoEncodeAccelerator::SendDummyFrameForTesting(bool key_frame) { | 110 void FakeVideoEncodeAccelerator::SendDummyFrameForTesting(bool key_frame) { |
| 101 DoBitstreamBufferReady(0, 23, key_frame); | 111 DoBitstreamBufferReady(0, 23, key_frame); |
| 102 } | 112 } |
| 103 | 113 |
| 114 void FakeVideoEncodeAccelerator::SetWillInitializationSucceed( | |
| 115 bool will_initialization_succeed) { | |
| 116 will_initialization_succeed_ = will_initialization_succeed; | |
| 117 } | |
| 118 | |
| 104 void FakeVideoEncodeAccelerator::DoRequireBitstreamBuffers( | 119 void FakeVideoEncodeAccelerator::DoRequireBitstreamBuffers( |
| 105 unsigned int input_count, | 120 unsigned int input_count, |
| 106 const gfx::Size& input_coded_size, | 121 const gfx::Size& input_coded_size, |
| 107 size_t output_buffer_size) const { | 122 size_t output_buffer_size) const { |
| 108 client_->RequireBitstreamBuffers( | 123 client_->RequireBitstreamBuffers( |
| 109 input_count, input_coded_size, output_buffer_size); | 124 input_count, input_coded_size, output_buffer_size); |
| 110 } | 125 } |
| 111 | 126 |
| 112 void FakeVideoEncodeAccelerator::DoBitstreamBufferReady( | 127 void FakeVideoEncodeAccelerator::DoBitstreamBufferReady( |
| 113 int32 bitstream_buffer_id, | 128 int32 bitstream_buffer_id, |
| 114 size_t payload_size, | 129 size_t payload_size, |
| 115 bool key_frame) const { | 130 bool key_frame) const { |
| 116 client_->BitstreamBufferReady(bitstream_buffer_id, payload_size, key_frame); | 131 client_->BitstreamBufferReady(bitstream_buffer_id, payload_size, key_frame); |
| 117 } | 132 } |
| 118 | 133 |
| 119 } // namespace test | |
| 120 } // namespace cast | |
| 121 } // namespace media | 134 } // namespace media |
| OLD | NEW |