OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/renderer/media/android/audio_decoder_android.h" | 5 #include "content/renderer/media/android/audio_decoder_android.h" |
6 | 6 |
7 #include <errno.h> | 7 #include <errno.h> |
8 #include <fcntl.h> | 8 #include <fcntl.h> |
9 #include <limits.h> | 9 #include <limits.h> |
10 #include <sys/mman.h> | 10 #include <sys/mman.h> |
(...skipping 387 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
398 blink::WebAudioBus* destination_bus, | 398 blink::WebAudioBus* destination_bus, |
399 size_t number_of_frames, | 399 size_t number_of_frames, |
400 unsigned number_of_channels, | 400 unsigned number_of_channels, |
401 double file_sample_rate) { | 401 double file_sample_rate) { |
402 destination_bus->initialize(number_of_channels, | 402 destination_bus->initialize(number_of_channels, |
403 number_of_frames, | 403 number_of_frames, |
404 file_sample_rate); | 404 file_sample_rate); |
405 | 405 |
406 int16_t pipe_data[PIPE_BUF / sizeof(int16_t)]; | 406 int16_t pipe_data[PIPE_BUF / sizeof(int16_t)]; |
407 size_t decoded_frames = 0; | 407 size_t decoded_frames = 0; |
| 408 size_t current_sample_in_frame = 0; |
408 ssize_t nread; | 409 ssize_t nread; |
409 | 410 |
410 while ((nread = HANDLE_EINTR(read(input_fd, pipe_data, sizeof(pipe_data)))) > | 411 while ((nread = HANDLE_EINTR(read(input_fd, pipe_data, sizeof(pipe_data)))) > |
411 0) { | 412 0) { |
412 size_t samples_in_pipe = nread / sizeof(int16_t); | 413 size_t samples_in_pipe = nread / sizeof(int16_t); |
413 for (size_t m = 0; m < samples_in_pipe; m += number_of_channels) { | 414 |
| 415 // The pipe may not contain a whole number of frames. This is |
| 416 // especially true if the number of channels is greater than |
| 417 // 2. Thus, keep track of which sample in a frame is being |
| 418 // processed, so we handle the boundary at the end of the pipe |
| 419 // correctly. |
| 420 for (size_t m = 0; m < samples_in_pipe; ++m) { |
414 if (decoded_frames >= number_of_frames) | 421 if (decoded_frames >= number_of_frames) |
415 break; | 422 break; |
416 | 423 |
417 for (size_t k = 0; k < number_of_channels; ++k) { | 424 destination_bus->channelData(current_sample_in_frame)[decoded_frames] = |
418 int16_t sample = pipe_data[m + k]; | 425 ConvertSampleToFloat(pipe_data[m]); |
419 destination_bus->channelData(k)[decoded_frames] = | 426 ++current_sample_in_frame; |
420 ConvertSampleToFloat(sample); | 427 |
| 428 if (current_sample_in_frame >= number_of_channels) { |
| 429 current_sample_in_frame = 0; |
| 430 ++decoded_frames; |
421 } | 431 } |
422 ++decoded_frames; | |
423 } | 432 } |
424 } | 433 } |
425 | 434 |
426 // number_of_frames is only an estimate. Resize the buffer with the | 435 // number_of_frames is only an estimate. Resize the buffer with the |
427 // actual number of received frames. | 436 // actual number of received frames. |
428 if (decoded_frames < number_of_frames) | 437 if (decoded_frames < number_of_frames) |
429 destination_bus->resizeSmaller(decoded_frames); | 438 destination_bus->resizeSmaller(decoded_frames); |
430 } | 439 } |
431 | 440 |
432 // The number of frames is unknown, so keep reading and buffering | 441 // The number of frames is unknown, so keep reading and buffering |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
565 BufferAndCopyPcmDataToBus(input_fd, | 574 BufferAndCopyPcmDataToBus(input_fd, |
566 destination_bus, | 575 destination_bus, |
567 number_of_channels, | 576 number_of_channels, |
568 file_sample_rate); | 577 file_sample_rate); |
569 } | 578 } |
570 | 579 |
571 return true; | 580 return true; |
572 } | 581 } |
573 | 582 |
574 } // namespace content | 583 } // namespace content |
OLD | NEW |