[cast] Handle frame size changes directly in the VideoToolbox encoder.
[chromium-blink-merge.git] / media / cast / sender / h264_vt_encoder.h
blob756571c0c10bb804ae46e26556bfeb2c248ea4f0
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 #ifndef MEDIA_CAST_SENDER_H264_VT_ENCODER_H_
6 #define MEDIA_CAST_SENDER_H264_VT_ENCODER_H_
8 #include "base/mac/scoped_cftyperef.h"
9 #include "base/threading/thread_checker.h"
10 #include "media/base/mac/videotoolbox_glue.h"
11 #include "media/cast/sender/size_adaptable_video_encoder_base.h"
12 #include "media/cast/sender/video_encoder.h"
14 namespace media {
15 namespace cast {
17 // VideoToolbox implementation of the media::cast::VideoEncoder interface.
18 // VideoToolbox makes no guarantees that it is thread safe, so this object is
19 // pinned to the thread on which it is constructed. Supports changing frame
20 // sizes directly.
21 class H264VideoToolboxEncoder : public VideoEncoder {
22 typedef CoreMediaGlue::CMSampleBufferRef CMSampleBufferRef;
23 typedef VideoToolboxGlue::VTCompressionSessionRef VTCompressionSessionRef;
24 typedef VideoToolboxGlue::VTEncodeInfoFlags VTEncodeInfoFlags;
26 public:
27 // Returns true if the current platform and system configuration supports
28 // using H264VideoToolboxEncoder with the given |video_config|.
29 static bool IsSupported(const VideoSenderConfig& video_config);
31 H264VideoToolboxEncoder(
32 const scoped_refptr<CastEnvironment>& cast_environment,
33 const VideoSenderConfig& video_config,
34 const StatusChangeCallback& status_change_cb);
35 ~H264VideoToolboxEncoder() override;
37 // media::cast::VideoEncoder implementation
38 bool EncodeVideoFrame(
39 const scoped_refptr<media::VideoFrame>& video_frame,
40 const base::TimeTicks& reference_time,
41 const FrameEncodedCallback& frame_encoded_callback) override;
42 void SetBitRate(int new_bit_rate) override;
43 void GenerateKeyFrame() override;
44 void LatestFrameIdToReference(uint32 frame_id) override;
45 scoped_ptr<VideoFrameFactory> CreateVideoFrameFactory() override;
46 void EmitFrames() override;
48 private:
49 // VideoFrameFactory tied to the VideoToolbox encoder.
50 class VideoFrameFactoryImpl;
52 // Reset the encoder's compression session by destroying the existing one
53 // using DestroyCompressionSession() and creating a new one. The new session
54 // is configured using ConfigureCompressionSession().
55 void ResetCompressionSession();
57 // Configure the current compression session using current encoder settings.
58 void ConfigureCompressionSession();
60 // Destroy the current compression session if any. Blocks until all pending
61 // frames have been flushed out (similar to EmitFrames without doing any
62 // encoding work).
63 void DestroyCompressionSession();
65 // Update the encoder's target frame size by resetting the compression
66 // session. This will also update the video frame factory.
67 void UpdateFrameSize(const gfx::Size& size_needed);
69 // Set a compression session property.
70 bool SetSessionProperty(CFStringRef key, int32_t value);
71 bool SetSessionProperty(CFStringRef key, bool value);
72 bool SetSessionProperty(CFStringRef key, CFStringRef value);
74 // Compression session callback function to handle compressed frames.
75 static void CompressionCallback(void* encoder_opaque,
76 void* request_opaque,
77 OSStatus status,
78 VTEncodeInfoFlags info,
79 CMSampleBufferRef sbuf);
81 // The cast environment (contains worker threads & more).
82 const scoped_refptr<CastEnvironment> cast_environment_;
84 // VideoToolboxGlue provides access to VideoToolbox at runtime.
85 const VideoToolboxGlue* const videotoolbox_glue_;
87 // VideoSenderConfig copy so we can create compression sessions on demand.
88 // This is needed to recover from backgrounding and other events that can
89 // invalidate compression sessions.
90 const VideoSenderConfig video_config_;
92 // Frame size of the current compression session. Can be changed by submitting
93 // a frame of a different size, which will cause a compression session reset.
94 gfx::Size frame_size_;
96 // Callback used to report initialization status and runtime errors.
97 const StatusChangeCallback status_change_cb_;
99 // Thread checker to enforce that this object is used on a specific thread.
100 base::ThreadChecker thread_checker_;
102 // The compression session.
103 base::ScopedCFTypeRef<VTCompressionSessionRef> compression_session_;
105 // Video frame factory tied to the encoder.
106 scoped_refptr<VideoFrameFactoryImpl> video_frame_factory_;
108 // The ID of the last frame that was emitted.
109 uint32 last_frame_id_;
111 // Force next frame to be a keyframe.
112 bool encode_next_frame_as_keyframe_;
114 // NOTE: Weak pointers must be invalidated before all other member variables.
115 base::WeakPtrFactory<H264VideoToolboxEncoder> weak_factory_;
117 DISALLOW_COPY_AND_ASSIGN(H264VideoToolboxEncoder);
120 } // namespace cast
121 } // namespace media
123 #endif // MEDIA_CAST_SENDER_H264_VT_ENCODER_H_