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 <algorithm> |
| 9 #include <limits> |
| 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 struct FrameDescription { |
| 40 FrameDescription() |
| 41 : channels(0), sample_rate(0), sample_size(0), samples_per_frame(0) {} |
| 42 FrameDescription(uint32_t channels, |
| 43 uint32_t sample_rate, |
| 44 uint32_t sample_size, |
| 45 uint32_t samples_per_frame) |
| 46 : channels(channels), |
| 47 sample_rate(sample_rate), |
| 48 sample_size(sample_size), |
| 49 samples_per_frame(samples_per_frame) {} |
| 50 ~FrameDescription() {} |
| 51 |
| 52 uint32_t channels; |
| 53 uint32_t sample_rate; |
| 54 uint32_t sample_size; |
| 55 uint32_t samples_per_frame; |
| 56 }; |
| 57 |
| 58 // |
| 59 virtual void HandleMessage(const pp::Var& var_message); |
| 60 |
| 61 void AddAudioProfile(PP_AudioProfile profile, const std::string& profile_str); |
| 62 void InitializeVideoProfiles(); |
| 63 PP_AudioProfile ProfileFromString(const std::string& string); |
| 64 std::string ProfileToString(PP_AudioProfile profile); |
| 65 |
| 66 void ProbeEncoder(); |
| 67 void OnEncoderProbed(int32_t result, |
| 68 const std::vector<PP_AudioProfileDescription> profiles); |
| 69 |
| 70 void StartAudioTrack(const pp::Resource& resource_track); |
| 71 void StopAudioTrack(); |
| 72 void OnGetFirstBuffer(int32_t result, pp::AudioBuffer buffer); |
| 73 void OnGetBuffer(int32_t result, pp::AudioBuffer buffer); |
| 74 |
| 75 void InitializeEncoder(); |
| 76 void OnEncoderInitialized(int32_t result); |
| 77 void OnAudioTrackReconfigured(int32_t result); |
| 78 void GetEncoderBuffer(const pp::AudioBuffer& track_buffer); |
| 79 void OnGetEncoderBuffer(int32_t result, |
| 80 pp::AudioBuffer buffer, |
| 81 pp::AudioBuffer track_buffer); |
| 82 void OnEncodeDone(int32_t result); |
| 83 void OnBitstreamBuffer(int32_t result, const PP_AudioBitstreamBuffer& buffer); |
| 84 |
| 85 pp::VarDictionary NewCommand(const std::string& type); |
| 86 |
| 87 void Log(const std::string& message); |
| 88 void LogError(int32_t error, const std::string& message); |
| 89 |
| 90 void PostAudioFormat(); |
| 91 void PostDataMessage(const void* data, uint32_t size); |
| 92 |
| 93 pp::CompletionCallbackFactory<AudioEncoderInstance> callback_factory_; |
| 94 |
| 95 bool no_encoding_; |
| 96 bool is_encoding_; |
| 97 PP_AudioProfile encoding_profile_; |
| 98 |
| 99 FrameDescription frame_description_; |
| 100 |
| 101 pp::MediaStreamAudioTrack audio_track_; |
| 102 pp::AudioEncoder audio_encoder_; |
| 103 |
| 104 typedef std::map<std::string, PP_AudioProfile> AudioProfileFromStringMap; |
| 105 AudioProfileFromStringMap profile_from_string_; |
| 106 |
| 107 typedef std::map<PP_AudioProfile, std::string> AudioProfileToStringMap; |
| 108 AudioProfileToStringMap profile_to_string_; |
| 109 }; |
| 110 |
| 111 AudioEncoderInstance::AudioEncoderInstance(PP_Instance instance) |
| 112 : pp::Instance(instance), |
| 113 callback_factory_(this), |
| 114 no_encoding_(true), |
| 115 is_encoding_(false), |
| 116 encoding_profile_(PP_AUDIOPROFILE_OPUS) { |
| 117 InitializeVideoProfiles(); |
| 118 ProbeEncoder(); |
| 119 } |
| 120 |
| 121 AudioEncoderInstance::~AudioEncoderInstance() { |
| 122 } |
| 123 |
| 124 void AudioEncoderInstance::HandleMessage(const pp::Var& var_message) { |
| 125 if (!var_message.is_dictionary()) { |
| 126 LogError(PP_ERROR_FAILED, |
| 127 "Cannot handle incoming message, not a dictionary"); |
| 128 return; |
| 129 } |
| 130 |
| 131 pp::VarDictionary dict_message(var_message); |
| 132 std::string command = dict_message.Get("command").AsString(); |
| 133 |
| 134 if (command == "start") { |
| 135 pp::Var var_track = dict_message.Get("track"); |
| 136 if (!var_track.is_resource()) { |
| 137 LogError(PP_ERROR_FAILED, "Given track is not a resource"); |
| 138 return; |
| 139 } |
| 140 std::string profile = dict_message.Get("profile").AsString(); |
| 141 if (profile == "wav") |
| 142 no_encoding_ = true; |
| 143 else { |
| 144 no_encoding_ = false; |
| 145 encoding_profile_ = ProfileFromString(profile); |
| 146 } |
| 147 StartAudioTrack(var_track.AsResource()); |
| 148 } else if (command == "stop") { |
| 149 StopAudioTrack(); |
| 150 } else { |
| 151 LogError(PP_ERROR_FAILED, "Invalid command"); |
| 152 } |
| 153 } |
| 154 |
| 155 void AudioEncoderInstance::AddAudioProfile(PP_AudioProfile profile, |
| 156 const std::string& profile_str) { |
| 157 profile_to_string_.insert(std::make_pair(profile, profile_str)); |
| 158 profile_from_string_.insert(std::make_pair(profile_str, profile)); |
| 159 } |
| 160 |
| 161 void AudioEncoderInstance::InitializeVideoProfiles() { |
| 162 AddAudioProfile(PP_AUDIOPROFILE_OPUS, "opus"); |
| 163 AddAudioProfile(PP_AUDIOPROFILE_SPEEX, "speex"); |
| 164 } |
| 165 |
| 166 PP_AudioProfile AudioEncoderInstance::ProfileFromString( |
| 167 const std::string& string) { |
| 168 AudioProfileFromStringMap::iterator it = profile_from_string_.find(string); |
| 169 if (it == profile_from_string_.end()) |
| 170 return PP_AUDIOPROFILE_OPUS; |
| 171 return it->second; |
| 172 } |
| 173 |
| 174 std::string AudioEncoderInstance::ProfileToString(PP_AudioProfile profile) { |
| 175 AudioProfileToStringMap::iterator it = profile_to_string_.find(profile); |
| 176 if (it == profile_to_string_.end()) |
| 177 return "unknown"; |
| 178 return it->second; |
| 179 } |
| 180 |
| 181 void AudioEncoderInstance::ProbeEncoder() { |
| 182 audio_encoder_ = pp::AudioEncoder(this); |
| 183 audio_encoder_.GetSupportedProfiles(callback_factory_.NewCallbackWithOutput( |
| 184 &AudioEncoderInstance::OnEncoderProbed)); |
| 185 } |
| 186 |
| 187 void AudioEncoderInstance::OnEncoderProbed( |
| 188 int32_t result, |
| 189 const std::vector<PP_AudioProfileDescription> profiles) { |
| 190 pp::VarDictionary dictionary = NewCommand("supportedProfiles"); |
| 191 pp::VarArray js_profiles; |
| 192 dictionary.Set(pp::Var("profiles"), js_profiles); |
| 193 |
| 194 if (result < 0) { |
| 195 LogError(result, "Cannot get supported profiles"); |
| 196 PostMessage(dictionary); |
| 197 } |
| 198 |
| 199 int32_t idx = 0; |
| 200 for (const PP_AudioProfileDescription& profile : profiles) { |
| 201 pp::VarDictionary js_profile; |
| 202 js_profile.Set(pp::Var("name"), pp::Var(ProfileToString(profile.profile))); |
| 203 js_profile.Set(pp::Var("sample_size"), |
| 204 pp::Var(static_cast<int32_t>(profile.sample_size))); |
| 205 js_profile.Set(pp::Var("sample_rate"), |
| 206 pp::Var(static_cast<int32_t>(profile.sample_rate))); |
| 207 js_profiles.Set(idx++, js_profile); |
| 208 } |
| 209 PostMessage(dictionary); |
| 210 } |
| 211 |
| 212 void AudioEncoderInstance::StartAudioTrack(const pp::Resource& resource_track) { |
| 213 audio_track_ = pp::MediaStreamAudioTrack(resource_track); |
| 214 audio_track_.GetBuffer(callback_factory_.NewCallbackWithOutput( |
| 215 &AudioEncoderInstance::OnGetFirstBuffer)); |
| 216 } |
| 217 |
| 218 void AudioEncoderInstance::StopAudioTrack() { |
| 219 Log("StopAudioTrack!"); |
| 220 audio_encoder_.Close(); |
| 221 audio_encoder_ = pp::AudioEncoder(); |
| 222 audio_track_.Close(); |
| 223 audio_track_ = pp::MediaStreamAudioTrack(); |
| 224 } |
| 225 |
| 226 void AudioEncoderInstance::OnGetFirstBuffer(int32_t result, |
| 227 pp::AudioBuffer buffer) { |
| 228 if (result == PP_ERROR_ABORTED) |
| 229 return; |
| 230 if (result != PP_OK) { |
| 231 LogError(result, "Cannot get audio track buffer"); |
| 232 return; |
| 233 } |
| 234 |
| 235 frame_description_ = |
| 236 FrameDescription(buffer.GetNumberOfChannels(), buffer.GetSampleRate(), |
| 237 buffer.GetSampleSize(), buffer.GetNumberOfSamples()); |
| 238 |
| 239 if (no_encoding_) { |
| 240 PostAudioFormat(); |
| 241 PostDataMessage(buffer.GetDataBuffer(), buffer.GetDataBufferSize()); |
| 242 audio_track_.GetBuffer(callback_factory_.NewCallbackWithOutput( |
| 243 &AudioEncoderInstance::OnGetBuffer)); |
| 244 } else { |
| 245 InitializeEncoder(); |
| 246 } |
| 247 |
| 248 audio_track_.RecycleBuffer(buffer); |
| 249 } |
| 250 |
| 251 void AudioEncoderInstance::OnGetBuffer(int32_t result, pp::AudioBuffer buffer) { |
| 252 if (result == PP_ERROR_ABORTED) |
| 253 return; |
| 254 if (result != PP_OK) { |
| 255 LogError(result, "Cannot get audio track buffer"); |
| 256 return; |
| 257 } |
| 258 |
| 259 if (no_encoding_) { |
| 260 PostDataMessage(buffer.GetDataBuffer(), buffer.GetDataBufferSize()); |
| 261 audio_track_.RecycleBuffer(buffer); |
| 262 } else |
| 263 GetEncoderBuffer(buffer); |
| 264 |
| 265 audio_track_.GetBuffer(callback_factory_.NewCallbackWithOutput( |
| 266 &AudioEncoderInstance::OnGetBuffer)); |
| 267 } |
| 268 |
| 269 void AudioEncoderInstance::InitializeEncoder() { |
| 270 audio_encoder_ = pp::AudioEncoder(this); |
| 271 audio_encoder_.Initialize( |
| 272 frame_description_.channels, |
| 273 static_cast<PP_AudioBuffer_SampleRate>(frame_description_.sample_rate), |
| 274 static_cast<PP_AudioBuffer_SampleSize>(frame_description_.sample_size), |
| 275 encoding_profile_, 100000, PP_HARDWAREACCELERATION_WITHFALLBACK, |
| 276 callback_factory_.NewCallback( |
| 277 &AudioEncoderInstance::OnEncoderInitialized)); |
| 278 } |
| 279 |
| 280 void AudioEncoderInstance::OnEncoderInitialized(int32_t result) { |
| 281 if (result == PP_ERROR_ABORTED) |
| 282 return; |
| 283 if (result != PP_OK) { |
| 284 LogError(result, "Cannot initialize encoder"); |
| 285 return; |
| 286 } |
| 287 |
| 288 int32_t attribs[] = {// Duration in milliseconds. |
| 289 PP_MEDIASTREAMAUDIOTRACK_ATTRIB_DURATION, |
| 290 1000 / (frame_description_.sample_rate / |
| 291 audio_encoder_.GetNumberOfSamples()), |
| 292 PP_MEDIASTREAMAUDIOTRACK_ATTRIB_NONE}; |
| 293 audio_track_.Configure(attribs, |
| 294 callback_factory_.NewCallback( |
| 295 &AudioEncoderInstance::OnAudioTrackReconfigured)); |
| 296 |
| 297 frame_description_.samples_per_frame = audio_encoder_.GetNumberOfSamples(); |
| 298 PostAudioFormat(); |
| 299 } |
| 300 |
| 301 void AudioEncoderInstance::OnAudioTrackReconfigured(int32_t result) { |
| 302 if (result == PP_ERROR_ABORTED) |
| 303 return; |
| 304 if (result != PP_OK) { |
| 305 LogError(result, "Cannot reconfigure audio track buffer duration"); |
| 306 return; |
| 307 } |
| 308 |
| 309 is_encoding_ = true; |
| 310 audio_encoder_.GetBitstreamBuffer(callback_factory_.NewCallbackWithOutput( |
| 311 &AudioEncoderInstance::OnBitstreamBuffer)); |
| 312 audio_track_.GetBuffer(callback_factory_.NewCallbackWithOutput( |
| 313 &AudioEncoderInstance::OnGetBuffer)); |
| 314 } |
| 315 |
| 316 void AudioEncoderInstance::GetEncoderBuffer( |
| 317 const pp::AudioBuffer& track_buffer) { |
| 318 audio_encoder_.GetBuffer(callback_factory_.NewCallbackWithOutput( |
| 319 &AudioEncoderInstance::OnGetEncoderBuffer, track_buffer)); |
| 320 } |
| 321 |
| 322 void AudioEncoderInstance::OnGetEncoderBuffer(int32_t result, |
| 323 pp::AudioBuffer buffer, |
| 324 pp::AudioBuffer track_buffer) { |
| 325 if (result == PP_ERROR_ABORTED) { |
| 326 audio_track_.RecycleBuffer(track_buffer); |
| 327 return; |
| 328 } |
| 329 if (result != PP_OK) { |
| 330 audio_track_.RecycleBuffer(track_buffer); |
| 331 LogError(result, "Cannot get encoder buffer"); |
| 332 return; |
| 333 } |
| 334 |
| 335 if (buffer.GetDataBufferSize() != track_buffer.GetDataBufferSize()) { |
| 336 audio_track_.RecycleBuffer(track_buffer); |
| 337 LogError(PP_ERROR_FAILED, "Invalid buffer size"); |
| 338 return; |
| 339 } |
| 340 |
| 341 memcpy(buffer.GetDataBuffer(), track_buffer.GetDataBuffer(), |
| 342 buffer.GetDataBufferSize()); |
| 343 audio_track_.RecycleBuffer(track_buffer); |
| 344 |
| 345 audio_encoder_.Encode(buffer, callback_factory_.NewCallback( |
| 346 &AudioEncoderInstance::OnEncodeDone)); |
| 347 } |
| 348 |
| 349 void AudioEncoderInstance::OnEncodeDone(int32_t result) { |
| 350 } |
| 351 |
| 352 void AudioEncoderInstance::OnBitstreamBuffer( |
| 353 int32_t result, |
| 354 const PP_AudioBitstreamBuffer& buffer) { |
| 355 if (result == PP_ERROR_ABORTED) |
| 356 return; |
| 357 if (result != PP_OK) { |
| 358 LogError(result, "Cannot get bitstream buffer"); |
| 359 return; |
| 360 } |
| 361 |
| 362 audio_encoder_.GetBitstreamBuffer(callback_factory_.NewCallbackWithOutput( |
| 363 &AudioEncoderInstance::OnBitstreamBuffer)); |
| 364 |
| 365 PostDataMessage(buffer.buffer, buffer.size); |
| 366 audio_encoder_.RecycleBitstreamBuffer(buffer); |
| 367 } |
| 368 |
| 369 pp::VarDictionary AudioEncoderInstance::NewCommand(const std::string& type) { |
| 370 pp::VarDictionary dictionary; |
| 371 dictionary.Set(pp::Var("command"), pp::Var(type)); |
| 372 return dictionary; |
| 373 } |
| 374 |
| 375 void AudioEncoderInstance::Log(const std::string& message) { |
| 376 pp::VarDictionary dictionary = NewCommand("log"); |
| 377 dictionary.Set(pp::Var("message"), pp::Var(message)); |
| 378 PostMessage(dictionary); |
| 379 } |
| 380 |
| 381 void AudioEncoderInstance::LogError(int32_t error, const std::string& message) { |
| 382 pp::VarDictionary dictionary = NewCommand("error"); |
| 383 std::string msg("Error: "); |
| 384 msg.append(message); |
| 385 msg.append(" : "); |
| 386 msg.append(pp::Var(error).DebugString()); |
| 387 dictionary.Set(pp::Var("message"), pp::Var(msg)); |
| 388 PostMessage(dictionary); |
| 389 } |
| 390 |
| 391 void AudioEncoderInstance::PostAudioFormat() { |
| 392 pp::VarDictionary dictionary = NewCommand("format"); |
| 393 dictionary.Set(pp::Var("channels"), |
| 394 pp::Var(static_cast<int32_t>(frame_description_.channels))); |
| 395 dictionary.Set(pp::Var("sample_rate"), |
| 396 pp::Var(static_cast<int32_t>(frame_description_.sample_rate))); |
| 397 dictionary.Set(pp::Var("sample_size"), |
| 398 pp::Var(static_cast<int32_t>(frame_description_.sample_size))); |
| 399 dictionary.Set( |
| 400 pp::Var("sample_per_frame"), |
| 401 pp::Var(static_cast<int32_t>(frame_description_.samples_per_frame))); |
| 402 PostMessage(dictionary); |
| 403 } |
| 404 |
| 405 void AudioEncoderInstance::PostDataMessage(const void* data, uint32_t size) { |
| 406 pp::VarDictionary dictionary = NewCommand("data"); |
| 407 uint8_t* buffer; |
| 408 |
| 409 pp::VarArrayBuffer array_buffer = pp::VarArrayBuffer(size); |
| 410 buffer = static_cast<uint8_t*>(array_buffer.Map()); |
| 411 memcpy(buffer, data, size); |
| 412 array_buffer.Unmap(); |
| 413 |
| 414 dictionary.Set(pp::Var("buffer"), array_buffer); |
| 415 |
| 416 PostMessage(dictionary); |
| 417 } |
| 418 |
| 419 class AudioEncoderModule : public pp::Module { |
| 420 public: |
| 421 virtual pp::Instance* CreateInstance(PP_Instance instance) { |
| 422 return new AudioEncoderInstance(instance); |
| 423 } |
| 424 }; |
| 425 |
| 426 } // namespace |
| 427 |
| 428 namespace pp { |
| 429 |
| 430 // Factory function for your specialization of the Module object. |
| 431 Module* CreateModule() { |
| 432 return new AudioEncoderModule(); |
| 433 } |
| 434 |
| 435 } // namespace pp |
OLD | NEW |