Remove INJECT_EVENTS permissions from test APKs.
[chromium-blink-merge.git] / media / cast / sender / h264_vt_encoder.h
blob1f51c230f8c4d6c6be7100e6f24fd5d50e486c51
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/power_monitor/power_observer.h"
10 #include "base/threading/thread_checker.h"
11 #include "media/base/mac/videotoolbox_glue.h"
12 #include "media/cast/sender/size_adaptable_video_encoder_base.h"
13 #include "media/cast/sender/video_encoder.h"
15 namespace media {
16 namespace cast {
18 // VideoToolbox implementation of the media::cast::VideoEncoder interface.
19 // VideoToolbox makes no guarantees that it is thread safe, so this object is
20 // pinned to the thread on which it is constructed. Supports changing frame
21 // sizes directly. Implements the base::PowerObserver interface to reset the
22 // compression session when the host process is suspended.
23 class H264VideoToolboxEncoder : public VideoEncoder,
24 public base::PowerObserver {
25 typedef CoreMediaGlue::CMSampleBufferRef CMSampleBufferRef;
26 typedef VideoToolboxGlue::VTCompressionSessionRef VTCompressionSessionRef;
27 typedef VideoToolboxGlue::VTEncodeInfoFlags VTEncodeInfoFlags;
29 public:
30 // Returns true if the current platform and system configuration supports
31 // using H264VideoToolboxEncoder with the given |video_config|.
32 static bool IsSupported(const VideoSenderConfig& video_config);
34 H264VideoToolboxEncoder(
35 const scoped_refptr<CastEnvironment>& cast_environment,
36 const VideoSenderConfig& video_config,
37 const StatusChangeCallback& status_change_cb);
38 ~H264VideoToolboxEncoder() final;
40 // media::cast::VideoEncoder implementation
41 bool EncodeVideoFrame(
42 const scoped_refptr<media::VideoFrame>& video_frame,
43 const base::TimeTicks& reference_time,
44 const FrameEncodedCallback& frame_encoded_callback) final;
45 void SetBitRate(int new_bit_rate) final;
46 void GenerateKeyFrame() final;
47 void LatestFrameIdToReference(uint32 frame_id) final;
48 scoped_ptr<VideoFrameFactory> CreateVideoFrameFactory() final;
49 void EmitFrames() final;
51 // base::PowerObserver
52 void OnSuspend() final;
53 void OnResume() final;
55 private:
56 // VideoFrameFactory tied to the VideoToolbox encoder.
57 class VideoFrameFactoryImpl;
59 // Reset the encoder's compression session by destroying the existing one
60 // using DestroyCompressionSession() and creating a new one. The new session
61 // is configured using ConfigureCompressionSession().
62 void ResetCompressionSession();
64 // Configure the current compression session using current encoder settings.
65 void ConfigureCompressionSession();
67 // Destroy the current compression session if any. Blocks until all pending
68 // frames have been flushed out (similar to EmitFrames without doing any
69 // encoding work).
70 void DestroyCompressionSession();
72 // Update the encoder's target frame size by resetting the compression
73 // session. This will also update the video frame factory.
74 void UpdateFrameSize(const gfx::Size& size_needed);
76 // Set a compression session property.
77 bool SetSessionProperty(CFStringRef key, int32_t value);
78 bool SetSessionProperty(CFStringRef key, bool value);
79 bool SetSessionProperty(CFStringRef key, CFStringRef value);
81 // Compression session callback function to handle compressed frames.
82 static void CompressionCallback(void* encoder_opaque,
83 void* request_opaque,
84 OSStatus status,
85 VTEncodeInfoFlags info,
86 CMSampleBufferRef sbuf);
88 // The cast environment (contains worker threads & more).
89 const scoped_refptr<CastEnvironment> cast_environment_;
91 // VideoToolboxGlue provides access to VideoToolbox at runtime.
92 const VideoToolboxGlue* const videotoolbox_glue_;
94 // VideoSenderConfig copy so we can create compression sessions on demand.
95 // This is needed to recover from backgrounding and other events that can
96 // invalidate compression sessions.
97 const VideoSenderConfig video_config_;
99 // Frame size of the current compression session. Can be changed by submitting
100 // a frame of a different size, which will cause a compression session reset.
101 gfx::Size frame_size_;
103 // Callback used to report initialization status and runtime errors.
104 const StatusChangeCallback status_change_cb_;
106 // Thread checker to enforce that this object is used on a specific thread.
107 base::ThreadChecker thread_checker_;
109 // The compression session.
110 base::ScopedCFTypeRef<VTCompressionSessionRef> compression_session_;
112 // Video frame factory tied to the encoder.
113 scoped_refptr<VideoFrameFactoryImpl> video_frame_factory_;
115 // The ID of the last frame that was emitted.
116 uint32 last_frame_id_;
118 // Force next frame to be a keyframe.
119 bool encode_next_frame_as_keyframe_;
121 // Power suspension state.
122 bool power_suspended_;
124 // NOTE: Weak pointers must be invalidated before all other member variables.
125 base::WeakPtrFactory<H264VideoToolboxEncoder> weak_factory_;
127 DISALLOW_COPY_AND_ASSIGN(H264VideoToolboxEncoder);
130 } // namespace cast
131 } // namespace media
133 #endif // MEDIA_CAST_SENDER_H264_VT_ENCODER_H_