OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef MEDIA_CAST_VIDEO_SENDER_VIDEO_ENCODER_IMPL_H_ | 5 #ifndef MEDIA_CAST_VIDEO_SENDER_VIDEO_ENCODER_IMPL_H_ |
6 #define MEDIA_CAST_VIDEO_SENDER_VIDEO_ENCODER_IMPL_H_ | 6 #define MEDIA_CAST_VIDEO_SENDER_VIDEO_ENCODER_IMPL_H_ |
7 | 7 |
8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
9 #include "base/memory/weak_ptr.h" | |
10 #include "media/cast/cast_config.h" | 9 #include "media/cast/cast_config.h" |
11 #include "media/cast/cast_environment.h" | 10 #include "media/cast/cast_environment.h" |
12 #include "media/cast/video_sender/codecs/vp8/vp8_encoder.h" | 11 #include "media/cast/video_sender/codecs/vp8/vp8_encoder.h" |
13 #include "media/cast/video_sender/video_encoder.h" | 12 #include "media/cast/video_sender/video_encoder.h" |
14 | 13 |
15 namespace media { | 14 namespace media { |
16 class VideoFrame; | 15 class VideoFrame; |
17 | 16 |
18 namespace cast { | 17 namespace cast { |
19 | 18 |
20 // This object is called external from the main cast thread and internally from | 19 // This object is called external from the main cast thread and internally from |
21 // the video encoder thread. | 20 // the video encoder thread. |
22 class VideoEncoderImpl : public VideoEncoder { | 21 class VideoEncoderImpl : public VideoEncoder { |
23 public: | 22 public: |
| 23 struct CodecDynamicConfig { |
| 24 bool key_frame_requested; |
| 25 uint32 latest_frame_id_to_reference; |
| 26 int bit_rate; |
| 27 }; |
| 28 |
24 typedef base::Callback<void(scoped_ptr<transport::EncodedVideoFrame>, | 29 typedef base::Callback<void(scoped_ptr<transport::EncodedVideoFrame>, |
25 const base::TimeTicks&)> FrameEncodedCallback; | 30 const base::TimeTicks&)> FrameEncodedCallback; |
26 | 31 |
27 VideoEncoderImpl(scoped_refptr<CastEnvironment> cast_environment, | 32 VideoEncoderImpl(scoped_refptr<CastEnvironment> cast_environment, |
28 const VideoSenderConfig& video_config, | 33 const VideoSenderConfig& video_config, |
29 uint8 max_unacked_frames); | 34 uint8 max_unacked_frames); |
30 | 35 |
31 virtual ~VideoEncoderImpl(); | 36 virtual ~VideoEncoderImpl(); |
32 | 37 |
33 // Called from the main cast thread. This function post the encode task to the | 38 // Called from the main cast thread. This function post the encode task to the |
34 // video encoder thread; | 39 // video encoder thread; |
35 // The video_frame must be valid until the closure callback is called. | 40 // The video_frame must be valid until the closure callback is called. |
36 // The closure callback is called from the video encoder thread as soon as | 41 // The closure callback is called from the video encoder thread as soon as |
37 // the encoder is done with the frame; it does not mean that the encoded frame | 42 // the encoder is done with the frame; it does not mean that the encoded frame |
38 // has been sent out. | 43 // has been sent out. |
39 // Once the encoded frame is ready the frame_encoded_callback is called. | 44 // Once the encoded frame is ready the frame_encoded_callback is called. |
40 virtual bool EncodeVideoFrame( | 45 virtual bool EncodeVideoFrame( |
41 const scoped_refptr<media::VideoFrame>& video_frame, | 46 const scoped_refptr<media::VideoFrame>& video_frame, |
42 const base::TimeTicks& capture_time, | 47 const base::TimeTicks& capture_time, |
43 const FrameEncodedCallback& frame_encoded_callback) OVERRIDE; | 48 const FrameEncodedCallback& frame_encoded_callback) OVERRIDE; |
44 | 49 |
45 // The following functions are called from the main cast thread. | 50 // The following functions are called from the main cast thread. |
46 virtual void SetBitRate(int new_bit_rate) OVERRIDE; | 51 virtual void SetBitRate(int new_bit_rate) OVERRIDE; |
47 virtual void SkipNextFrame(bool skip_next_frame) OVERRIDE; | 52 virtual void SkipNextFrame(bool skip_next_frame) OVERRIDE; |
48 virtual void GenerateKeyFrame() OVERRIDE; | 53 virtual void GenerateKeyFrame() OVERRIDE; |
49 virtual void LatestFrameIdToReference(uint32 frame_id) OVERRIDE; | 54 virtual void LatestFrameIdToReference(uint32 frame_id) OVERRIDE; |
50 virtual int NumberOfSkippedFrames() const OVERRIDE; | 55 virtual int NumberOfSkippedFrames() const OVERRIDE; |
51 | 56 |
52 protected: | 57 private: |
53 struct CodecDynamicConfig { | |
54 bool key_frame_requested; | |
55 uint32 latest_frame_id_to_reference; | |
56 int bit_rate; | |
57 }; | |
58 | 58 |
59 // The actual encode, called from the video encoder thread. | |
60 void EncodeVideoFrameEncoderThread( | |
61 const scoped_refptr<media::VideoFrame>& video_frame, | |
62 const base::TimeTicks& capture_time, | |
63 const CodecDynamicConfig& dynamic_config, | |
64 const FrameEncodedCallback& frame_encoded_callback); | |
65 | |
66 private: | |
67 const VideoSenderConfig video_config_; | 59 const VideoSenderConfig video_config_; |
68 scoped_refptr<CastEnvironment> cast_environment_; | 60 scoped_refptr<CastEnvironment> cast_environment_; |
69 scoped_ptr<Vp8Encoder> vp8_encoder_; | |
70 CodecDynamicConfig dynamic_config_; | 61 CodecDynamicConfig dynamic_config_; |
71 bool skip_next_frame_; | 62 bool skip_next_frame_; |
72 int skip_count_; | 63 int skip_count_; |
73 | 64 |
| 65 // This member belongs to the video encoder thread. It must not be |
| 66 // dereferenced on the main thread. We manage the lifetime of this member |
| 67 // manually because it needs to be initialize, used and destroyed on the |
| 68 // video encoder thread and video encoder thread can out-live the main thread. |
| 69 scoped_ptr<Vp8Encoder> vp8_encoder_; |
| 70 |
74 DISALLOW_COPY_AND_ASSIGN(VideoEncoderImpl); | 71 DISALLOW_COPY_AND_ASSIGN(VideoEncoderImpl); |
75 }; | 72 }; |
76 | 73 |
77 } // namespace cast | 74 } // namespace cast |
78 } // namespace media | 75 } // namespace media |
79 | 76 |
80 #endif // MEDIA_CAST_VIDEO_SENDER_VIDEO_ENCODER_IMPL_H_ | 77 #endif // MEDIA_CAST_VIDEO_SENDER_VIDEO_ENCODER_IMPL_H_ |
OLD | NEW |