- add third_party src.
[platform/framework/web/crosswalk.git] / src / third_party / webrtc / video_engine / vie_channel.h
1 /*
2  *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10
11 #ifndef WEBRTC_VIDEO_ENGINE_VIE_CHANNEL_H_
12 #define WEBRTC_VIDEO_ENGINE_VIE_CHANNEL_H_
13
14 #include <list>
15
16 #include "webrtc/modules/remote_bitrate_estimator/include/remote_bitrate_estimator.h"
17 #include "webrtc/modules/rtp_rtcp/interface/rtp_rtcp_defines.h"
18 #include "webrtc/modules/video_coding/main/interface/video_coding_defines.h"
19 #include "webrtc/system_wrappers/interface/scoped_ptr.h"
20 #include "webrtc/system_wrappers/interface/tick_util.h"
21 #include "webrtc/typedefs.h"
22 #include "webrtc/video_engine/include/vie_network.h"
23 #include "webrtc/video_engine/include/vie_rtp_rtcp.h"
24 #include "webrtc/video_engine/vie_defines.h"
25 #include "webrtc/video_engine/vie_frame_provider_base.h"
26 #include "webrtc/video_engine/vie_receiver.h"
27 #include "webrtc/video_engine/vie_sender.h"
28 #include "webrtc/video_engine/vie_sync_module.h"
29
30 namespace webrtc {
31
32 class CallStatsObserver;
33 class ChannelStatsObserver;
34 class Config;
35 class CriticalSectionWrapper;
36 class Encryption;
37 class I420FrameCallback;
38 class PacedSender;
39 class ProcessThread;
40 class RtcpRttObserver;
41 class RtpRtcp;
42 class ThreadWrapper;
43 class ViEDecoderObserver;
44 class ViEEffectFilter;
45 class ViERTCPObserver;
46 class ViERTPObserver;
47 class VideoCodingModule;
48 class VideoDecoder;
49 class VideoRenderCallback;
50 class VoEVideoSync;
51
52 class ViEChannel
53     : public VCMFrameTypeCallback,
54       public VCMReceiveCallback,
55       public VCMReceiveStatisticsCallback,
56       public VCMDecoderTimingCallback,
57       public VCMPacketRequestCallback,
58       public RtcpFeedback,
59       public RtpFeedback,
60       public ViEFrameProviderBase {
61  public:
62   friend class ChannelStatsObserver;
63
64   ViEChannel(int32_t channel_id,
65              int32_t engine_id,
66              uint32_t number_of_cores,
67              const Config& config,
68              ProcessThread& module_process_thread,
69              RtcpIntraFrameObserver* intra_frame_observer,
70              RtcpBandwidthObserver* bandwidth_observer,
71              RemoteBitrateEstimator* remote_bitrate_estimator,
72              RtcpRttObserver* rtt_observer,
73              PacedSender* paced_sender,
74              RtpRtcp* default_rtp_rtcp,
75              bool sender);
76   ~ViEChannel();
77
78   int32_t Init();
79
80   // Sets the encoder to use for the channel. |new_stream| indicates the encoder
81   // type has changed and we should start a new RTP stream.
82   int32_t SetSendCodec(const VideoCodec& video_codec, bool new_stream = true);
83   int32_t SetReceiveCodec(const VideoCodec& video_codec);
84   int32_t GetReceiveCodec(VideoCodec* video_codec);
85   int32_t RegisterCodecObserver(ViEDecoderObserver* observer);
86   // Registers an external decoder. |buffered_rendering| means that the decoder
87   // will render frames after decoding according to the render timestamp
88   // provided by the video coding module. |render_delay| indicates the time
89   // needed to decode and render a frame.
90   int32_t RegisterExternalDecoder(const uint8_t pl_type,
91                                   VideoDecoder* decoder,
92                                   bool buffered_rendering,
93                                   int32_t render_delay);
94   int32_t DeRegisterExternalDecoder(const uint8_t pl_type);
95   int32_t ReceiveCodecStatistics(uint32_t* num_key_frames,
96                                  uint32_t* num_delta_frames);
97   uint32_t DiscardedPackets() const;
98
99   // Returns the estimated delay in milliseconds.
100   int ReceiveDelay() const;
101
102   // Only affects calls to SetReceiveCodec done after this call.
103   int32_t WaitForKeyFrame(bool wait);
104
105   // If enabled, a key frame request will be sent as soon as there are lost
106   // packets. If |only_key_frames| are set, requests are only sent for loss in
107   // key frames.
108   int32_t SetSignalPacketLossStatus(bool enable, bool only_key_frames);
109
110   int32_t SetRTCPMode(const RTCPMethod rtcp_mode);
111   int32_t GetRTCPMode(RTCPMethod* rtcp_mode);
112   int32_t SetNACKStatus(const bool enable);
113   int32_t SetFECStatus(const bool enable,
114                        const unsigned char payload_typeRED,
115                        const unsigned char payload_typeFEC);
116   int32_t SetHybridNACKFECStatus(const bool enable,
117                                  const unsigned char payload_typeRED,
118                                  const unsigned char payload_typeFEC);
119   int SetSenderBufferingMode(int target_delay_ms);
120   int SetReceiverBufferingMode(int target_delay_ms);
121   int32_t SetKeyFrameRequestMethod(const KeyFrameRequestMethod method);
122   bool EnableRemb(bool enable);
123   int SetSendTimestampOffsetStatus(bool enable, int id);
124   int SetReceiveTimestampOffsetStatus(bool enable, int id);
125   int SetSendAbsoluteSendTimeStatus(bool enable, int id);
126   int SetReceiveAbsoluteSendTimeStatus(bool enable, int id);
127   bool GetReceiveAbsoluteSendTimeStatus() const;
128   void SetTransmissionSmoothingStatus(bool enable);
129   int32_t EnableTMMBR(const bool enable);
130   int32_t EnableKeyFrameRequestCallback(const bool enable);
131
132   // Sets SSRC for outgoing stream.
133   int32_t SetSSRC(const uint32_t SSRC,
134                   const StreamType usage,
135                   const unsigned char simulcast_idx);
136
137   // Gets SSRC for outgoing stream number |idx|.
138   int32_t GetLocalSSRC(uint8_t idx, unsigned int* ssrc);
139
140   // Gets SSRC for the incoming stream.
141   int32_t GetRemoteSSRC(uint32_t* ssrc);
142
143   // Gets the CSRC for the incoming stream.
144   int32_t GetRemoteCSRC(uint32_t CSRCs[kRtpCsrcSize]);
145
146   int SetRtxSendPayloadType(int payload_type);
147   void SetRtxReceivePayloadType(int payload_type);
148
149   // Sets the starting sequence number, must be called before StartSend.
150   int32_t SetStartSequenceNumber(uint16_t sequence_number);
151
152   // Sets the CName for the outgoing stream on the channel.
153   int32_t SetRTCPCName(const char rtcp_cname[]);
154
155   // Gets the CName for the outgoing stream on the channel.
156   int32_t GetRTCPCName(char rtcp_cname[]);
157
158   // Gets the CName of the incoming stream.
159   int32_t GetRemoteRTCPCName(char rtcp_cname[]);
160   int32_t RegisterRtpObserver(ViERTPObserver* observer);
161   int32_t RegisterRtcpObserver(ViERTCPObserver* observer);
162   int32_t SendApplicationDefinedRTCPPacket(
163       const uint8_t sub_type,
164       uint32_t name,
165       const uint8_t* data,
166       uint16_t data_length_in_bytes);
167
168   // Returns statistics reported by the remote client in an RTCP packet.
169   int32_t GetSendRtcpStatistics(uint16_t* fraction_lost,
170                                 uint32_t* cumulative_lost,
171                                 uint32_t* extended_max,
172                                 uint32_t* jitter_samples,
173                                 int32_t* rtt_ms);
174
175   // Returns our localy created statistics of the received RTP stream.
176   int32_t GetReceivedRtcpStatistics(uint16_t* fraction_lost,
177                                     uint32_t* cumulative_lost,
178                                     uint32_t* extended_max,
179                                     uint32_t* jitter_samples,
180                                     int32_t* rtt_ms);
181
182   // Gets sent/received packets statistics.
183   int32_t GetRtpStatistics(uint32_t* bytes_sent,
184                            uint32_t* packets_sent,
185                            uint32_t* bytes_received,
186                            uint32_t* packets_received) const;
187   void GetBandwidthUsage(uint32_t* total_bitrate_sent,
188                          uint32_t* video_bitrate_sent,
189                          uint32_t* fec_bitrate_sent,
190                          uint32_t* nackBitrateSent) const;
191   void GetEstimatedReceiveBandwidth(uint32_t* estimated_bandwidth) const;
192
193   int32_t StartRTPDump(const char file_nameUTF8[1024],
194                        RTPDirections direction);
195   int32_t StopRTPDump(RTPDirections direction);
196
197   // Implements RtcpFeedback.
198   // TODO(pwestin) Depricate this functionality.
199   virtual void OnApplicationDataReceived(const int32_t id,
200                                          const uint8_t sub_type,
201                                          const uint32_t name,
202                                          const uint16_t length,
203                                          const uint8_t* data);
204   // Implements RtpFeedback.
205   virtual int32_t OnInitializeDecoder(
206       const int32_t id,
207       const int8_t payload_type,
208       const char payload_name[RTP_PAYLOAD_NAME_SIZE],
209       const int frequency,
210       const uint8_t channels,
211       const uint32_t rate);
212   virtual void OnIncomingSSRCChanged(const int32_t id,
213                                      const uint32_t ssrc);
214   virtual void OnIncomingCSRCChanged(const int32_t id,
215                                      const uint32_t CSRC,
216                                      const bool added);
217   virtual void ResetStatistics(uint32_t);
218
219   int32_t SetLocalReceiver(const uint16_t rtp_port,
220                            const uint16_t rtcp_port,
221                            const char* ip_address);
222   int32_t GetLocalReceiver(uint16_t* rtp_port,
223                            uint16_t* rtcp_port,
224                            char* ip_address) const;
225   int32_t SetSendDestination(const char* ip_address,
226                              const uint16_t rtp_port,
227                              const uint16_t rtcp_port,
228                              const uint16_t source_rtp_port,
229                              const uint16_t source_rtcp_port);
230   int32_t GetSendDestination(char* ip_address,
231                              uint16_t* rtp_port,
232                              uint16_t* rtcp_port,
233                              uint16_t* source_rtp_port,
234                              uint16_t* source_rtcp_port) const;
235   int32_t GetSourceInfo(uint16_t* rtp_port,
236                         uint16_t* rtcp_port,
237                         char* ip_address,
238                         uint32_t ip_address_length);
239
240   int32_t SetRemoteSSRCType(const StreamType usage, const uint32_t SSRC);
241
242   int32_t StartSend();
243   int32_t StopSend();
244   bool Sending();
245   int32_t StartReceive();
246   int32_t StopReceive();
247
248   int32_t RegisterSendTransport(Transport* transport);
249   int32_t DeregisterSendTransport();
250
251   // Incoming packet from external transport.
252   int32_t ReceivedRTPPacket(const void* rtp_packet,
253                             const int32_t rtp_packet_length);
254
255   // Incoming packet from external transport.
256   int32_t ReceivedRTCPPacket(const void* rtcp_packet,
257                              const int32_t rtcp_packet_length);
258
259   // Sets the maximum transfer unit size for the network link, i.e. including
260   // IP, UDP and RTP headers.
261   int32_t SetMTU(uint16_t mtu);
262
263   // Returns maximum allowed payload size, i.e. the maximum allowed size of
264   // encoded data in each packet.
265   uint16_t MaxDataPayloadLength() const;
266   int32_t SetMaxPacketBurstSize(uint16_t max_number_of_packets);
267   int32_t SetPacketBurstSpreadState(bool enable, const uint16_t frame_periodMS);
268
269   int32_t EnableColorEnhancement(bool enable);
270
271   // Gets the modules used by the channel.
272   RtpRtcp* rtp_rtcp();
273
274   CallStatsObserver* GetStatsObserver();
275
276   // Implements VCMReceiveCallback.
277   virtual int32_t FrameToRender(I420VideoFrame& video_frame);  // NOLINT
278
279   // Implements VCMReceiveCallback.
280   virtual int32_t ReceivedDecodedReferenceFrame(
281       const uint64_t picture_id);
282
283   // Implements VCMReceiveCallback.
284   virtual void IncomingCodecChanged(const VideoCodec& codec);
285
286   // Implements VCMReceiveStatisticsCallback.
287   virtual int32_t OnReceiveStatisticsUpdate(const uint32_t bit_rate,
288                                     const uint32_t frame_rate);
289
290   // Implements VCMDecoderTimingCallback.
291   virtual void OnDecoderTiming(int decode_ms,
292                                int max_decode_ms,
293                                int current_delay_ms,
294                                int target_delay_ms,
295                                int jitter_buffer_ms,
296                                int min_playout_delay_ms,
297                                int render_delay_ms);
298
299   // Implements VideoFrameTypeCallback.
300   virtual int32_t RequestKeyFrame();
301
302   // Implements VideoFrameTypeCallback.
303   virtual int32_t SliceLossIndicationRequest(
304       const uint64_t picture_id);
305
306   // Implements VideoPacketRequestCallback.
307   virtual int32_t ResendPackets(const uint16_t* sequence_numbers,
308                                 uint16_t length);
309
310   int32_t RegisterExternalEncryption(Encryption* encryption);
311   int32_t DeRegisterExternalEncryption();
312
313   int32_t SetVoiceChannel(int32_t ve_channel_id,
314                           VoEVideoSync* ve_sync_interface);
315   int32_t VoiceChannel();
316
317   // Implements ViEFrameProviderBase.
318   virtual int FrameCallbackChanged() {return -1;}
319
320   int32_t RegisterEffectFilter(ViEEffectFilter* effect_filter);
321
322   // New-style callback, used by VideoReceiveStream.
323   void RegisterPreRenderCallback(I420FrameCallback* pre_render_callback);
324
325  protected:
326   static bool ChannelDecodeThreadFunction(void* obj);
327   bool ChannelDecodeProcess();
328
329   void OnRttUpdate(uint32_t rtt);
330
331  private:
332   // Assumed to be protected.
333   int32_t StartDecodeThread();
334   int32_t StopDecodeThread();
335
336   int32_t ProcessNACKRequest(const bool enable);
337   int32_t ProcessFECRequest(const bool enable,
338                             const unsigned char payload_typeRED,
339                             const unsigned char payload_typeFEC);
340   // Compute NACK list parameters for the buffering mode.
341   int GetRequiredNackListSize(int target_delay_ms);
342
343   int32_t channel_id_;
344   int32_t engine_id_;
345   uint32_t number_of_cores_;
346   uint8_t num_socket_threads_;
347
348   // Used for all registered callbacks except rendering.
349   scoped_ptr<CriticalSectionWrapper> callback_cs_;
350   scoped_ptr<CriticalSectionWrapper> rtp_rtcp_cs_;
351
352   RtpRtcp* default_rtp_rtcp_;
353
354   // Owned modules/classes.
355   scoped_ptr<RtpRtcp> rtp_rtcp_;
356   std::list<RtpRtcp*> simulcast_rtp_rtcp_;
357   std::list<RtpRtcp*> removed_rtp_rtcp_;
358   VideoCodingModule& vcm_;
359   ViEReceiver vie_receiver_;
360   ViESender vie_sender_;
361   ViESyncModule vie_sync_;
362
363   // Helper to report call statistics.
364   scoped_ptr<ChannelStatsObserver> stats_observer_;
365
366   // Not owned.
367   ProcessThread& module_process_thread_;
368   ViEDecoderObserver* codec_observer_;
369   bool do_key_frame_callbackRequest_;
370   ViERTPObserver* rtp_observer_;
371   ViERTCPObserver* rtcp_observer_;
372   RtcpIntraFrameObserver* intra_frame_observer_;
373   RtcpRttObserver* rtt_observer_;
374   PacedSender* paced_sender_;
375
376   scoped_ptr<RtcpBandwidthObserver> bandwidth_observer_;
377   int send_timestamp_extension_id_;
378   int absolute_send_time_extension_id_;
379   bool receive_absolute_send_time_enabled_;
380   bool using_packet_spread_;
381
382   Transport* external_transport_;
383
384   bool decoder_reset_;
385   // Current receive codec used for codec change callback.
386   VideoCodec receive_codec_;
387   bool wait_for_key_frame_;
388   ThreadWrapper* decode_thread_;
389
390   Encryption* external_encryption_;
391
392   ViEEffectFilter* effect_filter_;
393   bool color_enhancement_;
394
395   // User set MTU, -1 if not set.
396   uint16_t mtu_;
397   const bool sender_;
398
399   int nack_history_size_sender_;
400   int max_nack_reordering_threshold_;
401   I420FrameCallback* pre_render_callback_;
402 };
403
404 }  // namespace webrtc
405
406 #endif  // WEBRTC_VIDEO_ENGINE_VIE_CHANNEL_H_