10e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org/*
20e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * libjingle
30e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * Copyright 2012, Google Inc.
40e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *
50e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * Redistribution and use in source and binary forms, with or without
60e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * modification, are permitted provided that the following conditions are met:
70e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *
80e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *  1. Redistributions of source code must retain the above copyright notice,
90e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *     this list of conditions and the following disclaimer.
100e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *  2. Redistributions in binary form must reproduce the above copyright notice,
110e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *     this list of conditions and the following disclaimer in the documentation
120e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *     and/or other materials provided with the distribution.
130e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *  3. The name of the author may not be used to endorse or promote products
140e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *     derived from this software without specific prior written permission.
150e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *
160e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
170e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
180e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
190e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
200e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
210e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
220e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
230e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
240e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
250e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
260e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org */
270e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
280e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#ifndef TALK_APP_WEBRTC_DTMFSENDER_H_
290e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#define TALK_APP_WEBRTC_DTMFSENDER_H_
300e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
310e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#include <string>
320e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
330e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#include "talk/app/webrtc/dtmfsenderinterface.h"
340e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#include "talk/app/webrtc/mediastreaminterface.h"
350e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#include "talk/app/webrtc/proxy.h"
362a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org#include "webrtc/base/common.h"
372a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org#include "webrtc/base/messagehandler.h"
382a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org#include "webrtc/base/refcount.h"
390e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
400e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// DtmfSender is the native implementation of the RTCDTMFSender defined by
410e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// the WebRTC W3C Editor's Draft.
420e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// http://dev.w3.org/2011/webrtc/editor/webrtc.html
430e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
442a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.orgnamespace rtc {
450e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgclass Thread;
460e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org}
470e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
480e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgnamespace webrtc {
490e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
500e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// This interface is called by DtmfSender to talk to the actual audio channel
510e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// to send DTMF.
520e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgclass DtmfProviderInterface {
530e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org public:
540e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Returns true if the audio track with given id (|track_id|) is capable
550e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // of sending DTMF. Otherwise returns false.
560e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual bool CanInsertDtmf(const std::string& track_id) = 0;
570e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Sends DTMF |code| via the audio track with given id (|track_id|).
580e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // The |duration| indicates the length of the DTMF tone in ms.
590e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Returns true on success and false on failure.
600e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual bool InsertDtmf(const std::string& track_id,
610e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                          int code, int duration) = 0;
620e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Returns a |sigslot::signal0<>| signal. The signal should fire before
630e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // the provider is destroyed.
640e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual sigslot::signal0<>* GetOnDestroyedSignal() = 0;
650e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
660e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org protected:
670e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual ~DtmfProviderInterface() {}
680e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org};
690e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
700e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgclass DtmfSender
710e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    : public DtmfSenderInterface,
720e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      public sigslot::has_slots<>,
732a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org      public rtc::MessageHandler {
740e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org public:
752a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  static rtc::scoped_refptr<DtmfSender> Create(
760e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      AudioTrackInterface* track,
772a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org      rtc::Thread* signaling_thread,
780e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      DtmfProviderInterface* provider);
790e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
800e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Implements DtmfSenderInterface.
810e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual void RegisterObserver(DtmfSenderObserverInterface* observer) OVERRIDE;
820e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual void UnregisterObserver() OVERRIDE;
830e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual bool CanInsertDtmf() OVERRIDE;
840e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual bool InsertDtmf(const std::string& tones, int duration,
850e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                          int inter_tone_gap) OVERRIDE;
860e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual const AudioTrackInterface* track() const OVERRIDE;
870e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual std::string tones() const OVERRIDE;
880e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual int duration() const OVERRIDE;
890e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual int inter_tone_gap() const OVERRIDE;
900e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
910e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org protected:
920e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  DtmfSender(AudioTrackInterface* track,
932a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org             rtc::Thread* signaling_thread,
940e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org             DtmfProviderInterface* provider);
950e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual ~DtmfSender();
960e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
970e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org private:
980e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  DtmfSender();
990e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1000e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Implements MessageHandler.
1012a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  virtual void OnMessage(rtc::Message* msg);
1020e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1030e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // The DTMF sending task.
1040e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  void DoInsertDtmf();
1050e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1060e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  void OnProviderDestroyed();
1070e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1080e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  void StopSending();
1090e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1102a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  rtc::scoped_refptr<AudioTrackInterface> track_;
1110e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  DtmfSenderObserverInterface* observer_;
1122a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  rtc::Thread* signaling_thread_;
1130e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  DtmfProviderInterface* provider_;
1140e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  std::string tones_;
1150e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  int duration_;
1160e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  int inter_tone_gap_;
1170e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1180e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  DISALLOW_COPY_AND_ASSIGN(DtmfSender);
1190e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org};
1200e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1210e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// Define proxy for DtmfSenderInterface.
1220e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgBEGIN_PROXY_MAP(DtmfSender)
1230e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  PROXY_METHOD1(void, RegisterObserver, DtmfSenderObserverInterface*)
1240e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  PROXY_METHOD0(void, UnregisterObserver)
1250e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  PROXY_METHOD0(bool, CanInsertDtmf)
1260e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  PROXY_METHOD3(bool, InsertDtmf, const std::string&, int, int)
1270e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  PROXY_CONSTMETHOD0(const AudioTrackInterface*, track)
1280e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  PROXY_CONSTMETHOD0(std::string, tones)
1290e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  PROXY_CONSTMETHOD0(int, duration)
1300e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  PROXY_CONSTMETHOD0(int, inter_tone_gap)
1310e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgEND_PROXY()
1320e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1330e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// Get DTMF code from the DTMF event character.
1340e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgbool GetDtmfCode(char tone, int* code);
1350e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1360e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org}  // namespace webrtc
1370e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1380e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#endif  // TALK_APP_WEBRTC_DTMFSENDER_H_
139