1/*
2 *  Copyright (c) 2011 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_MODULES_VIDEO_CODING_MAIN_SOURCE_RECEIVER_H_
12#define WEBRTC_MODULES_VIDEO_CODING_MAIN_SOURCE_RECEIVER_H_
13
14#include "webrtc/modules/video_coding/main/source/jitter_buffer.h"
15#include "webrtc/modules/video_coding/main/source/packet.h"
16#include "webrtc/modules/video_coding/main/source/timing.h"
17#include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
18#include "webrtc/modules/video_coding/main/interface/video_coding.h"
19#include "webrtc/modules/video_coding/main/interface/video_coding_defines.h"
20
21namespace webrtc {
22
23class Clock;
24class VCMEncodedFrame;
25
26enum VCMNackStatus {
27  kNackOk,
28  kNackKeyFrameRequest
29};
30
31enum VCMReceiverState {
32  kReceiving,
33  kPassive,
34  kWaitForPrimaryDecode
35};
36
37class VCMReceiver {
38 public:
39  VCMReceiver(VCMTiming* timing,
40              Clock* clock,
41              EventFactory* event_factory,
42              bool master);
43  ~VCMReceiver();
44
45  void Reset();
46  int32_t Initialize();
47  void UpdateRtt(uint32_t rtt);
48  int32_t InsertPacket(const VCMPacket& packet,
49                       uint16_t frame_width,
50                       uint16_t frame_height);
51  VCMEncodedFrame* FrameForDecoding(uint16_t max_wait_time_ms,
52                                    int64_t& next_render_time_ms,
53                                    bool render_timing = true,
54                                    VCMReceiver* dual_receiver = NULL);
55  void ReleaseFrame(VCMEncodedFrame* frame);
56  void ReceiveStatistics(uint32_t* bitrate, uint32_t* framerate);
57  void ReceivedFrameCount(VCMFrameCount* frame_count) const;
58  uint32_t DiscardedPackets() const;
59
60  // NACK.
61  void SetNackMode(VCMNackMode nackMode,
62                   int low_rtt_nack_threshold_ms,
63                   int high_rtt_nack_threshold_ms);
64  void SetNackSettings(size_t max_nack_list_size,
65                       int max_packet_age_to_nack,
66                       int max_incomplete_time_ms);
67  VCMNackMode NackMode() const;
68  VCMNackStatus NackList(uint16_t* nackList, uint16_t size,
69                         uint16_t* nack_list_length);
70
71  // Dual decoder.
72  bool DualDecoderCaughtUp(VCMEncodedFrame* dual_frame,
73                           VCMReceiver& dual_receiver) const;
74  VCMReceiverState State() const;
75
76  // Receiver video delay.
77  int SetMinReceiverDelay(int desired_delay_ms);
78
79  // Decoding with errors.
80  void SetDecodeErrorMode(VCMDecodeErrorMode decode_error_mode);
81  VCMDecodeErrorMode DecodeErrorMode() const;
82
83  // Returns size in time (milliseconds) of complete continuous frames in the
84  // jitter buffer. The render time is estimated based on the render delay at
85  // the time this function is called.
86  int RenderBufferSizeMs();
87
88 private:
89  void CopyJitterBufferStateFromReceiver(const VCMReceiver& receiver);
90  void UpdateState(VCMReceiverState new_state);
91  void UpdateState(const VCMEncodedFrame& frame);
92  static int32_t GenerateReceiverId();
93
94  CriticalSectionWrapper* crit_sect_;
95  Clock* clock_;
96  bool master_;
97  VCMJitterBuffer jitter_buffer_;
98  VCMTiming* timing_;
99  scoped_ptr<EventWrapper> render_wait_event_;
100  VCMReceiverState state_;
101  int max_video_delay_ms_;
102
103  static int32_t receiver_id_counter_;
104};
105
106}  // namespace webrtc
107
108#endif  // WEBRTC_MODULES_VIDEO_CODING_MAIN_SOURCE_RECEIVER_H_
109