OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 #ifndef MEDIA_CAST_SENDER_SIZE_ADAPTABLE_VIDEO_ENCODER_BASE_H_ | |
6 #define MEDIA_CAST_SENDER_SIZE_ADAPTABLE_VIDEO_ENCODER_BASE_H_ | |
7 | |
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" | |
15 | |
16 namespace media { | |
17 namespace cast { | |
18 | |
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); | |
30 | |
31 ~SizeAdaptableVideoEncoderBase() override; | |
32 | |
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) override; | |
38 void SetBitRate(int new_bit_rate) override; | |
39 void GenerateKeyFrame() override; | |
40 void LatestFrameIdToReference(uint32 frame_id) override; | |
41 scoped_ptr<VideoFrameFactory> CreateVideoFrameFactory() override; | |
42 void EmitFrames() override; | |
43 | |
44 protected: | |
45 // Accessors for subclasses. | |
46 CastEnvironment* cast_environment() const { | |
47 return cast_environment_.get(); | |
48 } | |
49 const VideoSenderConfig& video_config() const { | |
50 return video_config_; | |
51 } | |
52 const gfx::Size& next_encoder_frame_size() const { | |
53 return next_encoder_frame_size_; | |
54 } | |
55 uint32 last_frame_id() const { | |
56 return last_frame_id_; | |
57 } | |
58 | |
59 // Returns a callback that calls OnEncoderStatusChange(). | |
hubbe
2015/02/11 00:47:54
Why not just make OnEncoderStatusChange() protecte
miu
2015/02/11 02:14:24
I didn't want to expose the weak pointer invalidat
| |
60 StatusChangeCallback CreateEncoderStatusChangeCallback(); | |
61 | |
62 // Overridden by subclasses to provide the underlying encoder implementation. | |
63 virtual scoped_ptr<VideoEncoder> CreateReplacementEncoder() = 0; | |
hubbe
2015/02/11 00:47:54
Wouldn't this also create the initial encoder?
(Sh
miu
2015/02/11 02:14:24
Done.
| |
64 | |
65 // Overridden by subclasses to perform additional steps when | |
66 // |replacement_encoder| becomes the active encoder. | |
67 virtual void OnEncoderReplaced(VideoEncoder* replacement_encoder); | |
68 | |
69 // Overridden by subclasses to perform additional steps before/after the | |
70 // current encoder is destroyed. | |
71 virtual void DestroyCurrentEncoder(); | |
72 | |
73 private: | |
74 // Create and initialize a replacement video encoder, if this not already | |
75 // in-progress. The replacement will call back to OnEncoderStatusChange() | |
76 // with success/fail status, and |current_encoder_| will be set there. | |
77 void MaybeSpawnReplacementEncoder(const gfx::Size& size_needed); | |
78 | |
79 // Called when a status change is received from an encoder. | |
80 void OnEncoderStatusChange(OperationalStatus status); | |
81 | |
82 // Called by the |current_encoder_| with the next EncodedFrame. | |
83 void OnEncodedVideoFrame(const FrameEncodedCallback& frame_encoded_callback, | |
84 scoped_ptr<EncodedFrame> encoded_frame); | |
85 | |
86 const scoped_refptr<CastEnvironment> cast_environment_; | |
87 | |
88 // This is not const since |video_config_.starting_bitrate| is modified by | |
89 // SetBitRate(), for when a replacement encoder is spawned. | |
90 VideoSenderConfig video_config_; | |
91 | |
92 // Run whenever the underlying encoder reports a status change. | |
93 const StatusChangeCallback status_change_cb_; | |
94 | |
95 // The underlying platform video encoder and current frame size. | |
96 scoped_ptr<VideoEncoder> current_encoder_; | |
hubbe
2015/02/11 00:47:54
Maybe make a struct which contains an encoder and
miu
2015/02/11 02:14:24
Eliminated the "next_encoder" stuff, as discussed.
| |
97 gfx::Size current_encoder_frame_size_; | |
98 | |
99 // The replacement encoder currently being initialized, but not yet ready for | |
100 // use. | |
101 scoped_ptr<VideoEncoder> next_encoder_; | |
hubbe
2015/02/11 00:47:54
Do we really need this?
As far as i can tell, we n
miu
2015/02/11 02:14:24
Done.
| |
102 gfx::Size next_encoder_frame_size_; | |
103 | |
104 // The number of frames in |current_encoder_|'s pipeline. | |
105 int frames_in_encoder_; | |
106 | |
107 // The ID of the last frame that was emitted from |current_encoder_|. | |
108 uint32 last_frame_id_; | |
109 | |
110 // NOTE: Weak pointers must be invalidated before all other member variables. | |
111 base::WeakPtrFactory<SizeAdaptableVideoEncoderBase> weak_factory_; | |
112 | |
113 DISALLOW_COPY_AND_ASSIGN(SizeAdaptableVideoEncoderBase); | |
114 }; | |
115 | |
116 } // namespace cast | |
117 } // namespace media | |
118 | |
119 #endif // MEDIA_CAST_SENDER_SIZE_ADAPTABLE_VIDEO_ENCODER_BASE_H_ | |
OLD | NEW |