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 // VideoToolbox makes no guarantees that it is thread safe, so this object is | |
21 // pinned to the thread on which it is constructed. | |
22 class H264VideoToolboxEncoder : public VideoEncoder { | |
23 public: | |
24 H264VideoToolboxEncoder(scoped_refptr<CastEnvironment> cast_environment, | |
25 const VideoSenderConfig& video_config); | |
26 virtual ~H264VideoToolboxEncoder(); | |
27 | |
28 // After Initialize has been called, returns the compression session's | |
29 // internal pixel buffer pool. Clients can use it to provide video frames that | |
30 // will not incur a copy into encoder-accessible memory. | |
31 CVPixelBufferPoolRef cv_pixel_buffer_pool() const; | |
32 | |
33 // media::cast::VideoEncoder implementation | |
34 virtual bool EncodeVideoFrame( | |
35 const scoped_refptr<media::VideoFrame>& video_frame, | |
36 const base::TimeTicks& capture_time, | |
37 const FrameEncodedCallback& frame_encoded_callback) OVERRIDE; | |
38 virtual void SetBitRate(int new_bit_rate) OVERRIDE; | |
39 virtual void GenerateKeyFrame() OVERRIDE; | |
40 virtual void LatestFrameIdToReference(uint32 frame_id) OVERRIDE; | |
41 | |
42 private: | |
43 // Initialize the compression session. | |
44 void Initialize(); | |
45 | |
46 // Configure the compression session. | |
47 void ConfigureSession(); | |
48 | |
49 // Teardown the encoder. | |
50 void Teardown(); | |
51 | |
52 // Wrap a VideoFrame in a new CVPixelBuffer | |
53 base::ScopedCFTypeRef<CVPixelBufferRef> WrapVideoFrame( | |
54 const scoped_refptr<media::VideoFrame>& frame); | |
55 | |
56 // Compression session callback function to handle compressed frames. | |
57 static void CompressionCallback(void* encoder_opaque, | |
58 void* frame_opaque, | |
59 OSStatus status, | |
60 VTEncodeInfoFlags info, | |
61 CMSampleBufferRef sbuf); | |
62 | |
63 // Copy a H.264 frame stored in a CM sample buffer to an Annex B buffer. | |
64 // Copies parameter sets for keyframes before the frame data. | |
65 static void CopySampleBufferToAnnexBBuffer(CMSampleBufferRef sbuf, | |
66 std::string* annexb_buffer, | |
67 bool keyframe); | |
68 | |
69 // The cast environment (contains worker threads & more). | |
70 const scoped_refptr<CastEnvironment> cast_environment_; | |
71 | |
72 // The cast video configuration (contains resolution, bitrate & more). | |
73 const VideoSenderConfig cast_config_; | |
miu
2014/08/25 19:21:17
nit: You don't need this member, as it's only used
jfroy
2014/08/25 20:59:13
Done.
| |
74 | |
75 // Thread checker to enforce that this object is used on a specific thread. | |
76 base::ThreadChecker thread_checker_; | |
77 | |
78 // The compression session. | |
79 base::ScopedCFTypeRef<VTCompressionSessionRef> compression_session_; | |
80 | |
81 // Frame identifier counter. | |
82 uint32 frame_id_; | |
83 | |
84 // Frame identifier of the last encoded keyframe. | |
85 uint32 last_keyframe_id_; | |
86 | |
87 // Force next frame to be a keyframe. | |
88 bool encode_next_frame_as_keyframe_; | |
89 | |
90 DISALLOW_COPY_AND_ASSIGN(H264VideoToolboxEncoder); | |
91 }; | |
92 | |
93 } // namespace cast | |
94 } // namespace media | |
95 | |
96 #endif // MEDIA_CAST_SENDER_H264_VT_ENCODER_H_ | |
OLD | NEW |