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