Home | History | Annotate | Download | only in media
      1 // Copyright (c) 2012 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_RENDERER_H_
      6 #define CONTENT_RENDERER_MEDIA_WEBRTC_LOCAL_AUDIO_RENDERER_H_
      7 
      8 #include <vector>
      9 
     10 #include "base/callback.h"
     11 #include "base/memory/ref_counted.h"
     12 #include "base/synchronization/lock.h"
     13 #include "base/threading/thread_checker.h"
     14 #include "content/common/content_export.h"
     15 #include "content/renderer/media/media_stream_audio_renderer.h"
     16 #include "content/renderer/media/webrtc_audio_device_impl.h"
     17 #include "content/renderer/media/webrtc_local_audio_track.h"
     18 
     19 namespace media {
     20 class AudioBus;
     21 class AudioFifo;
     22 class AudioOutputDevice;
     23 class AudioParameters;
     24 }
     25 
     26 namespace content {
     27 
     28 class WebRtcAudioCapturer;
     29 
     30 // WebRtcLocalAudioRenderer is a MediaStreamAudioRenderer designed for rendering
     31 // local audio media stream tracks,
     32 // http://dev.w3.org/2011/webrtc/editor/getusermedia.html#mediastreamtrack
     33 // It also implements media::AudioRendererSink::RenderCallback to render audio
     34 // data provided from a WebRtcLocalAudioTrack source.
     35 // When the audio layer in the browser process asks for data to render, this
     36 // class provides the data by implementing the WebRtcAudioCapturerSink
     37 // interface, i.e., we are a sink seen from the WebRtcAudioCapturer perspective.
     38 // TODO(henrika): improve by using similar principles as in RTCVideoRenderer
     39 // which register itself to the video track when the provider is started and
     40 // deregisters itself when it is stopped.
     41 // Tracking this at http://crbug.com/164813.
     42 class CONTENT_EXPORT WebRtcLocalAudioRenderer
     43     : NON_EXPORTED_BASE(public MediaStreamAudioRenderer),
     44       NON_EXPORTED_BASE(public media::AudioRendererSink::RenderCallback),
     45       NON_EXPORTED_BASE(public WebRtcAudioCapturerSink) {
     46  public:
     47   // Creates a local renderer and registers a capturing |source| object.
     48   // The |source| is owned by the WebRtcAudioDeviceImpl.
     49   // Called on the main thread.
     50   WebRtcLocalAudioRenderer(WebRtcLocalAudioTrack* audio_track,
     51                            int source_render_view_id);
     52 
     53   // MediaStreamAudioRenderer implementation.
     54   // Called on the main thread.
     55   virtual void Start() OVERRIDE;
     56   virtual void Stop() OVERRIDE;
     57   virtual void Play() OVERRIDE;
     58   virtual void Pause() OVERRIDE;
     59   virtual void SetVolume(float volume) OVERRIDE;
     60   virtual base::TimeDelta GetCurrentRenderTime() const OVERRIDE;
     61   virtual bool IsLocalRenderer() const OVERRIDE;
     62 
     63   const base::TimeDelta& total_render_time() const {
     64     return total_render_time_;
     65   }
     66 
     67  protected:
     68   virtual ~WebRtcLocalAudioRenderer();
     69 
     70  private:
     71   // WebRtcAudioCapturerSink implementation.
     72 
     73   // Called on the AudioInputDevice worker thread.
     74   virtual int CaptureData(const std::vector<int>& channels,
     75                           const int16* audio_data,
     76                           int sample_rate,
     77                           int number_of_channels,
     78                           int number_of_frames,
     79                           int audio_delay_milliseconds,
     80                           int current_volume,
     81                           bool need_audio_processing) OVERRIDE;
     82 
     83   // Can be called on different user thread.
     84   virtual void SetCaptureFormat(const media::AudioParameters& params) OVERRIDE;
     85 
     86   // media::AudioRendererSink::RenderCallback implementation.
     87   // Render() is called on the AudioOutputDevice thread and OnRenderError()
     88   // on the IO thread.
     89   virtual int Render(media::AudioBus* audio_bus,
     90                      int audio_delay_milliseconds) OVERRIDE;
     91   virtual void OnRenderError() OVERRIDE;
     92 
     93   // The audio track which provides data to render. Given that this class
     94   // implements local loopback, the audio track is getting data from a capture
     95   // instance like a selected microphone and forwards the recorded data to its
     96   // sinks. The recorded data is stored in a FIFO and consumed
     97   // by this class when the sink asks for new data.
     98   // The WebRtcAudioCapturer is today created by WebRtcAudioDeviceImpl.
     99   scoped_refptr<WebRtcLocalAudioTrack> audio_track_;
    100 
    101   // The render view in which the audio is rendered into |sink_|.
    102   const int source_render_view_id_;
    103 
    104   // The sink (destination) for rendered audio.
    105   scoped_refptr<media::AudioOutputDevice> sink_;
    106 
    107   // Used to DCHECK that we are called on the correct thread.
    108   base::ThreadChecker thread_checker_;
    109 
    110   // Contains copies of captured audio frames.
    111   scoped_ptr<media::AudioFifo> loopback_fifo_;
    112 
    113   // Stores last time a render callback was received. The time difference
    114   // between a new time stamp and this value can be used to derive the
    115   // total render time.
    116   base::Time last_render_time_;
    117 
    118   // Keeps track of total time audio has been rendered.
    119   base::TimeDelta total_render_time_;
    120 
    121   // The audio parameters used by the renderer.
    122   media::AudioParameters audio_params_;
    123 
    124   // Set when playing, cleared when paused.
    125   bool playing_;
    126 
    127   // Protects |loopback_fifo_|, |playing_| and |sink_|.
    128   mutable base::Lock thread_lock_;
    129 
    130   DISALLOW_COPY_AND_ASSIGN(WebRtcLocalAudioRenderer);
    131 };
    132 
    133 }  // namespace content
    134 
    135 #endif  // CONTENT_RENDERER_MEDIA_WEBRTC_LOCAL_AUDIO_RENDERER_H_
    136