| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 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 <list> |
| 6 |
| 7 #include "base/bind_helpers.h" |
| 8 #include "base/logging.h" |
| 9 #include "mojo/services/media/audio/interfaces/audio_server.mojom.h" |
| 10 #include "mojo/services/media/audio/interfaces/audio_track.mojom.h" |
| 11 #include "mojo/services/media/common/cpp/linear_transform.h" |
| 12 #include "mojo/services/media/common/cpp/local_time.h" |
| 13 #include "services/media/framework/conversion_pipeline_builder.h" |
| 14 #include "services/media/framework_mojo/audio_track_controller.h" |
| 15 #include "services/media/framework_mojo/mojo_type_conversions.h" |
| 16 |
| 17 namespace mojo { |
| 18 namespace media { |
| 19 |
| 20 AudioTrackController::AudioTrackController( |
| 21 const String& url, |
| 22 std::unique_ptr<StreamType> stream_type, |
| 23 Graph* graph, |
| 24 OutputRef output, |
| 25 ApplicationImpl* app, |
| 26 const ConstructorCallback& callback) { |
| 27 DCHECK(stream_type); |
| 28 DCHECK(graph); |
| 29 DCHECK(output); |
| 30 DCHECK(app); |
| 31 |
| 32 AudioServerPtr audio_server; |
| 33 app->ConnectToService(url, &audio_server); |
| 34 audio_server->CreateTrack(GetProxy(&audio_track_)); |
| 35 |
| 36 // TODO(dalesat): Remove capture hack once c++14 is happening. |
| 37 std::shared_ptr<StreamType> captured_stream_type(stream_type.release()); |
| 38 |
| 39 // Query the track's format capabilities. |
| 40 audio_track_->Describe( |
| 41 [this, callback, captured_stream_type, graph, output] |
| 42 (AudioTrackDescriptorPtr descriptor) { |
| 43 std::unique_ptr<StreamType> producer_stream_type; |
| 44 |
| 45 // Add transforms to the pipeline to convert from stream_type to a type |
| 46 // supported by the track. |
| 47 OutputRef out = output; |
| 48 bool result = BuildConversionPipeline( |
| 49 *captured_stream_type, |
| 50 *Convert(descriptor->supported_media_types), |
| 51 graph, |
| 52 &out, |
| 53 &producer_stream_type); |
| 54 if (!result) { |
| 55 // Failed to build conversion pipeline. |
| 56 callback.Run(nullptr); |
| 57 return; |
| 58 } |
| 59 |
| 60 switch (producer_stream_type->scheme()) { |
| 61 case StreamType::Scheme::kLpcm: |
| 62 frames_per_second_ = |
| 63 producer_stream_type->lpcm()->frames_per_second(); |
| 64 break; |
| 65 case StreamType::Scheme::kCompressedAudio: |
| 66 frames_per_second_ = |
| 67 producer_stream_type->compressed_audio()->frames_per_second(); |
| 68 break; |
| 69 default: |
| 70 // Unsupported producer stream type. |
| 71 callback.Run(nullptr); |
| 72 return; |
| 73 } |
| 74 |
| 75 AudioTrackConfigurationPtr config = AudioTrackConfiguration::New(); |
| 76 config->media_type = Convert(std::move(producer_stream_type)); |
| 77 |
| 78 audio_track_->Configure(config.Pass(), GetProxy(&pipe_)); |
| 79 |
| 80 std::shared_ptr<AudioTrackProducer> sink = |
| 81 AudioTrackProducer::Create(pipe_.Pass()); |
| 82 graph->ConnectOutputToPart(out, graph->Add(sink)); |
| 83 callback.Run(sink); |
| 84 }); |
| 85 } |
| 86 |
| 87 AudioTrackController::~AudioTrackController() {} |
| 88 |
| 89 void AudioTrackController::SetRate( |
| 90 float rate_factor, |
| 91 const SetRateCallback& callback) { |
| 92 // TODO(dalesat): Set the rate at a particular local time to coordinate rate |
| 93 // changes for multiple outputs. |
| 94 // TODO(dalesat): Need to specify the starting media time for seek and for |
| 95 // sources that don't start at zero. |
| 96 if (!rate_control_.is_bound()) { |
| 97 audio_track_->GetRateControl(GetProxy(&rate_control_)); |
| 98 } |
| 99 |
| 100 LinearTransform::Ratio audio_rate( |
| 101 static_cast<uint32_t>(frames_per_second_ * rate_factor), 1); |
| 102 LinearTransform::Ratio local_time_rate( |
| 103 LocalDuration::period::num, |
| 104 LocalDuration::period::den); |
| 105 |
| 106 LinearTransform::Ratio rate; |
| 107 bool success = |
| 108 LinearTransform::Ratio::Compose(local_time_rate, audio_rate, &rate); |
| 109 DCHECK(success) |
| 110 << "LinearTransform::Ratio::Compose reports loss of precision"; |
| 111 |
| 112 rate_control_->SetRate(rate.numerator, rate.denominator); |
| 113 |
| 114 // TODO(dalesat): Replace this with a clock. |
| 115 // The code below produces a transform that translates local time into media |
| 116 // time in nanosecond units. That transform is delivered to the application, |
| 117 // which uses it to implement a progress bar. This is OK for demo purposes, |
| 118 // but we really need a clock rather than this static transform. |
| 119 rate_control_->GetCurrentTransform( |
| 120 [this, callback](TimelineTransformPtr transform) { |
| 121 // Get the frame rate in local duration units. |
| 122 LinearTransform::Ratio audio_rate(frames_per_second_, 1); |
| 123 LinearTransform::Ratio local_time_rate( |
| 124 LocalDuration::period::num, |
| 125 LocalDuration::period::den); |
| 126 LinearTransform::Ratio presentation_rate; |
| 127 bool success = LinearTransform::Ratio::Compose( |
| 128 local_time_rate, |
| 129 audio_rate, |
| 130 &presentation_rate); |
| 131 DCHECK(success) |
| 132 << "LinearTransform::Ratio::Compose reports loss of precision"; |
| 133 |
| 134 // Create a LinearTransform to translate from presentation units to |
| 135 // local duration units. |
| 136 LinearTransform local_to_presentation(0, presentation_rate, 0); |
| 137 |
| 138 // Translate the current transform quad so the presentation time units |
| 139 // are the same as the local time units. |
| 140 success = local_to_presentation.DoReverseTransform( |
| 141 transform->quad->reference_offset, |
| 142 &transform->quad->reference_offset); |
| 143 DCHECK(success) |
| 144 << "LinearTransform::DoReverseTransform reports loss of precision"; |
| 145 int64_t presentation_delta; |
| 146 success = local_to_presentation.DoReverseTransform( |
| 147 static_cast<int64_t>(transform->quad->reference_delta), |
| 148 &presentation_delta); |
| 149 DCHECK(success) |
| 150 << "LinearTransform::DoReverseTransform reports loss of precision"; |
| 151 transform->quad->reference_delta = |
| 152 static_cast<int32_t>(presentation_delta); |
| 153 LinearTransform::Ratio::Reduce( |
| 154 &transform->quad->reference_delta, |
| 155 &transform->quad->target_delta); |
| 156 |
| 157 callback(transform.Clone()); |
| 158 }); |
| 159 } |
| 160 |
| 161 } // namespace media |
| 162 } // namespace mojo |
| OLD | NEW |