| OLD | NEW |
| (Empty) |
| 1 // Copyright 2013 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_AUDIO_RECEIVER_AUDIO_RECEIVER_H_ | |
| 6 #define MEDIA_CAST_AUDIO_RECEIVER_AUDIO_RECEIVER_H_ | |
| 7 | |
| 8 #include "base/basictypes.h" | |
| 9 #include "base/callback.h" | |
| 10 #include "base/macros.h" | |
| 11 #include "base/memory/ref_counted.h" | |
| 12 #include "base/memory/scoped_ptr.h" | |
| 13 #include "base/memory/weak_ptr.h" | |
| 14 #include "base/threading/non_thread_safe.h" | |
| 15 #include "base/time/tick_clock.h" | |
| 16 #include "base/time/time.h" | |
| 17 #include "media/cast/base/clock_drift_smoother.h" | |
| 18 #include "media/cast/cast_config.h" | |
| 19 #include "media/cast/cast_environment.h" | |
| 20 #include "media/cast/cast_receiver.h" | |
| 21 #include "media/cast/framer/framer.h" | |
| 22 #include "media/cast/rtcp/receiver_rtcp_event_subscriber.h" | |
| 23 #include "media/cast/rtcp/rtcp.h" | |
| 24 #include "media/cast/rtp_receiver/rtp_receiver.h" | |
| 25 #include "media/cast/rtp_receiver/rtp_receiver_defines.h" | |
| 26 #include "media/cast/transport/utility/transport_encryption_handler.h" | |
| 27 | |
| 28 namespace media { | |
| 29 namespace cast { | |
| 30 | |
| 31 class AudioDecoder; | |
| 32 | |
| 33 // AudioReceiver receives packets out-of-order while clients make requests for | |
| 34 // complete frames in-order. (A frame consists of one or more packets.) | |
| 35 // | |
| 36 // AudioReceiver also includes logic for computing the playout time for each | |
| 37 // frame, accounting for a constant targeted playout delay. The purpose of the | |
| 38 // playout delay is to provide a fixed window of time between the capture event | |
| 39 // on the sender and the playout on the receiver. This is important because | |
| 40 // each step of the pipeline (i.e., encode frame, then transmit/retransmit from | |
| 41 // the sender, then receive and re-order packets on the receiver, then decode | |
| 42 // frame) can vary in duration and is typically very hard to predict. | |
| 43 // | |
| 44 // Two types of frames can be requested: 1) A frame of decoded audio data; or 2) | |
| 45 // a frame of still-encoded audio data, to be passed into an external audio | |
| 46 // decoder. Each request for a frame includes a callback which AudioReceiver | |
| 47 // guarantees will be called at some point in the future unless the | |
| 48 // AudioReceiver is destroyed. Clients should generally limit the number of | |
| 49 // outstanding requests (perhaps to just one or two). | |
| 50 // | |
| 51 // This class is not thread safe. Should only be called from the Main cast | |
| 52 // thread. | |
| 53 class AudioReceiver : public RtpReceiver, | |
| 54 public RtpPayloadFeedback, | |
| 55 public base::NonThreadSafe, | |
| 56 public base::SupportsWeakPtr<AudioReceiver> { | |
| 57 public: | |
| 58 AudioReceiver(scoped_refptr<CastEnvironment> cast_environment, | |
| 59 const FrameReceiverConfig& audio_config, | |
| 60 transport::PacedPacketSender* const packet_sender); | |
| 61 | |
| 62 virtual ~AudioReceiver(); | |
| 63 | |
| 64 // Request a decoded audio frame. The audio signal data returned in the | |
| 65 // callback will have the sampling rate and number of channels as requested in | |
| 66 // the configuration that was passed to the ctor. | |
| 67 // | |
| 68 // The given |callback| is guaranteed to be run at some point in the future, | |
| 69 // even if to respond with NULL at shutdown time. | |
| 70 void GetRawAudioFrame(const AudioFrameDecodedCallback& callback); | |
| 71 | |
| 72 // Request an encoded audio frame. | |
| 73 // | |
| 74 // The given |callback| is guaranteed to be run at some point in the future, | |
| 75 // even if to respond with NULL at shutdown time. | |
| 76 void GetEncodedAudioFrame(const FrameEncodedCallback& callback); | |
| 77 | |
| 78 // Deliver another packet, possibly a duplicate, and possibly out-of-order. | |
| 79 void IncomingPacket(scoped_ptr<Packet> packet); | |
| 80 | |
| 81 protected: | |
| 82 friend class AudioReceiverTest; // Invokes OnReceivedPayloadData(). | |
| 83 | |
| 84 virtual void OnReceivedPayloadData(const uint8* payload_data, | |
| 85 size_t payload_size, | |
| 86 const RtpCastHeader& rtp_header) OVERRIDE; | |
| 87 | |
| 88 // RtpPayloadFeedback implementation. | |
| 89 virtual void CastFeedback(const RtcpCastMessage& cast_message) OVERRIDE; | |
| 90 | |
| 91 private: | |
| 92 // Processes ready-to-consume packets from |framer_|, decrypting each packet's | |
| 93 // payload data, and then running the enqueued callbacks in order (one for | |
| 94 // each packet). This method may post a delayed task to re-invoke itself in | |
| 95 // the future to wait for missing/incomplete frames. | |
| 96 void EmitAvailableEncodedFrames(); | |
| 97 | |
| 98 // Clears the |is_waiting_for_consecutive_frame_| flag and invokes | |
| 99 // EmitAvailableEncodedFrames(). | |
| 100 void EmitAvailableEncodedFramesAfterWaiting(); | |
| 101 | |
| 102 // Feeds an EncodedFrame into |audio_decoder_|. GetRawAudioFrame() uses this | |
| 103 // as a callback for GetEncodedAudioFrame(). | |
| 104 void DecodeEncodedAudioFrame( | |
| 105 const AudioFrameDecodedCallback& callback, | |
| 106 scoped_ptr<transport::EncodedFrame> encoded_frame); | |
| 107 | |
| 108 // Computes the playout time for a frame with the given |rtp_timestamp|. | |
| 109 // Because lip-sync info is refreshed regularly, calling this method with the | |
| 110 // same argument may return different results. | |
| 111 base::TimeTicks GetPlayoutTime(uint32 rtp_timestamp) const; | |
| 112 | |
| 113 // Schedule the next RTCP report. | |
| 114 void ScheduleNextRtcpReport(); | |
| 115 | |
| 116 // Actually send the next RTCP report. | |
| 117 void SendNextRtcpReport(); | |
| 118 | |
| 119 // Schedule timing for the next cast message. | |
| 120 void ScheduleNextCastMessage(); | |
| 121 | |
| 122 // Actually send the next cast message. | |
| 123 void SendNextCastMessage(); | |
| 124 | |
| 125 // Receives an AudioBus from |audio_decoder_|, logs the event, and passes the | |
| 126 // data on by running the given |callback|. This method is static to ensure | |
| 127 // it can be called after an AudioReceiver instance is destroyed. | |
| 128 // DecodeEncodedAudioFrame() uses this as a callback for | |
| 129 // AudioDecoder::DecodeFrame(). | |
| 130 static void EmitRawAudioFrame( | |
| 131 const scoped_refptr<CastEnvironment>& cast_environment, | |
| 132 const AudioFrameDecodedCallback& callback, | |
| 133 uint32 frame_id, | |
| 134 uint32 rtp_timestamp, | |
| 135 const base::TimeTicks& playout_time, | |
| 136 scoped_ptr<AudioBus> audio_bus, | |
| 137 bool is_continuous); | |
| 138 | |
| 139 const scoped_refptr<CastEnvironment> cast_environment_; | |
| 140 | |
| 141 // Subscribes to raw events. | |
| 142 // Processes raw audio events to be sent over to the cast sender via RTCP. | |
| 143 ReceiverRtcpEventSubscriber event_subscriber_; | |
| 144 | |
| 145 // Configured audio codec. | |
| 146 const transport::AudioCodec codec_; | |
| 147 | |
| 148 // RTP timebase: The number of RTP units advanced per one second. For audio, | |
| 149 // this is the sampling rate. | |
| 150 const int frequency_; | |
| 151 | |
| 152 // The total amount of time between a frame's capture/recording on the sender | |
| 153 // and its playback on the receiver (i.e., shown to a user). This is fixed as | |
| 154 // a value large enough to give the system sufficient time to encode, | |
| 155 // transmit/retransmit, receive, decode, and render; given its run-time | |
| 156 // environment (sender/receiver hardware performance, network conditions, | |
| 157 // etc.). | |
| 158 const base::TimeDelta target_playout_delay_; | |
| 159 | |
| 160 // Hack: This is used in logic that determines whether to skip frames. | |
| 161 const base::TimeDelta expected_frame_duration_; | |
| 162 | |
| 163 // Set to false initially, then set to true after scheduling the periodic | |
| 164 // sending of reports back to the sender. Reports are first scheduled just | |
| 165 // after receiving a first packet (since the first packet identifies the | |
| 166 // sender for the remainder of the session). | |
| 167 bool reports_are_scheduled_; | |
| 168 | |
| 169 // Assembles packets into frames, providing this receiver with complete, | |
| 170 // decodable EncodedFrames. | |
| 171 Framer framer_; | |
| 172 | |
| 173 // Decodes frames into raw audio for playback. | |
| 174 scoped_ptr<AudioDecoder> audio_decoder_; | |
| 175 | |
| 176 // Manages sending/receiving of RTCP packets, including sender/receiver | |
| 177 // reports. | |
| 178 Rtcp rtcp_; | |
| 179 | |
| 180 // Decrypts encrypted frames. | |
| 181 transport::TransportEncryptionHandler decryptor_; | |
| 182 | |
| 183 // Outstanding callbacks to run to deliver on client requests for frames. | |
| 184 std::list<FrameEncodedCallback> frame_request_queue_; | |
| 185 | |
| 186 // True while there's an outstanding task to re-invoke | |
| 187 // EmitAvailableEncodedFrames(). | |
| 188 bool is_waiting_for_consecutive_frame_; | |
| 189 | |
| 190 // This mapping allows us to log AUDIO_ACK_SENT as a frame event. In addition | |
| 191 // it allows the event to be transmitted via RTCP. | |
| 192 RtpTimestamp frame_id_to_rtp_timestamp_[256]; | |
| 193 | |
| 194 // Lip-sync values used to compute the playout time of each frame from its RTP | |
| 195 // timestamp. These are updated each time the first packet of a frame is | |
| 196 // received. | |
| 197 RtpTimestamp lip_sync_rtp_timestamp_; | |
| 198 base::TimeTicks lip_sync_reference_time_; | |
| 199 ClockDriftSmoother lip_sync_drift_; | |
| 200 | |
| 201 // NOTE: Weak pointers must be invalidated before all other member variables. | |
| 202 base::WeakPtrFactory<AudioReceiver> weak_factory_; | |
| 203 | |
| 204 DISALLOW_COPY_AND_ASSIGN(AudioReceiver); | |
| 205 }; | |
| 206 | |
| 207 } // namespace cast | |
| 208 } // namespace media | |
| 209 | |
| 210 #endif // MEDIA_CAST_AUDIO_RECEIVER_AUDIO_RECEIVER_H_ | |
| OLD | NEW |