OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #ifndef MEDIA_CAST_SENDER_H264_VT_ENCODER_H_ |
| 6 #define MEDIA_CAST_SENDER_H264_VT_ENCODER_H_ |
| 7 |
| 8 #include <CoreMedia/CoreMedia.h> |
| 9 #include <VideoToolbox/VideoToolbox.h> |
| 10 #include <string> |
| 11 |
| 12 #include "base/mac/scoped_cftyperef.h" |
| 13 #include "base/threading/thread_checker.h" |
| 14 #include "media/cast/sender/video_encoder.h" |
| 15 |
| 16 namespace media { |
| 17 namespace cast { |
| 18 |
| 19 // VideoToolbox implementation of the media::cast::VideoEncoder interface. |
| 20 class H264VideoToolboxEncoder : public VideoEncoder { |
| 21 public: |
| 22 typedef base::ScopedCFTypeRef<CVPixelBufferRef> PixelBufferPtr; |
| 23 |
| 24 explicit H264VideoToolboxEncoder( |
| 25 scoped_refptr<CastEnvironment> cast_environment, |
| 26 const VideoSenderConfig& video_config); |
| 27 virtual ~H264VideoToolboxEncoder(); |
| 28 |
| 29 // After Initialize has been called, returns the compression session's |
| 30 // internal pixel buffer pool. Clients can use it to provide video frames that |
| 31 // will not incur a copy into encoder-accessible memory. |
| 32 CVPixelBufferPoolRef cv_pixel_buffer_pool() const; |
| 33 |
| 34 // media::cast::VideoEncoder implementation |
| 35 virtual bool EncodeVideoFrame( |
| 36 const scoped_refptr<media::VideoFrame>& video_frame, |
| 37 const base::TimeTicks& capture_time, |
| 38 const FrameEncodedCallback& frame_encoded_callback) OVERRIDE; |
| 39 virtual void SetBitRate(int new_bit_rate) OVERRIDE; |
| 40 virtual void GenerateKeyFrame() OVERRIDE; |
| 41 virtual void LatestFrameIdToReference(uint32 frame_id) OVERRIDE; |
| 42 |
| 43 private: |
| 44 typedef base::ScopedCFTypeRef<CFDictionaryRef> DictionaryPtr; |
| 45 struct FrameContext; |
| 46 |
| 47 // Initialize the compression session. |
| 48 void Initialize(); |
| 49 |
| 50 // Configure the compression session. |
| 51 void ConfigureSession(); |
| 52 |
| 53 // Teardown the encoder. |
| 54 void Teardown(); |
| 55 |
| 56 // Wrap a VideoFrame in a new CVPixelBuffer |
| 57 PixelBufferPtr WrapVideoFrame(const scoped_refptr<media::VideoFrame>& frame); |
| 58 |
| 59 // Compression session callback function to handle compressed frames. |
| 60 static void CompressionCallback(void* encoder_opaque, |
| 61 void* frame_opaque, |
| 62 OSStatus status, |
| 63 VTEncodeInfoFlags info, |
| 64 CMSampleBufferRef sbuf); |
| 65 |
| 66 // Copy a H.264 frame stored in a CM sample buffer to an Annex B buffer. |
| 67 // Copies parameter sets for keyframes before the frame data. |
| 68 static void CopySampleBufferToAnnexBBuffer(CMSampleBufferRef sbuf, |
| 69 std::string* annexb_buffer, |
| 70 bool keyframe); |
| 71 |
| 72 // The cast environment (contains worker threads & more). |
| 73 scoped_refptr<CastEnvironment> cast_environment_; |
| 74 |
| 75 // The cast video configuration (contains resolution, bitrate & more). |
| 76 const VideoSenderConfig cast_config_; |
| 77 |
| 78 // Additional properties to apply on the compression session. See |
| 79 // VTCompressionSessionProperties.h. Will override any properties set by the |
| 80 // this class (i.e. this dictionary is applied last without filtering). |
| 81 DictionaryPtr compression_properties_; |
| 82 |
| 83 // VideoToolbox makes no guarantees that it is thread safe, so use a thread |
| 84 // checker to enforce that this object is pinned on a specific thread. The |
| 85 // binding occurs in Initialize(), not the ctor. |
| 86 base::ThreadChecker thread_checker_; |
| 87 |
| 88 // The compression session. |
| 89 base::ScopedCFTypeRef<VTCompressionSessionRef> compression_session_; |
| 90 |
| 91 // Frame identifier counter. |
| 92 uint32 frame_id_; |
| 93 |
| 94 // Frame identifier of the last encoded keyframe. |
| 95 uint32 last_keyframe_id_; |
| 96 |
| 97 // Are we actually using a hardware encoder. On some platforms under certain |
| 98 // conditions, VT will fallback to software. |
| 99 bool using_hardware_; |
| 100 |
| 101 // Force next frame to be a keyframe. |
| 102 bool encode_next_frame_as_keyframe_; |
| 103 |
| 104 DISALLOW_COPY_AND_ASSIGN(H264VideoToolboxEncoder); |
| 105 }; |
| 106 |
| 107 } // namespace cast |
| 108 } // namespace media |
| 109 |
| 110 #endif // MEDIA_CAST_SENDER_H264_VT_ENCODER_H_ |
OLD | NEW |