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 <string> | 7 #include <string> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/big_endian.h" | 10 #include "base/big_endian.h" |
11 #include "base/bind.h" | 11 #include "base/bind.h" |
12 #include "base/bind_helpers.h" | 12 #include "base/bind_helpers.h" |
13 #include "base/location.h" | 13 #include "base/location.h" |
14 #include "base/logging.h" | 14 #include "base/logging.h" |
15 #include "base/macros.h" | 15 #include "base/macros.h" |
| 16 #include "base/power_monitor/power_monitor.h" |
16 #include "base/synchronization/lock.h" | 17 #include "base/synchronization/lock.h" |
17 #include "media/base/mac/corevideo_glue.h" | 18 #include "media/base/mac/corevideo_glue.h" |
18 #include "media/base/mac/video_frame_mac.h" | 19 #include "media/base/mac/video_frame_mac.h" |
19 #include "media/cast/sender/video_frame_factory.h" | 20 #include "media/cast/sender/video_frame_factory.h" |
20 | 21 |
21 namespace media { | 22 namespace media { |
22 namespace cast { | 23 namespace cast { |
23 | 24 |
24 namespace { | 25 namespace { |
25 | 26 |
26 // Container for the associated data of a video frame being processed. | 27 // Container for the associated data of a video frame being processed. |
27 struct InProgressFrameEncode { | 28 struct InProgressFrameEncode { |
28 const RtpTimestamp rtp_timestamp; | 29 const RtpTimestamp rtp_timestamp; |
29 const base::TimeTicks reference_time; | 30 const base::TimeTicks reference_time; |
30 const VideoEncoder::FrameEncodedCallback frame_encoded_callback; | 31 const VideoEncoder::FrameEncodedCallback frame_encoded_callback; |
31 | 32 |
32 InProgressFrameEncode(RtpTimestamp rtp, | 33 InProgressFrameEncode(RtpTimestamp rtp, |
33 base::TimeTicks r_time, | 34 base::TimeTicks r_time, |
34 VideoEncoder::FrameEncodedCallback callback) | 35 VideoEncoder::FrameEncodedCallback callback) |
35 : rtp_timestamp(rtp), | 36 : rtp_timestamp(rtp), |
36 reference_time(r_time), | 37 reference_time(r_time), |
37 frame_encoded_callback(callback) {} | 38 frame_encoded_callback(callback) {} |
38 }; | 39 }; |
39 | 40 |
40 base::ScopedCFTypeRef<CFDictionaryRef> DictionaryWithKeysAndValues( | 41 base::ScopedCFTypeRef<CFDictionaryRef> |
41 CFTypeRef* keys, | 42 DictionaryWithKeysAndValues(CFTypeRef* keys, CFTypeRef* values, size_t size) { |
42 CFTypeRef* values, | |
43 size_t size) { | |
44 return base::ScopedCFTypeRef<CFDictionaryRef>(CFDictionaryCreate( | 43 return base::ScopedCFTypeRef<CFDictionaryRef>(CFDictionaryCreate( |
45 kCFAllocatorDefault, | 44 kCFAllocatorDefault, keys, values, size, &kCFTypeDictionaryKeyCallBacks, |
46 keys, | |
47 values, | |
48 size, | |
49 &kCFTypeDictionaryKeyCallBacks, | |
50 &kCFTypeDictionaryValueCallBacks)); | 45 &kCFTypeDictionaryValueCallBacks)); |
51 } | 46 } |
52 | 47 |
53 base::ScopedCFTypeRef<CFDictionaryRef> DictionaryWithKeyValue(CFTypeRef key, | 48 base::ScopedCFTypeRef<CFDictionaryRef> DictionaryWithKeyValue(CFTypeRef key, |
54 CFTypeRef value) { | 49 CFTypeRef value) { |
55 CFTypeRef keys[1] = {key}; | 50 CFTypeRef keys[1] = {key}; |
56 CFTypeRef values[1] = {value}; | 51 CFTypeRef values[1] = {value}; |
57 return DictionaryWithKeysAndValues(keys, values, 1); | 52 return DictionaryWithKeysAndValues(keys, values, 1); |
58 } | 53 } |
59 | 54 |
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
202 CopyNalsToAnnexB<uint8_t>(bb_data, bb_size, annexb_buffer); | 197 CopyNalsToAnnexB<uint8_t>(bb_data, bb_size, annexb_buffer); |
203 } else if (nal_size_field_bytes == 2) { | 198 } else if (nal_size_field_bytes == 2) { |
204 CopyNalsToAnnexB<uint16_t>(bb_data, bb_size, annexb_buffer); | 199 CopyNalsToAnnexB<uint16_t>(bb_data, bb_size, annexb_buffer); |
205 } else if (nal_size_field_bytes == 4) { | 200 } else if (nal_size_field_bytes == 4) { |
206 CopyNalsToAnnexB<uint32_t>(bb_data, bb_size, annexb_buffer); | 201 CopyNalsToAnnexB<uint32_t>(bb_data, bb_size, annexb_buffer); |
207 } else { | 202 } else { |
208 NOTREACHED(); | 203 NOTREACHED(); |
209 } | 204 } |
210 } | 205 } |
211 | 206 |
212 // Implementation of the VideoFrameFactory interface using |CVPixelBufferPool|. | 207 } // namespace |
213 class VideoFrameFactoryCVPixelBufferPoolImpl : public VideoFrameFactory { | 208 |
| 209 class H264VideoToolboxEncoder::VideoFrameFactoryImpl |
| 210 : public base::RefCountedThreadSafe<VideoFrameFactoryImpl>, |
| 211 public VideoFrameFactory { |
214 public: | 212 public: |
215 VideoFrameFactoryCVPixelBufferPoolImpl( | 213 // Type that proxies the VideoFrameFactory interface to this class. |
216 const base::ScopedCFTypeRef<CVPixelBufferPoolRef>& pool, | 214 class Proxy; |
217 const gfx::Size& frame_size) | |
218 : pool_(pool), | |
219 frame_size_(frame_size) {} | |
220 | 215 |
221 ~VideoFrameFactoryCVPixelBufferPoolImpl() override {} | 216 VideoFrameFactoryImpl(const base::WeakPtr<H264VideoToolboxEncoder>& encoder, |
| 217 const scoped_refptr<CastEnvironment>& cast_environment) |
| 218 : encoder_(encoder), cast_environment_(cast_environment) {} |
222 | 219 |
223 scoped_refptr<VideoFrame> MaybeCreateFrame( | 220 scoped_refptr<VideoFrame> MaybeCreateFrame( |
224 const gfx::Size& frame_size, base::TimeDelta timestamp) override { | 221 const gfx::Size& frame_size, |
225 if (frame_size != frame_size_) | 222 base::TimeDelta timestamp) override { |
226 return nullptr; // Buffer pool is not a match for requested frame size. | 223 if (frame_size.IsEmpty()) { |
| 224 DVLOG(1) << "Rejecting empty video frame."; |
| 225 return nullptr; |
| 226 } |
227 | 227 |
| 228 base::AutoLock auto_lock(lock_); |
| 229 |
| 230 // If the pool size does not match, speculatively reset the encoder to use |
| 231 // the new size and return null. Cache the new frame size right away and |
| 232 // toss away the pixel buffer pool to avoid spurious tasks until the encoder |
| 233 // is done resetting. |
| 234 if (frame_size != pool_frame_size_) { |
| 235 DVLOG(1) << "MaybeCreateFrame: Detected frame size change."; |
| 236 cast_environment_->PostTask( |
| 237 CastEnvironment::MAIN, FROM_HERE, |
| 238 base::Bind(&H264VideoToolboxEncoder::UpdateFrameSize, encoder_, |
| 239 frame_size)); |
| 240 pool_frame_size_ = frame_size; |
| 241 pool_.reset(); |
| 242 return nullptr; |
| 243 } |
| 244 |
| 245 if (!pool_) { |
| 246 DVLOG(1) << "MaybeCreateFrame: No pixel buffer pool."; |
| 247 return nullptr; |
| 248 } |
| 249 |
| 250 // Allocate a pixel buffer from the pool and return a wrapper VideoFrame. |
228 base::ScopedCFTypeRef<CVPixelBufferRef> buffer; | 251 base::ScopedCFTypeRef<CVPixelBufferRef> buffer; |
229 if (CVPixelBufferPoolCreatePixelBuffer(kCFAllocatorDefault, pool_, | 252 auto status = CVPixelBufferPoolCreatePixelBuffer(kCFAllocatorDefault, pool_, |
230 buffer.InitializeInto()) != | 253 buffer.InitializeInto()); |
231 kCVReturnSuccess) | 254 if (status != kCVReturnSuccess) { |
232 return nullptr; // Buffer pool has run out of pixel buffers. | 255 DLOG(ERROR) << "CVPixelBufferPoolCreatePixelBuffer failed: " << status; |
| 256 return nullptr; |
| 257 } |
| 258 |
233 DCHECK(buffer); | 259 DCHECK(buffer); |
234 | |
235 return VideoFrame::WrapCVPixelBuffer(buffer, timestamp); | 260 return VideoFrame::WrapCVPixelBuffer(buffer, timestamp); |
236 } | 261 } |
237 | 262 |
| 263 void Update(const base::ScopedCFTypeRef<CVPixelBufferPoolRef>& pool, |
| 264 const gfx::Size& frame_size) { |
| 265 base::AutoLock auto_lock(lock_); |
| 266 pool_ = pool; |
| 267 pool_frame_size_ = frame_size; |
| 268 } |
| 269 |
238 private: | 270 private: |
239 const base::ScopedCFTypeRef<CVPixelBufferPoolRef> pool_; | 271 friend class base::RefCountedThreadSafe<VideoFrameFactoryImpl>; |
240 const gfx::Size frame_size_; | 272 ~VideoFrameFactoryImpl() override {} |
241 | 273 |
242 DISALLOW_COPY_AND_ASSIGN(VideoFrameFactoryCVPixelBufferPoolImpl); | 274 base::Lock lock_; |
| 275 base::ScopedCFTypeRef<CVPixelBufferPoolRef> pool_; |
| 276 gfx::Size pool_frame_size_; |
| 277 |
| 278 // Weak back reference to the encoder and the cast envrionment so we can |
| 279 // message the encoder when the frame size changes. |
| 280 const base::WeakPtr<H264VideoToolboxEncoder> encoder_; |
| 281 const scoped_refptr<CastEnvironment> cast_environment_; |
| 282 |
| 283 DISALLOW_COPY_AND_ASSIGN(VideoFrameFactoryImpl); |
243 }; | 284 }; |
244 | 285 |
245 } // namespace | 286 class H264VideoToolboxEncoder::VideoFrameFactoryImpl::Proxy |
| 287 : public VideoFrameFactory { |
| 288 public: |
| 289 explicit Proxy( |
| 290 const scoped_refptr<VideoFrameFactoryImpl>& video_frame_factory) |
| 291 : video_frame_factory_(video_frame_factory) { |
| 292 DCHECK(video_frame_factory_); |
| 293 } |
| 294 |
| 295 scoped_refptr<VideoFrame> MaybeCreateFrame( |
| 296 const gfx::Size& frame_size, |
| 297 base::TimeDelta timestamp) override { |
| 298 return video_frame_factory_->MaybeCreateFrame(frame_size, timestamp); |
| 299 } |
| 300 |
| 301 private: |
| 302 ~Proxy() override {} |
| 303 |
| 304 const scoped_refptr<VideoFrameFactoryImpl> video_frame_factory_; |
| 305 |
| 306 DISALLOW_COPY_AND_ASSIGN(Proxy); |
| 307 }; |
246 | 308 |
247 // static | 309 // static |
248 bool H264VideoToolboxEncoder::IsSupported( | 310 bool H264VideoToolboxEncoder::IsSupported( |
249 const VideoSenderConfig& video_config) { | 311 const VideoSenderConfig& video_config) { |
250 return video_config.codec == CODEC_VIDEO_H264 && VideoToolboxGlue::Get(); | 312 return video_config.codec == CODEC_VIDEO_H264 && VideoToolboxGlue::Get(); |
251 } | 313 } |
252 | 314 |
253 H264VideoToolboxEncoder::H264VideoToolboxEncoder( | 315 H264VideoToolboxEncoder::H264VideoToolboxEncoder( |
254 const scoped_refptr<CastEnvironment>& cast_environment, | 316 const scoped_refptr<CastEnvironment>& cast_environment, |
255 const VideoSenderConfig& video_config, | 317 const VideoSenderConfig& video_config, |
256 const gfx::Size& frame_size, | |
257 uint32 first_frame_id, | |
258 const StatusChangeCallback& status_change_cb) | 318 const StatusChangeCallback& status_change_cb) |
259 : cast_environment_(cast_environment), | 319 : cast_environment_(cast_environment), |
260 videotoolbox_glue_(VideoToolboxGlue::Get()), | 320 videotoolbox_glue_(VideoToolboxGlue::Get()), |
261 frame_size_(frame_size), | 321 video_config_(video_config), |
262 status_change_cb_(status_change_cb), | 322 status_change_cb_(status_change_cb), |
263 next_frame_id_(first_frame_id), | 323 last_frame_id_(kStartFrameId), |
264 encode_next_frame_as_keyframe_(false) { | 324 encode_next_frame_as_keyframe_(false), |
265 DCHECK(!frame_size_.IsEmpty()); | 325 power_suspended_(false), |
| 326 weak_factory_(this) { |
| 327 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); |
266 DCHECK(!status_change_cb_.is_null()); | 328 DCHECK(!status_change_cb_.is_null()); |
267 | 329 |
268 OperationalStatus operational_status; | 330 OperationalStatus operational_status = |
269 if (video_config.codec == CODEC_VIDEO_H264 && videotoolbox_glue_) { | 331 H264VideoToolboxEncoder::IsSupported(video_config) |
270 operational_status = Initialize(video_config) ? | 332 ? STATUS_INITIALIZED |
271 STATUS_INITIALIZED : STATUS_INVALID_CONFIGURATION; | 333 : STATUS_UNSUPPORTED_CODEC; |
272 } else { | 334 cast_environment_->PostTask( |
273 operational_status = STATUS_UNSUPPORTED_CODEC; | 335 CastEnvironment::MAIN, FROM_HERE, |
| 336 base::Bind(status_change_cb_, operational_status)); |
| 337 |
| 338 if (operational_status == STATUS_INITIALIZED) { |
| 339 // Create the shared video frame factory. It persists for the combined |
| 340 // lifetime of the encoder and all video frame factory proxies created by |
| 341 // |CreateVideoFrameFactory| that reference it. |
| 342 video_frame_factory_ = |
| 343 scoped_refptr<VideoFrameFactoryImpl>(new VideoFrameFactoryImpl( |
| 344 weak_factory_.GetWeakPtr(), cast_environment_)); |
| 345 |
| 346 // Register for power state changes. |
| 347 auto power_monitor = base::PowerMonitor::Get(); |
| 348 if (power_monitor) { |
| 349 power_monitor->AddObserver(this); |
| 350 VLOG(1) << "Registered for power state changes."; |
| 351 } else { |
| 352 DLOG(WARNING) << "No power monitor. Process suspension will invalidate " |
| 353 "the encoder."; |
| 354 } |
274 } | 355 } |
275 cast_environment_->PostTask( | |
276 CastEnvironment::MAIN, | |
277 FROM_HERE, | |
278 base::Bind(status_change_cb_, operational_status)); | |
279 } | 356 } |
280 | 357 |
281 H264VideoToolboxEncoder::~H264VideoToolboxEncoder() { | 358 H264VideoToolboxEncoder::~H264VideoToolboxEncoder() { |
282 Teardown(); | 359 DestroyCompressionSession(); |
| 360 |
| 361 // If video_frame_factory_ is not null, the encoder registered for power state |
| 362 // changes in the ctor and it must now unregister. |
| 363 if (video_frame_factory_) { |
| 364 auto power_monitor = base::PowerMonitor::Get(); |
| 365 if (power_monitor) |
| 366 power_monitor->RemoveObserver(this); |
| 367 } |
283 } | 368 } |
284 | 369 |
285 bool H264VideoToolboxEncoder::Initialize( | 370 void H264VideoToolboxEncoder::ResetCompressionSession() { |
286 const VideoSenderConfig& video_config) { | |
287 DCHECK(thread_checker_.CalledOnValidThread()); | 371 DCHECK(thread_checker_.CalledOnValidThread()); |
288 DCHECK(!compression_session_); | |
289 | 372 |
290 // Note that the encoder object is given to the compression session as the | 373 // Ignore reset requests while power suspended. |
291 // callback context using a raw pointer. The C API does not allow us to use | 374 if (power_suspended_) |
292 // a smart pointer, nor is this encoder ref counted. However, this is still | 375 return; |
293 // safe, because we 1) we own the compression session and 2) we tear it down | 376 |
294 // safely. When destructing the encoder, the compression session is flushed | 377 // Notify that we're resetting the encoder. |
295 // and invalidated. Internally, VideoToolbox will join all of its threads | 378 cast_environment_->PostTask( |
296 // before returning to the client. Therefore, when control returns to us, we | 379 CastEnvironment::MAIN, FROM_HERE, |
297 // are guaranteed that the output callback will not execute again. | 380 base::Bind(status_change_cb_, STATUS_CODEC_REINIT_PENDING)); |
| 381 |
| 382 // Destroy the current session, if any. |
| 383 DestroyCompressionSession(); |
298 | 384 |
299 // On OS X, allow the hardware encoder. Don't require it, it does not support | 385 // On OS X, allow the hardware encoder. Don't require it, it does not support |
300 // all configurations (some of which are used for testing). | 386 // all configurations (some of which are used for testing). |
301 base::ScopedCFTypeRef<CFDictionaryRef> encoder_spec; | 387 base::ScopedCFTypeRef<CFDictionaryRef> encoder_spec; |
302 #if !defined(OS_IOS) | 388 #if !defined(OS_IOS) |
303 encoder_spec = DictionaryWithKeyValue( | 389 encoder_spec = DictionaryWithKeyValue( |
304 videotoolbox_glue_ | 390 videotoolbox_glue_ |
305 ->kVTVideoEncoderSpecification_EnableHardwareAcceleratedVideoEncoder()
, | 391 ->kVTVideoEncoderSpecification_EnableHardwareAcceleratedVideoEncoder()
, |
306 kCFBooleanTrue); | 392 kCFBooleanTrue); |
307 #endif | 393 #endif |
308 | 394 |
309 // Certain encoders prefer kCVPixelFormatType_422YpCbCr8, which is not | 395 // Certain encoders prefer kCVPixelFormatType_422YpCbCr8, which is not |
310 // supported through VideoFrame. We can force 420 formats to be used instead. | 396 // supported through VideoFrame. We can force 420 formats to be used instead. |
311 const int formats[] = { | 397 const int formats[] = { |
312 kCVPixelFormatType_420YpCbCr8Planar, | 398 kCVPixelFormatType_420YpCbCr8Planar, |
313 CoreVideoGlue::kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange | 399 CoreVideoGlue::kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange}; |
314 }; | |
315 // Keep these attachment settings in-sync with those in ConfigureSession(). | 400 // Keep these attachment settings in-sync with those in ConfigureSession(). |
316 CFTypeRef attachments_keys[] = { | 401 CFTypeRef attachments_keys[] = {kCVImageBufferColorPrimariesKey, |
317 kCVImageBufferColorPrimariesKey, | 402 kCVImageBufferTransferFunctionKey, |
318 kCVImageBufferTransferFunctionKey, | 403 kCVImageBufferYCbCrMatrixKey}; |
319 kCVImageBufferYCbCrMatrixKey | 404 CFTypeRef attachments_values[] = {kCVImageBufferColorPrimaries_ITU_R_709_2, |
320 }; | 405 kCVImageBufferTransferFunction_ITU_R_709_2, |
321 CFTypeRef attachments_values[] = { | 406 kCVImageBufferYCbCrMatrix_ITU_R_709_2}; |
322 kCVImageBufferColorPrimaries_ITU_R_709_2, | 407 CFTypeRef buffer_attributes_keys[] = {kCVPixelBufferPixelFormatTypeKey, |
323 kCVImageBufferTransferFunction_ITU_R_709_2, | 408 kCVBufferPropagatedAttachmentsKey}; |
324 kCVImageBufferYCbCrMatrix_ITU_R_709_2 | |
325 }; | |
326 CFTypeRef buffer_attributes_keys[] = { | |
327 kCVPixelBufferPixelFormatTypeKey, | |
328 kCVBufferPropagatedAttachmentsKey | |
329 }; | |
330 CFTypeRef buffer_attributes_values[] = { | 409 CFTypeRef buffer_attributes_values[] = { |
331 ArrayWithIntegers(formats, arraysize(formats)).release(), | 410 ArrayWithIntegers(formats, arraysize(formats)).release(), |
332 DictionaryWithKeysAndValues(attachments_keys, | 411 DictionaryWithKeysAndValues(attachments_keys, attachments_values, |
333 attachments_values, | 412 arraysize(attachments_keys)).release()}; |
334 arraysize(attachments_keys)).release() | |
335 }; | |
336 const base::ScopedCFTypeRef<CFDictionaryRef> buffer_attributes = | 413 const base::ScopedCFTypeRef<CFDictionaryRef> buffer_attributes = |
337 DictionaryWithKeysAndValues(buffer_attributes_keys, | 414 DictionaryWithKeysAndValues(buffer_attributes_keys, |
338 buffer_attributes_values, | 415 buffer_attributes_values, |
339 arraysize(buffer_attributes_keys)); | 416 arraysize(buffer_attributes_keys)); |
340 for (auto& v : buffer_attributes_values) | 417 for (auto& v : buffer_attributes_values) |
341 CFRelease(v); | 418 CFRelease(v); |
342 | 419 |
343 VTCompressionSessionRef session; | 420 // Create the compression session. |
| 421 |
| 422 // Note that the encoder object is given to the compression session as the |
| 423 // callback context using a raw pointer. The C API does not allow us to use a |
| 424 // smart pointer, nor is this encoder ref counted. However, this is still |
| 425 // safe, because we 1) we own the compression session and 2) we tear it down |
| 426 // safely. When destructing the encoder, the compression session is flushed |
| 427 // and invalidated. Internally, VideoToolbox will join all of its threads |
| 428 // before returning to the client. Therefore, when control returns to us, we |
| 429 // are guaranteed that the output callback will not execute again. |
344 OSStatus status = videotoolbox_glue_->VTCompressionSessionCreate( | 430 OSStatus status = videotoolbox_glue_->VTCompressionSessionCreate( |
345 kCFAllocatorDefault, frame_size_.width(), frame_size_.height(), | 431 kCFAllocatorDefault, frame_size_.width(), frame_size_.height(), |
346 CoreMediaGlue::kCMVideoCodecType_H264, encoder_spec, buffer_attributes, | 432 CoreMediaGlue::kCMVideoCodecType_H264, encoder_spec, buffer_attributes, |
347 nullptr /* compressedDataAllocator */, | 433 nullptr /* compressedDataAllocator */, |
348 &H264VideoToolboxEncoder::CompressionCallback, | 434 &H264VideoToolboxEncoder::CompressionCallback, |
349 reinterpret_cast<void*>(this), &session); | 435 reinterpret_cast<void*>(this), compression_session_.InitializeInto()); |
350 if (status != noErr) { | 436 if (status != noErr) { |
351 DLOG(ERROR) << " VTCompressionSessionCreate failed: " << status; | 437 DLOG(ERROR) << " VTCompressionSessionCreate failed: " << status; |
352 return false; | 438 // Notify that reinitialization has failed. |
| 439 cast_environment_->PostTask( |
| 440 CastEnvironment::MAIN, FROM_HERE, |
| 441 base::Bind(status_change_cb_, STATUS_CODEC_INIT_FAILED)); |
| 442 return; |
353 } | 443 } |
354 compression_session_.reset(session); | |
355 | 444 |
356 ConfigureSession(video_config); | 445 // Configure the session (apply session properties based on the current state |
| 446 // of the encoder, experimental tuning and requirements). |
| 447 ConfigureCompressionSession(); |
357 | 448 |
358 return true; | 449 // Update the video frame factory. |
| 450 base::ScopedCFTypeRef<CVPixelBufferPoolRef> pool( |
| 451 videotoolbox_glue_->VTCompressionSessionGetPixelBufferPool( |
| 452 compression_session_), |
| 453 base::scoped_policy::RETAIN); |
| 454 video_frame_factory_->Update(pool, frame_size_); |
| 455 |
| 456 // Notify that reinitialization is done. |
| 457 cast_environment_->PostTask( |
| 458 CastEnvironment::MAIN, FROM_HERE, |
| 459 base::Bind(status_change_cb_, STATUS_INITIALIZED)); |
359 } | 460 } |
360 | 461 |
361 void H264VideoToolboxEncoder::ConfigureSession( | 462 void H264VideoToolboxEncoder::ConfigureCompressionSession() { |
362 const VideoSenderConfig& video_config) { | |
363 SetSessionProperty( | 463 SetSessionProperty( |
364 videotoolbox_glue_->kVTCompressionPropertyKey_ProfileLevel(), | 464 videotoolbox_glue_->kVTCompressionPropertyKey_ProfileLevel(), |
365 videotoolbox_glue_->kVTProfileLevel_H264_Main_AutoLevel()); | 465 videotoolbox_glue_->kVTProfileLevel_H264_Main_AutoLevel()); |
366 SetSessionProperty(videotoolbox_glue_->kVTCompressionPropertyKey_RealTime(), | 466 SetSessionProperty(videotoolbox_glue_->kVTCompressionPropertyKey_RealTime(), |
367 true); | 467 true); |
368 SetSessionProperty( | 468 SetSessionProperty( |
369 videotoolbox_glue_->kVTCompressionPropertyKey_AllowFrameReordering(), | 469 videotoolbox_glue_->kVTCompressionPropertyKey_AllowFrameReordering(), |
370 false); | 470 false); |
371 SetSessionProperty( | 471 SetSessionProperty( |
372 videotoolbox_glue_->kVTCompressionPropertyKey_MaxKeyFrameInterval(), 240); | 472 videotoolbox_glue_->kVTCompressionPropertyKey_MaxKeyFrameInterval(), 240); |
373 SetSessionProperty( | 473 SetSessionProperty( |
374 videotoolbox_glue_ | 474 videotoolbox_glue_ |
375 ->kVTCompressionPropertyKey_MaxKeyFrameIntervalDuration(), | 475 ->kVTCompressionPropertyKey_MaxKeyFrameIntervalDuration(), |
376 240); | 476 240); |
377 // TODO(jfroy): implement better bitrate control | 477 // TODO(jfroy): implement better bitrate control |
378 // https://crbug.com/425352 | 478 // https://crbug.com/425352 |
379 SetSessionProperty( | 479 SetSessionProperty( |
380 videotoolbox_glue_->kVTCompressionPropertyKey_AverageBitRate(), | 480 videotoolbox_glue_->kVTCompressionPropertyKey_AverageBitRate(), |
381 (video_config.min_bitrate + video_config.max_bitrate) / 2); | 481 (video_config_.min_bitrate + video_config_.max_bitrate) / 2); |
382 SetSessionProperty( | 482 SetSessionProperty( |
383 videotoolbox_glue_->kVTCompressionPropertyKey_ExpectedFrameRate(), | 483 videotoolbox_glue_->kVTCompressionPropertyKey_ExpectedFrameRate(), |
384 video_config.max_frame_rate); | 484 video_config_.max_frame_rate); |
385 // Keep these attachment settings in-sync with those in Initialize(). | 485 // Keep these attachment settings in-sync with those in Initialize(). |
386 SetSessionProperty( | 486 SetSessionProperty( |
387 videotoolbox_glue_->kVTCompressionPropertyKey_ColorPrimaries(), | 487 videotoolbox_glue_->kVTCompressionPropertyKey_ColorPrimaries(), |
388 kCVImageBufferColorPrimaries_ITU_R_709_2); | 488 kCVImageBufferColorPrimaries_ITU_R_709_2); |
389 SetSessionProperty( | 489 SetSessionProperty( |
390 videotoolbox_glue_->kVTCompressionPropertyKey_TransferFunction(), | 490 videotoolbox_glue_->kVTCompressionPropertyKey_TransferFunction(), |
391 kCVImageBufferTransferFunction_ITU_R_709_2); | 491 kCVImageBufferTransferFunction_ITU_R_709_2); |
392 SetSessionProperty( | 492 SetSessionProperty( |
393 videotoolbox_glue_->kVTCompressionPropertyKey_YCbCrMatrix(), | 493 videotoolbox_glue_->kVTCompressionPropertyKey_YCbCrMatrix(), |
394 kCVImageBufferYCbCrMatrix_ITU_R_709_2); | 494 kCVImageBufferYCbCrMatrix_ITU_R_709_2); |
395 if (video_config.max_number_of_video_buffers_used > 0) { | 495 if (video_config_.max_number_of_video_buffers_used > 0) { |
396 SetSessionProperty( | 496 SetSessionProperty( |
397 videotoolbox_glue_->kVTCompressionPropertyKey_MaxFrameDelayCount(), | 497 videotoolbox_glue_->kVTCompressionPropertyKey_MaxFrameDelayCount(), |
398 video_config.max_number_of_video_buffers_used); | 498 video_config_.max_number_of_video_buffers_used); |
399 } | 499 } |
400 } | 500 } |
401 | 501 |
402 void H264VideoToolboxEncoder::Teardown() { | 502 void H264VideoToolboxEncoder::DestroyCompressionSession() { |
403 DCHECK(thread_checker_.CalledOnValidThread()); | 503 DCHECK(thread_checker_.CalledOnValidThread()); |
404 | 504 |
405 // If the compression session exists, invalidate it. This blocks until all | 505 // If the compression session exists, invalidate it. This blocks until all |
406 // pending output callbacks have returned and any internal threads have | 506 // pending output callbacks have returned and any internal threads have |
407 // joined, ensuring no output callback ever sees a dangling encoder pointer. | 507 // joined, ensuring no output callback ever sees a dangling encoder pointer. |
| 508 // |
| 509 // Before destroying the compression session, the video frame factory's pool |
| 510 // is updated to null so that no thread will produce new video frames via the |
| 511 // factory until a new compression session is created. The current frame size |
| 512 // is passed to prevent the video frame factory from posting |UpdateFrameSize| |
| 513 // tasks. Indeed, |DestroyCompressionSession| is either called from |
| 514 // |ResetCompressionSession|, in which case a new pool and frame size will be |
| 515 // set, or from callsites that require that there be no compression session |
| 516 // (ex: the dtor). |
408 if (compression_session_) { | 517 if (compression_session_) { |
| 518 video_frame_factory_->Update( |
| 519 base::ScopedCFTypeRef<CVPixelBufferPoolRef>(nullptr), frame_size_); |
409 videotoolbox_glue_->VTCompressionSessionInvalidate(compression_session_); | 520 videotoolbox_glue_->VTCompressionSessionInvalidate(compression_session_); |
410 compression_session_.reset(); | 521 compression_session_.reset(); |
411 } | 522 } |
412 } | 523 } |
413 | 524 |
414 bool H264VideoToolboxEncoder::EncodeVideoFrame( | 525 bool H264VideoToolboxEncoder::EncodeVideoFrame( |
415 const scoped_refptr<media::VideoFrame>& video_frame, | 526 const scoped_refptr<media::VideoFrame>& video_frame, |
416 const base::TimeTicks& reference_time, | 527 const base::TimeTicks& reference_time, |
417 const FrameEncodedCallback& frame_encoded_callback) { | 528 const FrameEncodedCallback& frame_encoded_callback) { |
418 DCHECK(thread_checker_.CalledOnValidThread()); | 529 DCHECK(thread_checker_.CalledOnValidThread()); |
419 DCHECK(!frame_encoded_callback.is_null()); | 530 DCHECK(!frame_encoded_callback.is_null()); |
420 | 531 |
421 if (!compression_session_) { | 532 // Reject empty video frames. |
422 DLOG(ERROR) << " compression session is null"; | 533 const gfx::Size frame_size = video_frame->visible_rect().size(); |
| 534 if (frame_size.IsEmpty()) { |
| 535 DVLOG(1) << "Rejecting empty video frame."; |
423 return false; | 536 return false; |
424 } | 537 } |
425 | 538 |
426 if (video_frame->visible_rect().size() != frame_size_) | 539 // Handle frame size changes. This will reset the compression session. |
| 540 if (frame_size != frame_size_) { |
| 541 DVLOG(1) << "EncodeVideoFrame: Detected frame size change."; |
| 542 UpdateFrameSize(frame_size); |
| 543 } |
| 544 |
| 545 // Need a compression session to continue. |
| 546 if (!compression_session_) { |
| 547 DLOG(ERROR) << "No compression session."; |
427 return false; | 548 return false; |
| 549 } |
428 | 550 |
429 // Wrap the VideoFrame in a CVPixelBuffer. In all cases, no data will be | 551 // Wrap the VideoFrame in a CVPixelBuffer. In all cases, no data will be |
430 // copied. If the VideoFrame was created by this encoder's video frame | 552 // copied. If the VideoFrame was created by this encoder's video frame |
431 // factory, then the returned CVPixelBuffer will have been obtained from the | 553 // factory, then the returned CVPixelBuffer will have been obtained from the |
432 // compression session's pixel buffer pool. This will eliminate a copy of the | 554 // compression session's pixel buffer pool. This will eliminate a copy of the |
433 // frame into memory visible by the hardware encoder. The VideoFrame's | 555 // frame into memory visible by the hardware encoder. The VideoFrame's |
434 // lifetime is extended for the lifetime of the returned CVPixelBuffer. | 556 // lifetime is extended for the lifetime of the returned CVPixelBuffer. |
435 auto pixel_buffer = media::WrapVideoFrameInCVPixelBuffer(*video_frame); | 557 auto pixel_buffer = media::WrapVideoFrameInCVPixelBuffer(*video_frame); |
436 if (!pixel_buffer) { | 558 if (!pixel_buffer) { |
| 559 DLOG(ERROR) << "WrapVideoFrameInCVPixelBuffer failed."; |
437 return false; | 560 return false; |
438 } | 561 } |
439 | 562 |
| 563 // Convert the frame timestamp to CMTime. |
440 auto timestamp_cm = CoreMediaGlue::CMTimeMake( | 564 auto timestamp_cm = CoreMediaGlue::CMTimeMake( |
441 (reference_time - base::TimeTicks()).InMicroseconds(), USEC_PER_SEC); | 565 (reference_time - base::TimeTicks()).InMicroseconds(), USEC_PER_SEC); |
442 | 566 |
| 567 // Wrap information we'll need after the frame is encoded in a heap object. |
| 568 // We'll get the pointer back from the VideoToolbox completion callback. |
443 scoped_ptr<InProgressFrameEncode> request(new InProgressFrameEncode( | 569 scoped_ptr<InProgressFrameEncode> request(new InProgressFrameEncode( |
444 TimeDeltaToRtpDelta(video_frame->timestamp(), kVideoFrequency), | 570 TimeDeltaToRtpDelta(video_frame->timestamp(), kVideoFrequency), |
445 reference_time, frame_encoded_callback)); | 571 reference_time, frame_encoded_callback)); |
446 | 572 |
| 573 // Build a suitable frame properties dictionary for keyframes. |
447 base::ScopedCFTypeRef<CFDictionaryRef> frame_props; | 574 base::ScopedCFTypeRef<CFDictionaryRef> frame_props; |
448 if (encode_next_frame_as_keyframe_) { | 575 if (encode_next_frame_as_keyframe_) { |
449 frame_props = DictionaryWithKeyValue( | 576 frame_props = DictionaryWithKeyValue( |
450 videotoolbox_glue_->kVTEncodeFrameOptionKey_ForceKeyFrame(), | 577 videotoolbox_glue_->kVTEncodeFrameOptionKey_ForceKeyFrame(), |
451 kCFBooleanTrue); | 578 kCFBooleanTrue); |
452 encode_next_frame_as_keyframe_ = false; | 579 encode_next_frame_as_keyframe_ = false; |
453 } | 580 } |
454 | 581 |
455 VTEncodeInfoFlags info; | 582 // Submit the frame to the compression session. The function returns as soon |
| 583 // as the frame has been enqueued. |
456 OSStatus status = videotoolbox_glue_->VTCompressionSessionEncodeFrame( | 584 OSStatus status = videotoolbox_glue_->VTCompressionSessionEncodeFrame( |
457 compression_session_, pixel_buffer, timestamp_cm, | 585 compression_session_, pixel_buffer, timestamp_cm, |
458 CoreMediaGlue::CMTime{0, 0, 0, 0}, frame_props, | 586 CoreMediaGlue::CMTime{0, 0, 0, 0}, frame_props, |
459 reinterpret_cast<void*>(request.release()), &info); | 587 reinterpret_cast<void*>(request.release()), nullptr); |
460 if (status != noErr) { | 588 if (status != noErr) { |
461 DLOG(ERROR) << " VTCompressionSessionEncodeFrame failed: " << status; | 589 DLOG(ERROR) << " VTCompressionSessionEncodeFrame failed: " << status; |
462 return false; | 590 return false; |
463 } | 591 } |
464 if ((info & VideoToolboxGlue::kVTEncodeInfo_FrameDropped)) { | |
465 DLOG(ERROR) << " frame dropped"; | |
466 return false; | |
467 } | |
468 | 592 |
469 return true; | 593 return true; |
470 } | 594 } |
471 | 595 |
472 void H264VideoToolboxEncoder::SetBitRate(int new_bit_rate) { | 596 void H264VideoToolboxEncoder::UpdateFrameSize(const gfx::Size& size_needed) { |
| 597 DCHECK(thread_checker_.CalledOnValidThread()); |
| 598 |
| 599 // Our video frame factory posts a task to update the frame size when its |
| 600 // cache of the frame size differs from what the client requested. To avoid |
| 601 // spurious encoder resets, check again here. |
| 602 if (size_needed == frame_size_) { |
| 603 DCHECK(compression_session_); |
| 604 return; |
| 605 } |
| 606 |
| 607 VLOG(1) << "Resetting compression session (for frame size change from " |
| 608 << frame_size_.ToString() << " to " << size_needed.ToString() << ")."; |
| 609 |
| 610 // If there is an existing session, finish every pending frame. |
| 611 if (compression_session_) { |
| 612 EmitFrames(); |
| 613 } |
| 614 |
| 615 // Store the new frame size. |
| 616 frame_size_ = size_needed; |
| 617 |
| 618 // Reset the compression session. |
| 619 ResetCompressionSession(); |
| 620 } |
| 621 |
| 622 void H264VideoToolboxEncoder::SetBitRate(int /*new_bit_rate*/) { |
473 DCHECK(thread_checker_.CalledOnValidThread()); | 623 DCHECK(thread_checker_.CalledOnValidThread()); |
474 // VideoToolbox does not seem to support bitrate reconfiguration. | 624 // VideoToolbox does not seem to support bitrate reconfiguration. |
475 } | 625 } |
476 | 626 |
477 void H264VideoToolboxEncoder::GenerateKeyFrame() { | 627 void H264VideoToolboxEncoder::GenerateKeyFrame() { |
478 DCHECK(thread_checker_.CalledOnValidThread()); | 628 DCHECK(thread_checker_.CalledOnValidThread()); |
479 DCHECK(compression_session_); | |
480 | |
481 encode_next_frame_as_keyframe_ = true; | 629 encode_next_frame_as_keyframe_ = true; |
482 } | 630 } |
483 | 631 |
484 void H264VideoToolboxEncoder::LatestFrameIdToReference(uint32 /*frame_id*/) { | 632 void H264VideoToolboxEncoder::LatestFrameIdToReference(uint32 /*frame_id*/) { |
485 // Not supported by VideoToolbox in any meaningful manner. | 633 // Not supported by VideoToolbox in any meaningful manner. |
486 } | 634 } |
487 | 635 |
488 scoped_ptr<VideoFrameFactory> | 636 scoped_ptr<VideoFrameFactory> |
489 H264VideoToolboxEncoder::CreateVideoFrameFactory() { | 637 H264VideoToolboxEncoder::CreateVideoFrameFactory() { |
490 if (!videotoolbox_glue_ || !compression_session_) | 638 DCHECK(thread_checker_.CalledOnValidThread()); |
491 return nullptr; | |
492 base::ScopedCFTypeRef<CVPixelBufferPoolRef> pool( | |
493 videotoolbox_glue_->VTCompressionSessionGetPixelBufferPool( | |
494 compression_session_), | |
495 base::scoped_policy::RETAIN); | |
496 return scoped_ptr<VideoFrameFactory>( | 639 return scoped_ptr<VideoFrameFactory>( |
497 new VideoFrameFactoryCVPixelBufferPoolImpl(pool, frame_size_)); | 640 new VideoFrameFactoryImpl::Proxy(video_frame_factory_)); |
498 } | 641 } |
499 | 642 |
500 void H264VideoToolboxEncoder::EmitFrames() { | 643 void H264VideoToolboxEncoder::EmitFrames() { |
501 DCHECK(thread_checker_.CalledOnValidThread()); | 644 DCHECK(thread_checker_.CalledOnValidThread()); |
502 | 645 if (!compression_session_) |
503 if (!compression_session_) { | |
504 DLOG(ERROR) << " compression session is null"; | |
505 return; | 646 return; |
506 } | |
507 | 647 |
508 OSStatus status = videotoolbox_glue_->VTCompressionSessionCompleteFrames( | 648 OSStatus status = videotoolbox_glue_->VTCompressionSessionCompleteFrames( |
509 compression_session_, CoreMediaGlue::CMTime{0, 0, 0, 0}); | 649 compression_session_, CoreMediaGlue::CMTime{0, 0, 0, 0}); |
510 if (status != noErr) { | 650 if (status != noErr) { |
511 DLOG(ERROR) << " VTCompressionSessionCompleteFrames failed: " << status; | 651 DLOG(ERROR) << " VTCompressionSessionCompleteFrames failed: " << status; |
512 } | 652 } |
513 } | 653 } |
514 | 654 |
| 655 void H264VideoToolboxEncoder::OnSuspend() { |
| 656 VLOG(1) |
| 657 << "OnSuspend: Emitting all frames and destroying compression session."; |
| 658 EmitFrames(); |
| 659 DestroyCompressionSession(); |
| 660 power_suspended_ = true; |
| 661 } |
| 662 |
| 663 void H264VideoToolboxEncoder::OnResume() { |
| 664 power_suspended_ = false; |
| 665 |
| 666 // Reset the compression session only if the frame size is not zero (which |
| 667 // will obviously fail). It is possible for the frame size to be zero if no |
| 668 // frame was submitted for encoding or requested from the video frame factory |
| 669 // before suspension. |
| 670 if (!frame_size_.IsEmpty()) { |
| 671 VLOG(1) << "OnResume: Resetting compression session."; |
| 672 ResetCompressionSession(); |
| 673 } |
| 674 } |
| 675 |
515 bool H264VideoToolboxEncoder::SetSessionProperty(CFStringRef key, | 676 bool H264VideoToolboxEncoder::SetSessionProperty(CFStringRef key, |
516 int32_t value) { | 677 int32_t value) { |
517 base::ScopedCFTypeRef<CFNumberRef> cfvalue( | 678 base::ScopedCFTypeRef<CFNumberRef> cfvalue( |
518 CFNumberCreate(nullptr, kCFNumberSInt32Type, &value)); | 679 CFNumberCreate(nullptr, kCFNumberSInt32Type, &value)); |
519 return videotoolbox_glue_->VTSessionSetProperty(compression_session_, key, | 680 return videotoolbox_glue_->VTSessionSetProperty(compression_session_, key, |
520 cfvalue) == noErr; | 681 cfvalue) == noErr; |
521 } | 682 } |
522 | 683 |
523 bool H264VideoToolboxEncoder::SetSessionProperty(CFStringRef key, bool value) { | 684 bool H264VideoToolboxEncoder::SetSessionProperty(CFStringRef key, bool value) { |
524 CFBooleanRef cfvalue = (value) ? kCFBooleanTrue : kCFBooleanFalse; | 685 CFBooleanRef cfvalue = (value) ? kCFBooleanTrue : kCFBooleanFalse; |
(...skipping 14 matching lines...) Expand all Loading... |
539 CMSampleBufferRef sbuf) { | 700 CMSampleBufferRef sbuf) { |
540 auto encoder = reinterpret_cast<H264VideoToolboxEncoder*>(encoder_opaque); | 701 auto encoder = reinterpret_cast<H264VideoToolboxEncoder*>(encoder_opaque); |
541 const scoped_ptr<InProgressFrameEncode> request( | 702 const scoped_ptr<InProgressFrameEncode> request( |
542 reinterpret_cast<InProgressFrameEncode*>(request_opaque)); | 703 reinterpret_cast<InProgressFrameEncode*>(request_opaque)); |
543 bool keyframe = false; | 704 bool keyframe = false; |
544 bool has_frame_data = false; | 705 bool has_frame_data = false; |
545 | 706 |
546 if (status != noErr) { | 707 if (status != noErr) { |
547 DLOG(ERROR) << " encoding failed: " << status; | 708 DLOG(ERROR) << " encoding failed: " << status; |
548 encoder->cast_environment_->PostTask( | 709 encoder->cast_environment_->PostTask( |
549 CastEnvironment::MAIN, | 710 CastEnvironment::MAIN, FROM_HERE, |
550 FROM_HERE, | |
551 base::Bind(encoder->status_change_cb_, STATUS_CODEC_RUNTIME_ERROR)); | 711 base::Bind(encoder->status_change_cb_, STATUS_CODEC_RUNTIME_ERROR)); |
552 } else if ((info & VideoToolboxGlue::kVTEncodeInfo_FrameDropped)) { | 712 } else if ((info & VideoToolboxGlue::kVTEncodeInfo_FrameDropped)) { |
553 DVLOG(2) << " frame dropped"; | 713 DVLOG(2) << " frame dropped"; |
554 } else { | 714 } else { |
555 auto sample_attachments = static_cast<CFDictionaryRef>( | 715 auto sample_attachments = |
556 CFArrayGetValueAtIndex( | 716 static_cast<CFDictionaryRef>(CFArrayGetValueAtIndex( |
557 CoreMediaGlue::CMSampleBufferGetSampleAttachmentsArray(sbuf, true), | 717 CoreMediaGlue::CMSampleBufferGetSampleAttachmentsArray(sbuf, true), |
558 0)); | 718 0)); |
559 | 719 |
560 // If the NotSync key is not present, it implies Sync, which indicates a | 720 // If the NotSync key is not present, it implies Sync, which indicates a |
561 // keyframe (at least I think, VT documentation is, erm, sparse). Could | 721 // keyframe (at least I think, VT documentation is, erm, sparse). Could |
562 // alternatively use kCMSampleAttachmentKey_DependsOnOthers == false. | 722 // alternatively use kCMSampleAttachmentKey_DependsOnOthers == false. |
563 keyframe = !CFDictionaryContainsKey( | 723 keyframe = !CFDictionaryContainsKey( |
564 sample_attachments, | 724 sample_attachments, |
565 CoreMediaGlue::kCMSampleAttachmentKey_NotSync()); | 725 CoreMediaGlue::kCMSampleAttachmentKey_NotSync()); |
566 has_frame_data = true; | 726 has_frame_data = true; |
567 } | 727 } |
568 | 728 |
569 // Increment the encoder-scoped frame id and assign the new value to this | 729 // Increment the encoder-scoped frame id and assign the new value to this |
570 // frame. VideoToolbox calls the output callback serially, so this is safe. | 730 // frame. VideoToolbox calls the output callback serially, so this is safe. |
571 const uint32 frame_id = encoder->next_frame_id_++; | 731 const uint32 frame_id = ++encoder->last_frame_id_; |
572 | 732 |
573 scoped_ptr<EncodedFrame> encoded_frame(new EncodedFrame()); | 733 scoped_ptr<EncodedFrame> encoded_frame(new EncodedFrame()); |
574 encoded_frame->frame_id = frame_id; | 734 encoded_frame->frame_id = frame_id; |
575 encoded_frame->reference_time = request->reference_time; | 735 encoded_frame->reference_time = request->reference_time; |
576 encoded_frame->rtp_timestamp = request->rtp_timestamp; | 736 encoded_frame->rtp_timestamp = request->rtp_timestamp; |
577 if (keyframe) { | 737 if (keyframe) { |
578 encoded_frame->dependency = EncodedFrame::KEY; | 738 encoded_frame->dependency = EncodedFrame::KEY; |
579 encoded_frame->referenced_frame_id = frame_id; | 739 encoded_frame->referenced_frame_id = frame_id; |
580 } else { | 740 } else { |
581 encoded_frame->dependency = EncodedFrame::DEPENDENT; | 741 encoded_frame->dependency = EncodedFrame::DEPENDENT; |
(...skipping 11 matching lines...) Expand all Loading... |
593 | 753 |
594 if (has_frame_data) | 754 if (has_frame_data) |
595 CopySampleBufferToAnnexBBuffer(sbuf, &encoded_frame->data, keyframe); | 755 CopySampleBufferToAnnexBBuffer(sbuf, &encoded_frame->data, keyframe); |
596 | 756 |
597 encoder->cast_environment_->PostTask( | 757 encoder->cast_environment_->PostTask( |
598 CastEnvironment::MAIN, FROM_HERE, | 758 CastEnvironment::MAIN, FROM_HERE, |
599 base::Bind(request->frame_encoded_callback, | 759 base::Bind(request->frame_encoded_callback, |
600 base::Passed(&encoded_frame))); | 760 base::Passed(&encoded_frame))); |
601 } | 761 } |
602 | 762 |
603 // A ref-counted structure that is shared to provide concurrent access to the | |
604 // VideoFrameFactory instance for the current encoder. OnEncoderReplaced() can | |
605 // change |factory| whenever an encoder instance has been replaced, while users | |
606 // of CreateVideoFrameFactory() may attempt to read/use |factory| by any thread | |
607 // at any time. | |
608 struct SizeAdaptableH264VideoToolboxVideoEncoder::FactoryHolder | |
609 : public base::RefCountedThreadSafe<FactoryHolder> { | |
610 base::Lock lock; | |
611 scoped_ptr<VideoFrameFactory> factory; | |
612 | |
613 private: | |
614 friend class base::RefCountedThreadSafe<FactoryHolder>; | |
615 ~FactoryHolder() {} | |
616 }; | |
617 | |
618 SizeAdaptableH264VideoToolboxVideoEncoder:: | |
619 SizeAdaptableH264VideoToolboxVideoEncoder( | |
620 const scoped_refptr<CastEnvironment>& cast_environment, | |
621 const VideoSenderConfig& video_config, | |
622 const StatusChangeCallback& status_change_cb) | |
623 : SizeAdaptableVideoEncoderBase(cast_environment, | |
624 video_config, | |
625 status_change_cb), | |
626 holder_(new FactoryHolder()) {} | |
627 | |
628 SizeAdaptableH264VideoToolboxVideoEncoder:: | |
629 ~SizeAdaptableH264VideoToolboxVideoEncoder() {} | |
630 | |
631 // A proxy allowing SizeAdaptableH264VideoToolboxVideoEncoder to swap out the | |
632 // VideoFrameFactory instance to match one appropriate for the current encoder | |
633 // instance. | |
634 class SizeAdaptableH264VideoToolboxVideoEncoder::VideoFrameFactoryProxy | |
635 : public VideoFrameFactory { | |
636 public: | |
637 explicit VideoFrameFactoryProxy(const scoped_refptr<FactoryHolder>& holder) | |
638 : holder_(holder) {} | |
639 | |
640 ~VideoFrameFactoryProxy() override {} | |
641 | |
642 scoped_refptr<VideoFrame> MaybeCreateFrame( | |
643 const gfx::Size& frame_size, base::TimeDelta timestamp) override { | |
644 base::AutoLock auto_lock(holder_->lock); | |
645 return holder_->factory ? | |
646 holder_->factory->MaybeCreateFrame(frame_size, timestamp) : nullptr; | |
647 } | |
648 | |
649 private: | |
650 const scoped_refptr<FactoryHolder> holder_; | |
651 | |
652 DISALLOW_COPY_AND_ASSIGN(VideoFrameFactoryProxy); | |
653 }; | |
654 | |
655 scoped_ptr<VideoFrameFactory> | |
656 SizeAdaptableH264VideoToolboxVideoEncoder::CreateVideoFrameFactory() { | |
657 return scoped_ptr<VideoFrameFactory>(new VideoFrameFactoryProxy(holder_)); | |
658 } | |
659 | |
660 scoped_ptr<VideoEncoder> | |
661 SizeAdaptableH264VideoToolboxVideoEncoder::CreateEncoder() { | |
662 return scoped_ptr<VideoEncoder>(new H264VideoToolboxEncoder( | |
663 cast_environment(), | |
664 video_config(), | |
665 frame_size(), | |
666 last_frame_id() + 1, | |
667 CreateEncoderStatusChangeCallback())); | |
668 } | |
669 | |
670 void SizeAdaptableH264VideoToolboxVideoEncoder::OnEncoderReplaced( | |
671 VideoEncoder* replacement_encoder) { | |
672 scoped_ptr<VideoFrameFactory> current_factory( | |
673 replacement_encoder->CreateVideoFrameFactory()); | |
674 base::AutoLock auto_lock(holder_->lock); | |
675 holder_->factory = current_factory.Pass(); | |
676 SizeAdaptableVideoEncoderBase::OnEncoderReplaced(replacement_encoder); | |
677 } | |
678 | |
679 void SizeAdaptableH264VideoToolboxVideoEncoder::DestroyEncoder() { | |
680 { | |
681 base::AutoLock auto_lock(holder_->lock); | |
682 holder_->factory.reset(); | |
683 } | |
684 SizeAdaptableVideoEncoderBase::DestroyEncoder(); | |
685 } | |
686 | |
687 } // namespace cast | 763 } // namespace cast |
688 } // namespace media | 764 } // namespace media |
OLD | NEW |