summaryrefslogtreecommitdiffstats
path: root/media/cast/net/rtp/framer.h
blob: b215f7a38071aca8a9d48e22c76a42e10c54837f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef MEDIA_CAST_NET_RTP_FRAMER_H_
#define MEDIA_CAST_NET_RTP_FRAMER_H_

#include <map>

#include "base/memory/linked_ptr.h"
#include "base/memory/scoped_ptr.h"
#include "base/time/tick_clock.h"
#include "base/time/time.h"
#include "media/cast/net/rtcp/rtcp.h"
#include "media/cast/net/rtp/cast_message_builder.h"
#include "media/cast/net/rtp/frame_buffer.h"
#include "media/cast/net/rtp/rtp_defines.h"

namespace media {
namespace cast {

typedef std::map<uint32_t, linked_ptr<FrameBuffer>> FrameList;

class Framer {
 public:
  Framer(base::TickClock* clock,
         RtpPayloadFeedback* incoming_payload_feedback,
         uint32_t ssrc,
         bool decoder_faster_than_max_frame_rate,
         int max_unacked_frames);
  ~Framer();

  // Return true when receiving the last packet in a frame, creating a
  // complete frame. If a duplicate packet for an already complete frame is
  // received, the function returns false but sets |duplicate| to true.
  bool InsertPacket(const uint8_t* payload_data,
                    size_t payload_size,
                    const RtpCastHeader& rtp_header,
                    bool* duplicate);

  // Extracts a complete encoded frame - will only return a complete and
  // decodable frame. Returns false if no such frames exist.
  // |next_frame| will be set to true if the returned frame is the very
  // next frame. |have_multiple_complete_frames| will be set to true
  // if there are more decodadble frames available.
  bool GetEncodedFrame(EncodedFrame* video_frame,
                       bool* next_frame,
                       bool* have_multiple_complete_frames);

  // TODO(hubbe): Move this elsewhere.
  void AckFrame(uint32_t frame_id);

  void ReleaseFrame(uint32_t frame_id);

  // Reset framer state to original state and flush all pending buffers.
  void Reset();
  bool TimeToSendNextCastMessage(base::TimeTicks* time_to_send);
  void SendCastMessage();

  bool Empty() const;
  bool FrameExists(uint32_t frame_id) const;
  uint32_t NewestFrameId() const;

  void RemoveOldFrames(uint32_t frame_id);

  // Identifies the next frame to be released (rendered).
  bool NextContinuousFrame(uint32_t* frame_id) const;
  uint32_t LastContinuousFrame() const;

  bool NextFrameAllowingSkippingFrames(uint32_t* frame_id) const;
  bool HaveMultipleDecodableFrames() const;

  int NumberOfCompleteFrames() const;
  void GetMissingPackets(uint32_t frame_id,
                         bool last_frame,
                         PacketIdSet* missing_packets) const;

 private:
  bool ContinuousFrame(FrameBuffer* frame) const;
  bool DecodableFrame(FrameBuffer* frame) const;

  const bool decoder_faster_than_max_frame_rate_;
  FrameList frames_;
  scoped_ptr<CastMessageBuilder> cast_msg_builder_;
  bool waiting_for_key_;
  uint32_t last_released_frame_;
  uint32_t newest_frame_id_;

  DISALLOW_COPY_AND_ASSIGN(Framer);
};

}  //  namespace cast
}  //  namespace media

#endif  // MEDIA_CAST_NET_RTP_FRAMER_H_