| 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/sender/h264_vt_encoder.h" | 5 #include "media/cast/sender/h264_vt_encoder.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <string> | 9 #include <string> |
| 10 #include <vector> | 10 #include <vector> |
| (...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 176 | 176 |
| 177 if (operational_status == STATUS_INITIALIZED) { | 177 if (operational_status == STATUS_INITIALIZED) { |
| 178 // Create the shared video frame factory. It persists for the combined | 178 // Create the shared video frame factory. It persists for the combined |
| 179 // lifetime of the encoder and all video frame factory proxies created by | 179 // lifetime of the encoder and all video frame factory proxies created by |
| 180 // |CreateVideoFrameFactory| that reference it. | 180 // |CreateVideoFrameFactory| that reference it. |
| 181 video_frame_factory_ = | 181 video_frame_factory_ = |
| 182 scoped_refptr<VideoFrameFactoryImpl>(new VideoFrameFactoryImpl( | 182 scoped_refptr<VideoFrameFactoryImpl>(new VideoFrameFactoryImpl( |
| 183 weak_factory_.GetWeakPtr(), cast_environment_)); | 183 weak_factory_.GetWeakPtr(), cast_environment_)); |
| 184 | 184 |
| 185 // Register for power state changes. | 185 // Register for power state changes. |
| 186 auto power_monitor = base::PowerMonitor::Get(); | 186 auto* power_monitor = base::PowerMonitor::Get(); |
| 187 if (power_monitor) { | 187 if (power_monitor) { |
| 188 power_monitor->AddObserver(this); | 188 power_monitor->AddObserver(this); |
| 189 VLOG(1) << "Registered for power state changes."; | 189 VLOG(1) << "Registered for power state changes."; |
| 190 } else { | 190 } else { |
| 191 DLOG(WARNING) << "No power monitor. Process suspension will invalidate " | 191 DLOG(WARNING) << "No power monitor. Process suspension will invalidate " |
| 192 "the encoder."; | 192 "the encoder."; |
| 193 } | 193 } |
| 194 } | 194 } |
| 195 } | 195 } |
| 196 | 196 |
| 197 H264VideoToolboxEncoder::~H264VideoToolboxEncoder() { | 197 H264VideoToolboxEncoder::~H264VideoToolboxEncoder() { |
| 198 DestroyCompressionSession(); | 198 DestroyCompressionSession(); |
| 199 | 199 |
| 200 // If video_frame_factory_ is not null, the encoder registered for power state | 200 // If video_frame_factory_ is not null, the encoder registered for power state |
| 201 // changes in the ctor and it must now unregister. | 201 // changes in the ctor and it must now unregister. |
| 202 if (video_frame_factory_) { | 202 if (video_frame_factory_) { |
| 203 auto power_monitor = base::PowerMonitor::Get(); | 203 auto* power_monitor = base::PowerMonitor::Get(); |
| 204 if (power_monitor) | 204 if (power_monitor) |
| 205 power_monitor->RemoveObserver(this); | 205 power_monitor->RemoveObserver(this); |
| 206 } | 206 } |
| 207 } | 207 } |
| 208 | 208 |
| 209 void H264VideoToolboxEncoder::ResetCompressionSession() { | 209 void H264VideoToolboxEncoder::ResetCompressionSession() { |
| 210 DCHECK(thread_checker_.CalledOnValidThread()); | 210 DCHECK(thread_checker_.CalledOnValidThread()); |
| 211 | 211 |
| 212 // Ignore reset requests while power suspended. | 212 // Ignore reset requests while power suspended. |
| 213 if (power_suspended_) | 213 if (power_suspended_) |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 246 kCVBufferPropagatedAttachmentsKey}; | 246 kCVBufferPropagatedAttachmentsKey}; |
| 247 CFTypeRef buffer_attributes_values[] = { | 247 CFTypeRef buffer_attributes_values[] = { |
| 248 video_toolbox::ArrayWithIntegers(format, arraysize(format)).release(), | 248 video_toolbox::ArrayWithIntegers(format, arraysize(format)).release(), |
| 249 video_toolbox::DictionaryWithKeysAndValues( | 249 video_toolbox::DictionaryWithKeysAndValues( |
| 250 attachments_keys, attachments_values, arraysize(attachments_keys)) | 250 attachments_keys, attachments_values, arraysize(attachments_keys)) |
| 251 .release()}; | 251 .release()}; |
| 252 const base::ScopedCFTypeRef<CFDictionaryRef> buffer_attributes = | 252 const base::ScopedCFTypeRef<CFDictionaryRef> buffer_attributes = |
| 253 video_toolbox::DictionaryWithKeysAndValues( | 253 video_toolbox::DictionaryWithKeysAndValues( |
| 254 buffer_attributes_keys, buffer_attributes_values, | 254 buffer_attributes_keys, buffer_attributes_values, |
| 255 arraysize(buffer_attributes_keys)); | 255 arraysize(buffer_attributes_keys)); |
| 256 for (auto& v : buffer_attributes_values) | 256 for (auto* v : buffer_attributes_values) |
| 257 CFRelease(v); | 257 CFRelease(v); |
| 258 | 258 |
| 259 // Create the compression session. | 259 // Create the compression session. |
| 260 | 260 |
| 261 // Note that the encoder object is given to the compression session as the | 261 // Note that the encoder object is given to the compression session as the |
| 262 // callback context using a raw pointer. The C API does not allow us to use a | 262 // callback context using a raw pointer. The C API does not allow us to use a |
| 263 // smart pointer, nor is this encoder ref counted. However, this is still | 263 // smart pointer, nor is this encoder ref counted. However, this is still |
| 264 // safe, because we 1) we own the compression session and 2) we tear it down | 264 // safe, because we 1) we own the compression session and 2) we tear it down |
| 265 // safely. When destructing the encoder, the compression session is flushed | 265 // safely. When destructing the encoder, the compression session is flushed |
| 266 // and invalidated. Internally, VideoToolbox will join all of its threads | 266 // and invalidated. Internally, VideoToolbox will join all of its threads |
| (...skipping 241 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 508 VLOG(1) << "OnResume: Resetting compression session."; | 508 VLOG(1) << "OnResume: Resetting compression session."; |
| 509 ResetCompressionSession(); | 509 ResetCompressionSession(); |
| 510 } | 510 } |
| 511 } | 511 } |
| 512 | 512 |
| 513 void H264VideoToolboxEncoder::CompressionCallback(void* encoder_opaque, | 513 void H264VideoToolboxEncoder::CompressionCallback(void* encoder_opaque, |
| 514 void* request_opaque, | 514 void* request_opaque, |
| 515 OSStatus status, | 515 OSStatus status, |
| 516 VTEncodeInfoFlags info, | 516 VTEncodeInfoFlags info, |
| 517 CMSampleBufferRef sbuf) { | 517 CMSampleBufferRef sbuf) { |
| 518 auto encoder = reinterpret_cast<H264VideoToolboxEncoder*>(encoder_opaque); | 518 auto* encoder = reinterpret_cast<H264VideoToolboxEncoder*>(encoder_opaque); |
| 519 const std::unique_ptr<InProgressFrameEncode> request( | 519 const std::unique_ptr<InProgressFrameEncode> request( |
| 520 reinterpret_cast<InProgressFrameEncode*>(request_opaque)); | 520 reinterpret_cast<InProgressFrameEncode*>(request_opaque)); |
| 521 bool keyframe = false; | 521 bool keyframe = false; |
| 522 bool has_frame_data = false; | 522 bool has_frame_data = false; |
| 523 | 523 |
| 524 if (status != noErr) { | 524 if (status != noErr) { |
| 525 DLOG(ERROR) << " encoding failed: " << status; | 525 DLOG(ERROR) << " encoding failed: " << status; |
| 526 encoder->cast_environment_->PostTask( | 526 encoder->cast_environment_->PostTask( |
| 527 CastEnvironment::MAIN, FROM_HERE, | 527 CastEnvironment::MAIN, FROM_HERE, |
| 528 base::Bind(encoder->status_change_cb_, STATUS_CODEC_RUNTIME_ERROR)); | 528 base::Bind(encoder->status_change_cb_, STATUS_CODEC_RUNTIME_ERROR)); |
| 529 } else if ((info & VideoToolboxGlue::kVTEncodeInfo_FrameDropped)) { | 529 } else if ((info & VideoToolboxGlue::kVTEncodeInfo_FrameDropped)) { |
| 530 DVLOG(2) << " frame dropped"; | 530 DVLOG(2) << " frame dropped"; |
| 531 } else { | 531 } else { |
| 532 auto sample_attachments = | 532 auto* sample_attachments = |
| 533 static_cast<CFDictionaryRef>(CFArrayGetValueAtIndex( | 533 static_cast<CFDictionaryRef>(CFArrayGetValueAtIndex( |
| 534 CoreMediaGlue::CMSampleBufferGetSampleAttachmentsArray(sbuf, true), | 534 CoreMediaGlue::CMSampleBufferGetSampleAttachmentsArray(sbuf, true), |
| 535 0)); | 535 0)); |
| 536 | 536 |
| 537 // If the NotSync key is not present, it implies Sync, which indicates a | 537 // If the NotSync key is not present, it implies Sync, which indicates a |
| 538 // keyframe (at least I think, VT documentation is, erm, sparse). Could | 538 // keyframe (at least I think, VT documentation is, erm, sparse). Could |
| 539 // alternatively use kCMSampleAttachmentKey_DependsOnOthers == false. | 539 // alternatively use kCMSampleAttachmentKey_DependsOnOthers == false. |
| 540 keyframe = !CFDictionaryContainsKey( | 540 keyframe = !CFDictionaryContainsKey( |
| 541 sample_attachments, | 541 sample_attachments, |
| 542 CoreMediaGlue::kCMSampleAttachmentKey_NotSync()); | 542 CoreMediaGlue::kCMSampleAttachmentKey_NotSync()); |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 579 encoded_frame->encode_completion_time = | 579 encoded_frame->encode_completion_time = |
| 580 encoder->cast_environment_->Clock()->NowTicks(); | 580 encoder->cast_environment_->Clock()->NowTicks(); |
| 581 encoder->cast_environment_->PostTask( | 581 encoder->cast_environment_->PostTask( |
| 582 CastEnvironment::MAIN, FROM_HERE, | 582 CastEnvironment::MAIN, FROM_HERE, |
| 583 base::Bind(request->frame_encoded_callback, | 583 base::Bind(request->frame_encoded_callback, |
| 584 base::Passed(&encoded_frame))); | 584 base::Passed(&encoded_frame))); |
| 585 } | 585 } |
| 586 | 586 |
| 587 } // namespace cast | 587 } // namespace cast |
| 588 } // namespace media | 588 } // namespace media |
| OLD | NEW |