webrtc_local_audio_track.h revision 58537e28ecd584eab876aee8be7156509866d23a
1// Copyright 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.
4
5#ifndef CONTENT_RENDERER_MEDIA_WEBRTC_LOCAL_AUDIO_TRACK_H_
6#define CONTENT_RENDERER_MEDIA_WEBRTC_LOCAL_AUDIO_TRACK_H_
7
8#include <list>
9#include <string>
10
11#include "base/synchronization/lock.h"
12#include "base/threading/thread_checker.h"
13#include "content/renderer/media/webrtc_audio_device_impl.h"
14#include "third_party/libjingle/source/talk/app/webrtc/mediaconstraintsinterface.h"
15#include "third_party/libjingle/source/talk/app/webrtc/mediastreaminterface.h"
16#include "third_party/libjingle/source/talk/app/webrtc/mediastreamtrack.h"
17#include "third_party/libjingle/source/talk/media/base/audiorenderer.h"
18
19namespace cricket {
20class AudioRenderer;
21}
22
23namespace content {
24
25class WebRtcAudioCapturer;
26class WebRtcAudioCapturerSinkOwner;
27
28// A WebRtcLocalAudioTrack instance contains the implementations of
29// MediaStreamTrack and WebRtcAudioCapturerSink.
30// When an instance is created, it will register itself as a track to the
31// WebRtcAudioCapturer to get the captured data, and forward the data to
32// its |sinks_|. The data flow can be stopped by disabling the audio track.
33class CONTENT_EXPORT WebRtcLocalAudioTrack
34    : NON_EXPORTED_BASE(public cricket::AudioRenderer),
35      NON_EXPORTED_BASE(
36          public webrtc::MediaStreamTrack<webrtc::AudioTrackInterface>) {
37 public:
38  static scoped_refptr<WebRtcLocalAudioTrack> Create(
39      const std::string& id,
40      const scoped_refptr<WebRtcAudioCapturer>& capturer,
41      webrtc::AudioSourceInterface* stream_source,
42      const webrtc::MediaConstraintsInterface* constraints);
43
44  // Add a sink to the track. This function will trigger a SetCaptureFormat()
45  // call on the |sink|.
46  // Called on the main render thread.
47  void AddSink(WebRtcAudioCapturerSink* sink);
48
49  // Remove a sink from the track.
50  // Called on the main render thread.
51  void RemoveSink(WebRtcAudioCapturerSink* sink);
52
53  // Starts the local audio track. Called on the main render thread and
54  // should be called only once when audio track is created.
55  void Start();
56
57  // Stops the local audio track. Called on the main render thread and
58  // should be called only once when audio track going away.
59  void Stop();
60
61  // Method called by the capturer to deliever the capture data.
62  void CaptureData(const int16* audio_data,
63                   int number_of_channels,
64                   int number_of_frames,
65                   int audio_delay_milliseconds,
66                   int volume,
67                   bool key_pressed);
68
69  // Method called by the capturer to set the audio parameters used by source
70  // of the capture data..
71  // Can be called on different user threads.
72  void SetCaptureFormat(const media::AudioParameters& params);
73
74 protected:
75  WebRtcLocalAudioTrack(const std::string& label,
76                        const scoped_refptr<WebRtcAudioCapturer>& capturer,
77                        webrtc::AudioSourceInterface* track_source,
78                        const webrtc::MediaConstraintsInterface* constraints);
79  virtual ~WebRtcLocalAudioTrack();
80
81 private:
82  typedef std::list<scoped_refptr<WebRtcAudioCapturerSinkOwner> > SinkList;
83
84  // cricket::AudioCapturer implementation.
85  virtual void AddChannel(int channel_id) OVERRIDE;
86  virtual void RemoveChannel(int channel_id) OVERRIDE;
87
88  // webrtc::AudioTrackInterface implementation.
89  virtual webrtc::AudioSourceInterface* GetSource() const OVERRIDE;
90  virtual cricket::AudioRenderer* GetRenderer() OVERRIDE;
91
92  // webrtc::MediaStreamTrack implementation.
93  virtual std::string kind() const OVERRIDE;
94
95  // The provider of captured data to render.
96  // The WebRtcAudioCapturer is today created by WebRtcAudioDeviceImpl.
97  scoped_refptr<WebRtcAudioCapturer> capturer_;
98
99  // The source of the audio track which handles the audio constraints.
100  // TODO(xians): merge |track_source_| to |capturer_|.
101  talk_base::scoped_refptr<webrtc::AudioSourceInterface> track_source_;
102
103  // A list of sinks that the audio data is fed to.
104  SinkList sinks_;
105
106  // Used to DCHECK that we are called on the correct thread.
107  base::ThreadChecker thread_checker_;
108
109  // Cached values of the audio parameters used by the |source_| and |sinks_|.
110  media::AudioParameters params_;
111
112  // Protects |params_| and |sinks_|.
113  mutable base::Lock lock_;
114
115  // A vector of WebRtc VoE channels that the capturer sends datat to.
116  std::vector<int> voe_channels_;
117
118  bool need_audio_processing_;
119
120  DISALLOW_COPY_AND_ASSIGN(WebRtcLocalAudioTrack);
121};
122
123}  // namespace content
124
125#endif  // CONTENT_RENDERER_MEDIA_WEBRTC_LOCAL_AUDIO_TRACK_H_
126