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
3 // found in the LICENSE file.
5 #include "media/cast/sender/h264_vt_encoder.h"
10 #include "base/big_endian.h"
11 #include "base/bind.h"
12 #include "base/bind_helpers.h"
13 #include "base/location.h"
14 #include "base/logging.h"
15 #include "base/macros.h"
16 #include "base/synchronization/lock.h"
17 #include "media/base/mac/corevideo_glue.h"
18 #include "media/base/mac/video_frame_mac.h"
19 #include "media/cast/sender/video_frame_factory.h"
26 // Container for the associated data of a video frame being processed.
27 struct InProgressFrameEncode
{
28 const RtpTimestamp rtp_timestamp
;
29 const base::TimeTicks reference_time
;
30 const VideoEncoder::FrameEncodedCallback frame_encoded_callback
;
32 InProgressFrameEncode(RtpTimestamp rtp
,
33 base::TimeTicks r_time
,
34 VideoEncoder::FrameEncodedCallback callback
)
36 reference_time(r_time
),
37 frame_encoded_callback(callback
) {}
40 base::ScopedCFTypeRef
<CFDictionaryRef
> DictionaryWithKeysAndValues(
44 return base::ScopedCFTypeRef
<CFDictionaryRef
>(CFDictionaryCreate(
49 &kCFTypeDictionaryKeyCallBacks
,
50 &kCFTypeDictionaryValueCallBacks
));
53 base::ScopedCFTypeRef
<CFDictionaryRef
> DictionaryWithKeyValue(CFTypeRef key
,
55 CFTypeRef keys
[1] = {key
};
56 CFTypeRef values
[1] = {value
};
57 return DictionaryWithKeysAndValues(keys
, values
, 1);
60 base::ScopedCFTypeRef
<CFArrayRef
> ArrayWithIntegers(const int* v
, size_t size
) {
61 std::vector
<CFNumberRef
> numbers
;
62 numbers
.reserve(size
);
63 for (const int* end
= v
+ size
; v
< end
; ++v
)
64 numbers
.push_back(CFNumberCreate(nullptr, kCFNumberSInt32Type
, v
));
65 base::ScopedCFTypeRef
<CFArrayRef
> array(CFArrayCreate(
66 kCFAllocatorDefault
, reinterpret_cast<const void**>(&numbers
[0]),
67 numbers
.size(), &kCFTypeArrayCallBacks
));
68 for (auto& number
: numbers
) {
74 template <typename NalSizeType
>
75 void CopyNalsToAnnexB(char* avcc_buffer
,
76 const size_t avcc_size
,
77 std::string
* annexb_buffer
) {
78 static_assert(sizeof(NalSizeType
) == 1 || sizeof(NalSizeType
) == 2 ||
79 sizeof(NalSizeType
) == 4,
80 "NAL size type has unsupported size");
81 static const char startcode_3
[3] = {0, 0, 1};
83 DCHECK(annexb_buffer
);
84 size_t bytes_left
= avcc_size
;
85 while (bytes_left
> 0) {
86 DCHECK_GT(bytes_left
, sizeof(NalSizeType
));
88 base::ReadBigEndian(avcc_buffer
, &nal_size
);
89 bytes_left
-= sizeof(NalSizeType
);
90 avcc_buffer
+= sizeof(NalSizeType
);
92 DCHECK_GE(bytes_left
, nal_size
);
93 annexb_buffer
->append(startcode_3
, sizeof(startcode_3
));
94 annexb_buffer
->append(avcc_buffer
, nal_size
);
95 bytes_left
-= nal_size
;
96 avcc_buffer
+= nal_size
;
100 // Copy a H.264 frame stored in a CM sample buffer to an Annex B buffer. Copies
101 // parameter sets for keyframes before the frame data as well.
102 void CopySampleBufferToAnnexBBuffer(CoreMediaGlue::CMSampleBufferRef sbuf
,
103 std::string
* annexb_buffer
,
105 // Perform two pass, one to figure out the total output size, and another to
106 // copy the data after having performed a single output allocation. Note that
107 // we'll allocate a bit more because we'll count 4 bytes instead of 3 for
112 // Get the sample buffer's block buffer and format description.
113 auto bb
= CoreMediaGlue::CMSampleBufferGetDataBuffer(sbuf
);
115 auto fdesc
= CoreMediaGlue::CMSampleBufferGetFormatDescription(sbuf
);
118 size_t bb_size
= CoreMediaGlue::CMBlockBufferGetDataLength(bb
);
119 size_t total_bytes
= bb_size
;
122 int nal_size_field_bytes
;
123 status
= CoreMediaGlue::CMVideoFormatDescriptionGetH264ParameterSetAtIndex(
124 fdesc
, 0, nullptr, nullptr, &pset_count
, &nal_size_field_bytes
);
126 CoreMediaGlue::kCMFormatDescriptionBridgeError_InvalidParameter
) {
127 DLOG(WARNING
) << " assuming 2 parameter sets and 4 bytes NAL length header";
129 nal_size_field_bytes
= 4;
130 } else if (status
!= noErr
) {
132 << " CMVideoFormatDescriptionGetH264ParameterSetAtIndex failed: "
140 for (size_t pset_i
= 0; pset_i
< pset_count
; ++pset_i
) {
142 CoreMediaGlue::CMVideoFormatDescriptionGetH264ParameterSetAtIndex(
143 fdesc
, pset_i
, &pset
, &pset_size
, nullptr, nullptr);
144 if (status
!= noErr
) {
146 << " CMVideoFormatDescriptionGetH264ParameterSetAtIndex failed: "
150 total_bytes
+= pset_size
+ nal_size_field_bytes
;
154 annexb_buffer
->reserve(total_bytes
);
156 // Copy all parameter sets before keyframes.
160 for (size_t pset_i
= 0; pset_i
< pset_count
; ++pset_i
) {
162 CoreMediaGlue::CMVideoFormatDescriptionGetH264ParameterSetAtIndex(
163 fdesc
, pset_i
, &pset
, &pset_size
, nullptr, nullptr);
164 if (status
!= noErr
) {
166 << " CMVideoFormatDescriptionGetH264ParameterSetAtIndex failed: "
170 static const char startcode_4
[4] = {0, 0, 0, 1};
171 annexb_buffer
->append(startcode_4
, sizeof(startcode_4
));
172 annexb_buffer
->append(reinterpret_cast<const char*>(pset
), pset_size
);
176 // Block buffers can be composed of non-contiguous chunks. For the sake of
177 // keeping this code simple, flatten non-contiguous block buffers.
178 base::ScopedCFTypeRef
<CoreMediaGlue::CMBlockBufferRef
> contiguous_bb(
179 bb
, base::scoped_policy::RETAIN
);
180 if (!CoreMediaGlue::CMBlockBufferIsRangeContiguous(bb
, 0, 0)) {
181 contiguous_bb
.reset();
182 status
= CoreMediaGlue::CMBlockBufferCreateContiguous(
183 kCFAllocatorDefault
, bb
, kCFAllocatorDefault
, nullptr, 0, 0, 0,
184 contiguous_bb
.InitializeInto());
185 if (status
!= noErr
) {
186 DLOG(ERROR
) << " CMBlockBufferCreateContiguous failed: " << status
;
191 // Copy all the NAL units. In the process convert them from AVCC format
192 // (length header) to AnnexB format (start code).
194 status
= CoreMediaGlue::CMBlockBufferGetDataPointer(contiguous_bb
, 0, nullptr,
196 if (status
!= noErr
) {
197 DLOG(ERROR
) << " CMBlockBufferGetDataPointer failed: " << status
;
201 if (nal_size_field_bytes
== 1) {
202 CopyNalsToAnnexB
<uint8_t>(bb_data
, bb_size
, annexb_buffer
);
203 } else if (nal_size_field_bytes
== 2) {
204 CopyNalsToAnnexB
<uint16_t>(bb_data
, bb_size
, annexb_buffer
);
205 } else if (nal_size_field_bytes
== 4) {
206 CopyNalsToAnnexB
<uint32_t>(bb_data
, bb_size
, annexb_buffer
);
212 // Implementation of the VideoFrameFactory interface using |CVPixelBufferPool|.
213 class VideoFrameFactoryCVPixelBufferPoolImpl
: public VideoFrameFactory
{
215 VideoFrameFactoryCVPixelBufferPoolImpl(
216 const base::ScopedCFTypeRef
<CVPixelBufferPoolRef
>& pool
,
217 const gfx::Size
& frame_size
)
219 frame_size_(frame_size
) {}
221 ~VideoFrameFactoryCVPixelBufferPoolImpl() override
{}
223 scoped_refptr
<VideoFrame
> MaybeCreateFrame(
224 const gfx::Size
& frame_size
, base::TimeDelta timestamp
) override
{
225 if (frame_size
!= frame_size_
)
226 return nullptr; // Buffer pool is not a match for requested frame size.
228 base::ScopedCFTypeRef
<CVPixelBufferRef
> buffer
;
229 if (CVPixelBufferPoolCreatePixelBuffer(kCFAllocatorDefault
, pool_
,
230 buffer
.InitializeInto()) !=
232 return nullptr; // Buffer pool has run out of pixel buffers.
235 return VideoFrame::WrapCVPixelBuffer(buffer
, timestamp
);
239 const base::ScopedCFTypeRef
<CVPixelBufferPoolRef
> pool_
;
240 const gfx::Size frame_size_
;
242 DISALLOW_COPY_AND_ASSIGN(VideoFrameFactoryCVPixelBufferPoolImpl
);
248 bool H264VideoToolboxEncoder::IsSupported(
249 const VideoSenderConfig
& video_config
) {
250 return video_config
.codec
== CODEC_VIDEO_H264
&& VideoToolboxGlue::Get();
253 H264VideoToolboxEncoder::H264VideoToolboxEncoder(
254 const scoped_refptr
<CastEnvironment
>& cast_environment
,
255 const VideoSenderConfig
& video_config
,
256 const gfx::Size
& frame_size
,
257 uint32 first_frame_id
,
258 const StatusChangeCallback
& status_change_cb
)
259 : cast_environment_(cast_environment
),
260 videotoolbox_glue_(VideoToolboxGlue::Get()),
261 frame_size_(frame_size
),
262 status_change_cb_(status_change_cb
),
263 next_frame_id_(first_frame_id
),
264 encode_next_frame_as_keyframe_(false) {
265 DCHECK(!frame_size_
.IsEmpty());
266 DCHECK(!status_change_cb_
.is_null());
268 OperationalStatus operational_status
;
269 if (video_config
.codec
== CODEC_VIDEO_H264
&& videotoolbox_glue_
) {
270 operational_status
= Initialize(video_config
) ?
271 STATUS_INITIALIZED
: STATUS_INVALID_CONFIGURATION
;
273 operational_status
= STATUS_UNSUPPORTED_CODEC
;
275 cast_environment_
->PostTask(
276 CastEnvironment::MAIN
,
278 base::Bind(status_change_cb_
, operational_status
));
281 H264VideoToolboxEncoder::~H264VideoToolboxEncoder() {
285 bool H264VideoToolboxEncoder::Initialize(
286 const VideoSenderConfig
& video_config
) {
287 DCHECK(thread_checker_
.CalledOnValidThread());
288 DCHECK(!compression_session_
);
290 // Note that the encoder object is given to the compression session as the
291 // callback context using a raw pointer. The C API does not allow us to use
292 // a smart pointer, nor is this encoder ref counted. However, this is still
293 // safe, because we 1) we own the compression session and 2) we tear it down
294 // safely. When destructing the encoder, the compression session is flushed
295 // and invalidated. Internally, VideoToolbox will join all of its threads
296 // before returning to the client. Therefore, when control returns to us, we
297 // are guaranteed that the output callback will not execute again.
299 // 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).
301 base::ScopedCFTypeRef
<CFDictionaryRef
> encoder_spec
;
303 encoder_spec
= DictionaryWithKeyValue(
305 ->kVTVideoEncoderSpecification_EnableHardwareAcceleratedVideoEncoder(),
309 // Certain encoders prefer kCVPixelFormatType_422YpCbCr8, which is not
310 // supported through VideoFrame. We can force 420 formats to be used instead.
311 const int formats
[] = {
312 kCVPixelFormatType_420YpCbCr8Planar
,
313 CoreVideoGlue::kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange
315 // Keep these attachment settings in-sync with those in ConfigureSession().
316 CFTypeRef attachments_keys
[] = {
317 kCVImageBufferColorPrimariesKey
,
318 kCVImageBufferTransferFunctionKey
,
319 kCVImageBufferYCbCrMatrixKey
321 CFTypeRef attachments_values
[] = {
322 kCVImageBufferColorPrimaries_ITU_R_709_2
,
323 kCVImageBufferTransferFunction_ITU_R_709_2
,
324 kCVImageBufferYCbCrMatrix_ITU_R_709_2
326 CFTypeRef buffer_attributes_keys
[] = {
327 kCVPixelBufferPixelFormatTypeKey
,
328 kCVBufferPropagatedAttachmentsKey
330 CFTypeRef buffer_attributes_values
[] = {
331 ArrayWithIntegers(formats
, arraysize(formats
)).release(),
332 DictionaryWithKeysAndValues(attachments_keys
,
334 arraysize(attachments_keys
)).release()
336 const base::ScopedCFTypeRef
<CFDictionaryRef
> buffer_attributes
=
337 DictionaryWithKeysAndValues(buffer_attributes_keys
,
338 buffer_attributes_values
,
339 arraysize(buffer_attributes_keys
));
340 for (auto& v
: buffer_attributes_values
)
343 VTCompressionSessionRef session
;
344 OSStatus status
= videotoolbox_glue_
->VTCompressionSessionCreate(
345 kCFAllocatorDefault
, frame_size_
.width(), frame_size_
.height(),
346 CoreMediaGlue::kCMVideoCodecType_H264
, encoder_spec
, buffer_attributes
,
347 nullptr /* compressedDataAllocator */,
348 &H264VideoToolboxEncoder::CompressionCallback
,
349 reinterpret_cast<void*>(this), &session
);
350 if (status
!= noErr
) {
351 DLOG(ERROR
) << " VTCompressionSessionCreate failed: " << status
;
354 compression_session_
.reset(session
);
356 ConfigureSession(video_config
);
361 void H264VideoToolboxEncoder::ConfigureSession(
362 const VideoSenderConfig
& video_config
) {
364 videotoolbox_glue_
->kVTCompressionPropertyKey_ProfileLevel(),
365 videotoolbox_glue_
->kVTProfileLevel_H264_Main_AutoLevel());
366 SetSessionProperty(videotoolbox_glue_
->kVTCompressionPropertyKey_RealTime(),
369 videotoolbox_glue_
->kVTCompressionPropertyKey_AllowFrameReordering(),
372 videotoolbox_glue_
->kVTCompressionPropertyKey_MaxKeyFrameInterval(), 240);
375 ->kVTCompressionPropertyKey_MaxKeyFrameIntervalDuration(),
377 // TODO(jfroy): implement better bitrate control
378 // https://crbug.com/425352
380 videotoolbox_glue_
->kVTCompressionPropertyKey_AverageBitRate(),
381 (video_config
.min_bitrate
+ video_config
.max_bitrate
) / 2);
383 videotoolbox_glue_
->kVTCompressionPropertyKey_ExpectedFrameRate(),
384 video_config
.max_frame_rate
);
385 // Keep these attachment settings in-sync with those in Initialize().
387 videotoolbox_glue_
->kVTCompressionPropertyKey_ColorPrimaries(),
388 kCVImageBufferColorPrimaries_ITU_R_709_2
);
390 videotoolbox_glue_
->kVTCompressionPropertyKey_TransferFunction(),
391 kCVImageBufferTransferFunction_ITU_R_709_2
);
393 videotoolbox_glue_
->kVTCompressionPropertyKey_YCbCrMatrix(),
394 kCVImageBufferYCbCrMatrix_ITU_R_709_2
);
395 if (video_config
.max_number_of_video_buffers_used
> 0) {
397 videotoolbox_glue_
->kVTCompressionPropertyKey_MaxFrameDelayCount(),
398 video_config
.max_number_of_video_buffers_used
);
402 void H264VideoToolboxEncoder::Teardown() {
403 DCHECK(thread_checker_
.CalledOnValidThread());
405 // If the compression session exists, invalidate it. This blocks until all
406 // pending output callbacks have returned and any internal threads have
407 // joined, ensuring no output callback ever sees a dangling encoder pointer.
408 if (compression_session_
) {
409 videotoolbox_glue_
->VTCompressionSessionInvalidate(compression_session_
);
410 compression_session_
.reset();
414 bool H264VideoToolboxEncoder::EncodeVideoFrame(
415 const scoped_refptr
<media::VideoFrame
>& video_frame
,
416 const base::TimeTicks
& reference_time
,
417 const FrameEncodedCallback
& frame_encoded_callback
) {
418 DCHECK(thread_checker_
.CalledOnValidThread());
419 DCHECK(!frame_encoded_callback
.is_null());
421 if (!compression_session_
) {
422 DLOG(ERROR
) << " compression session is null";
426 if (video_frame
->visible_rect().size() != frame_size_
)
429 // 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
431 // 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
433 // frame into memory visible by the hardware encoder. The VideoFrame's
434 // lifetime is extended for the lifetime of the returned CVPixelBuffer.
435 auto pixel_buffer
= media::WrapVideoFrameInCVPixelBuffer(*video_frame
);
440 auto timestamp_cm
= CoreMediaGlue::CMTimeMake(
441 (reference_time
- base::TimeTicks()).InMicroseconds(), USEC_PER_SEC
);
443 scoped_ptr
<InProgressFrameEncode
> request(new InProgressFrameEncode(
444 TimeDeltaToRtpDelta(video_frame
->timestamp(), kVideoFrequency
),
445 reference_time
, frame_encoded_callback
));
447 base::ScopedCFTypeRef
<CFDictionaryRef
> frame_props
;
448 if (encode_next_frame_as_keyframe_
) {
449 frame_props
= DictionaryWithKeyValue(
450 videotoolbox_glue_
->kVTEncodeFrameOptionKey_ForceKeyFrame(),
452 encode_next_frame_as_keyframe_
= false;
455 VTEncodeInfoFlags info
;
456 OSStatus status
= videotoolbox_glue_
->VTCompressionSessionEncodeFrame(
457 compression_session_
, pixel_buffer
, timestamp_cm
,
458 CoreMediaGlue::CMTime
{0, 0, 0, 0}, frame_props
,
459 reinterpret_cast<void*>(request
.release()), &info
);
460 if (status
!= noErr
) {
461 DLOG(ERROR
) << " VTCompressionSessionEncodeFrame failed: " << status
;
464 if ((info
& VideoToolboxGlue::kVTEncodeInfo_FrameDropped
)) {
465 DLOG(ERROR
) << " frame dropped";
472 void H264VideoToolboxEncoder::SetBitRate(int new_bit_rate
) {
473 DCHECK(thread_checker_
.CalledOnValidThread());
474 // VideoToolbox does not seem to support bitrate reconfiguration.
477 void H264VideoToolboxEncoder::GenerateKeyFrame() {
478 DCHECK(thread_checker_
.CalledOnValidThread());
479 DCHECK(compression_session_
);
481 encode_next_frame_as_keyframe_
= true;
484 void H264VideoToolboxEncoder::LatestFrameIdToReference(uint32
/*frame_id*/) {
485 // Not supported by VideoToolbox in any meaningful manner.
488 scoped_ptr
<VideoFrameFactory
>
489 H264VideoToolboxEncoder::CreateVideoFrameFactory() {
490 if (!videotoolbox_glue_
|| !compression_session_
)
492 base::ScopedCFTypeRef
<CVPixelBufferPoolRef
> pool(
493 videotoolbox_glue_
->VTCompressionSessionGetPixelBufferPool(
494 compression_session_
),
495 base::scoped_policy::RETAIN
);
496 return scoped_ptr
<VideoFrameFactory
>(
497 new VideoFrameFactoryCVPixelBufferPoolImpl(pool
, frame_size_
));
500 void H264VideoToolboxEncoder::EmitFrames() {
501 DCHECK(thread_checker_
.CalledOnValidThread());
503 if (!compression_session_
) {
504 DLOG(ERROR
) << " compression session is null";
508 OSStatus status
= videotoolbox_glue_
->VTCompressionSessionCompleteFrames(
509 compression_session_
, CoreMediaGlue::CMTime
{0, 0, 0, 0});
510 if (status
!= noErr
) {
511 DLOG(ERROR
) << " VTCompressionSessionCompleteFrames failed: " << status
;
515 bool H264VideoToolboxEncoder::SetSessionProperty(CFStringRef key
,
517 base::ScopedCFTypeRef
<CFNumberRef
> cfvalue(
518 CFNumberCreate(nullptr, kCFNumberSInt32Type
, &value
));
519 return videotoolbox_glue_
->VTSessionSetProperty(compression_session_
, key
,
523 bool H264VideoToolboxEncoder::SetSessionProperty(CFStringRef key
, bool value
) {
524 CFBooleanRef cfvalue
= (value
) ? kCFBooleanTrue
: kCFBooleanFalse
;
525 return videotoolbox_glue_
->VTSessionSetProperty(compression_session_
, key
,
529 bool H264VideoToolboxEncoder::SetSessionProperty(CFStringRef key
,
531 return videotoolbox_glue_
->VTSessionSetProperty(compression_session_
, key
,
535 void H264VideoToolboxEncoder::CompressionCallback(void* encoder_opaque
,
536 void* request_opaque
,
538 VTEncodeInfoFlags info
,
539 CMSampleBufferRef sbuf
) {
540 auto encoder
= reinterpret_cast<H264VideoToolboxEncoder
*>(encoder_opaque
);
541 const scoped_ptr
<InProgressFrameEncode
> request(
542 reinterpret_cast<InProgressFrameEncode
*>(request_opaque
));
543 bool keyframe
= false;
544 bool has_frame_data
= false;
546 if (status
!= noErr
) {
547 DLOG(ERROR
) << " encoding failed: " << status
;
548 encoder
->cast_environment_
->PostTask(
549 CastEnvironment::MAIN
,
551 base::Bind(encoder
->status_change_cb_
, STATUS_CODEC_RUNTIME_ERROR
));
552 } else if ((info
& VideoToolboxGlue::kVTEncodeInfo_FrameDropped
)) {
553 DVLOG(2) << " frame dropped";
555 auto sample_attachments
= static_cast<CFDictionaryRef
>(
556 CFArrayGetValueAtIndex(
557 CoreMediaGlue::CMSampleBufferGetSampleAttachmentsArray(sbuf
, true),
560 // 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
562 // alternatively use kCMSampleAttachmentKey_DependsOnOthers == false.
563 keyframe
= !CFDictionaryContainsKey(
565 CoreMediaGlue::kCMSampleAttachmentKey_NotSync());
566 has_frame_data
= true;
569 // 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.
571 const uint32 frame_id
= encoder
->next_frame_id_
++;
573 scoped_ptr
<EncodedFrame
> encoded_frame(new EncodedFrame());
574 encoded_frame
->frame_id
= frame_id
;
575 encoded_frame
->reference_time
= request
->reference_time
;
576 encoded_frame
->rtp_timestamp
= request
->rtp_timestamp
;
578 encoded_frame
->dependency
= EncodedFrame::KEY
;
579 encoded_frame
->referenced_frame_id
= frame_id
;
581 encoded_frame
->dependency
= EncodedFrame::DEPENDENT
;
582 // H.264 supports complex frame reference schemes (multiple reference
583 // frames, slice references, backward and forward references, etc). Cast
584 // doesn't support the concept of forward-referencing frame dependencies or
585 // multiple frame dependencies; so pretend that all frames are only
586 // decodable after their immediately preceding frame is decoded. This will
587 // ensure a Cast receiver only attempts to decode the frames sequentially
588 // and in order. Furthermore, the encoder is configured to never use forward
589 // references (see |kVTCompressionPropertyKey_AllowFrameReordering|). There
590 // is no way to prevent multiple reference frames.
591 encoded_frame
->referenced_frame_id
= frame_id
- 1;
595 CopySampleBufferToAnnexBBuffer(sbuf
, &encoded_frame
->data
, keyframe
);
597 encoder
->cast_environment_
->PostTask(
598 CastEnvironment::MAIN
, FROM_HERE
,
599 base::Bind(request
->frame_encoded_callback
,
600 base::Passed(&encoded_frame
)));
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
608 struct SizeAdaptableH264VideoToolboxVideoEncoder::FactoryHolder
609 : public base::RefCountedThreadSafe
<FactoryHolder
> {
611 scoped_ptr
<VideoFrameFactory
> factory
;
614 friend class base::RefCountedThreadSafe
<FactoryHolder
>;
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
,
626 holder_(new FactoryHolder()) {}
628 SizeAdaptableH264VideoToolboxVideoEncoder::
629 ~SizeAdaptableH264VideoToolboxVideoEncoder() {}
631 // A proxy allowing SizeAdaptableH264VideoToolboxVideoEncoder to swap out the
632 // VideoFrameFactory instance to match one appropriate for the current encoder
634 class SizeAdaptableH264VideoToolboxVideoEncoder::VideoFrameFactoryProxy
635 : public VideoFrameFactory
{
637 explicit VideoFrameFactoryProxy(const scoped_refptr
<FactoryHolder
>& holder
)
640 ~VideoFrameFactoryProxy() override
{}
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;
650 const scoped_refptr
<FactoryHolder
> holder_
;
652 DISALLOW_COPY_AND_ASSIGN(VideoFrameFactoryProxy
);
655 scoped_ptr
<VideoFrameFactory
>
656 SizeAdaptableH264VideoToolboxVideoEncoder::CreateVideoFrameFactory() {
657 return scoped_ptr
<VideoFrameFactory
>(new VideoFrameFactoryProxy(holder_
));
660 scoped_ptr
<VideoEncoder
>
661 SizeAdaptableH264VideoToolboxVideoEncoder::CreateEncoder() {
662 return scoped_ptr
<VideoEncoder
>(new H264VideoToolboxEncoder(
667 CreateEncoderStatusChangeCallback()));
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
);
679 void SizeAdaptableH264VideoToolboxVideoEncoder::DestroyEncoder() {
681 base::AutoLock
auto_lock(holder_
->lock
);
682 holder_
->factory
.reset();
684 SizeAdaptableVideoEncoderBase::DestroyEncoder();