• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2014 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_PEPPER_PEPPER_MEDIA_STREAM_AUDIO_TRACK_HOST_H_
6 #define CONTENT_RENDERER_PEPPER_PEPPER_MEDIA_STREAM_AUDIO_TRACK_HOST_H_
7 
8 #include <deque>
9 
10 #include "base/compiler_specific.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/memory/weak_ptr.h"
13 #include "base/synchronization/lock.h"
14 #include "base/threading/thread_checker.h"
15 #include "content/public/renderer/media_stream_audio_sink.h"
16 #include "content/renderer/pepper/pepper_media_stream_track_host_base.h"
17 #include "media/audio/audio_parameters.h"
18 #include "ppapi/shared_impl/media_stream_audio_track_shared.h"
19 #include "third_party/WebKit/public/platform/WebMediaStreamTrack.h"
20 
21 namespace base {
22 class MessageLoopProxy;
23 }  // namespace base
24 
25 namespace content {
26 
27 class PepperMediaStreamAudioTrackHost : public PepperMediaStreamTrackHostBase {
28  public:
29   PepperMediaStreamAudioTrackHost(RendererPpapiHost* host,
30                                   PP_Instance instance,
31                                   PP_Resource resource,
32                                   const blink::WebMediaStreamTrack& track);
33 
34  private:
35   // A helper class for receiving audio samples in the audio thread.
36   // This class is created and destroyed on the renderer main thread.
37   class AudioSink : public MediaStreamAudioSink {
38    public:
39     explicit AudioSink(PepperMediaStreamAudioTrackHost* host);
40     virtual ~AudioSink();
41 
42     // Enqueues a free buffer index into |buffers_| which will be used for
43     // sending audio samples to plugin.
44     // This function is called on the main thread.
45     void EnqueueBuffer(int32_t index);
46 
47     // This function is called on the main thread.
48     void Configure(int32_t number_of_buffers);
49 
50    private:
51     // Initializes buffers on the main thread.
52     void SetFormatOnMainThread(int bytes_per_second);
53 
54     void InitBuffers();
55 
56     // Send enqueue buffer message on the main thread.
57     void SendEnqueueBufferMessageOnMainThread(int32_t index);
58 
59     // MediaStreamAudioSink overrides:
60     // These two functions should be called on the audio thread.
61     virtual void OnData(const int16* audio_data,
62                         int sample_rate,
63                         int number_of_channels,
64                         int number_of_frames) OVERRIDE;
65     virtual void OnSetFormat(const media::AudioParameters& params) OVERRIDE;
66 
67     // Unowned host which is available during the AudioSink's lifespan.
68     // It is mainly used in the main thread. But the audio thread will use
69     // host_->buffer_manager() to read some buffer properties. It is safe
70     // because the buffer_manager()'s properties will not be changed after
71     // initialization.
72     PepperMediaStreamAudioTrackHost* host_;
73 
74     // Timestamp of the next received audio buffer.
75     // Access only on the audio thread.
76     base::TimeDelta timestamp_;
77 
78     // Duration of one audio buffer.
79     // Access only on the audio thread.
80     base::TimeDelta buffer_duration_;
81 
82     // The current audio parameters.
83     // Access only on the audio thread.
84     media::AudioParameters audio_params_;
85 
86     // The original audio parameters which is set in the first time of
87     // OnSetFormat being called.
88     // Access only on the audio thread.
89     media::AudioParameters original_audio_params_;
90 
91     // The audio data size of one audio buffer in bytes.
92     // Access only on the audio thread.
93     uint32_t buffer_data_size_;
94 
95     // A lock to protect the index queue |buffers_|.
96     base::Lock lock_;
97 
98     // A queue for free buffer indices.
99     std::deque<int32_t> buffers_;
100 
101     scoped_refptr<base::MessageLoopProxy> main_message_loop_proxy_;
102 
103     base::ThreadChecker audio_thread_checker_;
104 
105     base::WeakPtrFactory<AudioSink> weak_factory_;
106 
107     // Number of buffers.
108     int32_t number_of_buffers_;
109 
110     // Number of bytes per second.
111     int bytes_per_second_;
112 
113     DISALLOW_COPY_AND_ASSIGN(AudioSink);
114   };
115 
116   virtual ~PepperMediaStreamAudioTrackHost();
117 
118   // ResourceMessageHandler overrides:
119   virtual int32_t OnResourceMessageReceived(
120       const IPC::Message& msg,
121       ppapi::host::HostMessageContext* context) OVERRIDE;
122 
123   // Message handlers:
124   int32_t OnHostMsgConfigure(
125       ppapi::host::HostMessageContext* context,
126       const ppapi::MediaStreamAudioTrackShared::Attributes& attributes);
127 
128   // PepperMediaStreamTrackHostBase overrides:
129   virtual void OnClose() OVERRIDE;
130 
131   // MediaStreamBufferManager::Delegate overrides:
132   virtual void OnNewBufferEnqueued() OVERRIDE;
133 
134   // ResourceHost overrides:
135   virtual void DidConnectPendingHostToResource() OVERRIDE;
136 
137   blink::WebMediaStreamTrack track_;
138 
139   // True if |audio_sink_| has been added to |blink::WebMediaStreamTrack|
140   // as a sink.
141   bool connected_;
142 
143   AudioSink audio_sink_;
144 
145   DISALLOW_COPY_AND_ASSIGN(PepperMediaStreamAudioTrackHost);
146 };
147 
148 }  // namespace content
149 
150 #endif  // CONTENT_RENDERER_PEPPER_PEPPER_MEDIA_STREAM_AUDIO_TRACK_HOST_H_
151