OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2012 The WebRTC project authors. All Rights Reserved. | 2 * Copyright 2012 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
71 int gap; | 71 int gap; |
72 }; | 72 }; |
73 | 73 |
74 FakeDtmfProvider() : last_insert_dtmf_call_(0) {} | 74 FakeDtmfProvider() : last_insert_dtmf_call_(0) {} |
75 | 75 |
76 ~FakeDtmfProvider() { | 76 ~FakeDtmfProvider() { |
77 SignalDestroyed(); | 77 SignalDestroyed(); |
78 } | 78 } |
79 | 79 |
80 // Implements DtmfProviderInterface. | 80 // Implements DtmfProviderInterface. |
81 bool CanInsertDtmf(const std::string& track_label) override { | 81 bool CanInsertDtmf() override { return can_insert_; } |
82 return (can_insert_dtmf_tracks_.count(track_label) != 0); | |
83 } | |
84 | 82 |
85 bool InsertDtmf(const std::string& track_label, | 83 bool InsertDtmf(int code, int duration) override { |
86 int code, | |
87 int duration) override { | |
88 int gap = 0; | 84 int gap = 0; |
89 // TODO(ronghuawu): Make the timer (basically the rtc::TimeNanos) | 85 // TODO(ronghuawu): Make the timer (basically the rtc::TimeNanos) |
90 // mockable and use a fake timer in the unit tests. | 86 // mockable and use a fake timer in the unit tests. |
91 if (last_insert_dtmf_call_ > 0) { | 87 if (last_insert_dtmf_call_ > 0) { |
92 gap = static_cast<int>(rtc::TimeMillis() - last_insert_dtmf_call_); | 88 gap = static_cast<int>(rtc::TimeMillis() - last_insert_dtmf_call_); |
93 } | 89 } |
94 last_insert_dtmf_call_ = rtc::TimeMillis(); | 90 last_insert_dtmf_call_ = rtc::TimeMillis(); |
95 | 91 |
96 LOG(LS_VERBOSE) << "FakeDtmfProvider::InsertDtmf code=" << code | 92 LOG(LS_VERBOSE) << "FakeDtmfProvider::InsertDtmf code=" << code |
97 << " duration=" << duration | 93 << " duration=" << duration |
98 << " gap=" << gap << "."; | 94 << " gap=" << gap << "."; |
99 dtmf_info_queue_.push_back(DtmfInfo(code, duration, gap)); | 95 dtmf_info_queue_.push_back(DtmfInfo(code, duration, gap)); |
100 return true; | 96 return true; |
101 } | 97 } |
102 | 98 |
103 sigslot::signal0<>* GetOnDestroyedSignal() override { | 99 sigslot::signal0<>* GetOnDestroyedSignal() override { |
104 return &SignalDestroyed; | 100 return &SignalDestroyed; |
105 } | 101 } |
106 | 102 |
107 // getter and setter | 103 // getter and setter |
108 const std::vector<DtmfInfo>& dtmf_info_queue() const { | 104 const std::vector<DtmfInfo>& dtmf_info_queue() const { |
109 return dtmf_info_queue_; | 105 return dtmf_info_queue_; |
110 } | 106 } |
111 | 107 |
112 // helper functions | 108 // helper functions |
113 void AddCanInsertDtmfTrack(const std::string& label) { | 109 void SetCanInsertDtmf(bool can_insert) { can_insert_ = can_insert; } |
114 can_insert_dtmf_tracks_.insert(label); | |
115 } | |
116 void RemoveCanInsertDtmfTrack(const std::string& label) { | |
117 can_insert_dtmf_tracks_.erase(label); | |
118 } | |
119 | 110 |
120 private: | 111 private: |
121 std::set<std::string> can_insert_dtmf_tracks_; | 112 bool can_insert_ = false; |
122 std::vector<DtmfInfo> dtmf_info_queue_; | 113 std::vector<DtmfInfo> dtmf_info_queue_; |
123 int64_t last_insert_dtmf_call_; | 114 int64_t last_insert_dtmf_call_; |
124 sigslot::signal0<> SignalDestroyed; | 115 sigslot::signal0<> SignalDestroyed; |
125 }; | 116 }; |
126 | 117 |
127 class DtmfSenderTest : public testing::Test { | 118 class DtmfSenderTest : public testing::Test { |
128 protected: | 119 protected: |
129 DtmfSenderTest() | 120 DtmfSenderTest() |
130 : track_(AudioTrack::Create(kTestAudioLabel, NULL)), | 121 : track_(AudioTrack::Create(kTestAudioLabel, NULL)), |
131 observer_(new rtc::RefCountedObject<FakeDtmfObserver>()), | 122 observer_(new rtc::RefCountedObject<FakeDtmfObserver>()), |
132 provider_(new FakeDtmfProvider()) { | 123 provider_(new FakeDtmfProvider()) { |
133 provider_->AddCanInsertDtmfTrack(kTestAudioLabel); | 124 provider_->SetCanInsertDtmf(true); |
134 dtmf_ = DtmfSender::Create(track_, rtc::Thread::Current(), | 125 dtmf_ = DtmfSender::Create(track_, rtc::Thread::Current(), |
135 provider_.get()); | 126 provider_.get()); |
136 dtmf_->RegisterObserver(observer_.get()); | 127 dtmf_->RegisterObserver(observer_.get()); |
137 } | 128 } |
138 | 129 |
139 ~DtmfSenderTest() { | 130 ~DtmfSenderTest() { |
140 if (dtmf_.get()) { | 131 if (dtmf_.get()) { |
141 dtmf_->UnregisterObserver(); | 132 dtmf_->UnregisterObserver(); |
142 } | 133 } |
143 } | 134 } |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
220 | 211 |
221 rtc::scoped_refptr<AudioTrackInterface> track_; | 212 rtc::scoped_refptr<AudioTrackInterface> track_; |
222 std::unique_ptr<FakeDtmfObserver> observer_; | 213 std::unique_ptr<FakeDtmfObserver> observer_; |
223 std::unique_ptr<FakeDtmfProvider> provider_; | 214 std::unique_ptr<FakeDtmfProvider> provider_; |
224 rtc::scoped_refptr<DtmfSender> dtmf_; | 215 rtc::scoped_refptr<DtmfSender> dtmf_; |
225 rtc::ScopedFakeClock fake_clock_; | 216 rtc::ScopedFakeClock fake_clock_; |
226 }; | 217 }; |
227 | 218 |
228 TEST_F(DtmfSenderTest, CanInsertDtmf) { | 219 TEST_F(DtmfSenderTest, CanInsertDtmf) { |
229 EXPECT_TRUE(dtmf_->CanInsertDtmf()); | 220 EXPECT_TRUE(dtmf_->CanInsertDtmf()); |
230 provider_->RemoveCanInsertDtmfTrack(kTestAudioLabel); | 221 provider_->SetCanInsertDtmf(false); |
231 EXPECT_FALSE(dtmf_->CanInsertDtmf()); | 222 EXPECT_FALSE(dtmf_->CanInsertDtmf()); |
232 } | 223 } |
233 | 224 |
234 TEST_F(DtmfSenderTest, InsertDtmf) { | 225 TEST_F(DtmfSenderTest, InsertDtmf) { |
235 std::string tones = "@1%a&*$"; | 226 std::string tones = "@1%a&*$"; |
236 int duration = 100; | 227 int duration = 100; |
237 int inter_tone_gap = 50; | 228 int inter_tone_gap = 50; |
238 EXPECT_TRUE(dtmf_->InsertDtmf(tones, duration, inter_tone_gap)); | 229 EXPECT_TRUE(dtmf_->InsertDtmf(tones, duration, inter_tone_gap)); |
239 EXPECT_TRUE_SIMULATED_WAIT(observer_->completed(), kMaxWaitMs, fake_clock_); | 230 EXPECT_TRUE_SIMULATED_WAIT(observer_->completed(), kMaxWaitMs, fake_clock_); |
240 | 231 |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
326 EXPECT_TRUE_SIMULATED_WAIT(observer_->completed(), kMaxWaitMs, fake_clock_); | 317 EXPECT_TRUE_SIMULATED_WAIT(observer_->completed(), kMaxWaitMs, fake_clock_); |
327 | 318 |
328 VerifyOnProvider(tones, duration, inter_tone_gap); | 319 VerifyOnProvider(tones, duration, inter_tone_gap); |
329 VerifyOnObserver(tones); | 320 VerifyOnObserver(tones); |
330 } | 321 } |
331 | 322 |
332 TEST_F(DtmfSenderTest, TryInsertDtmfWhenItDoesNotWork) { | 323 TEST_F(DtmfSenderTest, TryInsertDtmfWhenItDoesNotWork) { |
333 std::string tones = "3,4"; | 324 std::string tones = "3,4"; |
334 int duration = 100; | 325 int duration = 100; |
335 int inter_tone_gap = 50; | 326 int inter_tone_gap = 50; |
336 provider_->RemoveCanInsertDtmfTrack(kTestAudioLabel); | 327 provider_->SetCanInsertDtmf(false); |
337 EXPECT_FALSE(dtmf_->InsertDtmf(tones, duration, inter_tone_gap)); | 328 EXPECT_FALSE(dtmf_->InsertDtmf(tones, duration, inter_tone_gap)); |
338 } | 329 } |
339 | 330 |
340 TEST_F(DtmfSenderTest, InsertDtmfWithInvalidDurationOrGap) { | 331 TEST_F(DtmfSenderTest, InsertDtmfWithInvalidDurationOrGap) { |
341 std::string tones = "3,4"; | 332 std::string tones = "3,4"; |
342 int duration = 100; | 333 int duration = 100; |
343 int inter_tone_gap = 50; | 334 int inter_tone_gap = 50; |
344 | 335 |
345 EXPECT_FALSE(dtmf_->InsertDtmf(tones, 6001, inter_tone_gap)); | 336 EXPECT_FALSE(dtmf_->InsertDtmf(tones, 6001, inter_tone_gap)); |
346 EXPECT_FALSE(dtmf_->InsertDtmf(tones, 69, inter_tone_gap)); | 337 EXPECT_FALSE(dtmf_->InsertDtmf(tones, 69, inter_tone_gap)); |
347 EXPECT_FALSE(dtmf_->InsertDtmf(tones, duration, 49)); | 338 EXPECT_FALSE(dtmf_->InsertDtmf(tones, duration, 49)); |
348 | 339 |
349 EXPECT_TRUE(dtmf_->InsertDtmf(tones, duration, inter_tone_gap)); | 340 EXPECT_TRUE(dtmf_->InsertDtmf(tones, duration, inter_tone_gap)); |
350 } | 341 } |
OLD | NEW |