- add sources.
[platform/framework/web/crosswalk.git] / src / media / cast / video_sender / video_encoder.h
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.
4
5 #ifndef MEDIA_CAST_VIDEO_SENDER_VIDEO_ENCODER_H_
6 #define MEDIA_CAST_VIDEO_SENDER_VIDEO_ENCODER_H_
7
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"
14
15 namespace media {
16 namespace cast {
17
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> {
22  public:
23   typedef base::Callback<void(scoped_ptr<EncodedVideoFrame>,
24                               const base::TimeTicks&)> FrameEncodedCallback;
25
26   VideoEncoder(scoped_refptr<CastEnvironment> cast_environment,
27                const VideoSenderConfig& video_config,
28                uint8 max_unacked_frames);
29
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
35   // has been sent out.
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);
41
42  protected:
43   virtual ~VideoEncoder();
44
45   struct CodecDynamicConfig {
46     bool key_frame_requested;
47     uint8 latest_frame_id_to_reference;
48     int bit_rate;
49   };
50
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);
58
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;
65
66  private:
67   friend class base::RefCountedThreadSafe<VideoEncoder>;
68
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_;
74   int skip_count_;
75
76   DISALLOW_COPY_AND_ASSIGN(VideoEncoder);
77 };
78
79 }  // namespace cast
80 }  // namespace media
81
82 #endif  // MEDIA_CAST_VIDEO_SENDER_VIDEO_ENCODER_H_