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 | 9 |
9 #include "base/big_endian.h" | 10 #include "base/big_endian.h" |
10 #include "base/bind.h" | 11 #include "base/bind.h" |
11 #include "base/bind_helpers.h" | 12 #include "base/bind_helpers.h" |
12 #include "base/location.h" | 13 #include "base/location.h" |
13 #include "base/logging.h" | 14 #include "base/logging.h" |
| 15 #include "base/macros.h" |
14 #include "media/base/mac/corevideo_glue.h" | 16 #include "media/base/mac/corevideo_glue.h" |
15 #include "media/base/mac/video_frame_mac.h" | 17 #include "media/base/mac/video_frame_mac.h" |
16 #include "media/cast/sender/video_frame_factory.h" | 18 #include "media/cast/sender/video_frame_factory.h" |
17 | 19 |
18 namespace media { | 20 namespace media { |
19 namespace cast { | 21 namespace cast { |
20 | 22 |
21 namespace { | 23 namespace { |
22 | 24 |
23 // Container for the associated data of a video frame being processed. | 25 // Container for the associated data of a video frame being processed. |
(...skipping 12 matching lines...) Expand all Loading... |
36 | 38 |
37 base::ScopedCFTypeRef<CFDictionaryRef> DictionaryWithKeyValue(CFTypeRef key, | 39 base::ScopedCFTypeRef<CFDictionaryRef> DictionaryWithKeyValue(CFTypeRef key, |
38 CFTypeRef value) { | 40 CFTypeRef value) { |
39 CFTypeRef keys[1] = {key}; | 41 CFTypeRef keys[1] = {key}; |
40 CFTypeRef values[1] = {value}; | 42 CFTypeRef values[1] = {value}; |
41 return base::ScopedCFTypeRef<CFDictionaryRef>(CFDictionaryCreate( | 43 return base::ScopedCFTypeRef<CFDictionaryRef>(CFDictionaryCreate( |
42 kCFAllocatorDefault, keys, values, 1, &kCFTypeDictionaryKeyCallBacks, | 44 kCFAllocatorDefault, keys, values, 1, &kCFTypeDictionaryKeyCallBacks, |
43 &kCFTypeDictionaryValueCallBacks)); | 45 &kCFTypeDictionaryValueCallBacks)); |
44 } | 46 } |
45 | 47 |
| 48 base::ScopedCFTypeRef<CFArrayRef> ArrayWithIntegers(const std::vector<int>& v) { |
| 49 std::vector<CFNumberRef> numbers; |
| 50 numbers.reserve(v.size()); |
| 51 for (const int i : v) { |
| 52 numbers.push_back(CFNumberCreate(nullptr, kCFNumberSInt32Type, &i)); |
| 53 } |
| 54 base::ScopedCFTypeRef<CFArrayRef> array(CFArrayCreate( |
| 55 kCFAllocatorDefault, reinterpret_cast<const void**>(&numbers[0]), |
| 56 numbers.size(), &kCFTypeArrayCallBacks)); |
| 57 for (CFNumberRef number : numbers) { |
| 58 CFRelease(number); |
| 59 } |
| 60 return array; |
| 61 } |
| 62 |
46 template <typename NalSizeType> | 63 template <typename NalSizeType> |
47 void CopyNalsToAnnexB(char* avcc_buffer, | 64 void CopyNalsToAnnexB(char* avcc_buffer, |
48 const size_t avcc_size, | 65 const size_t avcc_size, |
49 std::string* annexb_buffer) { | 66 std::string* annexb_buffer) { |
50 static_assert(sizeof(NalSizeType) == 1 || sizeof(NalSizeType) == 2 || | 67 static_assert(sizeof(NalSizeType) == 1 || sizeof(NalSizeType) == 2 || |
51 sizeof(NalSizeType) == 4, | 68 sizeof(NalSizeType) == 4, |
52 "NAL size type has unsupported size"); | 69 "NAL size type has unsupported size"); |
53 static const char startcode_3[3] = {0, 0, 1}; | 70 static const char startcode_3[3] = {0, 0, 1}; |
54 DCHECK(avcc_buffer); | 71 DCHECK(avcc_buffer); |
55 DCHECK(annexb_buffer); | 72 DCHECK(annexb_buffer); |
(...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
250 // On OS X, allow the hardware encoder. Don't require it, it does not support | 267 // On OS X, allow the hardware encoder. Don't require it, it does not support |
251 // all configurations (some of which are used for testing). | 268 // all configurations (some of which are used for testing). |
252 base::ScopedCFTypeRef<CFDictionaryRef> encoder_spec; | 269 base::ScopedCFTypeRef<CFDictionaryRef> encoder_spec; |
253 #if !defined(OS_IOS) | 270 #if !defined(OS_IOS) |
254 encoder_spec = DictionaryWithKeyValue( | 271 encoder_spec = DictionaryWithKeyValue( |
255 videotoolbox_glue_ | 272 videotoolbox_glue_ |
256 ->kVTVideoEncoderSpecification_EnableHardwareAcceleratedVideoEncoder()
, | 273 ->kVTVideoEncoderSpecification_EnableHardwareAcceleratedVideoEncoder()
, |
257 kCFBooleanTrue); | 274 kCFBooleanTrue); |
258 #endif | 275 #endif |
259 | 276 |
| 277 // Certain encoders prefer kCVPixelFormatType_422YpCbCr8, which is not |
| 278 // supported through VideoFrame. We can force 420 formats to be used instead. |
| 279 const int formats[] = { |
| 280 kCVPixelFormatType_420YpCbCr8Planar, |
| 281 CoreVideoGlue::kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange}; |
| 282 base::ScopedCFTypeRef<CFArrayRef> formats_array = ArrayWithIntegers( |
| 283 std::vector<int>(formats, formats + arraysize(formats))); |
| 284 base::ScopedCFTypeRef<CFDictionaryRef> buffer_attributes = |
| 285 DictionaryWithKeyValue(kCVPixelBufferPixelFormatTypeKey, formats_array); |
| 286 |
260 VTCompressionSessionRef session; | 287 VTCompressionSessionRef session; |
261 OSStatus status = videotoolbox_glue_->VTCompressionSessionCreate( | 288 OSStatus status = videotoolbox_glue_->VTCompressionSessionCreate( |
262 kCFAllocatorDefault, video_config.width, video_config.height, | 289 kCFAllocatorDefault, video_config.width, video_config.height, |
263 CoreMediaGlue::kCMVideoCodecType_H264, encoder_spec, | 290 CoreMediaGlue::kCMVideoCodecType_H264, encoder_spec, buffer_attributes, |
264 nullptr /* sourceImageBufferAttributes */, | |
265 nullptr /* compressedDataAllocator */, | 291 nullptr /* compressedDataAllocator */, |
266 &H264VideoToolboxEncoder::CompressionCallback, | 292 &H264VideoToolboxEncoder::CompressionCallback, |
267 reinterpret_cast<void*>(this), &session); | 293 reinterpret_cast<void*>(this), &session); |
268 if (status != noErr) { | 294 if (status != noErr) { |
269 DLOG(ERROR) << " VTCompressionSessionCreate failed: " << status; | 295 DLOG(ERROR) << " VTCompressionSessionCreate failed: " << status; |
270 return false; | 296 return false; |
271 } | 297 } |
272 compression_session_.reset(session); | 298 compression_session_.reset(session); |
273 | 299 |
274 ConfigureSession(video_config); | 300 ConfigureSession(video_config); |
(...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
479 CopySampleBufferToAnnexBBuffer(sbuf, &encoded_frame->data, keyframe); | 505 CopySampleBufferToAnnexBBuffer(sbuf, &encoded_frame->data, keyframe); |
480 | 506 |
481 encoder->cast_environment_->PostTask( | 507 encoder->cast_environment_->PostTask( |
482 CastEnvironment::MAIN, FROM_HERE, | 508 CastEnvironment::MAIN, FROM_HERE, |
483 base::Bind(request->frame_encoded_callback, | 509 base::Bind(request->frame_encoded_callback, |
484 base::Passed(&encoded_frame))); | 510 base::Passed(&encoded_frame))); |
485 } | 511 } |
486 | 512 |
487 } // namespace cast | 513 } // namespace cast |
488 } // namespace media | 514 } // namespace media |
OLD | NEW |