Remove INJECT_EVENTS permissions from test APKs.
[chromium-blink-merge.git] / media / cast / sender / size_adaptable_video_encoder_base.h
blob621c3d07125d57f7b189e3e9365e857538bda293
1 // Copyright 2015 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_SENDER_SIZE_ADAPTABLE_VIDEO_ENCODER_BASE_H_
6 #define MEDIA_CAST_SENDER_SIZE_ADAPTABLE_VIDEO_ENCODER_BASE_H_
8 #include "base/memory/ref_counted.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/memory/weak_ptr.h"
11 #include "media/cast/cast_config.h"
12 #include "media/cast/cast_environment.h"
13 #include "media/cast/sender/video_encoder.h"
14 #include "ui/gfx/geometry/size.h"
16 namespace media {
17 namespace cast {
19 // Creates and owns a VideoEncoder instance. The owned instance is an
20 // implementation that does not support changing frame sizes, and so
21 // SizeAdaptableVideoEncoderBase acts as a proxy to automatically detect when
22 // the owned instance should be replaced with one that can handle the new frame
23 // size.
24 class SizeAdaptableVideoEncoderBase : public VideoEncoder {
25 public:
26 SizeAdaptableVideoEncoderBase(
27 const scoped_refptr<CastEnvironment>& cast_environment,
28 const VideoSenderConfig& video_config,
29 const StatusChangeCallback& status_change_cb);
31 ~SizeAdaptableVideoEncoderBase() override;
33 // VideoEncoder implementation.
34 bool EncodeVideoFrame(
35 const scoped_refptr<media::VideoFrame>& video_frame,
36 const base::TimeTicks& reference_time,
37 const FrameEncodedCallback& frame_encoded_callback) final;
38 void SetBitRate(int new_bit_rate) final;
39 void GenerateKeyFrame() final;
40 void LatestFrameIdToReference(uint32 frame_id) final;
41 scoped_ptr<VideoFrameFactory> CreateVideoFrameFactory() final;
42 void EmitFrames() final;
44 protected:
45 // Accessors for subclasses.
46 CastEnvironment* cast_environment() const {
47 return cast_environment_.get();
49 const VideoSenderConfig& video_config() const {
50 return video_config_;
52 const gfx::Size& frame_size() const {
53 return frame_size_;
55 uint32 last_frame_id() const {
56 return last_frame_id_;
59 // Returns a callback that calls OnEncoderStatusChange(). The callback is
60 // canceled by invalidating its bound weak pointer just before a replacement
61 // encoder is instantiated. In this scheme, OnEncoderStatusChange() can only
62 // be called by the most-recent encoder.
63 StatusChangeCallback CreateEncoderStatusChangeCallback();
65 // Overridden by subclasses to create a new encoder instance that handles
66 // frames of the size specified by |frame_size()|.
67 virtual scoped_ptr<VideoEncoder> CreateEncoder() = 0;
69 // Overridden by subclasses to perform additional steps when
70 // |replacement_encoder| becomes the active encoder.
71 virtual void OnEncoderReplaced(VideoEncoder* replacement_encoder);
73 // Overridden by subclasses to perform additional steps before/after the
74 // current encoder is destroyed.
75 virtual void DestroyEncoder();
77 private:
78 // Create and initialize a replacement video encoder, if this not already
79 // in-progress. The replacement will call back to OnEncoderStatusChange()
80 // with success/fail status.
81 void TrySpawningReplacementEncoder(const gfx::Size& size_needed);
83 // Called when a status change is received from an encoder.
84 void OnEncoderStatusChange(OperationalStatus status);
86 // Called by the |encoder_| with the next EncodedFrame.
87 void OnEncodedVideoFrame(const FrameEncodedCallback& frame_encoded_callback,
88 scoped_ptr<SenderEncodedFrame> encoded_frame);
90 const scoped_refptr<CastEnvironment> cast_environment_;
92 // This is not const since |video_config_.starting_bitrate| is modified by
93 // SetBitRate(), for when a replacement encoder is spawned.
94 VideoSenderConfig video_config_;
96 // Run whenever the underlying encoder reports a status change.
97 const StatusChangeCallback status_change_cb_;
99 // The underlying platform video encoder and the frame size it expects.
100 scoped_ptr<VideoEncoder> encoder_;
101 gfx::Size frame_size_;
103 // The number of frames in |encoder_|'s pipeline. If this is set to
104 // kEncoderIsInitializing, |encoder_| is not yet ready to accept frames.
105 enum { kEncoderIsInitializing = -1 };
106 int frames_in_encoder_;
108 // The ID of the last frame that was emitted from |encoder_|.
109 uint32 last_frame_id_;
111 // NOTE: Weak pointers must be invalidated before all other member variables.
112 base::WeakPtrFactory<SizeAdaptableVideoEncoderBase> weak_factory_;
114 DISALLOW_COPY_AND_ASSIGN(SizeAdaptableVideoEncoderBase);
117 } // namespace cast
118 } // namespace media
120 #endif // MEDIA_CAST_SENDER_SIZE_ADAPTABLE_VIDEO_ENCODER_BASE_H_