OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "remoting/protocol/audio_writer.h" | 5 #include "remoting/protocol/audio_writer.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "net/socket/stream_socket.h" | 8 #include "net/socket/stream_socket.h" |
9 #include "remoting/base/constants.h" | 9 #include "remoting/base/constants.h" |
10 #include "remoting/proto/audio.pb.h" | 10 #include "remoting/proto/audio.pb.h" |
11 #include "remoting/protocol/session.h" | 11 #include "remoting/protocol/session.h" |
12 #include "remoting/protocol/session_config.h" | 12 #include "remoting/protocol/session_config.h" |
13 #include "remoting/protocol/util.h" | 13 #include "remoting/protocol/util.h" |
14 | 14 |
15 namespace remoting { | 15 namespace remoting { |
16 namespace protocol { | 16 namespace protocol { |
17 | 17 |
18 AudioWriter::AudioWriter() | 18 AudioWriter::AudioWriter() |
19 : session_(NULL) { | 19 : ChannelDispatcherBase(kAudioChannelName) { |
20 } | 20 } |
21 | 21 |
22 AudioWriter::~AudioWriter() { | 22 AudioWriter::~AudioWriter() { |
23 Close(); | |
24 } | 23 } |
25 | 24 |
26 void AudioWriter::Init(protocol::Session* session, | 25 void AudioWriter::OnInitialized() { |
27 const InitializedCallback& callback) { | 26 // TODO(sergeyu): Provide a non-null WriteFailedCallback for the writer. |
28 session_ = session; | |
29 initialized_callback_ = callback; | |
30 | |
31 session_->CreateStreamChannel( | |
32 kAudioChannelName, | |
33 base::Bind(&AudioWriter::OnChannelReady, base::Unretained(this))); | |
34 } | |
35 | |
36 void AudioWriter::OnChannelReady(scoped_ptr<net::StreamSocket> socket) { | |
37 if (!socket.get()) { | |
38 initialized_callback_.Run(false); | |
39 return; | |
40 } | |
41 | |
42 DCHECK(!channel_.get()); | |
43 channel_ = socket.Pass(); | |
44 // TODO(sergeyu): Provide WriteFailedCallback for the buffered writer. | |
45 buffered_writer_.Init( | 27 buffered_writer_.Init( |
46 channel_.get(), BufferedSocketWriter::WriteFailedCallback()); | 28 channel(), BufferedSocketWriter::WriteFailedCallback()); |
47 | |
48 initialized_callback_.Run(true); | |
49 } | |
50 | |
51 void AudioWriter::Close() { | |
52 buffered_writer_.Close(); | |
53 channel_.reset(); | |
54 if (session_) { | |
55 session_->CancelChannelCreation(kAudioChannelName); | |
56 session_ = NULL; | |
57 } | |
58 } | |
59 | |
60 bool AudioWriter::is_connected() { | |
61 return channel_.get() != NULL; | |
62 } | 29 } |
63 | 30 |
64 void AudioWriter::ProcessAudioPacket(scoped_ptr<AudioPacket> packet, | 31 void AudioWriter::ProcessAudioPacket(scoped_ptr<AudioPacket> packet, |
65 const base::Closure& done) { | 32 const base::Closure& done) { |
66 buffered_writer_.Write(SerializeAndFrameMessage(*packet), done); | 33 buffered_writer_.Write(SerializeAndFrameMessage(*packet), done); |
67 } | 34 } |
68 | 35 |
69 // static | 36 // static |
70 scoped_ptr<AudioWriter> AudioWriter::Create(const SessionConfig& config) { | 37 scoped_ptr<AudioWriter> AudioWriter::Create(const SessionConfig& config) { |
71 if (!config.is_audio_enabled()) | 38 if (!config.is_audio_enabled()) |
72 return scoped_ptr<AudioWriter>(NULL); | 39 return scoped_ptr<AudioWriter>(NULL); |
73 // TODO(kxing): Support different session configurations. | 40 // TODO(kxing): Support different session configurations. |
74 return scoped_ptr<AudioWriter>(new AudioWriter()); | 41 return scoped_ptr<AudioWriter>(new AudioWriter()); |
75 } | 42 } |
76 | 43 |
77 } // namespace protocol | 44 } // namespace protocol |
78 } // namespace remoting | 45 } // namespace remoting |
OLD | NEW |