summaryrefslogtreecommitdiff
path: root/media/cast/video_sender/video_encoder.h
blob: fa85468c1fa55f7ed7b4aa3fa570bf2234cf59fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef MEDIA_CAST_VIDEO_SENDER_VIDEO_ENCODER_H_
#define MEDIA_CAST_VIDEO_SENDER_VIDEO_ENCODER_H_

#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "base/message_loop/message_loop.h"
#include "media/cast/cast_config.h"
#include "media/cast/cast_environment.h"
#include "media/cast/video_sender/codecs/vp8/vp8_encoder.h"

namespace media {
namespace cast {

// This object is called external from the main cast thread and internally from
// the video encoder thread.
class VideoEncoder : public VideoEncoderController,
                     public base::RefCountedThreadSafe<VideoEncoder> {
 public:
  typedef base::Callback<void(scoped_ptr<EncodedVideoFrame>,
                              const base::TimeTicks&)> FrameEncodedCallback;

  VideoEncoder(scoped_refptr<CastEnvironment> cast_environment,
               const VideoSenderConfig& video_config,
               uint8 max_unacked_frames);

  // Called from the main cast thread. This function post the encode task to the
  // video encoder thread;
  // The video_frame must be valid until the closure callback is called.
  // The closure callback is called from the video encoder thread as soon as
  // the encoder is done with the frame; it does not mean that the encoded frame
  // has been sent out.
  // Once the encoded frame is ready the frame_encoded_callback is called.
  bool EncodeVideoFrame(const I420VideoFrame* video_frame,
                        const base::TimeTicks& capture_time,
                        const FrameEncodedCallback& frame_encoded_callback,
                        const base::Closure frame_release_callback);

 protected:
  virtual ~VideoEncoder();

  struct CodecDynamicConfig {
    bool key_frame_requested;
    uint8 latest_frame_id_to_reference;
    int bit_rate;
  };

  // The actual encode, called from the video encoder thread.
  void EncodeVideoFrameEncoderThread(
      const I420VideoFrame* video_frame,
      const base::TimeTicks& capture_time,
      const CodecDynamicConfig& dynamic_config,
      const FrameEncodedCallback& frame_encoded_callback,
      const base::Closure frame_release_callback);

  // The following functions are called from the main cast thread.
  virtual void SetBitRate(int new_bit_rate) OVERRIDE;
  virtual void SkipNextFrame(bool skip_next_frame) OVERRIDE;
  virtual void GenerateKeyFrame() OVERRIDE;
  virtual void LatestFrameIdToReference(uint8 frame_id) OVERRIDE;
  virtual int NumberOfSkippedFrames() const OVERRIDE;

 private:
  friend class base::RefCountedThreadSafe<VideoEncoder>;

  const VideoSenderConfig video_config_;
  scoped_refptr<CastEnvironment> cast_environment_;
  scoped_ptr<Vp8Encoder> vp8_encoder_;
  CodecDynamicConfig dynamic_config_;
  bool skip_next_frame_;
  int skip_count_;

  DISALLOW_COPY_AND_ASSIGN(VideoEncoder);
};

}  // namespace cast
}  // namespace media

#endif  // MEDIA_CAST_VIDEO_SENDER_VIDEO_ENCODER_H_