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#include "webrtc/video_engine/vie_sender.h"
12
13#include <assert.h>
14#include "webrtc/modules/rtp_rtcp/source/rtp_sender.h"
15
16#include "webrtc/modules/utility/interface/rtp_dump.h"
17#include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
18#include "webrtc/system_wrappers/interface/trace.h"
19
20namespace webrtc {
21
22ViESender::ViESender(int channel_id)
23    : channel_id_(channel_id),
24      critsect_(CriticalSectionWrapper::CreateCriticalSection()),
25      transport_(NULL),
26      rtp_dump_(NULL) {
27}
28
29ViESender::~ViESender() {
30  if (rtp_dump_) {
31    rtp_dump_->Stop();
32    RtpDump::DestroyRtpDump(rtp_dump_);
33    rtp_dump_ = NULL;
34  }
35}
36
37int ViESender::RegisterSendTransport(Transport* transport) {
38  CriticalSectionScoped cs(critsect_.get());
39  if (transport_) {
40    return -1;
41  }
42  transport_ = transport;
43  return 0;
44}
45
46int ViESender::DeregisterSendTransport() {
47  CriticalSectionScoped cs(critsect_.get());
48  if (transport_ == NULL) {
49    return -1;
50  }
51  transport_ = NULL;
52  return 0;
53}
54
55int ViESender::StartRTPDump(const char file_nameUTF8[1024]) {
56  CriticalSectionScoped cs(critsect_.get());
57  if (rtp_dump_) {
58    // Packet dump is already started, restart it.
59    rtp_dump_->Stop();
60  } else {
61    rtp_dump_ = RtpDump::CreateRtpDump();
62    if (rtp_dump_ == NULL) {
63      return -1;
64    }
65  }
66  if (rtp_dump_->Start(file_nameUTF8) != 0) {
67    RtpDump::DestroyRtpDump(rtp_dump_);
68    rtp_dump_ = NULL;
69    return -1;
70  }
71  return 0;
72}
73
74int ViESender::StopRTPDump() {
75  CriticalSectionScoped cs(critsect_.get());
76  if (rtp_dump_) {
77    if (rtp_dump_->IsActive()) {
78      rtp_dump_->Stop();
79    }
80    RtpDump::DestroyRtpDump(rtp_dump_);
81    rtp_dump_ = NULL;
82  } else {
83    return -1;
84  }
85  return 0;
86}
87
88int ViESender::SendPacket(int vie_id, const void* data, int len) {
89  CriticalSectionScoped cs(critsect_.get());
90  if (!transport_) {
91    // No transport
92    return -1;
93  }
94  assert(ChannelId(vie_id) == channel_id_);
95
96  if (rtp_dump_) {
97    rtp_dump_->DumpPacket(static_cast<const uint8_t*>(data),
98                          static_cast<uint16_t>(len));
99  }
100
101  return transport_->SendPacket(channel_id_, data, len);
102}
103
104int ViESender::SendRTCPPacket(int vie_id, const void* data, int len) {
105  CriticalSectionScoped cs(critsect_.get());
106  if (!transport_) {
107    return -1;
108  }
109  assert(ChannelId(vie_id) == channel_id_);
110
111  if (rtp_dump_) {
112    rtp_dump_->DumpPacket(static_cast<const uint8_t*>(data),
113                          static_cast<uint16_t>(len));
114  }
115
116  return transport_->SendRTCPPacket(channel_id_, data, len);
117}
118
119}  // namespace webrtc
120