| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef MEDIA_CAST_AUDIO_RECEIVER_AUDIO_RECEIVER_H_ | 5 #ifndef MEDIA_CAST_AUDIO_RECEIVER_AUDIO_RECEIVER_H_ |
| 6 #define MEDIA_CAST_AUDIO_RECEIVER_AUDIO_RECEIVER_H_ | 6 #define MEDIA_CAST_AUDIO_RECEIVER_AUDIO_RECEIVER_H_ |
| 7 | 7 |
| 8 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
| 9 #include "base/callback.h" | 9 #include "base/callback.h" |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| 11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
| 12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
| 14 #include "base/threading/non_thread_safe.h" | 14 #include "base/threading/non_thread_safe.h" |
| 15 #include "base/time/tick_clock.h" | 15 #include "base/time/tick_clock.h" |
| 16 #include "base/time/time.h" | 16 #include "base/time/time.h" |
| 17 #include "media/cast/cast_config.h" | 17 #include "media/cast/cast_config.h" |
| 18 #include "media/cast/cast_environment.h" | 18 #include "media/cast/cast_environment.h" |
| 19 #include "media/cast/cast_receiver.h" | 19 #include "media/cast/cast_receiver.h" |
| 20 #include "media/cast/framer/framer.h" |
| 20 #include "media/cast/rtcp/receiver_rtcp_event_subscriber.h" | 21 #include "media/cast/rtcp/receiver_rtcp_event_subscriber.h" |
| 21 #include "media/cast/rtcp/rtcp.h" // RtcpCastMessage | 22 #include "media/cast/rtcp/rtcp.h" |
| 22 #include "media/cast/rtp_receiver/rtp_receiver.h" | 23 #include "media/cast/rtp_receiver/rtp_receiver.h" |
| 23 #include "media/cast/rtp_receiver/rtp_receiver_defines.h" // RtpCastHeader | 24 #include "media/cast/rtp_receiver/rtp_receiver_defines.h" |
| 24 #include "media/cast/transport/utility/transport_encryption_handler.h" | 25 #include "media/cast/transport/utility/transport_encryption_handler.h" |
| 25 | 26 |
| 26 namespace media { | 27 namespace media { |
| 27 namespace cast { | 28 namespace cast { |
| 28 | 29 |
| 29 class AudioDecoder; | 30 class AudioDecoder; |
| 30 class Framer; | |
| 31 class LocalRtpAudioFeedback; | |
| 32 class RtpReceiver; | |
| 33 class RtpReceiverStatistics; | |
| 34 | 31 |
| 35 struct DecodedAudioCallbackData { | 32 // AudioReceiver receives packets out-of-order while clients make requests for |
| 36 DecodedAudioCallbackData(); | 33 // complete frames in-order. (A frame consists of one or more packets.) |
| 37 ~DecodedAudioCallbackData(); | 34 // AudioReceiver also includes logic for mapping RTP timestamps to the local |
| 38 int number_of_10ms_blocks; | 35 // base::TimeTicks clock for each frame. |
| 39 int desired_frequency; | 36 // |
| 40 AudioFrameDecodedCallback callback; | 37 // Two types of frames can be requested: 1) A frame of decoded audio data; or 2) |
| 41 }; | 38 // a frame of still-encoded audio data, to be passed into an external audio |
| 42 | 39 // decoder. Each request for a frame includes a callback which AudioReceiver |
| 43 // This class is not thread safe. Should only be called from the Main cast | 40 // guarantees will be called at some point in the future. Clients should |
| 41 // generally limit the number of outstanding requests (perhaps to just one or |
| 42 // two). When AudioReceiver is destroyed, any outstanding requests will be |
| 43 // immediately invoked with a NULL frame. |
| 44 // |
| 45 // This class is not thread safe. Should only be called from the Main cast |
| 44 // thread. | 46 // thread. |
| 45 class AudioReceiver : public base::NonThreadSafe, | 47 class AudioReceiver : public RtpReceiver, |
| 46 public base::SupportsWeakPtr<AudioReceiver>, | 48 public RtpPayloadFeedback, |
| 47 public RtpReceiver { | 49 public base::NonThreadSafe, |
| 50 public base::SupportsWeakPtr<AudioReceiver> { |
| 48 public: | 51 public: |
| 49 AudioReceiver(scoped_refptr<CastEnvironment> cast_environment, | 52 AudioReceiver(scoped_refptr<CastEnvironment> cast_environment, |
| 50 const AudioReceiverConfig& audio_config, | 53 const AudioReceiverConfig& audio_config, |
| 51 transport::PacedPacketSender* const packet_sender); | 54 transport::PacedPacketSender* const packet_sender); |
| 52 | 55 |
| 53 virtual ~AudioReceiver(); | 56 virtual ~AudioReceiver(); |
| 54 | 57 |
| 55 // Extract a raw audio frame from the cast receiver. | 58 // Request a decoded audio frame. The audio signal data returned in the |
| 56 // Actual decoding will be preformed on a designated audio_decoder thread. | 59 // callback will have the sampling rate and number of channels as requested in |
| 57 void GetRawAudioFrame(int number_of_10ms_blocks, | 60 // the configuration that was passed to the ctor. |
| 58 int desired_frequency, | 61 // |
| 59 const AudioFrameDecodedCallback& callback); | 62 // The given |callback| is guaranteed to be run at some point in the future, |
| 63 // even if to respond with NULL at shutdown time. |
| 64 void GetRawAudioFrame(const AudioFrameDecodedCallback& callback); |
| 60 | 65 |
| 61 // Extract an encoded audio frame from the cast receiver. | 66 // Extract an encoded audio frame from the cast receiver. |
| 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. |
| 62 void GetEncodedAudioFrame(const AudioFrameEncodedCallback& callback); | 70 void GetEncodedAudioFrame(const AudioFrameEncodedCallback& callback); |
| 63 | 71 |
| 64 // Should only be called from the main cast thread. | 72 // Deliver another packet, possibly a duplicate, and possibly out-of-order. |
| 65 void IncomingPacket(scoped_ptr<Packet> packet); | 73 void IncomingPacket(scoped_ptr<Packet> packet); |
| 66 | 74 |
| 67 // Update target audio delay used to compute the playout time. Rtcp | 75 // Update target audio delay used to compute the playout time. Rtcp |
| 68 // will also be updated (will be included in all outgoing reports). | 76 // will also be updated (will be included in all outgoing reports). |
| 69 void SetTargetDelay(base::TimeDelta target_delay); | 77 void SetTargetDelay(base::TimeDelta target_delay); |
| 70 | 78 |
| 79 protected: |
| 80 friend class AudioReceiverTest; // Invokes OnReceivedPayloadData(). |
| 81 |
| 71 virtual void OnReceivedPayloadData(const uint8* payload_data, | 82 virtual void OnReceivedPayloadData(const uint8* payload_data, |
| 72 size_t payload_size, | 83 size_t payload_size, |
| 73 const RtpCastHeader& rtp_header) OVERRIDE; | 84 const RtpCastHeader& rtp_header) OVERRIDE; |
| 74 | 85 |
| 86 // RtpPayloadFeedback implementation. |
| 87 virtual void CastFeedback(const RtcpCastMessage& cast_message) OVERRIDE; |
| 88 |
| 75 private: | 89 private: |
| 76 friend class LocalRtpAudioFeedback; | 90 // Processes ready-to-consume packets from |framer_|, decrypting each packet's |
| 91 // payload data, and then running the enqueued callbacks in order (one for |
| 92 // each packet). This method may post a delayed task to re-invoke itself in |
| 93 // the future to wait for missing/incomplete frames. |
| 94 void EmitAvailableEncodedFrames(); |
| 77 | 95 |
| 78 void CastFeedback(const RtcpCastMessage& cast_message); | 96 // Clears the |is_waiting_for_consecutive_frame_| flag and invokes |
| 97 // EmitAvailableEncodedFrames(). |
| 98 void EmitAvailableEncodedFramesAfterWaiting(); |
| 79 | 99 |
| 80 // Time to pull out the audio even though we are missing data. | 100 // Feeds an EncodedAudioFrame into |audio_decoder_|. GetRawAudioFrame() uses |
| 81 void PlayoutTimeout(); | 101 // this as a callback for GetEncodedAudioFrame(). |
| 82 | 102 void DecodeEncodedAudioFrame( |
| 83 bool PostEncodedAudioFrame( | 103 const AudioFrameDecodedCallback& callback, |
| 84 const AudioFrameEncodedCallback& callback, | 104 scoped_ptr<transport::EncodedAudioFrame> encoded_frame, |
| 85 bool next_frame, | 105 const base::TimeTicks& playout_time); |
| 86 scoped_ptr<transport::EncodedAudioFrame>* encoded_frame); | |
| 87 | |
| 88 // Actual decoding implementation - should be called under the audio decoder | |
| 89 // thread. | |
| 90 void DecodeAudioFrameThread(int number_of_10ms_blocks, | |
| 91 int desired_frequency, | |
| 92 const AudioFrameDecodedCallback callback); | |
| 93 void ReturnDecodedFrameWithPlayoutDelay( | |
| 94 scoped_ptr<PcmAudioFrame> audio_frame, | |
| 95 uint32 rtp_timestamp, | |
| 96 const AudioFrameDecodedCallback callback); | |
| 97 | 106 |
| 98 // Return the playout time based on the current time and rtp timestamp. | 107 // Return the playout time based on the current time and rtp timestamp. |
| 99 base::TimeTicks GetPlayoutTime(base::TimeTicks now, uint32 rtp_timestamp); | 108 base::TimeTicks GetPlayoutTime(base::TimeTicks now, uint32 rtp_timestamp); |
| 100 | 109 |
| 101 void InitializeTimers(); | 110 void InitializeTimers(); |
| 102 | 111 |
| 103 // Decrypts the data within the |audio_frame| and replaces the data with the | |
| 104 // decrypted string. | |
| 105 bool DecryptAudioFrame(scoped_ptr<transport::EncodedAudioFrame>* audio_frame); | |
| 106 | |
| 107 // Schedule the next RTCP report. | 112 // Schedule the next RTCP report. |
| 108 void ScheduleNextRtcpReport(); | 113 void ScheduleNextRtcpReport(); |
| 109 | 114 |
| 110 // Actually send the next RTCP report. | 115 // Actually send the next RTCP report. |
| 111 void SendNextRtcpReport(); | 116 void SendNextRtcpReport(); |
| 112 | 117 |
| 113 // Schedule timing for the next cast message. | 118 // Schedule timing for the next cast message. |
| 114 void ScheduleNextCastMessage(); | 119 void ScheduleNextCastMessage(); |
| 115 | 120 |
| 116 // Actually send the next cast message. | 121 // Actually send the next cast message. |
| 117 void SendNextCastMessage(); | 122 void SendNextCastMessage(); |
| 118 | 123 |
| 119 scoped_refptr<CastEnvironment> cast_environment_; | 124 // Receives an AudioBus from |audio_decoder_|, logs the event, and passes the |
| 125 // data on by running the given |callback|. This method is static to ensure |
| 126 // it can be called after an AudioReceiver instance is destroyed. |
| 127 // DecodeEncodedAudioFrame() uses this as a callback for |
| 128 // AudioDecoder::DecodeFrame(). |
| 129 static void EmitRawAudioFrame( |
| 130 const scoped_refptr<CastEnvironment>& cast_environment, |
| 131 const AudioFrameDecodedCallback& callback, |
| 132 uint32 frame_id, |
| 133 uint32 rtp_timestamp, |
| 134 const base::TimeTicks& playout_time, |
| 135 scoped_ptr<AudioBus> audio_bus, |
| 136 bool is_continuous); |
| 137 |
| 138 const scoped_refptr<CastEnvironment> cast_environment_; |
| 120 | 139 |
| 121 // Subscribes to raw events. | 140 // Subscribes to raw events. |
| 122 // Processes raw audio events to be sent over to the cast sender via RTCP. | 141 // Processes raw audio events to be sent over to the cast sender via RTCP. |
| 123 ReceiverRtcpEventSubscriber event_subscriber_; | 142 ReceiverRtcpEventSubscriber event_subscriber_; |
| 124 | 143 |
| 125 const transport::AudioCodec codec_; | 144 const transport::AudioCodec codec_; |
| 126 const int frequency_; | 145 const int frequency_; |
| 127 base::TimeDelta target_delay_delta_; | 146 base::TimeDelta target_delay_delta_; |
| 128 scoped_ptr<Framer> audio_buffer_; | 147 Framer framer_; |
| 129 scoped_ptr<AudioDecoder> audio_decoder_; | 148 scoped_ptr<AudioDecoder> audio_decoder_; |
| 130 scoped_ptr<LocalRtpAudioFeedback> incoming_payload_feedback_; | 149 Rtcp rtcp_; |
| 131 scoped_ptr<Rtcp> rtcp_; | |
| 132 base::TimeDelta time_offset_; | 150 base::TimeDelta time_offset_; |
| 133 base::TimeTicks time_first_incoming_packet_; | 151 base::TimeTicks time_first_incoming_packet_; |
| 134 uint32 first_incoming_rtp_timestamp_; | 152 uint32 first_incoming_rtp_timestamp_; |
| 135 transport::TransportEncryptionHandler decryptor_; | 153 transport::TransportEncryptionHandler decryptor_; |
| 136 base::TimeTicks last_playout_time_; | |
| 137 | 154 |
| 138 std::list<AudioFrameEncodedCallback> queued_encoded_callbacks_; | 155 // Outstanding callbacks to run to deliver on client requests for frames. |
| 139 std::list<DecodedAudioCallbackData> queued_decoded_callbacks_; | 156 std::list<AudioFrameEncodedCallback> frame_request_queue_; |
| 157 |
| 158 // True while there's an outstanding task to re-invoke |
| 159 // EmitAvailableEncodedFrames(). |
| 160 bool is_waiting_for_consecutive_frame_; |
| 140 | 161 |
| 141 // This mapping allows us to log kAudioAckSent as a frame event. In addition | 162 // This mapping allows us to log kAudioAckSent as a frame event. In addition |
| 142 // it allows the event to be transmitted via RTCP. | 163 // it allows the event to be transmitted via RTCP. |
| 143 RtpTimestamp frame_id_to_rtp_timestamp_[256]; | 164 RtpTimestamp frame_id_to_rtp_timestamp_[256]; |
| 144 | 165 |
| 145 // NOTE: Weak pointers must be invalidated before all other member variables. | 166 // NOTE: Weak pointers must be invalidated before all other member variables. |
| 146 base::WeakPtrFactory<AudioReceiver> weak_factory_; | 167 base::WeakPtrFactory<AudioReceiver> weak_factory_; |
| 147 | 168 |
| 148 DISALLOW_COPY_AND_ASSIGN(AudioReceiver); | 169 DISALLOW_COPY_AND_ASSIGN(AudioReceiver); |
| 149 }; | 170 }; |
| 150 | 171 |
| 151 } // namespace cast | 172 } // namespace cast |
| 152 } // namespace media | 173 } // namespace media |
| 153 | 174 |
| 154 #endif // MEDIA_CAST_AUDIO_RECEIVER_AUDIO_RECEIVER_H_ | 175 #endif // MEDIA_CAST_AUDIO_RECEIVER_AUDIO_RECEIVER_H_ |
| OLD | NEW |