summaryrefslogtreecommitdiff
path: root/video_engine/vie_sender.cc
blob: f28e86551b96fc3f550db5edff75d996016a0334 (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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
 *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

#include "webrtc/video_engine/vie_sender.h"

#include <cassert>

#include "webrtc/modules/utility/interface/rtp_dump.h"
#include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
#include "webrtc/system_wrappers/interface/trace.h"

namespace webrtc {

ViESender::ViESender(int channel_id)
    : channel_id_(channel_id),
      critsect_(CriticalSectionWrapper::CreateCriticalSection()),
      external_encryption_(NULL),
      encryption_buffer_(NULL),
      transport_(NULL),
      rtp_dump_(NULL) {
}

ViESender::~ViESender() {
  if (encryption_buffer_) {
    delete[] encryption_buffer_;
    encryption_buffer_ = NULL;
  }

  if (rtp_dump_) {
    rtp_dump_->Stop();
    RtpDump::DestroyRtpDump(rtp_dump_);
    rtp_dump_ = NULL;
  }
}

int ViESender::RegisterExternalEncryption(Encryption* encryption) {
  CriticalSectionScoped cs(critsect_.get());
  if (external_encryption_) {
    return -1;
  }
  encryption_buffer_ = new uint8_t[kViEMaxMtu];
  if (encryption_buffer_ == NULL) {
    return -1;
  }
  external_encryption_ = encryption;
  return 0;
}

int ViESender::DeregisterExternalEncryption() {
  CriticalSectionScoped cs(critsect_.get());
  if (external_encryption_ == NULL) {
    return -1;
  }
  if (encryption_buffer_) {
    delete[] encryption_buffer_;
    encryption_buffer_ = NULL;
  }
  external_encryption_ = NULL;
  return 0;
}

int ViESender::RegisterSendTransport(Transport* transport) {
  CriticalSectionScoped cs(critsect_.get());
  if (transport_) {
    return -1;
  }
  transport_ = transport;
  return 0;
}

int ViESender::DeregisterSendTransport() {
  CriticalSectionScoped cs(critsect_.get());
  if (transport_ == NULL) {
    return -1;
  }
  transport_ = NULL;
  return 0;
}

int ViESender::StartRTPDump(const char file_nameUTF8[1024]) {
  CriticalSectionScoped cs(critsect_.get());
  if (rtp_dump_) {
    // Packet dump is already started, restart it.
    rtp_dump_->Stop();
  } else {
    rtp_dump_ = RtpDump::CreateRtpDump();
    if (rtp_dump_ == NULL) {
      WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideo, channel_id_,
                   "StartSRTPDump: Failed to create RTP dump");
      return -1;
    }
  }
  if (rtp_dump_->Start(file_nameUTF8) != 0) {
    RtpDump::DestroyRtpDump(rtp_dump_);
    rtp_dump_ = NULL;
    WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideo, channel_id_,
                 "StartRTPDump: Failed to start RTP dump");
    return -1;
  }
  return 0;
}

int ViESender::StopRTPDump() {
  CriticalSectionScoped cs(critsect_.get());
  if (rtp_dump_) {
    if (rtp_dump_->IsActive()) {
      rtp_dump_->Stop();
    } else {
      WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideo, channel_id_,
                   "StopRTPDump: Dump not active");
    }
    RtpDump::DestroyRtpDump(rtp_dump_);
    rtp_dump_ = NULL;
  } else {
    WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideo, channel_id_,
                 "StopRTPDump: RTP dump not started");
    return -1;
  }
  return 0;
}

int ViESender::SendPacket(int vie_id, const void* data, int len) {
  CriticalSectionScoped cs(critsect_.get());
  if (!transport_) {
    // No transport
    return -1;
  }
  assert(ChannelId(vie_id) == channel_id_);

  // TODO(mflodman) Change decrypt to get rid of this cast.
  void* tmp_ptr = const_cast<void*>(data);
  unsigned char* send_packet = static_cast<unsigned char*>(tmp_ptr);

  // Data length for packets sent to possible encryption and to the transport.
  int send_packet_length = len;

  if (rtp_dump_) {
    rtp_dump_->DumpPacket(send_packet, send_packet_length);
  }

  if (external_encryption_) {
    // Encryption buffer size.
    int encrypted_packet_length = kViEMaxMtu;

    external_encryption_->encrypt(channel_id_, send_packet, encryption_buffer_,
                                  send_packet_length, &encrypted_packet_length);
    send_packet = encryption_buffer_;
    send_packet_length = encrypted_packet_length;
  }
  const int bytes_sent = transport_->SendPacket(channel_id_, send_packet,
                                                send_packet_length);
  if (bytes_sent != send_packet_length) {
    WEBRTC_TRACE(webrtc::kTraceWarning, webrtc::kTraceVideo, channel_id_,
                 "ViESender::SendPacket - Transport failed to send RTP packet");
  }
  return bytes_sent;
}

int ViESender::SendRTCPPacket(int vie_id, const void* data, int len) {
  CriticalSectionScoped cs(critsect_.get());

  if (!transport_) {
    return -1;
  }

  assert(ChannelId(vie_id) == channel_id_);

  // Prepare for possible encryption and sending.
  // TODO(mflodman) Change decrypt to get rid of this cast.
  void* tmp_ptr = const_cast<void*>(data);
  unsigned char* send_packet = static_cast<unsigned char*>(tmp_ptr);

  // Data length for packets sent to possible encryption and to the transport.
  int send_packet_length = len;

  if (rtp_dump_) {
    rtp_dump_->DumpPacket(send_packet, send_packet_length);
  }

  if (external_encryption_) {
    // Encryption buffer size.
    int encrypted_packet_length = kViEMaxMtu;

    external_encryption_->encrypt_rtcp(
        channel_id_, send_packet, encryption_buffer_, send_packet_length,
        &encrypted_packet_length);
    send_packet = encryption_buffer_;
    send_packet_length = encrypted_packet_length;
  }

  const int bytes_sent = transport_->SendRTCPPacket(channel_id_, send_packet,
                                                    send_packet_length);
  if (bytes_sent != send_packet_length) {
    WEBRTC_TRACE(
        webrtc::kTraceWarning, webrtc::kTraceVideo, channel_id_,
        "ViESender::SendRTCPPacket - Transport failed to send RTCP packet"
        " (%d vs %d)", bytes_sent, send_packet_length);
  }
  return bytes_sent;
}

}  // namespace webrtc