Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
michael@0 | 3 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | |
michael@0 | 6 | #ifndef AUDIO_SESSION_H_ |
michael@0 | 7 | #define AUDIO_SESSION_H_ |
michael@0 | 8 | |
michael@0 | 9 | #include "mozilla/Attributes.h" |
michael@0 | 10 | #include "mozilla/TimeStamp.h" |
michael@0 | 11 | #include "nsTArray.h" |
michael@0 | 12 | |
michael@0 | 13 | #include "MediaConduitInterface.h" |
michael@0 | 14 | #include "MediaEngineWrapper.h" |
michael@0 | 15 | |
michael@0 | 16 | // Audio Engine Includes |
michael@0 | 17 | #include "webrtc/common_types.h" |
michael@0 | 18 | #include "webrtc/voice_engine/include/voe_base.h" |
michael@0 | 19 | #include "webrtc/voice_engine/include/voe_volume_control.h" |
michael@0 | 20 | #include "webrtc/voice_engine/include/voe_codec.h" |
michael@0 | 21 | #include "webrtc/voice_engine/include/voe_file.h" |
michael@0 | 22 | #include "webrtc/voice_engine/include/voe_network.h" |
michael@0 | 23 | #include "webrtc/voice_engine/include/voe_external_media.h" |
michael@0 | 24 | #include "webrtc/voice_engine/include/voe_audio_processing.h" |
michael@0 | 25 | #include "webrtc/voice_engine/include/voe_video_sync.h" |
michael@0 | 26 | #include "webrtc/voice_engine/include/voe_rtp_rtcp.h" |
michael@0 | 27 | //Some WebRTC types for short notations |
michael@0 | 28 | using webrtc::VoEBase; |
michael@0 | 29 | using webrtc::VoENetwork; |
michael@0 | 30 | using webrtc::VoECodec; |
michael@0 | 31 | using webrtc::VoEExternalMedia; |
michael@0 | 32 | using webrtc::VoEAudioProcessing; |
michael@0 | 33 | using webrtc::VoEVideoSync; |
michael@0 | 34 | using webrtc::VoERTP_RTCP; |
michael@0 | 35 | /** This file hosts several structures identifying different aspects |
michael@0 | 36 | * of a RTP Session. |
michael@0 | 37 | */ |
michael@0 | 38 | namespace mozilla { |
michael@0 | 39 | // Helper function |
michael@0 | 40 | |
michael@0 | 41 | DOMHighResTimeStamp |
michael@0 | 42 | NTPtoDOMHighResTimeStamp(uint32_t ntpHigh, uint32_t ntpLow); |
michael@0 | 43 | |
michael@0 | 44 | /** |
michael@0 | 45 | * Concrete class for Audio session. Hooks up |
michael@0 | 46 | * - media-source and target to external transport |
michael@0 | 47 | */ |
michael@0 | 48 | class WebrtcAudioConduit:public AudioSessionConduit |
michael@0 | 49 | ,public webrtc::Transport |
michael@0 | 50 | { |
michael@0 | 51 | public: |
michael@0 | 52 | //VoiceEngine defined constant for Payload Name Size. |
michael@0 | 53 | static const unsigned int CODEC_PLNAME_SIZE; |
michael@0 | 54 | |
michael@0 | 55 | /** |
michael@0 | 56 | * APIs used by the registered external transport to this Conduit to |
michael@0 | 57 | * feed in received RTP Frames to the VoiceEngine for decoding |
michael@0 | 58 | */ |
michael@0 | 59 | virtual MediaConduitErrorCode ReceivedRTPPacket(const void *data, int len); |
michael@0 | 60 | |
michael@0 | 61 | /** |
michael@0 | 62 | * APIs used by the registered external transport to this Conduit to |
michael@0 | 63 | * feed in received RTCP Frames to the VoiceEngine for decoding |
michael@0 | 64 | */ |
michael@0 | 65 | virtual MediaConduitErrorCode ReceivedRTCPPacket(const void *data, int len); |
michael@0 | 66 | |
michael@0 | 67 | /** |
michael@0 | 68 | * Function to configure send codec for the audio session |
michael@0 | 69 | * @param sendSessionConfig: CodecConfiguration |
michael@0 | 70 | * @result: On Success, the audio engine is configured with passed in codec for send |
michael@0 | 71 | * On failure, audio engine transmit functionality is disabled. |
michael@0 | 72 | * NOTE: This API can be invoked multiple time. Invoking this API may involve restarting |
michael@0 | 73 | * transmission sub-system on the engine. |
michael@0 | 74 | */ |
michael@0 | 75 | virtual MediaConduitErrorCode ConfigureSendMediaCodec(const AudioCodecConfig* codecConfig); |
michael@0 | 76 | /** |
michael@0 | 77 | * Function to configure list of receive codecs for the audio session |
michael@0 | 78 | * @param sendSessionConfig: CodecConfiguration |
michael@0 | 79 | * @result: On Success, the audio engine is configured with passed in codec for send |
michael@0 | 80 | * Also the playout is enabled. |
michael@0 | 81 | * On failure, audio engine transmit functionality is disabled. |
michael@0 | 82 | * NOTE: This API can be invoked multiple time. Invoking this API may involve restarting |
michael@0 | 83 | * transmission sub-system on the engine. |
michael@0 | 84 | */ |
michael@0 | 85 | virtual MediaConduitErrorCode ConfigureRecvMediaCodecs( |
michael@0 | 86 | const std::vector<AudioCodecConfig* >& codecConfigList); |
michael@0 | 87 | /** |
michael@0 | 88 | * Function to enable the audio level extension |
michael@0 | 89 | * @param enabled: enable extension |
michael@0 | 90 | */ |
michael@0 | 91 | virtual MediaConduitErrorCode EnableAudioLevelExtension(bool enabled, uint8_t id); |
michael@0 | 92 | |
michael@0 | 93 | /** |
michael@0 | 94 | * Register External Transport to this Conduit. RTP and RTCP frames from the VoiceEngine |
michael@0 | 95 | * shall be passed to the registered transport for transporting externally. |
michael@0 | 96 | */ |
michael@0 | 97 | virtual MediaConduitErrorCode AttachTransport(mozilla::RefPtr<TransportInterface> aTransport); |
michael@0 | 98 | /** |
michael@0 | 99 | * Function to deliver externally captured audio sample for encoding and transport |
michael@0 | 100 | * @param audioData [in]: Pointer to array containing a frame of audio |
michael@0 | 101 | * @param lengthSamples [in]: Length of audio frame in samples in multiple of 10 milliseconds |
michael@0 | 102 | * Ex: Frame length is 160, 320, 440 for 16, 32, 44 kHz sampling rates |
michael@0 | 103 | respectively. |
michael@0 | 104 | audioData[] should be of lengthSamples in size |
michael@0 | 105 | say, for 16kz sampling rate, audioData[] should contain 160 |
michael@0 | 106 | samples of 16-bits each for a 10m audio frame. |
michael@0 | 107 | * @param samplingFreqHz [in]: Frequency/rate of the sampling in Hz ( 16000, 32000 ...) |
michael@0 | 108 | * @param capture_delay [in]: Approx Delay from recording until it is delivered to VoiceEngine |
michael@0 | 109 | in milliseconds. |
michael@0 | 110 | * NOTE: ConfigureSendMediaCodec() SHOULD be called before this function can be invoked |
michael@0 | 111 | * This ensures the inserted audio-samples can be transmitted by the conduit |
michael@0 | 112 | * |
michael@0 | 113 | */ |
michael@0 | 114 | virtual MediaConduitErrorCode SendAudioFrame(const int16_t speechData[], |
michael@0 | 115 | int32_t lengthSamples, |
michael@0 | 116 | int32_t samplingFreqHz, |
michael@0 | 117 | int32_t capture_time); |
michael@0 | 118 | |
michael@0 | 119 | /** |
michael@0 | 120 | * Function to grab a decoded audio-sample from the media engine for rendering |
michael@0 | 121 | * / playoutof length 10 milliseconds. |
michael@0 | 122 | * |
michael@0 | 123 | * @param speechData [in]: Pointer to a array to which a 10ms frame of audio will be copied |
michael@0 | 124 | * @param samplingFreqHz [in]: Frequency of the sampling for playback in Hertz (16000, 32000,..) |
michael@0 | 125 | * @param capture_delay [in]: Estimated Time between reading of the samples to rendering/playback |
michael@0 | 126 | * @param lengthSamples [out]: Will contain length of the audio frame in samples at return. |
michael@0 | 127 | Ex: A value of 160 implies 160 samples each of 16-bits was copied |
michael@0 | 128 | into speechData |
michael@0 | 129 | * NOTE: This function should be invoked every 10 milliseconds for the best |
michael@0 | 130 | * peformance |
michael@0 | 131 | * NOTE: ConfigureRecvMediaCodec() SHOULD be called before this function can be invoked |
michael@0 | 132 | * This ensures the decoded samples are ready for reading and playout is enabled. |
michael@0 | 133 | * |
michael@0 | 134 | */ |
michael@0 | 135 | virtual MediaConduitErrorCode GetAudioFrame(int16_t speechData[], |
michael@0 | 136 | int32_t samplingFreqHz, |
michael@0 | 137 | int32_t capture_delay, |
michael@0 | 138 | int& lengthSamples); |
michael@0 | 139 | |
michael@0 | 140 | |
michael@0 | 141 | /** |
michael@0 | 142 | * Webrtc transport implementation to send and receive RTP packet. |
michael@0 | 143 | * AudioConduit registers itself as ExternalTransport to the VoiceEngine |
michael@0 | 144 | */ |
michael@0 | 145 | virtual int SendPacket(int channel, const void *data, int len) ; |
michael@0 | 146 | |
michael@0 | 147 | /** |
michael@0 | 148 | * Webrtc transport implementation to send and receive RTCP packet. |
michael@0 | 149 | * AudioConduit registers itself as ExternalTransport to the VoiceEngine |
michael@0 | 150 | */ |
michael@0 | 151 | virtual int SendRTCPPacket(int channel, const void *data, int len) ; |
michael@0 | 152 | |
michael@0 | 153 | |
michael@0 | 154 | |
michael@0 | 155 | WebrtcAudioConduit(): |
michael@0 | 156 | mOtherDirection(nullptr), |
michael@0 | 157 | mShutDown(false), |
michael@0 | 158 | mVoiceEngine(nullptr), |
michael@0 | 159 | mTransport(nullptr), |
michael@0 | 160 | mEngineTransmitting(false), |
michael@0 | 161 | mEngineReceiving(false), |
michael@0 | 162 | mChannel(-1), |
michael@0 | 163 | mCurSendCodecConfig(nullptr), |
michael@0 | 164 | mCaptureDelay(150), |
michael@0 | 165 | #ifdef MOZILLA_INTERNAL_API |
michael@0 | 166 | mLastTimestamp(0), |
michael@0 | 167 | #endif // MOZILLA_INTERNAL_API |
michael@0 | 168 | mSamples(0), |
michael@0 | 169 | mLastSyncLog(0) |
michael@0 | 170 | { |
michael@0 | 171 | } |
michael@0 | 172 | |
michael@0 | 173 | virtual ~WebrtcAudioConduit(); |
michael@0 | 174 | |
michael@0 | 175 | MediaConduitErrorCode Init(WebrtcAudioConduit *other); |
michael@0 | 176 | |
michael@0 | 177 | int GetChannel() { return mChannel; } |
michael@0 | 178 | webrtc::VoiceEngine* GetVoiceEngine() { return mVoiceEngine; } |
michael@0 | 179 | bool GetLocalSSRC(unsigned int* ssrc); |
michael@0 | 180 | bool GetRemoteSSRC(unsigned int* ssrc); |
michael@0 | 181 | bool GetAVStats(int32_t* jitterBufferDelayMs, |
michael@0 | 182 | int32_t* playoutBufferDelayMs, |
michael@0 | 183 | int32_t* avSyncOffsetMs); |
michael@0 | 184 | bool GetRTPStats(unsigned int* jitterMs, unsigned int* cumulativeLost); |
michael@0 | 185 | bool GetRTCPReceiverReport(DOMHighResTimeStamp* timestamp, |
michael@0 | 186 | uint32_t* jitterMs, |
michael@0 | 187 | uint32_t* packetsReceived, |
michael@0 | 188 | uint64_t* bytesReceived, |
michael@0 | 189 | uint32_t *cumulativeLost, |
michael@0 | 190 | int32_t* rttMs); |
michael@0 | 191 | bool GetRTCPSenderReport(DOMHighResTimeStamp* timestamp, |
michael@0 | 192 | unsigned int* packetsSent, |
michael@0 | 193 | uint64_t* bytesSent); |
michael@0 | 194 | |
michael@0 | 195 | private: |
michael@0 | 196 | WebrtcAudioConduit(const WebrtcAudioConduit& other) MOZ_DELETE; |
michael@0 | 197 | void operator=(const WebrtcAudioConduit& other) MOZ_DELETE; |
michael@0 | 198 | |
michael@0 | 199 | //Local database of currently applied receive codecs |
michael@0 | 200 | typedef std::vector<AudioCodecConfig* > RecvCodecList; |
michael@0 | 201 | |
michael@0 | 202 | //Function to convert between WebRTC and Conduit codec structures |
michael@0 | 203 | bool CodecConfigToWebRTCCodec(const AudioCodecConfig* codecInfo, |
michael@0 | 204 | webrtc::CodecInst& cinst); |
michael@0 | 205 | |
michael@0 | 206 | //Checks if given sampling frequency is supported |
michael@0 | 207 | bool IsSamplingFreqSupported(int freq) const; |
michael@0 | 208 | |
michael@0 | 209 | //Generate block size in sample lenght for a given sampling frequency |
michael@0 | 210 | unsigned int GetNum10msSamplesForFrequency(int samplingFreqHz) const; |
michael@0 | 211 | |
michael@0 | 212 | // Function to copy a codec structure to Conduit's database |
michael@0 | 213 | bool CopyCodecToDB(const AudioCodecConfig* codecInfo); |
michael@0 | 214 | |
michael@0 | 215 | // Functions to verify if the codec passed is already in |
michael@0 | 216 | // conduits database |
michael@0 | 217 | bool CheckCodecForMatch(const AudioCodecConfig* codecInfo) const; |
michael@0 | 218 | bool CheckCodecsForMatch(const AudioCodecConfig* curCodecConfig, |
michael@0 | 219 | const AudioCodecConfig* codecInfo) const; |
michael@0 | 220 | //Checks the codec to be applied |
michael@0 | 221 | MediaConduitErrorCode ValidateCodecConfig(const AudioCodecConfig* codecInfo, bool send) const; |
michael@0 | 222 | |
michael@0 | 223 | //Utility function to dump recv codec database |
michael@0 | 224 | void DumpCodecDB() const; |
michael@0 | 225 | |
michael@0 | 226 | // The two sides of a send/receive pair of conduits each keep a pointer to the other. |
michael@0 | 227 | // The also share a single VoiceEngine and mChannel. Shutdown must be coordinated |
michael@0 | 228 | // carefully to avoid double-freeing or accessing after one frees. |
michael@0 | 229 | WebrtcAudioConduit* mOtherDirection; |
michael@0 | 230 | // Other side has shut down our channel and related items already |
michael@0 | 231 | bool mShutDown; |
michael@0 | 232 | |
michael@0 | 233 | // These are shared by both directions. They're released by the last |
michael@0 | 234 | // conduit to die |
michael@0 | 235 | webrtc::VoiceEngine* mVoiceEngine; |
michael@0 | 236 | mozilla::RefPtr<TransportInterface> mTransport; |
michael@0 | 237 | ScopedCustomReleasePtr<webrtc::VoENetwork> mPtrVoENetwork; |
michael@0 | 238 | ScopedCustomReleasePtr<webrtc::VoEBase> mPtrVoEBase; |
michael@0 | 239 | ScopedCustomReleasePtr<webrtc::VoECodec> mPtrVoECodec; |
michael@0 | 240 | ScopedCustomReleasePtr<webrtc::VoEExternalMedia> mPtrVoEXmedia; |
michael@0 | 241 | ScopedCustomReleasePtr<webrtc::VoEAudioProcessing> mPtrVoEProcessing; |
michael@0 | 242 | ScopedCustomReleasePtr<webrtc::VoEVideoSync> mPtrVoEVideoSync; |
michael@0 | 243 | ScopedCustomReleasePtr<webrtc::VoERTP_RTCP> mPtrVoERTP_RTCP; |
michael@0 | 244 | ScopedCustomReleasePtr<webrtc::VoERTP_RTCP> mPtrRTP; |
michael@0 | 245 | //engine states of our interets |
michael@0 | 246 | bool mEngineTransmitting; // If true => VoiceEngine Send-subsystem is up |
michael@0 | 247 | bool mEngineReceiving; // If true => VoiceEngine Receive-subsystem is up |
michael@0 | 248 | // and playout is enabled |
michael@0 | 249 | // Keep track of each inserted RTP block and the time it was inserted |
michael@0 | 250 | // so we can estimate the clock time for a specific TimeStamp coming out |
michael@0 | 251 | // (for when we send data to MediaStreamTracks). Blocks are aged out as needed. |
michael@0 | 252 | struct Processing { |
michael@0 | 253 | TimeStamp mTimeStamp; |
michael@0 | 254 | uint32_t mRTPTimeStamp; // RTP timestamps received |
michael@0 | 255 | }; |
michael@0 | 256 | nsAutoTArray<Processing,8> mProcessing; |
michael@0 | 257 | |
michael@0 | 258 | int mChannel; |
michael@0 | 259 | RecvCodecList mRecvCodecList; |
michael@0 | 260 | AudioCodecConfig* mCurSendCodecConfig; |
michael@0 | 261 | |
michael@0 | 262 | // Current "capture" delay (really output plus input delay) |
michael@0 | 263 | int32_t mCaptureDelay; |
michael@0 | 264 | |
michael@0 | 265 | #ifdef MOZILLA_INTERNAL_API |
michael@0 | 266 | uint32_t mLastTimestamp; |
michael@0 | 267 | #endif // MOZILLA_INTERNAL_API |
michael@0 | 268 | |
michael@0 | 269 | uint32_t mSamples; |
michael@0 | 270 | uint32_t mLastSyncLog; |
michael@0 | 271 | }; |
michael@0 | 272 | |
michael@0 | 273 | } // end namespace |
michael@0 | 274 | |
michael@0 | 275 | #endif |