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_RECEIVER_CAST_RECEIVER_IMPL_H_ 6 #define MEDIA_CAST_RECEIVER_CAST_RECEIVER_IMPL_H_ 7 8 #include "base/memory/ref_counted.h" 9 #include "base/memory/scoped_ptr.h" 10 #include "media/cast/cast_config.h" 11 #include "media/cast/cast_environment.h" 12 #include "media/cast/cast_receiver.h" 13 #include "media/cast/receiver/frame_receiver.h" 14 #include "media/cast/transport/pacing/paced_sender.h" 15 16 namespace media { 17 namespace cast { 18 19 class AudioDecoder; 20 class VideoDecoder; 21 22 // This is a pure owner class that groups all required receiver-related objects 23 // together, such as the paced packet sender, audio/video RTP frame receivers, 24 // and software decoders (created on-demand). 25 class CastReceiverImpl : public CastReceiver { 26 public: 27 CastReceiverImpl(scoped_refptr<CastEnvironment> cast_environment, 28 const FrameReceiverConfig& audio_config, 29 const FrameReceiverConfig& video_config, 30 transport::PacketSender* const packet_sender); 31 32 virtual ~CastReceiverImpl(); 33 34 // CastReceiver implementation. 35 virtual transport::PacketReceiverCallback packet_receiver() OVERRIDE; 36 virtual void RequestDecodedAudioFrame( 37 const AudioFrameDecodedCallback& callback) OVERRIDE; 38 virtual void RequestEncodedAudioFrame( 39 const ReceiveEncodedFrameCallback& callback) OVERRIDE; 40 virtual void RequestDecodedVideoFrame( 41 const VideoFrameDecodedCallback& callback) OVERRIDE; 42 virtual void RequestEncodedVideoFrame( 43 const ReceiveEncodedFrameCallback& callback) OVERRIDE; 44 45 private: 46 // Forwards |packet| to a specific RTP frame receiver, or drops it if SSRC 47 // does not map to one of the receivers. 48 void DispatchReceivedPacket(scoped_ptr<Packet> packet); 49 50 // Feeds an EncodedFrame into |audio_decoder_|. RequestDecodedAudioFrame() 51 // uses this as a callback for RequestEncodedAudioFrame(). 52 void DecodeEncodedAudioFrame( 53 const AudioFrameDecodedCallback& callback, 54 scoped_ptr<transport::EncodedFrame> encoded_frame); 55 56 // Feeds an EncodedFrame into |video_decoder_|. RequestDecodedVideoFrame() 57 // uses this as a callback for RequestEncodedVideoFrame(). 58 void DecodeEncodedVideoFrame( 59 const VideoFrameDecodedCallback& callback, 60 scoped_ptr<transport::EncodedFrame> encoded_frame); 61 62 // Receives an AudioBus from |audio_decoder_|, logs the event, and passes the 63 // data on by running the given |callback|. This method is static to ensure 64 // it can be called after a CastReceiverImpl instance is destroyed. 65 // DecodeEncodedAudioFrame() uses this as a callback for 66 // AudioDecoder::DecodeFrame(). 67 static void EmitDecodedAudioFrame( 68 const scoped_refptr<CastEnvironment>& cast_environment, 69 const AudioFrameDecodedCallback& callback, 70 uint32 frame_id, 71 uint32 rtp_timestamp, 72 const base::TimeTicks& playout_time, 73 scoped_ptr<AudioBus> audio_bus, 74 bool is_continuous); 75 76 // Receives a VideoFrame from |video_decoder_|, logs the event, and passes the 77 // data on by running the given |callback|. This method is static to ensure 78 // it can be called after a CastReceiverImpl instance is destroyed. 79 // DecodeEncodedVideoFrame() uses this as a callback for 80 // VideoDecoder::DecodeFrame(). 81 static void EmitDecodedVideoFrame( 82 const scoped_refptr<CastEnvironment>& cast_environment, 83 const VideoFrameDecodedCallback& callback, 84 uint32 frame_id, 85 uint32 rtp_timestamp, 86 const base::TimeTicks& playout_time, 87 const scoped_refptr<VideoFrame>& video_frame, 88 bool is_continuous); 89 90 const scoped_refptr<CastEnvironment> cast_environment_; 91 transport::PacedSender pacer_; 92 FrameReceiver audio_receiver_; 93 FrameReceiver video_receiver_; 94 95 // Used by DispatchReceivedPacket() to direct packets to the appropriate frame 96 // receiver. 97 const uint32 ssrc_of_audio_sender_; 98 const uint32 ssrc_of_video_sender_; 99 100 // Parameters for the decoders that are created on-demand. The values here 101 // might be nonsense if the client of CastReceiverImpl never intends to use 102 // the internal software-based decoders. 103 const int num_audio_channels_; 104 const int audio_sampling_rate_; 105 const transport::AudioCodec audio_codec_; 106 const transport::VideoCodec video_codec_; 107 108 // Created on-demand to decode frames from |audio_receiver_| into AudioBuses 109 // for playback. 110 scoped_ptr<AudioDecoder> audio_decoder_; 111 112 // Created on-demand to decode frames from |video_receiver_| into VideoFrame 113 // images for playback. 114 scoped_ptr<VideoDecoder> video_decoder_; 115 116 DISALLOW_COPY_AND_ASSIGN(CastReceiverImpl); 117 }; 118 119 } // namespace cast 120 } // namespace media 121 122 #endif // MEDIA_CAST_RECEIVER_CAST_RECEIVER_IMPL_ 123