1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "content/renderer/media/rtc_dtmf_sender_handler.h"
10 #include "base/location.h"
11 #include "base/logging.h"
12 #include "base/single_thread_task_runner.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "base/thread_task_runner_handle.h"
15 #include "base/threading/thread_checker.h"
17 using webrtc::DtmfSenderInterface
;
21 class RtcDtmfSenderHandler::Observer
:
22 public base::RefCountedThreadSafe
<Observer
>,
23 public webrtc::DtmfSenderObserverInterface
{
25 explicit Observer(const base::WeakPtr
<RtcDtmfSenderHandler
>& handler
)
26 : main_thread_(base::ThreadTaskRunnerHandle::Get()), handler_(handler
) {}
29 friend class base::RefCountedThreadSafe
<Observer
>;
31 ~Observer() override
{}
33 void OnToneChange(const std::string
& tone
) override
{
34 main_thread_
->PostTask(FROM_HERE
,
35 base::Bind(&RtcDtmfSenderHandler::Observer::OnToneChangeOnMainThread
,
39 void OnToneChangeOnMainThread(const std::string
& tone
) {
40 DCHECK(thread_checker_
.CalledOnValidThread());
42 handler_
->OnToneChange(tone
);
45 base::ThreadChecker thread_checker_
;
46 const scoped_refptr
<base::SingleThreadTaskRunner
> main_thread_
;
47 base::WeakPtr
<RtcDtmfSenderHandler
> handler_
;
50 RtcDtmfSenderHandler::RtcDtmfSenderHandler(DtmfSenderInterface
* dtmf_sender
)
51 : dtmf_sender_(dtmf_sender
),
55 observer_
= new Observer(weak_factory_
.GetWeakPtr());
56 dtmf_sender_
->RegisterObserver(observer_
.get());
59 RtcDtmfSenderHandler::~RtcDtmfSenderHandler() {
61 dtmf_sender_
->UnregisterObserver();
62 // Release |observer| before |weak_factory_| is destroyed.
66 void RtcDtmfSenderHandler::setClient(
67 blink::WebRTCDTMFSenderHandlerClient
* client
) {
68 webkit_client_
= client
;
71 blink::WebString
RtcDtmfSenderHandler::currentToneBuffer() {
72 return base::UTF8ToUTF16(dtmf_sender_
->tones());
75 bool RtcDtmfSenderHandler::canInsertDTMF() {
76 return dtmf_sender_
->CanInsertDtmf();
79 bool RtcDtmfSenderHandler::insertDTMF(const blink::WebString
& tones
,
82 std::string utf8_tones
= base::UTF16ToUTF8(base::StringPiece16(tones
));
83 return dtmf_sender_
->InsertDtmf(utf8_tones
, static_cast<int>(duration
),
84 static_cast<int>(interToneGap
));
87 void RtcDtmfSenderHandler::OnToneChange(const std::string
& tone
) {
88 if (!webkit_client_
) {
89 LOG(ERROR
) << "WebRTCDTMFSenderHandlerClient not set.";
92 webkit_client_
->didPlayTone(base::UTF8ToUTF16(tone
));
95 } // namespace content