Index: media/cast/sender/h264_vt_encoder.h |
diff --git a/media/cast/sender/h264_vt_encoder.h b/media/cast/sender/h264_vt_encoder.h |
index ece6ab38b1a6f362cdd27207a1b48ad9c284668d..756571c0c10bb804ae46e26556bfeb2c248ea4f0 100644 |
--- a/media/cast/sender/h264_vt_encoder.h |
+++ b/media/cast/sender/h264_vt_encoder.h |
@@ -16,7 +16,8 @@ namespace cast { |
// VideoToolbox implementation of the media::cast::VideoEncoder interface. |
// VideoToolbox makes no guarantees that it is thread safe, so this object is |
-// pinned to the thread on which it is constructed. |
+// pinned to the thread on which it is constructed. Supports changing frame |
+// sizes directly. |
class H264VideoToolboxEncoder : public VideoEncoder { |
typedef CoreMediaGlue::CMSampleBufferRef CMSampleBufferRef; |
typedef VideoToolboxGlue::VTCompressionSessionRef VTCompressionSessionRef; |
@@ -30,8 +31,6 @@ class H264VideoToolboxEncoder : public VideoEncoder { |
H264VideoToolboxEncoder( |
const scoped_refptr<CastEnvironment>& cast_environment, |
const VideoSenderConfig& video_config, |
- const gfx::Size& frame_size, |
- uint32 first_frame_id, |
const StatusChangeCallback& status_change_cb); |
~H264VideoToolboxEncoder() override; |
@@ -47,14 +46,25 @@ class H264VideoToolboxEncoder : public VideoEncoder { |
void EmitFrames() override; |
private: |
- // Initialize the compression session. |
- bool Initialize(const VideoSenderConfig& video_config); |
+ // VideoFrameFactory tied to the VideoToolbox encoder. |
+ class VideoFrameFactoryImpl; |
- // Configure the compression session. |
- void ConfigureSession(const VideoSenderConfig& video_config); |
+ // Reset the encoder's compression session by destroying the existing one |
+ // using DestroyCompressionSession() and creating a new one. The new session |
+ // is configured using ConfigureCompressionSession(). |
+ void ResetCompressionSession(); |
- // Teardown the encoder. |
- void Teardown(); |
+ // Configure the current compression session using current encoder settings. |
+ void ConfigureCompressionSession(); |
+ |
+ // Destroy the current compression session if any. Blocks until all pending |
+ // frames have been flushed out (similar to EmitFrames without doing any |
+ // encoding work). |
+ void DestroyCompressionSession(); |
+ |
+ // Update the encoder's target frame size by resetting the compression |
+ // session. This will also update the video frame factory. |
+ void UpdateFrameSize(const gfx::Size& size_needed); |
// Set a compression session property. |
bool SetSessionProperty(CFStringRef key, int32_t value); |
@@ -74,8 +84,14 @@ class H264VideoToolboxEncoder : public VideoEncoder { |
// VideoToolboxGlue provides access to VideoToolbox at runtime. |
const VideoToolboxGlue* const videotoolbox_glue_; |
- // The size of the visible region of the video frames to be encoded. |
- const gfx::Size frame_size_; |
+ // VideoSenderConfig copy so we can create compression sessions on demand. |
+ // This is needed to recover from backgrounding and other events that can |
+ // invalidate compression sessions. |
+ const VideoSenderConfig video_config_; |
+ |
+ // Frame size of the current compression session. Can be changed by submitting |
+ // a frame of a different size, which will cause a compression session reset. |
+ gfx::Size frame_size_; |
// Callback used to report initialization status and runtime errors. |
const StatusChangeCallback status_change_cb_; |
@@ -86,41 +102,19 @@ class H264VideoToolboxEncoder : public VideoEncoder { |
// The compression session. |
base::ScopedCFTypeRef<VTCompressionSessionRef> compression_session_; |
- // Frame identifier counter. |
- uint32 next_frame_id_; |
+ // Video frame factory tied to the encoder. |
+ scoped_refptr<VideoFrameFactoryImpl> video_frame_factory_; |
+ |
+ // The ID of the last frame that was emitted. |
+ uint32 last_frame_id_; |
// Force next frame to be a keyframe. |
bool encode_next_frame_as_keyframe_; |
- DISALLOW_COPY_AND_ASSIGN(H264VideoToolboxEncoder); |
-}; |
+ // NOTE: Weak pointers must be invalidated before all other member variables. |
+ base::WeakPtrFactory<H264VideoToolboxEncoder> weak_factory_; |
-// An implementation of SizeAdaptableVideoEncoderBase to proxy for |
-// H264VideoToolboxEncoder instances. |
-class SizeAdaptableH264VideoToolboxVideoEncoder |
- : public SizeAdaptableVideoEncoderBase { |
- public: |
- SizeAdaptableH264VideoToolboxVideoEncoder( |
- const scoped_refptr<CastEnvironment>& cast_environment, |
- const VideoSenderConfig& video_config, |
- const StatusChangeCallback& status_change_cb); |
- |
- ~SizeAdaptableH264VideoToolboxVideoEncoder() override; |
- |
- scoped_ptr<VideoFrameFactory> CreateVideoFrameFactory() override; |
- |
- protected: |
- scoped_ptr<VideoEncoder> CreateEncoder() override; |
- void OnEncoderReplaced(VideoEncoder* replacement_encoder) override; |
- void DestroyEncoder() override; |
- |
- private: |
- struct FactoryHolder; |
- class VideoFrameFactoryProxy; |
- |
- const scoped_refptr<FactoryHolder> holder_; |
- |
- DISALLOW_COPY_AND_ASSIGN(SizeAdaptableH264VideoToolboxVideoEncoder); |
+ DISALLOW_COPY_AND_ASSIGN(H264VideoToolboxEncoder); |
}; |
} // namespace cast |