| 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_defines.h" // RtpCastHeader | 23 #include "media/cast/rtp_receiver/rtp_receiver.h" |
| 24 #include "media/cast/rtp_receiver/rtp_receiver_defines.h" |
| 23 #include "media/cast/transport/utility/transport_encryption_handler.h" | 25 #include "media/cast/transport/utility/transport_encryption_handler.h" |
| 24 | 26 |
| 25 namespace media { | 27 namespace media { |
| 26 namespace cast { | 28 namespace cast { |
| 27 | 29 |
| 28 class AudioDecoder; | 30 class AudioDecoder; |
| 29 class Framer; | |
| 30 class LocalRtpAudioData; | |
| 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 RtpData, |
| 48 public RtpPayloadFeedback, |
| 49 public base::NonThreadSafe, |
| 46 public base::SupportsWeakPtr<AudioReceiver> { | 50 public base::SupportsWeakPtr<AudioReceiver> { |
| 47 public: | 51 public: |
| 48 AudioReceiver(scoped_refptr<CastEnvironment> cast_environment, | 52 AudioReceiver(scoped_refptr<CastEnvironment> cast_environment, |
| 49 const AudioReceiverConfig& audio_config, | 53 const AudioReceiverConfig& audio_config, |
| 50 transport::PacedPacketSender* const packet_sender); | 54 transport::PacedPacketSender* const packet_sender); |
| 51 | 55 |
| 52 virtual ~AudioReceiver(); | 56 virtual ~AudioReceiver(); |
| 53 | 57 |
| 54 // Extract a raw audio frame from the cast receiver. | 58 // Request a decoded audio frame. The audio signal data returned in the |
| 55 // 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 |
| 56 void GetRawAudioFrame(int number_of_10ms_blocks, | 60 // the configuration that was passed to the ctor. |
| 57 int desired_frequency, | 61 // |
| 58 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); |
| 59 | 65 |
| 60 // 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. |
| 61 void GetEncodedAudioFrame(const AudioFrameEncodedCallback& callback); | 70 void GetEncodedAudioFrame(const AudioFrameEncodedCallback& callback); |
| 62 | 71 |
| 63 // Should only be called from the main cast thread. | 72 // Deliver another packet, possibly a duplicate, and possibly out-of-order. |
| 64 void IncomingPacket(scoped_ptr<Packet> packet); | 73 void IncomingPacket(scoped_ptr<Packet> packet); |
| 65 | 74 |
| 66 // Update target audio delay used to compute the playout time. Rtcp | 75 // Update target audio delay used to compute the playout time. Rtcp |
| 67 // will also be updated (will be included in all outgoing reports). | 76 // will also be updated (will be included in all outgoing reports). |
| 68 void SetTargetDelay(base::TimeDelta target_delay); | 77 void SetTargetDelay(base::TimeDelta target_delay); |
| 69 | 78 |
| 70 protected: | 79 protected: |
| 71 void IncomingParsedRtpPacket(const uint8* payload_data, | 80 // RtpData implementation. |
| 72 size_t payload_size, | 81 virtual void OnReceivedPayloadData(const uint8* payload_data, |
| 73 const RtpCastHeader& rtp_header); | 82 size_t payload_size, |
| 83 const RtpCastHeader& rtp_header) OVERRIDE; |
| 84 |
| 85 // RtpPayloadFeedback implementation. |
| 86 virtual void CastFeedback(const RtcpCastMessage& cast_message) OVERRIDE; |
| 74 | 87 |
| 75 private: | 88 private: |
| 76 friend class LocalRtpAudioData; | 89 // Processes ready-to-consume packets from |framer_|, decrypting each packet's |
| 77 friend class LocalRtpAudioFeedback; | 90 // payload data, and then running the enqueued callbacks in order (one for |
| 91 // each packet). This method may post a delayed task to re-invoke itself in |
| 92 // the future to wait for missing/incomplete packets. |
| 93 void EmitAvailableEncodedFrames(); |
| 78 | 94 |
| 79 void CastFeedback(const RtcpCastMessage& cast_message); | 95 // Clears the |is_waiting_for_consecutive_frame_| flag and invokes |
| 96 // EmitAvailableEncodedFrames(). |
| 97 void EmitAvailableEncodedFramesAfterWaiting(); |
| 80 | 98 |
| 81 // Time to pull out the audio even though we are missing data. | 99 // Feeds an EncodedAudioFrame into |audio_decoder_|. GetRawAudioFrame() uses |
| 82 void PlayoutTimeout(); | 100 // this as a callback for GetEncodedAudioFrame(). |
| 83 | 101 void DecodeEncodedAudioFrame( |
| 84 bool PostEncodedAudioFrame( | 102 const AudioFrameDecodedCallback& callback, |
| 85 const AudioFrameEncodedCallback& callback, | 103 scoped_ptr<transport::EncodedAudioFrame> encoded_frame, |
| 86 bool next_frame, | 104 const base::TimeTicks& playout_time); |
| 87 scoped_ptr<transport::EncodedAudioFrame>* encoded_frame); | |
| 88 | |
| 89 // Actual decoding implementation - should be called under the audio decoder | |
| 90 // thread. | |
| 91 void DecodeAudioFrameThread(int number_of_10ms_blocks, | |
| 92 int desired_frequency, | |
| 93 const AudioFrameDecodedCallback callback); | |
| 94 void ReturnDecodedFrameWithPlayoutDelay( | |
| 95 scoped_ptr<PcmAudioFrame> audio_frame, | |
| 96 uint32 rtp_timestamp, | |
| 97 const AudioFrameDecodedCallback callback); | |
| 98 | 105 |
| 99 // Return the playout time based on the current time and rtp timestamp. | 106 // Return the playout time based on the current time and rtp timestamp. |
| 100 base::TimeTicks GetPlayoutTime(base::TimeTicks now, uint32 rtp_timestamp); | 107 base::TimeTicks GetPlayoutTime(base::TimeTicks now, uint32 rtp_timestamp); |
| 101 | 108 |
| 102 void InitializeTimers(); | 109 void InitializeTimers(); |
| 103 | 110 |
| 104 // Decrypts the data within the |audio_frame| and replaces the data with the | |
| 105 // decrypted string. | |
| 106 bool DecryptAudioFrame(scoped_ptr<transport::EncodedAudioFrame>* audio_frame); | |
| 107 | |
| 108 // Schedule the next RTCP report. | 111 // Schedule the next RTCP report. |
| 109 void ScheduleNextRtcpReport(); | 112 void ScheduleNextRtcpReport(); |
| 110 | 113 |
| 111 // Actually send the next RTCP report. | 114 // Actually send the next RTCP report. |
| 112 void SendNextRtcpReport(); | 115 void SendNextRtcpReport(); |
| 113 | 116 |
| 114 // Schedule timing for the next cast message. | 117 // Schedule timing for the next cast message. |
| 115 void ScheduleNextCastMessage(); | 118 void ScheduleNextCastMessage(); |
| 116 | 119 |
| 117 // Actually send the next cast message. | 120 // Actually send the next cast message. |
| 118 void SendNextCastMessage(); | 121 void SendNextCastMessage(); |
| 119 | 122 |
| 120 scoped_refptr<CastEnvironment> cast_environment_; | 123 // Receives an AudioBus from |audio_decoder_|, logs the event, and passes the |
| 124 // data on by running the given |callback|. This method is static to ensure |
| 125 // it can be called after an AudioReceiver instance is destroyed. |
| 126 // DecodeEncodedAudioFrame() uses this as a callback for |
| 127 // AudioDecoder::DecodeFrame(). |
| 128 static void EmitRawAudioFrame( |
| 129 const scoped_refptr<CastEnvironment>& cast_environment, |
| 130 const AudioFrameDecodedCallback& callback, |
| 131 uint32 frame_id, |
| 132 uint32 rtp_timestamp, |
| 133 const base::TimeTicks& playout_time, |
| 134 scoped_ptr<AudioBus> audio_bus, |
| 135 bool is_continuous); |
| 136 |
| 137 const scoped_refptr<CastEnvironment> cast_environment_; |
| 121 | 138 |
| 122 // Subscribes to raw events. | 139 // Subscribes to raw events. |
| 123 // Processes raw audio events to be sent over to the cast sender via RTCP. | 140 // Processes raw audio events to be sent over to the cast sender via RTCP. |
| 124 ReceiverRtcpEventSubscriber event_subscriber_; | 141 ReceiverRtcpEventSubscriber event_subscriber_; |
| 125 | 142 |
| 126 const transport::AudioCodec codec_; | 143 const transport::AudioCodec codec_; |
| 127 const int frequency_; | 144 const int frequency_; |
| 128 base::TimeDelta target_delay_delta_; | 145 base::TimeDelta target_delay_delta_; |
| 129 scoped_ptr<Framer> audio_buffer_; | 146 Framer framer_; |
| 130 scoped_ptr<AudioDecoder> audio_decoder_; | 147 scoped_ptr<AudioDecoder> audio_decoder_; |
| 131 scoped_ptr<LocalRtpAudioData> incoming_payload_callback_; | 148 RtpReceiver rtp_receiver_; |
| 132 scoped_ptr<LocalRtpAudioFeedback> incoming_payload_feedback_; | 149 Rtcp rtcp_; |
| 133 scoped_ptr<RtpReceiver> rtp_receiver_; | |
| 134 scoped_ptr<Rtcp> rtcp_; | |
| 135 scoped_ptr<RtpReceiverStatistics> rtp_audio_receiver_statistics_; | |
| 136 base::TimeDelta time_offset_; | 150 base::TimeDelta time_offset_; |
| 137 base::TimeTicks time_first_incoming_packet_; | 151 base::TimeTicks time_first_incoming_packet_; |
| 138 uint32 first_incoming_rtp_timestamp_; | 152 uint32 first_incoming_rtp_timestamp_; |
| 139 transport::TransportEncryptionHandler decryptor_; | 153 transport::TransportEncryptionHandler decryptor_; |
| 140 base::TimeTicks last_playout_time_; | |
| 141 | 154 |
| 142 std::list<AudioFrameEncodedCallback> queued_encoded_callbacks_; | 155 // Outstanding callbacks to run to deliver on client requests for frames. |
| 143 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_; |
| 144 | 161 |
| 145 // 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 |
| 146 // it allows the event to be transmitted via RTCP. | 163 // it allows the event to be transmitted via RTCP. |
| 147 RtpTimestamp frame_id_to_rtp_timestamp_[256]; | 164 RtpTimestamp frame_id_to_rtp_timestamp_[256]; |
| 148 | 165 |
| 149 // NOTE: Weak pointers must be invalidated before all other member variables. | 166 // NOTE: Weak pointers must be invalidated before all other member variables. |
| 150 base::WeakPtrFactory<AudioReceiver> weak_factory_; | 167 base::WeakPtrFactory<AudioReceiver> weak_factory_; |
| 151 | 168 |
| 152 DISALLOW_COPY_AND_ASSIGN(AudioReceiver); | 169 DISALLOW_COPY_AND_ASSIGN(AudioReceiver); |
| 153 }; | 170 }; |
| 154 | 171 |
| 155 } // namespace cast | 172 } // namespace cast |
| 156 } // namespace media | 173 } // namespace media |
| 157 | 174 |
| 158 #endif // MEDIA_CAST_AUDIO_RECEIVER_AUDIO_RECEIVER_H_ | 175 #endif // MEDIA_CAST_AUDIO_RECEIVER_AUDIO_RECEIVER_H_ |
| OLD | NEW |