OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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 #include <stdlib.h> |
| 6 #include <string.h> |
| 7 |
| 8 #include <map> |
| 9 #include <string> |
| 10 #include <vector> |
| 11 |
| 12 #include "ppapi/cpp/audio_buffer.h" |
| 13 #include "ppapi/cpp/audio_encoder.h" |
| 14 #include "ppapi/cpp/instance.h" |
| 15 #include "ppapi/cpp/logging.h" |
| 16 #include "ppapi/cpp/media_stream_audio_track.h" |
| 17 #include "ppapi/cpp/module.h" |
| 18 #include "ppapi/cpp/var_array_buffer.h" |
| 19 #include "ppapi/cpp/var_dictionary.h" |
| 20 #include "ppapi/utility/completion_callback_factory.h" |
| 21 |
| 22 // When compiling natively on Windows, PostMessage can be #define-d to |
| 23 // something else. |
| 24 #ifdef PostMessage |
| 25 #undef PostMessage |
| 26 #endif |
| 27 |
| 28 // This example demonstrates receiving audio samples from an audio |
| 29 // track and encoding them. |
| 30 |
| 31 namespace { |
| 32 |
| 33 class AudioEncoderInstance : public pp::Instance { |
| 34 public: |
| 35 explicit AudioEncoderInstance(PP_Instance instance); |
| 36 virtual ~AudioEncoderInstance(); |
| 37 |
| 38 private: |
| 39 // pp::Instance implementation. |
| 40 virtual void HandleMessage(const pp::Var& var_message); |
| 41 |
| 42 std::string ProfileToString(PP_AudioProfile profile); |
| 43 |
| 44 void GetSupportedProfiles(); |
| 45 void OnGetSupportedProfiles( |
| 46 int32_t result, |
| 47 const std::vector<PP_AudioProfileDescription> profiles); |
| 48 |
| 49 void StartAudioTrack(const pp::Resource& resource_track); |
| 50 void StopAudioTrack(); |
| 51 void OnGetBuffer(int32_t result, pp::AudioBuffer buffer); |
| 52 |
| 53 void InitializeEncoder(); |
| 54 void OnEncoderInitialized(int32_t result); |
| 55 void OnAudioTrackConfigured(int32_t result); |
| 56 void GetEncoderBuffer(const pp::AudioBuffer& track_buffer); |
| 57 void OnGetEncoderBuffer(int32_t result, |
| 58 pp::AudioBuffer buffer, |
| 59 pp::AudioBuffer track_buffer); |
| 60 void OnEncodeDone(int32_t result); |
| 61 void OnGetBitstreamBuffer(int32_t result, |
| 62 const PP_AudioBitstreamBuffer& buffer); |
| 63 |
| 64 pp::VarDictionary NewCommand(const std::string& type); |
| 65 |
| 66 void Log(const std::string& message); |
| 67 void LogError(int32_t error, const std::string& message); |
| 68 |
| 69 void PostAudioFormat(); |
| 70 void PostDataMessage(const void* data, uint32_t size); |
| 71 |
| 72 pp::CompletionCallbackFactory<AudioEncoderInstance> callback_factory_; |
| 73 |
| 74 // Set when the plugin is not performing any encoding and just passing the |
| 75 // uncompressed data directly to web page. |
| 76 bool no_encoding_; |
| 77 |
| 78 uint32_t channels_; |
| 79 uint32_t sample_rate_; |
| 80 uint32_t sample_size_; |
| 81 uint32_t samples_per_frame_; |
| 82 |
| 83 pp::MediaStreamAudioTrack audio_track_; |
| 84 pp::AudioEncoder audio_encoder_; |
| 85 }; |
| 86 |
| 87 AudioEncoderInstance::AudioEncoderInstance(PP_Instance instance) |
| 88 : pp::Instance(instance), |
| 89 callback_factory_(this), |
| 90 no_encoding_(true), |
| 91 channels_(0), |
| 92 sample_rate_(0), |
| 93 sample_size_(0), |
| 94 samples_per_frame_(0), |
| 95 audio_encoder_(this) { |
| 96 GetSupportedProfiles(); |
| 97 } |
| 98 |
| 99 AudioEncoderInstance::~AudioEncoderInstance() {} |
| 100 |
| 101 void AudioEncoderInstance::HandleMessage(const pp::Var& var_message) { |
| 102 if (!var_message.is_dictionary()) { |
| 103 LogError(PP_ERROR_FAILED, |
| 104 "Cannot handle incoming message, not a dictionary"); |
| 105 return; |
| 106 } |
| 107 |
| 108 pp::VarDictionary dict_message(var_message); |
| 109 std::string command = dict_message.Get("command").AsString(); |
| 110 |
| 111 if (command == "start") { |
| 112 pp::Var var_track = dict_message.Get("track"); |
| 113 if (!var_track.is_resource()) { |
| 114 LogError(PP_ERROR_FAILED, "Given track is not a resource"); |
| 115 return; |
| 116 } |
| 117 std::string profile = dict_message.Get("profile").AsString(); |
| 118 if (profile == "wav") { |
| 119 no_encoding_ = true; |
| 120 } else { |
| 121 no_encoding_ = false; |
| 122 } |
| 123 StartAudioTrack(var_track.AsResource()); |
| 124 } else if (command == "stop") { |
| 125 StopAudioTrack(); |
| 126 } else { |
| 127 LogError(PP_ERROR_FAILED, "Invalid command"); |
| 128 } |
| 129 } |
| 130 |
| 131 std::string AudioEncoderInstance::ProfileToString(PP_AudioProfile profile) { |
| 132 if (profile == PP_AUDIOPROFILE_OPUS) |
| 133 return "opus"; |
| 134 return "unknown"; |
| 135 } |
| 136 |
| 137 void AudioEncoderInstance::GetSupportedProfiles() { |
| 138 audio_encoder_.GetSupportedProfiles(callback_factory_.NewCallbackWithOutput( |
| 139 &AudioEncoderInstance::OnGetSupportedProfiles)); |
| 140 } |
| 141 |
| 142 void AudioEncoderInstance::OnGetSupportedProfiles( |
| 143 int32_t result, |
| 144 const std::vector<PP_AudioProfileDescription> profiles) { |
| 145 pp::VarDictionary dictionary = NewCommand("supportedProfiles"); |
| 146 pp::VarArray js_profiles; |
| 147 dictionary.Set(pp::Var("profiles"), js_profiles); |
| 148 |
| 149 if (result < 0) { |
| 150 LogError(result, "Cannot get supported profiles"); |
| 151 PostMessage(dictionary); |
| 152 } |
| 153 |
| 154 int32_t idx = 0; |
| 155 for (std::vector<PP_AudioProfileDescription>::const_iterator it = |
| 156 profiles.begin(); |
| 157 it != profiles.end(); ++it) { |
| 158 pp::VarDictionary js_profile; |
| 159 js_profile.Set(pp::Var("name"), pp::Var(ProfileToString(it->profile))); |
| 160 js_profile.Set(pp::Var("sample_size"), |
| 161 pp::Var(static_cast<int32_t>(it->sample_size))); |
| 162 js_profile.Set(pp::Var("sample_rate"), |
| 163 pp::Var(static_cast<int32_t>(it->sample_rate))); |
| 164 js_profiles.Set(idx++, js_profile); |
| 165 } |
| 166 PostMessage(dictionary); |
| 167 } |
| 168 |
| 169 void AudioEncoderInstance::StartAudioTrack(const pp::Resource& resource_track) { |
| 170 audio_track_ = pp::MediaStreamAudioTrack(resource_track); |
| 171 audio_track_.GetBuffer(callback_factory_.NewCallbackWithOutput( |
| 172 &AudioEncoderInstance::OnGetBuffer)); |
| 173 } |
| 174 |
| 175 void AudioEncoderInstance::StopAudioTrack() { |
| 176 channels_ = 0; |
| 177 audio_track_.Close(); |
| 178 audio_track_ = pp::MediaStreamAudioTrack(); |
| 179 audio_encoder_.Close(); |
| 180 audio_encoder_ = pp::AudioEncoder(); |
| 181 } |
| 182 |
| 183 void AudioEncoderInstance::OnGetBuffer(int32_t result, pp::AudioBuffer buffer) { |
| 184 if (result == PP_ERROR_ABORTED) |
| 185 return; |
| 186 if (result != PP_OK) { |
| 187 LogError(result, "Cannot get audio track buffer"); |
| 188 return; |
| 189 } |
| 190 |
| 191 // If this is the first buffer, then we need to initialize the encoder. |
| 192 if (channels_ == 0) { |
| 193 channels_ = buffer.GetNumberOfChannels(); |
| 194 sample_rate_ = buffer.GetSampleRate(); |
| 195 sample_size_ = buffer.GetSampleSize(); |
| 196 samples_per_frame_ = buffer.GetNumberOfSamples(); |
| 197 |
| 198 if (no_encoding_) { |
| 199 PostAudioFormat(); |
| 200 PostDataMessage(buffer.GetDataBuffer(), buffer.GetDataBufferSize()); |
| 201 audio_track_.GetBuffer(callback_factory_.NewCallbackWithOutput( |
| 202 &AudioEncoderInstance::OnGetBuffer)); |
| 203 } else { |
| 204 InitializeEncoder(); |
| 205 } |
| 206 |
| 207 // Given that once the encoder is initialized we might reconfigure the |
| 208 // media track, we discard the first buffer to keep this example a bit |
| 209 // simpler. |
| 210 audio_track_.RecycleBuffer(buffer); |
| 211 } else { |
| 212 if (no_encoding_) { |
| 213 PostDataMessage(buffer.GetDataBuffer(), buffer.GetDataBufferSize()); |
| 214 audio_track_.RecycleBuffer(buffer); |
| 215 } else { |
| 216 GetEncoderBuffer(buffer); |
| 217 } |
| 218 |
| 219 audio_track_.GetBuffer(callback_factory_.NewCallbackWithOutput( |
| 220 &AudioEncoderInstance::OnGetBuffer)); |
| 221 } |
| 222 } |
| 223 |
| 224 void AudioEncoderInstance::InitializeEncoder() { |
| 225 if (audio_encoder_.is_null()) |
| 226 audio_encoder_ = pp::AudioEncoder(this); |
| 227 audio_encoder_.Initialize( |
| 228 channels_, static_cast<PP_AudioBuffer_SampleRate>(sample_rate_), |
| 229 static_cast<PP_AudioBuffer_SampleSize>(sample_size_), |
| 230 PP_AUDIOPROFILE_OPUS, 100000, PP_HARDWAREACCELERATION_WITHFALLBACK, |
| 231 callback_factory_.NewCallback( |
| 232 &AudioEncoderInstance::OnEncoderInitialized)); |
| 233 } |
| 234 |
| 235 void AudioEncoderInstance::OnEncoderInitialized(int32_t result) { |
| 236 if (result == PP_ERROR_ABORTED) |
| 237 return; |
| 238 if (result != PP_OK) { |
| 239 LogError(result, "Cannot initialize encoder"); |
| 240 return; |
| 241 } |
| 242 |
| 243 int32_t attribs[] = { |
| 244 // Duration in milliseconds. |
| 245 PP_MEDIASTREAMAUDIOTRACK_ATTRIB_DURATION, |
| 246 1000 / (sample_rate_ / audio_encoder_.GetNumberOfSamples()), |
| 247 PP_MEDIASTREAMAUDIOTRACK_ATTRIB_NONE}; |
| 248 audio_track_.Configure(attribs, |
| 249 callback_factory_.NewCallback( |
| 250 &AudioEncoderInstance::OnAudioTrackConfigured)); |
| 251 |
| 252 samples_per_frame_ = audio_encoder_.GetNumberOfSamples(); |
| 253 PostAudioFormat(); |
| 254 } |
| 255 |
| 256 void AudioEncoderInstance::OnAudioTrackConfigured(int32_t result) { |
| 257 if (result == PP_ERROR_ABORTED) |
| 258 return; |
| 259 if (result != PP_OK) { |
| 260 LogError(result, "Cannot configure audio track buffer duration"); |
| 261 return; |
| 262 } |
| 263 |
| 264 audio_track_.GetBuffer(callback_factory_.NewCallbackWithOutput( |
| 265 &AudioEncoderInstance::OnGetBuffer)); |
| 266 audio_encoder_.GetBitstreamBuffer(callback_factory_.NewCallbackWithOutput( |
| 267 &AudioEncoderInstance::OnGetBitstreamBuffer)); |
| 268 } |
| 269 |
| 270 void AudioEncoderInstance::GetEncoderBuffer( |
| 271 const pp::AudioBuffer& track_buffer) { |
| 272 audio_encoder_.GetBuffer(callback_factory_.NewCallbackWithOutput( |
| 273 &AudioEncoderInstance::OnGetEncoderBuffer, track_buffer)); |
| 274 } |
| 275 |
| 276 void AudioEncoderInstance::OnGetEncoderBuffer(int32_t result, |
| 277 pp::AudioBuffer buffer, |
| 278 pp::AudioBuffer track_buffer) { |
| 279 const char* error(nullptr); |
| 280 |
| 281 if (result != PP_OK) |
| 282 error = "Cannot get encoder buffer"; |
| 283 if (buffer.GetDataBufferSize() != track_buffer.GetDataBufferSize()) { |
| 284 result = PP_ERROR_FAILED; |
| 285 error = "Invalid buffer size"; |
| 286 } |
| 287 |
| 288 if (result == PP_OK) { |
| 289 memcpy(buffer.GetDataBuffer(), track_buffer.GetDataBuffer(), |
| 290 buffer.GetDataBufferSize()); |
| 291 audio_encoder_.Encode(buffer, callback_factory_.NewCallback( |
| 292 &AudioEncoderInstance::OnEncodeDone)); |
| 293 } else { |
| 294 LogError(result, error); |
| 295 StopAudioTrack(); |
| 296 } |
| 297 audio_track_.RecycleBuffer(track_buffer); |
| 298 } |
| 299 |
| 300 void AudioEncoderInstance::OnEncodeDone(int32_t result) {} |
| 301 |
| 302 void AudioEncoderInstance::OnGetBitstreamBuffer( |
| 303 int32_t result, |
| 304 const PP_AudioBitstreamBuffer& buffer) { |
| 305 if (result == PP_ERROR_ABORTED) |
| 306 return; |
| 307 if (result != PP_OK) { |
| 308 LogError(result, "Cannot get bitstream buffer"); |
| 309 return; |
| 310 } |
| 311 |
| 312 audio_encoder_.GetBitstreamBuffer(callback_factory_.NewCallbackWithOutput( |
| 313 &AudioEncoderInstance::OnGetBitstreamBuffer)); |
| 314 |
| 315 PostDataMessage(buffer.buffer, buffer.size); |
| 316 audio_encoder_.RecycleBitstreamBuffer(buffer); |
| 317 } |
| 318 |
| 319 pp::VarDictionary AudioEncoderInstance::NewCommand(const std::string& type) { |
| 320 pp::VarDictionary dictionary; |
| 321 dictionary.Set(pp::Var("command"), pp::Var(type)); |
| 322 return dictionary; |
| 323 } |
| 324 |
| 325 void AudioEncoderInstance::Log(const std::string& message) { |
| 326 pp::VarDictionary dictionary = NewCommand("log"); |
| 327 dictionary.Set(pp::Var("message"), pp::Var(message)); |
| 328 PostMessage(dictionary); |
| 329 } |
| 330 |
| 331 void AudioEncoderInstance::LogError(int32_t error, const std::string& message) { |
| 332 pp::VarDictionary dictionary = NewCommand("error"); |
| 333 std::string msg("Error: "); |
| 334 msg.append(message); |
| 335 msg.append(" : "); |
| 336 msg.append(pp::Var(error).DebugString()); |
| 337 dictionary.Set(pp::Var("message"), pp::Var(msg)); |
| 338 PostMessage(dictionary); |
| 339 } |
| 340 |
| 341 void AudioEncoderInstance::PostAudioFormat() { |
| 342 pp::VarDictionary dictionary = NewCommand("format"); |
| 343 dictionary.Set(pp::Var("channels"), pp::Var(static_cast<int32_t>(channels_))); |
| 344 dictionary.Set(pp::Var("sample_rate"), |
| 345 pp::Var(static_cast<int32_t>(sample_rate_))); |
| 346 dictionary.Set(pp::Var("sample_size"), |
| 347 pp::Var(static_cast<int32_t>(sample_size_))); |
| 348 dictionary.Set(pp::Var("sample_per_frame"), |
| 349 pp::Var(static_cast<int32_t>(samples_per_frame_))); |
| 350 PostMessage(dictionary); |
| 351 } |
| 352 |
| 353 void AudioEncoderInstance::PostDataMessage(const void* data, uint32_t size) { |
| 354 pp::VarDictionary dictionary = NewCommand("data"); |
| 355 uint8_t* buffer; |
| 356 |
| 357 pp::VarArrayBuffer array_buffer = pp::VarArrayBuffer(size); |
| 358 buffer = static_cast<uint8_t*>(array_buffer.Map()); |
| 359 memcpy(buffer, data, size); |
| 360 array_buffer.Unmap(); |
| 361 |
| 362 dictionary.Set(pp::Var("buffer"), array_buffer); |
| 363 |
| 364 PostMessage(dictionary); |
| 365 } |
| 366 |
| 367 class AudioEncoderModule : public pp::Module { |
| 368 public: |
| 369 virtual pp::Instance* CreateInstance(PP_Instance instance) { |
| 370 return new AudioEncoderInstance(instance); |
| 371 } |
| 372 }; |
| 373 |
| 374 } // namespace |
| 375 |
| 376 namespace pp { |
| 377 |
| 378 // Factory function for your specialization of the Module object. |
| 379 Module* CreateModule() { |
| 380 return new AudioEncoderModule(); |
| 381 } |
| 382 |
| 383 } // namespace pp |
OLD | NEW |