| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 #ifndef REMOTING_BASE_DECODER_H_ | 5 #ifndef REMOTING_BASE_DECODER_H_ |
| 6 #define REMOTING_BASE_DECODER_H_ | 6 #define REMOTING_BASE_DECODER_H_ |
| 7 | 7 |
| 8 #include <vector> | |
| 9 | |
| 10 #include "base/task.h" | 8 #include "base/task.h" |
| 11 #include "base/scoped_ptr.h" | 9 #include "base/scoped_ptr.h" |
| 12 #include "gfx/rect.h" | 10 #include "gfx/rect.h" |
| 13 #include "media/base/video_frame.h" | 11 #include "media/base/video_frame.h" |
| 14 #include "remoting/base/protocol/chromotocol.pb.h" | 12 #include "remoting/base/protocol/chromotocol.pb.h" |
| 15 | 13 |
| 16 namespace remoting { | 14 namespace remoting { |
| 17 | 15 |
| 18 // TODO(hclam): Merge this with the one in remoting/host/encoder.h. | |
| 19 typedef std::vector<gfx::Rect> UpdatedRects; | 16 typedef std::vector<gfx::Rect> UpdatedRects; |
| 20 | 17 |
| 21 // Defines the behavior of a decoder for decoding images received from the | 18 // Interface for a decoder that takes a stream of bytes from the network and |
| 22 // host. | 19 // outputs frames of data. |
| 23 // | 20 // |
| 24 // Sequence of actions with a decoder is as follows: | 21 // TODO(ajwong): Beef up this documentation once the API stablizes. |
| 25 // | |
| 26 // 1. BeginDecode(PartialDecodeDone, DecodeDone, VideoFrame) | |
| 27 // 2. PartialDecode(ChromotingHostMessage) | |
| 28 // ... | |
| 29 // 3. EndDecode() | |
| 30 // | |
| 31 // The decoder will reply with: | |
| 32 // 1. PartialDecodeDone(VideoFrame, UpdatedRects) | |
| 33 // ... | |
| 34 // 2. DecodeDone(VideoFrame) | |
| 35 // | |
| 36 // The format of VideoFrame is a contract between the object that creates the | |
| 37 // decoder (most likely the renderer) and the decoder. | |
| 38 class Decoder { | 22 class Decoder { |
| 39 public: | 23 public: |
| 40 Decoder() | 24 Decoder() {} |
| 41 : encoding_(EncodingInvalid), | 25 virtual ~Decoder() {} |
| 42 started_(false) { | |
| 43 } | |
| 44 virtual ~Decoder() { | |
| 45 } | |
| 46 | 26 |
| 47 // Tell the decoder to use |frame| as a target to write the decoded image | |
| 48 // for the coming update stream. | |
| 49 // If decode is partially done and |frame| can be read, |partial_decode_done| | |
| 50 // is called and |update_rects| contains the updated regions. | |
| 51 // If decode is completed |decode_done| is called. | |
| 52 // Return true if the decoder can writes output to |frame| and accept | |
| 53 // the codec format. | |
| 54 // TODO(hclam): Provide more information when calling this function. | |
| 55 virtual bool BeginDecode(scoped_refptr<media::VideoFrame> frame, | |
| 56 UpdatedRects* updated_rects, | |
| 57 Task* partial_decode_done, | |
| 58 Task* decode_done) = 0; | |
| 59 | |
| 60 // Give a ChromotingHostMessage that contains the update stream packet that | |
| 61 // contains the encoded data to the decoder. | |
| 62 // The decoder will own |message| and is responsible for deleting it. | |
| 63 // | |
| 64 // If the decoder has written something into |frame|, | |
| 65 // |partial_decode_done_| is called with |frame| and updated regions. | |
| 66 // Return true if the decoder can accept |message| and decode it. | |
| 67 // | |
| 68 // ChromotingHostMessage returned by this method will contain a | |
| 69 // UpdateStreamPacketMessage. | |
| 70 // This message will contain either: | |
| 71 // 1. UpdateStreamBeginRect | |
| 72 // 2. UpdateStreamRectData | |
| 73 // 3. UpdateStreamEndRect | |
| 74 // | |
| 75 // See remoting/base/protocol/chromotocol.proto for more information about | |
| 76 // these messages. | |
| 77 virtual bool PartialDecode(ChromotingHostMessage* message) = 0; | |
| 78 | |
| 79 // Notify the decoder that we have received the last update stream packet. | |
| 80 // If the decoding of the update stream has completed |decode_done_| is | |
| 81 // called with |frame|. | |
| 82 // If the update stream is not received fully and this method is called the | |
| 83 // decoder should also call |decode_done_| as soon as possible. | |
| 84 virtual void EndDecode() = 0; | |
| 85 | |
| 86 // Return the encoding type that this decoder handles. | |
| 87 virtual UpdateStreamEncoding Encoding() { return encoding_; } | |
| 88 | |
| 89 // Return the current state of the decoder: 'true' if we're in the middle | |
| 90 // of BeginDecode() / EndDecode(). | |
| 91 virtual bool IsStarted() { return started_; } | |
| 92 | |
| 93 // --- NEW API --- | |
| 94 // TODO(ajwong): This API is incorrect in the face of a streaming decode | 27 // TODO(ajwong): This API is incorrect in the face of a streaming decode |
| 95 // protocol like VP8. However, it breaks the layering abstraction by | 28 // protocol like VP8. However, it breaks the layering abstraction by |
| 96 // depending on the network packet protocol buffer type. I'm going to go | 29 // depending on the network packet protocol buffer type. I'm going to go |
| 97 // forward with it as is, and then refactor again to support streaming | 30 // forward with it as is, and then refactor again to support streaming |
| 98 // decodes. | 31 // decodes. |
| 99 | 32 |
| 100 // Initializes the decoder to draw into the given |frame|. The |clip| | 33 // Initializes the decoder to draw into the given |frame|. The |clip| |
| 101 // specifies the region to draw into. The clip region must fit inside | 34 // specifies the region to draw into. The clip region must fit inside |
| 102 // the dimensions of frame. Failure to do so will CHECK Fail. | 35 // the dimensions of frame. Failure to do so will CHECK Fail. |
| 36 // |
| 37 // TODO(ajwong): Should this take the source pixel format? |
| 38 // TODO(ajwong): Should the protocol be split into basic-types followed |
| 39 // by packet types? Basic types might include the format enum below. |
| 103 virtual void Initialize(scoped_refptr<media::VideoFrame> frame, | 40 virtual void Initialize(scoped_refptr<media::VideoFrame> frame, |
| 104 const gfx::Rect& clip) {} | 41 const gfx::Rect& clip, int bytes_per_src_pixel) = 0; |
| 105 | 42 |
| 106 // Reset the decoder to an uninitialized state. Release all references to | 43 // Reset the decoder to an uninitialized state. Release all references to |
| 107 // the initialized |frame|. Initialize() must be called before the decoder | 44 // the initialized |frame|. Initialize() must be called before the decoder |
| 108 // is used again. | 45 // is used again. |
| 109 virtual void Reset() {} | 46 virtual void Reset() = 0; |
| 110 | 47 |
| 111 // Feeds more data into the decoder. | 48 // Feeds more data into the decoder. |
| 112 virtual void DecodeBytes(const std::string& encoded_bytes) {} | 49 virtual void DecodeBytes(const std::string& encoded_bytes) = 0; |
| 113 | 50 |
| 114 // Returns true if decoder is ready to accept data via ProcessRectangleData. | 51 // Returns true if decoder is ready to accept data via ProcessRectangleData. |
| 115 virtual bool IsReadyForData() { return false; } | 52 virtual bool IsReadyForData() = 0; |
| 116 | 53 |
| 117 protected: | 54 virtual UpdateStreamEncoding Encoding() = 0; |
| 118 // Every decoder will have two internal states because there are three | |
| 119 // kinds of messages send to PartialDecode(). | |
| 120 // | |
| 121 // Here's a state diagram: | |
| 122 // | |
| 123 // UpdateStreamBeginRect UpdateStreamRectData | |
| 124 // .............. ............ | |
| 125 // . . . . | |
| 126 // . v . . | |
| 127 // kWaitingForBeginRect kWaitingForRectData . | |
| 128 // ^ . ^ . | |
| 129 // . . . . | |
| 130 // .............. ............ | |
| 131 // UpdateStreaEndRect | |
| 132 enum State { | |
| 133 // In this state the decoder is waiting for UpdateStreamBeginRect. | |
| 134 // After receiving UpdateStreaBeginRect, the encoder will transit to | |
| 135 // to kWaitingForRectData state. | |
| 136 kWaitingForBeginRect, | |
| 137 | |
| 138 // In this state the decoder is waiting for UpdateStreamRectData. | |
| 139 // The decode remains in this state if UpdateStreamRectData is received. | |
| 140 // The decoder will transit to kWaitingForBeginRect if UpdateStreamEndRect | |
| 141 // is received. | |
| 142 kWaitingForRectData, | |
| 143 }; | |
| 144 | |
| 145 // The encoding that this decoder supports. | |
| 146 UpdateStreamEncoding encoding_; | |
| 147 | |
| 148 // Has the decoder been started? I.e., has BeginDecode() been called. | |
| 149 bool started_; | |
| 150 }; | 55 }; |
| 151 | 56 |
| 152 } // namespace remoting | 57 } // namespace remoting |
| 153 | 58 |
| 154 #endif // REMOTING_BASE_DECODER_H_ | 59 #endif // REMOTING_BASE_DECODER_H_ |
| OLD | NEW |