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" |
9 #include "media/cast/cast_config.h" | 10 #include "media/cast/cast_config.h" |
10 #include "media/cast/cast_environment.h" | 11 #include "media/cast/cast_environment.h" |
11 #include "media/cast/video_sender/codecs/vp8/vp8_encoder.h" | 12 #include "media/cast/video_sender/codecs/vp8/vp8_encoder.h" |
12 #include "media/cast/video_sender/video_encoder.h" | 13 #include "media/cast/video_sender/video_encoder.h" |
13 | 14 |
14 namespace media { | 15 namespace media { |
15 class VideoFrame; | 16 class VideoFrame; |
16 | 17 |
17 namespace cast { | 18 namespace cast { |
18 | 19 |
19 // This object is called external from the main cast thread and internally from | 20 // This object is called external from the main cast thread and internally from |
20 // the video encoder thread. | 21 // the video encoder thread. |
21 class VideoEncoderImpl : public VideoEncoder { | 22 class VideoEncoderImpl : public VideoEncoder { |
22 public: | 23 public: |
23 struct CodecDynamicConfig { | |
24 bool key_frame_requested; | |
25 uint32 latest_frame_id_to_reference; | |
26 int bit_rate; | |
27 }; | |
28 | |
29 typedef base::Callback<void(scoped_ptr<transport::EncodedVideoFrame>, | 24 typedef base::Callback<void(scoped_ptr<transport::EncodedVideoFrame>, |
30 const base::TimeTicks&)> FrameEncodedCallback; | 25 const base::TimeTicks&)> FrameEncodedCallback; |
31 | 26 |
32 VideoEncoderImpl(scoped_refptr<CastEnvironment> cast_environment, | 27 VideoEncoderImpl(scoped_refptr<CastEnvironment> cast_environment, |
33 const VideoSenderConfig& video_config, | 28 const VideoSenderConfig& video_config, |
34 uint8 max_unacked_frames); | 29 uint8 max_unacked_frames); |
35 | 30 |
36 virtual ~VideoEncoderImpl(); | 31 virtual ~VideoEncoderImpl(); |
37 | 32 |
38 // Called from the main cast thread. This function post the encode task to the | 33 // Called from the main cast thread. This function post the encode task to the |
39 // video encoder thread; | 34 // video encoder thread; |
40 // The video_frame must be valid until the closure callback is called. | 35 // The video_frame must be valid until the closure callback is called. |
41 // The closure callback is called from the video encoder thread as soon as | 36 // The closure callback is called from the video encoder thread as soon as |
42 // the encoder is done with the frame; it does not mean that the encoded frame | 37 // the encoder is done with the frame; it does not mean that the encoded frame |
43 // has been sent out. | 38 // has been sent out. |
44 // Once the encoded frame is ready the frame_encoded_callback is called. | 39 // Once the encoded frame is ready the frame_encoded_callback is called. |
45 virtual bool EncodeVideoFrame( | 40 virtual bool EncodeVideoFrame( |
46 const scoped_refptr<media::VideoFrame>& video_frame, | 41 const scoped_refptr<media::VideoFrame>& video_frame, |
47 const base::TimeTicks& capture_time, | 42 const base::TimeTicks& capture_time, |
48 const FrameEncodedCallback& frame_encoded_callback) OVERRIDE; | 43 const FrameEncodedCallback& frame_encoded_callback) OVERRIDE; |
49 | 44 |
50 // The following functions are called from the main cast thread. | 45 // The following functions are called from the main cast thread. |
51 virtual void SetBitRate(int new_bit_rate) OVERRIDE; | 46 virtual void SetBitRate(int new_bit_rate) OVERRIDE; |
52 virtual void SkipNextFrame(bool skip_next_frame) OVERRIDE; | 47 virtual void SkipNextFrame(bool skip_next_frame) OVERRIDE; |
53 virtual void GenerateKeyFrame() OVERRIDE; | 48 virtual void GenerateKeyFrame() OVERRIDE; |
54 virtual void LatestFrameIdToReference(uint32 frame_id) OVERRIDE; | 49 virtual void LatestFrameIdToReference(uint32 frame_id) OVERRIDE; |
55 virtual int NumberOfSkippedFrames() const OVERRIDE; | 50 virtual int NumberOfSkippedFrames() const OVERRIDE; |
56 | 51 |
| 52 protected: |
| 53 struct CodecDynamicConfig { |
| 54 bool key_frame_requested; |
| 55 uint32 latest_frame_id_to_reference; |
| 56 int bit_rate; |
| 57 }; |
| 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 |
57 private: | 66 private: |
58 | |
59 const VideoSenderConfig video_config_; | 67 const VideoSenderConfig video_config_; |
60 scoped_refptr<CastEnvironment> cast_environment_; | 68 scoped_refptr<CastEnvironment> cast_environment_; |
| 69 scoped_ptr<Vp8Encoder> vp8_encoder_; |
61 CodecDynamicConfig dynamic_config_; | 70 CodecDynamicConfig dynamic_config_; |
62 bool skip_next_frame_; | 71 bool skip_next_frame_; |
63 int skip_count_; | 72 int skip_count_; |
64 | 73 |
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 | |
71 DISALLOW_COPY_AND_ASSIGN(VideoEncoderImpl); | 74 DISALLOW_COPY_AND_ASSIGN(VideoEncoderImpl); |
72 }; | 75 }; |
73 | 76 |
74 } // namespace cast | 77 } // namespace cast |
75 } // namespace media | 78 } // namespace media |
76 | 79 |
77 #endif // MEDIA_CAST_VIDEO_SENDER_VIDEO_ENCODER_IMPL_H_ | 80 #endif // MEDIA_CAST_VIDEO_SENDER_VIDEO_ENCODER_IMPL_H_ |
OLD | NEW |