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 return -1; 63 } 64 } 65 if (rtp_dump_->Start(file_nameUTF8) != 0) { 66 RtpDump::DestroyRtpDump(rtp_dump_); 67 rtp_dump_ = NULL; 68 return -1; 69 } 70 return 0; 71 } 72 73 int ViESender::StopRTPDump() { 74 CriticalSectionScoped cs(critsect_.get()); 75 if (rtp_dump_) { 76 if (rtp_dump_->IsActive()) { 77 rtp_dump_->Stop(); 78 } 79 RtpDump::DestroyRtpDump(rtp_dump_); 80 rtp_dump_ = NULL; 81 } else { 82 return -1; 83 } 84 return 0; 85 } 86 87 int ViESender::SendPacket(int vie_id, const void* data, int len) { 88 CriticalSectionScoped cs(critsect_.get()); 89 if (!transport_) { 90 // No transport 91 return -1; 92 } 93 assert(ChannelId(vie_id) == channel_id_); 94 95 if (rtp_dump_) { 96 rtp_dump_->DumpPacket(static_cast<const uint8_t*>(data), 97 static_cast<uint16_t>(len)); 98 } 99 100 return transport_->SendPacket(channel_id_, data, len); 101 } 102 103 int ViESender::SendRTCPPacket(int vie_id, const void* data, int len) { 104 CriticalSectionScoped cs(critsect_.get()); 105 if (!transport_) { 106 return -1; 107 } 108 assert(ChannelId(vie_id) == channel_id_); 109 110 if (rtp_dump_) { 111 rtp_dump_->DumpPacket(static_cast<const uint8_t*>(data), 112 static_cast<uint16_t>(len)); 113 } 114 115 return transport_->SendRTCPPacket(channel_id_, data, len); 116 } 117 118 } // namespace webrtc 119