OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright (c) 2013 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/modules/video_coding/test/vcm_payload_sink_factory.h" | |
12 | |
13 #include <algorithm> | |
14 #include <utility> | |
15 | |
16 #include "webrtc/base/checks.h" | |
17 #include "webrtc/base/constructormagic.h" | |
18 #include "webrtc/modules/rtp_rtcp/include/rtp_rtcp.h" | |
19 #include "webrtc/modules/video_coding/test/test_util.h" | |
20 #include "webrtc/system_wrappers/include/clock.h" | |
21 #include "webrtc/system_wrappers/include/critical_section_wrapper.h" | |
22 | |
23 namespace webrtc { | |
24 namespace rtpplayer { | |
25 | |
26 class VcmPayloadSinkFactory::VcmPayloadSink : public PayloadSinkInterface, | |
27 public VCMPacketRequestCallback { | |
28 public: | |
29 VcmPayloadSink(VcmPayloadSinkFactory* factory, | |
30 RtpStreamInterface* stream, | |
31 std::unique_ptr<VideoCodingModule> vcm, | |
32 std::unique_ptr<FileOutputFrameReceiver> frame_receiver) | |
33 : factory_(factory), | |
34 stream_(stream), | |
35 vcm_(std::move(vcm)), | |
36 frame_receiver_(std::move(frame_receiver)) { | |
37 RTC_DCHECK(factory); | |
38 RTC_DCHECK(stream); | |
39 RTC_DCHECK(vcm_); | |
40 RTC_DCHECK(frame_receiver_); | |
41 vcm_->RegisterPacketRequestCallback(this); | |
42 vcm_->RegisterReceiveCallback(frame_receiver_.get()); | |
43 } | |
44 | |
45 ~VcmPayloadSink() override { factory_->Remove(this); } | |
46 | |
47 // PayloadSinkInterface | |
48 int32_t OnReceivedPayloadData(const uint8_t* payload_data, | |
49 size_t payload_size, | |
50 const WebRtcRTPHeader* rtp_header) override { | |
51 return vcm_->IncomingPacket(payload_data, payload_size, *rtp_header); | |
52 } | |
53 | |
54 bool OnRecoveredPacket(const uint8_t* packet, size_t packet_length) override { | |
55 // We currently don't handle FEC. | |
56 return true; | |
57 } | |
58 | |
59 // VCMPacketRequestCallback | |
60 int32_t ResendPackets(const uint16_t* sequence_numbers, | |
61 uint16_t length) override { | |
62 stream_->ResendPackets(sequence_numbers, length); | |
63 return 0; | |
64 } | |
65 | |
66 int DecodeAndProcess(bool should_decode, bool decode_dual_frame) { | |
67 if (should_decode) { | |
68 if (vcm_->Decode() < 0) { | |
69 return -1; | |
70 } | |
71 } | |
72 return Process() ? 0 : -1; | |
73 } | |
74 | |
75 bool Process() { | |
76 if (vcm_->TimeUntilNextProcess() <= 0) { | |
77 vcm_->Process(); | |
78 } | |
79 return true; | |
80 } | |
81 | |
82 bool Decode() { | |
83 vcm_->Decode(10000); | |
84 return true; | |
85 } | |
86 | |
87 private: | |
88 VcmPayloadSinkFactory* const factory_; | |
89 RtpStreamInterface* const stream_; | |
90 std::unique_ptr<VideoCodingModule> vcm_; | |
91 std::unique_ptr<FileOutputFrameReceiver> frame_receiver_; | |
92 | |
93 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(VcmPayloadSink); | |
94 }; | |
95 | |
96 VcmPayloadSinkFactory::VcmPayloadSinkFactory( | |
97 const std::string& base_out_filename, | |
98 Clock* clock, | |
99 bool protection_enabled, | |
100 VCMVideoProtection protection_method, | |
101 int64_t rtt_ms, | |
102 uint32_t render_delay_ms, | |
103 uint32_t min_playout_delay_ms) | |
104 : base_out_filename_(base_out_filename), | |
105 clock_(clock), | |
106 protection_enabled_(protection_enabled), | |
107 protection_method_(protection_method), | |
108 rtt_ms_(rtt_ms), | |
109 render_delay_ms_(render_delay_ms), | |
110 min_playout_delay_ms_(min_playout_delay_ms), | |
111 null_event_factory_(new NullEventFactory()), | |
112 crit_sect_(CriticalSectionWrapper::CreateCriticalSection()), | |
113 sinks_() { | |
114 RTC_DCHECK(clock); | |
115 RTC_DCHECK(crit_sect_.get()); | |
116 } | |
117 | |
118 VcmPayloadSinkFactory::~VcmPayloadSinkFactory() { | |
119 RTC_DCHECK(sinks_.empty()); | |
120 } | |
121 | |
122 PayloadSinkInterface* VcmPayloadSinkFactory::Create( | |
123 RtpStreamInterface* stream) { | |
124 RTC_DCHECK(stream); | |
125 CriticalSectionScoped cs(crit_sect_.get()); | |
126 | |
127 std::unique_ptr<VideoCodingModule> vcm( | |
128 VideoCodingModule::Create(clock_, null_event_factory_.get())); | |
129 if (vcm.get() == NULL) { | |
130 return NULL; | |
131 } | |
132 | |
133 const PayloadTypes& plt = stream->payload_types(); | |
134 for (PayloadTypesIterator it = plt.begin(); it != plt.end(); ++it) { | |
135 if (it->codec_type() != kVideoCodecULPFEC && | |
136 it->codec_type() != kVideoCodecRED) { | |
137 VideoCodec codec; | |
138 VideoCodingModule::Codec(it->codec_type(), &codec); | |
139 codec.plType = it->payload_type(); | |
140 if (vcm->RegisterReceiveCodec(&codec, 1) < 0) { | |
141 return NULL; | |
142 } | |
143 } | |
144 } | |
145 | |
146 vcm->SetChannelParameters(0, 0, rtt_ms_); | |
147 vcm->SetVideoProtection(protection_method_, protection_enabled_); | |
148 vcm->SetRenderDelay(render_delay_ms_); | |
149 vcm->SetMinimumPlayoutDelay(min_playout_delay_ms_); | |
150 vcm->SetNackSettings(kMaxNackListSize, kMaxPacketAgeToNack, 0); | |
151 | |
152 std::unique_ptr<FileOutputFrameReceiver> frame_receiver( | |
153 new FileOutputFrameReceiver(base_out_filename_, stream->ssrc())); | |
154 std::unique_ptr<VcmPayloadSink> sink(new VcmPayloadSink( | |
155 this, stream, std::move(vcm), std::move(frame_receiver))); | |
156 | |
157 sinks_.push_back(sink.get()); | |
158 return sink.release(); | |
159 } | |
160 | |
161 int VcmPayloadSinkFactory::DecodeAndProcessAll(bool decode_dual_frame) { | |
162 CriticalSectionScoped cs(crit_sect_.get()); | |
163 RTC_DCHECK(clock_); | |
164 bool should_decode = (clock_->TimeInMilliseconds() % 5) == 0; | |
165 for (Sinks::iterator it = sinks_.begin(); it != sinks_.end(); ++it) { | |
166 if ((*it)->DecodeAndProcess(should_decode, decode_dual_frame) < 0) { | |
167 return -1; | |
168 } | |
169 } | |
170 return 0; | |
171 } | |
172 | |
173 bool VcmPayloadSinkFactory::ProcessAll() { | |
174 CriticalSectionScoped cs(crit_sect_.get()); | |
175 for (Sinks::iterator it = sinks_.begin(); it != sinks_.end(); ++it) { | |
176 if (!(*it)->Process()) { | |
177 return false; | |
178 } | |
179 } | |
180 return true; | |
181 } | |
182 | |
183 bool VcmPayloadSinkFactory::DecodeAll() { | |
184 CriticalSectionScoped cs(crit_sect_.get()); | |
185 for (Sinks::iterator it = sinks_.begin(); it != sinks_.end(); ++it) { | |
186 if (!(*it)->Decode()) { | |
187 return false; | |
188 } | |
189 } | |
190 return true; | |
191 } | |
192 | |
193 void VcmPayloadSinkFactory::Remove(VcmPayloadSink* sink) { | |
194 RTC_DCHECK(sink); | |
195 CriticalSectionScoped cs(crit_sect_.get()); | |
196 Sinks::iterator it = std::find(sinks_.begin(), sinks_.end(), sink); | |
197 RTC_DCHECK(it != sinks_.end()); | |
198 sinks_.erase(it); | |
199 } | |
200 | |
201 } // namespace rtpplayer | |
202 } // namespace webrtc | |
OLD | NEW |