Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / webrtc / video_engine / vie_sender.cc
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
15 #include "webrtc/modules/utility/interface/rtp_dump.h"
16 #include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
17 #include "webrtc/system_wrappers/interface/trace.h"
18
19 namespace webrtc {
20
21 ViESender::ViESender(int channel_id)
22     : channel_id_(channel_id),
23       critsect_(CriticalSectionWrapper::CreateCriticalSection()),
24       transport_(NULL),
25       rtp_dump_(NULL) {
26 }
27
28 ViESender::~ViESender() {
29   if (rtp_dump_) {
30     rtp_dump_->Stop();
31     RtpDump::DestroyRtpDump(rtp_dump_);
32     rtp_dump_ = NULL;
33   }
34 }
35
36 int ViESender::RegisterSendTransport(Transport* transport) {
37   CriticalSectionScoped cs(critsect_.get());
38   if (transport_) {
39     return -1;
40   }
41   transport_ = transport;
42   return 0;
43 }
44
45 int ViESender::DeregisterSendTransport() {
46   CriticalSectionScoped cs(critsect_.get());
47   if (transport_ == NULL) {
48     return -1;
49   }
50   transport_ = NULL;
51   return 0;
52 }
53
54 int ViESender::StartRTPDump(const char file_nameUTF8[1024]) {
55   CriticalSectionScoped cs(critsect_.get());
56   if (rtp_dump_) {
57     // Packet dump is already started, restart it.
58     rtp_dump_->Stop();
59   } else {
60     rtp_dump_ = RtpDump::CreateRtpDump();
61     if (rtp_dump_ == NULL) {
62       WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideo, channel_id_,
63                    "StartSRTPDump: Failed to create RTP dump");
64       return -1;
65     }
66   }
67   if (rtp_dump_->Start(file_nameUTF8) != 0) {
68     RtpDump::DestroyRtpDump(rtp_dump_);
69     rtp_dump_ = NULL;
70     WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideo, channel_id_,
71                  "StartRTPDump: Failed to start RTP dump");
72     return -1;
73   }
74   return 0;
75 }
76
77 int ViESender::StopRTPDump() {
78   CriticalSectionScoped cs(critsect_.get());
79   if (rtp_dump_) {
80     if (rtp_dump_->IsActive()) {
81       rtp_dump_->Stop();
82     } else {
83       WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideo, channel_id_,
84                    "StopRTPDump: Dump not active");
85     }
86     RtpDump::DestroyRtpDump(rtp_dump_);
87     rtp_dump_ = NULL;
88   } else {
89     WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideo, channel_id_,
90                  "StopRTPDump: RTP dump not started");
91     return -1;
92   }
93   return 0;
94 }
95
96 int ViESender::SendPacket(int vie_id, const void* data, int len) {
97   CriticalSectionScoped cs(critsect_.get());
98   if (!transport_) {
99     // No transport
100     return -1;
101   }
102   assert(ChannelId(vie_id) == channel_id_);
103
104   if (rtp_dump_) {
105     rtp_dump_->DumpPacket(static_cast<const uint8_t*>(data),
106                           static_cast<uint16_t>(len));
107   }
108
109   const int bytes_sent = transport_->SendPacket(channel_id_, data, len);
110   if (bytes_sent != len) {
111     WEBRTC_TRACE(webrtc::kTraceWarning, webrtc::kTraceVideo, channel_id_,
112                  "ViESender::SendPacket - Transport failed to send RTP packet");
113   }
114   return bytes_sent;
115 }
116
117 int ViESender::SendRTCPPacket(int vie_id, const void* data, int len) {
118   CriticalSectionScoped cs(critsect_.get());
119   if (!transport_) {
120     return -1;
121   }
122   assert(ChannelId(vie_id) == channel_id_);
123
124   if (rtp_dump_) {
125     rtp_dump_->DumpPacket(static_cast<const uint8_t*>(data),
126                           static_cast<uint16_t>(len));
127   }
128
129   const int bytes_sent = transport_->SendRTCPPacket(channel_id_, data, len);
130   if (bytes_sent != len) {
131     WEBRTC_TRACE(
132         webrtc::kTraceWarning, webrtc::kTraceVideo, channel_id_,
133         "ViESender::SendRTCPPacket - Transport failed to send RTCP packet"
134         " (%d vs %d)", bytes_sent, len);
135   }
136   return bytes_sent;
137 }
138
139 }  // namespace webrtc