|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
|
3 * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 |
|
5 #ifndef VIDEO_SESSION_H_ |
|
6 #define VIDEO_SESSION_H_ |
|
7 |
|
8 #include "mozilla/Attributes.h" |
|
9 |
|
10 #include "MediaConduitInterface.h" |
|
11 #include "MediaEngineWrapper.h" |
|
12 |
|
13 // Video Engine Includes |
|
14 #include "webrtc/common_types.h" |
|
15 #ifdef FF |
|
16 #undef FF // Avoid name collision between scoped_ptr.h and nsCRTGlue.h. |
|
17 #endif |
|
18 #include "webrtc/modules/video_coding/codecs/interface/video_codec_interface.h" |
|
19 #include "webrtc/video_engine/include/vie_base.h" |
|
20 #include "webrtc/video_engine/include/vie_capture.h" |
|
21 #include "webrtc/video_engine/include/vie_codec.h" |
|
22 #include "webrtc/video_engine/include/vie_external_codec.h" |
|
23 #include "webrtc/video_engine/include/vie_render.h" |
|
24 #include "webrtc/video_engine/include/vie_network.h" |
|
25 #include "webrtc/video_engine/include/vie_rtp_rtcp.h" |
|
26 |
|
27 /** This file hosts several structures identifying different aspects |
|
28 * of a RTP Session. |
|
29 */ |
|
30 |
|
31 using webrtc::ViEBase; |
|
32 using webrtc::ViENetwork; |
|
33 using webrtc::ViECodec; |
|
34 using webrtc::ViECapture; |
|
35 using webrtc::ViERender; |
|
36 using webrtc::ViEExternalCapture; |
|
37 using webrtc::ViEExternalCodec; |
|
38 |
|
39 namespace mozilla { |
|
40 |
|
41 class WebrtcAudioConduit; |
|
42 |
|
43 // Interface of external video encoder for WebRTC. |
|
44 class WebrtcVideoEncoder:public VideoEncoder |
|
45 ,public webrtc::VideoEncoder |
|
46 {}; |
|
47 |
|
48 // Interface of external video decoder for WebRTC. |
|
49 class WebrtcVideoDecoder:public VideoDecoder |
|
50 ,public webrtc::VideoDecoder |
|
51 {}; |
|
52 |
|
53 /** |
|
54 * Concrete class for Video session. Hooks up |
|
55 * - media-source and target to external transport |
|
56 */ |
|
57 class WebrtcVideoConduit:public VideoSessionConduit |
|
58 ,public webrtc::Transport |
|
59 ,public webrtc::ExternalRenderer |
|
60 { |
|
61 public: |
|
62 //VoiceEngine defined constant for Payload Name Size. |
|
63 static const unsigned int CODEC_PLNAME_SIZE; |
|
64 |
|
65 /** |
|
66 * Set up A/V sync between this (incoming) VideoConduit and an audio conduit. |
|
67 */ |
|
68 void SyncTo(WebrtcAudioConduit *aConduit); |
|
69 |
|
70 /** |
|
71 * Function to attach Renderer end-point for the Media-Video conduit. |
|
72 * @param aRenderer : Reference to the concrete Video renderer implementation |
|
73 * Note: Multiple invocations of this API shall remove an existing renderer |
|
74 * and attaches the new to the Conduit. |
|
75 */ |
|
76 virtual MediaConduitErrorCode AttachRenderer(mozilla::RefPtr<VideoRenderer> aVideoRenderer); |
|
77 virtual void DetachRenderer(); |
|
78 |
|
79 /** |
|
80 * APIs used by the registered external transport to this Conduit to |
|
81 * feed in received RTP Frames to the VideoEngine for decoding |
|
82 */ |
|
83 virtual MediaConduitErrorCode ReceivedRTPPacket(const void *data, int len); |
|
84 |
|
85 /** |
|
86 * APIs used by the registered external transport to this Conduit to |
|
87 * feed in received RTP Frames to the VideoEngine for decoding |
|
88 */ |
|
89 virtual MediaConduitErrorCode ReceivedRTCPPacket(const void *data, int len); |
|
90 |
|
91 /** |
|
92 * Function to configure send codec for the video session |
|
93 * @param sendSessionConfig: CodecConfiguration |
|
94 * @result: On Success, the video engine is configured with passed in codec for send |
|
95 * On failure, video engine transmit functionality is disabled. |
|
96 * NOTE: This API can be invoked multiple time. Invoking this API may involve restarting |
|
97 * transmission sub-system on the engine. |
|
98 */ |
|
99 virtual MediaConduitErrorCode ConfigureSendMediaCodec(const VideoCodecConfig* codecInfo); |
|
100 |
|
101 /** |
|
102 * Function to configure list of receive codecs for the video session |
|
103 * @param sendSessionConfig: CodecConfiguration |
|
104 * @result: On Success, the video engine is configured with passed in codec for send |
|
105 * Also the playout is enabled. |
|
106 * On failure, video engine transmit functionality is disabled. |
|
107 * NOTE: This API can be invoked multiple time. Invoking this API may involve restarting |
|
108 * transmission sub-system on the engine. |
|
109 */ |
|
110 virtual MediaConduitErrorCode ConfigureRecvMediaCodecs( |
|
111 const std::vector<VideoCodecConfig* >& codecConfigList); |
|
112 |
|
113 /** |
|
114 * Register Transport for this Conduit. RTP and RTCP frames from the VideoEngine |
|
115 * shall be passed to the registered transport for transporting externally. |
|
116 */ |
|
117 virtual MediaConduitErrorCode AttachTransport(mozilla::RefPtr<TransportInterface> aTransport); |
|
118 |
|
119 /** |
|
120 * Function to select and change the encoding resolution based on incoming frame size |
|
121 * and current available bandwidth. |
|
122 * @param width, height: dimensions of the frame |
|
123 */ |
|
124 virtual bool SelectSendResolution(unsigned short width, |
|
125 unsigned short height); |
|
126 |
|
127 /** |
|
128 * Function to deliver a capture video frame for encoding and transport |
|
129 * @param video_frame: pointer to captured video-frame. |
|
130 * @param video_frame_length: size of the frame |
|
131 * @param width, height: dimensions of the frame |
|
132 * @param video_type: Type of the video frame - I420, RAW |
|
133 * @param captured_time: timestamp when the frame was captured. |
|
134 * if 0 timestamp is automatcally generated by the engine. |
|
135 *NOTE: ConfigureSendMediaCodec() SHOULD be called before this function can be invoked |
|
136 * This ensures the inserted video-frames can be transmitted by the conduit |
|
137 */ |
|
138 virtual MediaConduitErrorCode SendVideoFrame(unsigned char* video_frame, |
|
139 unsigned int video_frame_length, |
|
140 unsigned short width, |
|
141 unsigned short height, |
|
142 VideoType video_type, |
|
143 uint64_t capture_time); |
|
144 |
|
145 /** |
|
146 * Set an external encoder object |encoder| to the payload type |pltype| |
|
147 * for sender side codec. |
|
148 */ |
|
149 virtual MediaConduitErrorCode SetExternalSendCodec(int pltype, |
|
150 VideoEncoder* encoder); |
|
151 |
|
152 /** |
|
153 * Set an external decoder object |decoder| to the payload type |pltype| |
|
154 * for receiver side codec. |
|
155 */ |
|
156 virtual MediaConduitErrorCode SetExternalRecvCodec(int pltype, |
|
157 VideoDecoder* decoder); |
|
158 |
|
159 |
|
160 /** |
|
161 * Webrtc transport implementation to send and receive RTP packet. |
|
162 * VideoConduit registers itself as ExternalTransport to the VideoEngine |
|
163 */ |
|
164 virtual int SendPacket(int channel, const void *data, int len) ; |
|
165 |
|
166 /** |
|
167 * Webrtc transport implementation to send and receive RTCP packet. |
|
168 * VideoConduit registers itself as ExternalTransport to the VideoEngine |
|
169 */ |
|
170 virtual int SendRTCPPacket(int channel, const void *data, int len) ; |
|
171 |
|
172 |
|
173 /** |
|
174 * Webrtc External Renderer Implementation APIs. |
|
175 * Raw I420 Frames are delivred to the VideoConduit by the VideoEngine |
|
176 */ |
|
177 virtual int FrameSizeChange(unsigned int, unsigned int, unsigned int); |
|
178 |
|
179 virtual int DeliverFrame(unsigned char*,int, uint32_t , int64_t, |
|
180 void *handle); |
|
181 |
|
182 /** |
|
183 * Does DeliverFrame() support a null buffer and non-null handle |
|
184 * (video texture)? |
|
185 * B2G support it (when using HW video decoder with graphic buffer output). |
|
186 * XXX Investigate! Especially for Android |
|
187 */ |
|
188 virtual bool IsTextureSupported() { |
|
189 #ifdef WEBRTC_GONK |
|
190 return true; |
|
191 #else |
|
192 return false; |
|
193 #endif |
|
194 } |
|
195 |
|
196 unsigned short SendingWidth() { |
|
197 return mSendingWidth; |
|
198 } |
|
199 |
|
200 unsigned short SendingHeight() { |
|
201 return mSendingHeight; |
|
202 } |
|
203 |
|
204 unsigned int SendingMaxFs() { |
|
205 if(mCurSendCodecConfig) { |
|
206 return mCurSendCodecConfig->mMaxFrameSize; |
|
207 } |
|
208 return 0; |
|
209 } |
|
210 |
|
211 unsigned int SendingMaxFr() { |
|
212 if(mCurSendCodecConfig) { |
|
213 return mCurSendCodecConfig->mMaxFrameRate; |
|
214 } |
|
215 return 0; |
|
216 } |
|
217 |
|
218 WebrtcVideoConduit(): |
|
219 mOtherDirection(nullptr), |
|
220 mShutDown(false), |
|
221 mVideoEngine(nullptr), |
|
222 mTransport(nullptr), |
|
223 mRenderer(nullptr), |
|
224 mPtrExtCapture(nullptr), |
|
225 mEngineTransmitting(false), |
|
226 mEngineReceiving(false), |
|
227 mChannel(-1), |
|
228 mCapId(-1), |
|
229 mCurSendCodecConfig(nullptr), |
|
230 mSendingWidth(0), |
|
231 mSendingHeight(0), |
|
232 mReceivingWidth(640), |
|
233 mReceivingHeight(480), |
|
234 mVideoLatencyTestEnable(false), |
|
235 mVideoLatencyAvg(0) |
|
236 { |
|
237 } |
|
238 |
|
239 virtual ~WebrtcVideoConduit() ; |
|
240 |
|
241 MediaConduitErrorCode Init(WebrtcVideoConduit *other); |
|
242 |
|
243 int GetChannel() { return mChannel; } |
|
244 webrtc::VideoEngine* GetVideoEngine() { return mVideoEngine; } |
|
245 bool GetLocalSSRC(unsigned int* ssrc); |
|
246 bool GetRemoteSSRC(unsigned int* ssrc); |
|
247 bool GetAVStats(int32_t* jitterBufferDelayMs, |
|
248 int32_t* playoutBufferDelayMs, |
|
249 int32_t* avSyncOffsetMs); |
|
250 bool GetRTPStats(unsigned int* jitterMs, unsigned int* cumulativeLost); |
|
251 bool GetRTCPReceiverReport(DOMHighResTimeStamp* timestamp, |
|
252 uint32_t* jitterMs, |
|
253 uint32_t* packetsReceived, |
|
254 uint64_t* bytesReceived, |
|
255 uint32_t* cumulativeLost, |
|
256 int32_t* rttMs); |
|
257 bool GetRTCPSenderReport(DOMHighResTimeStamp* timestamp, |
|
258 unsigned int* packetsSent, |
|
259 uint64_t* bytesSent); |
|
260 uint64_t MozVideoLatencyAvg(); |
|
261 |
|
262 private: |
|
263 |
|
264 WebrtcVideoConduit(const WebrtcVideoConduit& other) MOZ_DELETE; |
|
265 void operator=(const WebrtcVideoConduit& other) MOZ_DELETE; |
|
266 |
|
267 //Local database of currently applied receive codecs |
|
268 typedef std::vector<VideoCodecConfig* > RecvCodecList; |
|
269 |
|
270 //Function to convert between WebRTC and Conduit codec structures |
|
271 void CodecConfigToWebRTCCodec(const VideoCodecConfig* codecInfo, |
|
272 webrtc::VideoCodec& cinst); |
|
273 |
|
274 // Function to copy a codec structure to Conduit's database |
|
275 bool CopyCodecToDB(const VideoCodecConfig* codecInfo); |
|
276 |
|
277 // Functions to verify if the codec passed is already in |
|
278 // conduits database |
|
279 bool CheckCodecForMatch(const VideoCodecConfig* codecInfo) const; |
|
280 bool CheckCodecsForMatch(const VideoCodecConfig* curCodecConfig, |
|
281 const VideoCodecConfig* codecInfo) const; |
|
282 |
|
283 //Checks the codec to be applied |
|
284 MediaConduitErrorCode ValidateCodecConfig(const VideoCodecConfig* codecInfo, bool send) const; |
|
285 |
|
286 //Utility function to dump recv codec database |
|
287 void DumpCodecDB() const; |
|
288 |
|
289 // Video Latency Test averaging filter |
|
290 void VideoLatencyUpdate(uint64_t new_sample); |
|
291 |
|
292 // The two sides of a send/receive pair of conduits each keep a pointer to the other. |
|
293 // They also share a single VideoEngine and mChannel. Shutdown must be coordinated |
|
294 // carefully to avoid double-freeing or accessing after one frees. |
|
295 WebrtcVideoConduit* mOtherDirection; |
|
296 // The other side has shut down our mChannel and related items already |
|
297 bool mShutDown; |
|
298 |
|
299 // A few of these are shared by both directions. They're released by the last |
|
300 // conduit to die. |
|
301 webrtc::VideoEngine* mVideoEngine; // shared |
|
302 mozilla::RefPtr<TransportInterface> mTransport; |
|
303 mozilla::RefPtr<VideoRenderer> mRenderer; |
|
304 |
|
305 ScopedCustomReleasePtr<webrtc::ViEBase> mPtrViEBase; |
|
306 ScopedCustomReleasePtr<webrtc::ViECapture> mPtrViECapture; |
|
307 ScopedCustomReleasePtr<webrtc::ViECodec> mPtrViECodec; |
|
308 ScopedCustomReleasePtr<webrtc::ViENetwork> mPtrViENetwork; |
|
309 ScopedCustomReleasePtr<webrtc::ViERender> mPtrViERender; |
|
310 ScopedCustomReleasePtr<webrtc::ViERTP_RTCP> mPtrRTP; |
|
311 ScopedCustomReleasePtr<webrtc::ViEExternalCodec> mPtrExtCodec; |
|
312 |
|
313 webrtc::ViEExternalCapture* mPtrExtCapture; // shared |
|
314 |
|
315 // Engine state we are concerned with. |
|
316 bool mEngineTransmitting; //If true ==> Transmit Sub-system is up and running |
|
317 bool mEngineReceiving; // if true ==> Receive Sus-sysmtem up and running |
|
318 |
|
319 int mChannel; // Video Channel for this conduit |
|
320 int mCapId; // Capturer for this conduit |
|
321 RecvCodecList mRecvCodecList; |
|
322 VideoCodecConfig* mCurSendCodecConfig; |
|
323 unsigned short mSendingWidth; |
|
324 unsigned short mSendingHeight; |
|
325 unsigned short mReceivingWidth; |
|
326 unsigned short mReceivingHeight; |
|
327 bool mVideoLatencyTestEnable; |
|
328 uint64_t mVideoLatencyAvg; |
|
329 |
|
330 static const unsigned int sAlphaNum = 7; |
|
331 static const unsigned int sAlphaDen = 8; |
|
332 static const unsigned int sRoundingPadding = 1024; |
|
333 |
|
334 mozilla::RefPtr<WebrtcAudioConduit> mSyncedTo; |
|
335 }; |
|
336 |
|
337 } // end namespace |
|
338 |
|
339 #endif |