summaryrefslogtreecommitdiff
path: root/media/cast/rtp_sender/rtp_sender.cc
blob: d06a503dc89018b20fa8fd4936388c3f6185c00f (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// Copyright 2013 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.

#include "media/cast/rtp_sender/rtp_sender.h"

#include "base/logging.h"
#include "base/rand_util.h"
#include "media/cast/cast_defines.h"
#include "media/cast/pacing/paced_sender.h"
#include "media/cast/rtcp/rtcp_defines.h"

namespace media {
namespace cast {

RtpSender::RtpSender(base::TickClock* clock,
                     const AudioSenderConfig* audio_config,
                     const VideoSenderConfig* video_config,
                     PacedPacketSender* transport)
    : config_(),
      transport_(transport),
      clock_(clock) {
  // Store generic cast config and create packetizer config.
  DCHECK(audio_config || video_config) << "Invalid argument";
  if (audio_config) {
    storage_.reset(new PacketStorage(clock, audio_config->rtp_history_ms));
    config_.audio = true;
    config_.ssrc = audio_config->sender_ssrc;
    config_.payload_type = audio_config->rtp_payload_type;
    config_.frequency = audio_config->frequency;
    config_.audio_codec = audio_config->codec;
  } else {
    storage_.reset(new PacketStorage(clock, video_config->rtp_history_ms));
    config_.audio = false;
    config_.ssrc = video_config->sender_ssrc;
    config_.payload_type = video_config->rtp_payload_type;
    config_.frequency = kVideoFrequency;
    config_.video_codec = video_config->codec;
  }
  // Randomly set start values.
  config_.sequence_number = base::RandInt(0, 65535);
  config_.rtp_timestamp = base::RandInt(0, 65535);
  config_.rtp_timestamp += base::RandInt(0, 65535) << 16;
  packetizer_.reset(new RtpPacketizer(transport, storage_.get(), config_));
}

RtpSender::~RtpSender() {}

void RtpSender::IncomingEncodedVideoFrame(const EncodedVideoFrame* video_frame,
    const base::TimeTicks& capture_time) {
  packetizer_->IncomingEncodedVideoFrame(video_frame, capture_time);
}

void RtpSender::IncomingEncodedAudioFrame(const EncodedAudioFrame* audio_frame,
    const base::TimeTicks& recorded_time) {
  packetizer_->IncomingEncodedAudioFrame(audio_frame, recorded_time);
}

void RtpSender::ResendPackets(
    const MissingFramesAndPacketsMap& missing_frames_and_packets) {
  // Iterate over all frames in the list.
  for (MissingFramesAndPacketsMap::const_iterator it =
       missing_frames_and_packets.begin();
       it != missing_frames_and_packets.end(); ++it) {
    PacketList packets_to_resend;
    uint8 frame_id = it->first;
    const PacketIdSet& packets_set = it->second;
    bool success = false;

    if (packets_set.empty()) {
      VLOG(1) << "Missing all packets in frame " << static_cast<int>(frame_id);

      uint16 packet_id = 0;
      do {
        // Get packet from storage.
        success = storage_->GetPacket(frame_id, packet_id, &packets_to_resend);

        // Resend packet to the network.
        if (success) {
          VLOG(1) << "Resend " << static_cast<int>(frame_id)
                  << ":" << packet_id;
          // Set a unique incremental sequence number for every packet.
          Packet& packet = packets_to_resend.back();
          UpdateSequenceNumber(&packet);
          // Set the size as correspond to each frame.
          ++packet_id;
        }
      } while (success);
    } else {
      // Iterate over all of the packets in the frame.
      for (PacketIdSet::const_iterator set_it = packets_set.begin();
          set_it != packets_set.end(); ++set_it) {
        uint16 packet_id = *set_it;
        success = storage_->GetPacket(frame_id, packet_id, &packets_to_resend);

        // Resend packet to the network.
        if (success) {
          VLOG(1) << "Resend " << static_cast<int>(frame_id)
                  << ":" << packet_id;
          Packet& packet = packets_to_resend.back();
          UpdateSequenceNumber(&packet);
        }
      }
    }
    transport_->ResendPackets(packets_to_resend);
  }
}

void RtpSender::UpdateSequenceNumber(Packet* packet) {
  uint16 new_sequence_number = packetizer_->NextSequenceNumber();
  int index = 2;
  (*packet)[index] = (static_cast<uint8>(new_sequence_number));
  (*packet)[index + 1] =(static_cast<uint8>(new_sequence_number >> 8));
}

void RtpSender::RtpStatistics(const base::TimeTicks& now,
                              RtcpSenderInfo* sender_info) {
  // The timestamp of this Rtcp packet should be estimated as the timestamp of
  // the frame being captured at this moment. We are calculating that
  // timestamp as the last frame's timestamp + the time since the last frame
  // was captured.
  uint32 ntp_seconds = 0;
  uint32 ntp_fraction = 0;
  ConvertTimeTicksToNtp(now, &ntp_seconds, &ntp_fraction);
  sender_info->ntp_seconds = ntp_seconds;
  sender_info->ntp_fraction = ntp_fraction;

  base::TimeTicks time_sent;
  uint32 rtp_timestamp;
  if (packetizer_->LastSentTimestamp(&time_sent, &rtp_timestamp)) {
    base::TimeDelta time_since_last_send = now - time_sent;
    sender_info->rtp_timestamp = rtp_timestamp +
        time_since_last_send.InMilliseconds() * (config_.frequency / 1000);
  } else {
    sender_info->rtp_timestamp = 0;
  }
  sender_info->send_packet_count = packetizer_->send_packets_count();
  sender_info->send_octet_count = packetizer_->send_octet_count();
}

}  //  namespace cast
}  // namespace media