1 // Copyright 2013 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_VIDEO_SENDER_VIDEO_ENCODER_H_
6 #define MEDIA_CAST_VIDEO_SENDER_VIDEO_ENCODER_H_
8 #include "base/memory/ref_counted.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/message_loop/message_loop.h"
11 #include "media/cast/cast_config.h"
12 #include "media/cast/cast_environment.h"
13 #include "media/cast/video_sender/codecs/vp8/vp8_encoder.h"
18 // This object is called external from the main cast thread and internally from
19 // the video encoder thread.
20 class VideoEncoder : public VideoEncoderController,
21 public base::RefCountedThreadSafe<VideoEncoder> {
23 typedef base::Callback<void(scoped_ptr<EncodedVideoFrame>,
24 const base::TimeTicks&)> FrameEncodedCallback;
26 VideoEncoder(scoped_refptr<CastEnvironment> cast_environment,
27 const VideoSenderConfig& video_config,
28 uint8 max_unacked_frames);
30 // Called from the main cast thread. This function post the encode task to the
31 // video encoder thread;
32 // The video_frame must be valid until the closure callback is called.
33 // The closure callback is called from the video encoder thread as soon as
34 // the encoder is done with the frame; it does not mean that the encoded frame
36 // Once the encoded frame is ready the frame_encoded_callback is called.
37 bool EncodeVideoFrame(const I420VideoFrame* video_frame,
38 const base::TimeTicks& capture_time,
39 const FrameEncodedCallback& frame_encoded_callback,
40 const base::Closure frame_release_callback);
43 virtual ~VideoEncoder();
45 struct CodecDynamicConfig {
46 bool key_frame_requested;
47 uint8 latest_frame_id_to_reference;
51 // The actual encode, called from the video encoder thread.
52 void EncodeVideoFrameEncoderThread(
53 const I420VideoFrame* video_frame,
54 const base::TimeTicks& capture_time,
55 const CodecDynamicConfig& dynamic_config,
56 const FrameEncodedCallback& frame_encoded_callback,
57 const base::Closure frame_release_callback);
59 // The following functions are called from the main cast thread.
60 virtual void SetBitRate(int new_bit_rate) OVERRIDE;
61 virtual void SkipNextFrame(bool skip_next_frame) OVERRIDE;
62 virtual void GenerateKeyFrame() OVERRIDE;
63 virtual void LatestFrameIdToReference(uint8 frame_id) OVERRIDE;
64 virtual int NumberOfSkippedFrames() const OVERRIDE;
67 friend class base::RefCountedThreadSafe<VideoEncoder>;
69 const VideoSenderConfig video_config_;
70 scoped_refptr<CastEnvironment> cast_environment_;
71 scoped_ptr<Vp8Encoder> vp8_encoder_;
72 CodecDynamicConfig dynamic_config_;
73 bool skip_next_frame_;
76 DISALLOW_COPY_AND_ASSIGN(VideoEncoder);
82 #endif // MEDIA_CAST_VIDEO_SENDER_VIDEO_ENCODER_H_