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/rtc_video_encoder.h" | 5 #include "content/renderer/media/rtc_video_encoder.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/location.h" | 8 #include "base/location.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/memory/scoped_vector.h" | 10 #include "base/memory/scoped_vector.h" |
11 #include "base/message_loop/message_loop_proxy.h" | 11 #include "base/message_loop/message_loop_proxy.h" |
12 #include "base/metrics/histogram.h" | 12 #include "base/metrics/histogram.h" |
13 #include "base/rand_util.h" | 13 #include "base/rand_util.h" |
14 #include "base/synchronization/waitable_event.h" | 14 #include "base/synchronization/waitable_event.h" |
15 #include "media/base/bitstream_buffer.h" | 15 #include "media/base/bitstream_buffer.h" |
16 #include "media/base/video_frame.h" | 16 #include "media/base/video_frame.h" |
17 #include "media/base/video_util.h" | 17 #include "media/base/video_util.h" |
18 #include "media/filters/gpu_video_accelerator_factories.h" | 18 #include "media/filters/gpu_video_accelerator_factories.h" |
| 19 #include "media/filters/h264_parser.h" |
19 #include "media/video/video_encode_accelerator.h" | 20 #include "media/video/video_encode_accelerator.h" |
20 #include "third_party/webrtc/system_wrappers/interface/tick_util.h" | 21 #include "third_party/webrtc/system_wrappers/interface/tick_util.h" |
21 | 22 |
22 #define NOTIFY_ERROR(x) \ | 23 #define NOTIFY_ERROR(x) \ |
23 do { \ | 24 do { \ |
24 DLOG(ERROR) << "calling NotifyError(): " << x; \ | 25 DLOG(ERROR) << "calling NotifyError(): " << x; \ |
25 NotifyError(x); \ | 26 NotifyError(x); \ |
26 } while (0) | 27 } while (0) |
27 | 28 |
28 namespace content { | 29 namespace content { |
29 | 30 |
| 31 namespace { |
| 32 |
| 33 // Populates struct webrtc::RTPFragmentationHeader for H264 codec. |
| 34 // Each entry specifies the offset and length (excluding start code) of a NALU. |
| 35 void GetRTPFragmentationHeaderH264( |
| 36 webrtc::RTPFragmentationHeader& header, uint8_t* data, uint32_t length) { |
| 37 media::H264Parser parser; |
| 38 parser.SetStream(data, length); |
| 39 |
| 40 std::vector<media::H264NALU> nalu_vector; |
| 41 while (true) { |
| 42 media::H264NALU nalu; |
| 43 media::H264Parser::Result result; |
| 44 result = parser.AdvanceToNextNALU(&nalu); |
| 45 if (result == media::H264Parser::kOk) { |
| 46 nalu_vector.push_back(nalu); |
| 47 } else if (result == media::H264Parser::kEOStream) { |
| 48 break; |
| 49 } else { |
| 50 DLOG(ERROR) << "Unexpected H264 parser result"; |
| 51 break; |
| 52 } |
| 53 } |
| 54 |
| 55 header.VerifyAndAllocateFragmentationHeader(nalu_vector.size()); |
| 56 for (size_t i = 0; i < nalu_vector.size(); ++i) { |
| 57 header.fragmentationOffset[i] = nalu_vector[i].data - data; |
| 58 header.fragmentationLength[i] = nalu_vector[i].size; |
| 59 header.fragmentationPlType[i] = 0; |
| 60 header.fragmentationTimeDiff[i] = 0; |
| 61 } |
| 62 } |
| 63 |
| 64 } // namespace |
| 65 |
30 // This private class of RTCVideoEncoder does the actual work of communicating | 66 // This private class of RTCVideoEncoder does the actual work of communicating |
31 // with a media::VideoEncodeAccelerator for handling video encoding. It can | 67 // with a media::VideoEncodeAccelerator for handling video encoding. It can |
32 // be created on any thread, but should subsequently be posted to (and Destroy() | 68 // be created on any thread, but should subsequently be posted to (and Destroy() |
33 // called on) a single thread. Callbacks to RTCVideoEncoder are posted to the | 69 // called on) a single thread. Callbacks to RTCVideoEncoder are posted to the |
34 // thread on which the instance was constructed. | 70 // thread on which the instance was constructed. |
35 // | 71 // |
36 // This class separates state related to the thread that RTCVideoEncoder | 72 // This class separates state related to the thread that RTCVideoEncoder |
37 // operates on (presently the libjingle worker thread) from the thread that | 73 // operates on (presently the libjingle worker thread) from the thread that |
38 // |gpu_factories_| provides for accelerator operations (presently the media | 74 // |gpu_factories_| provides for accelerator operations (presently the media |
39 // thread). The RTCVideoEncoder class can be deleted directly by WebRTC, while | 75 // thread). The RTCVideoEncoder class can be deleted directly by WebRTC, while |
(...skipping 601 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
641 int32 bitstream_buffer_id, | 677 int32 bitstream_buffer_id, |
642 uint16 picture_id) { | 678 uint16 picture_id) { |
643 DCHECK(thread_checker_.CalledOnValidThread()); | 679 DCHECK(thread_checker_.CalledOnValidThread()); |
644 DVLOG(3) << "ReturnEncodedImage(): " | 680 DVLOG(3) << "ReturnEncodedImage(): " |
645 << "bitstream_buffer_id=" << bitstream_buffer_id | 681 << "bitstream_buffer_id=" << bitstream_buffer_id |
646 << ", picture_id=" << picture_id; | 682 << ", picture_id=" << picture_id; |
647 | 683 |
648 if (!encoded_image_callback_) | 684 if (!encoded_image_callback_) |
649 return; | 685 return; |
650 | 686 |
| 687 webrtc::RTPFragmentationHeader header; |
| 688 memset(&header, 0, sizeof(header)); |
| 689 switch (video_codec_type_) { |
| 690 case webrtc::kVideoCodecVP8: |
| 691 case webrtc::kVideoCodecGeneric: |
| 692 header.VerifyAndAllocateFragmentationHeader(1); |
| 693 header.fragmentationOffset[0] = 0; |
| 694 header.fragmentationLength[0] = image->_length; |
| 695 header.fragmentationPlType[0] = 0; |
| 696 header.fragmentationTimeDiff[0] = 0; |
| 697 break; |
| 698 case webrtc::kVideoCodecH264: |
| 699 GetRTPFragmentationHeaderH264(header, image->_buffer, image->_length); |
| 700 break; |
| 701 default: |
| 702 NOTREACHED() << "Invalid video codec type"; |
| 703 return; |
| 704 } |
| 705 |
651 webrtc::CodecSpecificInfo info; | 706 webrtc::CodecSpecificInfo info; |
652 memset(&info, 0, sizeof(info)); | 707 memset(&info, 0, sizeof(info)); |
653 info.codecType = video_codec_type_; | 708 info.codecType = video_codec_type_; |
654 if (video_codec_type_ == webrtc::kVideoCodecVP8) { | 709 if (video_codec_type_ == webrtc::kVideoCodecVP8) { |
655 info.codecSpecific.VP8.pictureId = picture_id; | 710 info.codecSpecific.VP8.pictureId = picture_id; |
656 info.codecSpecific.VP8.tl0PicIdx = -1; | 711 info.codecSpecific.VP8.tl0PicIdx = -1; |
657 info.codecSpecific.VP8.keyIdx = -1; | 712 info.codecSpecific.VP8.keyIdx = -1; |
658 } | 713 } |
659 | 714 |
660 // Generate a header describing a single fragment. | |
661 webrtc::RTPFragmentationHeader header; | |
662 memset(&header, 0, sizeof(header)); | |
663 header.VerifyAndAllocateFragmentationHeader(1); | |
664 header.fragmentationOffset[0] = 0; | |
665 header.fragmentationLength[0] = image->_length; | |
666 header.fragmentationPlType[0] = 0; | |
667 header.fragmentationTimeDiff[0] = 0; | |
668 | |
669 int32_t retval = encoded_image_callback_->Encoded(*image, &info, &header); | 715 int32_t retval = encoded_image_callback_->Encoded(*image, &info, &header); |
670 if (retval < 0) { | 716 if (retval < 0) { |
671 DVLOG(2) << "ReturnEncodedImage(): encoded_image_callback_ returned " | 717 DVLOG(2) << "ReturnEncodedImage(): encoded_image_callback_ returned " |
672 << retval; | 718 << retval; |
673 } | 719 } |
674 | 720 |
675 // The call through webrtc::EncodedImageCallback is synchronous, so we can | 721 // The call through webrtc::EncodedImageCallback is synchronous, so we can |
676 // immediately recycle the output buffer back to the Impl. | 722 // immediately recycle the output buffer back to the Impl. |
677 gpu_factories_->GetTaskRunner()->PostTask( | 723 gpu_factories_->GetTaskRunner()->PostTask( |
678 FROM_HERE, | 724 FROM_HERE, |
(...skipping 16 matching lines...) Expand all Loading... |
695 UMA_HISTOGRAM_BOOLEAN("Media.RTCVideoEncoderInitEncodeSuccess", | 741 UMA_HISTOGRAM_BOOLEAN("Media.RTCVideoEncoderInitEncodeSuccess", |
696 init_retval == WEBRTC_VIDEO_CODEC_OK); | 742 init_retval == WEBRTC_VIDEO_CODEC_OK); |
697 if (init_retval == WEBRTC_VIDEO_CODEC_OK) { | 743 if (init_retval == WEBRTC_VIDEO_CODEC_OK) { |
698 UMA_HISTOGRAM_ENUMERATION("Media.RTCVideoEncoderProfile", | 744 UMA_HISTOGRAM_ENUMERATION("Media.RTCVideoEncoderProfile", |
699 video_codec_profile_, | 745 video_codec_profile_, |
700 media::VIDEO_CODEC_PROFILE_MAX + 1); | 746 media::VIDEO_CODEC_PROFILE_MAX + 1); |
701 } | 747 } |
702 } | 748 } |
703 | 749 |
704 } // namespace content | 750 } // namespace content |
OLD | NEW |