Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(594)

Side by Side Diff: services/media/framework_mojo/audio_track_controller.cc

Issue 1692443002: Motown: Framework parts for mojo transport (producer/consumer/mediapipe) and control (audiotrack). (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Sync and fix: InterfaceHandle<X> vs XPtr, changes to MediaPipe Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 pass hack once c++14 is happening.
37 StreamType* passed_stream_type = stream_type.release();
38
39 // Query the track's format capabilities.
40 audio_track_->Describe(
41 [this, callback, passed_stream_type, graph, output]
42 (AudioTrackDescriptorPtr descriptor) {
43 std::unique_ptr<StreamType> stream_type(passed_stream_type);
johngro 2016/02/23 00:49:42 I think that this is going to leak the passed_stre
dalesat 2016/02/23 20:34:34 I'm really opposed to cluttering the class definit
johngro 2016/02/23 22:11:04 We are not using C++14 yet. I went ahead and conf
dalesat 2016/02/26 19:36:22 I fixed this by capturing a shared pointer and cha
johngro 2016/02/26 20:36:26 k, works for me. FWIW - if you wanted, you could
44 std::unique_ptr<StreamType> producer_stream_type;
45
46 // Add transforms to the pipeline to convert from stream_type to a type
47 // supported by the track.
48 OutputRef out = output;
49 bool result = BuildConversionPipeline(
50 stream_type,
51 Convert(descriptor->supported_media_types),
52 graph,
53 &out,
54 &producer_stream_type);
55 if (!result) {
56 // Failed to build conversion pipeline.
57 callback.Run(nullptr);
58 return;
59 }
60
61 switch (producer_stream_type->scheme()) {
62 case StreamType::Scheme::kLpcm:
63 frames_per_second_ =
64 producer_stream_type->lpcm()->frames_per_second();
65 break;
66 case StreamType::Scheme::kCompressedAudio:
67 frames_per_second_ =
68 producer_stream_type->compressed_audio()->frames_per_second();
69 break;
70 default:
71 // Unsupported producer stream type.
72 callback.Run(nullptr);
73 return;
74 }
75
76 AudioTrackConfigurationPtr config = AudioTrackConfiguration::New();
77 config->media_type = Convert(std::move(producer_stream_type));
78
79 audio_track_->Configure(config.Pass(), GetProxy(&pipe_));
80
81 std::shared_ptr<AudioTrackProducer> sink =
82 AudioTrackProducer::Create(pipe_.Pass());
83 graph->ConnectOutputToPart(out, graph->Add(sink));
84 callback.Run(sink);
85 });
86 }
87
88 AudioTrackController::~AudioTrackController() {}
89
90 void AudioTrackController::SetRate(
91 float rate_factor,
92 const SetRateCallback& callback) {
93 if (!rate_control_.is_bound()) {
94 audio_track_->GetRateControl(GetProxy(&rate_control_));
95 }
96
97 LinearTransform::Ratio audio_rate(
98 static_cast<uint32_t>(frames_per_second_ * rate_factor), 1);
99 LinearTransform::Ratio local_time_rate(
100 LocalDuration::period::num,
101 LocalDuration::period::den);
102
103 LinearTransform::Ratio rate;
104 bool success =
105 LinearTransform::Ratio::Compose(local_time_rate, audio_rate, &rate);
106 DCHECK(success)
107 << "LinearTransform::Ratio::Compose reports loss of precision";
108
109 rate_control_->SetRate(rate.numerator, rate.denominator);
110
111 rate_control_->GetCurrentTransform(
112 [this, callback](TimelineTransformPtr transform) {
113 // Get the frame rate in local duration units.
114 LinearTransform::Ratio audio_rate(frames_per_second_, 1);
115 LinearTransform::Ratio local_time_rate(
116 LocalDuration::period::num,
117 LocalDuration::period::den);
118 LinearTransform::Ratio presentation_rate;
119 bool success = LinearTransform::Ratio::Compose(
120 local_time_rate,
121 audio_rate,
122 &presentation_rate);
123 DCHECK(success)
124 << "LinearTransform::Ratio::Compose reports loss of precision";
125
126 // Create a LinearTransform to translate from presentation units to
127 // local duration units.
128 LinearTransform local_to_presentation(0, presentation_rate, 0);
129
130 // Translate the current transform quad so the presentation time units
131 // are the same as the local time units.
johngro 2016/02/23 00:49:42 So, I need to sit with you and have you explain wh
132 success = local_to_presentation.DoReverseTransform(
133 transform->quad->reference_offset,
134 &transform->quad->reference_offset);
135 DCHECK(success)
136 << "LinearTransform::DoReverseTransform reports loss of precision";
137 int64_t presentation_delta;
138 success = local_to_presentation.DoReverseTransform(
139 static_cast<int64_t>(transform->quad->reference_delta),
140 &presentation_delta);
141 DCHECK(success)
142 << "LinearTransform::DoReverseTransform reports loss of precision";
143 transform->quad->reference_delta =
144 static_cast<int32_t>(presentation_delta);
145 LinearTransform::Ratio::Reduce(
146 &transform->quad->reference_delta,
147 &transform->quad->target_delta);
148
149 callback(transform.Clone());
150 });
151 }
152
153 } // namespace media
154 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698