1 // Copyright 2014 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_VIDEO_SENDER_H_ 6 #define MEDIA_CAST_SENDER_VIDEO_SENDER_H_ 7 8 #include "base/callback.h" 9 #include "base/memory/ref_counted.h" 10 #include "base/memory/scoped_ptr.h" 11 #include "base/memory/weak_ptr.h" 12 #include "base/threading/non_thread_safe.h" 13 #include "base/time/tick_clock.h" 14 #include "base/time/time.h" 15 #include "media/cast/cast_config.h" 16 #include "media/cast/sender/congestion_control.h" 17 #include "media/cast/sender/frame_sender.h" 18 19 namespace media { 20 21 class VideoFrame; 22 23 namespace cast { 24 25 class CastTransportSender; 26 class VideoEncoder; 27 28 typedef base::Callback<void(base::TimeDelta)> PlayoutDelayChangeCB; 29 30 // Not thread safe. Only called from the main cast thread. 31 // This class owns all objects related to sending video, objects that create RTP 32 // packets, congestion control, video encoder, parsing and sending of 33 // RTCP packets. 34 // Additionally it posts a bunch of delayed tasks to the main thread for various 35 // timeouts. 36 class VideoSender : public FrameSender, 37 public base::NonThreadSafe, 38 public base::SupportsWeakPtr<VideoSender> { 39 public: 40 VideoSender(scoped_refptr<CastEnvironment> cast_environment, 41 const VideoSenderConfig& video_config, 42 const CastInitializationCallback& initialization_cb, 43 const CreateVideoEncodeAcceleratorCallback& create_vea_cb, 44 const CreateVideoEncodeMemoryCallback& create_video_encode_mem_cb, 45 CastTransportSender* const transport_sender, 46 const PlayoutDelayChangeCB& playout_delay_change_cb); 47 48 virtual ~VideoSender(); 49 50 // Note: It is not guaranteed that |video_frame| will actually be encoded and 51 // sent, if VideoSender detects too many frames in flight. Therefore, clients 52 // should be careful about the rate at which this method is called. 53 // 54 // Note: It is invalid to call this method if InitializationResult() returns 55 // anything but STATUS_VIDEO_INITIALIZED. 56 void InsertRawVideoFrame(const scoped_refptr<media::VideoFrame>& video_frame, 57 const base::TimeTicks& capture_time); 58 59 protected: 60 virtual int GetNumberOfFramesInEncoder() const OVERRIDE; 61 virtual base::TimeDelta GetInFlightMediaDuration() const OVERRIDE; 62 virtual void OnAck(uint32 frame_id) OVERRIDE; 63 64 private: 65 // Called when the encoder is initialized or has failed to initialize. 66 void OnEncoderInitialized( 67 const CastInitializationCallback& initialization_cb, 68 CastInitializationStatus status); 69 70 // Called by the |video_encoder_| with the next EncodedFrame to send. 71 void OnEncodedVideoFrame(int encoder_bitrate, 72 scoped_ptr<EncodedFrame> encoded_frame); 73 74 // Encodes media::VideoFrame images into EncodedFrames. Per configuration, 75 // this will point to either the internal software-based encoder or a proxy to 76 // a hardware-based encoder. 77 scoped_ptr<VideoEncoder> video_encoder_; 78 79 // The number of frames queued for encoding, but not yet sent. 80 int frames_in_encoder_; 81 82 // The duration of video queued for encoding, but not yet sent. 83 base::TimeDelta duration_in_encoder_; 84 85 // The timestamp of the frame that was last enqueued in |video_encoder_|. 86 base::TimeTicks last_enqueued_frame_reference_time_; 87 88 // Remember what we set the bitrate to before, no need to set it again if 89 // we get the same value. 90 uint32 last_bitrate_; 91 92 PlayoutDelayChangeCB playout_delay_change_cb_; 93 94 // NOTE: Weak pointers must be invalidated before all other member variables. 95 base::WeakPtrFactory<VideoSender> weak_factory_; 96 97 DISALLOW_COPY_AND_ASSIGN(VideoSender); 98 }; 99 100 } // namespace cast 101 } // namespace media 102 103 #endif // MEDIA_CAST_SENDER_VIDEO_SENDER_H_ 104